mgh: fix for default HDD DMA mode, that wasn't correctly set
[open-ps2-loader.git] / thirdparty / freetype-2.3.12 / src / gxvalid / gxvcommn.c
blobde7ce6fdef1d2036c3513a899ee7fd11fc113351
1 /***************************************************************************/
2 /* */
3 /* gxvcommn.c */
4 /* */
5 /* TrueTypeGX/AAT common tables validation (body). */
6 /* */
7 /* Copyright 2004, 2005, 2009 */
8 /* by suzuki toshiya, Masatake YAMATO, Red Hat K.K., */
9 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* */
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. */
16 /* */
17 /***************************************************************************/
19 /***************************************************************************/
20 /* */
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. */
24 /* */
25 /***************************************************************************/
28 #include "gxvcommn.h"
31 /*************************************************************************/
32 /* */
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. */
36 /* */
37 #undef FT_COMPONENT
38 #define FT_COMPONENT trace_gxvcommon
41 /*************************************************************************/
42 /*************************************************************************/
43 /***** *****/
44 /***** 16bit offset sorter *****/
45 /***** *****/
46 /*************************************************************************/
47 /*************************************************************************/
49 static int
50 gxv_compare_ushort_offset( FT_UShort* a,
51 FT_UShort* b )
53 if ( *a < *b )
54 return -1;
55 else if ( *a > *b )
56 return 1;
57 else
58 return 0;
62 FT_LOCAL_DEF( void )
63 gxv_set_length_by_ushort_offset( FT_UShort* offset,
64 FT_UShort** length,
65 FT_UShort* buff,
66 FT_UInt nmemb,
67 FT_UShort limit,
68 GXV_Validator valid )
70 FT_UInt i;
73 for ( i = 0; i < nmemb; i++ )
74 *(length[i]) = 0;
76 for ( i = 0; i < nmemb; i++ )
77 buff[i] = offset[i];
78 buff[nmemb] = limit;
80 ft_qsort( buff, ( nmemb + 1 ), sizeof ( FT_UShort ),
81 ( int(*)(const void*, const void*) )gxv_compare_ushort_offset );
83 if ( buff[nmemb] > limit )
84 FT_INVALID_OFFSET;
86 for ( i = 0; i < nmemb; i++ )
88 FT_UInt j;
91 for ( j = 0; j < nmemb; j++ )
92 if ( buff[j] == offset[i] )
93 break;
95 if ( j == nmemb )
96 FT_INVALID_OFFSET;
98 *(length[i]) = (FT_UShort)( buff[j + 1] - buff[j] );
100 if ( 0 != offset[i] && 0 == *(length[i]) )
101 FT_INVALID_OFFSET;
106 /*************************************************************************/
107 /*************************************************************************/
108 /***** *****/
109 /***** 32bit offset sorter *****/
110 /***** *****/
111 /*************************************************************************/
112 /*************************************************************************/
114 static int
115 gxv_compare_ulong_offset( FT_ULong* a,
116 FT_ULong* b )
118 if ( *a < *b )
119 return -1;
120 else if ( *a > *b )
121 return 1;
122 else
123 return 0;
127 FT_LOCAL_DEF( void )
128 gxv_set_length_by_ulong_offset( FT_ULong* offset,
129 FT_ULong** length,
130 FT_ULong* buff,
131 FT_UInt nmemb,
132 FT_ULong limit,
133 GXV_Validator valid)
135 FT_UInt i;
138 for ( i = 0; i < nmemb; i++ )
139 *(length[i]) = 0;
141 for ( i = 0; i < nmemb; i++ )
142 buff[i] = offset[i];
143 buff[nmemb] = limit;
145 ft_qsort( buff, ( nmemb + 1 ), sizeof ( FT_ULong ),
146 ( int(*)(const void*, const void*) )gxv_compare_ulong_offset );
148 if ( buff[nmemb] > limit )
149 FT_INVALID_OFFSET;
151 for ( i = 0; i < nmemb; i++ )
153 FT_UInt j;
156 for ( j = 0; j < nmemb; j++ )
157 if ( buff[j] == offset[i] )
158 break;
160 if ( j == nmemb )
161 FT_INVALID_OFFSET;
163 *(length[i]) = buff[j + 1] - buff[j];
165 if ( 0 != offset[i] && 0 == *(length[i]) )
166 FT_INVALID_OFFSET;
171 /*************************************************************************/
172 /*************************************************************************/
173 /***** *****/
174 /***** scan value array and get min & max *****/
175 /***** *****/
176 /*************************************************************************/
177 /*************************************************************************/
180 FT_LOCAL_DEF( void )
181 gxv_array_getlimits_byte( FT_Bytes table,
182 FT_Bytes limit,
183 FT_Byte* min,
184 FT_Byte* max,
185 GXV_Validator valid )
187 FT_Bytes p = table;
190 *min = 0xFF;
191 *max = 0x00;
193 while ( p < limit )
195 FT_Byte val;
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 valid->subtable_length = p - table;
209 FT_LOCAL_DEF( void )
210 gxv_array_getlimits_ushort( FT_Bytes table,
211 FT_Bytes limit,
212 FT_UShort* min,
213 FT_UShort* max,
214 GXV_Validator valid )
216 FT_Bytes p = table;
219 *min = 0xFFFFU;
220 *max = 0x0000;
222 while ( p < limit )
224 FT_UShort val;
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 valid->subtable_length = p - table;
238 /*************************************************************************/
239 /*************************************************************************/
240 /***** *****/
241 /***** BINSEARCHHEADER *****/
242 /***** *****/
243 /*************************************************************************/
244 /*************************************************************************/
246 typedef struct GXV_BinSrchHeader_
248 FT_UShort unitSize;
249 FT_UShort nUnits;
250 FT_UShort searchRange;
251 FT_UShort entrySelector;
252 FT_UShort rangeShift;
254 } GXV_BinSrchHeader;
257 static void
258 gxv_BinSrchHeader_check_consistency( GXV_BinSrchHeader* binSrchHeader,
259 GXV_Validator valid )
261 FT_UShort searchRange;
262 FT_UShort entrySelector;
263 FT_UShort rangeShift;
266 if ( binSrchHeader->unitSize == 0 )
267 FT_INVALID_DATA;
269 if ( binSrchHeader->nUnits == 0 )
271 if ( binSrchHeader->searchRange == 0 &&
272 binSrchHeader->entrySelector == 0 &&
273 binSrchHeader->rangeShift == 0 )
274 return;
275 else
276 FT_INVALID_DATA;
279 for ( searchRange = 1, entrySelector = 1;
280 ( searchRange * 2 ) <= binSrchHeader->nUnits &&
281 searchRange < 0x8000U;
282 searchRange *= 2, entrySelector++ )
285 entrySelector--;
286 searchRange = (FT_UShort)( searchRange * binSrchHeader->unitSize );
287 rangeShift = (FT_UShort)( binSrchHeader->nUnits * binSrchHeader->unitSize
288 - searchRange );
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, "
297 "rangeShift=%d\n",
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, "
303 "rangeShift=%d\n",
304 binSrchHeader->unitSize, binSrchHeader->nUnits,
305 searchRange, entrySelector, rangeShift ));
307 if ( valid->root->level >= FT_VALIDATE_PARANOID )
308 FT_INVALID_DATA;
314 * parser & validator of BinSrchHeader
315 * which is used in LookupTable format 2, 4, 6.
317 * Essential parameters (unitSize, nUnits) are returned by
318 * given pointer, others (searchRange, entrySelector, rangeShift)
319 * can be calculated by essential parameters, so they are just
320 * validated and discarded.
322 * However, wrong values in searchRange, entrySelector, rangeShift
323 * won't cause fatal errors, because these parameters might be
324 * only used in old m68k font driver in MacOS.
325 * -- suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>
328 FT_LOCAL_DEF( void )
329 gxv_BinSrchHeader_validate( FT_Bytes table,
330 FT_Bytes limit,
331 FT_UShort* unitSize_p,
332 FT_UShort* nUnits_p,
333 GXV_Validator valid )
335 FT_Bytes p = table;
336 GXV_BinSrchHeader binSrchHeader;
339 GXV_NAME_ENTER( "BinSrchHeader validate" );
341 if ( *unitSize_p == 0 )
343 GXV_LIMIT_CHECK( 2 );
344 binSrchHeader.unitSize = FT_NEXT_USHORT( p );
346 else
347 binSrchHeader.unitSize = *unitSize_p;
349 if ( *nUnits_p == 0 )
351 GXV_LIMIT_CHECK( 2 );
352 binSrchHeader.nUnits = FT_NEXT_USHORT( p );
354 else
355 binSrchHeader.nUnits = *nUnits_p;
357 GXV_LIMIT_CHECK( 2 + 2 + 2 );
358 binSrchHeader.searchRange = FT_NEXT_USHORT( p );
359 binSrchHeader.entrySelector = FT_NEXT_USHORT( p );
360 binSrchHeader.rangeShift = FT_NEXT_USHORT( p );
361 GXV_TRACE(( "nUnits %d\n", binSrchHeader.nUnits ));
363 gxv_BinSrchHeader_check_consistency( &binSrchHeader, valid );
365 if ( *unitSize_p == 0 )
366 *unitSize_p = binSrchHeader.unitSize;
368 if ( *nUnits_p == 0 )
369 *nUnits_p = binSrchHeader.nUnits;
371 valid->subtable_length = p - table;
372 GXV_EXIT;
376 /*************************************************************************/
377 /*************************************************************************/
378 /***** *****/
379 /***** LOOKUP TABLE *****/
380 /***** *****/
381 /*************************************************************************/
382 /*************************************************************************/
384 #define GXV_LOOKUP_VALUE_LOAD( P, SIGNSPEC ) \
385 ( P += 2, gxv_lookup_value_load( P - 2, SIGNSPEC ) )
387 static GXV_LookupValueDesc
388 gxv_lookup_value_load( FT_Bytes p,
389 int signspec )
391 GXV_LookupValueDesc v;
394 if ( signspec == GXV_LOOKUPVALUE_UNSIGNED )
395 v.u = FT_NEXT_USHORT( p );
396 else
397 v.s = FT_NEXT_SHORT( p );
399 return v;
403 #define GXV_UNITSIZE_VALIDATE( FORMAT, UNITSIZE, NUNITS, CORRECTSIZE ) \
404 FT_BEGIN_STMNT \
405 if ( UNITSIZE != CORRECTSIZE ) \
407 FT_ERROR(( "unitSize=%d differs from" \
408 " expected unitSize=%d" \
409 " in LookupTable %s\n", \
410 UNITSIZE, CORRECTSIZE, FORMAT )); \
411 if ( UNITSIZE != 0 && NUNITS != 0 ) \
413 FT_ERROR(( " cannot validate anymore\n" )); \
414 FT_INVALID_FORMAT; \
416 else \
417 FT_ERROR(( " forcibly continues\n" )); \
419 FT_END_STMNT
422 /* ================= Simple Array Format 0 Lookup Table ================ */
423 static void
424 gxv_LookupTable_fmt0_validate( FT_Bytes table,
425 FT_Bytes limit,
426 GXV_Validator valid )
428 FT_Bytes p = table;
429 FT_UShort i;
431 GXV_LookupValueDesc value;
434 GXV_NAME_ENTER( "LookupTable format 0" );
436 GXV_LIMIT_CHECK( 2 * valid->face->num_glyphs );
438 for ( i = 0; i < valid->face->num_glyphs; i++ )
440 GXV_LIMIT_CHECK( 2 );
441 if ( p + 2 >= limit ) /* some fonts have too-short fmt0 array */
443 GXV_TRACE(( "too short, glyphs %d - %d are missing\n",
444 i, valid->face->num_glyphs ));
445 if ( valid->root->level >= FT_VALIDATE_PARANOID )
446 FT_INVALID_GLYPH_ID;
447 break;
450 value = GXV_LOOKUP_VALUE_LOAD( p, valid->lookupval_sign );
451 valid->lookupval_func( i, &value, valid );
454 valid->subtable_length = p - table;
455 GXV_EXIT;
459 /* ================= Segment Single Format 2 Loolup Table ============== */
461 * Apple spec says:
463 * To guarantee that a binary search terminates, you must include one or
464 * more special `end of search table' values at the end of the data to
465 * be searched. The number of termination values that need to be
466 * included is table-specific. The value that indicates binary search
467 * termination is 0xFFFF.
469 * The problem is that nUnits does not include this end-marker. It's
470 * quite difficult to discriminate whether the following 0xFFFF comes from
471 * the end-marker or some next data.
473 * -- suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>
475 static void
476 gxv_LookupTable_fmt2_skip_endmarkers( FT_Bytes table,
477 FT_UShort unitSize,
478 GXV_Validator valid )
480 FT_Bytes p = table;
483 while ( ( p + 4 ) < valid->root->limit )
485 if ( p[0] != 0xFF || p[1] != 0xFF || /* lastGlyph */
486 p[2] != 0xFF || p[3] != 0xFF ) /* firstGlyph */
487 break;
488 p += unitSize;
491 valid->subtable_length = p - table;
495 static void
496 gxv_LookupTable_fmt2_validate( FT_Bytes table,
497 FT_Bytes limit,
498 GXV_Validator valid )
500 FT_Bytes p = table;
501 FT_UShort gid;
503 FT_UShort unitSize;
504 FT_UShort nUnits;
505 FT_UShort unit;
506 FT_UShort lastGlyph;
507 FT_UShort firstGlyph;
508 GXV_LookupValueDesc value;
511 GXV_NAME_ENTER( "LookupTable format 2" );
513 unitSize = nUnits = 0;
514 gxv_BinSrchHeader_validate( p, limit, &unitSize, &nUnits, valid );
515 p += valid->subtable_length;
517 GXV_UNITSIZE_VALIDATE( "format2", unitSize, nUnits, 6 );
519 for ( unit = 0, gid = 0; unit < nUnits; unit++ )
521 GXV_LIMIT_CHECK( 2 + 2 + 2 );
522 lastGlyph = FT_NEXT_USHORT( p );
523 firstGlyph = FT_NEXT_USHORT( p );
524 value = GXV_LOOKUP_VALUE_LOAD( p, valid->lookupval_sign );
526 gxv_glyphid_validate( firstGlyph, valid );
527 gxv_glyphid_validate( lastGlyph, valid );
529 if ( lastGlyph < gid )
531 GXV_TRACE(( "reverse ordered segment specification:"
532 " lastGlyph[%d]=%d < lastGlyph[%d]=%d\n",
533 unit, lastGlyph, unit - 1 , gid ));
534 if ( valid->root->level >= FT_VALIDATE_PARANOID )
535 FT_INVALID_GLYPH_ID;
538 if ( lastGlyph < firstGlyph )
540 GXV_TRACE(( "reverse ordered range specification at unit %d:",
541 " lastGlyph %d < firstGlyph %d ",
542 unit, lastGlyph, firstGlyph ));
543 if ( valid->root->level >= FT_VALIDATE_PARANOID )
544 FT_INVALID_GLYPH_ID;
546 if ( valid->root->level == FT_VALIDATE_TIGHT )
547 continue; /* ftxvalidator silently skips such an entry */
549 FT_TRACE4(( "continuing with exchanged values\n" ));
550 gid = firstGlyph;
551 firstGlyph = lastGlyph;
552 lastGlyph = gid;
555 for ( gid = firstGlyph; gid <= lastGlyph; gid++ )
556 valid->lookupval_func( gid, &value, valid );
559 gxv_LookupTable_fmt2_skip_endmarkers( p, unitSize, valid );
560 p += valid->subtable_length;
562 valid->subtable_length = p - table;
563 GXV_EXIT;
567 /* ================= Segment Array Format 4 Lookup Table =============== */
568 static void
569 gxv_LookupTable_fmt4_validate( FT_Bytes table,
570 FT_Bytes limit,
571 GXV_Validator valid )
573 FT_Bytes p = table;
574 FT_UShort unit;
575 FT_UShort gid;
577 FT_UShort unitSize;
578 FT_UShort nUnits;
579 FT_UShort lastGlyph;
580 FT_UShort firstGlyph;
581 GXV_LookupValueDesc base_value;
582 GXV_LookupValueDesc value;
585 GXV_NAME_ENTER( "LookupTable format 4" );
587 unitSize = nUnits = 0;
588 gxv_BinSrchHeader_validate( p, limit, &unitSize, &nUnits, valid );
589 p += valid->subtable_length;
591 GXV_UNITSIZE_VALIDATE( "format4", unitSize, nUnits, 6 );
593 for ( unit = 0, gid = 0; unit < nUnits; unit++ )
595 GXV_LIMIT_CHECK( 2 + 2 );
596 lastGlyph = FT_NEXT_USHORT( p );
597 firstGlyph = FT_NEXT_USHORT( p );
599 gxv_glyphid_validate( firstGlyph, valid );
600 gxv_glyphid_validate( lastGlyph, valid );
602 if ( lastGlyph < gid )
604 GXV_TRACE(( "reverse ordered segment specification:"
605 " lastGlyph[%d]=%d < lastGlyph[%d]=%d\n",
606 unit, lastGlyph, unit - 1 , gid ));
607 if ( valid->root->level >= FT_VALIDATE_PARANOID )
608 FT_INVALID_GLYPH_ID;
611 if ( lastGlyph < firstGlyph )
613 GXV_TRACE(( "reverse ordered range specification at unit %d:",
614 " lastGlyph %d < firstGlyph %d ",
615 unit, lastGlyph, firstGlyph ));
616 if ( valid->root->level >= FT_VALIDATE_PARANOID )
617 FT_INVALID_GLYPH_ID;
619 if ( valid->root->level == FT_VALIDATE_TIGHT )
620 continue; /* ftxvalidator silently skips such an entry */
622 FT_TRACE4(( "continuing with exchanged values\n" ));
623 gid = firstGlyph;
624 firstGlyph = lastGlyph;
625 lastGlyph = gid;
628 GXV_LIMIT_CHECK( 2 );
629 base_value = GXV_LOOKUP_VALUE_LOAD( p, GXV_LOOKUPVALUE_UNSIGNED );
631 for ( gid = firstGlyph; gid <= lastGlyph; gid++ )
633 value = valid->lookupfmt4_trans( (FT_UShort)( gid - firstGlyph ),
634 &base_value,
635 limit,
636 valid );
638 valid->lookupval_func( gid, &value, valid );
642 gxv_LookupTable_fmt2_skip_endmarkers( p, unitSize, valid );
643 p += valid->subtable_length;
645 valid->subtable_length = p - table;
646 GXV_EXIT;
650 /* ================= Segment Table Format 6 Lookup Table =============== */
651 static void
652 gxv_LookupTable_fmt6_skip_endmarkers( FT_Bytes table,
653 FT_UShort unitSize,
654 GXV_Validator valid )
656 FT_Bytes p = table;
659 while ( p < valid->root->limit )
661 if ( p[0] != 0xFF || p[1] != 0xFF )
662 break;
663 p += unitSize;
666 valid->subtable_length = p - table;
670 static void
671 gxv_LookupTable_fmt6_validate( FT_Bytes table,
672 FT_Bytes limit,
673 GXV_Validator valid )
675 FT_Bytes p = table;
676 FT_UShort unit;
677 FT_UShort prev_glyph;
679 FT_UShort unitSize;
680 FT_UShort nUnits;
681 FT_UShort glyph;
682 GXV_LookupValueDesc value;
685 GXV_NAME_ENTER( "LookupTable format 6" );
687 unitSize = nUnits = 0;
688 gxv_BinSrchHeader_validate( p, limit, &unitSize, &nUnits, valid );
689 p += valid->subtable_length;
691 GXV_UNITSIZE_VALIDATE( "format6", unitSize, nUnits, 4 );
693 for ( unit = 0, prev_glyph = 0; unit < nUnits; unit++ )
695 GXV_LIMIT_CHECK( 2 + 2 );
696 glyph = FT_NEXT_USHORT( p );
697 value = GXV_LOOKUP_VALUE_LOAD( p, valid->lookupval_sign );
699 if ( gxv_glyphid_validate( glyph, valid ) )
700 GXV_TRACE(( " endmarker found within defined range"
701 " (entry %d < nUnits=%d)\n",
702 unit, nUnits ));
704 if ( prev_glyph > glyph )
706 GXV_TRACE(( "current gid 0x%04x < previous gid 0x%04x\n",
707 glyph, prev_glyph ));
708 if ( valid->root->level >= FT_VALIDATE_PARANOID )
709 FT_INVALID_GLYPH_ID;
711 prev_glyph = glyph;
713 valid->lookupval_func( glyph, &value, valid );
716 gxv_LookupTable_fmt6_skip_endmarkers( p, unitSize, valid );
717 p += valid->subtable_length;
719 valid->subtable_length = p - table;
720 GXV_EXIT;
724 /* ================= Trimmed Array Format 8 Lookup Table =============== */
725 static void
726 gxv_LookupTable_fmt8_validate( FT_Bytes table,
727 FT_Bytes limit,
728 GXV_Validator valid )
730 FT_Bytes p = table;
731 FT_UShort i;
733 GXV_LookupValueDesc value;
734 FT_UShort firstGlyph;
735 FT_UShort glyphCount;
738 GXV_NAME_ENTER( "LookupTable format 8" );
740 /* firstGlyph + glyphCount */
741 GXV_LIMIT_CHECK( 2 + 2 );
742 firstGlyph = FT_NEXT_USHORT( p );
743 glyphCount = FT_NEXT_USHORT( p );
745 gxv_glyphid_validate( firstGlyph, valid );
746 gxv_glyphid_validate( (FT_UShort)( firstGlyph + glyphCount ), valid );
748 /* valueArray */
749 for ( i = 0; i < glyphCount; i++ )
751 GXV_LIMIT_CHECK( 2 );
752 value = GXV_LOOKUP_VALUE_LOAD( p, valid->lookupval_sign );
753 valid->lookupval_func( (FT_UShort)( firstGlyph + i ), &value, valid );
756 valid->subtable_length = p - table;
757 GXV_EXIT;
761 FT_LOCAL_DEF( void )
762 gxv_LookupTable_validate( FT_Bytes table,
763 FT_Bytes limit,
764 GXV_Validator valid )
766 FT_Bytes p = table;
767 FT_UShort format;
769 GXV_Validate_Func fmt_funcs_table[] =
771 gxv_LookupTable_fmt0_validate, /* 0 */
772 NULL, /* 1 */
773 gxv_LookupTable_fmt2_validate, /* 2 */
774 NULL, /* 3 */
775 gxv_LookupTable_fmt4_validate, /* 4 */
776 NULL, /* 5 */
777 gxv_LookupTable_fmt6_validate, /* 6 */
778 NULL, /* 7 */
779 gxv_LookupTable_fmt8_validate, /* 8 */
782 GXV_Validate_Func func;
785 GXV_NAME_ENTER( "LookupTable" );
787 /* lookuptbl_head may be used in fmt4 transit function. */
788 valid->lookuptbl_head = table;
790 /* format */
791 GXV_LIMIT_CHECK( 2 );
792 format = FT_NEXT_USHORT( p );
793 GXV_TRACE(( " (format %d)\n", format ));
795 if ( format > 8 )
796 FT_INVALID_FORMAT;
798 func = fmt_funcs_table[format];
799 if ( func == NULL )
800 FT_INVALID_FORMAT;
802 func( p, limit, valid );
803 p += valid->subtable_length;
805 valid->subtable_length = p - table;
807 GXV_EXIT;
811 /*************************************************************************/
812 /*************************************************************************/
813 /***** *****/
814 /***** Glyph ID *****/
815 /***** *****/
816 /*************************************************************************/
817 /*************************************************************************/
819 FT_LOCAL_DEF( FT_Int )
820 gxv_glyphid_validate( FT_UShort gid,
821 GXV_Validator valid )
823 FT_Face face;
826 if ( gid == 0xFFFFU )
828 GXV_EXIT;
829 return 1;
832 face = valid->face;
833 if ( face->num_glyphs < gid )
835 GXV_TRACE(( " gxv_glyphid_check() gid overflow: num_glyphs %d < %d\n",
836 face->num_glyphs, gid ));
837 if ( valid->root->level >= FT_VALIDATE_PARANOID )
838 FT_INVALID_GLYPH_ID;
841 return 0;
845 /*************************************************************************/
846 /*************************************************************************/
847 /***** *****/
848 /***** CONTROL POINT *****/
849 /***** *****/
850 /*************************************************************************/
851 /*************************************************************************/
853 FT_LOCAL_DEF( void )
854 gxv_ctlPoint_validate( FT_UShort gid,
855 FT_Short ctl_point,
856 GXV_Validator valid )
858 FT_Face face;
859 FT_Error error;
861 FT_GlyphSlot glyph;
862 FT_Outline outline;
863 short n_points;
866 face = valid->face;
868 error = FT_Load_Glyph( face,
869 gid,
870 FT_LOAD_NO_BITMAP | FT_LOAD_IGNORE_TRANSFORM );
871 if ( error )
872 FT_INVALID_GLYPH_ID;
874 glyph = face->glyph;
875 outline = glyph->outline;
876 n_points = outline.n_points;
879 if ( !( ctl_point < n_points ) )
880 FT_INVALID_DATA;
884 /*************************************************************************/
885 /*************************************************************************/
886 /***** *****/
887 /***** SFNT NAME *****/
888 /***** *****/
889 /*************************************************************************/
890 /*************************************************************************/
892 FT_LOCAL_DEF( void )
893 gxv_sfntName_validate( FT_UShort name_index,
894 FT_UShort min_index,
895 FT_UShort max_index,
896 GXV_Validator valid )
898 FT_SfntName name;
899 FT_UInt i;
900 FT_UInt nnames;
903 GXV_NAME_ENTER( "sfntName" );
905 if ( name_index < min_index || max_index < name_index )
906 FT_INVALID_FORMAT;
908 nnames = FT_Get_Sfnt_Name_Count( valid->face );
909 for ( i = 0; i < nnames; i++ )
911 if ( FT_Get_Sfnt_Name( valid->face, i, &name ) != FT_Err_Ok )
912 continue ;
914 if ( name.name_id == name_index )
915 goto Out;
918 GXV_TRACE(( " nameIndex = %d (UNTITLED)\n", name_index ));
919 FT_INVALID_DATA;
920 goto Exit; /* make compiler happy */
922 Out:
923 FT_TRACE1(( " nameIndex = %d (", name_index ));
924 GXV_TRACE_HEXDUMP_SFNTNAME( name );
925 FT_TRACE1(( ")\n" ));
927 Exit:
928 GXV_EXIT;
932 /*************************************************************************/
933 /*************************************************************************/
934 /***** *****/
935 /***** STATE TABLE *****/
936 /***** *****/
937 /*************************************************************************/
938 /*************************************************************************/
940 /* -------------------------- Class Table --------------------------- */
943 * highestClass specifies how many classes are defined in this
944 * Class Subtable. Apple spec does not mention whether undefined
945 * holes in the class (e.g.: 0-3 are predefined, 4 is unused, 5 is used)
946 * are permitted. At present, holes in a defined class are not checked.
947 * -- suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>
950 static void
951 gxv_ClassTable_validate( FT_Bytes table,
952 FT_UShort* length_p,
953 FT_UShort stateSize,
954 FT_Byte* maxClassID_p,
955 GXV_Validator valid )
957 FT_Bytes p = table;
958 FT_Bytes limit = table + *length_p;
959 FT_UShort firstGlyph;
960 FT_UShort nGlyphs;
963 GXV_NAME_ENTER( "ClassTable" );
965 *maxClassID_p = 3; /* Classes 0, 2, and 3 are predefined */
967 GXV_LIMIT_CHECK( 2 + 2 );
968 firstGlyph = FT_NEXT_USHORT( p );
969 nGlyphs = FT_NEXT_USHORT( p );
971 GXV_TRACE(( " (firstGlyph = %d, nGlyphs = %d)\n", firstGlyph, nGlyphs ));
973 if ( !nGlyphs )
974 goto Out;
976 gxv_glyphid_validate( (FT_UShort)( firstGlyph + nGlyphs ), valid );
979 FT_Byte nGlyphInClass[256];
980 FT_Byte classID;
981 FT_UShort i;
984 ft_memset( nGlyphInClass, 0, 256 );
987 for ( i = 0; i < nGlyphs; i++ )
989 GXV_LIMIT_CHECK( 1 );
990 classID = FT_NEXT_BYTE( p );
991 switch ( classID )
993 /* following classes should not appear in class array */
994 case 0: /* end of text */
995 case 2: /* out of bounds */
996 case 3: /* end of line */
997 FT_INVALID_DATA;
998 break;
1000 case 1: /* out of bounds */
1001 default: /* user-defined: 4 - ( stateSize - 1 ) */
1002 if ( classID >= stateSize )
1003 FT_INVALID_DATA; /* assign glyph to undefined state */
1005 nGlyphInClass[classID]++;
1006 break;
1009 *length_p = (FT_UShort)( p - table );
1011 /* scan max ClassID in use */
1012 for ( i = 0; i < stateSize; i++ )
1013 if ( ( 3 < i ) && ( nGlyphInClass[i] > 0 ) )
1014 *maxClassID_p = (FT_Byte)i; /* XXX: Check Range? */
1017 Out:
1018 GXV_TRACE(( "Declared stateSize=0x%02x, Used maxClassID=0x%02x\n",
1019 stateSize, *maxClassID_p ));
1020 GXV_EXIT;
1024 /* --------------------------- State Array ----------------------------- */
1026 static void
1027 gxv_StateArray_validate( FT_Bytes table,
1028 FT_UShort* length_p,
1029 FT_Byte maxClassID,
1030 FT_UShort stateSize,
1031 FT_Byte* maxState_p,
1032 FT_Byte* maxEntry_p,
1033 GXV_Validator valid )
1035 FT_Bytes p = table;
1036 FT_Bytes limit = table + *length_p;
1037 FT_Byte clazz;
1038 FT_Byte entry;
1040 FT_UNUSED( stateSize ); /* for the non-debugging case */
1043 GXV_NAME_ENTER( "StateArray" );
1045 GXV_TRACE(( "parse %d bytes by stateSize=%d maxClassID=%d\n",
1046 (int)(*length_p), stateSize, (int)(maxClassID) ));
1049 * 2 states are predefined and must be described in StateArray:
1050 * state 0 (start of text), 1 (start of line)
1052 GXV_LIMIT_CHECK( ( 1 + maxClassID ) * 2 );
1054 *maxState_p = 0;
1055 *maxEntry_p = 0;
1057 /* read if enough to read another state */
1058 while ( p + ( 1 + maxClassID ) <= limit )
1060 (*maxState_p)++;
1061 for ( clazz = 0; clazz <= maxClassID; clazz++ )
1063 entry = FT_NEXT_BYTE( p );
1064 *maxEntry_p = (FT_Byte)FT_MAX( *maxEntry_p, entry );
1067 GXV_TRACE(( "parsed: maxState=%d, maxEntry=%d\n",
1068 *maxState_p, *maxEntry_p ));
1070 *length_p = (FT_UShort)( p - table );
1072 GXV_EXIT;
1076 /* --------------------------- Entry Table ----------------------------- */
1078 static void
1079 gxv_EntryTable_validate( FT_Bytes table,
1080 FT_UShort* length_p,
1081 FT_Byte maxEntry,
1082 FT_UShort stateArray,
1083 FT_UShort stateArray_length,
1084 FT_Byte maxClassID,
1085 FT_Bytes statetable_table,
1086 FT_Bytes statetable_limit,
1087 GXV_Validator valid )
1089 FT_Bytes p = table;
1090 FT_Bytes limit = table + *length_p;
1091 FT_Byte entry;
1092 FT_Byte state;
1093 FT_Int entrySize = 2 + 2 + GXV_GLYPHOFFSET_SIZE( statetable );
1095 GXV_XStateTable_GlyphOffsetDesc glyphOffset;
1098 GXV_NAME_ENTER( "EntryTable" );
1100 GXV_TRACE(( "maxEntry=%d entrySize=%d\n", maxEntry, entrySize ));
1102 if ( ( maxEntry + 1 ) * entrySize > *length_p )
1104 if ( valid->root->level >= FT_VALIDATE_PARANOID )
1105 FT_INVALID_TOO_SHORT;
1107 /* ftxvalidator and FontValidator both warn and continue */
1108 maxEntry = (FT_Byte)( *length_p / entrySize - 1 );
1109 GXV_TRACE(( "too large maxEntry, shrinking to %d fit EntryTable length\n",
1110 maxEntry ));
1113 for ( entry = 0; entry <= maxEntry; entry++ )
1115 FT_UShort newState;
1116 FT_UShort flags;
1119 GXV_LIMIT_CHECK( 2 + 2 );
1120 newState = FT_NEXT_USHORT( p );
1121 flags = FT_NEXT_USHORT( p );
1124 if ( newState < stateArray ||
1125 stateArray + stateArray_length < newState )
1127 GXV_TRACE(( " newState offset 0x%04x is out of stateArray\n",
1128 newState ));
1129 if ( valid->root->level >= FT_VALIDATE_PARANOID )
1130 FT_INVALID_OFFSET;
1131 continue;
1134 if ( 0 != ( ( newState - stateArray ) % ( 1 + maxClassID ) ) )
1136 GXV_TRACE(( " newState offset 0x%04x is not aligned to %d-classes\n",
1137 newState, 1 + maxClassID ));
1138 if ( valid->root->level >= FT_VALIDATE_PARANOID )
1139 FT_INVALID_OFFSET;
1140 continue;
1143 state = (FT_Byte)( ( newState - stateArray ) / ( 1 + maxClassID ) );
1145 switch ( GXV_GLYPHOFFSET_FMT( statetable ) )
1147 case GXV_GLYPHOFFSET_NONE:
1148 glyphOffset.uc = 0; /* make compiler happy */
1149 break;
1151 case GXV_GLYPHOFFSET_UCHAR:
1152 glyphOffset.uc = FT_NEXT_BYTE( p );
1153 break;
1155 case GXV_GLYPHOFFSET_CHAR:
1156 glyphOffset.c = FT_NEXT_CHAR( p );
1157 break;
1159 case GXV_GLYPHOFFSET_USHORT:
1160 glyphOffset.u = FT_NEXT_USHORT( p );
1161 break;
1163 case GXV_GLYPHOFFSET_SHORT:
1164 glyphOffset.s = FT_NEXT_SHORT( p );
1165 break;
1167 case GXV_GLYPHOFFSET_ULONG:
1168 glyphOffset.ul = FT_NEXT_ULONG( p );
1169 break;
1171 case GXV_GLYPHOFFSET_LONG:
1172 glyphOffset.l = FT_NEXT_LONG( p );
1173 break;
1175 default:
1176 if ( valid->root->level >= FT_VALIDATE_PARANOID )
1177 FT_INVALID_FORMAT;
1178 goto Exit;
1181 if ( NULL != valid->statetable.entry_validate_func )
1182 valid->statetable.entry_validate_func( state,
1183 flags,
1184 &glyphOffset,
1185 statetable_table,
1186 statetable_limit,
1187 valid );
1190 Exit:
1191 *length_p = (FT_UShort)( p - table );
1193 GXV_EXIT;
1197 /* =========================== State Table ============================= */
1199 FT_LOCAL_DEF( void )
1200 gxv_StateTable_subtable_setup( FT_UShort table_size,
1201 FT_UShort classTable,
1202 FT_UShort stateArray,
1203 FT_UShort entryTable,
1204 FT_UShort* classTable_length_p,
1205 FT_UShort* stateArray_length_p,
1206 FT_UShort* entryTable_length_p,
1207 GXV_Validator valid )
1209 FT_UShort o[3];
1210 FT_UShort* l[3];
1211 FT_UShort buff[4];
1214 o[0] = classTable;
1215 o[1] = stateArray;
1216 o[2] = entryTable;
1217 l[0] = classTable_length_p;
1218 l[1] = stateArray_length_p;
1219 l[2] = entryTable_length_p;
1221 gxv_set_length_by_ushort_offset( o, l, buff, 3, table_size, valid );
1225 FT_LOCAL_DEF( void )
1226 gxv_StateTable_validate( FT_Bytes table,
1227 FT_Bytes limit,
1228 GXV_Validator valid )
1230 FT_UShort stateSize;
1231 FT_UShort classTable; /* offset to Class(Sub)Table */
1232 FT_UShort stateArray; /* offset to StateArray */
1233 FT_UShort entryTable; /* offset to EntryTable */
1235 FT_UShort classTable_length;
1236 FT_UShort stateArray_length;
1237 FT_UShort entryTable_length;
1238 FT_Byte maxClassID;
1239 FT_Byte maxState;
1240 FT_Byte maxEntry;
1242 GXV_StateTable_Subtable_Setup_Func setup_func;
1244 FT_Bytes p = table;
1247 GXV_NAME_ENTER( "StateTable" );
1249 GXV_TRACE(( "StateTable header\n" ));
1251 GXV_LIMIT_CHECK( 2 + 2 + 2 + 2 );
1252 stateSize = FT_NEXT_USHORT( p );
1253 classTable = FT_NEXT_USHORT( p );
1254 stateArray = FT_NEXT_USHORT( p );
1255 entryTable = FT_NEXT_USHORT( p );
1257 GXV_TRACE(( "stateSize=0x%04x\n", stateSize ));
1258 GXV_TRACE(( "offset to classTable=0x%04x\n", classTable ));
1259 GXV_TRACE(( "offset to stateArray=0x%04x\n", stateArray ));
1260 GXV_TRACE(( "offset to entryTable=0x%04x\n", entryTable ));
1262 if ( stateSize > 0xFF )
1263 FT_INVALID_DATA;
1265 if ( valid->statetable.optdata_load_func != NULL )
1266 valid->statetable.optdata_load_func( p, limit, valid );
1268 if ( valid->statetable.subtable_setup_func != NULL)
1269 setup_func = valid->statetable.subtable_setup_func;
1270 else
1271 setup_func = gxv_StateTable_subtable_setup;
1273 setup_func( (FT_UShort)( limit - table ),
1274 classTable,
1275 stateArray,
1276 entryTable,
1277 &classTable_length,
1278 &stateArray_length,
1279 &entryTable_length,
1280 valid );
1282 GXV_TRACE(( "StateTable Subtables\n" ));
1284 if ( classTable != 0 )
1285 gxv_ClassTable_validate( table + classTable,
1286 &classTable_length,
1287 stateSize,
1288 &maxClassID,
1289 valid );
1290 else
1291 maxClassID = (FT_Byte)( stateSize - 1 );
1293 if ( stateArray != 0 )
1294 gxv_StateArray_validate( table + stateArray,
1295 &stateArray_length,
1296 maxClassID,
1297 stateSize,
1298 &maxState,
1299 &maxEntry,
1300 valid );
1301 else
1303 maxState = 1; /* 0:start of text, 1:start of line are predefined */
1304 maxEntry = 0;
1307 if ( maxEntry > 0 && entryTable == 0 )
1308 FT_INVALID_OFFSET;
1310 if ( entryTable != 0 )
1311 gxv_EntryTable_validate( table + entryTable,
1312 &entryTable_length,
1313 maxEntry,
1314 stateArray,
1315 stateArray_length,
1316 maxClassID,
1317 table,
1318 limit,
1319 valid );
1321 GXV_EXIT;
1325 /* ================= eXtended State Table (for morx) =================== */
1327 FT_LOCAL_DEF( void )
1328 gxv_XStateTable_subtable_setup( FT_ULong table_size,
1329 FT_ULong classTable,
1330 FT_ULong stateArray,
1331 FT_ULong entryTable,
1332 FT_ULong* classTable_length_p,
1333 FT_ULong* stateArray_length_p,
1334 FT_ULong* entryTable_length_p,
1335 GXV_Validator valid )
1337 FT_ULong o[3];
1338 FT_ULong* l[3];
1339 FT_ULong buff[4];
1342 o[0] = classTable;
1343 o[1] = stateArray;
1344 o[2] = entryTable;
1345 l[0] = classTable_length_p;
1346 l[1] = stateArray_length_p;
1347 l[2] = entryTable_length_p;
1349 gxv_set_length_by_ulong_offset( o, l, buff, 4, table_size, valid );
1353 static void
1354 gxv_XClassTable_lookupval_validate( FT_UShort glyph,
1355 GXV_LookupValueCPtr value_p,
1356 GXV_Validator valid )
1358 FT_UNUSED( glyph );
1360 if ( value_p->u >= valid->xstatetable.nClasses )
1361 FT_INVALID_DATA;
1362 if ( value_p->u > valid->xstatetable.maxClassID )
1363 valid->xstatetable.maxClassID = value_p->u;
1368 +===============+ --------+
1369 | lookup header | |
1370 +===============+ |
1371 | BinSrchHeader | |
1372 +===============+ |
1373 | lastGlyph[0] | |
1374 +---------------+ |
1375 | firstGlyph[0] | | head of lookup table
1376 +---------------+ | +
1377 | offset[0] | -> | offset [byte]
1378 +===============+ | +
1379 | lastGlyph[1] | | (glyphID - firstGlyph) * 2 [byte]
1380 +---------------+ |
1381 | firstGlyph[1] | |
1382 +---------------+ |
1383 | offset[1] | |
1384 +===============+ |
1386 .... |
1388 16bit value array |
1389 +===============+ |
1390 | value | <-------+
1391 ....
1393 static GXV_LookupValueDesc
1394 gxv_XClassTable_lookupfmt4_transit( FT_UShort relative_gindex,
1395 GXV_LookupValueCPtr base_value_p,
1396 FT_Bytes lookuptbl_limit,
1397 GXV_Validator valid )
1399 FT_Bytes p;
1400 FT_Bytes limit;
1401 FT_UShort offset;
1402 GXV_LookupValueDesc value;
1404 /* XXX: check range? */
1405 offset = (FT_UShort)( base_value_p->u +
1406 relative_gindex * sizeof ( FT_UShort ) );
1408 p = valid->lookuptbl_head + offset;
1409 limit = lookuptbl_limit;
1411 GXV_LIMIT_CHECK ( 2 );
1412 value.u = FT_NEXT_USHORT( p );
1414 return value;
1418 static void
1419 gxv_XStateArray_validate( FT_Bytes table,
1420 FT_ULong* length_p,
1421 FT_UShort maxClassID,
1422 FT_ULong stateSize,
1423 FT_UShort* maxState_p,
1424 FT_UShort* maxEntry_p,
1425 GXV_Validator valid )
1427 FT_Bytes p = table;
1428 FT_Bytes limit = table + *length_p;
1429 FT_UShort clazz;
1430 FT_UShort entry;
1432 FT_UNUSED( stateSize ); /* for the non-debugging case */
1435 GXV_NAME_ENTER( "XStateArray" );
1437 GXV_TRACE(( "parse % 3d bytes by stateSize=% 3d maxClassID=% 3d\n",
1438 (int)(*length_p), stateSize, (int)(maxClassID) ));
1441 * 2 states are predefined and must be described:
1442 * state 0 (start of text), 1 (start of line)
1444 GXV_LIMIT_CHECK( ( 1 + maxClassID ) * 2 * 2 );
1446 *maxState_p = 0;
1447 *maxEntry_p = 0;
1449 /* read if enough to read another state */
1450 while ( p + ( ( 1 + maxClassID ) * 2 ) <= limit )
1452 (*maxState_p)++;
1453 for ( clazz = 0; clazz <= maxClassID; clazz++ )
1455 entry = FT_NEXT_USHORT( p );
1456 *maxEntry_p = (FT_UShort)FT_MAX( *maxEntry_p, entry );
1459 GXV_TRACE(( "parsed: maxState=%d, maxEntry=%d\n",
1460 *maxState_p, *maxEntry_p ));
1462 *length_p = p - table;
1464 GXV_EXIT;
1468 static void
1469 gxv_XEntryTable_validate( FT_Bytes table,
1470 FT_ULong* length_p,
1471 FT_UShort maxEntry,
1472 FT_ULong stateArray_length,
1473 FT_UShort maxClassID,
1474 FT_Bytes xstatetable_table,
1475 FT_Bytes xstatetable_limit,
1476 GXV_Validator valid )
1478 FT_Bytes p = table;
1479 FT_Bytes limit = table + *length_p;
1480 FT_UShort entry;
1481 FT_UShort state;
1482 FT_Int entrySize = 2 + 2 + GXV_GLYPHOFFSET_SIZE( xstatetable );
1485 GXV_NAME_ENTER( "XEntryTable" );
1486 GXV_TRACE(( "maxEntry=%d entrySize=%d\n", maxEntry, entrySize ));
1488 if ( ( p + ( maxEntry + 1 ) * entrySize ) > limit )
1489 FT_INVALID_TOO_SHORT;
1491 for (entry = 0; entry <= maxEntry ; entry++ )
1493 FT_UShort newState_idx;
1494 FT_UShort flags;
1495 GXV_XStateTable_GlyphOffsetDesc glyphOffset;
1498 GXV_LIMIT_CHECK( 2 + 2 );
1499 newState_idx = FT_NEXT_USHORT( p );
1500 flags = FT_NEXT_USHORT( p );
1502 if ( stateArray_length < (FT_ULong)( newState_idx * 2 ) )
1504 GXV_TRACE(( " newState index 0x%04x points out of stateArray\n",
1505 newState_idx ));
1506 if ( valid->root->level >= FT_VALIDATE_PARANOID )
1507 FT_INVALID_OFFSET;
1510 state = (FT_UShort)( newState_idx / ( 1 + maxClassID ) );
1511 if ( 0 != ( newState_idx % ( 1 + maxClassID ) ) )
1513 FT_TRACE4(( "-> new state = %d (supposed)\n"
1514 "but newState index 0x%04x is not aligned to %d-classes\n",
1515 state, newState_idx, 1 + maxClassID ));
1516 if ( valid->root->level >= FT_VALIDATE_PARANOID )
1517 FT_INVALID_OFFSET;
1520 switch ( GXV_GLYPHOFFSET_FMT( xstatetable ) )
1522 case GXV_GLYPHOFFSET_NONE:
1523 glyphOffset.uc = 0; /* make compiler happy */
1524 break;
1526 case GXV_GLYPHOFFSET_UCHAR:
1527 glyphOffset.uc = FT_NEXT_BYTE( p );
1528 break;
1530 case GXV_GLYPHOFFSET_CHAR:
1531 glyphOffset.c = FT_NEXT_CHAR( p );
1532 break;
1534 case GXV_GLYPHOFFSET_USHORT:
1535 glyphOffset.u = FT_NEXT_USHORT( p );
1536 break;
1538 case GXV_GLYPHOFFSET_SHORT:
1539 glyphOffset.s = FT_NEXT_SHORT( p );
1540 break;
1542 case GXV_GLYPHOFFSET_ULONG:
1543 glyphOffset.ul = FT_NEXT_ULONG( p );
1544 break;
1546 case GXV_GLYPHOFFSET_LONG:
1547 glyphOffset.l = FT_NEXT_LONG( p );
1548 break;
1550 default:
1551 if ( valid->root->level >= FT_VALIDATE_PARANOID )
1552 FT_INVALID_FORMAT;
1553 goto Exit;
1556 if ( NULL != valid->xstatetable.entry_validate_func )
1557 valid->xstatetable.entry_validate_func( state,
1558 flags,
1559 &glyphOffset,
1560 xstatetable_table,
1561 xstatetable_limit,
1562 valid );
1565 Exit:
1566 *length_p = p - table;
1568 GXV_EXIT;
1572 FT_LOCAL_DEF( void )
1573 gxv_XStateTable_validate( FT_Bytes table,
1574 FT_Bytes limit,
1575 GXV_Validator valid )
1577 /* StateHeader members */
1578 FT_ULong classTable; /* offset to Class(Sub)Table */
1579 FT_ULong stateArray; /* offset to StateArray */
1580 FT_ULong entryTable; /* offset to EntryTable */
1582 FT_ULong classTable_length;
1583 FT_ULong stateArray_length;
1584 FT_ULong entryTable_length;
1585 FT_UShort maxState;
1586 FT_UShort maxEntry;
1588 GXV_XStateTable_Subtable_Setup_Func setup_func;
1590 FT_Bytes p = table;
1593 GXV_NAME_ENTER( "XStateTable" );
1595 GXV_TRACE(( "XStateTable header\n" ));
1597 GXV_LIMIT_CHECK( 4 + 4 + 4 + 4 );
1598 valid->xstatetable.nClasses = FT_NEXT_ULONG( p );
1599 classTable = FT_NEXT_ULONG( p );
1600 stateArray = FT_NEXT_ULONG( p );
1601 entryTable = FT_NEXT_ULONG( p );
1603 GXV_TRACE(( "nClasses =0x%08x\n", valid->xstatetable.nClasses ));
1604 GXV_TRACE(( "offset to classTable=0x%08x\n", classTable ));
1605 GXV_TRACE(( "offset to stateArray=0x%08x\n", stateArray ));
1606 GXV_TRACE(( "offset to entryTable=0x%08x\n", entryTable ));
1608 if ( valid->xstatetable.nClasses > 0xFFFFU )
1609 FT_INVALID_DATA;
1611 GXV_TRACE(( "StateTable Subtables\n" ));
1613 if ( valid->xstatetable.optdata_load_func != NULL )
1614 valid->xstatetable.optdata_load_func( p, limit, valid );
1616 if ( valid->xstatetable.subtable_setup_func != NULL )
1617 setup_func = valid->xstatetable.subtable_setup_func;
1618 else
1619 setup_func = gxv_XStateTable_subtable_setup;
1621 setup_func( limit - table,
1622 classTable,
1623 stateArray,
1624 entryTable,
1625 &classTable_length,
1626 &stateArray_length,
1627 &entryTable_length,
1628 valid );
1630 if ( classTable != 0 )
1632 valid->xstatetable.maxClassID = 0;
1633 valid->lookupval_sign = GXV_LOOKUPVALUE_UNSIGNED;
1634 valid->lookupval_func = gxv_XClassTable_lookupval_validate;
1635 valid->lookupfmt4_trans = gxv_XClassTable_lookupfmt4_transit;
1636 gxv_LookupTable_validate( table + classTable,
1637 table + classTable + classTable_length,
1638 valid );
1639 if ( valid->subtable_length < classTable_length )
1640 classTable_length = valid->subtable_length;
1642 else
1644 /* XXX: check range? */
1645 valid->xstatetable.maxClassID =
1646 (FT_UShort)( valid->xstatetable.nClasses - 1 );
1649 if ( stateArray != 0 )
1650 gxv_XStateArray_validate( table + stateArray,
1651 &stateArray_length,
1652 valid->xstatetable.maxClassID,
1653 valid->xstatetable.nClasses,
1654 &maxState,
1655 &maxEntry,
1656 valid );
1657 else
1659 maxState = 1; /* 0:start of text, 1:start of line are predefined */
1660 maxEntry = 0;
1663 if ( maxEntry > 0 && entryTable == 0 )
1664 FT_INVALID_OFFSET;
1666 if ( entryTable != 0 )
1667 gxv_XEntryTable_validate( table + entryTable,
1668 &entryTable_length,
1669 maxEntry,
1670 stateArray_length,
1671 valid->xstatetable.maxClassID,
1672 table,
1673 limit,
1674 valid );
1676 GXV_EXIT;
1680 /*************************************************************************/
1681 /*************************************************************************/
1682 /***** *****/
1683 /***** Table overlapping *****/
1684 /***** *****/
1685 /*************************************************************************/
1686 /*************************************************************************/
1688 static int
1689 gxv_compare_ranges( FT_Bytes table1_start,
1690 FT_ULong table1_length,
1691 FT_Bytes table2_start,
1692 FT_ULong table2_length )
1694 if ( table1_start == table2_start )
1696 if ( ( table1_length == 0 || table2_length == 0 ) )
1697 goto Out;
1699 else if ( table1_start < table2_start )
1701 if ( ( table1_start + table1_length ) <= table2_start )
1702 goto Out;
1704 else if ( table1_start > table2_start )
1706 if ( ( table1_start >= table2_start + table2_length ) )
1707 goto Out;
1709 return 1;
1711 Out:
1712 return 0;
1716 FT_LOCAL_DEF( void )
1717 gxv_odtect_add_range( FT_Bytes start,
1718 FT_ULong length,
1719 const FT_String* name,
1720 GXV_odtect_Range odtect )
1722 odtect->range[ odtect->nRanges ].start = start;
1723 odtect->range[ odtect->nRanges ].length = length;
1724 odtect->range[ odtect->nRanges ].name = (FT_String*)name;
1725 odtect->nRanges++;
1729 FT_LOCAL_DEF( void )
1730 gxv_odtect_validate( GXV_odtect_Range odtect,
1731 GXV_Validator valid )
1733 FT_UInt i, j;
1736 GXV_NAME_ENTER( "check overlap among multi ranges" );
1738 for ( i = 0; i < odtect->nRanges; i++ )
1739 for ( j = 0; j < i; j++ )
1740 if ( 0 != gxv_compare_ranges( odtect->range[i].start,
1741 odtect->range[i].length,
1742 odtect->range[j].start,
1743 odtect->range[j].length ) )
1745 if ( odtect->range[i].name || odtect->range[j].name )
1746 GXV_TRACE(( "found overlap between range %d and range %d\n",
1747 i, j ));
1748 else
1749 GXV_TRACE(( "found overlap between `%s' and `%s\'\n",
1750 odtect->range[i].name,
1751 odtect->range[j].name ));
1752 FT_INVALID_OFFSET;
1755 GXV_EXIT;
1759 /* END */