Commit 58609c40c8406a7ddc524f61affcdab16c55472d

David Turner 2005-03-15T23:31:48

* docs/CHANGES: updating * src/type1/t1parse.c (T1_New_Parser), src/type42/t42parse.c (t42_parser_init): modifying functions to check the font header before allocating anything on the heap. * internal/freetype/ftmemory.h: introducing the new macros FT_ARRAY_MAX and FT_ARRAY_CHECK * src/pcf/pcfread.c, src/pcf/pcfutil.c: minor fixes and simplifications. try to protect the PCF driver from doing stupid things with broken fonts.

diff --git a/docs/CHANGES b/docs/CHANGES
index d64c675..4ef3c24 100644
--- a/docs/CHANGES
+++ b/docs/CHANGES
@@ -18,10 +18,30 @@ LATEST CHANGES BETWEEN 2.1.10 and 2.1.9
       highly recommended to upgrade.
 
     - FreeType didn't properly parse empty Type 1 glyphs.
+    
+    - An unbounded dynamic buffer growth was fixed in the PFR loader
+    
+    - Several bugs were fixed in the cache sub-system.
+
+    - Fixed bug #12263: incorrect behaviour when resizing to distinct but
+      very close character pixel sizes through FT_Set_Char_Size
+      
+    - The auto-hinter didn't work when a font didn't have a Unicode charmap.
+      it even refused to load the glyphs !
 
 
   II. IMPORTANT CHANGES
 
+    - a LOT of work has been done in order to drastically reduce the
+      amount of heap memory used by FreeType, especially when using
+      memory-mapped font files (which is the default on Unix systems
+      which support them). This should be good news for any Unix distribution
+      upgrading to this release of the font engine.
+
+    - The auto-hinter has been completely rewritten as a new module,
+      called the 'auto-fitter'. The latter is the new default auto-hinting
+      module. It also consumes a lot less memory than its predecessor.
+
     - George Williams  contributed code to read  kerning data from PFM
       files.
 
