Commit 03ccfd004fcfd3fab4a0c0cea307bdcb0cd79129

Werner Lemberg 2015-02-22T08:25:16

[pcf] Signedness fixes. * src/pcf/pcf.h, src/pcf/pcfdrivr.c: Apply. * src/pcf/pcfread.c: Apply. (pcf_get_encodings): Ignore invalid negative encoding offsets.

diff --git a/ChangeLog b/ChangeLog
index d4e191c..fac4871 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2015-02-22  Werner Lemberg  <wl@gnu.org>
+
+	[pcf] Signedness fixes.
+
+	* src/pcf/pcf.h, src/pcf/pcfdrivr.c: Apply.
+	* src/pcf/pcfread.c: Apply.
+	(pcf_get_encodings): Ignore invalid negative encoding offsets.
+
 2015-02-21  Werner Lemberg  <wl@gnu.org>
 
 	* src/winfonts/winfnt.c: Signedness fixes.
diff --git a/src/pcf/pcf.h b/src/pcf/pcf.h
index af0ffc3..c0da503 100644
--- a/src/pcf/pcf.h
+++ b/src/pcf/pcf.h
@@ -148,9 +148,9 @@ FT_BEGIN_HEADER
     int            nprops;
     PCF_Property   properties;
 
-    FT_Long        nmetrics;
+    FT_ULong       nmetrics;
     PCF_Metric     metrics;
-    FT_Long        nencodings;
+    FT_ULong       nencodings;
     PCF_Encoding   encodings;
 
     FT_Short       defaultChar;
diff --git a/src/pcf/pcfdrivr.c b/src/pcf/pcfdrivr.c
index 96f6912..09baccf 100644
--- a/src/pcf/pcfdrivr.c
+++ b/src/pcf/pcfdrivr.c
@@ -64,7 +64,7 @@ THE SOFTWARE.
   typedef struct  PCF_CMapRec_
   {
     FT_CMapRec    root;
-    FT_UInt       num_encodings;
+    FT_ULong      num_encodings;
     PCF_Encoding  encodings;
 
   } PCF_CMapRec, *PCF_CMap;
@@ -80,7 +80,7 @@ THE SOFTWARE.
     FT_UNUSED( init_data );
 
 
-    cmap->num_encodings = (FT_UInt)face->nencodings;
+    cmap->num_encodings = face->nencodings;
     cmap->encodings     = face->encodings;
 
     return FT_Err_Ok;
