Commit 791d83a612ac9bbdd185dd3ad8ec7034e40a3959

David Turner 2002-05-27T23:52:01

* include/freetype/internal/tttypes.h, src/sfnt/ttload.c, src/sfnt/sfobjs.c, src/sfnt/sfdriver.c, src/base/ftnames.c: fixing the SFNT name table loader to support various buggy fonts. it now ignores empty name entries, entries with invalid pointer offsets and certain fonts containing tables with broken "storageOffset" fields. name strings are now loaded on demand, which reduces the memory requirements for a given FT_Face tremendously (for example, the name table of Arial.ttf is about 10Kb and contains 70 names !!) finally, this is a _quick_ fix. The whole name table loader and interface will be rewritten in a much more cleanly way shortly, once CSEH have been introduced in the sources.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
diff --git a/ChangeLog b/ChangeLog
index e503f58..0696765 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2002-05-28  David Turner  <david@freetype.org>
+
+    * include/freetype/internal/tttypes.h, src/sfnt/ttload.c,
+      src/sfnt/sfobjs.c, src/sfnt/sfdriver.c, src/base/ftnames.c:
+        fixing the SFNT name table loader to support various buggy fonts.
+        it now ignores empty name entries, entries with invalid pointer
+        offsets and certain fonts containing tables with broken "storageOffset"
+        fields.
+
+        name strings are now loaded on demand, which reduces the memory
+        requirements for a given FT_Face tremendously (for example, the
+        name table of Arial.ttf is about 10Kb and contains 70 names !!)
+
+        finally, this is a _quick_ fix. The whole name table loader and
+        interface will be rewritten in a much more cleanly way shortly,
+        once CSEH have been introduced in the sources.
+
 2002-05-24  Tim Mooney  <enchanter@users.sourceforge.net>
 
 	* builds/unix/ft-munmap.m4: New file, extracted FT_MUNMAP_DECL and
diff --git a/include/freetype/internal/tttypes.h b/include/freetype/internal/tttypes.h
index 7021786..b440aa5 100644
--- a/include/freetype/internal/tttypes.h
+++ b/include/freetype/internal/tttypes.h
@@ -289,7 +289,7 @@ FT_BEGIN_HEADER
     FT_UShort  languageID;
     FT_UShort  nameID;
     FT_UShort  stringLength;
-    FT_UShort  stringOffset;
+    FT_ULong   stringOffset;
 
     /* this last field is not defined in the spec */
     /* but used by the FreeType engine            */
@@ -317,15 +317,15 @@ FT_BEGIN_HEADER
   /*                                                                       */
   /*    names          :: An array of name records.                        */
   /*                                                                       */
-  /*    storage        :: The names storage area.                          */
+  /*    stream         :: the file's input stream.                         */
   /*                                                                       */
   typedef struct  TT_NameTableRec_
   {
     FT_UShort         format;
-    FT_UShort         numNameRecords;
-    FT_UShort         storageOffset;
+    FT_UInt           numNameRecords;
+    FT_UInt           storageOffset;
     TT_NameEntryRec*  names;
-    FT_Byte*          storage;
+    FT_Stream         stream;
 
   } TT_NameTableRec, *TT_NameTable;
 
@@ -1076,7 +1076,7 @@ FT_BEGIN_HEADER
 
     TT_CharMap_Func   get_index;
     TT_CharNext_Func  get_next_char;
-    
+
   } TT_CMapTableRec;
 
 
@@ -1664,7 +1664,7 @@ FT_BEGIN_HEADER
 
     /* for possible extensibility in other formats */
     void*            other;
-    
+
   } TT_LoaderRec;
 
 