@@ -53,6 +73,19 @@ LATEST CHANGES BETWEEN 2.1.10 and 2.1.9
     - A new option `--ftversion' has been  added to freetype-config to
       return the FreeType version.
 
+    - The memory debugger has been updated to dump allocation statistics
+      on all allocation sources in the library. This is useful to spot
+      greedy allocations when loading and processing fonts.
+
+    - we removed a huge array of constant pointers to constant strings
+      in the 'psnames' module. The problem was that when compiled in PIC
+      mode (i.e. when generating a Unix shared object / dll), the array
+      could only be placed in the non-shared writable section of the library*
+      since absolute pointers are not relocatable by nature.
+      
+      This saves about 16Kb per process linked to FreeType. We also store
+      the array in a novel compressed way that saves about 20 Kb of code
+      as well.
 
 LATEST CHANGES BETWEEN 2.1.9 and 2.1.8
 
diff --git a/include/freetype/internal/ftmemory.h b/include/freetype/internal/ftmemory.h
index ddd0cca..70e3a3f 100644
--- a/include/freetype/internal/ftmemory.h
+++ b/include/freetype/internal/ftmemory.h
@@ -270,6 +270,13 @@ FT_BEGIN_HEADER
 #define FT_ARRAY_MOVE( dest, source, count )                        \
           FT_MEM_MOVE( dest, source, (count) * sizeof ( *(dest) ) )
 
+/* return the maximum number of adressable elements in an array
+ * we limit ourselves to INT_MAX, rather than UINT_MAX, to avoid
+ * any problems
+ */
+#define FT_ARRAY_MAX( ptr )    (FT_INT_MAX/sizeof( *(ptr) ))
+
+#define FT_ARRAY_CHECK( ptr, count )   ( (count) <= FT_ARRAY_MAX(ptr) )
 
   /*************************************************************************/
   /*                                                                       */
@@ -413,6 +420,7 @@ FT_BEGIN_HEADER
           FT_REALLOC( _pointer, (_old_) * sizeof ( _type_ ),  \
                                 (_new_) * sizeof ( _type_ ) )
 
+
  /* */
 
 
diff --git a/src/pcf/pcfread.c b/src/pcf/pcfread.c
index f31b15e..6596bbf 100644
--- a/src/pcf/pcfread.c
+++ b/src/pcf/pcfread.c
@@ -101,7 +101,8 @@ THE SOFTWARE.
          FT_STREAM_READ_FIELDS ( pcf_toc_header, toc ) )
       return PCF_Err_Cannot_Open_Resource;
 
-    if ( toc->version != PCF_FILE_VERSION )
+    if ( toc->version != PCF_FILE_VERSION                 ||
+         toc->count   >  FT_ARRAY_MAX( face->toc.tables ) )
       return PCF_Err_Invalid_File_Format;
 
     if ( FT_NEW_ARRAY( face->toc.tables, toc->count ) )
diff --git a/src/pcf/pcfutil.c b/src/pcf/pcfutil.c
index 8fcf23d..547b786 100644
--- a/src/pcf/pcfutil.c
+++ b/src/pcf/pcfutil.c
@@ -32,44 +32,6 @@ in this Software without prior written authorization from The Open Group.
 #include "pcfutil.h"
 
 
-  /* Utility functions for reformatting font bitmaps */
-
-  static const unsigned char  _reverse_byte[0x100] =
-  {
-    0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
-    0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
-    0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
-    0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
-    0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
-    0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
-    0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
-    0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
-    0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
-    0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
-    0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
-    0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
-    0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
-    0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
-    0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
-    0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
-    0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
-    0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
-    0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
-    0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
-    0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
-    0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
-    0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
-    0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
-    0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
-    0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
-    0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
-    0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
-    0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
-    0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
-    0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
-    0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
-  };
-
   /*
    *  Invert bit order within each BYTE of an array.
    */
@@ -78,11 +40,16 @@ in this Software without prior written authorization from The Open Group.
   BitOrderInvert( unsigned char*  buf,
                   int             nbytes )
   {
-    const unsigned char*  rev = _reverse_byte;
-
-
     for ( ; --nbytes >= 0; buf++ )
-      *buf = rev[*buf];
+    {
+      unsigned int  val = *buf;
+      
+      val = ((val >> 1) & 0x55) | ((val << 1) & 0xAA);
+      val = ((val >> 2) & 0x33) | ((val << 2) & 0xCC);
+      val = ((val >> 4) & 0x0F) | ((val << 4) & 0xF0);
+      
+      *buf = (unsigned char)val;
+    }
   }
 
 
@@ -97,7 +64,7 @@ in this Software without prior written authorization from The Open Group.
     unsigned char  c;
 
 
-    for ( ; nbytes > 0; nbytes -= 2, buf += 2 )
+    for ( ; nbytes >= 2; nbytes -= 2, buf += 2 )
     {
       c      = buf[0];
       buf[0] = buf[1];
@@ -116,7 +83,7 @@ in this Software without prior written authorization from The Open Group.
     unsigned char  c;
 
 
-    for ( ; nbytes > 0; nbytes -= 4, buf += 4 )
+    for ( ; nbytes >= 4; nbytes -= 4, buf += 4 )
     {
       c      = buf[0];
       buf[0] = buf[3];
diff --git a/src/type1/t1parse.c b/src/type1/t1parse.c
index 501d9f2..0d9072a 100644
--- a/src/type1/t1parse.c
+++ b/src/type1/t1parse.c
@@ -93,6 +93,40 @@
   }
 
 
+  static FT_Error
+  check_type1_format( FT_Stream    stream,
+                      const char*  header_string,
+                      size_t       header_length )
+  {
+    FT_Error   error;
+    FT_UShort  tag;
+    FT_Long    size;
+
+    if ( FT_STREAM_SEEK( 0 ) )
+      goto Exit;
+      
+    error = read_pfb_tag( stream, &tag, &size );
+    if ( error )
+      goto Exit;
+      
+    if ( tag != 0x8001U && FT_STREAM_SEEK( 0 ) )
+      goto Exit;
+      
+    if ( !FT_FRAME_ENTER( header_length ) )
+    {
+      error = 0;
+      
+      if ( ft_memcmp( stream->cursor, header_string, header_length ) != 0 )
+        error = T1_Err_Unknown_File_Format;
+
+      FT_FRAME_EXIT();
+    }
+
+  Exit:
+    return error;
+  }
+
+
   FT_LOCAL_DEF( FT_Error )
   T1_New_Parser( T1_Parser      parser,
                  FT_Stream      stream,
@@ -115,6 +149,21 @@
     parser->in_memory    = 0;
     parser->single_block = 0;
 
+   /* check the header format */
+    error = check_type1_format( stream, "%!PS-AdobeFont-1", 16 );
+    if ( error )
+    {
+      if ( error != T1_Err_Unknown_File_Format )
+        goto Exit;
+
+      error = check_type1_format( stream, "%!FontType", 10 );
+      if ( error )
+      {
+        FT_TRACE2(( "[not a Type1 font]\n" ));
+        goto Exit;
+      }
+    }
+
     /******************************************************************/
     /*                                                                */
     /* Here a short summary of what is going on:                      */
@@ -167,32 +216,16 @@
     }
     else
     {
-      /* read segment in memory */
+      /* read segment in memory - yeah that sucks, but so does the format */
       if ( FT_ALLOC( parser->base_dict, size )     ||
            FT_STREAM_READ( parser->base_dict, size ) )
         goto Exit;
       parser->base_len = size;
     }
 
-    /* Now check font format; we must see `%!PS-AdobeFont-1' */
-    /* or `%!FontType'                                       */
-    {
-      if ( size <= 16                                       ||
-           ( ft_strncmp( (const char*)parser->base_dict,
-                         "%!PS-AdobeFont-1", 16 )        &&
-             ft_strncmp( (const char*)parser->base_dict,
-                         "%!FontType", 10 )              )  )
-      {
-        FT_TRACE2(( "[not a Type1 font]\n" ));
-        error = T1_Err_Unknown_File_Format;
-      }
-      else
-      {
-        parser->root.base   = parser->base_dict;
-        parser->root.cursor = parser->base_dict;
-        parser->root.limit  = parser->root.cursor + parser->base_len;
-      }
-    }
+    parser->root.base   = parser->base_dict;
+    parser->root.cursor = parser->base_dict;
+    parser->root.limit  = parser->root.cursor + parser->base_len;
 
   Exit:
     if ( error && !parser->in_memory )
