| 1 | /* |
| 2 | * Copyright © 2007,2008,2009,2010 Red Hat, Inc. |
| 3 | * Copyright © 2012,2018 Google, Inc. |
| 4 | * |
| 5 | * This is part of HarfBuzz, a text shaping library. |
| 6 | * |
| 7 | * Permission is hereby granted, without written agreement and without |
| 8 | * license or royalty fees, to use, copy, modify, and distribute this |
| 9 | * software and its documentation for any purpose, provided that the |
| 10 | * above copyright notice and the following two paragraphs appear in |
| 11 | * all copies of this software. |
| 12 | * |
| 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 14 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 15 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 16 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 17 | * DAMAGE. |
| 18 | * |
| 19 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 20 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 22 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 23 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 24 | * |
| 25 | * Red Hat Author(s): Behdad Esfahbod |
| 26 | * Google Author(s): Behdad Esfahbod |
| 27 | */ |
| 28 | |
| 29 | #ifndef HB_MACHINERY_HH |
| 30 | #define HB_MACHINERY_HH |
| 31 | |
| 32 | #include "hb.hh" |
| 33 | #include "hb-blob.hh" |
| 34 | |
| 35 | #include "hb-iter.hh" |
| 36 | #include "hb-vector.hh" |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | * Casts |
| 41 | */ |
| 42 | |
| 43 | /* Cast to struct T, reference to reference */ |
| 44 | template<typename Type, typename TObject> |
| 45 | static inline const Type& CastR(const TObject &X) |
| 46 | { return reinterpret_cast<const Type&> (X); } |
| 47 | template<typename Type, typename TObject> |
| 48 | static inline Type& CastR(TObject &X) |
| 49 | { return reinterpret_cast<Type&> (X); } |
| 50 | |
| 51 | /* Cast to struct T, pointer to pointer */ |
| 52 | template<typename Type, typename TObject> |
| 53 | static inline const Type* CastP(const TObject *X) |
| 54 | { return reinterpret_cast<const Type*> (X); } |
| 55 | template<typename Type, typename TObject> |
| 56 | static inline Type* CastP(TObject *X) |
| 57 | { return reinterpret_cast<Type*> (X); } |
| 58 | |
| 59 | /* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory |
| 60 | * location pointed to by P plus Ofs bytes. */ |
| 61 | template<typename Type> |
| 62 | static inline const Type& StructAtOffset(const void *P, unsigned int offset) |
| 63 | { return * reinterpret_cast<const Type*> ((const char *) P + offset); } |
| 64 | template<typename Type> |
| 65 | static inline Type& StructAtOffset(void *P, unsigned int offset) |
| 66 | { return * reinterpret_cast<Type*> ((char *) P + offset); } |
| 67 | |
| 68 | /* StructAfter<T>(X) returns the struct T& that is placed after X. |
| 69 | * Works with X of variable size also. X must implement get_size() */ |
| 70 | template<typename Type, typename TObject> |
| 71 | static inline const Type& StructAfter(const TObject &X) |
| 72 | { return StructAtOffset<Type>(&X, X.get_size()); } |
| 73 | template<typename Type, typename TObject> |
| 74 | static inline Type& StructAfter(TObject &X) |
| 75 | { return StructAtOffset<Type>(&X, X.get_size()); } |
| 76 | |
| 77 | |
| 78 | /* |
| 79 | * Size checking |
| 80 | */ |
| 81 | |
| 82 | /* Check _assertion in a method environment */ |
| 83 | #define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \ |
| 84 | inline void _instance_assertion_on_line_##_line (void) const \ |
| 85 | { \ |
| 86 | static_assert ((_assertion), ""); \ |
| 87 | ASSERT_INSTANCE_POD (*this); /* Make sure it's POD. */ \ |
| 88 | } |
| 89 | # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion) |
| 90 | # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion) |
| 91 | |
| 92 | /* Check that _code compiles in a method environment */ |
| 93 | #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \ |
| 94 | inline void _compiles_assertion_on_line_##_line (void) const \ |
| 95 | { _code; } |
| 96 | # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code) |
| 97 | # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code) |
| 98 | |
| 99 | |
| 100 | #define DEFINE_SIZE_STATIC(size) \ |
| 101 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)); \ |
| 102 | enum { static_size = (size) }; \ |
| 103 | enum { min_size = (size) }; \ |
| 104 | inline unsigned int get_size (void) const { return (size); } |
| 105 | |
| 106 | #define DEFINE_SIZE_UNION(size, _member) \ |
| 107 | DEFINE_INSTANCE_ASSERTION (0*sizeof(this->u._member.static_size) + sizeof(this->u._member) == (size)); \ |
| 108 | static const unsigned int min_size = (size) |
| 109 | |
| 110 | #define DEFINE_SIZE_MIN(size) \ |
| 111 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)); \ |
| 112 | static const unsigned int min_size = (size) |
| 113 | |
| 114 | #define DEFINE_SIZE_ARRAY(size, array) \ |
| 115 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + VAR * sizeof (array[0])); \ |
| 116 | DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \ |
| 117 | enum { min_size = (size) }; \ |
| 118 | |
| 119 | #define DEFINE_SIZE_ARRAY_SIZED(size, array) \ |
| 120 | DEFINE_SIZE_ARRAY(size, array); \ |
| 121 | inline unsigned int get_size (void) const { return (size - array[0].min_size + array.get_size ()); } |
| 122 | |
| 123 | #define DEFINE_SIZE_ARRAY2(size, array1, array2) \ |
| 124 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \ |
| 125 | DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].static_size) \ |
| 126 | static const unsigned int min_size = (size) |
| 127 | |
| 128 | |
| 129 | /* |
| 130 | * Dispatch |
| 131 | */ |
| 132 | |
| 133 | template <typename Context, typename Return, unsigned int MaxDebugDepth> |
| 134 | struct hb_dispatch_context_t |
| 135 | { |
| 136 | enum { max_debug_depth = MaxDebugDepth }; |
| 137 | typedef Return return_t; |
| 138 | template <typename T, typename F> |
| 139 | inline bool may_dispatch (const T *obj, const F *format) { return true; } |
| 140 | static return_t no_dispatch_return_value (void) { return Context::default_return_value (); } |
| 141 | }; |
| 142 | |
| 143 | |
| 144 | /* |
| 145 | * Sanitize |
| 146 | * |
| 147 | * |
| 148 | * === Introduction === |
| 149 | * |
| 150 | * The sanitize machinery is at the core of our zero-cost font loading. We |
| 151 | * mmap() font file into memory and create a blob out of it. Font subtables |
| 152 | * are returned as a readonly sub-blob of the main font blob. These table |
| 153 | * blobs are then sanitized before use, to ensure invalid memory access does |
| 154 | * not happen. The toplevel sanitize API use is like, eg. to load the 'head' |
| 155 | * table: |
| 156 | * |
| 157 | * hb_blob_t *head_blob = hb_sanitize_context_t ().reference_table<OT::head> (face); |
| 158 | * |
| 159 | * The blob then can be converted to a head table struct with: |
| 160 | * |
| 161 | * const head *head_table = head_blob->as<head> (); |
| 162 | * |
| 163 | * What the reference_table does is, to call hb_face_reference_table() to load |
| 164 | * the table blob, sanitize it and return either the sanitized blob, or empty |
| 165 | * blob if sanitization failed. The blob->as() function returns the null |
| 166 | * object of its template type argument if the blob is empty. Otherwise, it |
| 167 | * just casts the blob contents to the desired type. |
| 168 | * |
| 169 | * Sanitizing a blob of data with a type T works as follows (with minor |
| 170 | * simplification): |
| 171 | * |
| 172 | * - Cast blob content to T*, call sanitize() method of it, |
| 173 | * - If sanitize succeeded, return blob. |
| 174 | * - Otherwise, if blob is not writable, try making it writable, |
| 175 | * or copy if cannot be made writable in-place, |
| 176 | * - Call sanitize() again. Return blob if sanitize succeeded. |
| 177 | * - Return empty blob otherwise. |
| 178 | * |
| 179 | * |
| 180 | * === The sanitize() contract === |
| 181 | * |
| 182 | * The sanitize() method of each object type shall return true if it's safe to |
| 183 | * call other methods of the object, and false otherwise. |
| 184 | * |
| 185 | * Note that what sanitize() checks for might align with what the specification |
| 186 | * describes as valid table data, but does not have to be. In particular, we |
| 187 | * do NOT want to be pedantic and concern ourselves with validity checks that |
| 188 | * are irrelevant to our use of the table. On the contrary, we want to be |
| 189 | * lenient with error handling and accept invalid data to the extent that it |
| 190 | * does not impose extra burden on us. |
| 191 | * |
| 192 | * Based on the sanitize contract, one can see that what we check for depends |
| 193 | * on how we use the data in other table methods. Ie. if other table methods |
| 194 | * assume that offsets do NOT point out of the table data block, then that's |
| 195 | * something sanitize() must check for (GSUB/GPOS/GDEF/etc work this way). On |
| 196 | * the other hand, if other methods do such checks themselves, then sanitize() |
| 197 | * does not have to bother with them (glyf/local work this way). The choice |
| 198 | * depends on the table structure and sanitize() performance. For example, to |
| 199 | * check glyf/loca offsets in sanitize() would cost O(num-glyphs). We try hard |
| 200 | * to avoid such costs during font loading. By postponing such checks to the |
| 201 | * actual glyph loading, we reduce the sanitize cost to O(1) and total runtime |
| 202 | * cost to O(used-glyphs). As such, this is preferred. |
| 203 | * |
| 204 | * The same argument can be made re GSUB/GPOS/GDEF, but there, the table |
| 205 | * structure is so complicated that by checking all offsets at sanitize() time, |
| 206 | * we make the code much simpler in other methods, as offsets and referenced |
| 207 | * objectes do not need to be validated at each use site. |
| 208 | */ |
| 209 | |
| 210 | /* This limits sanitizing time on really broken fonts. */ |
| 211 | #ifndef HB_SANITIZE_MAX_EDITS |
| 212 | #define HB_SANITIZE_MAX_EDITS 32 |
| 213 | #endif |
| 214 | #ifndef HB_SANITIZE_MAX_OPS_FACTOR |
| 215 | #define HB_SANITIZE_MAX_OPS_FACTOR 8 |
| 216 | #endif |
| 217 | #ifndef HB_SANITIZE_MAX_OPS_MIN |
| 218 | #define HB_SANITIZE_MAX_OPS_MIN 16384 |
| 219 | #endif |
| 220 | |
| 221 | struct hb_sanitize_context_t : |
| 222 | hb_dispatch_context_t<hb_sanitize_context_t, bool, HB_DEBUG_SANITIZE> |
| 223 | { |
| 224 | inline hb_sanitize_context_t (void) : |
| 225 | debug_depth (0), |
| 226 | start (nullptr), end (nullptr), |
| 227 | writable (false), edit_count (0), max_ops (0), |
| 228 | blob (nullptr), |
| 229 | num_glyphs (65536), |
| 230 | num_glyphs_set (false) {} |
| 231 | |
| 232 | inline const char *get_name (void) { return "SANITIZE" ; } |
| 233 | template <typename T, typename F> |
| 234 | inline bool may_dispatch (const T *obj, const F *format) |
| 235 | { return format->sanitize (this); } |
| 236 | template <typename T> |
| 237 | inline return_t dispatch (const T &obj) { return obj.sanitize (this); } |
| 238 | static return_t default_return_value (void) { return true; } |
| 239 | static return_t no_dispatch_return_value (void) { return false; } |
| 240 | bool stop_sublookup_iteration (const return_t r) const { return !r; } |
| 241 | |
| 242 | inline void init (hb_blob_t *b) |
| 243 | { |
| 244 | this->blob = hb_blob_reference (b); |
| 245 | this->writable = false; |
| 246 | } |
| 247 | |
| 248 | inline void set_num_glyphs (unsigned int num_glyphs_) |
| 249 | { |
| 250 | num_glyphs = num_glyphs_; |
| 251 | num_glyphs_set = true; |
| 252 | } |
| 253 | inline unsigned int get_num_glyphs (void) { return num_glyphs; } |
| 254 | |
| 255 | inline void start_processing (void) |
| 256 | { |
| 257 | this->start = this->blob->data; |
| 258 | this->end = this->start + this->blob->length; |
| 259 | assert (this->start <= this->end); /* Must not overflow. */ |
| 260 | this->max_ops = MAX ((unsigned int) (this->end - this->start) * HB_SANITIZE_MAX_OPS_FACTOR, |
| 261 | (unsigned) HB_SANITIZE_MAX_OPS_MIN); |
| 262 | this->edit_count = 0; |
| 263 | this->debug_depth = 0; |
| 264 | |
| 265 | DEBUG_MSG_LEVEL (SANITIZE, start, 0, +1, |
| 266 | "start [%p..%p] (%lu bytes)" , |
| 267 | this->start, this->end, |
| 268 | (unsigned long) (this->end - this->start)); |
| 269 | } |
| 270 | |
| 271 | inline void end_processing (void) |
| 272 | { |
| 273 | DEBUG_MSG_LEVEL (SANITIZE, this->start, 0, -1, |
| 274 | "end [%p..%p] %u edit requests" , |
| 275 | this->start, this->end, this->edit_count); |
| 276 | |
| 277 | hb_blob_destroy (this->blob); |
| 278 | this->blob = nullptr; |
| 279 | this->start = this->end = nullptr; |
| 280 | } |
| 281 | |
| 282 | inline bool check_range (const void *base, unsigned int len) const |
| 283 | { |
| 284 | const char *p = (const char *) base; |
| 285 | bool ok = this->max_ops-- > 0 && |
| 286 | this->start <= p && |
| 287 | p <= this->end && |
| 288 | (unsigned int) (this->end - p) >= len; |
| 289 | |
| 290 | DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0, |
| 291 | "check_range [%p..%p] (%d bytes) in [%p..%p] -> %s" , |
| 292 | p, p + len, len, |
| 293 | this->start, this->end, |
| 294 | ok ? "OK" : "OUT-OF-RANGE" ); |
| 295 | |
| 296 | return likely (ok); |
| 297 | } |
| 298 | |
| 299 | inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const |
| 300 | { |
| 301 | const char *p = (const char *) base; |
| 302 | bool overflows = hb_unsigned_mul_overflows (len, record_size); |
| 303 | unsigned int array_size = record_size * len; |
| 304 | bool ok = !overflows && this->check_range (base, array_size); |
| 305 | |
| 306 | DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0, |
| 307 | "check_array [%p..%p] (%d*%d=%d bytes) in [%p..%p] -> %s" , |
| 308 | p, p + (record_size * len), record_size, len, (unsigned int) array_size, |
| 309 | this->start, this->end, |
| 310 | overflows ? "OVERFLOWS" : ok ? "OK" : "OUT-OF-RANGE" ); |
| 311 | |
| 312 | return likely (ok); |
| 313 | } |
| 314 | |
| 315 | template <typename Type> |
| 316 | inline bool check_struct (const Type *obj) const |
| 317 | { |
| 318 | return likely (this->check_range (obj, obj->min_size)); |
| 319 | } |
| 320 | |
| 321 | inline bool may_edit (const void *base, unsigned int len) |
| 322 | { |
| 323 | if (this->edit_count >= HB_SANITIZE_MAX_EDITS) |
| 324 | return false; |
| 325 | |
| 326 | const char *p = (const char *) base; |
| 327 | this->edit_count++; |
| 328 | |
| 329 | DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0, |
| 330 | "may_edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s" , |
| 331 | this->edit_count, |
| 332 | p, p + len, len, |
| 333 | this->start, this->end, |
| 334 | this->writable ? "GRANTED" : "DENIED" ); |
| 335 | |
| 336 | return this->writable; |
| 337 | } |
| 338 | |
| 339 | template <typename Type, typename ValueType> |
| 340 | inline bool try_set (const Type *obj, const ValueType &v) { |
| 341 | if (this->may_edit (obj, obj->static_size)) { |
| 342 | const_cast<Type *> (obj)->set (v); |
| 343 | return true; |
| 344 | } |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | template <typename Type> |
| 349 | inline hb_blob_t *sanitize_blob (hb_blob_t *blob) |
| 350 | { |
| 351 | bool sane; |
| 352 | |
| 353 | init (blob); |
| 354 | |
| 355 | retry: |
| 356 | DEBUG_MSG_FUNC (SANITIZE, start, "start" ); |
| 357 | |
| 358 | start_processing (); |
| 359 | |
| 360 | if (unlikely (!start)) |
| 361 | { |
| 362 | end_processing (); |
| 363 | return blob; |
| 364 | } |
| 365 | |
| 366 | Type *t = CastP<Type> (const_cast<char *> (start)); |
| 367 | |
| 368 | sane = t->sanitize (this); |
| 369 | if (sane) |
| 370 | { |
| 371 | if (edit_count) |
| 372 | { |
| 373 | DEBUG_MSG_FUNC (SANITIZE, start, "passed first round with %d edits; going for second round" , edit_count); |
| 374 | |
| 375 | /* sanitize again to ensure no toe-stepping */ |
| 376 | edit_count = 0; |
| 377 | sane = t->sanitize (this); |
| 378 | if (edit_count) { |
| 379 | DEBUG_MSG_FUNC (SANITIZE, start, "requested %d edits in second round; FAILLING" , edit_count); |
| 380 | sane = false; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | if (edit_count && !writable) { |
| 387 | start = hb_blob_get_data_writable (blob, nullptr); |
| 388 | end = start + blob->length; |
| 389 | |
| 390 | if (start) |
| 391 | { |
| 392 | writable = true; |
| 393 | /* ok, we made it writable by relocating. try again */ |
| 394 | DEBUG_MSG_FUNC (SANITIZE, start, "retry" ); |
| 395 | goto retry; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | end_processing (); |
| 401 | |
| 402 | DEBUG_MSG_FUNC (SANITIZE, start, sane ? "PASSED" : "FAILED" ); |
| 403 | if (sane) |
| 404 | { |
| 405 | hb_blob_make_immutable (blob); |
| 406 | return blob; |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | hb_blob_destroy (blob); |
| 411 | return hb_blob_get_empty (); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | template <typename Type> |
| 416 | inline hb_blob_t *reference_table (const hb_face_t *face, hb_tag_t tableTag = Type::tableTag) |
| 417 | { |
| 418 | if (!num_glyphs_set) |
| 419 | set_num_glyphs (hb_face_get_glyph_count (face)); |
| 420 | return sanitize_blob<Type> (hb_face_reference_table (face, tableTag)); |
| 421 | } |
| 422 | |
| 423 | mutable unsigned int debug_depth; |
| 424 | const char *start, *end; |
| 425 | private: |
| 426 | bool writable; |
| 427 | unsigned int edit_count; |
| 428 | mutable int max_ops; |
| 429 | hb_blob_t *blob; |
| 430 | unsigned int num_glyphs; |
| 431 | bool num_glyphs_set; |
| 432 | }; |
| 433 | |
| 434 | |
| 435 | /* |
| 436 | * Serialize |
| 437 | */ |
| 438 | |
| 439 | struct hb_serialize_context_t |
| 440 | { |
| 441 | inline hb_serialize_context_t (void *start_, unsigned int size) |
| 442 | { |
| 443 | this->start = (char *) start_; |
| 444 | this->end = this->start + size; |
| 445 | reset (); |
| 446 | } |
| 447 | |
| 448 | inline void reset (void) |
| 449 | { |
| 450 | this->ran_out_of_room = false; |
| 451 | this->head = this->start; |
| 452 | this->debug_depth = 0; |
| 453 | } |
| 454 | |
| 455 | inline bool err (bool e) { return this->ran_out_of_room = this->ran_out_of_room || e; } |
| 456 | |
| 457 | /* To be called around main operation. */ |
| 458 | template <typename Type> |
| 459 | inline Type *start_serialize (void) |
| 460 | { |
| 461 | DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1, |
| 462 | "start [%p..%p] (%lu bytes)" , |
| 463 | this->start, this->end, |
| 464 | (unsigned long) (this->end - this->start)); |
| 465 | |
| 466 | return start_embed<Type> (); |
| 467 | } |
| 468 | inline void end_serialize (void) |
| 469 | { |
| 470 | DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1, |
| 471 | "end [%p..%p] serialized %d bytes; %s" , |
| 472 | this->start, this->end, |
| 473 | (int) (this->head - this->start), |
| 474 | this->ran_out_of_room ? "RAN OUT OF ROOM" : "did not ran out of room" ); |
| 475 | } |
| 476 | |
| 477 | inline unsigned int length (void) const { return this->head - this->start; } |
| 478 | |
| 479 | inline void align (unsigned int alignment) |
| 480 | { |
| 481 | unsigned int l = length () % alignment; |
| 482 | if (l) |
| 483 | allocate_size<void> (alignment - l); |
| 484 | } |
| 485 | |
| 486 | template <typename Type> |
| 487 | inline Type *start_embed (void) const |
| 488 | { |
| 489 | Type *ret = reinterpret_cast<Type *> (this->head); |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | template <typename Type> |
| 494 | inline Type *allocate_size (unsigned int size) |
| 495 | { |
| 496 | if (unlikely (this->ran_out_of_room || this->end - this->head < ptrdiff_t (size))) { |
| 497 | this->ran_out_of_room = true; |
| 498 | return nullptr; |
| 499 | } |
| 500 | memset (this->head, 0, size); |
| 501 | char *ret = this->head; |
| 502 | this->head += size; |
| 503 | return reinterpret_cast<Type *> (ret); |
| 504 | } |
| 505 | |
| 506 | template <typename Type> |
| 507 | inline Type *allocate_min (void) |
| 508 | { |
| 509 | return this->allocate_size<Type> (Type::min_size); |
| 510 | } |
| 511 | |
| 512 | template <typename Type> |
| 513 | inline Type *embed (const Type &obj) |
| 514 | { |
| 515 | unsigned int size = obj.get_size (); |
| 516 | Type *ret = this->allocate_size<Type> (size); |
| 517 | if (unlikely (!ret)) return nullptr; |
| 518 | memcpy (ret, &obj, size); |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | template <typename Type> |
| 523 | inline Type *extend_min (Type &obj) |
| 524 | { |
| 525 | unsigned int size = obj.min_size; |
| 526 | assert (this->start <= (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head); |
| 527 | if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return nullptr; |
| 528 | return reinterpret_cast<Type *> (&obj); |
| 529 | } |
| 530 | |
| 531 | template <typename Type> |
| 532 | inline Type *extend (Type &obj) |
| 533 | { |
| 534 | unsigned int size = obj.get_size (); |
| 535 | assert (this->start < (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head); |
| 536 | if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return nullptr; |
| 537 | return reinterpret_cast<Type *> (&obj); |
| 538 | } |
| 539 | |
| 540 | /* Output routines. */ |
| 541 | template <typename Type> |
| 542 | inline Type *copy (void) const |
| 543 | { |
| 544 | assert (!this->ran_out_of_room); |
| 545 | unsigned int len = this->head - this->start; |
| 546 | void *p = malloc (len); |
| 547 | if (p) |
| 548 | memcpy (p, this->start, len); |
| 549 | return reinterpret_cast<Type *> (p); |
| 550 | } |
| 551 | inline hb_bytes_t copy_bytes (void) const |
| 552 | { |
| 553 | assert (!this->ran_out_of_room); |
| 554 | unsigned int len = this->head - this->start; |
| 555 | void *p = malloc (len); |
| 556 | if (p) |
| 557 | memcpy (p, this->start, len); |
| 558 | else |
| 559 | return hb_bytes_t (); |
| 560 | return hb_bytes_t (p, len); |
| 561 | } |
| 562 | inline hb_blob_t *copy_blob (void) const |
| 563 | { |
| 564 | assert (!this->ran_out_of_room); |
| 565 | return hb_blob_create (this->start, |
| 566 | this->head - this->start, |
| 567 | HB_MEMORY_MODE_DUPLICATE, |
| 568 | nullptr, nullptr); |
| 569 | } |
| 570 | |
| 571 | public: |
| 572 | unsigned int debug_depth; |
| 573 | char *start, *end, *head; |
| 574 | bool ran_out_of_room; |
| 575 | }; |
| 576 | |
| 577 | |
| 578 | /* |
| 579 | * Supplier |
| 580 | */ |
| 581 | |
| 582 | template <typename Type> |
| 583 | struct Supplier |
| 584 | { |
| 585 | inline Supplier (const Type *array, unsigned int len_, unsigned int stride_=sizeof (Type)) |
| 586 | { |
| 587 | head = array; |
| 588 | len = len_; |
| 589 | stride = stride_; |
| 590 | } |
| 591 | inline Supplier (const hb_vector_t<Type> *v) |
| 592 | { |
| 593 | head = v->arrayZ; |
| 594 | len = v->len; |
| 595 | stride = sizeof (Type); |
| 596 | } |
| 597 | |
| 598 | inline const Type operator [] (unsigned int i) const |
| 599 | { |
| 600 | if (unlikely (i >= len)) return Type (); |
| 601 | return * (const Type *) (const void *) ((const char *) head + stride * i); |
| 602 | } |
| 603 | |
| 604 | inline Supplier<Type> & operator += (unsigned int count) |
| 605 | { |
| 606 | if (unlikely (count > len)) |
| 607 | count = len; |
| 608 | len -= count; |
| 609 | head = (const Type *) (const void *) ((const char *) head + stride * count); |
| 610 | return *this; |
| 611 | } |
| 612 | |
| 613 | private: |
| 614 | inline Supplier (const Supplier<Type> &); /* Disallow copy */ |
| 615 | inline Supplier<Type>& operator= (const Supplier<Type> &); /* Disallow copy */ |
| 616 | |
| 617 | unsigned int len; |
| 618 | unsigned int stride; |
| 619 | const Type *head; |
| 620 | }; |
| 621 | |
| 622 | |
| 623 | /* |
| 624 | * Big-endian integers. |
| 625 | */ |
| 626 | |
| 627 | template <typename Type, int Bytes> struct BEInt; |
| 628 | |
| 629 | template <typename Type> |
| 630 | struct BEInt<Type, 1> |
| 631 | { |
| 632 | public: |
| 633 | inline void set (Type V) |
| 634 | { |
| 635 | v = V; |
| 636 | } |
| 637 | inline operator Type (void) const |
| 638 | { |
| 639 | return v; |
| 640 | } |
| 641 | private: uint8_t v; |
| 642 | }; |
| 643 | template <typename Type> |
| 644 | struct BEInt<Type, 2> |
| 645 | { |
| 646 | public: |
| 647 | inline void set (Type V) |
| 648 | { |
| 649 | v[0] = (V >> 8) & 0xFF; |
| 650 | v[1] = (V ) & 0xFF; |
| 651 | } |
| 652 | inline operator Type (void) const |
| 653 | { |
| 654 | return (v[0] << 8) |
| 655 | + (v[1] ); |
| 656 | } |
| 657 | private: uint8_t v[2]; |
| 658 | }; |
| 659 | template <typename Type> |
| 660 | struct BEInt<Type, 3> |
| 661 | { |
| 662 | public: |
| 663 | inline void set (Type V) |
| 664 | { |
| 665 | v[0] = (V >> 16) & 0xFF; |
| 666 | v[1] = (V >> 8) & 0xFF; |
| 667 | v[2] = (V ) & 0xFF; |
| 668 | } |
| 669 | inline operator Type (void) const |
| 670 | { |
| 671 | return (v[0] << 16) |
| 672 | + (v[1] << 8) |
| 673 | + (v[2] ); |
| 674 | } |
| 675 | private: uint8_t v[3]; |
| 676 | }; |
| 677 | template <typename Type> |
| 678 | struct BEInt<Type, 4> |
| 679 | { |
| 680 | public: |
| 681 | inline void set (Type V) |
| 682 | { |
| 683 | v[0] = (V >> 24) & 0xFF; |
| 684 | v[1] = (V >> 16) & 0xFF; |
| 685 | v[2] = (V >> 8) & 0xFF; |
| 686 | v[3] = (V ) & 0xFF; |
| 687 | } |
| 688 | inline operator Type (void) const |
| 689 | { |
| 690 | return (v[0] << 24) |
| 691 | + (v[1] << 16) |
| 692 | + (v[2] << 8) |
| 693 | + (v[3] ); |
| 694 | } |
| 695 | private: uint8_t v[4]; |
| 696 | }; |
| 697 | |
| 698 | |
| 699 | /* |
| 700 | * Lazy loaders. |
| 701 | */ |
| 702 | |
| 703 | template <typename Data, unsigned int WheresData> |
| 704 | struct hb_data_wrapper_t |
| 705 | { |
| 706 | static_assert (WheresData > 0, "" ); |
| 707 | |
| 708 | inline Data * get_data (void) const |
| 709 | { |
| 710 | return *(((Data **) (void *) this) - WheresData); |
| 711 | } |
| 712 | |
| 713 | template <typename Stored, typename Subclass> |
| 714 | inline Stored * call_create (void) const |
| 715 | { |
| 716 | Data *data = this->get_data (); |
| 717 | return likely (data) ? Subclass::create (data) : nullptr; |
| 718 | } |
| 719 | }; |
| 720 | template <> |
| 721 | struct hb_data_wrapper_t<void, 0> |
| 722 | { |
| 723 | template <typename Stored, typename Funcs> |
| 724 | inline Stored * call_create (void) const |
| 725 | { |
| 726 | return Funcs::create (); |
| 727 | } |
| 728 | }; |
| 729 | |
| 730 | template <typename T1, typename T2> struct hb_non_void_t { typedef T1 value; }; |
| 731 | template <typename T2> struct hb_non_void_t<void, T2> { typedef T2 value; }; |
| 732 | |
| 733 | template <typename Returned, |
| 734 | typename Subclass = void, |
| 735 | typename Data = void, |
| 736 | unsigned int WheresData = 0, |
| 737 | typename Stored = Returned> |
| 738 | struct hb_lazy_loader_t : hb_data_wrapper_t<Data, WheresData> |
| 739 | { |
| 740 | typedef typename hb_non_void_t<Subclass, |
| 741 | hb_lazy_loader_t<Returned,Subclass,Data,WheresData,Stored> |
| 742 | >::value Funcs; |
| 743 | |
| 744 | inline void init0 (void) {} /* Init, when memory is already set to 0. No-op for us. */ |
| 745 | inline void init (void) { instance.set_relaxed (nullptr); } |
| 746 | inline void fini (void) |
| 747 | { |
| 748 | do_destroy (instance.get ()); |
| 749 | } |
| 750 | inline void free_instance (void) |
| 751 | { |
| 752 | retry: |
| 753 | Stored *p = instance.get (); |
| 754 | if (unlikely (p && !this->instance.cmpexch (p, nullptr))) |
| 755 | goto retry; |
| 756 | do_destroy (p); |
| 757 | } |
| 758 | |
| 759 | inline Stored * do_create (void) const |
| 760 | { |
| 761 | Stored *p = this->template call_create<Stored, Funcs> (); |
| 762 | if (unlikely (!p)) |
| 763 | p = const_cast<Stored *> (Funcs::get_null ()); |
| 764 | return p; |
| 765 | } |
| 766 | static inline void do_destroy (Stored *p) |
| 767 | { |
| 768 | if (p && p != Funcs::get_null ()) |
| 769 | Funcs::destroy (p); |
| 770 | } |
| 771 | |
| 772 | inline const Returned * operator -> (void) const { return get (); } |
| 773 | inline const Returned & operator * (void) const { return *get (); } |
| 774 | |
| 775 | inline Data * get_data (void) const |
| 776 | { |
| 777 | return *(((Data **) this) - WheresData); |
| 778 | } |
| 779 | |
| 780 | inline Stored * get_stored (void) const |
| 781 | { |
| 782 | retry: |
| 783 | Stored *p = this->instance.get (); |
| 784 | if (unlikely (!p)) |
| 785 | { |
| 786 | p = do_create (); |
| 787 | if (unlikely (!this->instance.cmpexch (nullptr, p))) |
| 788 | { |
| 789 | do_destroy (p); |
| 790 | goto retry; |
| 791 | } |
| 792 | } |
| 793 | return p; |
| 794 | } |
| 795 | inline Stored * get_stored_relaxed (void) const |
| 796 | { |
| 797 | return this->instance.get_relaxed (); |
| 798 | } |
| 799 | |
| 800 | inline void set_stored (Stored *instance_) |
| 801 | { |
| 802 | /* This *must* be called when there are no other threads accessing. |
| 803 | * However, to make TSan, etc, happy, we using cmpexch. */ |
| 804 | retry: |
| 805 | Stored *p = this->instance.get (); |
| 806 | if (unlikely (!this->instance.cmpexch (p, instance_))) |
| 807 | goto retry; |
| 808 | do_destroy (p); |
| 809 | } |
| 810 | |
| 811 | inline const Returned * get (void) const { return Funcs::convert (get_stored ()); } |
| 812 | inline const Returned * get_relaxed (void) const { return Funcs::convert (get_stored_relaxed ()); } |
| 813 | inline Returned * get_unconst (void) const { return const_cast<Returned *> (Funcs::convert (get_stored ())); } |
| 814 | |
| 815 | /* To be possibly overloaded by subclasses. */ |
| 816 | static inline Returned* convert (Stored *p) { return p; } |
| 817 | |
| 818 | /* By default null/init/fini the object. */ |
| 819 | static inline const Stored* get_null (void) { return &Null(Stored); } |
| 820 | static inline Stored *create (Data *data) |
| 821 | { |
| 822 | Stored *p = (Stored *) calloc (1, sizeof (Stored)); |
| 823 | if (likely (p)) |
| 824 | p->init (data); |
| 825 | return p; |
| 826 | } |
| 827 | static inline Stored *create (void) |
| 828 | { |
| 829 | Stored *p = (Stored *) calloc (1, sizeof (Stored)); |
| 830 | if (likely (p)) |
| 831 | p->init (); |
| 832 | return p; |
| 833 | } |
| 834 | static inline void destroy (Stored *p) |
| 835 | { |
| 836 | p->fini (); |
| 837 | free (p); |
| 838 | } |
| 839 | |
| 840 | private: |
| 841 | /* Must only have one pointer. */ |
| 842 | hb_atomic_ptr_t<Stored *> instance; |
| 843 | }; |
| 844 | |
| 845 | /* Specializations. */ |
| 846 | |
| 847 | template <typename T, unsigned int WheresFace> |
| 848 | struct hb_face_lazy_loader_t : hb_lazy_loader_t<T, |
| 849 | hb_face_lazy_loader_t<T, WheresFace>, |
| 850 | hb_face_t, WheresFace> {}; |
| 851 | |
| 852 | template <typename T, unsigned int WheresFace> |
| 853 | struct hb_table_lazy_loader_t : hb_lazy_loader_t<T, |
| 854 | hb_table_lazy_loader_t<T, WheresFace>, |
| 855 | hb_face_t, WheresFace, |
| 856 | hb_blob_t> |
| 857 | { |
| 858 | static inline hb_blob_t *create (hb_face_t *face) |
| 859 | { |
| 860 | return hb_sanitize_context_t ().reference_table<T> (face); |
| 861 | } |
| 862 | static inline void destroy (hb_blob_t *p) |
| 863 | { |
| 864 | hb_blob_destroy (p); |
| 865 | } |
| 866 | static inline const hb_blob_t *get_null (void) |
| 867 | { |
| 868 | return hb_blob_get_empty (); |
| 869 | } |
| 870 | static inline const T* convert (const hb_blob_t *blob) |
| 871 | { |
| 872 | return blob->as<T> (); |
| 873 | } |
| 874 | |
| 875 | inline hb_blob_t* get_blob (void) const |
| 876 | { |
| 877 | return this->get_stored (); |
| 878 | } |
| 879 | }; |
| 880 | |
| 881 | template <typename Subclass> |
| 882 | struct hb_font_funcs_lazy_loader_t : hb_lazy_loader_t<hb_font_funcs_t, Subclass> |
| 883 | { |
| 884 | static inline void destroy (hb_font_funcs_t *p) |
| 885 | { |
| 886 | hb_font_funcs_destroy (p); |
| 887 | } |
| 888 | static inline const hb_font_funcs_t *get_null (void) |
| 889 | { |
| 890 | return hb_font_funcs_get_empty (); |
| 891 | } |
| 892 | }; |
| 893 | template <typename Subclass> |
| 894 | struct hb_unicode_funcs_lazy_loader_t : hb_lazy_loader_t<hb_unicode_funcs_t, Subclass> |
| 895 | { |
| 896 | static inline void destroy (hb_unicode_funcs_t *p) |
| 897 | { |
| 898 | hb_unicode_funcs_destroy (p); |
| 899 | } |
| 900 | static inline const hb_unicode_funcs_t *get_null (void) |
| 901 | { |
| 902 | return hb_unicode_funcs_get_empty (); |
| 903 | } |
| 904 | }; |
| 905 | |
| 906 | |
| 907 | #endif /* HB_MACHINERY_HH */ |
| 908 | |