2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
3 * Copyright © 2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #ifndef HB_OPEN_TYPE_PRIVATE_HH
30 #define HB_OPEN_TYPE_PRIVATE_HH
32 #include "hb-private.hh"
33 #include "hb-debug.hh"
34 #include "hb-face-private.hh"
45 /* Cast to struct T, reference to reference */
46 template<typename Type, typename TObject>
47 static inline const Type& CastR(const TObject &X)
48 { return reinterpret_cast<const Type&> (X); }
49 template<typename Type, typename TObject>
50 static inline Type& CastR(TObject &X)
51 { return reinterpret_cast<Type&> (X); }
53 /* Cast to struct T, pointer to pointer */
54 template<typename Type, typename TObject>
55 static inline const Type* CastP(const TObject *X)
56 { return reinterpret_cast<const Type*> (X); }
57 template<typename Type, typename TObject>
58 static inline Type* CastP(TObject *X)
59 { return reinterpret_cast<Type*> (X); }
61 /* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
62 * location pointed to by P plus Ofs bytes. */
63 template<typename Type>
64 static inline const Type& StructAtOffset(const void *P, unsigned int offset)
65 { return * reinterpret_cast<const Type*> ((const char *) P + offset); }
66 template<typename Type>
67 static inline Type& StructAtOffset(void *P, unsigned int offset)
68 { return * reinterpret_cast<Type*> ((char *) P + offset); }
70 /* StructAfter<T>(X) returns the struct T& that is placed after X.
71 * Works with X of variable size also. X must implement get_size() */
72 template<typename Type, typename TObject>
73 static inline const Type& StructAfter(const TObject &X)
74 { return StructAtOffset<Type>(&X, X.get_size()); }
75 template<typename Type, typename TObject>
76 static inline Type& StructAfter(TObject &X)
77 { return StructAtOffset<Type>(&X, X.get_size()); }
85 /* Check _assertion in a method environment */
86 #define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \
87 inline void _instance_assertion_on_line_##_line (void) const \
89 static_assert ((_assertion), ""); \
90 ASSERT_INSTANCE_POD (*this); /* Make sure it's POD. */ \
92 # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion)
93 # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion)
95 /* Check that _code compiles in a method environment */
96 #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \
97 inline void _compiles_assertion_on_line_##_line (void) const \
99 # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code)
100 # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code)
103 #define DEFINE_SIZE_STATIC(size) \
104 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)); \
105 static const unsigned int static_size = (size); \
106 static const unsigned int min_size = (size); \
107 inline unsigned int get_size (void) const { return (size); }
109 #define DEFINE_SIZE_UNION(size, _member) \
110 DEFINE_INSTANCE_ASSERTION (0*sizeof(this->u._member.static_size) + sizeof(this->u._member) == (size)); \
111 static const unsigned int min_size = (size)
113 #define DEFINE_SIZE_MIN(size) \
114 DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)); \
115 static const unsigned int min_size = (size)
117 #define DEFINE_SIZE_ARRAY(size, array) \
118 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \
119 DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \
120 static const unsigned int min_size = (size)
122 #define DEFINE_SIZE_ARRAY2(size, array1, array2) \
123 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \
124 DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].static_size) \
125 static const unsigned int min_size = (size)
133 /* Global nul-content Null pool. Enlarge as necessary. */
135 #define HB_NULL_POOL_SIZE 264
136 static_assert (HB_NULL_POOL_SIZE % sizeof (void *) == 0, "Align HB_NULL_POOL_SIZE.");
138 #ifdef HB_NO_VISIBILITY
143 const void * const _hb_NullPool[HB_NULL_POOL_SIZE / sizeof (void *)]
144 #ifdef HB_NO_VISIBILITY
149 /* Generic nul-content Null objects. */
150 template <typename Type>
151 static inline const Type& Null (void) {
152 static_assert (sizeof (Type) <= HB_NULL_POOL_SIZE, "Increase HB_NULL_POOL_SIZE.");
153 return *CastP<Type> (_hb_NullPool);
156 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
157 #define DEFINE_NULL_DATA(Type, data) \
158 static const char _Null##Type[sizeof (Type) + 1] = data; /* +1 is for nul-termination in data */ \
160 /*static*/ inline const Type& Null<Type> (void) { \
161 return *CastP<Type> (_Null##Type); \
162 } /* The following line really exists such that we end in a place needing semicolon */ \
163 static_assert (Type::min_size + 1 <= sizeof (_Null##Type), "Null pool too small. Enlarge.")
165 /* Accessor macro. */
166 #define Null(Type) Null<Type>()
173 template <typename Context, typename Return, unsigned int MaxDebugDepth>
174 struct hb_dispatch_context_t
176 static const unsigned int max_debug_depth = MaxDebugDepth;
177 typedef Return return_t;
178 template <typename T, typename F>
179 inline bool may_dispatch (const T *obj, const F *format) { return true; }
180 static return_t no_dispatch_return_value (void) { return Context::default_return_value (); }
188 /* This limits sanitizing time on really broken fonts. */
189 #ifndef HB_SANITIZE_MAX_EDITS
190 #define HB_SANITIZE_MAX_EDITS 32
192 #ifndef HB_SANITIZE_MAX_OPS_FACTOR
193 #define HB_SANITIZE_MAX_OPS_FACTOR 8
195 #ifndef HB_SANITIZE_MAX_OPS_MIN
196 #define HB_SANITIZE_MAX_OPS_MIN 16384
199 struct hb_sanitize_context_t :
200 hb_dispatch_context_t<hb_sanitize_context_t, bool, HB_DEBUG_SANITIZE>
202 inline hb_sanitize_context_t (void) :
204 start (nullptr), end (nullptr),
205 writable (false), edit_count (0), max_ops (0),
209 inline const char *get_name (void) { return "SANITIZE"; }
210 template <typename T, typename F>
211 inline bool may_dispatch (const T *obj, const F *format)
212 { return format->sanitize (this); }
213 template <typename T>
214 inline return_t dispatch (const T &obj) { return obj.sanitize (this); }
215 static return_t default_return_value (void) { return true; }
216 static return_t no_dispatch_return_value (void) { return false; }
217 bool stop_sublookup_iteration (const return_t r) const { return !r; }
219 inline void init (hb_blob_t *b)
221 this->blob = hb_blob_reference (b);
222 this->writable = false;
225 inline void start_processing (void)
227 this->start = hb_blob_get_data (this->blob, nullptr);
228 this->end = this->start + hb_blob_get_length (this->blob);
229 assert (this->start <= this->end); /* Must not overflow. */
230 this->max_ops = MAX ((unsigned int) (this->end - this->start) * HB_SANITIZE_MAX_OPS_FACTOR,
231 (unsigned) HB_SANITIZE_MAX_OPS_MIN);
232 this->edit_count = 0;
233 this->debug_depth = 0;
235 DEBUG_MSG_LEVEL (SANITIZE, start, 0, +1,
236 "start [%p..%p] (%lu bytes)",
237 this->start, this->end,
238 (unsigned long) (this->end - this->start));
241 inline void end_processing (void)
243 DEBUG_MSG_LEVEL (SANITIZE, this->start, 0, -1,
244 "end [%p..%p] %u edit requests",
245 this->start, this->end, this->edit_count);
247 hb_blob_destroy (this->blob);
248 this->blob = nullptr;
249 this->start = this->end = nullptr;
252 inline bool check_range (const void *base, unsigned int len) const
254 const char *p = (const char *) base;
255 bool ok = this->max_ops-- > 0 &&
258 (unsigned int) (this->end - p) >= len;
260 DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
261 "check_range [%p..%p] (%d bytes) in [%p..%p] -> %s",
263 this->start, this->end,
264 ok ? "OK" : "OUT-OF-RANGE");
269 inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
271 const char *p = (const char *) base;
272 bool overflows = _hb_unsigned_int_mul_overflows (len, record_size);
273 unsigned int array_size = record_size * len;
274 bool ok = !overflows && this->check_range (base, array_size);
276 DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
277 "check_array [%p..%p] (%d*%d=%d bytes) in [%p..%p] -> %s",
278 p, p + (record_size * len), record_size, len, (unsigned int) array_size,
279 this->start, this->end,
280 overflows ? "OVERFLOWS" : ok ? "OK" : "OUT-OF-RANGE");
285 template <typename Type>
286 inline bool check_struct (const Type *obj) const
288 return likely (this->check_range (obj, obj->min_size));
291 inline bool may_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
293 if (this->edit_count >= HB_SANITIZE_MAX_EDITS)
296 const char *p = (const char *) base;
299 DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
300 "may_edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s",
303 this->start, this->end,
304 this->writable ? "GRANTED" : "DENIED");
306 return this->writable;
309 template <typename Type, typename ValueType>
310 inline bool try_set (const Type *obj, const ValueType &v) {
311 if (this->may_edit (obj, obj->static_size)) {
312 const_cast<Type *> (obj)->set (v);
318 mutable unsigned int debug_depth;
319 const char *start, *end;
321 unsigned int edit_count;
324 unsigned int num_glyphs;
329 /* Template to sanitize an object. */
330 template <typename Type>
333 inline Sanitizer (void) {}
335 inline hb_blob_t *sanitize (hb_blob_t *blob) {
338 /* TODO is_sane() stuff */
343 DEBUG_MSG_FUNC (SANITIZE, c->start, "start");
345 c->start_processing ();
347 if (unlikely (!c->start)) {
348 c->end_processing ();
352 Type *t = CastP<Type> (const_cast<char *> (c->start));
354 sane = t->sanitize (c);
357 DEBUG_MSG_FUNC (SANITIZE, c->start, "passed first round with %d edits; going for second round", c->edit_count);
359 /* sanitize again to ensure no toe-stepping */
361 sane = t->sanitize (c);
363 DEBUG_MSG_FUNC (SANITIZE, c->start, "requested %d edits in second round; FAILLING", c->edit_count);
368 unsigned int edit_count = c->edit_count;
369 if (edit_count && !c->writable) {
370 c->start = hb_blob_get_data_writable (blob, nullptr);
371 c->end = c->start + hb_blob_get_length (blob);
375 /* ok, we made it writable by relocating. try again */
376 DEBUG_MSG_FUNC (SANITIZE, c->start, "retry");
382 c->end_processing ();
384 DEBUG_MSG_FUNC (SANITIZE, c->start, sane ? "PASSED" : "FAILED");
388 hb_blob_destroy (blob);
389 return hb_blob_get_empty ();
393 static const Type* lock_instance (hb_blob_t *blob) {
394 hb_blob_make_immutable (blob);
395 const char *base = hb_blob_get_data (blob, nullptr);
396 return unlikely (!base) ? &Null(Type) : CastP<Type> (base);
399 inline void set_num_glyphs (unsigned int num_glyphs) { c->num_glyphs = num_glyphs; }
402 hb_sanitize_context_t c[1];
412 struct hb_serialize_context_t
414 inline hb_serialize_context_t (void *start_, unsigned int size)
416 this->start = (char *) start_;
417 this->end = this->start + size;
419 this->ran_out_of_room = false;
420 this->head = this->start;
421 this->debug_depth = 0;
424 template <typename Type>
425 inline Type *start_serialize (void)
427 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1,
428 "start [%p..%p] (%lu bytes)",
429 this->start, this->end,
430 (unsigned long) (this->end - this->start));
432 return start_embed<Type> ();
435 inline void end_serialize (void)
437 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1,
438 "end [%p..%p] serialized %d bytes; %s",
439 this->start, this->end,
440 (int) (this->head - this->start),
441 this->ran_out_of_room ? "RAN OUT OF ROOM" : "did not ran out of room");
445 template <typename Type>
446 inline Type *copy (void)
448 assert (!this->ran_out_of_room);
449 unsigned int len = this->head - this->start;
450 void *p = malloc (len);
452 memcpy (p, this->start, len);
453 return reinterpret_cast<Type *> (p);
456 template <typename Type>
457 inline Type *allocate_size (unsigned int size)
459 if (unlikely (this->ran_out_of_room || this->end - this->head < ptrdiff_t (size))) {
460 this->ran_out_of_room = true;
463 memset (this->head, 0, size);
464 char *ret = this->head;
466 return reinterpret_cast<Type *> (ret);
469 template <typename Type>
470 inline Type *allocate_min (void)
472 return this->allocate_size<Type> (Type::min_size);
475 template <typename Type>
476 inline Type *start_embed (void)
478 Type *ret = reinterpret_cast<Type *> (this->head);
482 template <typename Type>
483 inline Type *embed (const Type &obj)
485 unsigned int size = obj.get_size ();
486 Type *ret = this->allocate_size<Type> (size);
487 if (unlikely (!ret)) return nullptr;
488 memcpy (ret, obj, size);
492 template <typename Type>
493 inline Type *extend_min (Type &obj)
495 unsigned int size = obj.min_size;
496 assert (this->start <= (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head);
497 if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return nullptr;
498 return reinterpret_cast<Type *> (&obj);
501 template <typename Type>
502 inline Type *extend (Type &obj)
504 unsigned int size = obj.get_size ();
505 assert (this->start < (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head);
506 if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return nullptr;
507 return reinterpret_cast<Type *> (&obj);
510 inline void truncate (void *new_head)
512 assert (this->start < new_head && new_head <= this->head);
513 this->head = (char *) new_head;
516 unsigned int debug_depth;
517 char *start, *end, *head;
518 bool ran_out_of_room;
521 template <typename Type>
524 inline Supplier (const Type *array, unsigned int len_, unsigned int stride_=sizeof(Type))
530 inline const Type operator [] (unsigned int i) const
532 if (unlikely (i >= len)) return Type ();
533 return * (const Type *) (const void *) ((const char *) head + stride * i);
536 inline Supplier<Type> & operator += (unsigned int count)
538 if (unlikely (count > len))
541 head = (const Type *) (const void *) ((const char *) head + stride * count);
546 inline Supplier (const Supplier<Type> &); /* Disallow copy */
547 inline Supplier<Type>& operator= (const Supplier<Type> &); /* Disallow copy */
557 * The OpenType Font File: Data Types
561 /* "The following data types are used in the OpenType font file.
562 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
569 template <typename Type, int Bytes> struct BEInt;
571 template <typename Type>
572 struct BEInt<Type, 1>
575 inline void set (Type V)
579 inline operator Type (void) const
585 template <typename Type>
586 struct BEInt<Type, 2>
589 inline void set (Type V)
591 v[0] = (V >> 8) & 0xFF;
594 inline operator Type (void) const
599 private: uint8_t v[2];
601 template <typename Type>
602 struct BEInt<Type, 3>
605 inline void set (Type V)
607 v[0] = (V >> 16) & 0xFF;
608 v[1] = (V >> 8) & 0xFF;
611 inline operator Type (void) const
617 private: uint8_t v[3];
619 template <typename Type>
620 struct BEInt<Type, 4>
623 inline void set (Type V)
625 v[0] = (V >> 24) & 0xFF;
626 v[1] = (V >> 16) & 0xFF;
627 v[2] = (V >> 8) & 0xFF;
630 inline operator Type (void) const
637 private: uint8_t v[4];
640 /* Integer types in big-endian order and no alignment requirement */
641 template <typename Type, unsigned int Size>
644 inline void set (Type i) { v.set (i); }
645 inline operator Type(void) const { return v; }
646 inline bool operator == (const IntType<Type,Size> &o) const { return (Type) v == (Type) o.v; }
647 inline bool operator != (const IntType<Type,Size> &o) const { return !(*this == o); }
648 static inline int cmp (const IntType<Type,Size> *a, const IntType<Type,Size> *b) { return b->cmp (*a); }
649 template <typename Type2>
650 inline int cmp (Type2 a) const
653 if (sizeof (Type) < sizeof (int) && sizeof (Type2) < sizeof (int))
654 return (int) a - (int) b;
656 return a < b ? -1 : a == b ? 0 : +1;
658 inline bool sanitize (hb_sanitize_context_t *c) const
660 TRACE_SANITIZE (this);
661 return_trace (likely (c->check_struct (this)));
666 DEFINE_SIZE_STATIC (Size);
669 typedef IntType<uint8_t, 1> HBUINT8; /* 8-bit unsigned integer. */
670 typedef IntType<int8_t, 1> HBINT8; /* 8-bit signed integer. */
671 typedef IntType<uint16_t, 2> HBUINT16; /* 16-bit unsigned integer. */
672 typedef IntType<int16_t, 2> HBINT16; /* 16-bit signed integer. */
673 typedef IntType<uint32_t, 4> HBUINT32; /* 32-bit unsigned integer. */
674 typedef IntType<int32_t, 4> HBINT32; /* 32-bit signed integer. */
675 typedef IntType<uint32_t, 3> UINT24; /* 24-bit unsigned integer. */
677 /* 16-bit signed integer (HBINT16) that describes a quantity in FUnits. */
678 typedef HBINT16 FWORD;
680 /* 16-bit unsigned integer (HBUINT16) that describes a quantity in FUnits. */
681 typedef HBUINT16 UFWORD;
683 /* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */
684 struct F2DOT14 : HBINT16
686 //inline float to_float (void) const { return ???; }
687 //inline void set_float (float f) { v.set (f * ???); }
689 DEFINE_SIZE_STATIC (2);
692 /* 32-bit signed fixed-point number (16.16). */
693 struct Fixed: HBINT32
695 inline float to_float (void) const { return ((int32_t) v) / 65536.0; }
696 inline void set_float (float f) { v.set (round (f * 65536.0)); }
698 DEFINE_SIZE_STATIC (4);
701 /* Date represented in number of seconds since 12:00 midnight, January 1,
702 * 1904. The value is represented as a signed 64-bit integer. */
705 inline bool sanitize (hb_sanitize_context_t *c) const
707 TRACE_SANITIZE (this);
708 return_trace (likely (c->check_struct (this)));
714 DEFINE_SIZE_STATIC (8);
717 /* Array of four uint8s (length = 32 bits) used to identify a script, language
718 * system, feature, or baseline */
719 struct Tag : HBUINT32
721 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
722 inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); }
723 inline operator char* (void) { return reinterpret_cast<char *> (&this->v); }
725 DEFINE_SIZE_STATIC (4);
727 DEFINE_NULL_DATA (Tag, " ");
729 /* Glyph index number, same as uint16 (length = 16 bits) */
730 typedef HBUINT16 GlyphID;
732 /* Script/language-system/feature index */
733 struct Index : HBUINT16 {
734 static const unsigned int NOT_FOUND_INDEX = 0xFFFFu;
736 DEFINE_NULL_DATA (Index, "\xff\xff");
738 /* Offset, Null offset = 0 */
739 template <typename Type>
742 inline bool is_null (void) const { return 0 == *this; }
744 inline void *serialize (hb_serialize_context_t *c, const void *base)
746 void *t = c->start_embed<void> ();
747 this->set ((char *) t - (char *) base); /* TODO(serialize) Overflow? */
752 DEFINE_SIZE_STATIC (sizeof(Type));
755 typedef Offset<HBUINT16> Offset16;
756 typedef Offset<HBUINT32> Offset32;
760 struct CheckSum : HBUINT32
762 /* This is reference implementation from the spec. */
763 static inline uint32_t CalcTableChecksum (const HBUINT32 *Table, uint32_t Length)
766 assert (0 == (Length & 3));
767 const HBUINT32 *EndPtr = Table + Length / HBUINT32::static_size;
769 while (Table < EndPtr)
774 /* Note: data should be 4byte aligned and have 4byte padding at the end. */
775 inline void set_for_data (const void *data, unsigned int length)
776 { set (CalcTableChecksum ((const HBUINT32 *) data, length)); }
779 DEFINE_SIZE_STATIC (4);
787 template <typename FixedType=HBUINT16>
790 inline uint32_t to_int (void) const { return (major << (sizeof(FixedType) * 8)) + minor; }
792 inline bool sanitize (hb_sanitize_context_t *c) const
794 TRACE_SANITIZE (this);
795 return_trace (c->check_struct (this));
801 DEFINE_SIZE_STATIC (2 * sizeof(FixedType));
807 * Template subclasses of Offset that do the dereferencing.
811 template <typename Type, typename OffsetType=HBUINT16>
812 struct OffsetTo : Offset<OffsetType>
814 inline const Type& operator () (const void *base) const
816 unsigned int offset = *this;
817 if (unlikely (!offset)) return Null(Type);
818 return StructAtOffset<Type> (base, offset);
821 inline Type& serialize (hb_serialize_context_t *c, const void *base)
823 return * (Type *) Offset<OffsetType>::serialize (c, base);
826 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
828 TRACE_SANITIZE (this);
829 if (unlikely (!c->check_struct (this))) return_trace (false);
830 unsigned int offset = *this;
831 if (unlikely (!offset)) return_trace (true);
832 if (unlikely (!c->check_range (base, offset))) return_trace (false);
833 const Type &obj = StructAtOffset<Type> (base, offset);
834 return_trace (likely (obj.sanitize (c)) || neuter (c));
836 template <typename T>
837 inline bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const
839 TRACE_SANITIZE (this);
840 if (unlikely (!c->check_struct (this))) return_trace (false);
841 unsigned int offset = *this;
842 if (unlikely (!offset)) return_trace (true);
843 if (unlikely (!c->check_range (base, offset))) return_trace (false);
844 const Type &obj = StructAtOffset<Type> (base, offset);
845 return_trace (likely (obj.sanitize (c, user_data)) || neuter (c));
848 /* Set the offset to Null */
849 inline bool neuter (hb_sanitize_context_t *c) const {
850 return c->try_set (this, 0);
852 DEFINE_SIZE_STATIC (sizeof(OffsetType));
854 template <typename Type> struct LOffsetTo : OffsetTo<Type, HBUINT32> {};
855 template <typename Base, typename OffsetType, typename Type>
856 static inline const Type& operator + (const Base &base, const OffsetTo<Type, OffsetType> &offset) { return offset (base); }
857 template <typename Base, typename OffsetType, typename Type>
858 static inline Type& operator + (Base &base, OffsetTo<Type, OffsetType> &offset) { return offset (base); }
865 /* An array with a number of elements. */
866 template <typename Type, typename LenType=HBUINT16>
869 const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
871 unsigned int count = len;
872 if (unlikely (start_offset > count))
875 count -= start_offset;
876 count = MIN (count, *pcount);
878 return array + start_offset;
881 inline const Type& operator [] (unsigned int i) const
883 if (unlikely (i >= len)) return Null(Type);
886 inline Type& operator [] (unsigned int i)
890 inline unsigned int get_size (void) const
891 { return len.static_size + len * Type::static_size; }
893 inline bool serialize (hb_serialize_context_t *c,
894 unsigned int items_len)
896 TRACE_SERIALIZE (this);
897 if (unlikely (!c->extend_min (*this))) return_trace (false);
898 len.set (items_len); /* TODO(serialize) Overflow? */
899 if (unlikely (!c->extend (*this))) return_trace (false);
903 inline bool serialize (hb_serialize_context_t *c,
904 Supplier<Type> &items,
905 unsigned int items_len)
907 TRACE_SERIALIZE (this);
908 if (unlikely (!serialize (c, items_len))) return_trace (false);
909 for (unsigned int i = 0; i < items_len; i++)
915 inline bool sanitize (hb_sanitize_context_t *c) const
917 TRACE_SANITIZE (this);
918 if (unlikely (!sanitize_shallow (c))) return_trace (false);
920 /* Note: for structs that do not reference other structs,
921 * we do not need to call their sanitize() as we already did
922 * a bound check on the aggregate array size. We just include
923 * a small unreachable expression to make sure the structs
924 * pointed to do have a simple sanitize(), ie. they do not
925 * reference other structs via offsets.
927 (void) (false && array[0].sanitize (c));
931 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
933 TRACE_SANITIZE (this);
934 if (unlikely (!sanitize_shallow (c))) return_trace (false);
935 unsigned int count = len;
936 for (unsigned int i = 0; i < count; i++)
937 if (unlikely (!array[i].sanitize (c, base)))
938 return_trace (false);
941 template <typename T>
942 inline bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const
944 TRACE_SANITIZE (this);
945 if (unlikely (!sanitize_shallow (c))) return_trace (false);
946 unsigned int count = len;
947 for (unsigned int i = 0; i < count; i++)
948 if (unlikely (!array[i].sanitize (c, base, user_data)))
949 return_trace (false);
953 template <typename SearchType>
954 inline int lsearch (const SearchType &x) const
956 unsigned int count = len;
957 for (unsigned int i = 0; i < count; i++)
958 if (!this->array[i].cmp (x))
963 inline void qsort (void)
965 ::qsort (array, len, sizeof (Type), Type::cmp);
969 inline bool sanitize_shallow (hb_sanitize_context_t *c) const
971 TRACE_SANITIZE (this);
972 return_trace (len.sanitize (c) && c->check_array (array, Type::static_size, len));
979 DEFINE_SIZE_ARRAY (sizeof (LenType), array);
981 template <typename Type> struct LArrayOf : ArrayOf<Type, HBUINT32> {};
983 /* Array of Offset's */
984 template <typename Type, typename OffsetType=HBUINT16>
985 struct OffsetArrayOf : ArrayOf<OffsetTo<Type, OffsetType> > {};
987 /* Array of offsets relative to the beginning of the array itself. */
988 template <typename Type>
989 struct OffsetListOf : OffsetArrayOf<Type>
991 inline const Type& operator [] (unsigned int i) const
993 if (unlikely (i >= this->len)) return Null(Type);
994 return this+this->array[i];
997 inline bool sanitize (hb_sanitize_context_t *c) const
999 TRACE_SANITIZE (this);
1000 return_trace (OffsetArrayOf<Type>::sanitize (c, this));
1002 template <typename T>
1003 inline bool sanitize (hb_sanitize_context_t *c, T user_data) const
1005 TRACE_SANITIZE (this);
1006 return_trace (OffsetArrayOf<Type>::sanitize (c, this, user_data));
1011 /* An array starting at second element. */
1012 template <typename Type, typename LenType=HBUINT16>
1013 struct HeadlessArrayOf
1015 inline const Type& operator [] (unsigned int i) const
1017 if (unlikely (i >= len || !i)) return Null(Type);
1020 inline unsigned int get_size (void) const
1021 { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
1023 inline bool serialize (hb_serialize_context_t *c,
1024 Supplier<Type> &items,
1025 unsigned int items_len)
1027 TRACE_SERIALIZE (this);
1028 if (unlikely (!c->extend_min (*this))) return_trace (false);
1029 len.set (items_len); /* TODO(serialize) Overflow? */
1030 if (unlikely (!items_len)) return_trace (true);
1031 if (unlikely (!c->extend (*this))) return_trace (false);
1032 for (unsigned int i = 0; i < items_len - 1; i++)
1033 array[i] = items[i];
1034 items += items_len - 1;
1035 return_trace (true);
1038 inline bool sanitize (hb_sanitize_context_t *c) const
1040 TRACE_SANITIZE (this);
1041 if (unlikely (!sanitize_shallow (c))) return_trace (false);
1043 /* Note: for structs that do not reference other structs,
1044 * we do not need to call their sanitize() as we already did
1045 * a bound check on the aggregate array size. We just include
1046 * a small unreachable expression to make sure the structs
1047 * pointed to do have a simple sanitize(), ie. they do not
1048 * reference other structs via offsets.
1050 (void) (false && array[0].sanitize (c));
1052 return_trace (true);
1056 inline bool sanitize_shallow (hb_sanitize_context_t *c) const
1058 TRACE_SANITIZE (this);
1059 return_trace (len.sanitize (c) &&
1060 (!len || c->check_array (array, Type::static_size, len - 1)));
1067 DEFINE_SIZE_ARRAY (sizeof (LenType), array);
1072 * An array with sorted elements. Supports binary searching.
1074 template <typename Type, typename LenType=HBUINT16>
1075 struct SortedArrayOf : ArrayOf<Type, LenType>
1077 template <typename SearchType>
1078 inline int bsearch (const SearchType &x) const
1080 /* Hand-coded bsearch here since this is in the hot inner loop. */
1081 const Type *arr = this->array;
1082 int min = 0, max = (int) this->len - 1;
1085 int mid = (min + max) / 2;
1086 int c = arr[mid].cmp (x);
1099 * Binary-search arrays
1102 struct BinSearchHeader
1104 inline operator uint32_t (void) const { return len; }
1106 inline bool sanitize (hb_sanitize_context_t *c) const
1108 TRACE_SANITIZE (this);
1109 return_trace (c->check_struct (this));
1112 inline void set (unsigned int v)
1116 entrySelectorZ.set (MAX (1u, _hb_bit_storage (v)) - 1);
1117 searchRangeZ.set (16 * (1u << entrySelectorZ));
1118 rangeShiftZ.set (v * 16 > searchRangeZ
1119 ? 16 * v - searchRangeZ
1125 HBUINT16 searchRangeZ;
1126 HBUINT16 entrySelectorZ;
1127 HBUINT16 rangeShiftZ;
1130 DEFINE_SIZE_STATIC (8);
1133 template <typename Type>
1134 struct BinSearchArrayOf : SortedArrayOf<Type, BinSearchHeader> {};
1137 /* Lazy struct and blob loaders. */
1139 /* Logic is shared between hb_lazy_loader_t and hb_lazy_table_loader_t */
1140 template <typename T>
1141 struct hb_lazy_loader_t
1143 inline void init (hb_face_t *face_)
1149 inline void fini (void)
1151 if (instance && instance != &OT::Null(T))
1158 inline const T* get (void) const
1161 T *p = (T *) hb_atomic_ptr_get (&instance);
1164 p = (T *) calloc (1, sizeof (T));
1166 p = const_cast<T *> (&OT::Null(T));
1169 if (unlikely (!hb_atomic_ptr_cmpexch (const_cast<T **>(&instance), nullptr, p)))
1171 if (p != &OT::Null(T))
1179 inline const T* operator-> (void) const
1189 /* Logic is shared between hb_lazy_loader_t and hb_lazy_table_loader_t */
1190 template <typename T>
1191 struct hb_lazy_table_loader_t
1193 inline void init (hb_face_t *face_)
1200 inline void fini (void)
1202 hb_blob_destroy (blob);
1205 inline const T* get (void) const
1208 T *p = (T *) hb_atomic_ptr_get (&instance);
1211 hb_blob_t *blob_ = OT::Sanitizer<T>().sanitize (face->reference_table (T::tableTag));
1212 p = const_cast<T *>(OT::Sanitizer<T>::lock_instance (blob_));
1213 if (!hb_atomic_ptr_cmpexch (const_cast<T **>(&instance), nullptr, p))
1215 hb_blob_destroy (blob_);
1223 inline const T* operator-> (void) const
1229 mutable hb_blob_t *blob;
1231 mutable T *instance;
1235 } /* namespace OT */
1238 #endif /* HB_OPEN_TYPE_PRIVATE_HH */