static FT_Error check_table_dir( SFNT_Header sfnt, FT_Stream stream ) { FT_Error error; FT_UShort nn, valid_entries = 0; FT_UInt has_head = 0, has_sing = 0, has_meta = 0; FT_ULong offset = sfnt->offset + 12; static const FT_Frame_Field table_dir_entry_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE TT_TableRec FT_FRAME_START( 16 ), FT_FRAME_ULONG( Tag ), FT_FRAME_ULONG( CheckSum ), FT_FRAME_ULONG( Offset ), FT_FRAME_ULONG( Length ), FT_FRAME_END }; if ( FT_STREAM_SEEK( offset ) ) goto Exit; for ( nn = 0; nn < sfnt->num_tables; nn++ ) { TT_TableRec table; if ( FT_STREAM_READ_FIELDS( table_dir_entry_fields, &table ) ) { nn--; FT_TRACE2(( "check_table_dir:" " can read only %d table%s in font (instead of %d)\n", nn, nn == 1 ? "" : "s", sfnt->num_tables )); sfnt->num_tables = nn; break; } /* we ignore invalid tables */ if ( table.Offset + table.Length > stream->size ) { //BUGID: 53876, the cmap table is invalid, the font file couldn't be used. if (table.Tag == TTAG_cmap) break; FT_TRACE2(( "check_table_dir: table entry %d invalid\n", nn )); continue; } else valid_entries++; if ( table.Tag == TTAG_head || table.Tag == TTAG_bhed ) { FT_UInt32 magic; #ifndef TT_CONFIG_OPTION_EMBEDDED_BITMAPS if ( table.Tag == TTAG_head ) #endif has_head = 1; /* * The table length should be 0x36, but certain font tools make it * 0x38, so we will just check that it is greater. * * Note that according to the specification, the table must be * padded to 32-bit lengths, but this doesn't apply to the value of * its `Length' field! * */ if ( table.Length < 0x36 ) { FT_TRACE2(( "check_table_dir: `head' table too small\n" )); error = FT_THROW( Table_Missing ); goto Exit; } if ( FT_STREAM_SEEK( table.Offset + 12 ) || FT_READ_ULONG( magic ) ) goto Exit; if ( magic != 0x5F0F3CF5UL ) { FT_TRACE2(( "check_table_dir:" " no magic number found in `head' table\n")); error = FT_THROW( Table_Missing ); goto Exit; } if ( FT_STREAM_SEEK( offset + ( nn + 1 ) * 16 ) ) goto Exit; } else if ( table.Tag == TTAG_SING ) has_sing = 1; else if ( table.Tag == TTAG_META ) has_meta = 1; } sfnt->num_tables = valid_entries; if ( sfnt->num_tables == 0 ) { FT_TRACE2(( "check_table_dir: no tables found\n" )); error = FT_THROW( Unknown_File_Format ); goto Exit; } /* if `sing' and `meta' tables are present, there is no `head' table */ if ( has_head || ( has_sing && has_meta ) ) { error = FT_Err_Ok; goto Exit; } else { FT_TRACE2(( "check_table_dir:" )); #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS FT_TRACE2(( " neither `head', `bhed', nor `sing' table found\n" )); #else FT_TRACE2(( " neither `head' nor `sing' table found\n" )); #endif error = FT_THROW( Table_Missing ); } Exit: return error; }
tt_face_load_font_dir( TT_Face face, FT_Stream stream ) { SFNT_HeaderRec sfnt; FT_Error error; FT_Memory memory = stream->memory; TT_TableRec* entry; FT_Int nn; static const FT_Frame_Field offset_table_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE SFNT_HeaderRec FT_FRAME_START( 8 ), FT_FRAME_USHORT( num_tables ), FT_FRAME_USHORT( search_range ), FT_FRAME_USHORT( entry_selector ), FT_FRAME_USHORT( range_shift ), FT_FRAME_END }; FT_TRACE2(( "tt_face_load_font_dir: %08p\n", face )); /* read the offset table */ sfnt.offset = FT_STREAM_POS(); if ( FT_READ_ULONG( sfnt.format_tag ) || FT_STREAM_READ_FIELDS( offset_table_fields, &sfnt ) ) goto Exit; /* many fonts don't have these fields set correctly */ #if 0 if ( sfnt.search_range != 1 << ( sfnt.entry_selector + 4 ) || sfnt.search_range + sfnt.range_shift != sfnt.num_tables << 4 ) return FT_THROW( Unknown_File_Format ); #endif /* load the table directory */ FT_TRACE2(( "-- Number of tables: %10u\n", sfnt.num_tables )); FT_TRACE2(( "-- Format version: 0x%08lx\n", sfnt.format_tag )); if ( sfnt.format_tag != TTAG_OTTO ) { /* check first */ error = check_table_dir( &sfnt, stream ); if ( error ) { FT_TRACE2(( "tt_face_load_font_dir:" " invalid table directory for TrueType\n" )); goto Exit; } } face->num_tables = sfnt.num_tables; face->format_tag = sfnt.format_tag; if ( FT_QNEW_ARRAY( face->dir_tables, face->num_tables ) ) goto Exit; if ( FT_STREAM_SEEK( sfnt.offset + 12 ) || FT_FRAME_ENTER( face->num_tables * 16L ) ) goto Exit; entry = face->dir_tables; FT_TRACE2(( "\n" " tag offset length checksum\n" " ----------------------------------\n" )); for ( nn = 0; nn < sfnt.num_tables; nn++ ) { entry->Tag = FT_GET_TAG4(); entry->CheckSum = FT_GET_ULONG(); entry->Offset = FT_GET_LONG(); entry->Length = FT_GET_LONG(); /* ignore invalid tables */ if ( entry->Offset + entry->Length > stream->size ) continue; else { FT_TRACE2(( " %c%c%c%c %08lx %08lx %08lx\n", (FT_Char)( entry->Tag >> 24 ), (FT_Char)( entry->Tag >> 16 ), (FT_Char)( entry->Tag >> 8 ), (FT_Char)( entry->Tag ), entry->Offset, entry->Length, entry->CheckSum )); entry++; } } FT_FRAME_EXIT(); FT_TRACE2(( "table directory loaded\n\n" )); Exit: return error; }
static FT_Error fnt_face_get_dll_fonts( FNT_Face face ) { FT_Error error; FT_Stream stream = FT_FACE( face )->stream; FT_Memory memory = FT_FACE( face )->memory; WinMZ_HeaderRec mz_header; face->fonts = 0; face->num_fonts = 0; /* does it begin with a MZ header? */ if ( FT_STREAM_SEEK( 0 ) || FT_STREAM_READ_FIELDS( winmz_header_fields, &mz_header ) ) goto Exit; error = FNT_Err_Unknown_File_Format; if ( mz_header.magic == WINFNT_MZ_MAGIC ) { /* yes, now look for a NE header in the file */ WinNE_HeaderRec ne_header; if ( FT_STREAM_SEEK( mz_header.lfanew ) || FT_STREAM_READ_FIELDS( winne_header_fields, &ne_header ) ) goto Exit; error = FNT_Err_Unknown_File_Format; if ( ne_header.magic == WINFNT_NE_MAGIC ) { /* good, now look in the resource table for each FNT resource */ FT_ULong res_offset = mz_header.lfanew + ne_header.resource_tab_offset; FT_UShort size_shift; FT_UShort font_count = 0; FT_ULong font_offset = 0; if ( FT_STREAM_SEEK( res_offset ) || FT_FRAME_ENTER( ne_header.rname_tab_offset - ne_header.resource_tab_offset ) ) goto Exit; size_shift = FT_GET_USHORT_LE(); for (;;) { FT_UShort type_id, count; type_id = FT_GET_USHORT_LE(); if ( !type_id ) break; count = FT_GET_USHORT_LE(); if ( type_id == 0x8008 ) { font_count = count; font_offset = (FT_ULong)( FT_STREAM_POS() + 4 + ( stream->cursor - stream->limit ) ); break; } stream->cursor += 4 + count * 12; } FT_FRAME_EXIT(); if ( !font_count || !font_offset ) { FT_TRACE2(( "this file doesn't contain any FNT resources!\n" )); error = FNT_Err_Unknown_File_Format; goto Exit; } if ( FT_STREAM_SEEK( font_offset ) || FT_NEW_ARRAY( face->fonts, font_count ) ) goto Exit; face->num_fonts = font_count; if ( FT_FRAME_ENTER( (FT_Long)font_count * 12 ) ) goto Exit; /* now read the offset and position of each FNT font */ { FNT_Font cur = face->fonts; FNT_Font limit = cur + font_count; for ( ; cur < limit; cur++ ) { cur->offset = (FT_ULong)FT_GET_USHORT_LE() << size_shift; cur->fnt_size = (FT_ULong)FT_GET_USHORT_LE() << size_shift; cur->size_shift = size_shift; stream->cursor += 8; } } FT_FRAME_EXIT(); /* finally, try to load each font there */ { FNT_Font cur = face->fonts; FNT_Font limit = cur + font_count; for ( ; cur < limit; cur++ ) { error = fnt_font_load( cur, stream ); if ( error ) goto Fail; } } } } Fail: if ( error ) fnt_face_done_fonts( face ); Exit: return error; }
/* synthesized into a TTC with one offset table. */ static FT_Error sfnt_open_font( FT_Stream stream, TT_Face face ) { FT_Memory memory = stream->memory; FT_Error error; FT_ULong tag, offset; static const FT_Frame_Field ttc_header_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE TTC_HeaderRec FT_FRAME_START( 8 ), FT_FRAME_LONG( version ), FT_FRAME_LONG( count ), FT_FRAME_END }; face->ttc_header.tag = 0; face->ttc_header.version = 0; face->ttc_header.count = 0; offset = FT_STREAM_POS(); if ( FT_READ_ULONG( tag ) ) return error; if ( tag != 0x00010000UL && tag != TTAG_ttcf && tag != FT_MAKE_TAG( 'O', 'T', 'T', 'O' ) && tag != TTAG_true && tag != 0x00020000UL ) return SFNT_Err_Unknown_File_Format; face->ttc_header.tag = TTAG_ttcf; if ( tag == TTAG_ttcf ) { FT_Int n; FT_TRACE3(( "sfnt_open_font: file is a collection\n" )); if ( FT_STREAM_READ_FIELDS( ttc_header_fields, &face->ttc_header ) ) return error; /* now read the offsets of each font in the file */ if ( FT_NEW_ARRAY( face->ttc_header.offsets, face->ttc_header.count ) ) return error; if ( FT_FRAME_ENTER( face->ttc_header.count * 4L ) ) return error; for ( n = 0; n < face->ttc_header.count; n++ ) face->ttc_header.offsets[n] = FT_GET_ULONG(); FT_FRAME_EXIT(); } else { FT_TRACE3(( "sfnt_open_font: synthesize TTC\n" )); face->ttc_header.version = 1 << 16; face->ttc_header.count = 1; if ( FT_NEW( face->ttc_header.offsets) ) return error; face->ttc_header.offsets[0] = offset; } return error; }
static FT_Error pcf_read_TOC( FT_Stream stream, PCF_Face face ) { FT_Error error; PCF_Toc toc = &face->toc; PCF_Table tables; FT_Memory memory = FT_FACE(face)->memory; FT_UInt n; if ( FT_STREAM_SEEK ( 0 ) || FT_STREAM_READ_FIELDS ( pcf_toc_header, toc ) ) return PCF_Err_Cannot_Open_Resource; if ( toc->version != PCF_FILE_VERSION || toc->count > FT_ARRAY_MAX( face->toc.tables ) || toc->count == 0 ) return PCF_Err_Invalid_File_Format; if ( FT_NEW_ARRAY( face->toc.tables, toc->count ) ) return PCF_Err_Out_Of_Memory; tables = face->toc.tables; for ( n = 0; n < toc->count; n++ ) { if ( FT_STREAM_READ_FIELDS( pcf_table_header, tables ) ) goto Exit; tables++; } /* Sort tables and check for overlaps. Because they are almost */ /* always ordered already, an in-place bubble sort with simultaneous */ /* boundary checking seems appropriate. */ tables = face->toc.tables; for ( n = 0; n < toc->count - 1; n++ ) { FT_UInt i, have_change; have_change = 0; for ( i = 0; i < toc->count - 1 - n; i++ ) { PCF_TableRec tmp; if ( tables[i].offset > tables[i + 1].offset ) { tmp = tables[i]; tables[i] = tables[i + 1]; tables[i + 1] = tmp; have_change = 1; } if ( ( tables[i].size > tables[i + 1].offset ) || ( tables[i].offset > tables[i + 1].offset - tables[i].size ) ) return PCF_Err_Invalid_Offset; } if ( !have_change ) break; } #ifdef FT_DEBUG_LEVEL_TRACE { FT_UInt i, j; const char* name = "?"; FT_TRACE4(( "pcf_read_TOC:\n" )); FT_TRACE4(( " number of tables: %ld\n", face->toc.count )); tables = face->toc.tables; for ( i = 0; i < toc->count; i++ ) { for ( j = 0; j < sizeof ( tableNames ) / sizeof ( tableNames[0] ); j++ ) if ( tables[i].type == (FT_UInt)( 1 << j ) ) name = tableNames[j]; FT_TRACE4(( " %d: type=%s, format=0x%X, " "size=%ld (0x%lX), offset=%ld (0x%lX)\n", i, name, tables[i].format, tables[i].size, tables[i].size, tables[i].offset, tables[i].offset )); } } #endif return PCF_Err_Ok; Exit: FT_FREE( face->toc.tables ); return error; }
static FT_Error pcf_read_TOC( FT_Stream stream, PCF_Face face ) { FT_Error error; PCF_Toc toc = &face->toc; PCF_Table tables; FT_Memory memory = FT_FACE( face )->memory; FT_UInt n; FT_ULong size; if ( FT_STREAM_SEEK( 0 ) || FT_STREAM_READ_FIELDS( pcf_toc_header, toc ) ) return FT_THROW( Cannot_Open_Resource ); if ( toc->version != PCF_FILE_VERSION || toc->count > FT_ARRAY_MAX( face->toc.tables ) || toc->count == 0 ) return FT_THROW( Invalid_File_Format ); if ( FT_NEW_ARRAY( face->toc.tables, toc->count ) ) return FT_THROW( Out_Of_Memory ); tables = face->toc.tables; for ( n = 0; n < toc->count; n++ ) { if ( FT_STREAM_READ_FIELDS( pcf_table_header, tables ) ) goto Exit; tables++; } /* Sort tables and check for overlaps. Because they are almost */ /* always ordered already, an in-place bubble sort with simultaneous */ /* boundary checking seems appropriate. */ tables = face->toc.tables; for ( n = 0; n < toc->count - 1; n++ ) { FT_UInt i, have_change; have_change = 0; for ( i = 0; i < toc->count - 1 - n; i++ ) { PCF_TableRec tmp; if ( tables[i].offset > tables[i + 1].offset ) { tmp = tables[i]; tables[i] = tables[i + 1]; tables[i + 1] = tmp; have_change = 1; } if ( ( tables[i].size > tables[i + 1].offset ) || ( tables[i].offset > tables[i + 1].offset - tables[i].size ) ) { error = FT_THROW( Invalid_Offset ); goto Exit; } } if ( !have_change ) break; } /* * We now check whether the `size' and `offset' values are reasonable: * `offset' + `size' must not exceed the stream size. * * Note, however, that X11's `pcfWriteFont' routine (used by the * `bdftopcf' program to create PDF font files) has two special * features. * * - It always assigns the accelerator table a size of 100 bytes in the * TOC, regardless of its real size, which can vary between 34 and 72 * bytes. * * - Due to the way the routine is designed, it ships out the last font * table with its real size, ignoring the TOC's size value. Since * the TOC size values are always rounded up to a multiple of 4, the * difference can be up to three bytes for all tables except the * accelerator table, for which the difference can be as large as 66 * bytes. * */ tables = face->toc.tables; size = stream->size; for ( n = 0; n < toc->count - 1; n++ ) { /* we need two checks to avoid overflow */ if ( ( tables->size > size ) || ( tables->offset > size - tables->size ) ) { error = FT_THROW( Invalid_Table ); goto Exit; } tables++; } /* only check `tables->offset' for last table element ... */ if ( ( tables->offset > size ) ) { error = FT_THROW( Invalid_Table ); goto Exit; } /* ... and adjust `tables->size' to the real value if necessary */ if ( tables->size > size - tables->offset ) tables->size = size - tables->offset; #ifdef FT_DEBUG_LEVEL_TRACE { FT_UInt i, j; const char* name = "?"; FT_TRACE4(( "pcf_read_TOC:\n" )); FT_TRACE4(( " number of tables: %ld\n", face->toc.count )); tables = face->toc.tables; for ( i = 0; i < toc->count; i++ ) { for ( j = 0; j < sizeof ( tableNames ) / sizeof ( tableNames[0] ); j++ ) if ( tables[i].type == (FT_UInt)( 1 << j ) ) name = tableNames[j]; FT_TRACE4(( " %d: type=%s, format=0x%X, " "size=%ld (0x%lX), offset=%ld (0x%lX)\n", i, name, tables[i].format, tables[i].size, tables[i].size, tables[i].offset, tables[i].offset )); } } #endif return FT_Err_Ok; Exit: FT_FREE( face->toc.tables ); return error; }
/* synthesized into a TTC with one offset table. */ static FT_Error sfnt_open_font( FT_Stream stream, TT_Face face ) { FT_Memory memory = stream->memory; FT_Error error; FT_ULong tag, offset; static const FT_Frame_Field ttc_header_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE TTC_HeaderRec FT_FRAME_START( 8 ), FT_FRAME_LONG( version ), FT_FRAME_LONG( count ), /* this is ULong in the specs */ FT_FRAME_END }; face->ttc_header.tag = 0; face->ttc_header.version = 0; face->ttc_header.count = 0; offset = FT_STREAM_POS(); if ( FT_READ_ULONG( tag ) ) return error; if ( tag != 0x00010000UL && tag != TTAG_ttcf && tag != TTAG_OTTO && tag != TTAG_true && tag != TTAG_typ1 && tag != 0x00020000UL ) { FT_TRACE2(( " not a font using the SFNT container format\n" )); return FT_THROW( Unknown_File_Format ); } face->ttc_header.tag = TTAG_ttcf; if ( tag == TTAG_ttcf ) { FT_Int n; FT_TRACE3(( "sfnt_open_font: file is a collection\n" )); if ( FT_STREAM_READ_FIELDS( ttc_header_fields, &face->ttc_header ) ) return error; if ( face->ttc_header.count == 0 ) return FT_THROW( Invalid_Table ); /* a rough size estimate: let's conservatively assume that there */ /* is just a single table info in each subfont header (12 + 16*1 = */ /* 28 bytes), thus we have (at least) `12 + 4*count' bytes for the */ /* size of the TTC header plus `28*count' bytes for all subfont */ /* headers */ if ( (FT_ULong)face->ttc_header.count > stream->size / ( 28 + 4 ) ) return FT_THROW( Array_Too_Large ); /* now read the offsets of each font in the file */ if ( FT_NEW_ARRAY( face->ttc_header.offsets, face->ttc_header.count ) ) return error; if ( FT_FRAME_ENTER( face->ttc_header.count * 4L ) ) return error; for ( n = 0; n < face->ttc_header.count; n++ ) face->ttc_header.offsets[n] = FT_GET_ULONG(); FT_FRAME_EXIT(); } else { FT_TRACE3(( "sfnt_open_font: synthesize TTC\n" )); face->ttc_header.version = 1 << 16; face->ttc_header.count = 1; if ( FT_NEW( face->ttc_header.offsets ) ) return error; face->ttc_header.offsets[0] = offset; } return error; }
tt_face_load_os2( TT_Face face, FT_Stream stream ) { FT_Error error; TT_OS2* os2; static const FT_Frame_Field os2_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE TT_OS2 FT_FRAME_START( 78 ), FT_FRAME_USHORT( version ), FT_FRAME_SHORT ( xAvgCharWidth ), FT_FRAME_USHORT( usWeightClass ), FT_FRAME_USHORT( usWidthClass ), FT_FRAME_SHORT ( fsType ), FT_FRAME_SHORT ( ySubscriptXSize ), FT_FRAME_SHORT ( ySubscriptYSize ), FT_FRAME_SHORT ( ySubscriptXOffset ), FT_FRAME_SHORT ( ySubscriptYOffset ), FT_FRAME_SHORT ( ySuperscriptXSize ), FT_FRAME_SHORT ( ySuperscriptYSize ), FT_FRAME_SHORT ( ySuperscriptXOffset ), FT_FRAME_SHORT ( ySuperscriptYOffset ), FT_FRAME_SHORT ( yStrikeoutSize ), FT_FRAME_SHORT ( yStrikeoutPosition ), FT_FRAME_SHORT ( sFamilyClass ), FT_FRAME_BYTE ( panose[0] ), FT_FRAME_BYTE ( panose[1] ), FT_FRAME_BYTE ( panose[2] ), FT_FRAME_BYTE ( panose[3] ), FT_FRAME_BYTE ( panose[4] ), FT_FRAME_BYTE ( panose[5] ), FT_FRAME_BYTE ( panose[6] ), FT_FRAME_BYTE ( panose[7] ), FT_FRAME_BYTE ( panose[8] ), FT_FRAME_BYTE ( panose[9] ), FT_FRAME_ULONG ( ulUnicodeRange1 ), FT_FRAME_ULONG ( ulUnicodeRange2 ), FT_FRAME_ULONG ( ulUnicodeRange3 ), FT_FRAME_ULONG ( ulUnicodeRange4 ), FT_FRAME_BYTE ( achVendID[0] ), FT_FRAME_BYTE ( achVendID[1] ), FT_FRAME_BYTE ( achVendID[2] ), FT_FRAME_BYTE ( achVendID[3] ), FT_FRAME_USHORT( fsSelection ), FT_FRAME_USHORT( usFirstCharIndex ), FT_FRAME_USHORT( usLastCharIndex ), FT_FRAME_SHORT ( sTypoAscender ), FT_FRAME_SHORT ( sTypoDescender ), FT_FRAME_SHORT ( sTypoLineGap ), FT_FRAME_USHORT( usWinAscent ), FT_FRAME_USHORT( usWinDescent ), FT_FRAME_END }; static const FT_Frame_Field os2_fields_extra[] = { FT_FRAME_START( 8 ), FT_FRAME_ULONG( ulCodePageRange1 ), FT_FRAME_ULONG( ulCodePageRange2 ), FT_FRAME_END }; static const FT_Frame_Field os2_fields_extra2[] = { FT_FRAME_START( 10 ), FT_FRAME_SHORT ( sxHeight ), FT_FRAME_SHORT ( sCapHeight ), FT_FRAME_USHORT( usDefaultChar ), FT_FRAME_USHORT( usBreakChar ), FT_FRAME_USHORT( usMaxContext ), FT_FRAME_END }; /* We now support old Mac fonts where the OS/2 table doesn't */ /* exist. Simply put, we set the `version' field to 0xFFFF */ /* and test this value each time we need to access the table. */ error = face->goto_table( face, TTAG_OS2, stream, 0 ); if ( error ) goto Exit; os2 = &face->os2; if ( FT_STREAM_READ_FIELDS( os2_fields, os2 ) ) goto Exit; os2->ulCodePageRange1 = 0; os2->ulCodePageRange2 = 0; os2->sxHeight = 0; os2->sCapHeight = 0; os2->usDefaultChar = 0; os2->usBreakChar = 0; os2->usMaxContext = 0; if ( os2->version >= 0x0001 ) { /* only version 1 tables */ if ( FT_STREAM_READ_FIELDS( os2_fields_extra, os2 ) ) goto Exit; if ( os2->version >= 0x0002 ) { /* only version 2 tables */ if ( FT_STREAM_READ_FIELDS( os2_fields_extra2, os2 ) ) goto Exit; } } FT_TRACE3(( "sTypoAscender: %4d\n", os2->sTypoAscender )); FT_TRACE3(( "sTypoDescender: %4d\n", os2->sTypoDescender )); FT_TRACE3(( "usWinAscent: %4u\n", os2->usWinAscent )); FT_TRACE3(( "usWinDescent: %4u\n", os2->usWinDescent )); FT_TRACE3(( "fsSelection: 0x%2x\n", os2->fsSelection )); Exit: /*return error;*/ return 0; /* XYQ 2007-11-23 We can't just quit if OS/2 table can't be loaded. TESTDOC: Bug #3160 - MyDoc.pdf */ }
tt_face_load_maxp( TT_Face face, FT_Stream stream ) { FT_Error error; TT_MaxProfile* maxProfile = &face->max_profile; const FT_Frame_Field maxp_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE TT_MaxProfile FT_FRAME_START( 6 ), FT_FRAME_LONG ( version ), FT_FRAME_USHORT( numGlyphs ), FT_FRAME_END }; const FT_Frame_Field maxp_fields_extra[] = { FT_FRAME_START( 26 ), FT_FRAME_USHORT( maxPoints ), FT_FRAME_USHORT( maxContours ), FT_FRAME_USHORT( maxCompositePoints ), FT_FRAME_USHORT( maxCompositeContours ), FT_FRAME_USHORT( maxZones ), FT_FRAME_USHORT( maxTwilightPoints ), FT_FRAME_USHORT( maxStorage ), FT_FRAME_USHORT( maxFunctionDefs ), FT_FRAME_USHORT( maxInstructionDefs ), FT_FRAME_USHORT( maxStackElements ), FT_FRAME_USHORT( maxSizeOfInstructions ), FT_FRAME_USHORT( maxComponentElements ), FT_FRAME_USHORT( maxComponentDepth ), FT_FRAME_END }; error = face->goto_table( face, TTAG_maxp, stream, 0 ); if ( error ) goto Exit; if ( FT_STREAM_READ_FIELDS( maxp_fields, maxProfile ) ) goto Exit; maxProfile->maxPoints = 0; maxProfile->maxContours = 0; maxProfile->maxCompositePoints = 0; maxProfile->maxCompositeContours = 0; maxProfile->maxZones = 0; maxProfile->maxTwilightPoints = 0; maxProfile->maxStorage = 0; maxProfile->maxFunctionDefs = 0; maxProfile->maxInstructionDefs = 0; maxProfile->maxStackElements = 0; maxProfile->maxSizeOfInstructions = 0; maxProfile->maxComponentElements = 0; maxProfile->maxComponentDepth = 0; if ( maxProfile->version >= 0x10000L ) { if ( FT_STREAM_READ_FIELDS( maxp_fields_extra, maxProfile ) ) goto Exit; /* XXX: an adjustment that is necessary to load certain */ /* broken fonts like `Keystrokes MT' :-( */ /* */ /* We allocate 64 function entries by default when */ /* the maxFunctionDefs field is null. */ if ( maxProfile->maxFunctionDefs == 0 ) maxProfile->maxFunctionDefs = 64; } FT_TRACE3(( "numGlyphs: %u\n", maxProfile->numGlyphs )); Exit: return error; }
cff_font_load( FT_Stream stream, FT_Int face_index, CFF_Font font ) { static const FT_Frame_Field cff_header_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE CFF_FontRec FT_FRAME_START( 4 ), FT_FRAME_BYTE( version_major ), FT_FRAME_BYTE( version_minor ), FT_FRAME_BYTE( header_size ), FT_FRAME_BYTE( absolute_offsize ), FT_FRAME_END }; FT_Error error; FT_Memory memory = stream->memory; FT_ULong base_offset; CFF_FontRecDict dict; FT_ZERO( font ); font->stream = stream; font->memory = memory; dict = &font->top_font.font_dict; base_offset = FT_STREAM_POS(); /* read CFF font header */ if ( FT_STREAM_READ_FIELDS( cff_header_fields, font ) ) goto Exit; /* check format */ if ( font->version_major != 1 || font->header_size < 4 || font->absolute_offsize > 4 ) { FT_TRACE2(( "[not a CFF font header!]\n" )); error = CFF_Err_Unknown_File_Format; goto Exit; } /* skip the rest of the header */ if ( FT_STREAM_SKIP( font->header_size - 4 ) ) goto Exit; /* read the name, top dict, string and global subrs index */ if ( FT_SET_ERROR( cff_new_index( &font->name_index, stream, 0 )) || FT_SET_ERROR( cff_new_index( &font->font_dict_index, stream, 0 )) || FT_SET_ERROR( cff_new_index( &font->string_index, stream, 0 )) || FT_SET_ERROR( cff_new_index( &font->global_subrs_index, stream, 1 )) ) goto Exit; /* well, we don't really forget the `disabled' fonts... */ font->num_faces = font->name_index.count; if ( face_index >= (FT_Int)font->num_faces ) { FT_ERROR(( "cff_font_load: incorrect face index = %d\n", face_index )); error = CFF_Err_Invalid_Argument; } /* in case of a font format check, simply exit now */ if ( face_index < 0 ) goto Exit; /* now, parse the top-level font dictionary */ error = cff_subfont_load( &font->top_font, &font->font_dict_index, face_index, stream, base_offset ); if ( error ) goto Exit; /* now, check for a CID font */ if ( dict->cid_registry ) { CFF_IndexRec fd_index; CFF_SubFont sub; FT_UInt idx; /* this is a CID-keyed font, we must now allocate a table of */ /* sub-fonts, then load each of them separately */ if ( FT_STREAM_SEEK( base_offset + dict->cid_fd_array_offset ) ) goto Exit; error = cff_new_index( &fd_index, stream, 0 ); if ( error ) goto Exit; if ( fd_index.count > CFF_MAX_CID_FONTS ) { FT_ERROR(( "cff_font_load: FD array too large in CID font\n" )); goto Fail_CID; } /* allocate & read each font dict independently */ font->num_subfonts = fd_index.count; if ( FT_NEW_ARRAY( sub, fd_index.count ) ) goto Fail_CID; /* setup pointer table */ for ( idx = 0; idx < fd_index.count; idx++ ) font->subfonts[idx] = sub + idx; /* now load each sub font independently */ for ( idx = 0; idx < fd_index.count; idx++ ) { sub = font->subfonts[idx]; error = cff_subfont_load( sub, &fd_index, idx, stream, base_offset ); if ( error ) goto Fail_CID; } /* now load the FD Select array */ error = CFF_Load_FD_Select( &font->fd_select, (FT_UInt)dict->cid_count, stream, base_offset + dict->cid_fd_select_offset ); Fail_CID: cff_done_index( &fd_index ); if ( error ) goto Exit; } else font->num_subfonts = 0; /* read the charstrings index now */ if ( dict->charstrings_offset == 0 ) { FT_ERROR(( "cff_font_load: no charstrings offset!\n" )); error = CFF_Err_Unknown_File_Format; goto Exit; } if ( FT_STREAM_SEEK( base_offset + dict->charstrings_offset ) ) goto Exit; error = cff_new_index( &font->charstrings_index, stream, 0 ); if ( error ) goto Exit; /* explicit the global subrs */ font->num_global_subrs = font->global_subrs_index.count; font->num_glyphs = font->charstrings_index.count; error = cff_index_get_pointers( &font->global_subrs_index, &font->global_subrs ) ; if ( error ) goto Exit; /* read the Charset and Encoding tables when available */ if ( font->num_glyphs > 0 ) { error = cff_charset_load( &font->charset, font->num_glyphs, stream, base_offset, dict->charset_offset ); if ( error ) goto Exit; error = cff_encoding_load( &font->encoding, &font->charset, font->num_glyphs, stream, base_offset, dict->encoding_offset ); if ( error ) goto Exit; } /* get the font name */ font->font_name = cff_index_get_name( &font->name_index, face_index ); Exit: return error; }
tt_face_load_font_dir( TT_Face face, FT_Stream stream ) { SFNT_HeaderRec sfnt; FT_Error error; FT_Memory memory = stream->memory; TT_TableRec* entry; TT_TableRec* limit; static const FT_Frame_Field offset_table_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE SFNT_HeaderRec FT_FRAME_START( 8 ), FT_FRAME_USHORT( num_tables ), FT_FRAME_USHORT( search_range ), FT_FRAME_USHORT( entry_selector ), FT_FRAME_USHORT( range_shift ), FT_FRAME_END }; FT_TRACE2(( "tt_face_load_font_dir: %08p\n", face )); /* read the offset table */ sfnt.offset = FT_STREAM_POS(); if ( FT_READ_ULONG( sfnt.format_tag ) || FT_STREAM_READ_FIELDS( offset_table_fields, &sfnt ) ) return error; /* many fonts don't have these fields set correctly */ #if 0 if ( sfnt.search_range != 1 << ( sfnt.entry_selector + 4 ) || sfnt.search_range + sfnt.range_shift != sfnt.num_tables << 4 ) return SFNT_Err_Unknown_File_Format; #endif /* load the table directory */ FT_TRACE2(( "-- Tables count: %12u\n", sfnt.num_tables )); FT_TRACE2(( "-- Format version: %08lx\n", sfnt.format_tag )); /* check first */ error = check_table_dir( &sfnt, stream ); if ( error ) { FT_TRACE2(( "tt_face_load_font_dir: invalid table directory!\n" )); return error; } face->num_tables = sfnt.num_tables; face->format_tag = sfnt.format_tag; if ( FT_QNEW_ARRAY( face->dir_tables, face->num_tables ) ) return error; if ( FT_STREAM_SEEK( sfnt.offset + 12 ) || FT_FRAME_ENTER( face->num_tables * 16L ) ) return error; entry = face->dir_tables; limit = entry + face->num_tables; for ( ; entry < limit; entry++ ) { entry->Tag = FT_GET_TAG4(); entry->CheckSum = FT_GET_ULONG(); entry->Offset = FT_GET_LONG(); entry->Length = FT_GET_LONG(); FT_TRACE2(( " %c%c%c%c - %08lx - %08lx\n", (FT_Char)( entry->Tag >> 24 ), (FT_Char)( entry->Tag >> 16 ), (FT_Char)( entry->Tag >> 8 ), (FT_Char)( entry->Tag ), entry->Offset, entry->Length )); } FT_FRAME_EXIT(); FT_TRACE2(( "table directory loaded\n\n" )); return error; }
static FT_Error check_table_dir( SFNT_Header sfnt, FT_Stream stream ) { FT_Error error; FT_UInt nn; FT_UInt has_head = 0, has_sing = 0, has_meta = 0; FT_ULong offset = sfnt->offset + 12; const FT_ULong glyx_tag = FT_MAKE_TAG( 'g', 'l', 'y', 'x' ); const FT_ULong locx_tag = FT_MAKE_TAG( 'l', 'o', 'c', 'x' ); static const FT_Frame_Field table_dir_entry_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE TT_TableRec FT_FRAME_START( 16 ), FT_FRAME_ULONG( Tag ), FT_FRAME_ULONG( CheckSum ), FT_FRAME_ULONG( Offset ), FT_FRAME_ULONG( Length ), FT_FRAME_END }; if ( sfnt->num_tables == 0 || offset + sfnt->num_tables * 16 > stream->size ) return SFNT_Err_Unknown_File_Format; if ( FT_STREAM_SEEK( offset ) ) return error; for ( nn = 0; nn < sfnt->num_tables; nn++ ) { TT_TableRec table; if ( FT_STREAM_READ_FIELDS( table_dir_entry_fields, &table ) ) return error; if ( table.Offset + table.Length > stream->size && table.Tag != glyx_tag && table.Tag != locx_tag ) return SFNT_Err_Unknown_File_Format; if ( table.Tag == TTAG_head || table.Tag == TTAG_bhed ) { FT_UInt32 magic; #ifndef TT_CONFIG_OPTION_EMBEDDED_BITMAPS if ( table.Tag == TTAG_head ) #endif has_head = 1; /* * The table length should be 0x36, but certain font tools make it * 0x38, so we will just check that it is greater. * * Note that according to the specification, the table must be * padded to 32-bit lengths, but this doesn't apply to the value of * its `Length' field! * */ if ( table.Length < 0x36 ) return SFNT_Err_Unknown_File_Format; if ( FT_STREAM_SEEK( table.Offset + 12 ) || FT_READ_ULONG( magic ) ) return error; if ( magic != 0x5F0F3CF5UL ) return SFNT_Err_Unknown_File_Format; if ( FT_STREAM_SEEK( offset + ( nn + 1 ) * 16 ) ) return error; } else if ( table.Tag == TTAG_SING ) has_sing = 1; else if ( table.Tag == TTAG_META ) has_meta = 1; } /* if `sing' and `meta' tables are present, there is no `head' table */ if ( has_head || ( has_sing && has_meta ) ) return SFNT_Err_Ok; else return SFNT_Err_Unknown_File_Format; }
static FT_Error woff_open_font( FT_Stream stream, TT_Face face ) { FT_Memory memory = stream->memory; FT_Error error = FT_Err_Ok; WOFF_HeaderRec woff; WOFF_Table tables = NULL; WOFF_Table* indices = NULL; FT_ULong woff_offset; FT_Byte* sfnt = NULL; FT_Stream sfnt_stream = NULL; FT_Byte* sfnt_header; FT_ULong sfnt_offset; FT_Int nn; FT_ULong old_tag = 0; static const FT_Frame_Field woff_header_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE WOFF_HeaderRec FT_FRAME_START( 44 ), FT_FRAME_ULONG ( signature ), FT_FRAME_ULONG ( flavor ), FT_FRAME_ULONG ( length ), FT_FRAME_USHORT( num_tables ), FT_FRAME_USHORT( reserved ), FT_FRAME_ULONG ( totalSfntSize ), FT_FRAME_USHORT( majorVersion ), FT_FRAME_USHORT( minorVersion ), FT_FRAME_ULONG ( metaOffset ), FT_FRAME_ULONG ( metaLength ), FT_FRAME_ULONG ( metaOrigLength ), FT_FRAME_ULONG ( privOffset ), FT_FRAME_ULONG ( privLength ), FT_FRAME_END }; FT_ASSERT( stream == face->root.stream ); FT_ASSERT( FT_STREAM_POS() == 0 ); if ( FT_STREAM_READ_FIELDS( woff_header_fields, &woff ) ) return error; /* Make sure we don't recurse back here or hit TTC code. */ if ( woff.flavor == TTAG_wOFF || woff.flavor == TTAG_ttcf ) return FT_THROW( Invalid_Table ); /* Miscellaneous checks. */ if ( woff.length != stream->size || woff.num_tables == 0 || 44 + woff.num_tables * 20UL >= woff.length || 12 + woff.num_tables * 16UL >= woff.totalSfntSize || ( woff.totalSfntSize & 3 ) != 0 || ( woff.metaOffset == 0 && ( woff.metaLength != 0 || woff.metaOrigLength != 0 ) ) || ( woff.metaLength != 0 && woff.metaOrigLength == 0 ) || ( woff.privOffset == 0 && woff.privLength != 0 ) ) return FT_THROW( Invalid_Table ); if ( FT_ALLOC( sfnt, woff.totalSfntSize ) || FT_NEW( sfnt_stream ) ) goto Exit; sfnt_header = sfnt; /* Write sfnt header. */ { FT_UInt searchRange, entrySelector, rangeShift, x; x = woff.num_tables; entrySelector = 0; while ( x ) { x >>= 1; entrySelector += 1; } entrySelector--; searchRange = ( 1 << entrySelector ) * 16; rangeShift = woff.num_tables * 16 - searchRange; WRITE_ULONG ( sfnt_header, woff.flavor ); WRITE_USHORT( sfnt_header, woff.num_tables ); WRITE_USHORT( sfnt_header, searchRange ); WRITE_USHORT( sfnt_header, entrySelector ); WRITE_USHORT( sfnt_header, rangeShift ); } /* While the entries in the sfnt header must be sorted by the */ /* tag value, the tables themselves are not. We thus have to */ /* sort them by offset and check that they don't overlap. */ if ( FT_NEW_ARRAY( tables, woff.num_tables ) || FT_NEW_ARRAY( indices, woff.num_tables ) ) goto Exit; FT_TRACE2(( "\n" " tag offset compLen origLen checksum\n" " -------------------------------------------\n" )); if ( FT_FRAME_ENTER( 20L * woff.num_tables ) ) goto Exit; for ( nn = 0; nn < woff.num_tables; nn++ ) { WOFF_Table table = tables + nn; table->Tag = FT_GET_TAG4(); table->Offset = FT_GET_ULONG(); table->CompLength = FT_GET_ULONG(); table->OrigLength = FT_GET_ULONG(); table->CheckSum = FT_GET_ULONG(); FT_TRACE2(( " %c%c%c%c %08lx %08lx %08lx %08lx\n", (FT_Char)( table->Tag >> 24 ), (FT_Char)( table->Tag >> 16 ), (FT_Char)( table->Tag >> 8 ), (FT_Char)( table->Tag ), table->Offset, table->CompLength, table->OrigLength, table->CheckSum )); if ( table->Tag <= old_tag ) { FT_FRAME_EXIT(); error = FT_THROW( Invalid_Table ); goto Exit; } old_tag = table->Tag; indices[nn] = table; } FT_FRAME_EXIT(); /* Sort by offset. */ ft_qsort( indices, woff.num_tables, sizeof ( WOFF_Table ), compare_offsets ); /* Check offsets and lengths. */ woff_offset = 44 + woff.num_tables * 20L; sfnt_offset = 12 + woff.num_tables * 16L; for ( nn = 0; nn < woff.num_tables; nn++ ) { WOFF_Table table = indices[nn]; if ( table->Offset != woff_offset || table->CompLength > woff.length || table->Offset > woff.length - table->CompLength || table->OrigLength > woff.totalSfntSize || sfnt_offset > woff.totalSfntSize - table->OrigLength || table->CompLength > table->OrigLength ) { error = FT_THROW( Invalid_Table ); goto Exit; } table->OrigOffset = sfnt_offset; /* The offsets must be multiples of 4. */ woff_offset += ( table->CompLength + 3 ) & ~3U; sfnt_offset += ( table->OrigLength + 3 ) & ~3U; } /* * Final checks! * * We don't decode and check the metadata block. * We don't check table checksums either. * But other than those, I think we implement all * `MUST' checks from the spec. */ if ( woff.metaOffset ) { if ( woff.metaOffset != woff_offset || woff.metaOffset + woff.metaLength > woff.length ) { error = FT_THROW( Invalid_Table ); goto Exit; } /* We have padding only ... */ woff_offset += woff.metaLength; } if ( woff.privOffset ) { /* ... if it isn't the last block. */ woff_offset = ( woff_offset + 3 ) & ~3U; if ( woff.privOffset != woff_offset || woff.privOffset + woff.privLength > woff.length ) { error = FT_THROW( Invalid_Table ); goto Exit; } /* No padding for the last block. */ woff_offset += woff.privLength; } if ( sfnt_offset != woff.totalSfntSize || woff_offset != woff.length ) { error = FT_THROW( Invalid_Table ); goto Exit; } /* Write the tables. */ for ( nn = 0; nn < woff.num_tables; nn++ ) { WOFF_Table table = tables + nn; /* Write SFNT table entry. */ WRITE_ULONG( sfnt_header, table->Tag ); WRITE_ULONG( sfnt_header, table->CheckSum ); WRITE_ULONG( sfnt_header, table->OrigOffset ); WRITE_ULONG( sfnt_header, table->OrigLength ); /* Write table data. */ if ( FT_STREAM_SEEK( table->Offset ) || FT_FRAME_ENTER( table->CompLength ) ) goto Exit; if ( table->CompLength == table->OrigLength ) { /* Uncompressed data; just copy. */ ft_memcpy( sfnt + table->OrigOffset, stream->cursor, table->OrigLength ); } else { #ifdef FT_CONFIG_OPTION_USE_ZLIB /* Uncompress with zlib. */ FT_ULong output_len = table->OrigLength; error = FT_Gzip_Uncompress( memory, sfnt + table->OrigOffset, &output_len, stream->cursor, table->CompLength ); if ( error ) goto Exit; if ( output_len != table->OrigLength ) { error = FT_THROW( Invalid_Table ); goto Exit; } #else /* !FT_CONFIG_OPTION_USE_ZLIB */ error = FT_THROW( Unimplemented_Feature ); goto Exit; #endif /* !FT_CONFIG_OPTION_USE_ZLIB */ } FT_FRAME_EXIT(); /* We don't check whether the padding bytes in the WOFF file are */ /* actually '\0'. For the output, however, we do set them properly. */ sfnt_offset = table->OrigOffset + table->OrigLength; while ( sfnt_offset & 3 ) { sfnt[sfnt_offset] = '\0'; sfnt_offset++; } } /* Ok! Finally ready. Swap out stream and return. */ FT_Stream_OpenMemory( sfnt_stream, sfnt, woff.totalSfntSize ); sfnt_stream->memory = stream->memory; sfnt_stream->close = sfnt_stream_close; FT_Stream_Free( face->root.stream, ( face->root.face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 ); face->root.stream = sfnt_stream; face->root.face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM; Exit: FT_FREE( tables ); FT_FREE( indices ); if ( error ) { FT_FREE( sfnt ); FT_Stream_Close( sfnt_stream ); FT_FREE( sfnt_stream ); } return error; }
tt_face_load_maxp( TT_Face face, FT_Stream stream ) { FT_Error error; TT_MaxProfile* maxProfile = &face->max_profile; static const FT_Frame_Field maxp_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE TT_MaxProfile FT_FRAME_START( 6 ), FT_FRAME_LONG ( version ), FT_FRAME_USHORT( numGlyphs ), FT_FRAME_END }; static const FT_Frame_Field maxp_fields_extra[] = { FT_FRAME_START( 26 ), FT_FRAME_USHORT( maxPoints ), FT_FRAME_USHORT( maxContours ), FT_FRAME_USHORT( maxCompositePoints ), FT_FRAME_USHORT( maxCompositeContours ), FT_FRAME_USHORT( maxZones ), FT_FRAME_USHORT( maxTwilightPoints ), FT_FRAME_USHORT( maxStorage ), FT_FRAME_USHORT( maxFunctionDefs ), FT_FRAME_USHORT( maxInstructionDefs ), FT_FRAME_USHORT( maxStackElements ), FT_FRAME_USHORT( maxSizeOfInstructions ), FT_FRAME_USHORT( maxComponentElements ), FT_FRAME_USHORT( maxComponentDepth ), FT_FRAME_END }; error = face->goto_table( face, TTAG_maxp, stream, 0 ); if ( error ) goto Exit; if ( FT_STREAM_READ_FIELDS( maxp_fields, maxProfile ) ) goto Exit; maxProfile->maxPoints = 0; maxProfile->maxContours = 0; maxProfile->maxCompositePoints = 0; maxProfile->maxCompositeContours = 0; maxProfile->maxZones = 0; maxProfile->maxTwilightPoints = 0; maxProfile->maxStorage = 0; maxProfile->maxFunctionDefs = 0; maxProfile->maxInstructionDefs = 0; maxProfile->maxStackElements = 0; maxProfile->maxSizeOfInstructions = 0; maxProfile->maxComponentElements = 0; maxProfile->maxComponentDepth = 0; if ( maxProfile->version >= 0x10000L ) { if ( FT_STREAM_READ_FIELDS( maxp_fields_extra, maxProfile ) ) goto Exit; /* XXX: an adjustment that is necessary to load certain */ /* broken fonts like `Keystrokes MT' :-( */ /* */ /* We allocate 64 function entries by default when */ /* the maxFunctionDefs value is smaller. */ if ( maxProfile->maxFunctionDefs < 64 ) maxProfile->maxFunctionDefs = 64; /* we add 4 phantom points later */ if ( maxProfile->maxTwilightPoints > ( 0xFFFFU - 4 ) ) { FT_TRACE0(( "tt_face_load_maxp:" " too much twilight points in `maxp' table;\n" " " " some glyphs might be rendered incorrectly\n" )); maxProfile->maxTwilightPoints = 0xFFFFU - 4; } /* we arbitrarily limit recursion to avoid stack exhaustion */ if ( maxProfile->maxComponentDepth > 100 ) { FT_TRACE0(( "tt_face_load_maxp:" " abnormally large component depth (%d) set to 100\n", maxProfile->maxComponentDepth )); maxProfile->maxComponentDepth = 100; } } FT_TRACE3(( "numGlyphs: %u\n", maxProfile->numGlyphs )); Exit: return error; }
static FT_Error fnt_face_get_dll_font( FNT_Face face, FT_Int face_index ) { FT_Error error; FT_Stream stream = FT_FACE( face )->stream; FT_Memory memory = FT_FACE( face )->memory; WinMZ_HeaderRec mz_header; face->font = 0; /* does it begin with an MZ header? */ if ( FT_STREAM_SEEK( 0 ) || FT_STREAM_READ_FIELDS( winmz_header_fields, &mz_header ) ) goto Exit; error = FNT_Err_Unknown_File_Format; if ( mz_header.magic == WINFNT_MZ_MAGIC ) { /* yes, now look for an NE header in the file */ WinNE_HeaderRec ne_header; if ( FT_STREAM_SEEK( mz_header.lfanew ) || FT_STREAM_READ_FIELDS( winne_header_fields, &ne_header ) ) goto Exit; error = FNT_Err_Unknown_File_Format; if ( ne_header.magic == WINFNT_NE_MAGIC ) { /* good, now look into the resource table for each FNT resource */ FT_ULong res_offset = mz_header.lfanew + ne_header.resource_tab_offset; FT_UShort size_shift; FT_UShort font_count = 0; FT_ULong font_offset = 0; if ( FT_STREAM_SEEK( res_offset ) || FT_FRAME_ENTER( ne_header.rname_tab_offset - ne_header.resource_tab_offset ) ) goto Exit; size_shift = FT_GET_USHORT_LE(); for (;;) { FT_UShort type_id, count; type_id = FT_GET_USHORT_LE(); if ( !type_id ) break; count = FT_GET_USHORT_LE(); if ( type_id == 0x8008U ) { font_count = count; font_offset = (FT_ULong)( FT_STREAM_POS() + 4 + ( stream->cursor - stream->limit ) ); break; } stream->cursor += 4 + count * 12; } FT_FRAME_EXIT(); if ( !font_count || !font_offset ) { FT_TRACE2(( "this file doesn't contain any FNT resources!\n" )); error = FNT_Err_Unknown_File_Format; goto Exit; } face->root.num_faces = font_count; if ( face_index >= font_count ) { error = FNT_Err_Bad_Argument; goto Exit; } if ( FT_NEW( face->font ) ) goto Exit; if ( FT_STREAM_SEEK( font_offset + face_index * 12 ) || FT_FRAME_ENTER( 12 ) ) goto Fail; face->font->offset = (FT_ULong)FT_GET_USHORT_LE() << size_shift; face->font->fnt_size = (FT_ULong)FT_GET_USHORT_LE() << size_shift; face->font->size_shift = size_shift; stream->cursor += 8; FT_FRAME_EXIT(); error = fnt_font_load( face->font, stream ); } } Fail: if ( error ) fnt_font_done( face ); Exit: return error; }
tt_face_load_name( TT_Face face, FT_Stream stream ) { FT_Error error; FT_Memory memory = stream->memory; FT_ULong table_pos, table_len; FT_ULong storage_start, storage_limit; FT_UInt count; TT_NameTable table; static const FT_Frame_Field name_table_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE TT_NameTableRec FT_FRAME_START( 6 ), FT_FRAME_USHORT( format ), FT_FRAME_USHORT( numNameRecords ), FT_FRAME_USHORT( storageOffset ), FT_FRAME_END }; static const FT_Frame_Field name_record_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE TT_NameEntryRec /* no FT_FRAME_START */ FT_FRAME_USHORT( platformID ), FT_FRAME_USHORT( encodingID ), FT_FRAME_USHORT( languageID ), FT_FRAME_USHORT( nameID ), FT_FRAME_USHORT( stringLength ), FT_FRAME_USHORT( stringOffset ), FT_FRAME_END }; table = &face->name_table; table->stream = stream; error = face->goto_table( face, TTAG_name, stream, &table_len ); if ( error ) goto Exit; table_pos = FT_STREAM_POS(); if ( FT_STREAM_READ_FIELDS( name_table_fields, table ) ) goto Exit; /* 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. */ /* */ storage_start = table_pos + 6 + 12*table->numNameRecords; storage_limit = table_pos + table_len; if ( storage_start > storage_limit ) { FT_ERROR(( "tt_face_load_name: invalid `name' table\n" )); error = FT_THROW( Name_Table_Missing ); goto Exit; } /* Allocate the array of name records. */ count = table->numNameRecords; table->numNameRecords = 0; 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* entry = table->names; for ( ; count > 0; count-- ) { if ( FT_STREAM_READ_FIELDS( name_record_fields, entry ) ) continue; /* check that the name is not empty */ if ( entry->stringLength == 0 ) continue; /* 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 ) { /* invalid entry - ignore it */ entry->stringOffset = 0; entry->stringLength = 0; continue; } entry++; } table->numNameRecords = (FT_UInt)( entry - table->names ); } FT_FRAME_EXIT(); /* everything went well, update face->num_names */ face->num_names = (FT_UShort) table->numNameRecords; Exit: return error; }
cff_font_load( FT_Library library, FT_Stream stream, FT_Int face_index, CFF_Font font, FT_Bool pure_cff ) { static const FT_Frame_Field cff_header_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE CFF_FontRec FT_FRAME_START( 4 ), FT_FRAME_BYTE( version_major ), FT_FRAME_BYTE( version_minor ), FT_FRAME_BYTE( header_size ), FT_FRAME_BYTE( absolute_offsize ), FT_FRAME_END }; FT_Error error; FT_Memory memory = stream->memory; FT_ULong base_offset; CFF_FontRecDict dict; CFF_IndexRec string_index; FT_UInt subfont_index; FT_ZERO( font ); FT_ZERO( &string_index ); font->stream = stream; font->memory = memory; dict = &font->top_font.font_dict; base_offset = FT_STREAM_POS(); /* read CFF font header */ if ( FT_STREAM_READ_FIELDS( cff_header_fields, font ) ) goto Exit; /* check format */ if ( font->version_major != 1 || font->header_size < 4 || font->absolute_offsize > 4 ) { FT_TRACE2(( " not a CFF font header\n" )); error = FT_THROW( Unknown_File_Format ); goto Exit; } /* skip the rest of the header */ if ( FT_STREAM_SKIP( font->header_size - 4 ) ) goto Exit; /* read the name, top dict, string and global subrs index */ if ( FT_SET_ERROR( cff_index_init( &font->name_index, stream, 0 ) ) || FT_SET_ERROR( cff_index_init( &font->font_dict_index, stream, 0 ) ) || FT_SET_ERROR( cff_index_init( &string_index, stream, 1 ) ) || FT_SET_ERROR( cff_index_init( &font->global_subrs_index, stream, 1 ) ) || FT_SET_ERROR( cff_index_get_pointers( &string_index, &font->strings, &font->string_pool ) ) ) goto Exit; font->num_strings = string_index.count; if ( pure_cff ) { /* well, we don't really forget the `disabled' fonts... */ subfont_index = (FT_UInt)( face_index & 0xFFFF ); if ( face_index > 0 && subfont_index >= font->name_index.count ) { FT_ERROR(( "cff_font_load:" " invalid subfont index for pure CFF font (%d)\n", subfont_index )); error = FT_THROW( Invalid_Argument ); goto Exit; } font->num_faces = font->name_index.count; } else { subfont_index = 0; if ( font->name_index.count > 1 ) { FT_ERROR(( "cff_font_load:" " invalid CFF font with multiple subfonts\n" " " " in SFNT wrapper\n" )); error = FT_THROW( Invalid_File_Format ); goto Exit; } } /* in case of a font format check, simply exit now */ if ( face_index < 0 ) goto Exit; /* now, parse the top-level font dictionary */ FT_TRACE4(( "parsing top-level\n" )); error = cff_subfont_load( &font->top_font, &font->font_dict_index, subfont_index, stream, base_offset, library ); if ( error ) goto Exit; if ( FT_STREAM_SEEK( base_offset + dict->charstrings_offset ) ) goto Exit; error = cff_index_init( &font->charstrings_index, stream, 0 ); if ( error ) goto Exit; /* now, check for a CID font */ if ( dict->cid_registry != 0xFFFFU ) { CFF_IndexRec fd_index; CFF_SubFont sub = NULL; FT_UInt idx; /* this is a CID-keyed font, we must now allocate a table of */ /* sub-fonts, then load each of them separately */ if ( FT_STREAM_SEEK( base_offset + dict->cid_fd_array_offset ) ) goto Exit; error = cff_index_init( &fd_index, stream, 0 ); if ( error ) goto Exit; if ( fd_index.count > CFF_MAX_CID_FONTS ) { FT_TRACE0(( "cff_font_load: FD array too large in CID font\n" )); goto Fail_CID; } /* allocate & read each font dict independently */ font->num_subfonts = fd_index.count; if ( FT_NEW_ARRAY( sub, fd_index.count ) ) goto Fail_CID; /* set up pointer table */ for ( idx = 0; idx < fd_index.count; idx++ ) font->subfonts[idx] = sub + idx; /* now load each subfont independently */ for ( idx = 0; idx < fd_index.count; idx++ ) { sub = font->subfonts[idx]; FT_TRACE4(( "parsing subfont %u\n", idx )); error = cff_subfont_load( sub, &fd_index, idx, stream, base_offset, library ); if ( error ) goto Fail_CID; } /* now load the FD Select array */ error = CFF_Load_FD_Select( &font->fd_select, font->charstrings_index.count, stream, base_offset + dict->cid_fd_select_offset ); Fail_CID: cff_index_done( &fd_index ); if ( error ) goto Exit; } else font->num_subfonts = 0; /* read the charstrings index now */ if ( dict->charstrings_offset == 0 ) { FT_ERROR(( "cff_font_load: no charstrings offset\n" )); error = FT_THROW( Invalid_File_Format ); goto Exit; } font->num_glyphs = font->charstrings_index.count; error = cff_index_get_pointers( &font->global_subrs_index, &font->global_subrs, NULL ); if ( error ) goto Exit; /* read the Charset and Encoding tables if available */ if ( font->num_glyphs > 0 ) { FT_Bool invert = FT_BOOL( dict->cid_registry != 0xFFFFU && pure_cff ); error = cff_charset_load( &font->charset, font->num_glyphs, stream, base_offset, dict->charset_offset, invert ); if ( error ) goto Exit; /* CID-keyed CFFs don't have an encoding */ if ( dict->cid_registry == 0xFFFFU ) { error = cff_encoding_load( &font->encoding, &font->charset, font->num_glyphs, stream, base_offset, dict->encoding_offset ); if ( error ) goto Exit; } } /* get the font name (/CIDFontName for CID-keyed fonts, */ /* /FontName otherwise) */ font->font_name = cff_index_get_name( font, subfont_index ); Exit: cff_index_done( &string_index ); return error; }
tt_face_load_hhea( TT_Face face, FT_Stream stream, FT_Bool vertical ) { FT_Error error; TT_HoriHeader* header; static const FT_Frame_Field metrics_header_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE TT_HoriHeader FT_FRAME_START( 36 ), FT_FRAME_ULONG ( Version ), FT_FRAME_SHORT ( Ascender ), FT_FRAME_SHORT ( Descender ), FT_FRAME_SHORT ( Line_Gap ), FT_FRAME_USHORT( advance_Width_Max ), FT_FRAME_SHORT ( min_Left_Side_Bearing ), FT_FRAME_SHORT ( min_Right_Side_Bearing ), FT_FRAME_SHORT ( xMax_Extent ), FT_FRAME_SHORT ( caret_Slope_Rise ), FT_FRAME_SHORT ( caret_Slope_Run ), FT_FRAME_SHORT ( caret_Offset ), FT_FRAME_SHORT ( Reserved[0] ), FT_FRAME_SHORT ( Reserved[1] ), FT_FRAME_SHORT ( Reserved[2] ), FT_FRAME_SHORT ( Reserved[3] ), FT_FRAME_SHORT ( metric_Data_Format ), FT_FRAME_USHORT( number_Of_HMetrics ), FT_FRAME_END }; if ( vertical ) { void *v = &face->vertical; error = face->goto_table( face, TTAG_vhea, stream, 0 ); if ( error ) goto Fail; header = (TT_HoriHeader*)v; } else { error = face->goto_table( face, TTAG_hhea, stream, 0 ); if ( error ) goto Fail; header = &face->horizontal; } if ( FT_STREAM_READ_FIELDS( metrics_header_fields, header ) ) goto Fail; FT_TRACE3(( "Ascender: %5d\n", header->Ascender )); FT_TRACE3(( "Descender: %5d\n", header->Descender )); FT_TRACE3(( "number_Of_Metrics: %5u\n", header->number_Of_HMetrics )); header->long_metrics = NULL; header->short_metrics = NULL; Fail: return error; }
static FT_Error pcf_get_properties( FT_Stream stream, PCF_Face face ) { PCF_ParseProperty props = 0; PCF_Property properties = 0; FT_Int nprops, i; FT_ULong format, size; FT_Error error; FT_Memory memory = FT_FACE(face)->memory; FT_ULong string_size; FT_String* strings = 0; error = pcf_seek_to_table_type( stream, face->toc.tables, face->toc.count, PCF_PROPERTIES, &format, &size ); if ( error ) goto Bail; if ( FT_READ_ULONG_LE( format ) ) goto Bail; FT_TRACE4(( "get_prop: format = %ld\n", format )); if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) ) goto Bail; if ( PCF_BYTE_ORDER( format ) == MSBFirst ) (void)FT_READ_ULONG( nprops ); else (void)FT_READ_ULONG_LE( nprops ); if ( error ) goto Bail; FT_TRACE4(( "get_prop: nprop = %d\n", nprops )); if ( FT_NEW_ARRAY( props, nprops ) ) goto Bail; for ( i = 0; i < nprops; i++ ) { if ( PCF_BYTE_ORDER( format ) == MSBFirst ) { if ( FT_STREAM_READ_FIELDS( pcf_property_msb_header, props + i ) ) goto Bail; } else { if ( FT_STREAM_READ_FIELDS( pcf_property_header, props + i ) ) goto Bail; } } /* pad the property array */ /* */ /* clever here - nprops is the same as the number of odd-units read, */ /* as only isStringProp are odd length (Keith Packard) */ /* */ if ( nprops & 3 ) { i = 4 - ( nprops & 3 ); FT_Stream_Skip( stream, i ); } if ( PCF_BYTE_ORDER( format ) == MSBFirst ) (void)FT_READ_ULONG( string_size ); else (void)FT_READ_ULONG_LE( string_size ); if ( error ) goto Bail; FT_TRACE4(( "get_prop: string_size = %ld\n", string_size )); if ( FT_NEW_ARRAY( strings, string_size ) ) goto Bail; error = FT_Stream_Read( stream, (FT_Byte*)strings, string_size ); if ( error ) goto Bail; if ( FT_NEW_ARRAY( properties, nprops ) ) goto Bail; for ( i = 0; i < nprops; i++ ) { /* XXX: make atom */ if ( FT_NEW_ARRAY( properties[i].name, ft_strlen( strings + props[i].name ) + 1 ) ) goto Bail; ft_strcpy( properties[i].name,strings + props[i].name ); properties[i].isString = props[i].isString; if ( props[i].isString ) { if ( FT_NEW_ARRAY( properties[i].value.atom, ft_strlen( strings + props[i].value ) + 1 ) ) goto Bail; ft_strcpy( properties[i].value.atom, strings + props[i].value ); } else properties[i].value.integer = props[i].value; } face->properties = properties; face->nprops = nprops; FT_FREE( props ); FT_FREE( strings ); return PCF_Err_Ok; Bail: FT_FREE( props ); FT_FREE( strings ); return error; }
static FT_Error pcf_get_properties( FT_Stream stream, PCF_Face face ) { PCF_ParseProperty props = 0; PCF_Property properties = NULL; FT_ULong nprops, i; FT_ULong format, size; FT_Error error; FT_Memory memory = FT_FACE( face )->memory; FT_ULong string_size; FT_String* strings = 0; error = pcf_seek_to_table_type( stream, face->toc.tables, face->toc.count, PCF_PROPERTIES, &format, &size ); if ( error ) goto Bail; if ( FT_READ_ULONG_LE( format ) ) goto Bail; FT_TRACE4(( "pcf_get_properties:\n" )); FT_TRACE4(( " format = %ld\n", format )); if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) ) goto Bail; if ( PCF_BYTE_ORDER( format ) == MSBFirst ) (void)FT_READ_ULONG( nprops ); else (void)FT_READ_ULONG_LE( nprops ); if ( error ) goto Bail; FT_TRACE4(( " nprop = %d (truncate %d props)\n", (int)nprops, nprops - (int)nprops )); nprops = (int)nprops; /* rough estimate */ if ( nprops > size / PCF_PROPERTY_SIZE ) { error = FT_THROW( Invalid_Table ); goto Bail; } face->nprops = (int)nprops; if ( FT_NEW_ARRAY( props, nprops ) ) goto Bail; for ( i = 0; i < nprops; i++ ) { if ( PCF_BYTE_ORDER( format ) == MSBFirst ) { if ( FT_STREAM_READ_FIELDS( pcf_property_msb_header, props + i ) ) goto Bail; } else { if ( FT_STREAM_READ_FIELDS( pcf_property_header, props + i ) ) goto Bail; } } /* pad the property array */ /* */ /* clever here - nprops is the same as the number of odd-units read, */ /* as only isStringProp are odd length (Keith Packard) */ /* */ if ( nprops & 3 ) { i = 4 - ( nprops & 3 ); if ( FT_STREAM_SKIP( i ) ) { error = FT_THROW( Invalid_Stream_Skip ); goto Bail; } } if ( PCF_BYTE_ORDER( format ) == MSBFirst ) (void)FT_READ_ULONG( string_size ); else (void)FT_READ_ULONG_LE( string_size ); if ( error ) goto Bail; FT_TRACE4(( " string_size = %ld\n", string_size )); /* rough estimate */ if ( string_size > size - nprops * PCF_PROPERTY_SIZE ) { error = FT_THROW( Invalid_Table ); goto Bail; } /* allocate one more byte so that we have a final null byte */ if ( FT_NEW_ARRAY( strings, string_size + 1 ) ) goto Bail; error = FT_Stream_Read( stream, (FT_Byte*)strings, string_size ); if ( error ) goto Bail; if ( FT_NEW_ARRAY( properties, nprops ) ) goto Bail; face->properties = properties; for ( i = 0; i < nprops; i++ ) { FT_Long name_offset = props[i].name; if ( ( name_offset < 0 ) || ( (FT_ULong)name_offset > string_size ) ) { error = FT_THROW( Invalid_Offset ); goto Bail; } if ( FT_STRDUP( properties[i].name, strings + name_offset ) ) goto Bail; FT_TRACE4(( " %s:", properties[i].name )); properties[i].isString = props[i].isString; if ( props[i].isString ) { FT_Long value_offset = props[i].value; if ( ( value_offset < 0 ) || ( (FT_ULong)value_offset > string_size ) ) { error = FT_THROW( Invalid_Offset ); goto Bail; } if ( FT_STRDUP( properties[i].value.atom, strings + value_offset ) ) goto Bail; FT_TRACE4(( " `%s'\n", properties[i].value.atom )); } else { properties[i].value.l = props[i].value; FT_TRACE4(( " %d\n", properties[i].value.l )); } } error = FT_Err_Ok; Bail: FT_FREE( props ); FT_FREE( strings ); return error; }
static FT_Error pcf_read_TOC( FT_Stream stream, PCF_Face face ) { FT_Error error; PCF_Toc toc = &face->toc; PCF_Table tables; FT_Memory memory = FT_FACE(face)->memory; FT_UInt n; if ( FT_STREAM_SEEK ( 0 ) || FT_STREAM_READ_FIELDS ( pcf_toc_header, toc ) ) return PCF_Err_Cannot_Open_Resource; if ( toc->version != PCF_FILE_VERSION ) return PCF_Err_Invalid_File_Format; if ( FT_NEW_ARRAY( face->toc.tables, toc->count ) ) return PCF_Err_Out_Of_Memory; tables = face->toc.tables; for ( n = 0; n < toc->count; n++ ) { if ( FT_STREAM_READ_FIELDS( pcf_table_header, tables ) ) goto Exit; tables++; } #if defined( FT_DEBUG_LEVEL_TRACE ) { FT_UInt i, j; const char* name = "?"; FT_TRACE4(( "Tables count: %ld\n", face->toc.count )); tables = face->toc.tables; for ( i = 0; i < toc->count; i++ ) { for( j = 0; j < sizeof ( tableNames ) / sizeof ( tableNames[0] ); j++ ) if ( tables[i].type == (FT_UInt)( 1 << j ) ) name = tableNames[j]; FT_TRACE4(( "Table %d: type=%-6s format=0x%04lX " "size=0x%06lX (%8ld) offset=0x%04lX\n", i, name, tables[i].format, tables[i].size, tables[i].size, tables[i].offset )); } } #endif return PCF_Err_Ok; Exit: FT_FREE( face->toc.tables ); return error; }
static FT_Error pcf_get_accel( FT_Stream stream, PCF_Face face, FT_ULong type ) { FT_ULong format, size; FT_Error error; PCF_Accel accel = &face->accel; error = pcf_seek_to_table_type( stream, face->toc.tables, face->toc.count, type, &format, &size ); if ( error ) goto Bail; if ( FT_READ_ULONG_LE( format ) ) goto Bail; if ( !PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) && !PCF_FORMAT_MATCH( format, PCF_ACCEL_W_INKBOUNDS ) ) goto Bail; if ( PCF_BYTE_ORDER( format ) == MSBFirst ) { if ( FT_STREAM_READ_FIELDS( pcf_accel_msb_header, accel ) ) goto Bail; } else { if ( FT_STREAM_READ_FIELDS( pcf_accel_header, accel ) ) goto Bail; } error = pcf_get_metric( stream, format & ( ~PCF_FORMAT_MASK ), &(accel->minbounds) ); if ( error ) goto Bail; error = pcf_get_metric( stream, format & ( ~PCF_FORMAT_MASK ), &(accel->maxbounds) ); if ( error ) goto Bail; if ( PCF_FORMAT_MATCH( format, PCF_ACCEL_W_INKBOUNDS ) ) { error = pcf_get_metric( stream, format & ( ~PCF_FORMAT_MASK ), &(accel->ink_minbounds) ); if ( error ) goto Bail; error = pcf_get_metric( stream, format & ( ~PCF_FORMAT_MASK ), &(accel->ink_maxbounds) ); if ( error ) goto Bail; } else { accel->ink_minbounds = accel->minbounds; /* I'm not sure about this */ accel->ink_maxbounds = accel->maxbounds; } Bail: return error; }
tt_face_load_font_dir( TT_Face face, FT_Stream stream ) { SFNT_HeaderRec sfnt; FT_Error error; FT_Memory memory = stream->memory; FT_UShort nn, valid_entries; static const FT_Frame_Field offset_table_fields[] = { #undef FT_STRUCTURE #define FT_STRUCTURE SFNT_HeaderRec FT_FRAME_START( 8 ), FT_FRAME_USHORT( num_tables ), FT_FRAME_USHORT( search_range ), FT_FRAME_USHORT( entry_selector ), FT_FRAME_USHORT( range_shift ), FT_FRAME_END }; FT_TRACE2(( "tt_face_load_font_dir: %08p\n", face )); /* read the offset table */ sfnt.offset = FT_STREAM_POS(); if ( FT_READ_ULONG( sfnt.format_tag ) || FT_STREAM_READ_FIELDS( offset_table_fields, &sfnt ) ) goto Exit; /* many fonts don't have these fields set correctly */ #if 0 if ( sfnt.search_range != 1 << ( sfnt.entry_selector + 4 ) || sfnt.search_range + sfnt.range_shift != sfnt.num_tables << 4 ) return FT_THROW( Unknown_File_Format ); #endif /* load the table directory */ FT_TRACE2(( "-- Number of tables: %10u\n", sfnt.num_tables )); FT_TRACE2(( "-- Format version: 0x%08lx\n", sfnt.format_tag )); if ( sfnt.format_tag != TTAG_OTTO ) { /* check first */ error = check_table_dir( &sfnt, stream, &valid_entries ); if ( error ) { FT_TRACE2(( "tt_face_load_font_dir:" " invalid table directory for TrueType\n" )); goto Exit; } } else valid_entries = sfnt.num_tables; face->num_tables = valid_entries; face->format_tag = sfnt.format_tag; if ( FT_QNEW_ARRAY( face->dir_tables, face->num_tables ) ) goto Exit; if ( FT_STREAM_SEEK( sfnt.offset + 12 ) || FT_FRAME_ENTER( sfnt.num_tables * 16L ) ) goto Exit; FT_TRACE2(( "\n" " tag offset length checksum\n" " ----------------------------------\n" )); valid_entries = 0; for ( nn = 0; nn < sfnt.num_tables; nn++ ) { TT_TableRec entry; FT_UShort i; FT_Bool duplicate; entry.Tag = FT_GET_TAG4(); entry.CheckSum = FT_GET_ULONG(); entry.Offset = FT_GET_ULONG(); entry.Length = FT_GET_ULONG(); /* ignore invalid tables that can't be sanitized */ if ( entry.Offset > stream->size ) continue; else if ( entry.Length > stream->size - entry.Offset ) { if ( entry.Tag == TTAG_hmtx || entry.Tag == TTAG_vmtx ) { #ifdef FT_DEBUG_LEVEL_TRACE FT_ULong old_length = entry.Length; #endif /* make metrics table length a multiple of 4 */ entry.Length = ( stream->size - entry.Offset ) & ~3U; FT_TRACE2(( " %c%c%c%c %08lx %08lx %08lx" " (sanitized; original length %08lx)", (FT_Char)( entry.Tag >> 24 ), (FT_Char)( entry.Tag >> 16 ), (FT_Char)( entry.Tag >> 8 ), (FT_Char)( entry.Tag ), entry.Offset, entry.Length, entry.CheckSum, old_length )); } else continue; }