269 TRACE_SANITIZE (this); 270 if (unlikely (!u.header.version.sanitize (c))) return_trace (false); 271 switch (u.header.version.major) { 272 case 2: /* version 2 is compatible with version 1 */ 273 case 1: return_trace (u.version1.sanitize (c)); 274 default:return_trace (true); 275 } 276 } 277 278 protected: 279 union { 280 struct { 281 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ 282 FixedVersion<>version; /* Version of the TTC Header (1.0 or 2.0), 283 * 0x00010000u or 0x00020000u */ 284 } header; 285 TTCHeaderVersion1 version1; 286 } u; 287 }; 288 289 290 /* 291 * OpenType Font File 292 */ 293 294 struct OpenTypeFontFile 295 { 296 static const hb_tag_t tableTag = HB_TAG ('_','_','_','_'); /* Sanitizer needs this. */ 297 298 enum { 299 CFFTag = HB_TAG ('O','T','T','O'), /* OpenType with Postscript outlines */ 300 TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 ), /* OpenType with TrueType outlines */ 301 TTCTag = HB_TAG ('t','t','c','f'), /* TrueType Collection */ 302 TrueTag = HB_TAG ('t','r','u','e'), /* Obsolete Apple TrueType */ 303 Typ1Tag = HB_TAG ('t','y','p','1') /* Obsolete Apple Type1 font in SFNT container */ 304 }; 305 306 inline hb_tag_t get_tag (void) const { return u.tag; } 307 308 inline unsigned int get_face_count (void) const 309 { 310 switch (u.tag) { 311 case CFFTag: /* All the non-collection tags */ 312 case TrueTag: 313 case Typ1Tag: 314 case TrueTypeTag: return 1; 315 case TTCTag: return u.ttcHeader.get_face_count (); 316 default: return 0; 317 } 318 } 319 inline const OpenTypeFontFace& get_face (unsigned int i) const 320 { 321 switch (u.tag) { 322 /* Note: for non-collection SFNT data we ignore index. This is because 323 * Apple dfont container is a container of SFNT's. So each SFNT is a 324 * non-TTC, but the index is more than zero. */ 325 case CFFTag: /* All the non-collection tags */ 326 case TrueTag: 327 case Typ1Tag: 328 case TrueTypeTag: return u.fontFace; 329 case TTCTag: return u.ttcHeader.get_face (i); 330 default: return Null(OpenTypeFontFace); 331 } 332 } 333 334 inline bool serialize_single (hb_serialize_context_t *c, 335 hb_tag_t sfnt_tag, 336 Supplier<hb_tag_t> &tags, 337 Supplier<hb_blob_t *> &blobs, 338 unsigned int table_count) 339 { 340 TRACE_SERIALIZE (this); 341 assert (sfnt_tag != TTCTag); 342 if (unlikely (!c->extend_min (*this))) return_trace (false); 343 return_trace (u.fontFace.serialize (c, sfnt_tag, tags, blobs, table_count)); 344 } 345 346 inline bool sanitize (hb_sanitize_context_t *c) const 347 { 348 TRACE_SANITIZE (this); 349 if (unlikely (!u.tag.sanitize (c))) return_trace (false); 350 switch (u.tag) { 351 case CFFTag: /* All the non-collection tags */ 352 case TrueTag: 353 case Typ1Tag: 354 case TrueTypeTag: return_trace (u.fontFace.sanitize (c)); 355 case TTCTag: return_trace (u.ttcHeader.sanitize (c)); 356 default: return_trace (true); 357 } 358 } 359 360 protected: 361 union { 362 Tag tag; /* 4-byte identifier. */ 363 OpenTypeFontFace fontFace; 364 TTCHeader ttcHeader; 365 } u; 366 public: 367 DEFINE_SIZE_UNION (4, tag); 368 }; 369 370 371 } /* namespace OT */ 372 373 374 #endif /* HB_OPEN_FILE_PRIVATE_HH */ | 269 TRACE_SANITIZE (this); 270 if (unlikely (!u.header.version.sanitize (c))) return_trace (false); 271 switch (u.header.version.major) { 272 case 2: /* version 2 is compatible with version 1 */ 273 case 1: return_trace (u.version1.sanitize (c)); 274 default:return_trace (true); 275 } 276 } 277 278 protected: 279 union { 280 struct { 281 Tag ttcTag; /* TrueType Collection ID string: 'ttcf' */ 282 FixedVersion<>version; /* Version of the TTC Header (1.0 or 2.0), 283 * 0x00010000u or 0x00020000u */ 284 } header; 285 TTCHeaderVersion1 version1; 286 } u; 287 }; 288 289 /* 290 * Mac Resource Fork 291 */ 292 293 struct ResourceRefItem 294 { 295 inline bool sanitize (hb_sanitize_context_t *c) const 296 { 297 TRACE_SANITIZE (this); 298 // actual data sanitization is done on ResourceForkHeader sanitizer 299 return_trace (likely (c->check_struct (this))); 300 } 301 302 HBINT16 id; /* Resource ID, is really should be signed? */ 303 HBINT16 nameOffset; /* Offset from beginning of resource name list 304 * to resource name, minus means there is no */ 305 HBUINT8 attr; /* Resource attributes */ 306 HBUINT24 dataOffset; /* Offset from beginning of resource data to 307 * data for this resource */ 308 HBUINT32 reserved; /* Reserved for handle to resource */ 309 public: 310 DEFINE_SIZE_STATIC (12); 311 }; 312 313 struct ResourceTypeItem 314 { 315 inline bool sanitize (hb_sanitize_context_t *c) const 316 { 317 TRACE_SANITIZE (this); 318 // RefList sanitization is done on ResourceMap sanitizer 319 return_trace (likely (c->check_struct (this))); 320 } 321 322 inline unsigned int get_resource_count () const 323 { 324 return numRes + 1; 325 } 326 327 inline bool is_sfnt () const 328 { 329 return type == HB_TAG ('s','f','n','t'); 330 } 331 332 inline const ResourceRefItem& get_ref_item (const void *base, 333 unsigned int i) const 334 { 335 return (base+refList)[i]; 336 } 337 338 protected: 339 Tag type; /* Resource type */ 340 HBUINT16 numRes; /* Number of resource this type in map minus 1 */ 341 OffsetTo<UnsizedArrayOf<ResourceRefItem> > 342 refList; /* Offset from beginning of resource type list 343 * to reference list for this type */ 344 public: 345 DEFINE_SIZE_STATIC (8); 346 }; 347 348 struct ResourceMap 349 { 350 inline bool sanitize (hb_sanitize_context_t *c) const 351 { 352 TRACE_SANITIZE (this); 353 if (unlikely (!c->check_struct (this))) 354 return_trace (false); 355 for (unsigned int i = 0; i < get_types_count (); ++i) 356 { 357 const ResourceTypeItem& type = get_type (i); 358 if (unlikely (!type.sanitize (c))) 359 return_trace (false); 360 for (unsigned int j = 0; j < type.get_resource_count (); ++j) 361 if (unlikely (!get_ref_item (type, j).sanitize (c))) 362 return_trace (false); 363 } 364 return_trace (true); 365 } 366 367 inline const ResourceTypeItem& get_type (unsigned int i) const 368 { 369 // Why offset from the second byte of the object? I'm not sure 370 return ((&reserved[2])+typeList)[i]; 371 } 372 373 inline unsigned int get_types_count () const 374 { 375 return nTypes + 1; 376 } 377 378 inline const ResourceRefItem &get_ref_item (const ResourceTypeItem &type, 379 unsigned int i) const 380 { 381 return type.get_ref_item (&(this+typeList), i); 382 } 383 384 inline const PString& get_name (const ResourceRefItem &item, 385 unsigned int i) const 386 { 387 if (item.nameOffset == -1) 388 return Null (PString); 389 390 return StructAtOffset<PString> (this, nameList + item.nameOffset); 391 } 392 393 protected: 394 HBUINT8 reserved[16]; /* Reserved for copy of resource header */ 395 LOffsetTo<ResourceMap> 396 reserved1; /* Reserved for handle to next resource map */ 397 HBUINT16 reserved2; /* Reserved for file reference number */ 398 HBUINT16 attr; /* Resource fork attribute */ 399 OffsetTo<UnsizedArrayOf<ResourceTypeItem> > 400 typeList; /* Offset from beginning of map to 401 * resource type list */ 402 HBUINT16 nameList; /* Offset from beginning of map to 403 * resource name list */ 404 HBUINT16 nTypes; /* Number of types in the map minus 1 */ 405 public: 406 DEFINE_SIZE_STATIC (30); 407 }; 408 409 struct ResourceForkHeader 410 { 411 inline unsigned int get_face_count () const 412 { 413 const ResourceMap &resource_map = this+map; 414 for (unsigned int i = 0; i < resource_map.get_types_count (); ++i) 415 { 416 const ResourceTypeItem& type = resource_map.get_type (i); 417 if (type.is_sfnt ()) 418 return type.get_resource_count (); 419 } 420 return 0; 421 } 422 423 inline const LArrayOf<HBUINT8>& get_data (const ResourceTypeItem& type, 424 unsigned int idx) const 425 { 426 const ResourceMap &resource_map = this+map; 427 unsigned int offset = dataOffset; 428 offset += resource_map.get_ref_item (type, idx).dataOffset; 429 return StructAtOffset<LArrayOf<HBUINT8> > (this, offset); 430 } 431 432 inline const OpenTypeFontFace& get_face (unsigned int idx) const 433 { 434 const ResourceMap &resource_map = this+map; 435 for (unsigned int i = 0; i < resource_map.get_types_count (); ++i) 436 { 437 const ResourceTypeItem& type = resource_map.get_type (i); 438 if (type.is_sfnt () && idx < type.get_resource_count ()) 439 return (OpenTypeFontFace&) get_data (type, idx).arrayZ; 440 } 441 return Null (OpenTypeFontFace); 442 } 443 444 inline bool sanitize (hb_sanitize_context_t *c) const 445 { 446 TRACE_SANITIZE (this); 447 if (unlikely (!c->check_struct (this))) 448 return_trace (false); 449 450 const ResourceMap &resource_map = this+map; 451 if (unlikely (!resource_map.sanitize (c))) 452 return_trace (false); 453 454 for (unsigned int i = 0; i < resource_map.get_types_count (); ++i) 455 { 456 const ResourceTypeItem& type = resource_map.get_type (i); 457 for (unsigned int j = 0; j < type.get_resource_count (); ++j) 458 { 459 const LArrayOf<HBUINT8>& data = get_data (type, j); 460 if (unlikely (!(data.sanitize (c) && 461 ((OpenTypeFontFace&) data.arrayZ).sanitize (c)))) 462 return_trace (false); 463 } 464 } 465 466 return_trace (true); 467 } 468 469 protected: 470 HBUINT32 dataOffset; /* Offset from beginning of resource fork 471 * to resource data */ 472 LOffsetTo<ResourceMap> 473 map; /* Offset from beginning of resource fork 474 * to resource map */ 475 HBUINT32 dataLen; /* Length of resource data */ 476 HBUINT32 mapLen; /* Length of resource map */ 477 public: 478 DEFINE_SIZE_STATIC (16); 479 }; 480 481 /* 482 * OpenType Font File 483 */ 484 485 struct OpenTypeFontFile 486 { 487 static const hb_tag_t tableTag = HB_TAG ('_','_','_','_'); /* Sanitizer needs this. */ 488 489 enum { 490 CFFTag = HB_TAG ('O','T','T','O'), /* OpenType with Postscript outlines */ 491 TrueTypeTag = HB_TAG ( 0 , 1 , 0 , 0 ), /* OpenType with TrueType outlines */ 492 TTCTag = HB_TAG ('t','t','c','f'), /* TrueType Collection */ 493 DFontTag = HB_TAG ( 0 , 0 , 1 , 0 ), /* DFont Mac Resource Fork */ 494 TrueTag = HB_TAG ('t','r','u','e'), /* Obsolete Apple TrueType */ 495 Typ1Tag = HB_TAG ('t','y','p','1') /* Obsolete Apple Type1 font in SFNT container */ 496 }; 497 498 inline hb_tag_t get_tag (void) const { return u.tag; } 499 500 inline unsigned int get_face_count (void) const 501 { 502 switch (u.tag) { 503 case CFFTag: /* All the non-collection tags */ 504 case TrueTag: 505 case Typ1Tag: 506 case TrueTypeTag: return 1; 507 case TTCTag: return u.ttcHeader.get_face_count (); 508 // case DFontTag: return u.rfHeader.get_face_count (); 509 default: return 0; 510 } 511 } 512 inline const OpenTypeFontFace& get_face (unsigned int i) const 513 { 514 switch (u.tag) { 515 /* Note: for non-collection SFNT data we ignore index. This is because 516 * Apple dfont container is a container of SFNT's. So each SFNT is a 517 * non-TTC, but the index is more than zero. */ 518 case CFFTag: /* All the non-collection tags */ 519 case TrueTag: 520 case Typ1Tag: 521 case TrueTypeTag: return u.fontFace; 522 case TTCTag: return u.ttcHeader.get_face (i); 523 // case DFontTag: return u.rfHeader.get_face (i); 524 default: return Null(OpenTypeFontFace); 525 } 526 } 527 528 inline bool serialize_single (hb_serialize_context_t *c, 529 hb_tag_t sfnt_tag, 530 Supplier<hb_tag_t> &tags, 531 Supplier<hb_blob_t *> &blobs, 532 unsigned int table_count) 533 { 534 TRACE_SERIALIZE (this); 535 assert (sfnt_tag != TTCTag); 536 if (unlikely (!c->extend_min (*this))) return_trace (false); 537 return_trace (u.fontFace.serialize (c, sfnt_tag, tags, blobs, table_count)); 538 } 539 540 inline bool sanitize (hb_sanitize_context_t *c) const 541 { 542 TRACE_SANITIZE (this); 543 if (unlikely (!u.tag.sanitize (c))) return_trace (false); 544 switch (u.tag) { 545 case CFFTag: /* All the non-collection tags */ 546 case TrueTag: 547 case Typ1Tag: 548 case TrueTypeTag: return_trace (u.fontFace.sanitize (c)); 549 case TTCTag: return_trace (u.ttcHeader.sanitize (c)); 550 // case DFontTag: return_trace (u.rfHeader.sanitize (c)); 551 default: return_trace (true); 552 } 553 } 554 555 protected: 556 union { 557 Tag tag; /* 4-byte identifier. */ 558 OpenTypeFontFace fontFace; 559 TTCHeader ttcHeader; 560 ResourceForkHeader rfHeader; 561 } u; 562 public: 563 DEFINE_SIZE_UNION (4, tag); 564 }; 565 566 567 } /* namespace OT */ 568 569 570 #endif /* HB_OPEN_FILE_PRIVATE_HH */ |