2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Red Hat Author(s): Behdad Esfahbod
27 #ifndef HB_OPEN_TYPE_PRIVATE_HH
28 #define HB_OPEN_TYPE_PRIVATE_HH
30 #include "hb-private.hh"
42 /* Cast to struct T, reference to reference */
43 template<typename Type, typename TObject>
44 inline const Type& CastR(const TObject &X)
45 { return reinterpret_cast<const Type&> (X); }
46 template<typename Type, typename TObject>
47 inline Type& CastR(TObject &X)
48 { return reinterpret_cast<Type&> (X); }
50 /* Cast to struct T, pointer to pointer */
51 template<typename Type, typename TObject>
52 inline const Type* CastP(const TObject *X)
53 { return reinterpret_cast<const Type*> (X); }
54 template<typename Type, typename TObject>
55 inline Type* CastP(TObject *X)
56 { return reinterpret_cast<Type*> (X); }
58 /* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
59 * location pointed to by P plus Ofs bytes. */
60 template<typename Type>
61 inline const Type& StructAtOffset(const void *P, unsigned int offset)
62 { return * reinterpret_cast<const Type*> ((const char *) P + offset); }
63 template<typename Type>
64 inline Type& StructAtOffset(void *P, unsigned int offset)
65 { return * reinterpret_cast<Type*> ((char *) P + offset); }
67 /* StructAfter<T>(X) returns the struct T& that is placed after X.
68 * Works with X of variable size also. X must implement get_size() */
69 template<typename Type, typename TObject>
70 inline const Type& StructAfter(const TObject &X)
71 { return StructAtOffset<Type>(&X, X.get_size()); }
72 template<typename Type, typename TObject>
73 inline Type& StructAfter(TObject &X)
74 { return StructAtOffset<Type>(&X, X.get_size()); }
82 /* Check _assertion in a method environment */
83 #define _DEFINE_SIZE_ASSERTION(_assertion) \
84 inline void _size_assertion (void) const \
85 { ASSERT_STATIC (_assertion); }
86 /* Check that _code compiles in a method environment */
87 #define _DEFINE_COMPILES_ASSERTION(_code) \
88 inline void _compiles_assertion (void) const \
92 #define DEFINE_SIZE_STATIC(size) \
93 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size)); \
94 static const unsigned int static_size = (size); \
95 static const unsigned int min_size = (size)
97 /* Size signifying variable-sized array */
100 #define DEFINE_SIZE_UNION(size, _member) \
101 _DEFINE_SIZE_ASSERTION (this->u._member.static_size == (size)); \
102 static const unsigned int min_size = (size)
104 #define DEFINE_SIZE_MIN(size) \
105 _DEFINE_SIZE_ASSERTION (sizeof (*this) >= (size)); \
106 static const unsigned int min_size = (size)
108 #define DEFINE_SIZE_ARRAY(size, array) \
109 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \
110 _DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \
111 static const unsigned int min_size = (size)
113 #define DEFINE_SIZE_ARRAY2(size, array1, array2) \
114 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \
115 _DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].static_size) \
116 static const unsigned int min_size = (size)
124 /* Global nul-content Null pool. Enlarge as necessary. */
125 static const void *_NullPool[64 / sizeof (void *)];
127 /* Generic nul-content Null objects. */
128 template <typename Type>
129 static inline const Type& Null (void) {
130 ASSERT_STATIC (Type::min_size <= sizeof (_NullPool));
131 return *CastP<Type> (_NullPool);
134 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
135 #define DEFINE_NULL_DATA(Type, data) \
136 static const char _Null##Type[Type::min_size + 1] = data; /* +1 is for nul-termination in data */ \
138 inline const Type& Null<Type> (void) { \
139 return *CastP<Type> (_Null##Type); \
140 } /* The following line really exists such that we end in a place needing semicolon */ \
141 ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type))
143 /* Accessor macro. */
144 #define Null(Type) Null<Type>()
152 #ifndef HB_DEBUG_SANITIZE
153 #define HB_DEBUG_SANITIZE (HB_DEBUG+0)
157 #define TRACE_SANITIZE() \
158 hb_auto_trace_t<HB_DEBUG_SANITIZE> trace (&c->debug_depth, "SANITIZE", this, NULL, HB_FUNC);
161 struct hb_sanitize_context_t
163 inline void init (hb_blob_t *b)
165 this->blob = hb_blob_reference (b);
166 this->writable = false;
169 inline void setup (void)
171 this->start = hb_blob_get_data (this->blob, NULL);
172 this->end = this->start + hb_blob_get_length (this->blob);
173 this->edit_count = 0;
174 this->debug_depth = 0;
176 DEBUG_MSG (SANITIZE, this->blob,
177 "init [%p..%p] (%lu bytes)",
178 this->start, this->end,
179 (unsigned long) (this->end - this->start));
182 inline void finish (void)
184 DEBUG_MSG (SANITIZE, this->blob,
185 "fini [%p..%p] %u edit requests",
186 this->start, this->end, this->edit_count);
188 hb_blob_destroy (this->blob);
190 this->start = this->end = NULL;
193 inline bool check_range (const void *base, unsigned int len) const
195 const char *p = (const char *) base;
196 bool ret = this->start <= p &&
198 (unsigned int) (this->end - p) >= len;
200 DEBUG_MSG_LEVEL (SANITIZE, this->blob, this->debug_depth,
201 "%-*d-> range [%p..%p] (%d bytes) in [%p..%p] -> %s",
202 this->debug_depth, this->debug_depth,
204 this->start, this->end,
205 ret ? "pass" : "FAIL");
210 inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
212 const char *p = (const char *) base;
213 bool overflows = _hb_unsigned_int_mul_overflows (len, record_size);
215 DEBUG_MSG_LEVEL (SANITIZE, this->blob, this->debug_depth,
216 "%-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s",
217 this->debug_depth, this->debug_depth,
218 p, p + (record_size * len), record_size, len, (unsigned long) record_size * len,
219 this->start, this->end,
220 !overflows ? "does not overflow" : "OVERFLOWS FAIL");
222 return likely (!overflows && this->check_range (base, record_size * len));
225 template <typename Type>
226 inline bool check_struct (const Type *obj) const
228 return likely (this->check_range (obj, obj->min_size));
231 inline bool can_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
233 const char *p = (const char *) base;
236 DEBUG_MSG_LEVEL (SANITIZE, this->blob, this->debug_depth,
237 "%-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s",
238 this->debug_depth, this->debug_depth,
241 this->start, this->end,
242 this->writable ? "granted" : "REJECTED");
244 return this->writable;
247 unsigned int debug_depth;
248 const char *start, *end;
250 unsigned int edit_count;
256 /* Template to sanitize an object. */
257 template <typename Type>
260 static hb_blob_t *sanitize (hb_blob_t *blob) {
261 hb_sanitize_context_t c[1] = {{0}};
264 /* TODO is_sane() stuff */
269 DEBUG_MSG_FUNC (SANITIZE, blob, "start");
273 if (unlikely (!c->start)) {
278 Type *t = CastP<Type> (const_cast<char *> (c->start));
280 sane = t->sanitize (c);
283 DEBUG_MSG_FUNC (SANITIZE, blob, "passed first round with %d edits; going for second round", c->edit_count);
285 /* sanitize again to ensure no toe-stepping */
287 sane = t->sanitize (c);
289 DEBUG_MSG_FUNC (SANITIZE, blob, "requested %d edits in second round; FAILLING", c->edit_count);
294 unsigned int edit_count = c->edit_count;
295 if (edit_count && !c->writable) {
296 c->start = hb_blob_get_data_writable (blob, NULL);
297 c->end = c->start + hb_blob_get_length (blob);
301 /* ok, we made it writable by relocating. try again */
302 DEBUG_MSG_FUNC (SANITIZE, blob, "retry");
310 DEBUG_MSG_FUNC (SANITIZE, blob, sane ? "PASSED" : "FAILED");
314 hb_blob_destroy (blob);
315 return hb_blob_get_empty ();
319 static const Type* lock_instance (hb_blob_t *blob) {
320 hb_blob_make_immutable (blob);
321 const char *base = hb_blob_get_data (blob, NULL);
322 return unlikely (!base) ? &Null(Type) : CastP<Type> (base);
331 * The OpenType Font File: Data Types
335 /* "The following data types are used in the OpenType font file.
336 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
343 template <typename Type, int Bytes> class BEInt;
345 /* LONGTERMTODO: On machines allowing unaligned access, we can make the
346 * following tighter by using byteswap instructions on ints directly. */
347 template <typename Type>
351 inline void set (Type i) { hb_be_uint16_put (v,i); }
352 inline operator Type (void) const { return hb_be_uint16_get (v); }
353 inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_eq (v, o.v); }
354 inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
355 private: uint8_t v[2];
357 template <typename Type>
361 inline void set (Type i) { hb_be_uint32_put (v,i); }
362 inline operator Type (void) const { return hb_be_uint32_get (v); }
363 inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_eq (v, o.v); }
364 inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
365 private: uint8_t v[4];
368 /* Integer types in big-endian order and no alignment requirement */
369 template <typename Type>
372 inline void set (Type i) { v.set (i); }
373 inline operator Type(void) const { return v; }
374 inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
375 inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
376 inline int cmp (Type a) const { Type b = v; return a < b ? -1 : a == b ? 0 : +1; }
377 inline bool sanitize (hb_sanitize_context_t *c) {
379 return likely (c->check_struct (this));
382 BEInt<Type, sizeof (Type)> v;
384 DEFINE_SIZE_STATIC (sizeof (Type));
387 typedef IntType<uint16_t> USHORT; /* 16-bit unsigned integer. */
388 typedef IntType<int16_t> SHORT; /* 16-bit signed integer. */
389 typedef IntType<uint32_t> ULONG; /* 32-bit unsigned integer. */
390 typedef IntType<int32_t> LONG; /* 32-bit signed integer. */
392 /* Date represented in number of seconds since 12:00 midnight, January 1,
393 * 1904. The value is represented as a signed 64-bit integer. */
396 inline bool sanitize (hb_sanitize_context_t *c) {
398 return likely (c->check_struct (this));
404 DEFINE_SIZE_STATIC (8);
407 /* Array of four uint8s (length = 32 bits) used to identify a script, language
408 * system, feature, or baseline */
411 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
412 inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); }
413 inline operator char* (void) { return reinterpret_cast<char *> (&this->v); }
415 DEFINE_SIZE_STATIC (4);
417 DEFINE_NULL_DATA (Tag, " ");
419 /* Glyph index number, same as uint16 (length = 16 bits) */
420 typedef USHORT GlyphID;
422 /* Script/language-system/feature index */
423 struct Index : USHORT {
424 static const unsigned int NOT_FOUND_INDEX = 0xFFFF;
426 DEFINE_NULL_DATA (Index, "\xff\xff");
428 /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
429 typedef USHORT Offset;
431 /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
432 typedef ULONG LongOffset;
436 struct CheckSum : ULONG
438 static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
441 ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
443 while (Table < EndPtr)
448 DEFINE_SIZE_STATIC (4);
458 inline uint32_t to_int (void) const { return (major << 16) + minor; }
460 inline bool sanitize (hb_sanitize_context_t *c) {
462 return c->check_struct (this);
468 DEFINE_SIZE_STATIC (4);
474 * Template subclasses of Offset and LongOffset that do the dereferencing.
478 template <typename OffsetType, typename Type>
479 struct GenericOffsetTo : OffsetType
481 inline const Type& operator () (const void *base) const
483 unsigned int offset = *this;
484 if (unlikely (!offset)) return Null(Type);
485 return StructAtOffset<Type> (base, offset);
488 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
490 if (unlikely (!c->check_struct (this))) return false;
491 unsigned int offset = *this;
492 if (unlikely (!offset)) return true;
493 Type &obj = StructAtOffset<Type> (base, offset);
494 return likely (obj.sanitize (c)) || neuter (c);
496 template <typename T>
497 inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) {
499 if (unlikely (!c->check_struct (this))) return false;
500 unsigned int offset = *this;
501 if (unlikely (!offset)) return true;
502 Type &obj = StructAtOffset<Type> (base, offset);
503 return likely (obj.sanitize (c, user_data)) || neuter (c);
507 /* Set the offset to Null */
508 inline bool neuter (hb_sanitize_context_t *c) {
509 if (c->can_edit (this, this->static_size)) {
510 this->set (0); /* 0 is Null offset */
516 template <typename Base, typename OffsetType, typename Type>
517 inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
519 template <typename Type>
520 struct OffsetTo : GenericOffsetTo<Offset, Type> {};
522 template <typename Type>
523 struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
530 template <typename LenType, typename Type>
531 struct GenericArrayOf
533 const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
535 unsigned int count = len;
536 if (unlikely (start_offset > count))
539 count -= start_offset;
540 count = MIN (count, *pcount);
542 return array + start_offset;
545 inline const Type& operator [] (unsigned int i) const
547 if (unlikely (i >= len)) return Null(Type);
550 inline unsigned int get_size (void) const
551 { return len.static_size + len * Type::static_size; }
553 inline bool sanitize (hb_sanitize_context_t *c) {
555 if (unlikely (!sanitize_shallow (c))) return false;
557 /* Note: for structs that do not reference other structs,
558 * we do not need to call their sanitize() as we already did
559 * a bound check on the aggregate array size. We just include
560 * a small unreachable expression to make sure the structs
561 * pointed to do have a simple sanitize(), ie. they do not
562 * reference other structs via offsets.
564 (void) (false && array[0].sanitize (c));
568 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
570 if (unlikely (!sanitize_shallow (c))) return false;
571 unsigned int count = len;
572 for (unsigned int i = 0; i < count; i++)
573 if (unlikely (!array[i].sanitize (c, base)))
577 template <typename T>
578 inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) {
580 if (unlikely (!sanitize_shallow (c))) return false;
581 unsigned int count = len;
582 for (unsigned int i = 0; i < count; i++)
583 if (unlikely (!array[i].sanitize (c, base, user_data)))
589 inline bool sanitize_shallow (hb_sanitize_context_t *c) {
591 return c->check_struct (this)
592 && c->check_array (this, Type::static_size, len);
599 DEFINE_SIZE_ARRAY (sizeof (LenType), array);
602 /* An array with a USHORT number of elements. */
603 template <typename Type>
604 struct ArrayOf : GenericArrayOf<USHORT, Type> {};
606 /* An array with a ULONG number of elements. */
607 template <typename Type>
608 struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
610 /* Array of Offset's */
611 template <typename Type>
612 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
614 /* Array of LongOffset's */
615 template <typename Type>
616 struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
618 /* LongArray of LongOffset's */
619 template <typename Type>
620 struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
622 /* Array of offsets relative to the beginning of the array itself. */
623 template <typename Type>
624 struct OffsetListOf : OffsetArrayOf<Type>
626 inline const Type& operator [] (unsigned int i) const
628 if (unlikely (i >= this->len)) return Null(Type);
629 return this+this->array[i];
632 inline bool sanitize (hb_sanitize_context_t *c) {
634 return OffsetArrayOf<Type>::sanitize (c, this);
636 template <typename T>
637 inline bool sanitize (hb_sanitize_context_t *c, T user_data) {
639 return OffsetArrayOf<Type>::sanitize (c, this, user_data);
644 /* An array with a USHORT number of elements,
645 * starting at second element. */
646 template <typename Type>
647 struct HeadlessArrayOf
649 inline const Type& operator [] (unsigned int i) const
651 if (unlikely (i >= len || !i)) return Null(Type);
654 inline unsigned int get_size (void) const
655 { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
657 inline bool sanitize_shallow (hb_sanitize_context_t *c) {
658 return c->check_struct (this)
659 && c->check_array (this, Type::static_size, len);
662 inline bool sanitize (hb_sanitize_context_t *c) {
664 if (unlikely (!sanitize_shallow (c))) return false;
666 /* Note: for structs that do not reference other structs,
667 * we do not need to call their sanitize() as we already did
668 * a bound check on the aggregate array size. We just include
669 * a small unreachable expression to make sure the structs
670 * pointed to do have a simple sanitize(), ie. they do not
671 * reference other structs via offsets.
673 (void) (false && array[0].sanitize (c));
681 DEFINE_SIZE_ARRAY (sizeof (USHORT), array);
685 /* An array with sorted elements. Supports binary searching. */
686 template <typename Type>
687 struct SortedArrayOf : ArrayOf<Type> {
689 template <typename SearchType>
690 inline int search (const SearchType &x) const {
692 public: static int cmp (const SearchType *a, const Type *b) { return b->cmp (*a); }
694 const Type *p = (const Type *) bsearch (&x, this->array, this->len, sizeof (this->array[0]), (hb_compare_func_t) Cmp::cmp);
695 return p ? p - this->array : -1;
703 #endif /* HB_OPEN_TYPE_PRIVATE_HH */