1 /****************************************************************************
5 * TrueTypeGX/AAT common tables validation (body).
7 * Copyright (C) 2004-2020 by
8 * suzuki toshiya, Masatake YAMATO, Red Hat K.K.,
9 * David Turner, Robert Wilhelm, and Werner Lemberg.
11 * This file is part of the FreeType project, and may only be used,
12 * modified, and distributed under the terms of the FreeType project
13 * license, LICENSE.TXT. By continuing to use, modify, or distribute
14 * this file you indicate that you have read the license and
15 * understand and accept it fully.
19 /****************************************************************************
21 * gxvalid is derived from both gxlayout module and otvalid module.
22 * Development of gxlayout is supported by the Information-technology
23 * Promotion Agency(IPA), Japan.
31 /**************************************************************************
33 * The macro FT_COMPONENT is used in trace mode. It is an implicit
34 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
35 * messages during execution.
38 #define FT_COMPONENT gxvcommon
41 /*************************************************************************/
42 /*************************************************************************/
44 /***** 16bit offset sorter *****/
46 /*************************************************************************/
47 /*************************************************************************/
50 gxv_compare_ushort_offset( FT_UShort* a,
63 gxv_set_length_by_ushort_offset( FT_UShort* offset,
68 GXV_Validator gxvalid )
73 for ( i = 0; i < nmemb; i++ )
76 for ( i = 0; i < nmemb; i++ )
80 ft_qsort( buff, ( nmemb + 1 ), sizeof ( FT_UShort ),
81 ( int(*)(const void*, const void*) )gxv_compare_ushort_offset );
83 if ( buff[nmemb] > limit )
86 for ( i = 0; i < nmemb; i++ )
91 for ( j = 0; j < nmemb; j++ )
92 if ( buff[j] == offset[i] )
98 *(length[i]) = (FT_UShort)( buff[j + 1] - buff[j] );
100 if ( 0 != offset[i] && 0 == *(length[i]) )
106 /*************************************************************************/
107 /*************************************************************************/
109 /***** 32bit offset sorter *****/
111 /*************************************************************************/
112 /*************************************************************************/
115 gxv_compare_ulong_offset( FT_ULong* a,
128 gxv_set_length_by_ulong_offset( FT_ULong* offset,
133 GXV_Validator gxvalid)
138 for ( i = 0; i < nmemb; i++ )
141 for ( i = 0; i < nmemb; i++ )
145 ft_qsort( buff, ( nmemb + 1 ), sizeof ( FT_ULong ),
146 ( int(*)(const void*, const void*) )gxv_compare_ulong_offset );
148 if ( buff[nmemb] > limit )
151 for ( i = 0; i < nmemb; i++ )
156 for ( j = 0; j < nmemb; j++ )
157 if ( buff[j] == offset[i] )
163 *(length[i]) = buff[j + 1] - buff[j];
165 if ( 0 != offset[i] && 0 == *(length[i]) )
171 /*************************************************************************/
172 /*************************************************************************/
174 /***** scan value array and get min & max *****/
176 /*************************************************************************/
177 /*************************************************************************/
181 gxv_array_getlimits_byte( FT_Bytes table,
185 GXV_Validator gxvalid )
198 GXV_LIMIT_CHECK( 1 );
199 val = FT_NEXT_BYTE( p );
201 *min = (FT_Byte)FT_MIN( *min, val );
202 *max = (FT_Byte)FT_MAX( *max, val );
205 gxvalid->subtable_length = (FT_ULong)( p - table );
210 gxv_array_getlimits_ushort( FT_Bytes table,
214 GXV_Validator gxvalid )
227 GXV_LIMIT_CHECK( 2 );
228 val = FT_NEXT_USHORT( p );
230 *min = (FT_Byte)FT_MIN( *min, val );
231 *max = (FT_Byte)FT_MAX( *max, val );
234 gxvalid->subtable_length = (FT_ULong)( p - table );
238 /*************************************************************************/
239 /*************************************************************************/
241 /***** BINSEARCHHEADER *****/
243 /*************************************************************************/
244 /*************************************************************************/
246 typedef struct GXV_BinSrchHeader_
250 FT_UShort searchRange;
251 FT_UShort entrySelector;
252 FT_UShort rangeShift;
258 gxv_BinSrchHeader_check_consistency( GXV_BinSrchHeader* binSrchHeader,
259 GXV_Validator gxvalid )
261 FT_UShort searchRange;
262 FT_UShort entrySelector;
263 FT_UShort rangeShift;
266 if ( binSrchHeader->unitSize == 0 )
269 if ( binSrchHeader->nUnits == 0 )
271 if ( binSrchHeader->searchRange == 0 &&
272 binSrchHeader->entrySelector == 0 &&
273 binSrchHeader->rangeShift == 0 )
279 for ( searchRange = 1, entrySelector = 1;
280 ( searchRange * 2 ) <= binSrchHeader->nUnits &&
281 searchRange < 0x8000U;
282 searchRange *= 2, entrySelector++ )
286 searchRange = (FT_UShort)( searchRange * binSrchHeader->unitSize );
287 rangeShift = (FT_UShort)( binSrchHeader->nUnits * binSrchHeader->unitSize
290 if ( searchRange != binSrchHeader->searchRange ||
291 entrySelector != binSrchHeader->entrySelector ||
292 rangeShift != binSrchHeader->rangeShift )
294 GXV_TRACE(( "Inconsistency found in BinSrchHeader\n" ));
295 GXV_TRACE(( "originally: unitSize=%d, nUnits=%d, "
296 "searchRange=%d, entrySelector=%d, "
298 binSrchHeader->unitSize, binSrchHeader->nUnits,
299 binSrchHeader->searchRange, binSrchHeader->entrySelector,
300 binSrchHeader->rangeShift ));
301 GXV_TRACE(( "calculated: unitSize=%d, nUnits=%d, "
302 "searchRange=%d, entrySelector=%d, "
304 binSrchHeader->unitSize, binSrchHeader->nUnits,
305 searchRange, entrySelector, rangeShift ));
307 GXV_SET_ERR_IF_PARANOID( FT_INVALID_DATA );
313 * parser & validator of BinSrchHeader
314 * which is used in LookupTable format 2, 4, 6.
316 * Essential parameters (unitSize, nUnits) are returned by
317 * given pointer, others (searchRange, entrySelector, rangeShift)
318 * can be calculated by essential parameters, so they are just
319 * validated and discarded.
321 * However, wrong values in searchRange, entrySelector, rangeShift
322 * won't cause fatal errors, because these parameters might be
323 * only used in old m68k font driver in MacOS.
324 * -- suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>
328 gxv_BinSrchHeader_validate( FT_Bytes table,
330 FT_UShort* unitSize_p,
332 GXV_Validator gxvalid )
335 GXV_BinSrchHeader binSrchHeader;
338 GXV_NAME_ENTER( "BinSrchHeader validate" );
340 if ( *unitSize_p == 0 )
342 GXV_LIMIT_CHECK( 2 );
343 binSrchHeader.unitSize = FT_NEXT_USHORT( p );
346 binSrchHeader.unitSize = *unitSize_p;
348 if ( *nUnits_p == 0 )
350 GXV_LIMIT_CHECK( 2 );
351 binSrchHeader.nUnits = FT_NEXT_USHORT( p );
354 binSrchHeader.nUnits = *nUnits_p;
356 GXV_LIMIT_CHECK( 2 + 2 + 2 );
357 binSrchHeader.searchRange = FT_NEXT_USHORT( p );
358 binSrchHeader.entrySelector = FT_NEXT_USHORT( p );
359 binSrchHeader.rangeShift = FT_NEXT_USHORT( p );
360 GXV_TRACE(( "nUnits %d\n", binSrchHeader.nUnits ));
362 gxv_BinSrchHeader_check_consistency( &binSrchHeader, gxvalid );
364 if ( *unitSize_p == 0 )
365 *unitSize_p = binSrchHeader.unitSize;
367 if ( *nUnits_p == 0 )
368 *nUnits_p = binSrchHeader.nUnits;
370 gxvalid->subtable_length = (FT_ULong)( p - table );
375 /*************************************************************************/
376 /*************************************************************************/
378 /***** LOOKUP TABLE *****/
380 /*************************************************************************/
381 /*************************************************************************/
383 #define GXV_LOOKUP_VALUE_LOAD( P, SIGNSPEC ) \
384 ( P += 2, gxv_lookup_value_load( P - 2, SIGNSPEC ) )
386 static GXV_LookupValueDesc
387 gxv_lookup_value_load( FT_Bytes p,
388 GXV_LookupValue_SignSpec signspec )
390 GXV_LookupValueDesc v;
393 if ( signspec == GXV_LOOKUPVALUE_UNSIGNED )
394 v.u = FT_NEXT_USHORT( p );
396 v.s = FT_NEXT_SHORT( p );
402 #define GXV_UNITSIZE_VALIDATE( FORMAT, UNITSIZE, NUNITS, CORRECTSIZE ) \
404 if ( UNITSIZE != CORRECTSIZE ) \
406 FT_ERROR(( "unitSize=%d differs from" \
407 " expected unitSize=%d" \
408 " in LookupTable %s\n", \
409 UNITSIZE, CORRECTSIZE, FORMAT )); \
410 if ( UNITSIZE != 0 && NUNITS != 0 ) \
412 FT_ERROR(( " cannot validate anymore\n" )); \
416 FT_ERROR(( " forcibly continues\n" )); \
421 /* ================= Simple Array Format 0 Lookup Table ================ */
423 gxv_LookupTable_fmt0_validate( FT_Bytes table,
425 GXV_Validator gxvalid )
430 GXV_LookupValueDesc value;
433 GXV_NAME_ENTER( "LookupTable format 0" );
435 GXV_LIMIT_CHECK( 2 * gxvalid->face->num_glyphs );
437 for ( i = 0; i < gxvalid->face->num_glyphs; i++ )
439 GXV_LIMIT_CHECK( 2 );
440 if ( p + 2 >= limit ) /* some fonts have too-short fmt0 array */
442 GXV_TRACE(( "too short, glyphs %d - %d are missing\n",
443 i, gxvalid->face->num_glyphs ));
444 GXV_SET_ERR_IF_PARANOID( FT_INVALID_GLYPH_ID );
448 value = GXV_LOOKUP_VALUE_LOAD( p, gxvalid->lookupval_sign );
449 gxvalid->lookupval_func( i, &value, gxvalid );
452 gxvalid->subtable_length = (FT_ULong)( p - table );
457 /* ================= Segment Single Format 2 Lookup Table ============== */
461 * To guarantee that a binary search terminates, you must include one or
462 * more special `end of search table' values at the end of the data to
463 * be searched. The number of termination values that need to be
464 * included is table-specific. The value that indicates binary search
465 * termination is 0xFFFF.
467 * The problem is that nUnits does not include this end-marker. It's
468 * quite difficult to discriminate whether the following 0xFFFF comes from
469 * the end-marker or some next data.
471 * -- suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>
474 gxv_LookupTable_fmt2_skip_endmarkers( FT_Bytes table,
476 GXV_Validator gxvalid )
481 while ( ( p + 4 ) < gxvalid->root->limit )
483 if ( p[0] != 0xFF || p[1] != 0xFF || /* lastGlyph */
484 p[2] != 0xFF || p[3] != 0xFF ) /* firstGlyph */
489 gxvalid->subtable_length = (FT_ULong)( p - table );
494 gxv_LookupTable_fmt2_validate( FT_Bytes table,
496 GXV_Validator gxvalid )
505 FT_UShort firstGlyph;
506 GXV_LookupValueDesc value;
509 GXV_NAME_ENTER( "LookupTable format 2" );
511 unitSize = nUnits = 0;
512 gxv_BinSrchHeader_validate( p, limit, &unitSize, &nUnits, gxvalid );
513 p += gxvalid->subtable_length;
515 GXV_UNITSIZE_VALIDATE( "format2", unitSize, nUnits, 6 );
517 for ( unit = 0, gid = 0; unit < nUnits; unit++ )
519 GXV_LIMIT_CHECK( 2 + 2 + 2 );
520 lastGlyph = FT_NEXT_USHORT( p );
521 firstGlyph = FT_NEXT_USHORT( p );
522 value = GXV_LOOKUP_VALUE_LOAD( p, gxvalid->lookupval_sign );
524 gxv_glyphid_validate( firstGlyph, gxvalid );
525 gxv_glyphid_validate( lastGlyph, gxvalid );
527 if ( lastGlyph < gid )
529 GXV_TRACE(( "reverse ordered segment specification:"
530 " lastGlyph[%d]=%d < lastGlyph[%d]=%d\n",
531 unit, lastGlyph, unit - 1 , gid ));
532 GXV_SET_ERR_IF_PARANOID( FT_INVALID_GLYPH_ID );
535 if ( lastGlyph < firstGlyph )
537 GXV_TRACE(( "reverse ordered range specification at unit %d:",
538 " lastGlyph %d < firstGlyph %d ",
539 unit, lastGlyph, firstGlyph ));
540 GXV_SET_ERR_IF_PARANOID( FT_INVALID_GLYPH_ID );
542 if ( gxvalid->root->level == FT_VALIDATE_TIGHT )
543 continue; /* ftxvalidator silently skips such an entry */
545 FT_TRACE4(( "continuing with exchanged values\n" ));
547 firstGlyph = lastGlyph;
551 for ( gid = firstGlyph; gid <= lastGlyph; gid++ )
552 gxvalid->lookupval_func( gid, &value, gxvalid );
555 gxv_LookupTable_fmt2_skip_endmarkers( p, unitSize, gxvalid );
556 p += gxvalid->subtable_length;
558 gxvalid->subtable_length = (FT_ULong)( p - table );
563 /* ================= Segment Array Format 4 Lookup Table =============== */
565 gxv_LookupTable_fmt4_validate( FT_Bytes table,
567 GXV_Validator gxvalid )
576 FT_UShort firstGlyph;
577 GXV_LookupValueDesc base_value;
578 GXV_LookupValueDesc value;
581 GXV_NAME_ENTER( "LookupTable format 4" );
583 unitSize = nUnits = 0;
584 gxv_BinSrchHeader_validate( p, limit, &unitSize, &nUnits, gxvalid );
585 p += gxvalid->subtable_length;
587 GXV_UNITSIZE_VALIDATE( "format4", unitSize, nUnits, 6 );
589 for ( unit = 0, gid = 0; unit < nUnits; unit++ )
591 GXV_LIMIT_CHECK( 2 + 2 );
592 lastGlyph = FT_NEXT_USHORT( p );
593 firstGlyph = FT_NEXT_USHORT( p );
595 gxv_glyphid_validate( firstGlyph, gxvalid );
596 gxv_glyphid_validate( lastGlyph, gxvalid );
598 if ( lastGlyph < gid )
600 GXV_TRACE(( "reverse ordered segment specification:"
601 " lastGlyph[%d]=%d < lastGlyph[%d]=%d\n",
602 unit, lastGlyph, unit - 1 , gid ));
603 GXV_SET_ERR_IF_PARANOID( FT_INVALID_GLYPH_ID );
606 if ( lastGlyph < firstGlyph )
608 GXV_TRACE(( "reverse ordered range specification at unit %d:",
609 " lastGlyph %d < firstGlyph %d ",
610 unit, lastGlyph, firstGlyph ));
611 GXV_SET_ERR_IF_PARANOID( FT_INVALID_GLYPH_ID );
613 if ( gxvalid->root->level == FT_VALIDATE_TIGHT )
614 continue; /* ftxvalidator silently skips such an entry */
616 FT_TRACE4(( "continuing with exchanged values\n" ));
618 firstGlyph = lastGlyph;
622 GXV_LIMIT_CHECK( 2 );
623 base_value = GXV_LOOKUP_VALUE_LOAD( p, GXV_LOOKUPVALUE_UNSIGNED );
625 for ( gid = firstGlyph; gid <= lastGlyph; gid++ )
627 value = gxvalid->lookupfmt4_trans( (FT_UShort)( gid - firstGlyph ),
632 gxvalid->lookupval_func( gid, &value, gxvalid );
636 gxv_LookupTable_fmt2_skip_endmarkers( p, unitSize, gxvalid );
637 p += gxvalid->subtable_length;
639 gxvalid->subtable_length = (FT_ULong)( p - table );
644 /* ================= Segment Table Format 6 Lookup Table =============== */
646 gxv_LookupTable_fmt6_skip_endmarkers( FT_Bytes table,
648 GXV_Validator gxvalid )
653 while ( p < gxvalid->root->limit )
655 if ( p[0] != 0xFF || p[1] != 0xFF )
660 gxvalid->subtable_length = (FT_ULong)( p - table );
665 gxv_LookupTable_fmt6_validate( FT_Bytes table,
667 GXV_Validator gxvalid )
671 FT_UShort prev_glyph;
676 GXV_LookupValueDesc value;
679 GXV_NAME_ENTER( "LookupTable format 6" );
681 unitSize = nUnits = 0;
682 gxv_BinSrchHeader_validate( p, limit, &unitSize, &nUnits, gxvalid );
683 p += gxvalid->subtable_length;
685 GXV_UNITSIZE_VALIDATE( "format6", unitSize, nUnits, 4 );
687 for ( unit = 0, prev_glyph = 0; unit < nUnits; unit++ )
689 GXV_LIMIT_CHECK( 2 + 2 );
690 glyph = FT_NEXT_USHORT( p );
691 value = GXV_LOOKUP_VALUE_LOAD( p, gxvalid->lookupval_sign );
693 if ( gxv_glyphid_validate( glyph, gxvalid ) )
694 GXV_TRACE(( " endmarker found within defined range"
695 " (entry %d < nUnits=%d)\n",
698 if ( prev_glyph > glyph )
700 GXV_TRACE(( "current gid 0x%04x < previous gid 0x%04x\n",
701 glyph, prev_glyph ));
702 GXV_SET_ERR_IF_PARANOID( FT_INVALID_GLYPH_ID );
706 gxvalid->lookupval_func( glyph, &value, gxvalid );
709 gxv_LookupTable_fmt6_skip_endmarkers( p, unitSize, gxvalid );
710 p += gxvalid->subtable_length;
712 gxvalid->subtable_length = (FT_ULong)( p - table );
717 /* ================= Trimmed Array Format 8 Lookup Table =============== */
719 gxv_LookupTable_fmt8_validate( FT_Bytes table,
721 GXV_Validator gxvalid )
726 GXV_LookupValueDesc value;
727 FT_UShort firstGlyph;
728 FT_UShort glyphCount;
731 GXV_NAME_ENTER( "LookupTable format 8" );
733 /* firstGlyph + glyphCount */
734 GXV_LIMIT_CHECK( 2 + 2 );
735 firstGlyph = FT_NEXT_USHORT( p );
736 glyphCount = FT_NEXT_USHORT( p );
738 gxv_glyphid_validate( firstGlyph, gxvalid );
739 gxv_glyphid_validate( (FT_UShort)( firstGlyph + glyphCount ), gxvalid );
742 for ( i = 0; i < glyphCount; i++ )
744 GXV_LIMIT_CHECK( 2 );
745 value = GXV_LOOKUP_VALUE_LOAD( p, gxvalid->lookupval_sign );
746 gxvalid->lookupval_func( (FT_UShort)( firstGlyph + i ), &value, gxvalid );
749 gxvalid->subtable_length = (FT_ULong)( p - table );
755 gxv_LookupTable_validate( FT_Bytes table,
757 GXV_Validator gxvalid )
762 GXV_Validate_Func fmt_funcs_table[] =
764 gxv_LookupTable_fmt0_validate, /* 0 */
766 gxv_LookupTable_fmt2_validate, /* 2 */
768 gxv_LookupTable_fmt4_validate, /* 4 */
770 gxv_LookupTable_fmt6_validate, /* 6 */
772 gxv_LookupTable_fmt8_validate, /* 8 */
775 GXV_Validate_Func func;
778 GXV_NAME_ENTER( "LookupTable" );
780 /* lookuptbl_head may be used in fmt4 transit function. */
781 gxvalid->lookuptbl_head = table;
784 GXV_LIMIT_CHECK( 2 );
785 format = FT_NEXT_USHORT( p );
786 GXV_TRACE(( " (format %d)\n", format ));
791 func = fmt_funcs_table[format];
795 func( p, limit, gxvalid );
796 p += gxvalid->subtable_length;
798 gxvalid->subtable_length = (FT_ULong)( p - table );
804 /*************************************************************************/
805 /*************************************************************************/
807 /***** Glyph ID *****/
809 /*************************************************************************/
810 /*************************************************************************/
812 FT_LOCAL_DEF( FT_Int )
813 gxv_glyphid_validate( FT_UShort gid,
814 GXV_Validator gxvalid )
819 if ( gid == 0xFFFFU )
825 face = gxvalid->face;
826 if ( face->num_glyphs < gid )
828 GXV_TRACE(( " gxv_glyphid_check() gid overflow: num_glyphs %d < %d\n",
829 face->num_glyphs, gid ));
830 GXV_SET_ERR_IF_PARANOID( FT_INVALID_GLYPH_ID );
837 /*************************************************************************/
838 /*************************************************************************/
840 /***** CONTROL POINT *****/
842 /*************************************************************************/
843 /*************************************************************************/
846 gxv_ctlPoint_validate( FT_UShort gid,
848 GXV_Validator gxvalid )
858 face = gxvalid->face;
860 error = FT_Load_Glyph( face,
862 FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM );
867 outline = glyph->outline;
868 n_points = (FT_UShort)outline.n_points;
870 if ( !( ctl_point < n_points ) )
875 /*************************************************************************/
876 /*************************************************************************/
878 /***** SFNT NAME *****/
880 /*************************************************************************/
881 /*************************************************************************/
884 gxv_sfntName_validate( FT_UShort name_index,
887 GXV_Validator gxvalid )
894 GXV_NAME_ENTER( "sfntName" );
896 if ( name_index < min_index || max_index < name_index )
899 nnames = FT_Get_Sfnt_Name_Count( gxvalid->face );
900 for ( i = 0; i < nnames; i++ )
902 if ( FT_Get_Sfnt_Name( gxvalid->face, i, &name ) != FT_Err_Ok )
905 if ( name.name_id == name_index )
909 GXV_TRACE(( " nameIndex = %d (UNTITLED)\n", name_index ));
911 goto Exit; /* make compiler happy */
914 FT_TRACE1(( " nameIndex = %d (", name_index ));
915 GXV_TRACE_HEXDUMP_SFNTNAME( name );
916 FT_TRACE1(( ")\n" ));
923 /*************************************************************************/
924 /*************************************************************************/
926 /***** STATE TABLE *****/
928 /*************************************************************************/
929 /*************************************************************************/
931 /* -------------------------- Class Table --------------------------- */
934 * highestClass specifies how many classes are defined in this
935 * Class Subtable. Apple spec does not mention whether undefined
936 * holes in the class (e.g.: 0-3 are predefined, 4 is unused, 5 is used)
937 * are permitted. At present, holes in a defined class are not checked.
938 * -- suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>
942 gxv_ClassTable_validate( FT_Bytes table,
945 FT_Byte* maxClassID_p,
946 GXV_Validator gxvalid )
949 FT_Bytes limit = table + *length_p;
950 FT_UShort firstGlyph;
954 GXV_NAME_ENTER( "ClassTable" );
956 *maxClassID_p = 3; /* Classes 0, 2, and 3 are predefined */
958 GXV_LIMIT_CHECK( 2 + 2 );
959 firstGlyph = FT_NEXT_USHORT( p );
960 nGlyphs = FT_NEXT_USHORT( p );
962 GXV_TRACE(( " (firstGlyph = %d, nGlyphs = %d)\n", firstGlyph, nGlyphs ));
967 gxv_glyphid_validate( (FT_UShort)( firstGlyph + nGlyphs ), gxvalid );
970 FT_Byte nGlyphInClass[256];
975 FT_MEM_ZERO( nGlyphInClass, 256 );
978 for ( i = 0; i < nGlyphs; i++ )
980 GXV_LIMIT_CHECK( 1 );
981 classID = FT_NEXT_BYTE( p );
984 /* following classes should not appear in class array */
985 case 0: /* end of text */
986 case 2: /* out of bounds */
987 case 3: /* end of line */
991 case 1: /* out of bounds */
992 default: /* user-defined: 4 - ( stateSize - 1 ) */
993 if ( classID >= stateSize )
994 FT_INVALID_DATA; /* assign glyph to undefined state */
996 nGlyphInClass[classID]++;
1000 *length_p = (FT_UShort)( p - table );
1002 /* scan max ClassID in use */
1003 for ( i = 0; i < stateSize; i++ )
1004 if ( ( 3 < i ) && ( nGlyphInClass[i] > 0 ) )
1005 *maxClassID_p = (FT_Byte)i; /* XXX: Check Range? */
1009 GXV_TRACE(( "Declared stateSize=0x%02x, Used maxClassID=0x%02x\n",
1010 stateSize, *maxClassID_p ));
1015 /* --------------------------- State Array ----------------------------- */
1018 gxv_StateArray_validate( FT_Bytes table,
1019 FT_UShort* length_p,
1021 FT_UShort stateSize,
1022 FT_Byte* maxState_p,
1023 FT_Byte* maxEntry_p,
1024 GXV_Validator gxvalid )
1027 FT_Bytes limit = table + *length_p;
1031 FT_UNUSED( stateSize ); /* for the non-debugging case */
1034 GXV_NAME_ENTER( "StateArray" );
1036 GXV_TRACE(( "parse %d bytes by stateSize=%d maxClassID=%d\n",
1037 (int)(*length_p), stateSize, (int)(maxClassID) ));
1040 * 2 states are predefined and must be described in StateArray:
1041 * state 0 (start of text), 1 (start of line)
1043 GXV_LIMIT_CHECK( ( 1 + maxClassID ) * 2 );
1048 /* read if enough to read another state */
1049 while ( p + ( 1 + maxClassID ) <= limit )
1052 for ( clazz = 0; clazz <= maxClassID; clazz++ )
1054 entry = FT_NEXT_BYTE( p );
1055 *maxEntry_p = (FT_Byte)FT_MAX( *maxEntry_p, entry );
1058 GXV_TRACE(( "parsed: maxState=%d, maxEntry=%d\n",
1059 *maxState_p, *maxEntry_p ));
1061 *length_p = (FT_UShort)( p - table );
1067 /* --------------------------- Entry Table ----------------------------- */
1070 gxv_EntryTable_validate( FT_Bytes table,
1071 FT_UShort* length_p,
1073 FT_UShort stateArray,
1074 FT_UShort stateArray_length,
1076 FT_Bytes statetable_table,
1077 FT_Bytes statetable_limit,
1078 GXV_Validator gxvalid )
1081 FT_Bytes limit = table + *length_p;
1084 FT_Int entrySize = 2 + 2 + GXV_GLYPHOFFSET_SIZE( statetable );
1086 GXV_XStateTable_GlyphOffsetDesc glyphOffset;
1089 GXV_NAME_ENTER( "EntryTable" );
1091 GXV_TRACE(( "maxEntry=%d entrySize=%d\n", maxEntry, entrySize ));
1093 if ( ( maxEntry + 1 ) * entrySize > *length_p )
1095 GXV_SET_ERR_IF_PARANOID( FT_INVALID_TOO_SHORT );
1097 /* ftxvalidator and FontValidator both warn and continue */
1098 maxEntry = (FT_Byte)( *length_p / entrySize - 1 );
1099 GXV_TRACE(( "too large maxEntry, shrinking to %d fit EntryTable length\n",
1103 for ( entry = 0; entry <= maxEntry; entry++ )
1109 GXV_LIMIT_CHECK( 2 + 2 );
1110 newState = FT_NEXT_USHORT( p );
1111 flags = FT_NEXT_USHORT( p );
1114 if ( newState < stateArray ||
1115 stateArray + stateArray_length < newState )
1117 GXV_TRACE(( " newState offset 0x%04x is out of stateArray\n",
1119 GXV_SET_ERR_IF_PARANOID( FT_INVALID_OFFSET );
1123 if ( 0 != ( ( newState - stateArray ) % ( 1 + maxClassID ) ) )
1125 GXV_TRACE(( " newState offset 0x%04x is not aligned to %d-classes\n",
1126 newState, 1 + maxClassID ));
1127 GXV_SET_ERR_IF_PARANOID( FT_INVALID_OFFSET );
1131 state = (FT_Byte)( ( newState - stateArray ) / ( 1 + maxClassID ) );
1133 switch ( GXV_GLYPHOFFSET_FMT( statetable ) )
1135 case GXV_GLYPHOFFSET_NONE:
1136 glyphOffset.uc = 0; /* make compiler happy */
1139 case GXV_GLYPHOFFSET_UCHAR:
1140 glyphOffset.uc = FT_NEXT_BYTE( p );
1143 case GXV_GLYPHOFFSET_CHAR:
1144 glyphOffset.c = FT_NEXT_CHAR( p );
1147 case GXV_GLYPHOFFSET_USHORT:
1148 glyphOffset.u = FT_NEXT_USHORT( p );
1151 case GXV_GLYPHOFFSET_SHORT:
1152 glyphOffset.s = FT_NEXT_SHORT( p );
1155 case GXV_GLYPHOFFSET_ULONG:
1156 glyphOffset.ul = FT_NEXT_ULONG( p );
1159 case GXV_GLYPHOFFSET_LONG:
1160 glyphOffset.l = FT_NEXT_LONG( p );
1164 if ( gxvalid->statetable.entry_validate_func )
1165 gxvalid->statetable.entry_validate_func( state,
1173 *length_p = (FT_UShort)( p - table );
1179 /* =========================== State Table ============================= */
1181 FT_LOCAL_DEF( void )
1182 gxv_StateTable_subtable_setup( FT_UShort table_size,
1183 FT_UShort classTable,
1184 FT_UShort stateArray,
1185 FT_UShort entryTable,
1186 FT_UShort* classTable_length_p,
1187 FT_UShort* stateArray_length_p,
1188 FT_UShort* entryTable_length_p,
1189 GXV_Validator gxvalid )
1199 l[0] = classTable_length_p;
1200 l[1] = stateArray_length_p;
1201 l[2] = entryTable_length_p;
1203 gxv_set_length_by_ushort_offset( o, l, buff, 3, table_size, gxvalid );
1207 FT_LOCAL_DEF( void )
1208 gxv_StateTable_validate( FT_Bytes table,
1210 GXV_Validator gxvalid )
1212 FT_UShort stateSize;
1213 FT_UShort classTable; /* offset to Class(Sub)Table */
1214 FT_UShort stateArray; /* offset to StateArray */
1215 FT_UShort entryTable; /* offset to EntryTable */
1217 FT_UShort classTable_length;
1218 FT_UShort stateArray_length;
1219 FT_UShort entryTable_length;
1224 GXV_StateTable_Subtable_Setup_Func setup_func;
1229 GXV_NAME_ENTER( "StateTable" );
1231 GXV_TRACE(( "StateTable header\n" ));
1233 GXV_LIMIT_CHECK( 2 + 2 + 2 + 2 );
1234 stateSize = FT_NEXT_USHORT( p );
1235 classTable = FT_NEXT_USHORT( p );
1236 stateArray = FT_NEXT_USHORT( p );
1237 entryTable = FT_NEXT_USHORT( p );
1239 GXV_TRACE(( "stateSize=0x%04x\n", stateSize ));
1240 GXV_TRACE(( "offset to classTable=0x%04x\n", classTable ));
1241 GXV_TRACE(( "offset to stateArray=0x%04x\n", stateArray ));
1242 GXV_TRACE(( "offset to entryTable=0x%04x\n", entryTable ));
1244 if ( stateSize > 0xFF )
1247 if ( gxvalid->statetable.optdata_load_func )
1248 gxvalid->statetable.optdata_load_func( p, limit, gxvalid );
1250 if ( gxvalid->statetable.subtable_setup_func )
1251 setup_func = gxvalid->statetable.subtable_setup_func;
1253 setup_func = gxv_StateTable_subtable_setup;
1255 setup_func( (FT_UShort)( limit - table ),
1264 GXV_TRACE(( "StateTable Subtables\n" ));
1266 if ( classTable != 0 )
1267 gxv_ClassTable_validate( table + classTable,
1273 maxClassID = (FT_Byte)( stateSize - 1 );
1275 if ( stateArray != 0 )
1276 gxv_StateArray_validate( table + stateArray,
1286 maxState = 1; /* 0:start of text, 1:start of line are predefined */
1291 if ( maxEntry > 0 && entryTable == 0 )
1294 if ( entryTable != 0 )
1295 gxv_EntryTable_validate( table + entryTable,
1309 /* ================= eXtended State Table (for morx) =================== */
1311 FT_LOCAL_DEF( void )
1312 gxv_XStateTable_subtable_setup( FT_ULong table_size,
1313 FT_ULong classTable,
1314 FT_ULong stateArray,
1315 FT_ULong entryTable,
1316 FT_ULong* classTable_length_p,
1317 FT_ULong* stateArray_length_p,
1318 FT_ULong* entryTable_length_p,
1319 GXV_Validator gxvalid )
1329 l[0] = classTable_length_p;
1330 l[1] = stateArray_length_p;
1331 l[2] = entryTable_length_p;
1333 gxv_set_length_by_ulong_offset( o, l, buff, 3, table_size, gxvalid );
1338 gxv_XClassTable_lookupval_validate( FT_UShort glyph,
1339 GXV_LookupValueCPtr value_p,
1340 GXV_Validator gxvalid )
1344 if ( value_p->u >= gxvalid->xstatetable.nClasses )
1346 if ( value_p->u > gxvalid->xstatetable.maxClassID )
1347 gxvalid->xstatetable.maxClassID = value_p->u;
1352 +===============+ --------+
1359 | firstGlyph[0] | | head of lookup table
1360 +---------------+ | +
1361 | offset[0] | -> | offset [byte]
1362 +===============+ | +
1363 | lastGlyph[1] | | (glyphID - firstGlyph) * 2 [byte]
1377 static GXV_LookupValueDesc
1378 gxv_XClassTable_lookupfmt4_transit( FT_UShort relative_gindex,
1379 GXV_LookupValueCPtr base_value_p,
1380 FT_Bytes lookuptbl_limit,
1381 GXV_Validator gxvalid )
1386 GXV_LookupValueDesc value;
1388 /* XXX: check range? */
1389 offset = (FT_UShort)( base_value_p->u +
1390 relative_gindex * sizeof ( FT_UShort ) );
1392 p = gxvalid->lookuptbl_head + offset;
1393 limit = lookuptbl_limit;
1395 GXV_LIMIT_CHECK ( 2 );
1396 value.u = FT_NEXT_USHORT( p );
1403 gxv_XStateArray_validate( FT_Bytes table,
1405 FT_UShort maxClassID,
1407 FT_UShort* maxState_p,
1408 FT_UShort* maxEntry_p,
1409 GXV_Validator gxvalid )
1412 FT_Bytes limit = table + *length_p;
1416 FT_UNUSED( stateSize ); /* for the non-debugging case */
1419 GXV_NAME_ENTER( "XStateArray" );
1421 GXV_TRACE(( "parse % 3d bytes by stateSize=% 3d maxClassID=% 3d\n",
1422 (int)(*length_p), stateSize, (int)(maxClassID) ));
1425 * 2 states are predefined and must be described:
1426 * state 0 (start of text), 1 (start of line)
1428 GXV_LIMIT_CHECK( ( 1 + maxClassID ) * 2 * 2 );
1433 /* read if enough to read another state */
1434 while ( p + ( ( 1 + maxClassID ) * 2 ) <= limit )
1437 for ( clazz = 0; clazz <= maxClassID; clazz++ )
1439 entry = FT_NEXT_USHORT( p );
1440 *maxEntry_p = (FT_UShort)FT_MAX( *maxEntry_p, entry );
1443 GXV_TRACE(( "parsed: maxState=%d, maxEntry=%d\n",
1444 *maxState_p, *maxEntry_p ));
1446 *length_p = (FT_ULong)( p - table );
1453 gxv_XEntryTable_validate( FT_Bytes table,
1456 FT_ULong stateArray_length,
1457 FT_UShort maxClassID,
1458 FT_Bytes xstatetable_table,
1459 FT_Bytes xstatetable_limit,
1460 GXV_Validator gxvalid )
1463 FT_Bytes limit = table + *length_p;
1466 FT_Int entrySize = 2 + 2 + GXV_GLYPHOFFSET_SIZE( xstatetable );
1469 GXV_NAME_ENTER( "XEntryTable" );
1470 GXV_TRACE(( "maxEntry=%d entrySize=%d\n", maxEntry, entrySize ));
1472 if ( ( p + ( maxEntry + 1 ) * entrySize ) > limit )
1473 FT_INVALID_TOO_SHORT;
1475 for (entry = 0; entry <= maxEntry; entry++ )
1477 FT_UShort newState_idx;
1479 GXV_XStateTable_GlyphOffsetDesc glyphOffset;
1482 GXV_LIMIT_CHECK( 2 + 2 );
1483 newState_idx = FT_NEXT_USHORT( p );
1484 flags = FT_NEXT_USHORT( p );
1486 if ( stateArray_length < (FT_ULong)( newState_idx * 2 ) )
1488 GXV_TRACE(( " newState index 0x%04x points out of stateArray\n",
1490 GXV_SET_ERR_IF_PARANOID( FT_INVALID_OFFSET );
1493 state = (FT_UShort)( newState_idx / ( 1 + maxClassID ) );
1494 if ( 0 != ( newState_idx % ( 1 + maxClassID ) ) )
1496 FT_TRACE4(( "-> new state = %d (supposed)\n"
1497 "but newState index 0x%04x is not aligned to %d-classes\n",
1498 state, newState_idx, 1 + maxClassID ));
1499 GXV_SET_ERR_IF_PARANOID( FT_INVALID_OFFSET );
1502 switch ( GXV_GLYPHOFFSET_FMT( xstatetable ) )
1504 case GXV_GLYPHOFFSET_NONE:
1505 glyphOffset.uc = 0; /* make compiler happy */
1508 case GXV_GLYPHOFFSET_UCHAR:
1509 glyphOffset.uc = FT_NEXT_BYTE( p );
1512 case GXV_GLYPHOFFSET_CHAR:
1513 glyphOffset.c = FT_NEXT_CHAR( p );
1516 case GXV_GLYPHOFFSET_USHORT:
1517 glyphOffset.u = FT_NEXT_USHORT( p );
1520 case GXV_GLYPHOFFSET_SHORT:
1521 glyphOffset.s = FT_NEXT_SHORT( p );
1524 case GXV_GLYPHOFFSET_ULONG:
1525 glyphOffset.ul = FT_NEXT_ULONG( p );
1528 case GXV_GLYPHOFFSET_LONG:
1529 glyphOffset.l = FT_NEXT_LONG( p );
1533 GXV_SET_ERR_IF_PARANOID( FT_INVALID_FORMAT );
1537 if ( gxvalid->xstatetable.entry_validate_func )
1538 gxvalid->xstatetable.entry_validate_func( state,
1547 *length_p = (FT_ULong)( p - table );
1553 FT_LOCAL_DEF( void )
1554 gxv_XStateTable_validate( FT_Bytes table,
1556 GXV_Validator gxvalid )
1558 /* StateHeader members */
1559 FT_ULong classTable; /* offset to Class(Sub)Table */
1560 FT_ULong stateArray; /* offset to StateArray */
1561 FT_ULong entryTable; /* offset to EntryTable */
1563 FT_ULong classTable_length;
1564 FT_ULong stateArray_length;
1565 FT_ULong entryTable_length;
1569 GXV_XStateTable_Subtable_Setup_Func setup_func;
1574 GXV_NAME_ENTER( "XStateTable" );
1576 GXV_TRACE(( "XStateTable header\n" ));
1578 GXV_LIMIT_CHECK( 4 + 4 + 4 + 4 );
1579 gxvalid->xstatetable.nClasses = FT_NEXT_ULONG( p );
1580 classTable = FT_NEXT_ULONG( p );
1581 stateArray = FT_NEXT_ULONG( p );
1582 entryTable = FT_NEXT_ULONG( p );
1584 GXV_TRACE(( "nClasses =0x%08x\n", gxvalid->xstatetable.nClasses ));
1585 GXV_TRACE(( "offset to classTable=0x%08x\n", classTable ));
1586 GXV_TRACE(( "offset to stateArray=0x%08x\n", stateArray ));
1587 GXV_TRACE(( "offset to entryTable=0x%08x\n", entryTable ));
1589 if ( gxvalid->xstatetable.nClasses > 0xFFFFU )
1592 GXV_TRACE(( "StateTable Subtables\n" ));
1594 if ( gxvalid->xstatetable.optdata_load_func )
1595 gxvalid->xstatetable.optdata_load_func( p, limit, gxvalid );
1597 if ( gxvalid->xstatetable.subtable_setup_func )
1598 setup_func = gxvalid->xstatetable.subtable_setup_func;
1600 setup_func = gxv_XStateTable_subtable_setup;
1602 setup_func( (FT_ULong)( limit - table ),
1611 if ( classTable != 0 )
1613 gxvalid->xstatetable.maxClassID = 0;
1614 gxvalid->lookupval_sign = GXV_LOOKUPVALUE_UNSIGNED;
1615 gxvalid->lookupval_func = gxv_XClassTable_lookupval_validate;
1616 gxvalid->lookupfmt4_trans = gxv_XClassTable_lookupfmt4_transit;
1617 gxv_LookupTable_validate( table + classTable,
1618 table + classTable + classTable_length,
1621 if ( gxvalid->subtable_length < classTable_length )
1622 classTable_length = gxvalid->subtable_length;
1627 /* XXX: check range? */
1628 gxvalid->xstatetable.maxClassID =
1629 (FT_UShort)( gxvalid->xstatetable.nClasses - 1 );
1632 if ( stateArray != 0 )
1633 gxv_XStateArray_validate( table + stateArray,
1635 gxvalid->xstatetable.maxClassID,
1636 gxvalid->xstatetable.nClasses,
1643 maxState = 1; /* 0:start of text, 1:start of line are predefined */
1648 if ( maxEntry > 0 && entryTable == 0 )
1651 if ( entryTable != 0 )
1652 gxv_XEntryTable_validate( table + entryTable,
1656 gxvalid->xstatetable.maxClassID,
1665 /*************************************************************************/
1666 /*************************************************************************/
1668 /***** Table overlapping *****/
1670 /*************************************************************************/
1671 /*************************************************************************/
1674 gxv_compare_ranges( FT_Bytes table1_start,
1675 FT_ULong table1_length,
1676 FT_Bytes table2_start,
1677 FT_ULong table2_length )
1679 if ( table1_start == table2_start )
1681 if ( ( table1_length == 0 || table2_length == 0 ) )
1684 else if ( table1_start < table2_start )
1686 if ( ( table1_start + table1_length ) <= table2_start )
1689 else if ( table1_start > table2_start )
1691 if ( ( table1_start >= table2_start + table2_length ) )
1701 FT_LOCAL_DEF( void )
1702 gxv_odtect_add_range( FT_Bytes start,
1704 const FT_String* name,
1705 GXV_odtect_Range odtect )
1707 odtect->range[odtect->nRanges].start = start;
1708 odtect->range[odtect->nRanges].length = length;
1709 odtect->range[odtect->nRanges].name = (FT_String*)name;
1714 FT_LOCAL_DEF( void )
1715 gxv_odtect_validate( GXV_odtect_Range odtect,
1716 GXV_Validator gxvalid )
1721 GXV_NAME_ENTER( "check overlap among multi ranges" );
1723 for ( i = 0; i < odtect->nRanges; i++ )
1724 for ( j = 0; j < i; j++ )
1725 if ( 0 != gxv_compare_ranges( odtect->range[i].start,
1726 odtect->range[i].length,
1727 odtect->range[j].start,
1728 odtect->range[j].length ) )
1730 #ifdef FT_DEBUG_LEVEL_TRACE
1731 if ( odtect->range[i].name || odtect->range[j].name )
1732 GXV_TRACE(( "found overlap between range %d and range %d\n",
1735 GXV_TRACE(( "found overlap between `%s' and `%s\'\n",
1736 odtect->range[i].name,
1737 odtect->range[j].name ));