diff --git a/src/base/ftnames.c b/src/base/ftnames.c
index 7e9f1ee..9527583 100644
--- a/src/base/ftnames.c
+++ b/src/base/ftnames.c
@@ -53,15 +53,30 @@
 
       if ( idx < (FT_UInt)ttface->num_names )
       {
-        TT_NameEntryRec*  name = ttface->name_table.names + idx;
-
-
-        aname->platform_id = name->platformID;
-        aname->encoding_id = name->encodingID;
-        aname->language_id = name->languageID;
-        aname->name_id     = name->nameID;
-        aname->string      = (FT_Byte*)name->string;
-        aname->string_len  = name->stringLength;
+        TT_NameEntryRec*  entry = ttface->name_table.names + idx;
+
+
+        /* load name on demand */
+        if ( entry->stringLength > 0 && entry->string == NULL )
+        {
+          FT_Memory  memory = face->memory;
+
+
+          if ( FT_NEW_ARRAY     ( entry->string, entry->stringLength ) ||
+               FT_STREAM_SEEK   ( entry->stringOffset )                ||
+               FT_STREAM_READ_AT( entry->string, entry->stringLength ) )
+          {
+            FT_FREE( entry->string );
+            entry->stringLength = 0;
+          }
+        }
+
+        aname->platform_id = entry->platformID;
+        aname->encoding_id = entry->encodingID;
+        aname->language_id = entry->languageID;
+        aname->name_id     = entry->nameID;
+        aname->string      = (FT_Byte*)entry->string;
+        aname->string_len  = entry->stringLength;
 
         error = FT_Err_Ok;
       }
diff --git a/src/sfnt/sfdriver.c b/src/sfnt/sfdriver.c
index fc06ba4..e3a67ec 100644
--- a/src/sfnt/sfdriver.c
+++ b/src/sfnt/sfdriver.c
@@ -124,39 +124,52 @@
     /* either in Macintosh or Windows platform encodings                  */
     found_win   = -1;
     found_apple = -1;
-    
+
     for ( n = 0; n < face->num_names; n++ )
     {
       TT_NameEntryRec*  name = face->name_table.names + n;
 
 
-      if ( name->nameID == 6 && name->string != NULL )
+      if ( name->nameID == 6 && name->stringLength > 0 )
       {
         if ( name->platformID == 3     &&
              name->encodingID == 1     &&
              name->languageID == 0x409 )
           found_win = n;
-          
+
         if ( name->platformID == 1 &&
              name->encodingID == 0 &&
              name->languageID == 0 )
           found_apple = n;
       }
     }
-    
+
     if ( found_win != -1 )
     {
       FT_Memory         memory = face->root.memory;
       TT_NameEntryRec*  name   = face->name_table.names + found_win;
       FT_UInt           len    = name->stringLength / 2;
       FT_Error          error;
-      
 
-      if ( !FT_ALLOC( result, len + 1 ) )
+
+      if ( !FT_ALLOC( result, name->stringLength+1 ) )
       {
+        FT_Stream   stream = face->name_table.stream;
         FT_String*  r = (FT_String*)result;
         FT_Byte*    p = (FT_Byte*)name->string;
-        
+
+
+        if ( FT_STREAM_SEEK( name->stringOffset ) ||
+             FT_FRAME_ENTER( name->stringLength ) )
+        {
+          FT_FREE( result );
+          name->stringLength = 0;
+          name->stringOffset = 0;
+          FT_FREE( name->string );
+          goto Exit;
+        }
+
+        p = (FT_Byte*) stream->cursor;
 
         for ( ; len > 0; len--, p += 2 )
         {
@@ -164,6 +177,8 @@
             *r++ = p[1];
         }
         *r = '\0';
+
+        FT_FRAME_EXIT();
       }
       goto Exit;
     }
@@ -178,10 +193,20 @@
 
       if ( !FT_ALLOC( result, len + 1 ) )
       {
-        FT_MEM_COPY( (char*)result, name->string, len );
+        FT_Stream  stream = face->name_table.stream;
+
+
+        if ( FT_STREAM_SEEK( name->stringOffset ) ||
+             FT_STREAM_READ( result, len )        )
+        {
+          name->stringOffset = 0;
+          name->stringLength = 0;
+          FT_FREE( name->string );
+          FT_FREE( result );
+          goto Exit;
+        }
         ((char*)result)[len] = '\0';
       }
