1 /***************************************************************************/
5 /* TrueTypeGX/AAT just table validation (body). */
7 /* Copyright 2005-2018 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. */
17 /***************************************************************************/
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. */
25 /***************************************************************************/
31 #include FT_SFNT_NAMES_H
34 /*************************************************************************/
36 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
37 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
38 /* messages during execution. */
41 #define FT_COMPONENT trace_gxvjust
44 * referred `just' table format specification:
45 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6just.html
47 * ----------------------------------------------
48 * [JUST HEADER]: GXV_JUST_HEADER_SIZE
49 * version (fixed: 32bit) = 0x00010000
50 * format (uint16: 16bit) = 0 is only defined (2000)
51 * horizOffset (uint16: 16bit)
52 * vertOffset (uint16: 16bit)
53 * ----------------------------------------------
56 typedef struct GXV_just_DataRec_
58 FT_UShort wdc_offset_max
;
59 FT_UShort wdc_offset_min
;
60 FT_UShort pc_offset_max
;
61 FT_UShort pc_offset_min
;
63 } GXV_just_DataRec
, *GXV_just_Data
;
66 #define GXV_JUST_DATA( a ) GXV_TABLE_DATA( just, a )
69 /* GX just table does not define their subset of GID */
71 gxv_just_check_max_gid( FT_UShort gid
,
72 const FT_String
* msg_tag
,
73 GXV_Validator gxvalid
)
75 if ( gid
< gxvalid
->face
->num_glyphs
)
78 GXV_TRACE(( "just table includes too large %s"
79 " GID=%d > %d (in maxp)\n",
80 msg_tag
, gid
, gxvalid
->face
->num_glyphs
));
81 GXV_SET_ERR_IF_PARANOID( FT_INVALID_GLYPH_ID
);
86 gxv_just_wdp_entry_validate( FT_Bytes table
,
88 GXV_Validator gxvalid
)
92 #ifdef GXV_LOAD_UNUSED_VARS
93 FT_Fixed beforeGrowLimit
;
94 FT_Fixed beforeShrinkGrowLimit
;
95 FT_Fixed afterGrowLimit
;
96 FT_Fixed afterShrinkGrowLimit
;
98 FT_UShort shrinkFlags
;
102 GXV_LIMIT_CHECK( 4 + 4 + 4 + 4 + 4 + 2 + 2 );
103 justClass
= FT_NEXT_ULONG( p
);
104 #ifndef GXV_LOAD_UNUSED_VARS
105 p
+= 4 + 4 + 4 + 4 + 2 + 2;
107 beforeGrowLimit
= FT_NEXT_ULONG( p
);
108 beforeShrinkGrowLimit
= FT_NEXT_ULONG( p
);
109 afterGrowLimit
= FT_NEXT_ULONG( p
);
110 afterShrinkGrowLimit
= FT_NEXT_ULONG( p
);
111 growFlags
= FT_NEXT_USHORT( p
);
112 shrinkFlags
= FT_NEXT_USHORT( p
);
115 /* According to Apple spec, only 7bits in justClass is used */
116 if ( ( justClass
& 0xFFFFFF80UL
) != 0 )
118 GXV_TRACE(( "just table includes non-zero value"
119 " in unused justClass higher bits"
120 " of WidthDeltaPair" ));
121 GXV_SET_ERR_IF_PARANOID( FT_INVALID_DATA
);
124 gxvalid
->subtable_length
= (FT_ULong
)( p
- table
);
129 gxv_just_wdc_entry_validate( FT_Bytes table
,
131 GXV_Validator gxvalid
)
137 GXV_LIMIT_CHECK( 4 );
138 count
= FT_NEXT_ULONG( p
);
139 for ( i
= 0; i
< count
; i
++ )
141 GXV_TRACE(( "validating wdc pair %d/%d\n", i
+ 1, count
));
142 gxv_just_wdp_entry_validate( p
, limit
, gxvalid
);
143 p
+= gxvalid
->subtable_length
;
146 gxvalid
->subtable_length
= (FT_ULong
)( p
- table
);
151 gxv_just_widthDeltaClusters_validate( FT_Bytes table
,
153 GXV_Validator gxvalid
)
156 FT_Bytes wdc_end
= table
+ GXV_JUST_DATA( wdc_offset_max
);
160 GXV_NAME_ENTER( "just justDeltaClusters" );
162 if ( limit
<= wdc_end
)
165 for ( i
= 0; p
<= wdc_end
; i
++ )
167 gxv_just_wdc_entry_validate( p
, limit
, gxvalid
);
168 p
+= gxvalid
->subtable_length
;
171 gxvalid
->subtable_length
= (FT_ULong
)( p
- table
);
178 gxv_just_actSubrecord_type0_validate( FT_Bytes table
,
180 GXV_Validator gxvalid
)
186 #ifdef GXV_LOAD_UNUSED_VARS
189 FT_UShort decomposedCount
;
194 GXV_LIMIT_CHECK( 4 + 4 + 2 + 2 );
195 lowerLimit
= FT_NEXT_LONG( p
);
196 upperLimit
= FT_NEXT_LONG( p
);
197 #ifdef GXV_LOAD_UNUSED_VARS
198 order
= FT_NEXT_USHORT( p
);
202 decomposedCount
= FT_NEXT_USHORT( p
);
204 if ( lowerLimit
>= upperLimit
)
206 GXV_TRACE(( "just table includes invalid range spec:"
207 " lowerLimit(%d) > upperLimit(%d)\n" ));
208 GXV_SET_ERR_IF_PARANOID( FT_INVALID_DATA
);
211 for ( i
= 0; i
< decomposedCount
; i
++ )
216 GXV_LIMIT_CHECK( 2 );
217 glyphs
= FT_NEXT_USHORT( p
);
218 gxv_just_check_max_gid( glyphs
, "type0:glyphs", gxvalid
);
221 gxvalid
->subtable_length
= (FT_ULong
)( p
- table
);
226 gxv_just_actSubrecord_type1_validate( FT_Bytes table
,
228 GXV_Validator gxvalid
)
234 GXV_LIMIT_CHECK( 2 );
235 addGlyph
= FT_NEXT_USHORT( p
);
237 gxv_just_check_max_gid( addGlyph
, "type1:addGlyph", gxvalid
);
239 gxvalid
->subtable_length
= (FT_ULong
)( p
- table
);
244 gxv_just_actSubrecord_type2_validate( FT_Bytes table
,
246 GXV_Validator gxvalid
)
249 #ifdef GXV_LOAD_UNUSED_VARS
250 FT_Fixed substThreshhold
; /* Apple misspelled "Threshhold" */
253 FT_UShort substGlyph
;
256 GXV_LIMIT_CHECK( 4 + 2 + 2 );
257 #ifdef GXV_LOAD_UNUSED_VARS
258 substThreshhold
= FT_NEXT_ULONG( p
);
262 addGlyph
= FT_NEXT_USHORT( p
);
263 substGlyph
= FT_NEXT_USHORT( p
);
265 if ( addGlyph
!= 0xFFFF )
266 gxv_just_check_max_gid( addGlyph
, "type2:addGlyph", gxvalid
);
268 gxv_just_check_max_gid( substGlyph
, "type2:substGlyph", gxvalid
);
270 gxvalid
->subtable_length
= (FT_ULong
)( p
- table
);
275 gxv_just_actSubrecord_type4_validate( FT_Bytes table
,
277 GXV_Validator gxvalid
)
280 FT_ULong variantsAxis
;
281 FT_Fixed minimumLimit
;
282 FT_Fixed noStretchValue
;
283 FT_Fixed maximumLimit
;
286 GXV_LIMIT_CHECK( 4 + 4 + 4 + 4 );
287 variantsAxis
= FT_NEXT_ULONG( p
);
288 minimumLimit
= FT_NEXT_LONG( p
);
289 noStretchValue
= FT_NEXT_LONG( p
);
290 maximumLimit
= FT_NEXT_LONG( p
);
292 gxvalid
->subtable_length
= (FT_ULong
)( p
- table
);
294 if ( variantsAxis
!= 0x64756374L
) /* 'duct' */
295 GXV_TRACE(( "variantsAxis 0x%08x is non default value",
298 if ( minimumLimit
> noStretchValue
)
299 GXV_TRACE(( "type4:minimumLimit 0x%08x > noStretchValue 0x%08x\n",
300 minimumLimit
, noStretchValue
));
301 else if ( noStretchValue
> maximumLimit
)
302 GXV_TRACE(( "type4:noStretchValue 0x%08x > maximumLimit 0x%08x\n",
303 noStretchValue
, maximumLimit
));
304 else if ( !IS_PARANOID_VALIDATION
)
312 gxv_just_actSubrecord_type5_validate( FT_Bytes table
,
314 GXV_Validator gxvalid
)
321 GXV_LIMIT_CHECK( 2 + 2 );
322 flags
= FT_NEXT_USHORT( p
);
323 glyph
= FT_NEXT_USHORT( p
);
326 GXV_TRACE(( "type5: nonzero value 0x%04x in unused flags\n",
328 gxv_just_check_max_gid( glyph
, "type5:glyph", gxvalid
);
330 gxvalid
->subtable_length
= (FT_ULong
)( p
- table
);
334 /* parse single actSubrecord */
336 gxv_just_actSubrecord_validate( FT_Bytes table
,
338 GXV_Validator gxvalid
)
341 FT_UShort actionClass
;
342 FT_UShort actionType
;
343 FT_ULong actionLength
;
346 GXV_NAME_ENTER( "just actSubrecord" );
348 GXV_LIMIT_CHECK( 2 + 2 + 4 );
349 actionClass
= FT_NEXT_USHORT( p
);
350 actionType
= FT_NEXT_USHORT( p
);
351 actionLength
= FT_NEXT_ULONG( p
);
353 /* actionClass is related with justClass using 7bit only */
354 if ( ( actionClass
& 0xFF80 ) != 0 )
355 GXV_SET_ERR_IF_PARANOID( FT_INVALID_DATA
);
357 if ( actionType
== 0 )
358 gxv_just_actSubrecord_type0_validate( p
, limit
, gxvalid
);
359 else if ( actionType
== 1 )
360 gxv_just_actSubrecord_type1_validate( p
, limit
, gxvalid
);
361 else if ( actionType
== 2 )
362 gxv_just_actSubrecord_type2_validate( p
, limit
, gxvalid
);
363 else if ( actionType
== 3 )
364 ; /* Stretch glyph action: no actionData */
365 else if ( actionType
== 4 )
366 gxv_just_actSubrecord_type4_validate( p
, limit
, gxvalid
);
367 else if ( actionType
== 5 )
368 gxv_just_actSubrecord_type5_validate( p
, limit
, gxvalid
);
372 gxvalid
->subtable_length
= actionLength
;
379 gxv_just_pcActionRecord_validate( FT_Bytes table
,
381 GXV_Validator gxvalid
)
384 FT_ULong actionCount
;
388 GXV_LIMIT_CHECK( 4 );
389 actionCount
= FT_NEXT_ULONG( p
);
390 GXV_TRACE(( "actionCount = %d\n", actionCount
));
392 for ( i
= 0; i
< actionCount
; i
++ )
394 gxv_just_actSubrecord_validate( p
, limit
, gxvalid
);
395 p
+= gxvalid
->subtable_length
;
398 gxvalid
->subtable_length
= (FT_ULong
)( p
- table
);
405 gxv_just_pcTable_LookupValue_entry_validate( FT_UShort glyph
,
406 GXV_LookupValueCPtr value_p
,
407 GXV_Validator gxvalid
)
411 if ( value_p
->u
> GXV_JUST_DATA( pc_offset_max
) )
412 GXV_JUST_DATA( pc_offset_max
) = value_p
->u
;
413 if ( value_p
->u
< GXV_JUST_DATA( pc_offset_max
) )
414 GXV_JUST_DATA( pc_offset_min
) = value_p
->u
;
419 gxv_just_pcLookupTable_validate( FT_Bytes table
,
421 GXV_Validator gxvalid
)
426 GXV_NAME_ENTER( "just pcLookupTable" );
427 GXV_JUST_DATA( pc_offset_max
) = 0x0000;
428 GXV_JUST_DATA( pc_offset_min
) = 0xFFFFU
;
430 gxvalid
->lookupval_sign
= GXV_LOOKUPVALUE_UNSIGNED
;
431 gxvalid
->lookupval_func
= gxv_just_pcTable_LookupValue_entry_validate
;
433 gxv_LookupTable_validate( p
, limit
, gxvalid
);
435 /* subtable_length is set by gxv_LookupTable_validate() */
442 gxv_just_postcompTable_validate( FT_Bytes table
,
444 GXV_Validator gxvalid
)
449 GXV_NAME_ENTER( "just postcompTable" );
451 gxv_just_pcLookupTable_validate( p
, limit
, gxvalid
);
452 p
+= gxvalid
->subtable_length
;
454 gxv_just_pcActionRecord_validate( p
, limit
, gxvalid
);
455 p
+= gxvalid
->subtable_length
;
457 gxvalid
->subtable_length
= (FT_ULong
)( p
- table
);
464 gxv_just_classTable_entry_validate(
467 GXV_StateTable_GlyphOffsetCPtr glyphOffset_p
,
470 GXV_Validator gxvalid
)
472 #ifdef GXV_LOAD_UNUSED_VARS
473 /* TODO: validate markClass & currentClass */
475 FT_UShort dontAdvance
;
477 FT_UShort currentClass
;
481 FT_UNUSED( glyphOffset_p
);
484 FT_UNUSED( gxvalid
);
486 #ifndef GXV_LOAD_UNUSED_VARS
489 setMark
= (FT_UShort
)( ( flags
>> 15 ) & 1 );
490 dontAdvance
= (FT_UShort
)( ( flags
>> 14 ) & 1 );
491 markClass
= (FT_UShort
)( ( flags
>> 7 ) & 0x7F );
492 currentClass
= (FT_UShort
)( flags
& 0x7F );
498 gxv_just_justClassTable_validate ( FT_Bytes table
,
500 GXV_Validator gxvalid
)
505 FT_ULong subFeatureFlags
;
508 GXV_NAME_ENTER( "just justClassTable" );
510 GXV_LIMIT_CHECK( 2 + 2 + 4 );
511 length
= FT_NEXT_USHORT( p
);
512 coverage
= FT_NEXT_USHORT( p
);
513 subFeatureFlags
= FT_NEXT_ULONG( p
);
515 GXV_TRACE(( " justClassTable: coverage = 0x%04x (%s) ", coverage
));
516 if ( ( coverage
& 0x4000 ) == 0 )
517 GXV_TRACE(( "ascending\n" ));
519 GXV_TRACE(( "descending\n" ));
521 if ( subFeatureFlags
)
522 GXV_TRACE(( " justClassTable: nonzero value (0x%08x)"
523 " in unused subFeatureFlags\n", subFeatureFlags
));
525 gxvalid
->statetable
.optdata
= NULL
;
526 gxvalid
->statetable
.optdata_load_func
= NULL
;
527 gxvalid
->statetable
.subtable_setup_func
= NULL
;
528 gxvalid
->statetable
.entry_glyphoffset_fmt
= GXV_GLYPHOFFSET_NONE
;
529 gxvalid
->statetable
.entry_validate_func
=
530 gxv_just_classTable_entry_validate
;
532 gxv_StateTable_validate( p
, table
+ length
, gxvalid
);
534 /* subtable_length is set by gxv_LookupTable_validate() */
541 gxv_just_wdcTable_LookupValue_validate( FT_UShort glyph
,
542 GXV_LookupValueCPtr value_p
,
543 GXV_Validator gxvalid
)
547 if ( value_p
->u
> GXV_JUST_DATA( wdc_offset_max
) )
548 GXV_JUST_DATA( wdc_offset_max
) = value_p
->u
;
549 if ( value_p
->u
< GXV_JUST_DATA( wdc_offset_min
) )
550 GXV_JUST_DATA( wdc_offset_min
) = value_p
->u
;
555 gxv_just_justData_lookuptable_validate( FT_Bytes table
,
557 GXV_Validator gxvalid
)
562 GXV_JUST_DATA( wdc_offset_max
) = 0x0000;
563 GXV_JUST_DATA( wdc_offset_min
) = 0xFFFFU
;
565 gxvalid
->lookupval_sign
= GXV_LOOKUPVALUE_UNSIGNED
;
566 gxvalid
->lookupval_func
= gxv_just_wdcTable_LookupValue_validate
;
568 gxv_LookupTable_validate( p
, limit
, gxvalid
);
570 /* subtable_length is set by gxv_LookupTable_validate() */
577 * gxv_just_justData_validate() parses and validates horizData, vertData.
580 gxv_just_justData_validate( FT_Bytes table
,
582 GXV_Validator gxvalid
)
585 * following 3 offsets are measured from the start of `just'
586 * (which table points to), not justData
588 FT_UShort justClassTableOffset
;
589 FT_UShort wdcTableOffset
;
590 FT_UShort pcTableOffset
;
593 GXV_ODTECT( 4, odtect
);
596 GXV_NAME_ENTER( "just justData" );
598 GXV_ODTECT_INIT( odtect
);
599 GXV_LIMIT_CHECK( 2 + 2 + 2 );
600 justClassTableOffset
= FT_NEXT_USHORT( p
);
601 wdcTableOffset
= FT_NEXT_USHORT( p
);
602 pcTableOffset
= FT_NEXT_USHORT( p
);
604 GXV_TRACE(( " (justClassTableOffset = 0x%04x)\n", justClassTableOffset
));
605 GXV_TRACE(( " (wdcTableOffset = 0x%04x)\n", wdcTableOffset
));
606 GXV_TRACE(( " (pcTableOffset = 0x%04x)\n", pcTableOffset
));
608 gxv_just_justData_lookuptable_validate( p
, limit
, gxvalid
);
609 gxv_odtect_add_range( p
, gxvalid
->subtable_length
,
610 "just_LookupTable", odtect
);
612 if ( wdcTableOffset
)
614 gxv_just_widthDeltaClusters_validate(
615 gxvalid
->root
->base
+ wdcTableOffset
, limit
, gxvalid
);
616 gxv_odtect_add_range( gxvalid
->root
->base
+ wdcTableOffset
,
617 gxvalid
->subtable_length
, "just_wdcTable", odtect
);
622 gxv_just_postcompTable_validate( gxvalid
->root
->base
+ pcTableOffset
,
624 gxv_odtect_add_range( gxvalid
->root
->base
+ pcTableOffset
,
625 gxvalid
->subtable_length
, "just_pcTable", odtect
);
628 if ( justClassTableOffset
)
630 gxv_just_justClassTable_validate(
631 gxvalid
->root
->base
+ justClassTableOffset
, limit
, gxvalid
);
632 gxv_odtect_add_range( gxvalid
->root
->base
+ justClassTableOffset
,
633 gxvalid
->subtable_length
, "just_justClassTable",
637 gxv_odtect_validate( odtect
, gxvalid
);
644 gxv_just_validate( FT_Bytes table
,
646 FT_Validator ftvalid
)
651 GXV_ValidatorRec gxvalidrec
;
652 GXV_Validator gxvalid
= &gxvalidrec
;
653 GXV_just_DataRec justrec
;
654 GXV_just_Data just
= &justrec
;
658 FT_UShort horizOffset
;
659 FT_UShort vertOffset
;
661 GXV_ODTECT( 3, odtect
);
664 GXV_ODTECT_INIT( odtect
);
666 gxvalid
->root
= ftvalid
;
667 gxvalid
->table_data
= just
;
668 gxvalid
->face
= face
;
670 FT_TRACE3(( "validating `just' table\n" ));
673 limit
= gxvalid
->root
->limit
;
675 GXV_LIMIT_CHECK( 4 + 2 + 2 + 2 );
676 version
= FT_NEXT_ULONG( p
);
677 format
= FT_NEXT_USHORT( p
);
678 horizOffset
= FT_NEXT_USHORT( p
);
679 vertOffset
= FT_NEXT_USHORT( p
);
680 gxv_odtect_add_range( table
, (FT_ULong
)( p
- table
),
681 "just header", odtect
);
684 /* Version 1.0 (always:2000) */
685 GXV_TRACE(( " (version = 0x%08x)\n", version
));
686 if ( version
!= 0x00010000UL
)
689 /* format 0 (always:2000) */
690 GXV_TRACE(( " (format = 0x%04x)\n", format
));
691 if ( format
!= 0x0000 )
694 GXV_TRACE(( " (horizOffset = %d)\n", horizOffset
));
695 GXV_TRACE(( " (vertOffset = %d)\n", vertOffset
));
698 /* validate justData */
699 if ( 0 < horizOffset
)
701 gxv_just_justData_validate( table
+ horizOffset
, limit
, gxvalid
);
702 gxv_odtect_add_range( table
+ horizOffset
, gxvalid
->subtable_length
,
703 "horizJustData", odtect
);
706 if ( 0 < vertOffset
)
708 gxv_just_justData_validate( table
+ vertOffset
, limit
, gxvalid
);
709 gxv_odtect_add_range( table
+ vertOffset
, gxvalid
->subtable_length
,
710 "vertJustData", odtect
);
713 gxv_odtect_validate( odtect
, gxvalid
);