[CMAKE]
[reactos.git] / lib / 3rdparty / freetype / src / gxvalid / gxvcommn.c
1 /***************************************************************************/
2 /* */
3 /* gxvcommn.c */
4 /* */
5 /* TrueTypeGX/AAT common tables validation (body). */
6 /* */
7 /* Copyright 2004, 2005, 2009, 2010 */
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 /***************************************************************************/
18
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 /***************************************************************************/
26
27
28 #include "gxvcommn.h"
29
30
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
39
40
41 /*************************************************************************/
42 /*************************************************************************/
43 /***** *****/
44 /***** 16bit offset sorter *****/
45 /***** *****/
46 /*************************************************************************/
47 /*************************************************************************/
48
49 static int
50 gxv_compare_ushort_offset( FT_UShort* a,
51 FT_UShort* b )
52 {
53 if ( *a < *b )
54 return -1;
55 else if ( *a > *b )
56 return 1;
57 else
58 return 0;
59 }
60
61
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 )
69 {
70 FT_UInt i;
71
72
73 for ( i = 0; i < nmemb; i++ )
74 *(length[i]) = 0;
75
76 for ( i = 0; i < nmemb; i++ )
77 buff[i] = offset[i];
78 buff[nmemb] = limit;
79
80 ft_qsort( buff, ( nmemb + 1 ), sizeof ( FT_UShort ),
81 ( int(*)(const void*, const void*) )gxv_compare_ushort_offset );
82
83 if ( buff[nmemb] > limit )
84 FT_INVALID_OFFSET;
85
86 for ( i = 0; i < nmemb; i++ )
87 {
88 FT_UInt j;
89
90
91 for ( j = 0; j < nmemb; j++ )
92 if ( buff[j] == offset[i] )
93 break;
94
95 if ( j == nmemb )
96 FT_INVALID_OFFSET;
97
98 *(length[i]) = (FT_UShort)( buff[j + 1] - buff[j] );
99
100 if ( 0 != offset[i] && 0 == *(length[i]) )
101 FT_INVALID_OFFSET;
102 }
103 }
104
105
106 /*************************************************************************/
107 /*************************************************************************/
108 /***** *****/
109 /***** 32bit offset sorter *****/
110 /***** *****/
111 /*************************************************************************/
112 /*************************************************************************/
113
114 static int
115 gxv_compare_ulong_offset( FT_ULong* a,
116 FT_ULong* b )
117 {
118 if ( *a < *b )
119 return -1;
120 else if ( *a > *b )
121 return 1;
122 else
123 return 0;
124 }
125
126
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)
134 {
135 FT_UInt i;
136
137
138 for ( i = 0; i < nmemb; i++ )
139 *(length[i]) = 0;
140
141 for ( i = 0; i < nmemb; i++ )
142 buff[i] = offset[i];
143 buff[nmemb] = limit;
144
145 ft_qsort( buff, ( nmemb + 1 ), sizeof ( FT_ULong ),
146 ( int(*)(const void*, const void*) )gxv_compare_ulong_offset );
147
148 if ( buff[nmemb] > limit )
149 FT_INVALID_OFFSET;
150
151 for ( i = 0; i < nmemb; i++ )
152 {
153 FT_UInt j;
154
155
156 for ( j = 0; j < nmemb; j++ )
157 if ( buff[j] == offset[i] )
158 break;
159
160 if ( j == nmemb )
161 FT_INVALID_OFFSET;
162
163 *(length[i]) = buff[j + 1] - buff[j];
164
165 if ( 0 != offset[i] && 0 == *(length[i]) )
166 FT_INVALID_OFFSET;
167 }
168 }
169
170
171 /*************************************************************************/
172 /*************************************************************************/
173 /***** *****/
174 /***** scan value array and get min & max *****/
175 /***** *****/
176 /*************************************************************************/
177 /*************************************************************************/
178
179
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 )
186 {
187 FT_Bytes p = table;
188
189
190 *min = 0xFF;
191 *max = 0x00;
192
193 while ( p < limit )
194 {
195 FT_Byte val;
196
197
198 GXV_LIMIT_CHECK( 1 );
199 val = FT_NEXT_BYTE( p );
200
201 *min = (FT_Byte)FT_MIN( *min, val );
202 *max = (FT_Byte)FT_MAX( *max, val );
203 }
204
205 valid->subtable_length = p - table;
206 }
207
208
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 )
215 {
216 FT_Bytes p = table;
217
218
219 *min = 0xFFFFU;
220 *max = 0x0000;
221
222 while ( p < limit )
223 {
224 FT_UShort val;
225
226
227 GXV_LIMIT_CHECK( 2 );
228 val = FT_NEXT_USHORT( p );
229
230 *min = (FT_Byte)FT_MIN( *min, val );
231 *max = (FT_Byte)FT_MAX( *max, val );
232 }
233
234 valid->subtable_length = p - table;
235 }
236
237
238 /*************************************************************************/
239 /*************************************************************************/
240 /***** *****/
241 /***** BINSEARCHHEADER *****/
242 /***** *****/
243 /*************************************************************************/
244 /*************************************************************************/
245
246 typedef struct GXV_BinSrchHeader_
247 {
248 FT_UShort unitSize;
249 FT_UShort nUnits;
250 FT_UShort searchRange;
251 FT_UShort entrySelector;
252 FT_UShort rangeShift;
253
254 } GXV_BinSrchHeader;
255
256
257 static void
258 gxv_BinSrchHeader_check_consistency( GXV_BinSrchHeader* binSrchHeader,
259 GXV_Validator valid )
260 {
261 FT_UShort searchRange;
262 FT_UShort entrySelector;
263 FT_UShort rangeShift;
264
265
266 if ( binSrchHeader->unitSize == 0 )
267 FT_INVALID_DATA;
268
269 if ( binSrchHeader->nUnits == 0 )
270 {
271 if ( binSrchHeader->searchRange == 0 &&
272 binSrchHeader->entrySelector == 0 &&
273 binSrchHeader->rangeShift == 0 )
274 return;
275 else
276 FT_INVALID_DATA;
277 }
278
279 for ( searchRange = 1, entrySelector = 1;
280 ( searchRange * 2 ) <= binSrchHeader->nUnits &&
281 searchRange < 0x8000U;
282 searchRange *= 2, entrySelector++ )
283 ;
284
285 entrySelector--;
286 searchRange = (FT_UShort)( searchRange * binSrchHeader->unitSize );
287 rangeShift = (FT_UShort)( binSrchHeader->nUnits * binSrchHeader->unitSize
288 - searchRange );
289
290 if ( searchRange != binSrchHeader->searchRange ||
291 entrySelector != binSrchHeader->entrySelector ||
292 rangeShift != binSrchHeader->rangeShift )
293 {
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 ));
306
307 if ( valid->root->level >= FT_VALIDATE_PARANOID )
308 FT_INVALID_DATA;
309 }
310 }
311
312
313 /*
314 * parser & validator of BinSrchHeader
315 * which is used in LookupTable format 2, 4, 6.
316 *
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.
321 *
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>
326 */
327
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 )
334 {
335 FT_Bytes p = table;
336 GXV_BinSrchHeader binSrchHeader;
337
338
339 GXV_NAME_ENTER( "BinSrchHeader validate" );
340
341 if ( *unitSize_p == 0 )
342 {
343 GXV_LIMIT_CHECK( 2 );
344 binSrchHeader.unitSize = FT_NEXT_USHORT( p );
345 }
346 else
347 binSrchHeader.unitSize = *unitSize_p;
348
349 if ( *nUnits_p == 0 )
350 {
351 GXV_LIMIT_CHECK( 2 );
352 binSrchHeader.nUnits = FT_NEXT_USHORT( p );
353 }
354 else
355 binSrchHeader.nUnits = *nUnits_p;
356
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 ));
362
363 gxv_BinSrchHeader_check_consistency( &binSrchHeader, valid );
364
365 if ( *unitSize_p == 0 )
366 *unitSize_p = binSrchHeader.unitSize;
367
368 if ( *nUnits_p == 0 )
369 *nUnits_p = binSrchHeader.nUnits;
370
371 valid->subtable_length = p - table;
372 GXV_EXIT;
373 }
374
375
376 /*************************************************************************/
377 /*************************************************************************/
378 /***** *****/
379 /***** LOOKUP TABLE *****/
380 /***** *****/
381 /*************************************************************************/
382 /*************************************************************************/
383
384 #define GXV_LOOKUP_VALUE_LOAD( P, SIGNSPEC ) \
385 ( P += 2, gxv_lookup_value_load( P - 2, SIGNSPEC ) )
386
387 static GXV_LookupValueDesc
388 gxv_lookup_value_load( FT_Bytes p,
389 int signspec )
390 {
391 GXV_LookupValueDesc v;
392
393
394 if ( signspec == GXV_LOOKUPVALUE_UNSIGNED )
395 v.u = FT_NEXT_USHORT( p );
396 else
397 v.s = FT_NEXT_SHORT( p );
398
399 return v;
400 }
401
402
403 #define GXV_UNITSIZE_VALIDATE( FORMAT, UNITSIZE, NUNITS, CORRECTSIZE ) \
404 FT_BEGIN_STMNT \
405 if ( UNITSIZE != CORRECTSIZE ) \
406 { \
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 ) \
412 { \
413 FT_ERROR(( " cannot validate anymore\n" )); \
414 FT_INVALID_FORMAT; \
415 } \
416 else \
417 FT_ERROR(( " forcibly continues\n" )); \
418 } \
419 FT_END_STMNT
420
421
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 )
427 {
428 FT_Bytes p = table;
429 FT_UShort i;
430
431 GXV_LookupValueDesc value;
432
433
434 GXV_NAME_ENTER( "LookupTable format 0" );
435
436 GXV_LIMIT_CHECK( 2 * valid->face->num_glyphs );
437
438 for ( i = 0; i < valid->face->num_glyphs; i++ )
439 {
440 GXV_LIMIT_CHECK( 2 );
441 if ( p + 2 >= limit ) /* some fonts have too-short fmt0 array */
442 {
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;
448 }
449
450 value = GXV_LOOKUP_VALUE_LOAD( p, valid->lookupval_sign );
451 valid->lookupval_func( i, &value, valid );
452 }
453
454 valid->subtable_length = p - table;
455 GXV_EXIT;
456 }
457
458
459 /* ================= Segment Single Format 2 Loolup Table ============== */
460 /*
461 * Apple spec says:
462 *
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.
468 *
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.
472 *
473 * -- suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>
474 */
475 static void
476 gxv_LookupTable_fmt2_skip_endmarkers( FT_Bytes table,
477 FT_UShort unitSize,
478 GXV_Validator valid )
479 {
480 FT_Bytes p = table;
481
482
483 while ( ( p + 4 ) < valid->root->limit )
484 {
485 if ( p[0] != 0xFF || p[1] != 0xFF || /* lastGlyph */
486 p[2] != 0xFF || p[3] != 0xFF ) /* firstGlyph */
487 break;
488 p += unitSize;
489 }
490
491 valid->subtable_length = p - table;
492 }
493
494
495 static void
496 gxv_LookupTable_fmt2_validate( FT_Bytes table,
497 FT_Bytes limit,
498 GXV_Validator valid )
499 {
500 FT_Bytes p = table;
501 FT_UShort gid;
502
503 FT_UShort unitSize;
504 FT_UShort nUnits;
505 FT_UShort unit;
506 FT_UShort lastGlyph;
507 FT_UShort firstGlyph;
508 GXV_LookupValueDesc value;
509
510
511 GXV_NAME_ENTER( "LookupTable format 2" );
512
513 unitSize = nUnits = 0;
514 gxv_BinSrchHeader_validate( p, limit, &unitSize, &nUnits, valid );
515 p += valid->subtable_length;
516
517 GXV_UNITSIZE_VALIDATE( "format2", unitSize, nUnits, 6 );
518
519 for ( unit = 0, gid = 0; unit < nUnits; unit++ )
520 {
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 );
525
526 gxv_glyphid_validate( firstGlyph, valid );
527 gxv_glyphid_validate( lastGlyph, valid );
528
529 if ( lastGlyph < gid )
530 {
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;
536 }
537
538 if ( lastGlyph < firstGlyph )
539 {
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;
545
546 if ( valid->root->level == FT_VALIDATE_TIGHT )
547 continue; /* ftxvalidator silently skips such an entry */
548
549 FT_TRACE4(( "continuing with exchanged values\n" ));
550 gid = firstGlyph;
551 firstGlyph = lastGlyph;
552 lastGlyph = gid;
553 }
554
555 for ( gid = firstGlyph; gid <= lastGlyph; gid++ )
556 valid->lookupval_func( gid, &value, valid );
557 }
558
559 gxv_LookupTable_fmt2_skip_endmarkers( p, unitSize, valid );
560 p += valid->subtable_length;
561
562 valid->subtable_length = p - table;
563 GXV_EXIT;
564 }
565
566
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 )
572 {
573 FT_Bytes p = table;
574 FT_UShort unit;
575 FT_UShort gid;
576
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;
583
584
585 GXV_NAME_ENTER( "LookupTable format 4" );
586
587 unitSize = nUnits = 0;
588 gxv_BinSrchHeader_validate( p, limit, &unitSize, &nUnits, valid );
589 p += valid->subtable_length;
590
591 GXV_UNITSIZE_VALIDATE( "format4", unitSize, nUnits, 6 );
592
593 for ( unit = 0, gid = 0; unit < nUnits; unit++ )
594 {
595 GXV_LIMIT_CHECK( 2 + 2 );
596 lastGlyph = FT_NEXT_USHORT( p );
597 firstGlyph = FT_NEXT_USHORT( p );
598
599 gxv_glyphid_validate( firstGlyph, valid );
600 gxv_glyphid_validate( lastGlyph, valid );
601
602 if ( lastGlyph < gid )
603 {
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;
609 }
610
611 if ( lastGlyph < firstGlyph )
612 {
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;
618
619 if ( valid->root->level == FT_VALIDATE_TIGHT )
620 continue; /* ftxvalidator silently skips such an entry */
621
622 FT_TRACE4(( "continuing with exchanged values\n" ));
623 gid = firstGlyph;
624 firstGlyph = lastGlyph;
625 lastGlyph = gid;
626 }
627
628 GXV_LIMIT_CHECK( 2 );
629 base_value = GXV_LOOKUP_VALUE_LOAD( p, GXV_LOOKUPVALUE_UNSIGNED );
630
631 for ( gid = firstGlyph; gid <= lastGlyph; gid++ )
632 {
633 value = valid->lookupfmt4_trans( (FT_UShort)( gid - firstGlyph ),
634 &base_value,
635 limit,
636 valid );
637
638 valid->lookupval_func( gid, &value, valid );
639 }
640 }
641
642 gxv_LookupTable_fmt2_skip_endmarkers( p, unitSize, valid );
643 p += valid->subtable_length;
644
645 valid->subtable_length = p - table;
646 GXV_EXIT;
647 }
648
649
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 )
655 {
656 FT_Bytes p = table;
657
658
659 while ( p < valid->root->limit )
660 {
661 if ( p[0] != 0xFF || p[1] != 0xFF )
662 break;
663 p += unitSize;
664 }
665
666 valid->subtable_length = p - table;
667 }
668
669
670 static void
671 gxv_LookupTable_fmt6_validate( FT_Bytes table,
672 FT_Bytes limit,
673 GXV_Validator valid )
674 {
675 FT_Bytes p = table;
676 FT_UShort unit;
677 FT_UShort prev_glyph;
678
679 FT_UShort unitSize;
680 FT_UShort nUnits;
681 FT_UShort glyph;
682 GXV_LookupValueDesc value;
683
684
685 GXV_NAME_ENTER( "LookupTable format 6" );
686
687 unitSize = nUnits = 0;
688 gxv_BinSrchHeader_validate( p, limit, &unitSize, &nUnits, valid );
689 p += valid->subtable_length;
690
691 GXV_UNITSIZE_VALIDATE( "format6", unitSize, nUnits, 4 );
692
693 for ( unit = 0, prev_glyph = 0; unit < nUnits; unit++ )
694 {
695 GXV_LIMIT_CHECK( 2 + 2 );
696 glyph = FT_NEXT_USHORT( p );
697 value = GXV_LOOKUP_VALUE_LOAD( p, valid->lookupval_sign );
698
699 if ( gxv_glyphid_validate( glyph, valid ) )
700 GXV_TRACE(( " endmarker found within defined range"
701 " (entry %d < nUnits=%d)\n",
702 unit, nUnits ));
703
704 if ( prev_glyph > glyph )
705 {
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;
710 }
711 prev_glyph = glyph;
712
713 valid->lookupval_func( glyph, &value, valid );
714 }
715
716 gxv_LookupTable_fmt6_skip_endmarkers( p, unitSize, valid );
717 p += valid->subtable_length;
718
719 valid->subtable_length = p - table;
720 GXV_EXIT;
721 }
722
723
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 )
729 {
730 FT_Bytes p = table;
731 FT_UShort i;
732
733 GXV_LookupValueDesc value;
734 FT_UShort firstGlyph;
735 FT_UShort glyphCount;
736
737
738 GXV_NAME_ENTER( "LookupTable format 8" );
739
740 /* firstGlyph + glyphCount */
741 GXV_LIMIT_CHECK( 2 + 2 );
742 firstGlyph = FT_NEXT_USHORT( p );
743 glyphCount = FT_NEXT_USHORT( p );
744
745 gxv_glyphid_validate( firstGlyph, valid );
746 gxv_glyphid_validate( (FT_UShort)( firstGlyph + glyphCount ), valid );
747
748 /* valueArray */
749 for ( i = 0; i < glyphCount; i++ )
750 {
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 );
754 }
755
756 valid->subtable_length = p - table;
757 GXV_EXIT;
758 }
759
760
761 FT_LOCAL_DEF( void )
762 gxv_LookupTable_validate( FT_Bytes table,
763 FT_Bytes limit,
764 GXV_Validator valid )
765 {
766 FT_Bytes p = table;
767 FT_UShort format;
768
769 GXV_Validate_Func fmt_funcs_table[] =
770 {
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 */
780 };
781
782 GXV_Validate_Func func;
783
784
785 GXV_NAME_ENTER( "LookupTable" );
786
787 /* lookuptbl_head may be used in fmt4 transit function. */
788 valid->lookuptbl_head = table;
789
790 /* format */
791 GXV_LIMIT_CHECK( 2 );
792 format = FT_NEXT_USHORT( p );
793 GXV_TRACE(( " (format %d)\n", format ));
794
795 if ( format > 8 )
796 FT_INVALID_FORMAT;
797
798 func = fmt_funcs_table[format];
799 if ( func == NULL )
800 FT_INVALID_FORMAT;
801
802 func( p, limit, valid );
803 p += valid->subtable_length;
804
805 valid->subtable_length = p - table;
806
807 GXV_EXIT;
808 }
809
810
811 /*************************************************************************/
812 /*************************************************************************/
813 /***** *****/
814 /***** Glyph ID *****/
815 /***** *****/
816 /*************************************************************************/
817 /*************************************************************************/
818
819 FT_LOCAL_DEF( FT_Int )
820 gxv_glyphid_validate( FT_UShort gid,
821 GXV_Validator valid )
822 {
823 FT_Face face;
824
825
826 if ( gid == 0xFFFFU )
827 {
828 GXV_EXIT;
829 return 1;
830 }
831
832 face = valid->face;
833 if ( face->num_glyphs < gid )
834 {
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;
839 }
840
841 return 0;
842 }
843
844
845 /*************************************************************************/
846 /*************************************************************************/
847 /***** *****/
848 /***** CONTROL POINT *****/
849 /***** *****/
850 /*************************************************************************/
851 /*************************************************************************/
852
853 FT_LOCAL_DEF( void )
854 gxv_ctlPoint_validate( FT_UShort gid,
855 FT_Short ctl_point,
856 GXV_Validator valid )
857 {
858 FT_Face face;
859 FT_Error error;
860
861 FT_GlyphSlot glyph;
862 FT_Outline outline;
863 short n_points;
864
865
866 face = valid->face;
867
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;
873
874 glyph = face->glyph;
875 outline = glyph->outline;
876 n_points = outline.n_points;
877
878
879 if ( !( ctl_point < n_points ) )
880 FT_INVALID_DATA;
881 }
882
883
884 /*************************************************************************/
885 /*************************************************************************/
886 /***** *****/
887 /***** SFNT NAME *****/
888 /***** *****/
889 /*************************************************************************/
890 /*************************************************************************/
891
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 )
897 {
898 FT_SfntName name;
899 FT_UInt i;
900 FT_UInt nnames;
901
902
903 GXV_NAME_ENTER( "sfntName" );
904
905 if ( name_index < min_index || max_index < name_index )
906 FT_INVALID_FORMAT;
907
908 nnames = FT_Get_Sfnt_Name_Count( valid->face );
909 for ( i = 0; i < nnames; i++ )
910 {
911 if ( FT_Get_Sfnt_Name( valid->face, i, &name ) != GXV_Err_Ok )
912 continue ;
913
914 if ( name.name_id == name_index )
915 goto Out;
916 }
917
918 GXV_TRACE(( " nameIndex = %d (UNTITLED)\n", name_index ));
919 FT_INVALID_DATA;
920 goto Exit; /* make compiler happy */
921
922 Out:
923 FT_TRACE1(( " nameIndex = %d (", name_index ));
924 GXV_TRACE_HEXDUMP_SFNTNAME( name );
925 FT_TRACE1(( ")\n" ));
926
927 Exit:
928 GXV_EXIT;
929 }
930
931
932 /*************************************************************************/
933 /*************************************************************************/
934 /***** *****/
935 /***** STATE TABLE *****/
936 /***** *****/
937 /*************************************************************************/
938 /*************************************************************************/
939
940 /* -------------------------- Class Table --------------------------- */
941
942 /*
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>
948 */
949
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 )
956 {
957 FT_Bytes p = table;
958 FT_Bytes limit = table + *length_p;
959 FT_UShort firstGlyph;
960 FT_UShort nGlyphs;
961
962
963 GXV_NAME_ENTER( "ClassTable" );
964
965 *maxClassID_p = 3; /* Classes 0, 2, and 3 are predefined */
966
967 GXV_LIMIT_CHECK( 2 + 2 );
968 firstGlyph = FT_NEXT_USHORT( p );
969 nGlyphs = FT_NEXT_USHORT( p );
970
971 GXV_TRACE(( " (firstGlyph = %d, nGlyphs = %d)\n", firstGlyph, nGlyphs ));
972
973 if ( !nGlyphs )
974 goto Out;
975
976 gxv_glyphid_validate( (FT_UShort)( firstGlyph + nGlyphs ), valid );
977
978 {
979 FT_Byte nGlyphInClass[256];
980 FT_Byte classID;
981 FT_UShort i;
982
983
984 ft_memset( nGlyphInClass, 0, 256 );
985
986
987 for ( i = 0; i < nGlyphs; i++ )
988 {
989 GXV_LIMIT_CHECK( 1 );
990 classID = FT_NEXT_BYTE( p );
991 switch ( classID )
992 {
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;
999
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 */
1004
1005 nGlyphInClass[classID]++;
1006 break;
1007 }
1008 }
1009 *length_p = (FT_UShort)( p - table );
1010
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? */
1015 }
1016
1017 Out:
1018 GXV_TRACE(( "Declared stateSize=0x%02x, Used maxClassID=0x%02x\n",
1019 stateSize, *maxClassID_p ));
1020 GXV_EXIT;
1021 }
1022
1023
1024 /* --------------------------- State Array ----------------------------- */
1025
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 )
1034 {
1035 FT_Bytes p = table;
1036 FT_Bytes limit = table + *length_p;
1037 FT_Byte clazz;
1038 FT_Byte entry;
1039
1040 FT_UNUSED( stateSize ); /* for the non-debugging case */
1041
1042
1043 GXV_NAME_ENTER( "StateArray" );
1044
1045 GXV_TRACE(( "parse %d bytes by stateSize=%d maxClassID=%d\n",
1046 (int)(*length_p), stateSize, (int)(maxClassID) ));
1047
1048 /*
1049 * 2 states are predefined and must be described in StateArray:
1050 * state 0 (start of text), 1 (start of line)
1051 */
1052 GXV_LIMIT_CHECK( ( 1 + maxClassID ) * 2 );
1053
1054 *maxState_p = 0;
1055 *maxEntry_p = 0;
1056
1057 /* read if enough to read another state */
1058 while ( p + ( 1 + maxClassID ) <= limit )
1059 {
1060 (*maxState_p)++;
1061 for ( clazz = 0; clazz <= maxClassID; clazz++ )
1062 {
1063 entry = FT_NEXT_BYTE( p );
1064 *maxEntry_p = (FT_Byte)FT_MAX( *maxEntry_p, entry );
1065 }
1066 }
1067 GXV_TRACE(( "parsed: maxState=%d, maxEntry=%d\n",
1068 *maxState_p, *maxEntry_p ));
1069
1070 *length_p = (FT_UShort)( p - table );
1071
1072 GXV_EXIT;
1073 }
1074
1075
1076 /* --------------------------- Entry Table ----------------------------- */
1077
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 )
1088 {
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 );
1094
1095 GXV_XStateTable_GlyphOffsetDesc glyphOffset;
1096
1097
1098 GXV_NAME_ENTER( "EntryTable" );
1099
1100 GXV_TRACE(( "maxEntry=%d entrySize=%d\n", maxEntry, entrySize ));
1101
1102 if ( ( maxEntry + 1 ) * entrySize > *length_p )
1103 {
1104 if ( valid->root->level >= FT_VALIDATE_PARANOID )
1105 FT_INVALID_TOO_SHORT;
1106
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 ));
1111 }
1112
1113 for ( entry = 0; entry <= maxEntry; entry++ )
1114 {
1115 FT_UShort newState;
1116 FT_UShort flags;
1117
1118
1119 GXV_LIMIT_CHECK( 2 + 2 );
1120 newState = FT_NEXT_USHORT( p );
1121 flags = FT_NEXT_USHORT( p );
1122
1123
1124 if ( newState < stateArray ||
1125 stateArray + stateArray_length < newState )
1126 {
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;
1132 }
1133
1134 if ( 0 != ( ( newState - stateArray ) % ( 1 + maxClassID ) ) )
1135 {
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;
1141 }
1142
1143 state = (FT_Byte)( ( newState - stateArray ) / ( 1 + maxClassID ) );
1144
1145 switch ( GXV_GLYPHOFFSET_FMT( statetable ) )
1146 {
1147 case GXV_GLYPHOFFSET_NONE:
1148 glyphOffset.uc = 0; /* make compiler happy */
1149 break;
1150
1151 case GXV_GLYPHOFFSET_UCHAR:
1152 glyphOffset.uc = FT_NEXT_BYTE( p );
1153 break;
1154
1155 case GXV_GLYPHOFFSET_CHAR:
1156 glyphOffset.c = FT_NEXT_CHAR( p );
1157 break;
1158
1159 case GXV_GLYPHOFFSET_USHORT:
1160 glyphOffset.u = FT_NEXT_USHORT( p );
1161 break;
1162
1163 case GXV_GLYPHOFFSET_SHORT:
1164 glyphOffset.s = FT_NEXT_SHORT( p );
1165 break;
1166
1167 case GXV_GLYPHOFFSET_ULONG:
1168 glyphOffset.ul = FT_NEXT_ULONG( p );
1169 break;
1170
1171 case GXV_GLYPHOFFSET_LONG:
1172 glyphOffset.l = FT_NEXT_LONG( p );
1173 break;
1174
1175 default:
1176 if ( valid->root->level >= FT_VALIDATE_PARANOID )
1177 FT_INVALID_FORMAT;
1178 goto Exit;
1179 }
1180
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 );
1188 }
1189
1190 Exit:
1191 *length_p = (FT_UShort)( p - table );
1192
1193 GXV_EXIT;
1194 }
1195
1196
1197 /* =========================== State Table ============================= */
1198
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 )
1208 {
1209 FT_UShort o[3];
1210 FT_UShort* l[3];
1211 FT_UShort buff[4];
1212
1213
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;
1220
1221 gxv_set_length_by_ushort_offset( o, l, buff, 3, table_size, valid );
1222 }
1223
1224
1225 FT_LOCAL_DEF( void )
1226 gxv_StateTable_validate( FT_Bytes table,
1227 FT_Bytes limit,
1228 GXV_Validator valid )
1229 {
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 */
1234
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;
1241
1242 GXV_StateTable_Subtable_Setup_Func setup_func;
1243
1244 FT_Bytes p = table;
1245
1246
1247 GXV_NAME_ENTER( "StateTable" );
1248
1249 GXV_TRACE(( "StateTable header\n" ));
1250
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 );
1256
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 ));
1261
1262 if ( stateSize > 0xFF )
1263 FT_INVALID_DATA;
1264
1265 if ( valid->statetable.optdata_load_func != NULL )
1266 valid->statetable.optdata_load_func( p, limit, valid );
1267
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;
1272
1273 setup_func( (FT_UShort)( limit - table ),
1274 classTable,
1275 stateArray,
1276 entryTable,
1277 &classTable_length,
1278 &stateArray_length,
1279 &entryTable_length,
1280 valid );
1281
1282 GXV_TRACE(( "StateTable Subtables\n" ));
1283
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 );
1292
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
1302 {
1303 maxState = 1; /* 0:start of text, 1:start of line are predefined */
1304 maxEntry = 0;
1305 }
1306
1307 if ( maxEntry > 0 && entryTable == 0 )
1308 FT_INVALID_OFFSET;
1309
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 );
1320
1321 GXV_EXIT;
1322 }
1323
1324
1325 /* ================= eXtended State Table (for morx) =================== */
1326
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 )
1336 {
1337 FT_ULong o[3];
1338 FT_ULong* l[3];
1339 FT_ULong buff[4];
1340
1341
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;
1348
1349 gxv_set_length_by_ulong_offset( o, l, buff, 4, table_size, valid );
1350 }
1351
1352
1353 static void
1354 gxv_XClassTable_lookupval_validate( FT_UShort glyph,
1355 GXV_LookupValueCPtr value_p,
1356 GXV_Validator valid )
1357 {
1358 FT_UNUSED( glyph );
1359
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;
1364 }
1365
1366
1367 /*
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 +===============+ |
1385 |
1386 .... |
1387 |
1388 16bit value array |
1389 +===============+ |
1390 | value | <-------+
1391 ....
1392 */
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 )
1398 {
1399 FT_Bytes p;
1400 FT_Bytes limit;
1401 FT_UShort offset;
1402 GXV_LookupValueDesc value;
1403
1404 /* XXX: check range? */
1405 offset = (FT_UShort)( base_value_p->u +
1406 relative_gindex * sizeof ( FT_UShort ) );
1407
1408 p = valid->lookuptbl_head + offset;
1409 limit = lookuptbl_limit;
1410
1411 GXV_LIMIT_CHECK ( 2 );
1412 value.u = FT_NEXT_USHORT( p );
1413
1414 return value;
1415 }
1416
1417
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 )
1426 {
1427 FT_Bytes p = table;
1428 FT_Bytes limit = table + *length_p;
1429 FT_UShort clazz;
1430 FT_UShort entry;
1431
1432 FT_UNUSED( stateSize ); /* for the non-debugging case */
1433
1434
1435 GXV_NAME_ENTER( "XStateArray" );
1436
1437 GXV_TRACE(( "parse % 3d bytes by stateSize=% 3d maxClassID=% 3d\n",
1438 (int)(*length_p), stateSize, (int)(maxClassID) ));
1439
1440 /*
1441 * 2 states are predefined and must be described:
1442 * state 0 (start of text), 1 (start of line)
1443 */
1444 GXV_LIMIT_CHECK( ( 1 + maxClassID ) * 2 * 2 );
1445
1446 *maxState_p = 0;
1447 *maxEntry_p = 0;
1448
1449 /* read if enough to read another state */
1450 while ( p + ( ( 1 + maxClassID ) * 2 ) <= limit )
1451 {
1452 (*maxState_p)++;
1453 for ( clazz = 0; clazz <= maxClassID; clazz++ )
1454 {
1455 entry = FT_NEXT_USHORT( p );
1456 *maxEntry_p = (FT_UShort)FT_MAX( *maxEntry_p, entry );
1457 }
1458 }
1459 GXV_TRACE(( "parsed: maxState=%d, maxEntry=%d\n",
1460 *maxState_p, *maxEntry_p ));
1461
1462 *length_p = p - table;
1463
1464 GXV_EXIT;
1465 }
1466
1467
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 )
1477 {
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 );
1483
1484
1485 GXV_NAME_ENTER( "XEntryTable" );
1486 GXV_TRACE(( "maxEntry=%d entrySize=%d\n", maxEntry, entrySize ));
1487
1488 if ( ( p + ( maxEntry + 1 ) * entrySize ) > limit )
1489 FT_INVALID_TOO_SHORT;
1490
1491 for (entry = 0; entry <= maxEntry ; entry++ )
1492 {
1493 FT_UShort newState_idx;
1494 FT_UShort flags;
1495 GXV_XStateTable_GlyphOffsetDesc glyphOffset;
1496
1497
1498 GXV_LIMIT_CHECK( 2 + 2 );
1499 newState_idx = FT_NEXT_USHORT( p );
1500 flags = FT_NEXT_USHORT( p );
1501
1502 if ( stateArray_length < (FT_ULong)( newState_idx * 2 ) )
1503 {
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;
1508 }
1509
1510 state = (FT_UShort)( newState_idx / ( 1 + maxClassID ) );
1511 if ( 0 != ( newState_idx % ( 1 + maxClassID ) ) )
1512 {
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;
1518 }
1519
1520 switch ( GXV_GLYPHOFFSET_FMT( xstatetable ) )
1521 {
1522 case GXV_GLYPHOFFSET_NONE:
1523 glyphOffset.uc = 0; /* make compiler happy */
1524 break;
1525
1526 case GXV_GLYPHOFFSET_UCHAR:
1527 glyphOffset.uc = FT_NEXT_BYTE( p );
1528 break;
1529
1530 case GXV_GLYPHOFFSET_CHAR:
1531 glyphOffset.c = FT_NEXT_CHAR( p );
1532 break;
1533
1534 case GXV_GLYPHOFFSET_USHORT:
1535 glyphOffset.u = FT_NEXT_USHORT( p );
1536 break;
1537
1538 case GXV_GLYPHOFFSET_SHORT:
1539 glyphOffset.s = FT_NEXT_SHORT( p );
1540 break;
1541
1542 case GXV_GLYPHOFFSET_ULONG:
1543 glyphOffset.ul = FT_NEXT_ULONG( p );
1544 break;
1545
1546 case GXV_GLYPHOFFSET_LONG:
1547 glyphOffset.l = FT_NEXT_LONG( p );
1548 break;
1549
1550 default:
1551 if ( valid->root->level >= FT_VALIDATE_PARANOID )
1552 FT_INVALID_FORMAT;
1553 goto Exit;
1554 }
1555
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 );
1563 }
1564
1565 Exit:
1566 *length_p = p - table;
1567
1568 GXV_EXIT;
1569 }
1570
1571
1572 FT_LOCAL_DEF( void )
1573 gxv_XStateTable_validate( FT_Bytes table,
1574 FT_Bytes limit,
1575 GXV_Validator valid )
1576 {
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 */
1581
1582 FT_ULong classTable_length;
1583 FT_ULong stateArray_length;
1584 FT_ULong entryTable_length;
1585 FT_UShort maxState;
1586 FT_UShort maxEntry;
1587
1588 GXV_XStateTable_Subtable_Setup_Func setup_func;
1589
1590 FT_Bytes p = table;
1591
1592
1593 GXV_NAME_ENTER( "XStateTable" );
1594
1595 GXV_TRACE(( "XStateTable header\n" ));
1596
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 );
1602
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 ));
1607
1608 if ( valid->xstatetable.nClasses > 0xFFFFU )
1609 FT_INVALID_DATA;
1610
1611 GXV_TRACE(( "StateTable Subtables\n" ));
1612
1613 if ( valid->xstatetable.optdata_load_func != NULL )
1614 valid->xstatetable.optdata_load_func( p, limit, valid );
1615
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;
1620
1621 setup_func( limit - table,
1622 classTable,
1623 stateArray,
1624 entryTable,
1625 &classTable_length,
1626 &stateArray_length,
1627 &entryTable_length,
1628 valid );
1629
1630 if ( classTable != 0 )
1631 {
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;
1641 }
1642 else
1643 {
1644 /* XXX: check range? */
1645 valid->xstatetable.maxClassID =
1646 (FT_UShort)( valid->xstatetable.nClasses - 1 );
1647 }
1648
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
1658 {
1659 maxState = 1; /* 0:start of text, 1:start of line are predefined */
1660 maxEntry = 0;
1661 }
1662
1663 if ( maxEntry > 0 && entryTable == 0 )
1664 FT_INVALID_OFFSET;
1665
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 );
1675
1676 GXV_EXIT;
1677 }
1678
1679
1680 /*************************************************************************/
1681 /*************************************************************************/
1682 /***** *****/
1683 /***** Table overlapping *****/
1684 /***** *****/
1685 /*************************************************************************/
1686 /*************************************************************************/
1687
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 )
1693 {
1694 if ( table1_start == table2_start )
1695 {
1696 if ( ( table1_length == 0 || table2_length == 0 ) )
1697 goto Out;
1698 }
1699 else if ( table1_start < table2_start )
1700 {
1701 if ( ( table1_start + table1_length ) <= table2_start )
1702 goto Out;
1703 }
1704 else if ( table1_start > table2_start )
1705 {
1706 if ( ( table1_start >= table2_start + table2_length ) )
1707 goto Out;
1708 }
1709 return 1;
1710
1711 Out:
1712 return 0;
1713 }
1714
1715
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 )
1721 {
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++;
1726 }
1727
1728
1729 FT_LOCAL_DEF( void )
1730 gxv_odtect_validate( GXV_odtect_Range odtect,
1731 GXV_Validator valid )
1732 {
1733 FT_UInt i, j;
1734
1735
1736 GXV_NAME_ENTER( "check overlap among multi ranges" );
1737
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 ) )
1744 {
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;
1753 }
1754
1755 GXV_EXIT;
1756 }
1757
1758
1759 /* END */