OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2007,2008,2009,2010 Red Hat, Inc. |
| 3 * |
| 4 * This is part of HarfBuzz, a text shaping library. |
| 5 * |
| 6 * Permission is hereby granted, without written agreement and without |
| 7 * license or royalty fees, to use, copy, modify, and distribute this |
| 8 * software and its documentation for any purpose, provided that the |
| 9 * above copyright notice and the following two paragraphs appear in |
| 10 * all copies of this software. |
| 11 * |
| 12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 16 * DAMAGE. |
| 17 * |
| 18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 23 * |
| 24 * Red Hat Author(s): Behdad Esfahbod |
| 25 */ |
| 26 |
| 27 #ifndef HB_OPEN_TYPE_PRIVATE_HH |
| 28 #define HB_OPEN_TYPE_PRIVATE_HH |
| 29 |
| 30 #include "hb-private.h" |
| 31 |
| 32 #include "hb-blob.h" |
| 33 |
| 34 HB_BEGIN_DECLS |
| 35 HB_END_DECLS |
| 36 |
| 37 |
| 38 /* |
| 39 * Casts |
| 40 */ |
| 41 |
| 42 /* Cast to struct T, reference to reference */ |
| 43 template<typename Type, typename TObject> |
| 44 inline const Type& CastR(const TObject &X) |
| 45 { return reinterpret_cast<const Type&> (X); } |
| 46 template<typename Type, typename TObject> |
| 47 inline Type& CastR(TObject &X) |
| 48 { return reinterpret_cast<Type&> (X); } |
| 49 |
| 50 /* Cast to struct T, pointer to pointer */ |
| 51 template<typename Type, typename TObject> |
| 52 inline const Type* CastP(const TObject *X) |
| 53 { return reinterpret_cast<const Type*> (X); } |
| 54 template<typename Type, typename TObject> |
| 55 inline Type* CastP(TObject *X) |
| 56 { return reinterpret_cast<Type*> (X); } |
| 57 |
| 58 /* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory |
| 59 * location pointed to by P plus Ofs bytes. */ |
| 60 template<typename Type> |
| 61 inline const Type& StructAtOffset(const void *P, unsigned int offset) |
| 62 { return * reinterpret_cast<const Type*> ((const char *) P + offset); } |
| 63 template<typename Type> |
| 64 inline Type& StructAtOffset(void *P, unsigned int offset) |
| 65 { return * reinterpret_cast<Type*> ((char *) P + offset); } |
| 66 |
| 67 /* StructAfter<T>(X) returns the struct T& that is placed after X. |
| 68 * Works with X of variable size also. X must implement get_size() */ |
| 69 template<typename Type, typename TObject> |
| 70 inline const Type& StructAfter(const TObject &X) |
| 71 { return StructAtOffset<Type>(&X, X.get_size()); } |
| 72 template<typename Type, typename TObject> |
| 73 inline Type& StructAfter(TObject &X) |
| 74 { return StructAtOffset<Type>(&X, X.get_size()); } |
| 75 |
| 76 |
| 77 |
| 78 /* |
| 79 * Size checking |
| 80 */ |
| 81 |
| 82 /* Check _assertion in a method environment */ |
| 83 #define _DEFINE_SIZE_ASSERTION(_assertion) \ |
| 84 inline void _size_assertion (void) const \ |
| 85 { ASSERT_STATIC (_assertion); } |
| 86 /* Check that _code compiles in a method environment */ |
| 87 #define _DEFINE_COMPILES_ASSERTION(_code) \ |
| 88 inline void _compiles_assertion (void) const \ |
| 89 { _code; } |
| 90 |
| 91 |
| 92 #define DEFINE_SIZE_STATIC(size) \ |
| 93 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size)); \ |
| 94 static const unsigned int static_size = (size); \ |
| 95 static const unsigned int min_size = (size) |
| 96 |
| 97 /* Size signifying variable-sized array */ |
| 98 #define VAR 1 |
| 99 |
| 100 #define DEFINE_SIZE_UNION(size, _member) \ |
| 101 _DEFINE_SIZE_ASSERTION (this->u._member.static_size == (size)); \ |
| 102 static const unsigned int min_size = (size) |
| 103 |
| 104 #define DEFINE_SIZE_MIN(size) \ |
| 105 _DEFINE_SIZE_ASSERTION (sizeof (*this) >= (size)); \ |
| 106 static const unsigned int min_size = (size) |
| 107 |
| 108 #define DEFINE_SIZE_ARRAY(size, array) \ |
| 109 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \ |
| 110 _DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \ |
| 111 static const unsigned int min_size = (size) |
| 112 |
| 113 #define DEFINE_SIZE_ARRAY2(size, array1, array2) \ |
| 114 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) +
sizeof (this->array2[0])); \ |
| 115 _DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].sta
tic_size) \ |
| 116 static const unsigned int min_size = (size) |
| 117 |
| 118 |
| 119 |
| 120 /* |
| 121 * Null objects |
| 122 */ |
| 123 |
| 124 /* Global nul-content Null pool. Enlarge as necessary. */ |
| 125 static const void *_NullPool[64 / sizeof (void *)]; |
| 126 |
| 127 /* Generic nul-content Null objects. */ |
| 128 template <typename Type> |
| 129 static inline const Type& Null (void) { |
| 130 ASSERT_STATIC (Type::min_size <= sizeof (_NullPool)); |
| 131 return *CastP<Type> (_NullPool); |
| 132 } |
| 133 |
| 134 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */ |
| 135 #define DEFINE_NULL_DATA(Type, data) \ |
| 136 static const char _Null##Type[Type::min_size + 1] = data; /* +1 is for nul-termi
nation in data */ \ |
| 137 template <> \ |
| 138 inline const Type& Null<Type> (void) { \ |
| 139 return *CastP<Type> (_Null##Type); \ |
| 140 } /* The following line really exists such that we end in a place needing semico
lon */ \ |
| 141 ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type)) |
| 142 |
| 143 /* Accessor macro. */ |
| 144 #define Null(Type) Null<Type>() |
| 145 |
| 146 |
| 147 /* |
| 148 * Trace |
| 149 */ |
| 150 |
| 151 |
| 152 template <int max_depth> |
| 153 struct hb_trace_t { |
| 154 explicit hb_trace_t (unsigned int *pdepth, const char *what, const char *funct
ion, const void *obj) : pdepth(pdepth) { |
| 155 (void) (*pdepth < max_depth && |
| 156 fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, *pdepth, *pdepth,
function)); |
| 157 if (max_depth) ++*pdepth; |
| 158 } |
| 159 ~hb_trace_t (void) { if (max_depth) --*pdepth; } |
| 160 |
| 161 private: |
| 162 unsigned int *pdepth; |
| 163 }; |
| 164 template <> /* Optimize when tracing is disabled */ |
| 165 struct hb_trace_t<0> { |
| 166 explicit hb_trace_t (unsigned int *pdepth HB_UNUSED, const char *what HB_UNUSE
D, const char *function HB_UNUSED, const void *obj HB_UNUSED) {} |
| 167 }; |
| 168 |
| 169 |
| 170 |
| 171 /* |
| 172 * Sanitize |
| 173 */ |
| 174 |
| 175 #ifndef HB_DEBUG_SANITIZE |
| 176 #define HB_DEBUG_SANITIZE (HB_DEBUG+0) |
| 177 #endif |
| 178 |
| 179 |
| 180 #define TRACE_SANITIZE() \ |
| 181 hb_trace_t<HB_DEBUG_SANITIZE> trace (&c->debug_depth, "SANITIZE", HB_FUN
C, this); \ |
| 182 |
| 183 |
| 184 struct hb_sanitize_context_t |
| 185 { |
| 186 inline void init (hb_blob_t *blob) |
| 187 { |
| 188 this->blob = hb_blob_reference (blob); |
| 189 this->start = hb_blob_lock (blob); |
| 190 this->end = this->start + hb_blob_get_length (blob); |
| 191 this->writable = hb_blob_is_writable (blob); |
| 192 this->edit_count = 0; |
| 193 this->debug_depth = 0; |
| 194 |
| 195 (void) (HB_DEBUG_SANITIZE && |
| 196 fprintf (stderr, "sanitize %p init [%p..%p] (%lu bytes)\n", |
| 197 this->blob, this->start, this->end, |
| 198 (unsigned long) (this->end - this->start))); |
| 199 } |
| 200 |
| 201 inline void finish (void) |
| 202 { |
| 203 (void) (HB_DEBUG_SANITIZE && |
| 204 fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n", |
| 205 this->blob, this->start, this->end, this->edit_count)); |
| 206 |
| 207 hb_blob_unlock (this->blob); |
| 208 hb_blob_destroy (this->blob); |
| 209 this->blob = NULL; |
| 210 this->start = this->end = NULL; |
| 211 } |
| 212 |
| 213 inline bool check_range (const void *base, unsigned int len) const |
| 214 { |
| 215 const char *p = (const char *) base; |
| 216 bool ret = this->start <= p && |
| 217 p <= this->end && |
| 218 (unsigned int) (this->end - p) >= len; |
| 219 |
| 220 (void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITI
ZE && |
| 221 fprintf (stderr, "SANITIZE(%p) %-*d-> range [%p..%p] (%d bytes) in [%p..%p
] -> %s\n", |
| 222 p, |
| 223 this->debug_depth, this->debug_depth, |
| 224 p, p + len, len, |
| 225 this->start, this->end, |
| 226 ret ? "pass" : "FAIL")); |
| 227 |
| 228 return likely (ret); |
| 229 } |
| 230 |
| 231 inline bool check_array (const void *base, unsigned int record_size, unsigned
int len) const |
| 232 { |
| 233 const char *p = (const char *) base; |
| 234 bool overflows = record_size > 0 && len >= ((unsigned int) -1) / record_size
; |
| 235 |
| 236 (void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITI
ZE && |
| 237 fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in
[%p..%p] -> %s\n", |
| 238 p, |
| 239 this->debug_depth, this->debug_depth, |
| 240 p, p + (record_size * len), record_size, len, (unsigned long) rec
ord_size * len, |
| 241 this->start, this->end, |
| 242 !overflows ? "does not overflow" : "OVERFLOWS FAIL")); |
| 243 |
| 244 return likely (!overflows && this->check_range (base, record_size * len)); |
| 245 } |
| 246 |
| 247 template <typename Type> |
| 248 inline bool check_struct (const Type *obj) const |
| 249 { |
| 250 return likely (this->check_range (obj, obj->min_size)); |
| 251 } |
| 252 |
| 253 inline bool can_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED) |
| 254 { |
| 255 const char *p = (const char *) base; |
| 256 this->edit_count++; |
| 257 |
| 258 (void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITI
ZE && |
| 259 fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p.
.%p] -> %s\n", |
| 260 p, |
| 261 this->debug_depth, this->debug_depth, |
| 262 this->edit_count, |
| 263 p, p + len, len, |
| 264 this->start, this->end, |
| 265 this->writable ? "granted" : "REJECTED")); |
| 266 |
| 267 return this->writable; |
| 268 } |
| 269 |
| 270 unsigned int debug_depth; |
| 271 const char *start, *end; |
| 272 bool writable; |
| 273 unsigned int edit_count; |
| 274 hb_blob_t *blob; |
| 275 }; |
| 276 |
| 277 |
| 278 |
| 279 /* Template to sanitize an object. */ |
| 280 template <typename Type> |
| 281 struct Sanitizer |
| 282 { |
| 283 static hb_blob_t *sanitize (hb_blob_t *blob) { |
| 284 hb_sanitize_context_t c[1] = {{0}}; |
| 285 bool sane; |
| 286 |
| 287 /* TODO is_sane() stuff */ |
| 288 |
| 289 if (!blob) |
| 290 return hb_blob_create_empty (); |
| 291 |
| 292 retry: |
| 293 (void) (HB_DEBUG_SANITIZE && |
| 294 fprintf (stderr, "Sanitizer %p start %s\n", blob, HB_FUNC)); |
| 295 |
| 296 c->init (blob); |
| 297 |
| 298 if (unlikely (!c->start)) { |
| 299 c->finish (); |
| 300 return blob; |
| 301 } |
| 302 |
| 303 Type *t = CastP<Type> (const_cast<char *> (c->start)); |
| 304 |
| 305 sane = t->sanitize (c); |
| 306 if (sane) { |
| 307 if (c->edit_count) { |
| 308 (void) (HB_DEBUG_SANITIZE && |
| 309 fprintf (stderr, "Sanitizer %p passed first round with %d edits; doing
a second round %s\n", |
| 310 blob, c->edit_count, HB_FUNC)); |
| 311 |
| 312 /* sanitize again to ensure no toe-stepping */ |
| 313 c->edit_count = 0; |
| 314 sane = t->sanitize (c); |
| 315 if (c->edit_count) { |
| 316 (void) (HB_DEBUG_SANITIZE && |
| 317 fprintf (stderr, "Sanitizer %p requested %d edits in second round; F
AILLING %s\n", |
| 318 blob, c->edit_count, HB_FUNC)); |
| 319 sane = false; |
| 320 } |
| 321 } |
| 322 c->finish (); |
| 323 } else { |
| 324 unsigned int edit_count = c->edit_count; |
| 325 c->finish (); |
| 326 if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blo
b)) { |
| 327 /* ok, we made it writable by relocating. try again */ |
| 328 (void) (HB_DEBUG_SANITIZE && |
| 329 fprintf (stderr, "Sanitizer %p retry %s\n", blob, HB_FUNC)); |
| 330 goto retry; |
| 331 } |
| 332 } |
| 333 |
| 334 (void) (HB_DEBUG_SANITIZE && |
| 335 fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED",
HB_FUNC)); |
| 336 if (sane) |
| 337 return blob; |
| 338 else { |
| 339 hb_blob_destroy (blob); |
| 340 return hb_blob_create_empty (); |
| 341 } |
| 342 } |
| 343 |
| 344 static const Type* lock_instance (hb_blob_t *blob) { |
| 345 const char *base = hb_blob_lock (blob); |
| 346 return unlikely (!base) ? &Null(Type) : CastP<Type> (base); |
| 347 } |
| 348 }; |
| 349 |
| 350 |
| 351 |
| 352 |
| 353 /* |
| 354 * |
| 355 * The OpenType Font File: Data Types |
| 356 */ |
| 357 |
| 358 |
| 359 /* "The following data types are used in the OpenType font file. |
| 360 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */ |
| 361 |
| 362 /* |
| 363 * Int types |
| 364 */ |
| 365 |
| 366 |
| 367 template <typename Type, int Bytes> class BEInt; |
| 368 |
| 369 /* LONGTERMTODO: On machines allowing unaligned access, we can make the |
| 370 * following tighter by using byteswap instructions on ints directly. */ |
| 371 template <typename Type> |
| 372 class BEInt<Type, 2> |
| 373 { |
| 374 public: |
| 375 inline void set (Type i) { hb_be_uint16_put (v,i); } |
| 376 inline operator Type (void) const { return hb_be_uint16_get (v); } |
| 377 inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_
cmp (v, o.v); } |
| 378 inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o)
; } |
| 379 private: uint8_t v[2]; |
| 380 }; |
| 381 template <typename Type> |
| 382 class BEInt<Type, 4> |
| 383 { |
| 384 public: |
| 385 inline void set (Type i) { hb_be_uint32_put (v,i); } |
| 386 inline operator Type (void) const { return hb_be_uint32_get (v); } |
| 387 inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_
cmp (v, o.v); } |
| 388 inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o)
; } |
| 389 private: uint8_t v[4]; |
| 390 }; |
| 391 |
| 392 /* Integer types in big-endian order and no alignment requirement */ |
| 393 template <typename Type> |
| 394 struct IntType |
| 395 { |
| 396 inline void set (Type i) { v.set (i); } |
| 397 inline operator Type(void) const { return v; } |
| 398 inline bool operator == (const IntType<Type> &o) const { return v == o.v; } |
| 399 inline bool operator != (const IntType<Type> &o) const { return v != o.v; } |
| 400 inline int cmp (Type a) const { Type b = v; return a < b ? -1 : a == b ? 0 : +
1; } |
| 401 inline bool sanitize (hb_sanitize_context_t *c) { |
| 402 TRACE_SANITIZE (); |
| 403 return likely (c->check_struct (this)); |
| 404 } |
| 405 protected: |
| 406 BEInt<Type, sizeof (Type)> v; |
| 407 public: |
| 408 DEFINE_SIZE_STATIC (sizeof (Type)); |
| 409 }; |
| 410 |
| 411 typedef IntType<uint16_t> USHORT; /* 16-bit unsigned integer. */ |
| 412 typedef IntType<int16_t> SHORT; /* 16-bit signed integer. */ |
| 413 typedef IntType<uint32_t> ULONG; /* 32-bit unsigned integer. */ |
| 414 typedef IntType<int32_t> LONG; /* 32-bit signed integer. */ |
| 415 |
| 416 /* Date represented in number of seconds since 12:00 midnight, January 1, |
| 417 * 1904. The value is represented as a signed 64-bit integer. */ |
| 418 struct LONGDATETIME |
| 419 { |
| 420 inline bool sanitize (hb_sanitize_context_t *c) { |
| 421 TRACE_SANITIZE (); |
| 422 return likely (c->check_struct (this)); |
| 423 } |
| 424 private: |
| 425 LONG major; |
| 426 ULONG minor; |
| 427 public: |
| 428 DEFINE_SIZE_STATIC (8); |
| 429 }; |
| 430 |
| 431 /* Array of four uint8s (length = 32 bits) used to identify a script, language |
| 432 * system, feature, or baseline */ |
| 433 struct Tag : ULONG |
| 434 { |
| 435 /* What the char* converters return is NOT nul-terminated. Print using "%.4s"
*/ |
| 436 inline operator const char* (void) const { return reinterpret_cast<const char
*> (&this->v); } |
| 437 inline operator char* (void) { return reinterpret_cast<char *> (&this->v); } |
| 438 public: |
| 439 DEFINE_SIZE_STATIC (4); |
| 440 }; |
| 441 DEFINE_NULL_DATA (Tag, " "); |
| 442 |
| 443 /* Glyph index number, same as uint16 (length = 16 bits) */ |
| 444 typedef USHORT GlyphID; |
| 445 |
| 446 /* Script/language-system/feature index */ |
| 447 struct Index : USHORT { |
| 448 static const unsigned int NOT_FOUND_INDEX = 0xFFFF; |
| 449 }; |
| 450 DEFINE_NULL_DATA (Index, "\xff\xff"); |
| 451 |
| 452 /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */ |
| 453 typedef USHORT Offset; |
| 454 |
| 455 /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x000
00000 */ |
| 456 typedef ULONG LongOffset; |
| 457 |
| 458 |
| 459 /* CheckSum */ |
| 460 struct CheckSum : ULONG |
| 461 { |
| 462 static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length) |
| 463 { |
| 464 uint32_t Sum = 0L; |
| 465 ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size; |
| 466 |
| 467 while (Table < EndPtr) |
| 468 Sum += *Table++; |
| 469 return Sum; |
| 470 } |
| 471 public: |
| 472 DEFINE_SIZE_STATIC (4); |
| 473 }; |
| 474 |
| 475 |
| 476 /* |
| 477 * Version Numbers |
| 478 */ |
| 479 |
| 480 struct FixedVersion |
| 481 { |
| 482 inline operator uint32_t (void) const { return (major << 16) + minor; } |
| 483 |
| 484 inline bool sanitize (hb_sanitize_context_t *c) { |
| 485 TRACE_SANITIZE (); |
| 486 return c->check_struct (this); |
| 487 } |
| 488 |
| 489 USHORT major; |
| 490 USHORT minor; |
| 491 public: |
| 492 DEFINE_SIZE_STATIC (4); |
| 493 }; |
| 494 |
| 495 |
| 496 |
| 497 /* |
| 498 * Template subclasses of Offset and LongOffset that do the dereferencing. |
| 499 * Use: (base+offset) |
| 500 */ |
| 501 |
| 502 template <typename OffsetType, typename Type> |
| 503 struct GenericOffsetTo : OffsetType |
| 504 { |
| 505 inline const Type& operator () (const void *base) const |
| 506 { |
| 507 unsigned int offset = *this; |
| 508 if (unlikely (!offset)) return Null(Type); |
| 509 return StructAtOffset<Type> (base, offset); |
| 510 } |
| 511 |
| 512 inline bool sanitize (hb_sanitize_context_t *c, void *base) { |
| 513 TRACE_SANITIZE (); |
| 514 if (unlikely (!c->check_struct (this))) return false; |
| 515 unsigned int offset = *this; |
| 516 if (unlikely (!offset)) return true; |
| 517 Type &obj = StructAtOffset<Type> (base, offset); |
| 518 return likely (obj.sanitize (c)) || neuter (c); |
| 519 } |
| 520 template <typename T> |
| 521 inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) { |
| 522 TRACE_SANITIZE (); |
| 523 if (unlikely (!c->check_struct (this))) return false; |
| 524 unsigned int offset = *this; |
| 525 if (unlikely (!offset)) return true; |
| 526 Type &obj = StructAtOffset<Type> (base, offset); |
| 527 return likely (obj.sanitize (c, user_data)) || neuter (c); |
| 528 } |
| 529 |
| 530 private: |
| 531 /* Set the offset to Null */ |
| 532 inline bool neuter (hb_sanitize_context_t *c) { |
| 533 if (c->can_edit (this, this->static_size)) { |
| 534 this->set (0); /* 0 is Null offset */ |
| 535 return true; |
| 536 } |
| 537 return false; |
| 538 } |
| 539 }; |
| 540 template <typename Base, typename OffsetType, typename Type> |
| 541 inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Typ
e> offset) { return offset (base); } |
| 542 |
| 543 template <typename Type> |
| 544 struct OffsetTo : GenericOffsetTo<Offset, Type> {}; |
| 545 |
| 546 template <typename Type> |
| 547 struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {}; |
| 548 |
| 549 |
| 550 /* |
| 551 * Array Types |
| 552 */ |
| 553 |
| 554 template <typename LenType, typename Type> |
| 555 struct GenericArrayOf |
| 556 { |
| 557 const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/O
UT */) const |
| 558 { |
| 559 unsigned int count = len; |
| 560 if (unlikely (start_offset > count)) |
| 561 count = 0; |
| 562 else |
| 563 count -= start_offset; |
| 564 count = MIN (count, *pcount); |
| 565 *pcount = count; |
| 566 return array + start_offset; |
| 567 } |
| 568 |
| 569 inline const Type& operator [] (unsigned int i) const |
| 570 { |
| 571 if (unlikely (i >= len)) return Null(Type); |
| 572 return array[i]; |
| 573 } |
| 574 inline unsigned int get_size (void) const |
| 575 { return len.static_size + len * Type::static_size; } |
| 576 |
| 577 inline bool sanitize (hb_sanitize_context_t *c) { |
| 578 TRACE_SANITIZE (); |
| 579 if (unlikely (!sanitize_shallow (c))) return false; |
| 580 |
| 581 /* Note: for structs that do not reference other structs, |
| 582 * we do not need to call their sanitize() as we already did |
| 583 * a bound check on the aggregate array size. We just include |
| 584 * a small unreachable expression to make sure the structs |
| 585 * pointed to do have a simple sanitize(), ie. they do not |
| 586 * reference other structs via offsets. |
| 587 */ |
| 588 (void) (false && array[0].sanitize (c)); |
| 589 |
| 590 return true; |
| 591 } |
| 592 inline bool sanitize (hb_sanitize_context_t *c, void *base) { |
| 593 TRACE_SANITIZE (); |
| 594 if (unlikely (!sanitize_shallow (c))) return false; |
| 595 unsigned int count = len; |
| 596 for (unsigned int i = 0; i < count; i++) |
| 597 if (unlikely (!array[i].sanitize (c, base))) |
| 598 return false; |
| 599 return true; |
| 600 } |
| 601 template <typename T> |
| 602 inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) { |
| 603 TRACE_SANITIZE (); |
| 604 if (unlikely (!sanitize_shallow (c))) return false; |
| 605 unsigned int count = len; |
| 606 for (unsigned int i = 0; i < count; i++) |
| 607 if (unlikely (!array[i].sanitize (c, base, user_data))) |
| 608 return false; |
| 609 return true; |
| 610 } |
| 611 |
| 612 private: |
| 613 inline bool sanitize_shallow (hb_sanitize_context_t *c) { |
| 614 TRACE_SANITIZE (); |
| 615 return c->check_struct (this) |
| 616 && c->check_array (this, Type::static_size, len); |
| 617 } |
| 618 |
| 619 public: |
| 620 LenType len; |
| 621 Type array[VAR]; |
| 622 public: |
| 623 DEFINE_SIZE_ARRAY (sizeof (LenType), array); |
| 624 }; |
| 625 |
| 626 /* An array with a USHORT number of elements. */ |
| 627 template <typename Type> |
| 628 struct ArrayOf : GenericArrayOf<USHORT, Type> {}; |
| 629 |
| 630 /* An array with a ULONG number of elements. */ |
| 631 template <typename Type> |
| 632 struct LongArrayOf : GenericArrayOf<ULONG, Type> {}; |
| 633 |
| 634 /* Array of Offset's */ |
| 635 template <typename Type> |
| 636 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {}; |
| 637 |
| 638 /* Array of LongOffset's */ |
| 639 template <typename Type> |
| 640 struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {}; |
| 641 |
| 642 /* LongArray of LongOffset's */ |
| 643 template <typename Type> |
| 644 struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {}; |
| 645 |
| 646 /* Array of offsets relative to the beginning of the array itself. */ |
| 647 template <typename Type> |
| 648 struct OffsetListOf : OffsetArrayOf<Type> |
| 649 { |
| 650 inline const Type& operator [] (unsigned int i) const |
| 651 { |
| 652 if (unlikely (i >= this->len)) return Null(Type); |
| 653 return this+this->array[i]; |
| 654 } |
| 655 |
| 656 inline bool sanitize (hb_sanitize_context_t *c) { |
| 657 TRACE_SANITIZE (); |
| 658 return OffsetArrayOf<Type>::sanitize (c, this); |
| 659 } |
| 660 template <typename T> |
| 661 inline bool sanitize (hb_sanitize_context_t *c, T user_data) { |
| 662 TRACE_SANITIZE (); |
| 663 return OffsetArrayOf<Type>::sanitize (c, this, user_data); |
| 664 } |
| 665 }; |
| 666 |
| 667 |
| 668 /* An array with a USHORT number of elements, |
| 669 * starting at second element. */ |
| 670 template <typename Type> |
| 671 struct HeadlessArrayOf |
| 672 { |
| 673 inline const Type& operator [] (unsigned int i) const |
| 674 { |
| 675 if (unlikely (i >= len || !i)) return Null(Type); |
| 676 return array[i-1]; |
| 677 } |
| 678 inline unsigned int get_size (void) const |
| 679 { return len.static_size + (len ? len - 1 : 0) * Type::static_size; } |
| 680 |
| 681 inline bool sanitize_shallow (hb_sanitize_context_t *c) { |
| 682 return c->check_struct (this) |
| 683 && c->check_array (this, Type::static_size, len); |
| 684 } |
| 685 |
| 686 inline bool sanitize (hb_sanitize_context_t *c) { |
| 687 TRACE_SANITIZE (); |
| 688 if (unlikely (!sanitize_shallow (c))) return false; |
| 689 |
| 690 /* Note: for structs that do not reference other structs, |
| 691 * we do not need to call their sanitize() as we already did |
| 692 * a bound check on the aggregate array size. We just include |
| 693 * a small unreachable expression to make sure the structs |
| 694 * pointed to do have a simple sanitize(), ie. they do not |
| 695 * reference other structs via offsets. |
| 696 */ |
| 697 (void) (false && array[0].sanitize (c)); |
| 698 |
| 699 return true; |
| 700 } |
| 701 |
| 702 USHORT len; |
| 703 Type array[VAR]; |
| 704 public: |
| 705 DEFINE_SIZE_ARRAY (sizeof (USHORT), array); |
| 706 }; |
| 707 |
| 708 |
| 709 /* An array with sorted elements. Supports binary searching. */ |
| 710 template <typename Type> |
| 711 struct SortedArrayOf : ArrayOf<Type> { |
| 712 |
| 713 template <typename SearchType> |
| 714 inline int search (const SearchType &x) const { |
| 715 class Cmp { |
| 716 public: static int cmp (const SearchType *a, const Type *b) { return b->cm
p (*a); } |
| 717 }; |
| 718 const Type *p = (const Type *) bsearch (&x, this->array, this->len, sizeof (
this->array[0]), (hb_compare_func_t) Cmp::cmp); |
| 719 return p ? p - this->array : -1; |
| 720 } |
| 721 }; |
| 722 |
| 723 |
| 724 HB_BEGIN_DECLS |
| 725 HB_END_DECLS |
| 726 |
| 727 #endif /* HB_OPEN_TYPE_PRIVATE_HH */ |
OLD | NEW |