2 * Copyright (C) 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_TYPES_PRIVATE_HH
28 #define HB_OPEN_TYPES_PRIVATE_HH
30 #include "hb-private.h"
35 /* Table/script/language-system/feature/... not found */
36 #define NO_INDEX ((unsigned int) 0xFFFF)
44 /* Cast to "const char *" and "char *" */
45 template <typename Type>
46 inline const char * CharP (const Type* X)
47 { return reinterpret_cast<const char *>(X); }
48 template <typename Type>
49 inline char * CharP (Type* X)
50 { return reinterpret_cast<char *>(X); }
52 /* Cast to struct T, reference to reference */
53 template<typename Type, typename TObject>
54 inline const Type& CastR(const TObject &X)
55 { return reinterpret_cast<const Type&> (X); }
56 template<typename Type, typename TObject>
57 inline Type& CastR(TObject &X)
58 { return reinterpret_cast<Type&> (X); }
60 /* Cast to struct T, pointer to pointer */
61 template<typename Type, typename TObject>
62 inline const Type* CastP(const TObject *X)
63 { return reinterpret_cast<const Type*> (X); }
64 template<typename Type, typename TObject>
65 inline Type* CastP(TObject *X)
66 { return reinterpret_cast<Type*> (X); }
68 /* StructAtOffset<T>(X,Ofs) returns the struct T& that is placed at memory
69 * location of X plus Ofs bytes. */
70 template<typename Type, typename TObject>
71 inline const Type& StructAtOffset(const TObject &X, unsigned int offset)
72 { return * reinterpret_cast<const Type*> (CharP(&X) + offset); }
73 template<typename Type, typename TObject>
74 inline Type& StructAtOffset(TObject &X, unsigned int offset)
75 { return * reinterpret_cast<Type*> (CharP(&X) + offset); }
77 /* StructAfter<T>(X) returns the struct T& that is placed after X.
78 * Works with X of variable size also. X must implement get_size() */
79 template<typename Type, typename TObject>
80 inline const Type& StructAfter(const TObject &X)
81 { return StructAtOffset<Type>(X, X.get_size()); }
82 template<typename Type, typename TObject>
83 inline Type& StructAfter(TObject &X)
84 { return StructAtOffset<Type>(X, X.get_size()); }
92 #define DEFINE_SIZE_STATIC(size) \
93 inline void _size_assertion (void) const \
94 { ASSERT_STATIC (sizeof (*this) == (size)); } \
95 static inline unsigned int get_size (void) { return (size); } \
96 static const unsigned int static_size = (size); \
97 static const unsigned int min_size = (size)
99 /* Size signifying variable-sized array */
103 #define DEFINE_SIZE_VAR0(size) \
104 inline void _size_assertion (void) const \
105 { ASSERT_STATIC (sizeof (*this) == (size)); } \
106 static const unsigned int min_size = (size)
108 #define DEFINE_SIZE_VAR(size, _var_type) \
109 inline void _size_assertion (void) const \
110 { ASSERT_STATIC (sizeof (*this) == (size) + VAR0 * sizeof (_var_type)); } \
111 static const unsigned int min_size = (size)
113 #define DEFINE_SIZE_VAR2(size, _var_type1, _var_type2) \
114 inline void _size_assertion (void) const \
115 { ASSERT_STATIC (sizeof (*this) == (size) + VAR0 * sizeof (_var_type1) + VAR0 * sizeof (_var_type2)); } \
116 static const unsigned int min_size = (size)
124 /* Global nul-content Null pool. Enlarge as necessary. */
125 static const void *_NullPool[32 / sizeof (void *)];
127 /* Generic template for nul-content sizeof-sized Null objects. */
128 template <typename Type>
129 static inline const Type& Null () {
130 ASSERT_STATIC (sizeof (Type) <= 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> () { \
139 return *CastP<Type> (_Null##Type); \
140 } /* The following line really exists such that we end in a place needing semicolon */ \
141 ASSERT_STATIC (sizeof (Type) + 1 <= sizeof (_Null##Type))
143 /* Accessor macro. */
144 #define Null(Type) Null<Type>()
152 template <int max_depth>
154 explicit hb_trace_t (unsigned int *pdepth) : pdepth(pdepth) { if (max_depth) ++*pdepth; }
155 ~hb_trace_t (void) { if (max_depth) --*pdepth; }
157 inline void log (const char *what, const char *function, const void *obj)
159 if (*pdepth < max_depth)
160 fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, *pdepth, *pdepth, function);
164 unsigned int *pdepth;
166 template <> /* Optimize when tracing is disabled */
167 struct hb_trace_t<0> {
168 explicit hb_trace_t (unsigned int *p) {}
169 inline void log (const char *what, const char *function, const void *obj) {};
178 #ifndef HB_DEBUG_SANITIZE
179 #define HB_DEBUG_SANITIZE HB_DEBUG+0
183 #define TRACE_SANITIZE() \
184 hb_trace_t<HB_DEBUG_SANITIZE> trace (&context->debug_depth); \
185 trace.log ("SANITIZE", HB_FUNC, this);
188 struct hb_sanitize_context_t
190 inline void init (hb_blob_t *blob)
192 this->blob = hb_blob_reference (blob);
193 this->start = hb_blob_lock (blob);
194 this->end = this->start + hb_blob_get_length (blob);
195 this->writable = hb_blob_is_writable (blob);
196 this->edit_count = 0;
197 this->debug_depth = 0;
199 if (HB_DEBUG_SANITIZE)
200 fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
201 this->blob, this->start, this->end, this->end - this->start);
204 inline void finish (void)
206 if (HB_DEBUG_SANITIZE)
207 fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
208 this->blob, this->start, this->end, this->edit_count);
210 hb_blob_unlock (this->blob);
211 hb_blob_destroy (this->blob);
213 this->start = this->end = NULL;
216 inline bool check_range (const void *base, unsigned int len) const
218 bool ret = this->start <= base &&
220 (unsigned int) (this->end - CharP(base)) >= len;
222 if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE) \
223 fprintf (stderr, "SANITIZE(%p) %-*d-> range [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
225 this->debug_depth, this->debug_depth,
226 base, CharP(base)+len, len,
227 this->start, this->end,
228 ret ? "pass" : "FAIL");
233 inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
235 bool overflows = len >= ((unsigned int) -1) / record_size;
237 if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
238 fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
240 this->debug_depth, this->debug_depth,
241 base, CharP(base) + (record_size * len), record_size, len, (unsigned long) record_size * len,
242 this->start, this->end,
243 !overflows ? "does not overflow" : "OVERFLOWS FAIL");
245 return likely (!overflows && this->check_range (base, record_size * len));
248 template <typename Type>
249 inline bool check_struct (const Type *obj) const
251 return likely (this->check_range (obj, sizeof (*obj)));
254 inline bool can_edit (const char *base HB_UNUSED, unsigned int len HB_UNUSED)
258 if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
259 fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
261 this->debug_depth, this->debug_depth,
264 this->start, this->end,
265 this->writable ? "granted" : "REJECTED");
267 return this->writable;
270 unsigned int debug_depth;
271 const char *start, *end;
273 unsigned int edit_count;
279 /* Template to sanitize an object. */
280 template <typename Type>
283 static hb_blob_t *sanitize (hb_blob_t *blob) {
284 hb_sanitize_context_t context[1] = {{0}};
287 /* TODO is_sane() stuff */
290 if (HB_DEBUG_SANITIZE)
291 fprintf (stderr, "Sanitizer %p start %s\n", blob, HB_FUNC);
293 context->init (blob);
295 Type *t = CastP<Type> (const_cast<char *> (context->start));
297 sane = t->sanitize (context);
299 if (context->edit_count) {
300 if (HB_DEBUG_SANITIZE)
301 fprintf (stderr, "Sanitizer %p passed first round with %d edits; doing a second round %s\n",
302 blob, context->edit_count, HB_FUNC);
304 /* sanitize again to ensure no toe-stepping */
305 context->edit_count = 0;
306 sane = t->sanitize (context);
307 if (context->edit_count) {
308 if (HB_DEBUG_SANITIZE)
309 fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
310 blob, context->edit_count, HB_FUNC);
316 unsigned int edit_count = context->edit_count;
318 if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
319 /* ok, we made it writable by relocating. try again */
320 if (HB_DEBUG_SANITIZE)
321 fprintf (stderr, "Sanitizer %p retry %s\n", blob, HB_FUNC);
326 if (HB_DEBUG_SANITIZE)
327 fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", HB_FUNC);
331 hb_blob_destroy (blob);
332 return hb_blob_create_empty ();
342 * The OpenType Font File: Data Types
346 /* "The following data types are used in the OpenType font file.
347 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
354 template <typename Type, int Bytes> class BEInt;
356 /* LONGTERMTODO: On machines allowing unaligned access, we can make the
357 * following tighter by using byteswap instructions on ints directly. */
358 template <typename Type>
362 inline class BEInt<Type,2>& operator = (Type i) { hb_be_uint16_put (v,i); return *this; }
363 inline operator Type () const { return hb_be_uint16_get (v); }
364 inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_cmp (v, o.v); }
365 inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
366 private: uint8_t v[2];
368 template <typename Type>
372 inline class BEInt<Type,4>& operator = (Type i) { hb_be_uint32_put (v,i); return *this; }
373 inline operator Type () const { return hb_be_uint32_get (v); }
374 inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_cmp (v, o.v); }
375 inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
376 private: uint8_t v[4];
379 /* Integer types in big-endian order and no alignment requirement */
380 template <typename Type>
383 inline void set (Type i) { v = i; }
384 inline operator Type(void) const { return v; }
385 inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
386 inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
387 inline bool sanitize (hb_sanitize_context_t *context) {
389 return context->check_struct (this);
392 BEInt<Type, sizeof (Type)> v;
394 DEFINE_SIZE_STATIC (sizeof (Type));
397 typedef IntType<uint16_t> USHORT; /* 16-bit unsigned integer. */
398 typedef IntType<int16_t> SHORT; /* 16-bit signed integer. */
399 typedef IntType<uint32_t> ULONG; /* 32-bit unsigned integer. */
400 typedef IntType<int32_t> LONG; /* 32-bit signed integer. */
402 /* Array of four uint8s (length = 32 bits) used to identify a script, language
403 * system, feature, or baseline */
406 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
407 inline operator const char* (void) const { return CharP(this); }
408 inline operator char* (void) { return CharP(this); }
410 DEFINE_SIZE_STATIC (4);
412 DEFINE_NULL_DATA (Tag, " ");
414 /* Glyph index number, same as uint16 (length = 16 bits) */
415 typedef USHORT GlyphID;
417 /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
418 typedef USHORT Offset;
420 /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
421 typedef ULONG LongOffset;
425 struct CheckSum : ULONG
427 static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
430 ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
432 while (Table < EndPtr)
437 DEFINE_SIZE_STATIC (4);
447 inline operator uint32_t (void) const { return (major << 16) + minor; }
449 inline bool sanitize (hb_sanitize_context_t *context) {
451 return context->check_struct (this);
457 DEFINE_SIZE_STATIC (4);
463 * Template subclasses of Offset and LongOffset that do the dereferencing.
467 template <typename OffsetType, typename Type>
468 struct GenericOffsetTo : OffsetType
470 inline const Type& operator () (const void *base) const
472 unsigned int offset = *this;
473 if (unlikely (!offset)) return Null(Type);
474 return StructAtOffset<Type> (*CharP(base), offset);
477 inline bool sanitize (hb_sanitize_context_t *context, void *base) {
479 if (!context->check_struct (this)) return false;
480 unsigned int offset = *this;
481 if (unlikely (!offset)) return true;
482 Type &obj = StructAtOffset<Type> (*CharP(base), offset);
483 return likely (obj.sanitize (context)) || neuter (context);
485 template <typename T>
486 inline bool sanitize (hb_sanitize_context_t *context, void *base, T user_data) {
488 if (!context->check_struct (this)) return false;
489 unsigned int offset = *this;
490 if (unlikely (!offset)) return true;
491 Type &obj = StructAtOffset<Type> (*CharP(base), offset);
492 return likely (obj.sanitize (context, user_data)) || neuter (context);
496 /* Set the offset to Null */
497 inline bool neuter (hb_sanitize_context_t *context) {
498 if (context->can_edit (CharP(this), this->static_size)) {
499 this->set (0); /* 0 is Null offset */
505 template <typename Base, typename OffsetType, typename Type>
506 inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
508 template <typename Type>
509 struct OffsetTo : GenericOffsetTo<Offset, Type> {};
511 template <typename Type>
512 struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
519 template <typename LenType, typename Type>
520 struct GenericArrayOf
522 const Type *array(void) const { return &StructAfter<Type> (len); }
523 Type *array(void) { return &StructAfter<Type> (len); }
525 const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
527 unsigned int count = len;
528 if (unlikely (start_offset > count))
531 count -= start_offset;
532 count = MIN (count, *pcount);
534 return array() + start_offset;
537 inline const Type& operator [] (unsigned int i) const
539 if (unlikely (i >= len)) return Null(Type);
542 inline unsigned int get_size () const
543 { return len.static_size + len * Type::static_size; }
545 inline bool sanitize (hb_sanitize_context_t *context) {
547 if (!likely (sanitize_shallow (context))) return false;
548 /* Note: for structs that do not reference other structs,
549 * we do not need to call their sanitize() as we already did
550 * a bound check on the aggregate array size, hence the return.
553 /* We do keep this code though to make sure the structs pointed
554 * to do have a simple sanitize(), ie. they do not reference
556 unsigned int count = len;
557 for (unsigned int i = 0; i < count; i++)
558 if (array()[i].sanitize (context))
562 inline bool sanitize (hb_sanitize_context_t *context, void *base) {
564 if (!likely (sanitize_shallow (context))) return false;
565 unsigned int count = len;
566 for (unsigned int i = 0; i < count; i++)
567 if (!array()[i].sanitize (context, base))
571 template <typename T>
572 inline bool sanitize (hb_sanitize_context_t *context, void *base, T user_data) {
574 if (!likely (sanitize_shallow (context))) return false;
575 unsigned int count = len;
576 for (unsigned int i = 0; i < count; i++)
577 if (!array()[i].sanitize (context, base, user_data))
583 inline bool sanitize_shallow (hb_sanitize_context_t *context) {
585 return context->check_struct (this)
586 && context->check_array (this, Type::static_size, len);
593 DEFINE_SIZE_VAR0 (sizeof (LenType));
596 /* An array with a USHORT number of elements. */
597 template <typename Type>
598 struct ArrayOf : GenericArrayOf<USHORT, Type> {};
600 /* An array with a ULONG number of elements. */
601 template <typename Type>
602 struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
604 /* Array of Offset's */
605 template <typename Type>
606 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
608 /* Array of LongOffset's */
609 template <typename Type>
610 struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
612 /* LongArray of LongOffset's */
613 template <typename Type>
614 struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
616 /* Array of offsets relative to the beginning of the array itself. */
617 template <typename Type>
618 struct OffsetListOf : OffsetArrayOf<Type>
620 inline const Type& operator [] (unsigned int i) const
622 if (unlikely (i >= this->len)) return Null(Type);
623 return this+this->array()[i];
626 inline bool sanitize (hb_sanitize_context_t *context) {
628 return OffsetArrayOf<Type>::sanitize (context, CharP(this));
630 template <typename T>
631 inline bool sanitize (hb_sanitize_context_t *context, T user_data) {
633 return OffsetArrayOf<Type>::sanitize (context, CharP(this), user_data);
638 /* An array with a USHORT number of elements,
639 * starting at second element. */
640 template <typename Type>
641 struct HeadlessArrayOf
643 const Type *array(void) const { return &StructAfter<Type> (len); }
644 Type *array(void) { return &StructAfter<Type> (len); }
646 inline const Type& operator [] (unsigned int i) const
648 if (unlikely (i >= len || !i)) return Null(Type);
651 inline unsigned int get_size () const
652 { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
654 inline bool sanitize_shallow (hb_sanitize_context_t *context) {
655 return context->check_struct (this)
656 && context->check_array (this, Type::static_size, len);
659 inline bool sanitize (hb_sanitize_context_t *context) {
661 if (!likely (sanitize_shallow (context))) return false;
662 /* Note: for structs that do not reference other structs,
663 * we do not need to call their sanitize() as we already did
664 * a bound check on the aggregate array size, hence the return.
667 /* We do keep this code though to make sure the structs pointed
668 * to do have a simple sanitize(), ie. they do not reference
670 unsigned int count = len ? len - 1 : 0;
672 for (unsigned int i = 0; i < count; i++)
673 if (!a[i].sanitize (context))
683 #endif /* HB_OPEN_TYPE_PRIVATE_HH */