diff --git a/src/type42/t42parse.c b/src/type42/t42parse.c
index b5139e5..80e7547 100644
--- a/src/type42/t42parse.c
+++ b/src/type42/t42parse.c
@@ -159,7 +159,19 @@
     /*   parser->in_memory is set if we have a memory stream.          */
     /*                                                                 */
 
-    if ( FT_STREAM_SEEK( 0L ) )
+    if ( FT_STREAM_SEEK( 0L ) ||
+         FT_FRAME_ENTER( 17 ) )
+      goto Exit;
+
+    if ( ft_memcmp( stream->cursor, "%!PS-TrueTypeFont", 17 ) != 0 )
+    {
+      FT_TRACE2(( "not a Type42 font\n" ));
+      error = T42_Err_Unknown_File_Format;
+    }
+
+    FT_FRAME_EXIT();
+
+    if ( error || FT_STREAM_SEEK( 0 ) )
       goto Exit;
 
     size = stream->size;
@@ -188,17 +200,9 @@
       parser->base_len = size;
     }
 
-    /* Now check font format; we must see `%!PS-TrueTypeFont' */
-    if ( size <= 17                                    ||
-         ( ft_strncmp( (const char*)parser->base_dict,
-                       "%!PS-TrueTypeFont", 17 ) )     )
-      error = T42_Err_Unknown_File_Format;
-    else
-    {
-      parser->root.base   = parser->base_dict;
-      parser->root.cursor = parser->base_dict;
-      parser->root.limit  = parser->root.cursor + parser->base_len;
-    }
+    parser->root.base   = parser->base_dict;
+    parser->root.cursor = parser->base_dict;
+    parser->root.limit  = parser->root.cursor + parser->base_len;
 
   Exit:
     if ( error && !parser->in_memory )