-      goto Exit;
     }
 
   Exit:
diff --git a/src/sfnt/sfobjs.c b/src/sfnt/sfobjs.c
index f9c77bc..b9bbb33 100644
--- a/src/sfnt/sfobjs.c
+++ b/src/sfnt/sfobjs.c
@@ -129,6 +129,9 @@
   }
 
 
+  typedef FT_String*  (*TT_NameEntry_ConvertFunc)( TT_NameEntry  entry,
+                                                   FT_Memory     memory );
+
   /*************************************************************************/
   /*                                                                       */
   /* <Function>                                                            */
@@ -157,6 +160,7 @@
     FT_Int            found_win     = -1;
     FT_Int            found_unicode = -1;
 
+    TT_NameEntry_ConvertFunc  convert;
 
     rec = face->name_table.names;
     for ( n = 0; n < face->num_names; n++, rec++ )
@@ -170,7 +174,7 @@
       /* thing and goes to suggest that all Unicode `name' table entries */
       /* should be coded in UTF-16 (in big-endian format I suppose).     */
       /*                                                                 */
-      if ( rec->nameID == nameid && rec->string )
+      if ( rec->nameID == nameid && rec->stringLength > 0 )
       {
         switch ( rec->platformID )
         {
@@ -218,6 +222,7 @@
     /* some fonts contain invalid Unicode or Macintosh formatted entries; */
     /* we will thus favor names encoded in Windows formats if available   */
     /*                                                                    */
+    convert = NULL;
     if ( found_win >= 0 )
     {
       rec = face->name_table.names + found_win;
@@ -225,11 +230,11 @@
       {
       case TT_MS_ID_UNICODE_CS:
       case TT_MS_ID_SYMBOL_CS:
-        result = tt_name_entry_ascii_from_utf16( rec, memory );
+        convert = tt_name_entry_ascii_from_utf16;
         break;
 
       case TT_MS_ID_UCS_4:
-        result = tt_name_entry_ascii_from_ucs4( rec, memory );
+        convert = tt_name_entry_ascii_from_ucs4;
         break;
 
       default:
@@ -238,15 +243,38 @@
     }
     else if ( found_apple >= 0 )
     {
-      rec    = face->name_table.names + found_apple;
-      result = tt_name_entry_ascii_from_other( rec, memory );
+      rec     = face->name_table.names + found_apple;
+      convert = tt_name_entry_ascii_from_other;
     }
     else if ( found_unicode >= 0 )
     {
-      rec    = face->name_table.names + found_unicode;
-      result = tt_name_entry_ascii_from_utf16( rec, memory );
+      rec     = face->name_table.names + found_unicode;
+      convert = tt_name_entry_ascii_from_utf16;
     }
 
+    if ( rec && convert )
+    {
+      if ( rec->string == NULL )
+      {
+        FT_Error   error;
+        FT_Stream  stream = face->name_table.stream;
+
+
+        if ( FT_NEW_ARRAY  ( rec->string, rec->stringLength ) ||
+             FT_STREAM_SEEK( rec->stringOffset )              ||
+             FT_STREAM_READ( rec->string, rec->stringLength ) )
+        {
+          FT_FREE( rec->string );
+          rec->stringLength = 0;
+          result            = NULL;
+          goto Exit;
+        }
+      }
+
+      result = convert( rec, memory );
+    }
+
+  Exit:
     return result;
   }
 
diff --git a/src/sfnt/ttload.c b/src/sfnt/ttload.c
index 15f2786..d8fed1f 100644
--- a/src/sfnt/ttload.c
+++ b/src/sfnt/ttload.c
@@ -934,14 +934,12 @@
   {
     FT_Error      error;
     FT_Memory     memory = stream->memory;
-
     FT_ULong      table_pos, table_len;
-    FT_ULong      storageOffset, storageSize;
-    FT_Byte*      storage;
-
-    TT_NameTable  names;
+    FT_ULong      storage_start, storage_limit;
+    FT_UInt       count;
+    TT_NameTable  table;
 
-    const FT_Frame_Field  name_table_fields[] =
+    static const FT_Frame_Field  name_table_fields[] =
     {
 #undef  FT_STRUCTURE
 #define FT_STRUCTURE  TT_NameTableRec
@@ -953,7 +951,7 @@
       FT_FRAME_END
     };
 
-    const FT_Frame_Field  name_record_fields[] =
+    static const FT_Frame_Field  name_record_fields[] =
     {
 #undef  FT_STRUCTURE
 #define FT_STRUCTURE  TT_NameEntryRec
@@ -969,6 +967,9 @@
     };
 
 
+    table     = &face->name_table;
+    table->stream = stream;
+
     FT_TRACE2(( "Names " ));
 
     error = face->goto_table( face, TTAG_name, stream, &table_len );
@@ -982,115 +983,74 @@
 
     table_pos = FT_STREAM_POS();
 
-    names = &face->name_table;
 
-    if ( FT_STREAM_READ_FIELDS( name_table_fields, names ) )
+    if ( FT_STREAM_READ_FIELDS( name_table_fields, table ) )
       goto Exit;
 
-    /* check the 'storageOffset' field */
-    storageOffset = names->storageOffset;
+    /* some popular asian fonts have an invalid 'storageOffset' value  */
+    /* (it should be at least "6 + 12*num_names"). However, the string */
+    /* offsets, computed as "storageOffset + entry->stringOffset" are  */
+    /* valid pointers within the name table...                         */
+    /*                                                                 */
+    /* we thus can't check "storageOffset" right now                   */
+    /*                                                                 */
 
-    /* Some broken Asian fonts have a storage offset whose value is */
-    /* 12 * numNameRecords.  We deal with them here.                */
-    if ( storageOffset == (FT_ULong)(12 * names->numNameRecords) )
-      storageOffset += 6;
-    
-    if ( storageOffset <  (FT_ULong)( 6 + 12 * names->numNameRecords ) ||
-         table_len     <= storageOffset                                )
+    storage_start = table_pos + 6 + 12*table->numNameRecords;
+    storage_limit = table_pos + table_len;
+
+    if ( storage_start > storage_limit )
     {
       FT_ERROR(( "TT_Load_Names: invalid `name' table\n" ));
       error = SFNT_Err_Name_Table_Missing;
       goto Exit;
     }
 
-    storageSize = (FT_ULong)(table_len - storageOffset);
-
     /* Allocate the array of name records. */
-    if ( FT_ALLOC( names->names,
-                   names->numNameRecords * sizeof ( TT_NameEntryRec ) +
-                     storageSize )                                      ||
-         FT_FRAME_ENTER( names->numNameRecords * 12L )                  )
-      goto Exit;
+    count                 = table->numNameRecords;
+    table->numNameRecords = 0;
 
-    storage = (FT_Byte*)( names->names + names->numNameRecords );
+    if ( FT_NEW_ARRAY( table->names, count ) ||
+         FT_FRAME_ENTER( count * 12 )        )
+      goto Exit;
 
     /* Load the name records and determine how much storage is needed */
     /* to hold the strings themselves.                                */
     {
-      TT_NameEntryRec*  cur   = names->names;
-      TT_NameEntryRec*  limit = cur + names->numNameRecords;
+      TT_NameEntryRec*  entry = table->names;
 
 
-      for ( ; cur < limit; cur ++ )
+      for ( ; count > 0; count-- )
       {
-        if ( FT_STREAM_READ_FIELDS( name_record_fields, cur ) )
-          break;
+        if ( FT_STREAM_READ_FIELDS( name_record_fields, entry ) )
+          continue;
+
+        /* check that the name is not empty */
+        if ( entry->stringLength == 0 )
+          continue;
 
-        /* invalid name entries will have "cur->string" set to NULL! */
-        if ( (FT_ULong)( cur->stringOffset + cur->stringLength ) < storageSize )
-          cur->string = storage + cur->stringOffset;
-        else
+        /* check that the name string is within the table */
+        entry->stringOffset += table_pos + table->storageOffset;
+        if ( entry->stringOffset                       < storage_start ||
+             entry->stringOffset + entry->stringLength > storage_limit )
         {
-          /* that's an invalid entry */
-          cur->stringOffset = 0;
-          cur->string       = NULL;
+          /* invalid entry - ignore it */
+          entry->stringOffset = 0;
+          entry->stringLength = 0;
+          continue;
         }
+
+        entry++;
       }
+
+      table->numNameRecords = entry - table->names;
     }
 
     FT_FRAME_EXIT();
 
-    if ( error )
-      goto Exit;
-
-    storageOffset -= 6 + 12 * names->numNameRecords;
-    if ( FT_STREAM_SKIP( storageOffset )        ||
-         FT_STREAM_READ( storage, storageSize ) )
-      goto Exit;
-
-#ifdef FT_DEBUG_LEVEL_TRACE
-
-      /* print Name Record Table in case of debugging */
-      {
-        TT_NameEntryRec*  cur   = names->names;
-        TT_NameEntryRec*  limit = cur + names->numNameRecords;
-
-
-        for ( ; cur < limit; cur++ )
-        {
-          FT_UInt  j;
-
-
-          FT_TRACE3(( "(%2d %2d %4x %2d)  ",
-                      cur->platformID,
-                      cur->encodingID,
-                      cur->languageID,
-                      cur->nameID ));
-
-          /* I know that M$ encoded strings are Unicode,            */
-          /* but this works reasonable well for debugging purposes. */
-          if ( cur->string )
-            for ( j = 0; j < (FT_UInt)cur->stringLength; j++ )
-            {
-              FT_Byte  c = *(FT_Byte*)( cur->string + j );
-
-
-              if ( c >= 32 && c < 128 )
-                FT_TRACE3(( "%c", c ));
-            }
-          else
-            FT_TRACE3(( "Invalid entry!\n" ));
-
-          FT_TRACE3(( "\n" ));
-        }
-      }
-
-#endif /* FT_DEBUG_LEVEL_TRACE */
-
     FT_TRACE2(( "loaded\n" ));
 
     /* everything went well, update face->num_names */
-    face->num_names = names->numNameRecords;
+    face->num_names = table->numNameRecords;
 
   Exit:
     return error;
@@ -1112,18 +1072,23 @@
   TT_Free_Names( TT_Face  face )
   {
     FT_Memory     memory = face->root.driver->root.memory;
-    TT_NameTable  names  = &face->name_table;
+    TT_NameTable  table  = &face->name_table;
+    TT_NameEntry  entry  = table->names;
+    FT_UInt       count  = table->numNameRecords;
 
 
-    /* free strings table */
-    FT_FREE( names->names );
+    for ( ; count > 0; count--, entry++ )
+    {
+      FT_FREE( entry->string );
+      entry->stringLength = 0;
+    }
 
-    /* free strings storage */
-    FT_FREE( names->storage );
+    /* free strings table */
+    FT_FREE( table->names );
 
-    names->numNameRecords = 0;
-    names->format         = 0;
-    names->storageOffset  = 0;
+    table->numNameRecords = 0;
+    table->format         = 0;
+    table->storageOffset  = 0;
   }
 
 
@@ -1161,7 +1126,7 @@
       error = SFNT_Err_CMap_Table_Missing;
       goto Exit;
     }
-    
+
     if ( !FT_FRAME_EXTRACT( face->cmap_size, face->cmap_table ) )
       FT_TRACE2(( "`cmap' table loaded\n" ));
     else
@@ -1169,13 +1134,13 @@
       FT_ERROR(( "`cmap' table is too short!\n" ));
       face->cmap_size = 0;
     }
-    
+
   Exit:
     return error;
   }
 
 #else /* !FT_CONFIG_OPTION_USE_CMAPS */
-  
+
   FT_LOCAL_DEF( FT_Error )
   TT_Load_CMap( TT_Face    face,
                 FT_Stream  stream )