Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2007,2008,2009,2010 Red Hat, Inc. |
michael@0 | 3 | * Copyright © 2012 Google, Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * This is part of HarfBuzz, a text shaping library. |
michael@0 | 6 | * |
michael@0 | 7 | * Permission is hereby granted, without written agreement and without |
michael@0 | 8 | * license or royalty fees, to use, copy, modify, and distribute this |
michael@0 | 9 | * software and its documentation for any purpose, provided that the |
michael@0 | 10 | * above copyright notice and the following two paragraphs appear in |
michael@0 | 11 | * all copies of this software. |
michael@0 | 12 | * |
michael@0 | 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
michael@0 | 14 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
michael@0 | 15 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
michael@0 | 16 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
michael@0 | 17 | * DAMAGE. |
michael@0 | 18 | * |
michael@0 | 19 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
michael@0 | 20 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
michael@0 | 21 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
michael@0 | 22 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
michael@0 | 23 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
michael@0 | 24 | * |
michael@0 | 25 | * Red Hat Author(s): Behdad Esfahbod |
michael@0 | 26 | * Google Author(s): Behdad Esfahbod |
michael@0 | 27 | */ |
michael@0 | 28 | |
michael@0 | 29 | #ifndef HB_OPEN_TYPE_PRIVATE_HH |
michael@0 | 30 | #define HB_OPEN_TYPE_PRIVATE_HH |
michael@0 | 31 | |
michael@0 | 32 | #include "hb-private.hh" |
michael@0 | 33 | |
michael@0 | 34 | |
michael@0 | 35 | namespace OT { |
michael@0 | 36 | |
michael@0 | 37 | |
michael@0 | 38 | |
michael@0 | 39 | /* |
michael@0 | 40 | * Casts |
michael@0 | 41 | */ |
michael@0 | 42 | |
michael@0 | 43 | /* Cast to struct T, reference to reference */ |
michael@0 | 44 | template<typename Type, typename TObject> |
michael@0 | 45 | inline const Type& CastR(const TObject &X) |
michael@0 | 46 | { return reinterpret_cast<const Type&> (X); } |
michael@0 | 47 | template<typename Type, typename TObject> |
michael@0 | 48 | inline Type& CastR(TObject &X) |
michael@0 | 49 | { return reinterpret_cast<Type&> (X); } |
michael@0 | 50 | |
michael@0 | 51 | /* Cast to struct T, pointer to pointer */ |
michael@0 | 52 | template<typename Type, typename TObject> |
michael@0 | 53 | inline const Type* CastP(const TObject *X) |
michael@0 | 54 | { return reinterpret_cast<const Type*> (X); } |
michael@0 | 55 | template<typename Type, typename TObject> |
michael@0 | 56 | inline Type* CastP(TObject *X) |
michael@0 | 57 | { return reinterpret_cast<Type*> (X); } |
michael@0 | 58 | |
michael@0 | 59 | /* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory |
michael@0 | 60 | * location pointed to by P plus Ofs bytes. */ |
michael@0 | 61 | template<typename Type> |
michael@0 | 62 | inline const Type& StructAtOffset(const void *P, unsigned int offset) |
michael@0 | 63 | { return * reinterpret_cast<const Type*> ((const char *) P + offset); } |
michael@0 | 64 | template<typename Type> |
michael@0 | 65 | inline Type& StructAtOffset(void *P, unsigned int offset) |
michael@0 | 66 | { return * reinterpret_cast<Type*> ((char *) P + offset); } |
michael@0 | 67 | |
michael@0 | 68 | /* StructAfter<T>(X) returns the struct T& that is placed after X. |
michael@0 | 69 | * Works with X of variable size also. X must implement get_size() */ |
michael@0 | 70 | template<typename Type, typename TObject> |
michael@0 | 71 | inline const Type& StructAfter(const TObject &X) |
michael@0 | 72 | { return StructAtOffset<Type>(&X, X.get_size()); } |
michael@0 | 73 | template<typename Type, typename TObject> |
michael@0 | 74 | inline Type& StructAfter(TObject &X) |
michael@0 | 75 | { return StructAtOffset<Type>(&X, X.get_size()); } |
michael@0 | 76 | |
michael@0 | 77 | |
michael@0 | 78 | |
michael@0 | 79 | /* |
michael@0 | 80 | * Size checking |
michael@0 | 81 | */ |
michael@0 | 82 | |
michael@0 | 83 | /* Check _assertion in a method environment */ |
michael@0 | 84 | #define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \ |
michael@0 | 85 | inline void _instance_assertion_on_line_##_line (void) const \ |
michael@0 | 86 | { \ |
michael@0 | 87 | ASSERT_STATIC (_assertion); \ |
michael@0 | 88 | ASSERT_INSTANCE_POD (*this); /* Make sure it's POD. */ \ |
michael@0 | 89 | } |
michael@0 | 90 | # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion) |
michael@0 | 91 | # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion) |
michael@0 | 92 | |
michael@0 | 93 | /* Check that _code compiles in a method environment */ |
michael@0 | 94 | #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \ |
michael@0 | 95 | inline void _compiles_assertion_on_line_##_line (void) const \ |
michael@0 | 96 | { _code; } |
michael@0 | 97 | # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code) |
michael@0 | 98 | # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code) |
michael@0 | 99 | |
michael@0 | 100 | |
michael@0 | 101 | #define DEFINE_SIZE_STATIC(size) \ |
michael@0 | 102 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)); \ |
michael@0 | 103 | static const unsigned int static_size = (size); \ |
michael@0 | 104 | static const unsigned int min_size = (size) |
michael@0 | 105 | |
michael@0 | 106 | /* Size signifying variable-sized array */ |
michael@0 | 107 | #define VAR 1 |
michael@0 | 108 | |
michael@0 | 109 | #define DEFINE_SIZE_UNION(size, _member) \ |
michael@0 | 110 | DEFINE_INSTANCE_ASSERTION (this->u._member.static_size == (size)); \ |
michael@0 | 111 | static const unsigned int min_size = (size) |
michael@0 | 112 | |
michael@0 | 113 | #define DEFINE_SIZE_MIN(size) \ |
michael@0 | 114 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)); \ |
michael@0 | 115 | static const unsigned int min_size = (size) |
michael@0 | 116 | |
michael@0 | 117 | #define DEFINE_SIZE_ARRAY(size, array) \ |
michael@0 | 118 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \ |
michael@0 | 119 | DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \ |
michael@0 | 120 | static const unsigned int min_size = (size) |
michael@0 | 121 | |
michael@0 | 122 | #define DEFINE_SIZE_ARRAY2(size, array1, array2) \ |
michael@0 | 123 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \ |
michael@0 | 124 | DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].static_size) \ |
michael@0 | 125 | static const unsigned int min_size = (size) |
michael@0 | 126 | |
michael@0 | 127 | |
michael@0 | 128 | |
michael@0 | 129 | /* |
michael@0 | 130 | * Null objects |
michael@0 | 131 | */ |
michael@0 | 132 | |
michael@0 | 133 | /* Global nul-content Null pool. Enlarge as necessary. */ |
michael@0 | 134 | /* TODO This really should be a extern HB_INTERNAL and defined somewhere... */ |
michael@0 | 135 | static const void *_NullPool[64 / sizeof (void *)]; |
michael@0 | 136 | |
michael@0 | 137 | /* Generic nul-content Null objects. */ |
michael@0 | 138 | template <typename Type> |
michael@0 | 139 | static inline const Type& Null (void) { |
michael@0 | 140 | ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool)); |
michael@0 | 141 | return *CastP<Type> (_NullPool); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */ |
michael@0 | 145 | #define DEFINE_NULL_DATA(Type, data) \ |
michael@0 | 146 | static const char _Null##Type[sizeof (Type) + 1] = data; /* +1 is for nul-termination in data */ \ |
michael@0 | 147 | template <> \ |
michael@0 | 148 | inline const Type& Null<Type> (void) { \ |
michael@0 | 149 | return *CastP<Type> (_Null##Type); \ |
michael@0 | 150 | } /* The following line really exists such that we end in a place needing semicolon */ \ |
michael@0 | 151 | ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type)) |
michael@0 | 152 | |
michael@0 | 153 | /* Accessor macro. */ |
michael@0 | 154 | #define Null(Type) Null<Type>() |
michael@0 | 155 | |
michael@0 | 156 | |
michael@0 | 157 | |
michael@0 | 158 | /* |
michael@0 | 159 | * Sanitize |
michael@0 | 160 | */ |
michael@0 | 161 | |
michael@0 | 162 | #ifndef HB_DEBUG_SANITIZE |
michael@0 | 163 | #define HB_DEBUG_SANITIZE (HB_DEBUG+0) |
michael@0 | 164 | #endif |
michael@0 | 165 | |
michael@0 | 166 | |
michael@0 | 167 | #define TRACE_SANITIZE(this) \ |
michael@0 | 168 | hb_auto_trace_t<HB_DEBUG_SANITIZE, bool> trace \ |
michael@0 | 169 | (&c->debug_depth, c->get_name (), this, HB_FUNC, \ |
michael@0 | 170 | ""); |
michael@0 | 171 | |
michael@0 | 172 | /* This limits sanitizing time on really broken fonts. */ |
michael@0 | 173 | #ifndef HB_SANITIZE_MAX_EDITS |
michael@0 | 174 | #define HB_SANITIZE_MAX_EDITS 100 |
michael@0 | 175 | #endif |
michael@0 | 176 | |
michael@0 | 177 | struct hb_sanitize_context_t |
michael@0 | 178 | { |
michael@0 | 179 | inline const char *get_name (void) { return "SANITIZE"; } |
michael@0 | 180 | static const unsigned int max_debug_depth = HB_DEBUG_SANITIZE; |
michael@0 | 181 | typedef bool return_t; |
michael@0 | 182 | template <typename T> |
michael@0 | 183 | inline return_t dispatch (const T &obj) { return obj.sanitize (this); } |
michael@0 | 184 | static return_t default_return_value (void) { return true; } |
michael@0 | 185 | bool stop_sublookup_iteration (const return_t r HB_UNUSED) const { return false; } |
michael@0 | 186 | |
michael@0 | 187 | inline void init (hb_blob_t *b) |
michael@0 | 188 | { |
michael@0 | 189 | this->blob = hb_blob_reference (b); |
michael@0 | 190 | this->writable = false; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | inline void start_processing (void) |
michael@0 | 194 | { |
michael@0 | 195 | this->start = hb_blob_get_data (this->blob, NULL); |
michael@0 | 196 | this->end = this->start + hb_blob_get_length (this->blob); |
michael@0 | 197 | this->edit_count = 0; |
michael@0 | 198 | this->debug_depth = 0; |
michael@0 | 199 | |
michael@0 | 200 | DEBUG_MSG_LEVEL (SANITIZE, this->blob, 0, +1, |
michael@0 | 201 | "start [%p..%p] (%lu bytes)", |
michael@0 | 202 | this->start, this->end, |
michael@0 | 203 | (unsigned long) (this->end - this->start)); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | inline void end_processing (void) |
michael@0 | 207 | { |
michael@0 | 208 | DEBUG_MSG_LEVEL (SANITIZE, this->blob, 0, -1, |
michael@0 | 209 | "end [%p..%p] %u edit requests", |
michael@0 | 210 | this->start, this->end, this->edit_count); |
michael@0 | 211 | |
michael@0 | 212 | hb_blob_destroy (this->blob); |
michael@0 | 213 | this->blob = NULL; |
michael@0 | 214 | this->start = this->end = NULL; |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | inline bool check_range (const void *base, unsigned int len) const |
michael@0 | 218 | { |
michael@0 | 219 | const char *p = (const char *) base; |
michael@0 | 220 | |
michael@0 | 221 | hb_auto_trace_t<HB_DEBUG_SANITIZE, bool> trace |
michael@0 | 222 | (&this->debug_depth, "SANITIZE", this->blob, NULL, |
michael@0 | 223 | "check_range [%p..%p] (%d bytes) in [%p..%p]", |
michael@0 | 224 | p, p + len, len, |
michael@0 | 225 | this->start, this->end); |
michael@0 | 226 | |
michael@0 | 227 | return TRACE_RETURN (likely (this->start <= p && p <= this->end && (unsigned int) (this->end - p) >= len)); |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const |
michael@0 | 231 | { |
michael@0 | 232 | const char *p = (const char *) base; |
michael@0 | 233 | bool overflows = _hb_unsigned_int_mul_overflows (len, record_size); |
michael@0 | 234 | |
michael@0 | 235 | hb_auto_trace_t<HB_DEBUG_SANITIZE, bool> trace |
michael@0 | 236 | (&this->debug_depth, "SANITIZE", this->blob, NULL, |
michael@0 | 237 | "check_array [%p..%p] (%d*%d=%ld bytes) in [%p..%p]", |
michael@0 | 238 | p, p + (record_size * len), record_size, len, (unsigned long) record_size * len, |
michael@0 | 239 | this->start, this->end); |
michael@0 | 240 | |
michael@0 | 241 | return TRACE_RETURN (likely (!overflows && this->check_range (base, record_size * len))); |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | template <typename Type> |
michael@0 | 245 | inline bool check_struct (const Type *obj) const |
michael@0 | 246 | { |
michael@0 | 247 | return likely (this->check_range (obj, obj->min_size)); |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | inline bool may_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED) |
michael@0 | 251 | { |
michael@0 | 252 | if (this->edit_count >= HB_SANITIZE_MAX_EDITS) |
michael@0 | 253 | return false; |
michael@0 | 254 | |
michael@0 | 255 | const char *p = (const char *) base; |
michael@0 | 256 | this->edit_count++; |
michael@0 | 257 | |
michael@0 | 258 | hb_auto_trace_t<HB_DEBUG_SANITIZE, bool> trace |
michael@0 | 259 | (&this->debug_depth, "SANITIZE", this->blob, NULL, |
michael@0 | 260 | "may_edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s", |
michael@0 | 261 | this->edit_count, |
michael@0 | 262 | p, p + len, len, |
michael@0 | 263 | this->start, this->end, |
michael@0 | 264 | this->writable ? "GRANTED" : "DENIED"); |
michael@0 | 265 | |
michael@0 | 266 | return TRACE_RETURN (this->writable); |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | mutable unsigned int debug_depth; |
michael@0 | 270 | const char *start, *end; |
michael@0 | 271 | bool writable; |
michael@0 | 272 | unsigned int edit_count; |
michael@0 | 273 | hb_blob_t *blob; |
michael@0 | 274 | }; |
michael@0 | 275 | |
michael@0 | 276 | |
michael@0 | 277 | |
michael@0 | 278 | /* Template to sanitize an object. */ |
michael@0 | 279 | template <typename Type> |
michael@0 | 280 | struct Sanitizer |
michael@0 | 281 | { |
michael@0 | 282 | static hb_blob_t *sanitize (hb_blob_t *blob) { |
michael@0 | 283 | hb_sanitize_context_t c[1] = {{0}}; |
michael@0 | 284 | bool sane; |
michael@0 | 285 | |
michael@0 | 286 | /* TODO is_sane() stuff */ |
michael@0 | 287 | |
michael@0 | 288 | c->init (blob); |
michael@0 | 289 | |
michael@0 | 290 | retry: |
michael@0 | 291 | DEBUG_MSG_FUNC (SANITIZE, blob, "start"); |
michael@0 | 292 | |
michael@0 | 293 | c->start_processing (); |
michael@0 | 294 | |
michael@0 | 295 | if (unlikely (!c->start)) { |
michael@0 | 296 | c->end_processing (); |
michael@0 | 297 | return blob; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | Type *t = CastP<Type> (const_cast<char *> (c->start)); |
michael@0 | 301 | |
michael@0 | 302 | sane = t->sanitize (c); |
michael@0 | 303 | if (sane) { |
michael@0 | 304 | if (c->edit_count) { |
michael@0 | 305 | DEBUG_MSG_FUNC (SANITIZE, blob, "passed first round with %d edits; going for second round", c->edit_count); |
michael@0 | 306 | |
michael@0 | 307 | /* sanitize again to ensure no toe-stepping */ |
michael@0 | 308 | c->edit_count = 0; |
michael@0 | 309 | sane = t->sanitize (c); |
michael@0 | 310 | if (c->edit_count) { |
michael@0 | 311 | DEBUG_MSG_FUNC (SANITIZE, blob, "requested %d edits in second round; FAILLING", c->edit_count); |
michael@0 | 312 | sane = false; |
michael@0 | 313 | } |
michael@0 | 314 | } |
michael@0 | 315 | } else { |
michael@0 | 316 | unsigned int edit_count = c->edit_count; |
michael@0 | 317 | if (edit_count && !c->writable) { |
michael@0 | 318 | c->start = hb_blob_get_data_writable (blob, NULL); |
michael@0 | 319 | c->end = c->start + hb_blob_get_length (blob); |
michael@0 | 320 | |
michael@0 | 321 | if (c->start) { |
michael@0 | 322 | c->writable = true; |
michael@0 | 323 | /* ok, we made it writable by relocating. try again */ |
michael@0 | 324 | DEBUG_MSG_FUNC (SANITIZE, blob, "retry"); |
michael@0 | 325 | goto retry; |
michael@0 | 326 | } |
michael@0 | 327 | } |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | c->end_processing (); |
michael@0 | 331 | |
michael@0 | 332 | DEBUG_MSG_FUNC (SANITIZE, blob, sane ? "PASSED" : "FAILED"); |
michael@0 | 333 | if (sane) |
michael@0 | 334 | return blob; |
michael@0 | 335 | else { |
michael@0 | 336 | hb_blob_destroy (blob); |
michael@0 | 337 | return hb_blob_get_empty (); |
michael@0 | 338 | } |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | static const Type* lock_instance (hb_blob_t *blob) { |
michael@0 | 342 | hb_blob_make_immutable (blob); |
michael@0 | 343 | const char *base = hb_blob_get_data (blob, NULL); |
michael@0 | 344 | return unlikely (!base) ? &Null(Type) : CastP<Type> (base); |
michael@0 | 345 | } |
michael@0 | 346 | }; |
michael@0 | 347 | |
michael@0 | 348 | |
michael@0 | 349 | |
michael@0 | 350 | /* |
michael@0 | 351 | * Serialize |
michael@0 | 352 | */ |
michael@0 | 353 | |
michael@0 | 354 | #ifndef HB_DEBUG_SERIALIZE |
michael@0 | 355 | #define HB_DEBUG_SERIALIZE (HB_DEBUG+0) |
michael@0 | 356 | #endif |
michael@0 | 357 | |
michael@0 | 358 | |
michael@0 | 359 | #define TRACE_SERIALIZE(this) \ |
michael@0 | 360 | hb_auto_trace_t<HB_DEBUG_SERIALIZE, bool> trace \ |
michael@0 | 361 | (&c->debug_depth, "SERIALIZE", c, HB_FUNC, \ |
michael@0 | 362 | ""); |
michael@0 | 363 | |
michael@0 | 364 | |
michael@0 | 365 | struct hb_serialize_context_t |
michael@0 | 366 | { |
michael@0 | 367 | inline hb_serialize_context_t (void *start, unsigned int size) |
michael@0 | 368 | { |
michael@0 | 369 | this->start = (char *) start; |
michael@0 | 370 | this->end = this->start + size; |
michael@0 | 371 | |
michael@0 | 372 | this->ran_out_of_room = false; |
michael@0 | 373 | this->head = this->start; |
michael@0 | 374 | this->debug_depth = 0; |
michael@0 | 375 | } |
michael@0 | 376 | |
michael@0 | 377 | template <typename Type> |
michael@0 | 378 | inline Type *start_serialize (void) |
michael@0 | 379 | { |
michael@0 | 380 | DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1, |
michael@0 | 381 | "start [%p..%p] (%lu bytes)", |
michael@0 | 382 | this->start, this->end, |
michael@0 | 383 | (unsigned long) (this->end - this->start)); |
michael@0 | 384 | |
michael@0 | 385 | return start_embed<Type> (); |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | inline void end_serialize (void) |
michael@0 | 389 | { |
michael@0 | 390 | DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1, |
michael@0 | 391 | "end [%p..%p] serialized %d bytes; %s", |
michael@0 | 392 | this->start, this->end, |
michael@0 | 393 | (int) (this->head - this->start), |
michael@0 | 394 | this->ran_out_of_room ? "RAN OUT OF ROOM" : "did not ran out of room"); |
michael@0 | 395 | |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | template <typename Type> |
michael@0 | 399 | inline Type *copy (void) |
michael@0 | 400 | { |
michael@0 | 401 | assert (!this->ran_out_of_room); |
michael@0 | 402 | unsigned int len = this->head - this->start; |
michael@0 | 403 | void *p = malloc (len); |
michael@0 | 404 | if (p) |
michael@0 | 405 | memcpy (p, this->start, len); |
michael@0 | 406 | return reinterpret_cast<Type *> (p); |
michael@0 | 407 | } |
michael@0 | 408 | |
michael@0 | 409 | template <typename Type> |
michael@0 | 410 | inline Type *allocate_size (unsigned int size) |
michael@0 | 411 | { |
michael@0 | 412 | if (unlikely (this->ran_out_of_room || this->end - this->head < ptrdiff_t (size))) { |
michael@0 | 413 | this->ran_out_of_room = true; |
michael@0 | 414 | return NULL; |
michael@0 | 415 | } |
michael@0 | 416 | memset (this->head, 0, size); |
michael@0 | 417 | char *ret = this->head; |
michael@0 | 418 | this->head += size; |
michael@0 | 419 | return reinterpret_cast<Type *> (ret); |
michael@0 | 420 | } |
michael@0 | 421 | |
michael@0 | 422 | template <typename Type> |
michael@0 | 423 | inline Type *allocate_min (void) |
michael@0 | 424 | { |
michael@0 | 425 | return this->allocate_size<Type> (Type::min_size); |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | template <typename Type> |
michael@0 | 429 | inline Type *start_embed (void) |
michael@0 | 430 | { |
michael@0 | 431 | Type *ret = reinterpret_cast<Type *> (this->head); |
michael@0 | 432 | return ret; |
michael@0 | 433 | } |
michael@0 | 434 | |
michael@0 | 435 | template <typename Type> |
michael@0 | 436 | inline Type *embed (const Type &obj) |
michael@0 | 437 | { |
michael@0 | 438 | unsigned int size = obj.get_size (); |
michael@0 | 439 | Type *ret = this->allocate_size<Type> (size); |
michael@0 | 440 | if (unlikely (!ret)) return NULL; |
michael@0 | 441 | memcpy (ret, obj, size); |
michael@0 | 442 | return ret; |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | template <typename Type> |
michael@0 | 446 | inline Type *extend_min (Type &obj) |
michael@0 | 447 | { |
michael@0 | 448 | unsigned int size = obj.min_size; |
michael@0 | 449 | assert (this->start <= (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head); |
michael@0 | 450 | if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return NULL; |
michael@0 | 451 | return reinterpret_cast<Type *> (&obj); |
michael@0 | 452 | } |
michael@0 | 453 | |
michael@0 | 454 | template <typename Type> |
michael@0 | 455 | inline Type *extend (Type &obj) |
michael@0 | 456 | { |
michael@0 | 457 | unsigned int size = obj.get_size (); |
michael@0 | 458 | assert (this->start < (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head); |
michael@0 | 459 | if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return NULL; |
michael@0 | 460 | return reinterpret_cast<Type *> (&obj); |
michael@0 | 461 | } |
michael@0 | 462 | |
michael@0 | 463 | inline void truncate (void *head) |
michael@0 | 464 | { |
michael@0 | 465 | assert (this->start < head && head <= this->head); |
michael@0 | 466 | this->head = (char *) head; |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | unsigned int debug_depth; |
michael@0 | 470 | char *start, *end, *head; |
michael@0 | 471 | bool ran_out_of_room; |
michael@0 | 472 | }; |
michael@0 | 473 | |
michael@0 | 474 | template <typename Type> |
michael@0 | 475 | struct Supplier |
michael@0 | 476 | { |
michael@0 | 477 | inline Supplier (const Type *array, unsigned int len_) |
michael@0 | 478 | { |
michael@0 | 479 | head = array; |
michael@0 | 480 | len = len_; |
michael@0 | 481 | } |
michael@0 | 482 | inline const Type operator [] (unsigned int i) const |
michael@0 | 483 | { |
michael@0 | 484 | if (unlikely (i >= len)) return Type (); |
michael@0 | 485 | return head[i]; |
michael@0 | 486 | } |
michael@0 | 487 | |
michael@0 | 488 | inline void advance (unsigned int count) |
michael@0 | 489 | { |
michael@0 | 490 | if (unlikely (count > len)) |
michael@0 | 491 | count = len; |
michael@0 | 492 | len -= count; |
michael@0 | 493 | head += count; |
michael@0 | 494 | } |
michael@0 | 495 | |
michael@0 | 496 | private: |
michael@0 | 497 | inline Supplier (const Supplier<Type> &); /* Disallow copy */ |
michael@0 | 498 | inline Supplier<Type>& operator= (const Supplier<Type> &); /* Disallow copy */ |
michael@0 | 499 | |
michael@0 | 500 | unsigned int len; |
michael@0 | 501 | const Type *head; |
michael@0 | 502 | }; |
michael@0 | 503 | |
michael@0 | 504 | |
michael@0 | 505 | |
michael@0 | 506 | |
michael@0 | 507 | /* |
michael@0 | 508 | * |
michael@0 | 509 | * The OpenType Font File: Data Types |
michael@0 | 510 | */ |
michael@0 | 511 | |
michael@0 | 512 | |
michael@0 | 513 | /* "The following data types are used in the OpenType font file. |
michael@0 | 514 | * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */ |
michael@0 | 515 | |
michael@0 | 516 | /* |
michael@0 | 517 | * Int types |
michael@0 | 518 | */ |
michael@0 | 519 | |
michael@0 | 520 | |
michael@0 | 521 | template <typename Type, int Bytes> struct BEInt; |
michael@0 | 522 | |
michael@0 | 523 | template <typename Type> |
michael@0 | 524 | struct BEInt<Type, 2> |
michael@0 | 525 | { |
michael@0 | 526 | public: |
michael@0 | 527 | inline void set (Type i) { hb_be_uint16_put (v,i); } |
michael@0 | 528 | inline operator Type (void) const { return hb_be_uint16_get (v); } |
michael@0 | 529 | inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_eq (v, o.v); } |
michael@0 | 530 | inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); } |
michael@0 | 531 | private: uint8_t v[2]; |
michael@0 | 532 | }; |
michael@0 | 533 | template <typename Type> |
michael@0 | 534 | struct BEInt<Type, 4> |
michael@0 | 535 | { |
michael@0 | 536 | public: |
michael@0 | 537 | inline void set (Type i) { hb_be_uint32_put (v,i); } |
michael@0 | 538 | inline operator Type (void) const { return hb_be_uint32_get (v); } |
michael@0 | 539 | inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_eq (v, o.v); } |
michael@0 | 540 | inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); } |
michael@0 | 541 | private: uint8_t v[4]; |
michael@0 | 542 | }; |
michael@0 | 543 | template <typename Type> |
michael@0 | 544 | struct BEInt<Type, 3> |
michael@0 | 545 | { |
michael@0 | 546 | public: |
michael@0 | 547 | inline void set (Type i) { hb_be_uint24_put (v,i); } |
michael@0 | 548 | inline operator Type (void) const { return hb_be_uint24_get (v); } |
michael@0 | 549 | inline bool operator == (const BEInt<Type, 3>& o) const { return hb_be_uint24_eq (v, o.v); } |
michael@0 | 550 | inline bool operator != (const BEInt<Type, 3>& o) const { return !(*this == o); } |
michael@0 | 551 | private: uint8_t v[3]; |
michael@0 | 552 | }; |
michael@0 | 553 | |
michael@0 | 554 | /* Integer types in big-endian order and no alignment requirement */ |
michael@0 | 555 | template <typename Type, unsigned int Size> |
michael@0 | 556 | struct IntType |
michael@0 | 557 | { |
michael@0 | 558 | inline void set (Type i) { v.set (i); } |
michael@0 | 559 | inline operator Type(void) const { return v; } |
michael@0 | 560 | inline bool operator == (const IntType<Type,Size> &o) const { return v == o.v; } |
michael@0 | 561 | inline bool operator != (const IntType<Type,Size> &o) const { return v != o.v; } |
michael@0 | 562 | static inline int cmp (const IntType<Type,Size> *a, const IntType<Type,Size> *b) { return b->cmp (*a); } |
michael@0 | 563 | inline int cmp (IntType<Type,Size> va) const { Type a = va; Type b = v; return a < b ? -1 : a == b ? 0 : +1; } |
michael@0 | 564 | inline int cmp (Type a) const { Type b = v; return a < b ? -1 : a == b ? 0 : +1; } |
michael@0 | 565 | inline bool sanitize (hb_sanitize_context_t *c) { |
michael@0 | 566 | TRACE_SANITIZE (this); |
michael@0 | 567 | return TRACE_RETURN (likely (c->check_struct (this))); |
michael@0 | 568 | } |
michael@0 | 569 | protected: |
michael@0 | 570 | BEInt<Type, Size> v; |
michael@0 | 571 | public: |
michael@0 | 572 | DEFINE_SIZE_STATIC (Size); |
michael@0 | 573 | }; |
michael@0 | 574 | |
michael@0 | 575 | typedef IntType<uint16_t, 2> USHORT; /* 16-bit unsigned integer. */ |
michael@0 | 576 | typedef IntType<int16_t, 2> SHORT; /* 16-bit signed integer. */ |
michael@0 | 577 | typedef IntType<uint32_t, 4> ULONG; /* 32-bit unsigned integer. */ |
michael@0 | 578 | typedef IntType<int32_t, 4> LONG; /* 32-bit signed integer. */ |
michael@0 | 579 | typedef IntType<uint32_t, 3> UINT24; /* 24-bit unsigned integer. */ |
michael@0 | 580 | |
michael@0 | 581 | /* 16-bit signed integer (SHORT) that describes a quantity in FUnits. */ |
michael@0 | 582 | typedef SHORT FWORD; |
michael@0 | 583 | |
michael@0 | 584 | /* 16-bit unsigned integer (USHORT) that describes a quantity in FUnits. */ |
michael@0 | 585 | typedef USHORT UFWORD; |
michael@0 | 586 | |
michael@0 | 587 | /* Date represented in number of seconds since 12:00 midnight, January 1, |
michael@0 | 588 | * 1904. The value is represented as a signed 64-bit integer. */ |
michael@0 | 589 | struct LONGDATETIME |
michael@0 | 590 | { |
michael@0 | 591 | inline bool sanitize (hb_sanitize_context_t *c) { |
michael@0 | 592 | TRACE_SANITIZE (this); |
michael@0 | 593 | return TRACE_RETURN (likely (c->check_struct (this))); |
michael@0 | 594 | } |
michael@0 | 595 | protected: |
michael@0 | 596 | LONG major; |
michael@0 | 597 | ULONG minor; |
michael@0 | 598 | public: |
michael@0 | 599 | DEFINE_SIZE_STATIC (8); |
michael@0 | 600 | }; |
michael@0 | 601 | |
michael@0 | 602 | /* Array of four uint8s (length = 32 bits) used to identify a script, language |
michael@0 | 603 | * system, feature, or baseline */ |
michael@0 | 604 | struct Tag : ULONG |
michael@0 | 605 | { |
michael@0 | 606 | /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */ |
michael@0 | 607 | inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); } |
michael@0 | 608 | inline operator char* (void) { return reinterpret_cast<char *> (&this->v); } |
michael@0 | 609 | public: |
michael@0 | 610 | DEFINE_SIZE_STATIC (4); |
michael@0 | 611 | }; |
michael@0 | 612 | DEFINE_NULL_DATA (Tag, " "); |
michael@0 | 613 | |
michael@0 | 614 | /* Glyph index number, same as uint16 (length = 16 bits) */ |
michael@0 | 615 | typedef USHORT GlyphID; |
michael@0 | 616 | |
michael@0 | 617 | /* Script/language-system/feature index */ |
michael@0 | 618 | struct Index : USHORT { |
michael@0 | 619 | static const unsigned int NOT_FOUND_INDEX = 0xFFFF; |
michael@0 | 620 | }; |
michael@0 | 621 | DEFINE_NULL_DATA (Index, "\xff\xff"); |
michael@0 | 622 | |
michael@0 | 623 | /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */ |
michael@0 | 624 | struct Offset : USHORT |
michael@0 | 625 | { |
michael@0 | 626 | inline bool is_null (void) const { return 0 == *this; } |
michael@0 | 627 | public: |
michael@0 | 628 | DEFINE_SIZE_STATIC (2); |
michael@0 | 629 | }; |
michael@0 | 630 | |
michael@0 | 631 | /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */ |
michael@0 | 632 | struct LongOffset : ULONG |
michael@0 | 633 | { |
michael@0 | 634 | inline bool is_null (void) const { return 0 == *this; } |
michael@0 | 635 | public: |
michael@0 | 636 | DEFINE_SIZE_STATIC (4); |
michael@0 | 637 | }; |
michael@0 | 638 | |
michael@0 | 639 | |
michael@0 | 640 | /* CheckSum */ |
michael@0 | 641 | struct CheckSum : ULONG |
michael@0 | 642 | { |
michael@0 | 643 | /* This is reference implementation from the spec. */ |
michael@0 | 644 | static inline uint32_t CalcTableChecksum (const ULONG *Table, uint32_t Length) |
michael@0 | 645 | { |
michael@0 | 646 | uint32_t Sum = 0L; |
michael@0 | 647 | const ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size; |
michael@0 | 648 | |
michael@0 | 649 | while (Table < EndPtr) |
michael@0 | 650 | Sum += *Table++; |
michael@0 | 651 | return Sum; |
michael@0 | 652 | } |
michael@0 | 653 | |
michael@0 | 654 | /* Note: data should be 4byte aligned and have 4byte padding at the end. */ |
michael@0 | 655 | inline void set_for_data (const void *data, unsigned int length) |
michael@0 | 656 | { set (CalcTableChecksum ((const ULONG *) data, length)); } |
michael@0 | 657 | |
michael@0 | 658 | public: |
michael@0 | 659 | DEFINE_SIZE_STATIC (4); |
michael@0 | 660 | }; |
michael@0 | 661 | |
michael@0 | 662 | |
michael@0 | 663 | /* |
michael@0 | 664 | * Version Numbers |
michael@0 | 665 | */ |
michael@0 | 666 | |
michael@0 | 667 | struct FixedVersion |
michael@0 | 668 | { |
michael@0 | 669 | inline uint32_t to_int (void) const { return (major << 16) + minor; } |
michael@0 | 670 | |
michael@0 | 671 | inline bool sanitize (hb_sanitize_context_t *c) { |
michael@0 | 672 | TRACE_SANITIZE (this); |
michael@0 | 673 | return TRACE_RETURN (c->check_struct (this)); |
michael@0 | 674 | } |
michael@0 | 675 | |
michael@0 | 676 | USHORT major; |
michael@0 | 677 | USHORT minor; |
michael@0 | 678 | public: |
michael@0 | 679 | DEFINE_SIZE_STATIC (4); |
michael@0 | 680 | }; |
michael@0 | 681 | |
michael@0 | 682 | |
michael@0 | 683 | |
michael@0 | 684 | /* |
michael@0 | 685 | * Template subclasses of Offset and LongOffset that do the dereferencing. |
michael@0 | 686 | * Use: (base+offset) |
michael@0 | 687 | */ |
michael@0 | 688 | |
michael@0 | 689 | template <typename OffsetType, typename Type> |
michael@0 | 690 | struct GenericOffsetTo : OffsetType |
michael@0 | 691 | { |
michael@0 | 692 | inline const Type& operator () (const void *base) const |
michael@0 | 693 | { |
michael@0 | 694 | unsigned int offset = *this; |
michael@0 | 695 | if (unlikely (!offset)) return Null(Type); |
michael@0 | 696 | return StructAtOffset<Type> (base, offset); |
michael@0 | 697 | } |
michael@0 | 698 | |
michael@0 | 699 | inline Type& serialize (hb_serialize_context_t *c, void *base) |
michael@0 | 700 | { |
michael@0 | 701 | Type *t = c->start_embed<Type> (); |
michael@0 | 702 | this->set ((char *) t - (char *) base); /* TODO(serialize) Overflow? */ |
michael@0 | 703 | return *t; |
michael@0 | 704 | } |
michael@0 | 705 | |
michael@0 | 706 | inline bool sanitize (hb_sanitize_context_t *c, void *base) { |
michael@0 | 707 | TRACE_SANITIZE (this); |
michael@0 | 708 | if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false); |
michael@0 | 709 | unsigned int offset = *this; |
michael@0 | 710 | if (unlikely (!offset)) return TRACE_RETURN (true); |
michael@0 | 711 | Type &obj = StructAtOffset<Type> (base, offset); |
michael@0 | 712 | return TRACE_RETURN (likely (obj.sanitize (c)) || neuter (c)); |
michael@0 | 713 | } |
michael@0 | 714 | template <typename T> |
michael@0 | 715 | inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) { |
michael@0 | 716 | TRACE_SANITIZE (this); |
michael@0 | 717 | if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false); |
michael@0 | 718 | unsigned int offset = *this; |
michael@0 | 719 | if (unlikely (!offset)) return TRACE_RETURN (true); |
michael@0 | 720 | Type &obj = StructAtOffset<Type> (base, offset); |
michael@0 | 721 | return TRACE_RETURN (likely (obj.sanitize (c, user_data)) || neuter (c)); |
michael@0 | 722 | } |
michael@0 | 723 | |
michael@0 | 724 | inline bool try_set (hb_sanitize_context_t *c, const OffsetType &v) { |
michael@0 | 725 | if (c->may_edit (this, this->static_size)) { |
michael@0 | 726 | this->set (v); |
michael@0 | 727 | return true; |
michael@0 | 728 | } |
michael@0 | 729 | return false; |
michael@0 | 730 | } |
michael@0 | 731 | /* Set the offset to Null */ |
michael@0 | 732 | inline bool neuter (hb_sanitize_context_t *c) { |
michael@0 | 733 | if (c->may_edit (this, this->static_size)) { |
michael@0 | 734 | this->set (0); /* 0 is Null offset */ |
michael@0 | 735 | return true; |
michael@0 | 736 | } |
michael@0 | 737 | return false; |
michael@0 | 738 | } |
michael@0 | 739 | }; |
michael@0 | 740 | template <typename Base, typename OffsetType, typename Type> |
michael@0 | 741 | inline const Type& operator + (const Base &base, const GenericOffsetTo<OffsetType, Type> &offset) { return offset (base); } |
michael@0 | 742 | template <typename Base, typename OffsetType, typename Type> |
michael@0 | 743 | inline Type& operator + (Base &base, GenericOffsetTo<OffsetType, Type> &offset) { return offset (base); } |
michael@0 | 744 | |
michael@0 | 745 | template <typename Type> |
michael@0 | 746 | struct OffsetTo : GenericOffsetTo<Offset, Type> {}; |
michael@0 | 747 | |
michael@0 | 748 | template <typename Type> |
michael@0 | 749 | struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {}; |
michael@0 | 750 | |
michael@0 | 751 | |
michael@0 | 752 | /* |
michael@0 | 753 | * Array Types |
michael@0 | 754 | */ |
michael@0 | 755 | |
michael@0 | 756 | template <typename LenType, typename Type> |
michael@0 | 757 | struct GenericArrayOf |
michael@0 | 758 | { |
michael@0 | 759 | const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const |
michael@0 | 760 | { |
michael@0 | 761 | unsigned int count = len; |
michael@0 | 762 | if (unlikely (start_offset > count)) |
michael@0 | 763 | count = 0; |
michael@0 | 764 | else |
michael@0 | 765 | count -= start_offset; |
michael@0 | 766 | count = MIN (count, *pcount); |
michael@0 | 767 | *pcount = count; |
michael@0 | 768 | return array + start_offset; |
michael@0 | 769 | } |
michael@0 | 770 | |
michael@0 | 771 | inline const Type& operator [] (unsigned int i) const |
michael@0 | 772 | { |
michael@0 | 773 | if (unlikely (i >= len)) return Null(Type); |
michael@0 | 774 | return array[i]; |
michael@0 | 775 | } |
michael@0 | 776 | inline Type& operator [] (unsigned int i) |
michael@0 | 777 | { |
michael@0 | 778 | return array[i]; |
michael@0 | 779 | } |
michael@0 | 780 | inline unsigned int get_size (void) const |
michael@0 | 781 | { return len.static_size + len * Type::static_size; } |
michael@0 | 782 | |
michael@0 | 783 | inline bool serialize (hb_serialize_context_t *c, |
michael@0 | 784 | unsigned int items_len) |
michael@0 | 785 | { |
michael@0 | 786 | TRACE_SERIALIZE (this); |
michael@0 | 787 | if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); |
michael@0 | 788 | len.set (items_len); /* TODO(serialize) Overflow? */ |
michael@0 | 789 | if (unlikely (!c->extend (*this))) return TRACE_RETURN (false); |
michael@0 | 790 | return TRACE_RETURN (true); |
michael@0 | 791 | } |
michael@0 | 792 | |
michael@0 | 793 | inline bool serialize (hb_serialize_context_t *c, |
michael@0 | 794 | Supplier<Type> &items, |
michael@0 | 795 | unsigned int items_len) |
michael@0 | 796 | { |
michael@0 | 797 | TRACE_SERIALIZE (this); |
michael@0 | 798 | if (unlikely (!serialize (c, items_len))) return TRACE_RETURN (false); |
michael@0 | 799 | for (unsigned int i = 0; i < items_len; i++) |
michael@0 | 800 | array[i] = items[i]; |
michael@0 | 801 | items.advance (items_len); |
michael@0 | 802 | return TRACE_RETURN (true); |
michael@0 | 803 | } |
michael@0 | 804 | |
michael@0 | 805 | inline bool sanitize (hb_sanitize_context_t *c) { |
michael@0 | 806 | TRACE_SANITIZE (this); |
michael@0 | 807 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
michael@0 | 808 | |
michael@0 | 809 | /* Note: for structs that do not reference other structs, |
michael@0 | 810 | * we do not need to call their sanitize() as we already did |
michael@0 | 811 | * a bound check on the aggregate array size. We just include |
michael@0 | 812 | * a small unreachable expression to make sure the structs |
michael@0 | 813 | * pointed to do have a simple sanitize(), ie. they do not |
michael@0 | 814 | * reference other structs via offsets. |
michael@0 | 815 | */ |
michael@0 | 816 | (void) (false && array[0].sanitize (c)); |
michael@0 | 817 | |
michael@0 | 818 | return TRACE_RETURN (true); |
michael@0 | 819 | } |
michael@0 | 820 | inline bool sanitize (hb_sanitize_context_t *c, void *base) { |
michael@0 | 821 | TRACE_SANITIZE (this); |
michael@0 | 822 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
michael@0 | 823 | unsigned int count = len; |
michael@0 | 824 | for (unsigned int i = 0; i < count; i++) |
michael@0 | 825 | if (unlikely (!array[i].sanitize (c, base))) |
michael@0 | 826 | return TRACE_RETURN (false); |
michael@0 | 827 | return TRACE_RETURN (true); |
michael@0 | 828 | } |
michael@0 | 829 | template <typename T> |
michael@0 | 830 | inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) { |
michael@0 | 831 | TRACE_SANITIZE (this); |
michael@0 | 832 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
michael@0 | 833 | unsigned int count = len; |
michael@0 | 834 | for (unsigned int i = 0; i < count; i++) |
michael@0 | 835 | if (unlikely (!array[i].sanitize (c, base, user_data))) |
michael@0 | 836 | return TRACE_RETURN (false); |
michael@0 | 837 | return TRACE_RETURN (true); |
michael@0 | 838 | } |
michael@0 | 839 | |
michael@0 | 840 | private: |
michael@0 | 841 | inline bool sanitize_shallow (hb_sanitize_context_t *c) { |
michael@0 | 842 | TRACE_SANITIZE (this); |
michael@0 | 843 | return TRACE_RETURN (c->check_struct (this) && c->check_array (this, Type::static_size, len)); |
michael@0 | 844 | } |
michael@0 | 845 | |
michael@0 | 846 | public: |
michael@0 | 847 | LenType len; |
michael@0 | 848 | Type array[VAR]; |
michael@0 | 849 | public: |
michael@0 | 850 | DEFINE_SIZE_ARRAY (sizeof (LenType), array); |
michael@0 | 851 | }; |
michael@0 | 852 | |
michael@0 | 853 | /* An array with a USHORT number of elements. */ |
michael@0 | 854 | template <typename Type> |
michael@0 | 855 | struct ArrayOf : GenericArrayOf<USHORT, Type> {}; |
michael@0 | 856 | |
michael@0 | 857 | /* An array with a ULONG number of elements. */ |
michael@0 | 858 | template <typename Type> |
michael@0 | 859 | struct LongArrayOf : GenericArrayOf<ULONG, Type> {}; |
michael@0 | 860 | |
michael@0 | 861 | /* Array of Offset's */ |
michael@0 | 862 | template <typename Type> |
michael@0 | 863 | struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {}; |
michael@0 | 864 | |
michael@0 | 865 | /* Array of LongOffset's */ |
michael@0 | 866 | template <typename Type> |
michael@0 | 867 | struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {}; |
michael@0 | 868 | |
michael@0 | 869 | /* LongArray of LongOffset's */ |
michael@0 | 870 | template <typename Type> |
michael@0 | 871 | struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {}; |
michael@0 | 872 | |
michael@0 | 873 | /* Array of offsets relative to the beginning of the array itself. */ |
michael@0 | 874 | template <typename Type> |
michael@0 | 875 | struct OffsetListOf : OffsetArrayOf<Type> |
michael@0 | 876 | { |
michael@0 | 877 | inline const Type& operator [] (unsigned int i) const |
michael@0 | 878 | { |
michael@0 | 879 | if (unlikely (i >= this->len)) return Null(Type); |
michael@0 | 880 | return this+this->array[i]; |
michael@0 | 881 | } |
michael@0 | 882 | |
michael@0 | 883 | inline bool sanitize (hb_sanitize_context_t *c) { |
michael@0 | 884 | TRACE_SANITIZE (this); |
michael@0 | 885 | return TRACE_RETURN (OffsetArrayOf<Type>::sanitize (c, this)); |
michael@0 | 886 | } |
michael@0 | 887 | template <typename T> |
michael@0 | 888 | inline bool sanitize (hb_sanitize_context_t *c, T user_data) { |
michael@0 | 889 | TRACE_SANITIZE (this); |
michael@0 | 890 | return TRACE_RETURN (OffsetArrayOf<Type>::sanitize (c, this, user_data)); |
michael@0 | 891 | } |
michael@0 | 892 | }; |
michael@0 | 893 | |
michael@0 | 894 | |
michael@0 | 895 | /* An array with a USHORT number of elements, |
michael@0 | 896 | * starting at second element. */ |
michael@0 | 897 | template <typename Type> |
michael@0 | 898 | struct HeadlessArrayOf |
michael@0 | 899 | { |
michael@0 | 900 | inline const Type& operator [] (unsigned int i) const |
michael@0 | 901 | { |
michael@0 | 902 | if (unlikely (i >= len || !i)) return Null(Type); |
michael@0 | 903 | return array[i-1]; |
michael@0 | 904 | } |
michael@0 | 905 | inline unsigned int get_size (void) const |
michael@0 | 906 | { return len.static_size + (len ? len - 1 : 0) * Type::static_size; } |
michael@0 | 907 | |
michael@0 | 908 | inline bool serialize (hb_serialize_context_t *c, |
michael@0 | 909 | Supplier<Type> &items, |
michael@0 | 910 | unsigned int items_len) |
michael@0 | 911 | { |
michael@0 | 912 | TRACE_SERIALIZE (this); |
michael@0 | 913 | if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); |
michael@0 | 914 | len.set (items_len); /* TODO(serialize) Overflow? */ |
michael@0 | 915 | if (unlikely (!items_len)) return TRACE_RETURN (true); |
michael@0 | 916 | if (unlikely (!c->extend (*this))) return TRACE_RETURN (false); |
michael@0 | 917 | for (unsigned int i = 0; i < items_len - 1; i++) |
michael@0 | 918 | array[i] = items[i]; |
michael@0 | 919 | items.advance (items_len - 1); |
michael@0 | 920 | return TRACE_RETURN (true); |
michael@0 | 921 | } |
michael@0 | 922 | |
michael@0 | 923 | inline bool sanitize_shallow (hb_sanitize_context_t *c) { |
michael@0 | 924 | return c->check_struct (this) |
michael@0 | 925 | && c->check_array (this, Type::static_size, len); |
michael@0 | 926 | } |
michael@0 | 927 | |
michael@0 | 928 | inline bool sanitize (hb_sanitize_context_t *c) { |
michael@0 | 929 | TRACE_SANITIZE (this); |
michael@0 | 930 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
michael@0 | 931 | |
michael@0 | 932 | /* Note: for structs that do not reference other structs, |
michael@0 | 933 | * we do not need to call their sanitize() as we already did |
michael@0 | 934 | * a bound check on the aggregate array size. We just include |
michael@0 | 935 | * a small unreachable expression to make sure the structs |
michael@0 | 936 | * pointed to do have a simple sanitize(), ie. they do not |
michael@0 | 937 | * reference other structs via offsets. |
michael@0 | 938 | */ |
michael@0 | 939 | (void) (false && array[0].sanitize (c)); |
michael@0 | 940 | |
michael@0 | 941 | return TRACE_RETURN (true); |
michael@0 | 942 | } |
michael@0 | 943 | |
michael@0 | 944 | USHORT len; |
michael@0 | 945 | Type array[VAR]; |
michael@0 | 946 | public: |
michael@0 | 947 | DEFINE_SIZE_ARRAY (sizeof (USHORT), array); |
michael@0 | 948 | }; |
michael@0 | 949 | |
michael@0 | 950 | |
michael@0 | 951 | /* An array with sorted elements. Supports binary searching. */ |
michael@0 | 952 | template <typename Type> |
michael@0 | 953 | struct SortedArrayOf : ArrayOf<Type> { |
michael@0 | 954 | |
michael@0 | 955 | template <typename SearchType> |
michael@0 | 956 | inline int search (const SearchType &x) const |
michael@0 | 957 | { |
michael@0 | 958 | /* Hand-coded bsearch here since this is in the hot inner loop. */ |
michael@0 | 959 | int min = 0, max = (int) this->len - 1; |
michael@0 | 960 | while (min <= max) |
michael@0 | 961 | { |
michael@0 | 962 | int mid = (min + max) / 2; |
michael@0 | 963 | int c = this->array[mid].cmp (x); |
michael@0 | 964 | if (c < 0) |
michael@0 | 965 | max = mid - 1; |
michael@0 | 966 | else if (c > 0) |
michael@0 | 967 | min = mid + 1; |
michael@0 | 968 | else |
michael@0 | 969 | return mid; |
michael@0 | 970 | } |
michael@0 | 971 | return -1; |
michael@0 | 972 | } |
michael@0 | 973 | }; |
michael@0 | 974 | |
michael@0 | 975 | |
michael@0 | 976 | } /* namespace OT */ |
michael@0 | 977 | |
michael@0 | 978 | |
michael@0 | 979 | #endif /* HB_OPEN_TYPE_PRIVATE_HH */ |