1 /**************************************************************************** 2 * 3 * gxvcommn.h 4 * 5 * TrueTypeGX/AAT common tables validation (specification). 6 * 7 * Copyright (C) 2004-2020 by 8 * 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 /* 29 * keywords in variable naming 30 * --------------------------- 31 * table: Of type FT_Bytes, pointing to the start of this table/subtable. 32 * limit: Of type FT_Bytes, pointing to the end of this table/subtable, 33 * including padding for alignment. 34 * offset: Of type FT_UInt, the number of octets from the start to target. 35 * length: Of type FT_UInt, the number of octets from the start to the 36 * end in this table/subtable, including padding for alignment. 37 * 38 * _MIN, _MAX: Should be added to the tail of macros, as INT_MIN, etc. 39 */ 40 41 42 #ifndef GXVCOMMN_H_ 43 #define GXVCOMMN_H_ 44 45 46 #include "gxvalid.h" 47 #include <freetype/internal/ftdebug.h> 48 #include <freetype/ftsnames.h> 49 50 51 FT_BEGIN_HEADER 52 53 54 /* some variables are not evaluated or only used in trace */ 55 56 #ifdef FT_DEBUG_LEVEL_TRACE 57 #define GXV_LOAD_TRACE_VARS 58 #else 59 #undef GXV_LOAD_TRACE_VARS 60 #endif 61 62 #undef GXV_LOAD_UNUSED_VARS /* debug purpose */ 63 64 #define IS_PARANOID_VALIDATION ( gxvalid->root->level >= FT_VALIDATE_PARANOID ) 65 #define GXV_SET_ERR_IF_PARANOID( err ) { if ( IS_PARANOID_VALIDATION ) ( err ); } 66 67 /*************************************************************************/ 68 /*************************************************************************/ 69 /***** *****/ 70 /***** VALIDATION *****/ 71 /***** *****/ 72 /*************************************************************************/ 73 /*************************************************************************/ 74 75 typedef struct GXV_ValidatorRec_* GXV_Validator; 76 77 78 #define DUMMY_LIMIT 0 79 80 typedef void 81 (*GXV_Validate_Func)( FT_Bytes table, 82 FT_Bytes limit, 83 GXV_Validator gxvalid ); 84 85 86 /* ====================== LookupTable Validator ======================== */ 87 88 typedef union GXV_LookupValueDesc_ 89 { 90 FT_UShort u; 91 FT_Short s; 92 93 } GXV_LookupValueDesc; 94 95 typedef const GXV_LookupValueDesc* GXV_LookupValueCPtr; 96 97 typedef enum GXV_LookupValue_SignSpec_ 98 { 99 GXV_LOOKUPVALUE_UNSIGNED = 0, 100 GXV_LOOKUPVALUE_SIGNED 101 102 } GXV_LookupValue_SignSpec; 103 104 105 typedef void 106 (*GXV_Lookup_Value_Validate_Func)( FT_UShort glyph, 107 GXV_LookupValueCPtr value_p, 108 GXV_Validator gxvalid ); 109 110 typedef GXV_LookupValueDesc 111 (*GXV_Lookup_Fmt4_Transit_Func)( FT_UShort relative_gindex, 112 GXV_LookupValueCPtr base_value_p, 113 FT_Bytes lookuptbl_limit, 114 GXV_Validator gxvalid ); 115 116 117 /* ====================== StateTable Validator ========================= */ 118 119 typedef enum GXV_GlyphOffset_Format_ 120 { 121 GXV_GLYPHOFFSET_NONE = -1, 122 GXV_GLYPHOFFSET_UCHAR = 2, 123 GXV_GLYPHOFFSET_CHAR, 124 GXV_GLYPHOFFSET_USHORT = 4, 125 GXV_GLYPHOFFSET_SHORT, 126 GXV_GLYPHOFFSET_ULONG = 8, 127 GXV_GLYPHOFFSET_LONG 128 129 } GXV_GlyphOffset_Format; 130 131 132 #define GXV_GLYPHOFFSET_FMT( table ) \ 133 ( gxvalid->table.entry_glyphoffset_fmt ) 134 135 #define GXV_GLYPHOFFSET_SIZE( table ) \ 136 ( gxvalid->table.entry_glyphoffset_fmt / 2 ) 137 138 139 /* ----------------------- 16bit StateTable ---------------------------- */ 140 141 typedef union GXV_StateTable_GlyphOffsetDesc_ 142 { 143 FT_Byte uc; 144 FT_UShort u; /* same as GXV_LookupValueDesc */ 145 FT_ULong ul; 146 FT_Char c; 147 FT_Short s; /* same as GXV_LookupValueDesc */ 148 FT_Long l; 149 150 } GXV_StateTable_GlyphOffsetDesc; 151 152 typedef const GXV_StateTable_GlyphOffsetDesc* GXV_StateTable_GlyphOffsetCPtr; 153 154 typedef void 155 (*GXV_StateTable_Subtable_Setup_Func)( FT_UShort table_size, 156 FT_UShort classTable, 157 FT_UShort stateArray, 158 FT_UShort entryTable, 159 FT_UShort* classTable_length_p, 160 FT_UShort* stateArray_length_p, 161 FT_UShort* entryTable_length_p, 162 GXV_Validator gxvalid ); 163 164 typedef void 165 (*GXV_StateTable_Entry_Validate_Func)( 166 FT_Byte state, 167 FT_UShort flags, 168 GXV_StateTable_GlyphOffsetCPtr glyphOffset_p, 169 FT_Bytes statetable_table, 170 FT_Bytes statetable_limit, 171 GXV_Validator gxvalid ); 172 173 typedef void 174 (*GXV_StateTable_OptData_Load_Func)( FT_Bytes table, 175 FT_Bytes limit, 176 GXV_Validator gxvalid ); 177 178 typedef struct GXV_StateTable_ValidatorRec_ 179 { 180 GXV_GlyphOffset_Format entry_glyphoffset_fmt; 181 void* optdata; 182 183 GXV_StateTable_Subtable_Setup_Func subtable_setup_func; 184 GXV_StateTable_Entry_Validate_Func entry_validate_func; 185 GXV_StateTable_OptData_Load_Func optdata_load_func; 186 187 } GXV_StateTable_ValidatorRec, *GXV_StateTable_ValidatorRecData; 188 189 190 /* ---------------------- 32bit XStateTable ---------------------------- */ 191 192 typedef GXV_StateTable_GlyphOffsetDesc GXV_XStateTable_GlyphOffsetDesc; 193 194 typedef const GXV_XStateTable_GlyphOffsetDesc* GXV_XStateTable_GlyphOffsetCPtr; 195 196 typedef void 197 (*GXV_XStateTable_Subtable_Setup_Func)( FT_ULong table_size, 198 FT_ULong classTable, 199 FT_ULong stateArray, 200 FT_ULong entryTable, 201 FT_ULong* classTable_length_p, 202 FT_ULong* stateArray_length_p, 203 FT_ULong* entryTable_length_p, 204 GXV_Validator gxvalid ); 205 206 typedef void 207 (*GXV_XStateTable_Entry_Validate_Func)( 208 FT_UShort state, 209 FT_UShort flags, 210 GXV_StateTable_GlyphOffsetCPtr glyphOffset_p, 211 FT_Bytes xstatetable_table, 212 FT_Bytes xstatetable_limit, 213 GXV_Validator gxvalid ); 214 215 216 typedef GXV_StateTable_OptData_Load_Func GXV_XStateTable_OptData_Load_Func; 217 218 219 typedef struct GXV_XStateTable_ValidatorRec_ 220 { 221 int entry_glyphoffset_fmt; 222 void* optdata; 223 224 GXV_XStateTable_Subtable_Setup_Func subtable_setup_func; 225 GXV_XStateTable_Entry_Validate_Func entry_validate_func; 226 GXV_XStateTable_OptData_Load_Func optdata_load_func; 227 228 FT_ULong nClasses; 229 FT_UShort maxClassID; 230 231 } GXV_XStateTable_ValidatorRec, *GXV_XStateTable_ValidatorRecData; 232 233 234 /* ===================================================================== */ 235 236 typedef struct GXV_ValidatorRec_ 237 { 238 FT_Validator root; 239 240 FT_Face face; 241 void* table_data; 242 243 FT_ULong subtable_length; 244 245 GXV_LookupValue_SignSpec lookupval_sign; 246 GXV_Lookup_Value_Validate_Func lookupval_func; 247 GXV_Lookup_Fmt4_Transit_Func lookupfmt4_trans; 248 FT_Bytes lookuptbl_head; 249 250 FT_UShort min_gid; 251 FT_UShort max_gid; 252 253 GXV_StateTable_ValidatorRec statetable; 254 GXV_XStateTable_ValidatorRec xstatetable; 255 256 #ifdef FT_DEBUG_LEVEL_TRACE 257 FT_UInt debug_indent; 258 const FT_String* debug_function_name[3]; 259 #endif 260 261 } GXV_ValidatorRec; 262 263 264 #define GXV_TABLE_DATA( tag, field ) \ 265 ( ( (GXV_ ## tag ## _Data)gxvalid->table_data )->field ) 266 267 #undef FT_INVALID_ 268 #define FT_INVALID_( _error ) \ 269 ft_validator_error( gxvalid->root, FT_THROW( _error ) ) 270 271 #define GXV_LIMIT_CHECK( _count ) \ 272 FT_BEGIN_STMNT \ 273 if ( p + _count > ( limit? limit : gxvalid->root->limit ) ) \ 274 FT_INVALID_TOO_SHORT; \ 275 FT_END_STMNT 276 277 278 #ifdef FT_DEBUG_LEVEL_TRACE 279 280 #define GXV_INIT gxvalid->debug_indent = 0 281 282 #define GXV_NAME_ENTER( name ) \ 283 FT_BEGIN_STMNT \ 284 gxvalid->debug_indent += 2; \ 285 FT_TRACE4(( "%*.s", gxvalid->debug_indent, 0 )); \ 286 FT_TRACE4(( "%s table\n", name )); \ 287 FT_END_STMNT 288 289 #define GXV_EXIT gxvalid->debug_indent -= 2 290 291 #define GXV_TRACE( s ) \ 292 FT_BEGIN_STMNT \ 293 FT_TRACE4(( "%*.s", gxvalid->debug_indent, 0 )); \ 294 FT_TRACE4( s ); \ 295 FT_END_STMNT 296 297 #else /* !FT_DEBUG_LEVEL_TRACE */ 298 299 #define GXV_INIT do { } while ( 0 ) 300 #define GXV_NAME_ENTER( name ) do { } while ( 0 ) 301 #define GXV_EXIT do { } while ( 0 ) 302 303 #define GXV_TRACE( s ) do { } while ( 0 ) 304 305 #endif /* !FT_DEBUG_LEVEL_TRACE */ 306 307 308 /*************************************************************************/ 309 /*************************************************************************/ 310 /***** *****/ 311 /***** 32bit alignment checking *****/ 312 /***** *****/ 313 /*************************************************************************/ 314 /*************************************************************************/ 315 316 #define GXV_32BIT_ALIGNMENT_VALIDATE( a ) \ 317 FT_BEGIN_STMNT \ 318 { \ 319 if ( (a) & 3 ) \ 320 FT_INVALID_OFFSET; \ 321 } \ 322 FT_END_STMNT 323 324 325 /*************************************************************************/ 326 /*************************************************************************/ 327 /***** *****/ 328 /***** Dumping Binary Data *****/ 329 /***** *****/ 330 /*************************************************************************/ 331 /*************************************************************************/ 332 333 #define GXV_TRACE_HEXDUMP( p, len ) \ 334 FT_BEGIN_STMNT \ 335 { \ 336 FT_Bytes b; \ 337 \ 338 \ 339 for ( b = p; b < (FT_Bytes)p + len; b++ ) \ 340 FT_TRACE1(("\\x%02x", *b)); \ 341 } \ 342 FT_END_STMNT 343 344 #define GXV_TRACE_HEXDUMP_C( p, len ) \ 345 FT_BEGIN_STMNT \ 346 { \ 347 FT_Bytes b; \ 348 \ 349 \ 350 for ( b = p; b < (FT_Bytes)p + len; b++ ) \ 351 if ( 0x40 < *b && *b < 0x7E ) \ 352 FT_TRACE1(("%c", *b)); \ 353 else \ 354 FT_TRACE1(("\\x%02x", *b)); \ 355 } \ 356 FT_END_STMNT 357 358 #define GXV_TRACE_HEXDUMP_SFNTNAME( n ) \ 359 GXV_TRACE_HEXDUMP( n.string, n.string_len ) 360 361 362 /*************************************************************************/ 363 /*************************************************************************/ 364 /***** *****/ 365 /***** LOOKUP TABLE *****/ 366 /***** *****/ 367 /*************************************************************************/ 368 /*************************************************************************/ 369 370 FT_LOCAL( void ) 371 gxv_BinSrchHeader_validate( FT_Bytes p, 372 FT_Bytes limit, 373 FT_UShort* unitSize_p, 374 FT_UShort* nUnits_p, 375 GXV_Validator gxvalid ); 376 377 FT_LOCAL( void ) 378 gxv_LookupTable_validate( FT_Bytes table, 379 FT_Bytes limit, 380 GXV_Validator gxvalid ); 381 382 383 /*************************************************************************/ 384 /*************************************************************************/ 385 /***** *****/ 386 /***** Glyph ID *****/ 387 /***** *****/ 388 /*************************************************************************/ 389 /*************************************************************************/ 390 391 FT_LOCAL( FT_Int ) 392 gxv_glyphid_validate( FT_UShort gid, 393 GXV_Validator gxvalid ); 394 395 396 /*************************************************************************/ 397 /*************************************************************************/ 398 /***** *****/ 399 /***** CONTROL POINT *****/ 400 /***** *****/ 401 /*************************************************************************/ 402 /*************************************************************************/ 403 404 FT_LOCAL( void ) 405 gxv_ctlPoint_validate( FT_UShort gid, 406 FT_UShort ctl_point, 407 GXV_Validator gxvalid ); 408 409 410 /*************************************************************************/ 411 /*************************************************************************/ 412 /***** *****/ 413 /***** SFNT NAME *****/ 414 /***** *****/ 415 /*************************************************************************/ 416 /*************************************************************************/ 417 418 FT_LOCAL( void ) 419 gxv_sfntName_validate( FT_UShort name_index, 420 FT_UShort min_index, 421 FT_UShort max_index, 422 GXV_Validator gxvalid ); 423 424 425 /*************************************************************************/ 426 /*************************************************************************/ 427 /***** *****/ 428 /***** STATE TABLE *****/ 429 /***** *****/ 430 /*************************************************************************/ 431 /*************************************************************************/ 432 433 FT_LOCAL( void ) 434 gxv_StateTable_subtable_setup( FT_UShort table_size, 435 FT_UShort classTable, 436 FT_UShort stateArray, 437 FT_UShort entryTable, 438 FT_UShort* classTable_length_p, 439 FT_UShort* stateArray_length_p, 440 FT_UShort* entryTable_length_p, 441 GXV_Validator gxvalid ); 442 443 FT_LOCAL( void ) 444 gxv_XStateTable_subtable_setup( FT_ULong table_size, 445 FT_ULong classTable, 446 FT_ULong stateArray, 447 FT_ULong entryTable, 448 FT_ULong* classTable_length_p, 449 FT_ULong* stateArray_length_p, 450 FT_ULong* entryTable_length_p, 451 GXV_Validator gxvalid ); 452 453 FT_LOCAL( void ) 454 gxv_StateTable_validate( FT_Bytes table, 455 FT_Bytes limit, 456 GXV_Validator gxvalid ); 457 458 FT_LOCAL( void ) 459 gxv_XStateTable_validate( FT_Bytes table, 460 FT_Bytes limit, 461 GXV_Validator gxvalid ); 462 463 464 /*************************************************************************/ 465 /*************************************************************************/ 466 /***** *****/ 467 /***** UTILITY MACROS AND FUNCTIONS *****/ 468 /***** *****/ 469 /*************************************************************************/ 470 /*************************************************************************/ 471 472 FT_LOCAL( void ) 473 gxv_array_getlimits_byte( FT_Bytes table, 474 FT_Bytes limit, 475 FT_Byte* min, 476 FT_Byte* max, 477 GXV_Validator gxvalid ); 478 479 FT_LOCAL( void ) 480 gxv_array_getlimits_ushort( FT_Bytes table, 481 FT_Bytes limit, 482 FT_UShort* min, 483 FT_UShort* max, 484 GXV_Validator gxvalid ); 485 486 FT_LOCAL( void ) 487 gxv_set_length_by_ushort_offset( FT_UShort* offset, 488 FT_UShort** length, 489 FT_UShort* buff, 490 FT_UInt nmemb, 491 FT_UShort limit, 492 GXV_Validator gxvalid ); 493 494 FT_LOCAL( void ) 495 gxv_set_length_by_ulong_offset( FT_ULong* offset, 496 FT_ULong** length, 497 FT_ULong* buff, 498 FT_UInt nmemb, 499 FT_ULong limit, 500 GXV_Validator gxvalid); 501 502 503 #define GXV_SUBTABLE_OFFSET_CHECK( _offset ) \ 504 FT_BEGIN_STMNT \ 505 if ( (_offset) > gxvalid->subtable_length ) \ 506 FT_INVALID_OFFSET; \ 507 FT_END_STMNT 508 509 #define GXV_SUBTABLE_LIMIT_CHECK( _count ) \ 510 FT_BEGIN_STMNT \ 511 if ( ( p + (_count) - gxvalid->subtable_start ) > \ 512 gxvalid->subtable_length ) \ 513 FT_INVALID_TOO_SHORT; \ 514 FT_END_STMNT 515 516 #define GXV_USHORT_TO_SHORT( _us ) \ 517 ( ( 0x8000U < ( _us ) ) ? ( ( _us ) - 0x8000U ) : ( _us ) ) 518 519 #define GXV_STATETABLE_HEADER_SIZE ( 2 + 2 + 2 + 2 ) 520 #define GXV_STATEHEADER_SIZE GXV_STATETABLE_HEADER_SIZE 521 522 #define GXV_XSTATETABLE_HEADER_SIZE ( 4 + 4 + 4 + 4 ) 523 #define GXV_XSTATEHEADER_SIZE GXV_XSTATETABLE_HEADER_SIZE 524 525 526 /*************************************************************************/ 527 /*************************************************************************/ 528 /***** *****/ 529 /***** Table overlapping *****/ 530 /***** *****/ 531 /*************************************************************************/ 532 /*************************************************************************/ 533 534 typedef struct GXV_odtect_DataRec_ 535 { 536 FT_Bytes start; 537 FT_ULong length; 538 FT_String* name; 539 540 } GXV_odtect_DataRec, *GXV_odtect_Data; 541 542 typedef struct GXV_odtect_RangeRec_ 543 { 544 FT_UInt nRanges; 545 GXV_odtect_Data range; 546 547 } GXV_odtect_RangeRec, *GXV_odtect_Range; 548 549 550 FT_LOCAL( void ) 551 gxv_odtect_add_range( FT_Bytes start, 552 FT_ULong length, 553 const FT_String* name, 554 GXV_odtect_Range odtect ); 555 556 FT_LOCAL( void ) 557 gxv_odtect_validate( GXV_odtect_Range odtect, 558 GXV_Validator gxvalid ); 559 560 561 #define GXV_ODTECT( n, odtect ) \ 562 GXV_odtect_DataRec odtect ## _range[n]; \ 563 GXV_odtect_RangeRec odtect ## _rec = { 0, NULL }; \ 564 GXV_odtect_Range odtect = NULL 565 566 #define GXV_ODTECT_INIT( odtect ) \ 567 FT_BEGIN_STMNT \ 568 odtect ## _rec.nRanges = 0; \ 569 odtect ## _rec.range = odtect ## _range; \ 570 odtect = & odtect ## _rec; \ 571 FT_END_STMNT 572 573 574 /* */ 575 576 FT_END_HEADER 577 578 #endif /* GXVCOMMN_H_ */ 579 580 581 /* END */ 582