@@ -104,7 +104,7 @@ THE SOFTWARE.
   {
     PCF_CMap      cmap      = (PCF_CMap)pcfcmap;
     PCF_Encoding  encodings = cmap->encodings;
-    FT_UInt       min, max, mid;
+    FT_ULong      min, max, mid;
     FT_UInt       result    = 0;
 
 
@@ -117,7 +117,7 @@ THE SOFTWARE.
 
 
       mid  = ( min + max ) >> 1;
-      code = encodings[mid].enc;
+      code = (FT_ULong)encodings[mid].enc;
 
       if ( charcode == code )
       {
@@ -141,7 +141,7 @@ THE SOFTWARE.
   {
     PCF_CMap      cmap      = (PCF_CMap)pcfcmap;
     PCF_Encoding  encodings = cmap->encodings;
-    FT_UInt       min, max, mid;
+    FT_ULong      min, max, mid;
     FT_ULong      charcode  = *acharcode + 1;
     FT_UInt       result    = 0;
 
@@ -155,7 +155,7 @@ THE SOFTWARE.
 
 
       mid  = ( min + max ) >> 1;
-      code = encodings[mid].enc;
+      code = (FT_ULong)encodings[mid].enc;
 
       if ( charcode == code )
       {
@@ -172,7 +172,7 @@ THE SOFTWARE.
     charcode = 0;
     if ( min < cmap->num_encodings )
     {
-      charcode = encodings[min].enc;
+      charcode = (FT_ULong)encodings[min].enc;
       result   = encodings[min].glyph + 1;
     }
 
@@ -487,7 +487,7 @@ THE SOFTWARE.
     FT_Error    error  = FT_Err_Ok;
     FT_Bitmap*  bitmap = &slot->bitmap;
     PCF_Metric  metric;
-    FT_Offset   bytes;
+    FT_ULong    bytes;
 
     FT_UNUSED( load_flags );
 
@@ -513,8 +513,10 @@ THE SOFTWARE.
 
     metric = face->metrics + glyph_index;
 
-    bitmap->rows       = metric->ascent + metric->descent;
-    bitmap->width      = metric->rightSideBearing - metric->leftSideBearing;
+    bitmap->rows       = (unsigned int)( metric->ascent +
+                                         metric->descent );
+    bitmap->width      = (unsigned int)( metric->rightSideBearing -
+                                         metric->leftSideBearing );
     bitmap->num_grays  = 1;
     bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
 
@@ -526,19 +528,19 @@ THE SOFTWARE.
     switch ( PCF_GLYPH_PAD( face->bitmapsFormat ) )
     {
     case 1:
-      bitmap->pitch = ( bitmap->width + 7 ) >> 3;
+      bitmap->pitch = (int)( ( bitmap->width + 7 ) >> 3 );
       break;
 
     case 2:
-      bitmap->pitch = ( ( bitmap->width + 15 ) >> 4 ) << 1;
+      bitmap->pitch = (int)( ( ( bitmap->width + 15 ) >> 4 ) << 1 );
       break;
 
     case 4:
-      bitmap->pitch = ( ( bitmap->width + 31 ) >> 5 ) << 2;
+      bitmap->pitch = (int)( ( ( bitmap->width + 31 ) >> 5 ) << 2 );
       break;
 
     case 8:
-      bitmap->pitch = ( ( bitmap->width + 63 ) >> 6 ) << 3;
+      bitmap->pitch = (int)( ( ( bitmap->width + 63 ) >> 6 ) << 3 );
       break;
 
     default:
@@ -546,7 +548,7 @@ THE SOFTWARE.
     }
 
     /* XXX: to do: are there cases that need repadding the bitmap? */
-    bytes = bitmap->pitch * bitmap->rows;
+    bytes = (FT_ULong)bitmap->pitch * bitmap->rows;
 
     error = ft_glyphslot_alloc_bitmap( slot, (FT_ULong)bytes );
     if ( error )
@@ -581,12 +583,12 @@ THE SOFTWARE.
     slot->bitmap_left = metric->leftSideBearing;
     slot->bitmap_top  = metric->ascent;
 
-    slot->metrics.horiAdvance  = metric->characterWidth << 6;
-    slot->metrics.horiBearingX = metric->leftSideBearing << 6;
-    slot->metrics.horiBearingY = metric->ascent << 6;
-    slot->metrics.width        = ( metric->rightSideBearing -
-                                   metric->leftSideBearing ) << 6;
-    slot->metrics.height       = bitmap->rows << 6;
+    slot->metrics.horiAdvance  = (FT_Pos)( metric->characterWidth << 6 );
+    slot->metrics.horiBearingX = (FT_Pos)( metric->leftSideBearing << 6 );
+    slot->metrics.horiBearingY = (FT_Pos)( metric->ascent << 6 );
+    slot->metrics.width        = (FT_Pos)( ( metric->rightSideBearing -
+                                             metric->leftSideBearing ) << 6 );
+    slot->metrics.height       = (FT_Pos)( bitmap->rows << 6 );
 
     ft_synthesize_vertical_metrics( &slot->metrics,
                                     ( face->accel.fontAscent +
diff --git a/src/pcf/pcfread.c b/src/pcf/pcfread.c
index a29a9e3..3cdd9ed 100644
--- a/src/pcf/pcfread.c
+++ b/src/pcf/pcfread.c
@@ -485,9 +485,9 @@ THE SOFTWARE.
       goto Bail;
 
     FT_TRACE4(( "  nprop = %d (truncate %d props)\n",
-                (int)nprops, nprops - (int)nprops ));
+                (int)nprops, nprops - (FT_ULong)(int)nprops ));
 
-    nprops = (int)nprops;
+    nprops = (FT_ULong)(int)nprops;
 
     /* rough estimate */
     if ( nprops > size / PCF_PROPERTY_SIZE )
@@ -766,8 +766,7 @@ THE SOFTWARE.
 
     FT_TRACE4(( "  number of bitmaps: %d\n", nbitmaps ));
 
-    /* XXX: PCF_Face->nmetrics is signed FT_Long, see pcf.h */
-    if ( face->nmetrics < 0 || nbitmaps != (FT_ULong)face->nmetrics )
+    if ( nbitmaps != face->nmetrics )
       return FT_THROW( Invalid_File_Format );
 
     if ( FT_NEW_ARRAY( offsets, nbitmaps ) )
@@ -795,9 +794,10 @@ THE SOFTWARE.
       if ( error )
         goto Bail;
 
-      sizebitmaps = bitmapSizes[PCF_GLYPH_PAD_INDEX( format )];
+      sizebitmaps = (FT_ULong)bitmapSizes[PCF_GLYPH_PAD_INDEX( format )];
 
-      FT_TRACE4(( "  padding %d implies a size of %ld\n", i, bitmapSizes[i] ));
+      FT_TRACE4(( "  padding %d implies a size of %ld\n",
+                  i, bitmapSizes[i] ));
     }
 
     FT_TRACE4(( "  %d bitmaps, padding index %ld\n",
@@ -817,7 +817,7 @@ THE SOFTWARE.
                     " invalid offset to bitmap data of glyph %d\n", i ));
       }
       else
-        face->metrics[i].bits = stream->pos + offsets[i];
+        face->metrics[i].bits = stream->pos + (FT_ULong)offsets[i];
     }
 
     face->bitmapsFormat = format;
@@ -837,8 +837,10 @@ THE SOFTWARE.
     FT_ULong      format, size;
     int           firstCol, lastCol;
     int           firstRow, lastRow;
-    int           nencoding, encodingOffset;
-    int           i, j, k;
+    FT_ULong      nencoding;
+    int           encodingOffset;
+    int           i, j;
+    FT_ULong      k;
     PCF_Encoding  encoding = NULL;
 
 
@@ -893,7 +895,8 @@ THE SOFTWARE.
     FT_TRACE4(( "  firstCol %d, lastCol %d, firstRow %d, lastRow %d\n",
                 firstCol, lastCol, firstRow, lastRow ));
 
-    nencoding = ( lastCol - firstCol + 1 ) * ( lastRow - firstRow + 1 );
+    nencoding = (FT_ULong)( lastCol - firstCol + 1 ) *
+                (FT_ULong)( lastRow - firstRow + 1 );
 
     if ( FT_NEW_ARRAY( encoding, nencoding ) )
       return FT_THROW( Out_Of_Memory );
@@ -912,10 +915,10 @@ THE SOFTWARE.
         else
           encodingOffset = FT_GET_SHORT_LE();
 
-        if ( encodingOffset != -1 )
+        if ( encodingOffset > -1 )
         {
           encoding[k].enc   = i * 256 + j;
-          encoding[k].glyph = (FT_Short)encodingOffset;
+          encoding[k].glyph = (FT_UShort)encodingOffset;
 
           FT_TRACE5(( "  code %d (0x%04X): idx %d\n",
                       encoding[k].enc, encoding[k].enc, encoding[k].glyph ));
@@ -1256,7 +1259,7 @@ THE SOFTWARE.
        *
        * This implies bumping the number of `available' glyphs by 1.
        */
-      root->num_glyphs = face->nmetrics + 1;
+      root->num_glyphs = (FT_Long)( face->nmetrics + 1 );
 
       root->num_fixed_sizes = 1;
       if ( FT_NEW_ARRAY( root->available_sizes, 1 ) )