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()); }
95 /* Global nul-content Null pool. Enlarge as necessary. */
96 static const void *_NullPool[32 / sizeof (void *)];
98 /* Generic template for nul-content sizeof-sized Null objects. */
99 template <typename Type>
100 static inline const Type& Null () {
101 ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool));
102 return *CastP<Type> (_NullPool);
105 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
106 #define DEFINE_NULL_DATA(Type, size, data) \
107 static const char _Null##Type[size + 1] = data; /* +1 is for nul-termination in data */ \
109 inline const Type& Null<Type> () { \
110 return *CastP<Type> (_Null##Type); \
111 } /* The following line really exists such that we end in a place needing semicolon */ \
112 ASSERT_STATIC (sizeof (Type) + 1 <= sizeof (_Null##Type))
114 /* Accessor macro. */
115 #define Null(Type) Null<Type>()
123 #ifndef HB_DEBUG_SANITIZE
124 #define HB_DEBUG_SANITIZE HB_DEBUG
127 #if HB_DEBUG_SANITIZE
129 #define TRACE_SANITIZE_ARG_DEF , unsigned int sanitize_depth HB_GNUC_UNUSED
130 #define TRACE_SANITIZE_ARG , sanitize_depth + 1
131 #define TRACE_SANITIZE_ARG_INIT , 1
132 #define TRACE_SANITIZE() \
134 if (sanitize_depth < HB_DEBUG_SANITIZE) \
135 fprintf (stderr, "SANITIZE(%p) %-*d-> %s\n", \
136 (CharP(this) == CharP(&NullPool)) ? 0 : this, \
137 sanitize_depth, sanitize_depth, \
138 __PRETTY_FUNCTION__); \
141 #define TRACE_SANITIZE_ARG_DEF
142 #define TRACE_SANITIZE_ARG
143 #define TRACE_SANITIZE_ARG_INIT
144 #define TRACE_SANITIZE() HB_STMT_START {} HB_STMT_END
147 #define SANITIZE_ARG_DEF \
148 hb_sanitize_context_t *context TRACE_SANITIZE_ARG_DEF
149 #define SANITIZE_ARG \
150 context TRACE_SANITIZE_ARG
151 #define SANITIZE_ARG_INIT \
152 &context TRACE_SANITIZE_ARG_INIT
154 typedef struct _hb_sanitize_context_t hb_sanitize_context_t;
155 struct _hb_sanitize_context_t
157 const char *start, *end;
159 unsigned int edit_count;
162 static HB_GNUC_UNUSED void
163 _hb_sanitize_init (hb_sanitize_context_t *context,
166 context->start = hb_blob_lock (blob);
167 context->end = context->start + hb_blob_get_length (blob);
168 context->writable = hb_blob_is_writable (blob);
169 context->edit_count = 0;
171 #if HB_DEBUG_SANITIZE
172 fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
173 context->blob, context->start, context->end, context->end - context->start);
177 static HB_GNUC_UNUSED void
178 _hb_sanitize_fini (hb_sanitize_context_t *context HB_GNUC_UNUSED,
181 #if HB_DEBUG_SANITIZE
182 fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
183 blob, context->start, context->end, context->edit_count);
186 hb_blob_unlock (blob);
189 static HB_GNUC_UNUSED inline bool
190 _hb_sanitize_check (SANITIZE_ARG_DEF,
194 bool ret = context->start <= base &&
195 base <= context->end &&
196 (unsigned int) (context->end - base) >= len;
198 #if HB_DEBUG_SANITIZE
199 if (sanitize_depth < HB_DEBUG_SANITIZE) \
200 fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
202 sanitize_depth, sanitize_depth,
204 context->start, context->end,
205 ret ? "pass" : "FAIL");
210 static HB_GNUC_UNUSED inline bool
211 _hb_sanitize_array (SANITIZE_ARG_DEF,
213 unsigned int record_size,
216 bool overflows = len >= ((unsigned int) -1) / record_size;
218 #if HB_DEBUG_SANITIZE
219 if (sanitize_depth < HB_DEBUG_SANITIZE) \
220 fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
222 sanitize_depth, sanitize_depth,
223 base, base + (record_size * len), record_size, len, (unsigned long) record_size * len,
224 context->start, context->end,
225 !overflows ? "does not overflow" : "OVERFLOWS FAIL");
228 return HB_LIKELY (!overflows) && _hb_sanitize_check (SANITIZE_ARG, base, record_size * len);
231 static HB_GNUC_UNUSED inline bool
232 _hb_sanitize_edit (SANITIZE_ARG_DEF,
233 const char *base HB_GNUC_UNUSED,
234 unsigned int len HB_GNUC_UNUSED)
236 context->edit_count++;
238 #if HB_DEBUG_SANITIZE
239 fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
241 sanitize_depth, sanitize_depth,
244 context->start, context->end,
245 context->writable ? "granted" : "REJECTED");
248 return context->writable;
251 #define SANITIZE(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG))
252 #define SANITIZE2(X,Y) (SANITIZE (X) && SANITIZE (Y))
254 #define SANITIZE_THIS(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG, CharP(this)))
255 #define SANITIZE_THIS2(X,Y) (SANITIZE_THIS (X) && SANITIZE_THIS (Y))
256 #define SANITIZE_THIS3(X,Y,Z) (SANITIZE_THIS (X) && SANITIZE_THIS (Y) && SANITIZE_THIS(Z))
258 #define SANITIZE_BASE(X,B) HB_LIKELY ((X).sanitize (SANITIZE_ARG, B))
259 #define SANITIZE_BASE2(X,Y,B) (SANITIZE_BASE (X,B) && SANITIZE_BASE (Y,B))
261 #define SANITIZE_SELF() SANITIZE_OBJ (*this)
262 #define SANITIZE_OBJ(X) SANITIZE_MEM(&(X), sizeof (X))
264 #define SANITIZE_MEM(B,L) HB_LIKELY (_hb_sanitize_check (SANITIZE_ARG, CharP(B), (L)))
266 #define SANITIZE_ARRAY(A,S,L) HB_LIKELY (_hb_sanitize_array (SANITIZE_ARG, CharP(A), S, L))
268 #define NEUTER(Var, Val) \
269 (SANITIZE_OBJ (Var) && \
270 _hb_sanitize_edit (SANITIZE_ARG, CharP(&(Var)), (Var).get_size ()) && \
271 ((Var).set (Val), true))
274 /* Template to sanitize an object. */
275 template <typename Type>
278 static hb_blob_t *sanitize (hb_blob_t *blob) {
279 hb_sanitize_context_t context;
282 /* TODO is_sane() stuff */
285 #if HB_DEBUG_SANITIZE
286 fprintf (stderr, "Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__);
289 _hb_sanitize_init (&context, blob);
291 /* Note: We drop const here */
292 Type *t = CastP<Type> ((void *) context.start);
294 sane = t->sanitize (SANITIZE_ARG_INIT);
296 if (context.edit_count) {
297 #if HB_DEBUG_SANITIZE
298 fprintf (stderr, "Sanitizer %p passed first round with %d edits; going a second round %s\n",
299 blob, context.edit_count, __PRETTY_FUNCTION__);
301 /* sanitize again to ensure no toe-stepping */
302 context.edit_count = 0;
303 sane = t->sanitize (SANITIZE_ARG_INIT);
304 if (context.edit_count) {
305 #if HB_DEBUG_SANITIZE
306 fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
307 blob, context.edit_count, __PRETTY_FUNCTION__);
312 _hb_sanitize_fini (&context, blob);
314 unsigned int edit_count = context.edit_count;
315 _hb_sanitize_fini (&context, blob);
316 if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
317 /* ok, we made it writable by relocating. try again */
318 #if HB_DEBUG_SANITIZE
319 fprintf (stderr, "Sanitizer %p retry %s\n", blob, __PRETTY_FUNCTION__);
325 #if HB_DEBUG_SANITIZE
326 fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", __PRETTY_FUNCTION__);
331 hb_blob_destroy (blob);
332 return hb_blob_create_empty ();
340 * The OpenType Font File: Data Types
344 /* "The following data types are used in the OpenType font file.
345 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
352 template <typename Type, int Bytes> class BEInt;
354 /* LONGTERMTODO: On machines allowing unaligned access, we can make the
355 * following tighter by using byteswap instructions on ints directly. */
356 template <typename Type>
360 inline class BEInt<Type,2>& operator = (Type i) { hb_be_uint16_put (v,i); return *this; }
361 inline operator Type () const { return hb_be_uint16_get (v); }
362 inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_cmp (v, o.v); }
363 inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
364 private: uint8_t v[2];
366 template <typename Type>
370 inline class BEInt<Type,4>& operator = (Type i) { hb_be_uint32_put (v,i); return *this; }
371 inline operator Type () const { return hb_be_uint32_get (v); }
372 inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_cmp (v, o.v); }
373 inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
374 private: uint8_t v[4];
377 /* Integer types in big-endian order and no alignment requirement */
378 template <typename Type>
381 static inline unsigned int get_size () { return sizeof (Type); }
382 inline void set (Type i) { v = i; }
383 inline operator Type(void) const { return v; }
384 inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
385 inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
386 inline bool sanitize (SANITIZE_ARG_DEF) {
388 return SANITIZE_SELF ();
390 private: BEInt<Type, sizeof (Type)> v;
393 typedef IntType<uint16_t> USHORT; /* 16-bit unsigned integer. */
394 typedef IntType<int16_t> SHORT; /* 16-bit signed integer. */
395 typedef IntType<uint32_t> ULONG; /* 32-bit unsigned integer. */
396 typedef IntType<int32_t> LONG; /* 32-bit signed integer. */
398 ASSERT_SIZE (USHORT, 2);
399 ASSERT_SIZE (SHORT, 2);
400 ASSERT_SIZE (ULONG, 4);
401 ASSERT_SIZE (LONG, 4);
403 /* Array of four uint8s (length = 32 bits) used to identify a script, language
404 * system, feature, or baseline */
407 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
408 inline operator const char* (void) const { return CharP(this); }
409 inline operator char* (void) { return CharP(this); }
411 ASSERT_SIZE (Tag, 4);
412 DEFINE_NULL_DATA (Tag, 4, " ");
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::get_size ();
432 while (Table < EndPtr)
437 ASSERT_SIZE (CheckSum, 4);
446 inline operator uint32_t (void) const { return (major << 16) + minor; }
448 inline bool sanitize (SANITIZE_ARG_DEF) {
450 return SANITIZE_SELF ();
456 ASSERT_SIZE (FixedVersion, 4);
461 * Template subclasses of Offset and LongOffset that do the dereferencing.
462 * Use: (this+memberName)
465 template <typename OffsetType, typename Type>
466 struct GenericOffsetTo : OffsetType
468 inline const Type& operator () (const void *base) const
470 unsigned int offset = *this;
471 if (HB_UNLIKELY (!offset)) return Null(Type);
472 return StructAtOffset<Type> (*CharP(base), offset);
475 inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
477 if (!SANITIZE_SELF ()) return false;
478 unsigned int offset = *this;
479 if (HB_UNLIKELY (!offset)) return true;
480 return SANITIZE (StructAtOffset<Type> (*CharP(base), offset)) || NEUTER (*this, 0);
482 inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
484 if (!SANITIZE_SELF ()) return false;
485 unsigned int offset = *this;
486 if (HB_UNLIKELY (!offset)) return true;
487 return SANITIZE_BASE (StructAtOffset<Type> (*CharP(base), offset), base2) || NEUTER (*this, 0);
489 inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
491 if (!SANITIZE_SELF ()) return false;
492 unsigned int offset = *this;
493 if (HB_UNLIKELY (!offset)) return true;
494 return SANITIZE_BASE (StructAtOffset<Type> (*CharP(base), offset), user_data) || NEUTER (*this, 0);
497 template <typename Base, typename OffsetType, typename Type>
498 inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
500 template <typename Type>
501 struct OffsetTo : GenericOffsetTo<Offset, Type> {};
503 template <typename Type>
504 struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
511 template <typename LenType, typename Type>
512 struct GenericArrayOf
514 const Type *array(void) const { return &StructAfter<Type> (len); }
515 Type *array(void) { return &StructAfter<Type> (len); }
517 const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
519 unsigned int count = len;
520 if (HB_UNLIKELY (start_offset > count))
523 count -= start_offset;
524 count = MIN (count, *pcount);
526 return array() + start_offset;
529 inline const Type& operator [] (unsigned int i) const
531 if (HB_UNLIKELY (i >= len)) return Null(Type);
534 inline unsigned int get_size () const
535 { return len.get_size () + len * Type::get_size (); }
537 inline bool sanitize_shallow (SANITIZE_ARG_DEF) {
539 return SANITIZE_SELF() && SANITIZE_ARRAY (this, Type::get_size (), len);
542 inline bool sanitize (SANITIZE_ARG_DEF) {
544 if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
545 /* Note: for structs that do not reference other structs,
546 * we do not need to call their sanitize() as we already did
547 * a bound check on the aggregate array size, hence the return.
550 /* We do keep this code though to make sure the structs pointed
551 * to do have a simple sanitize(), ie. they do not reference
553 unsigned int count = len;
554 for (unsigned int i = 0; i < count; i++)
555 if (!SANITIZE (array()[i]))
559 inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
561 if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
562 unsigned int count = len;
563 for (unsigned int i = 0; i < count; i++)
564 if (!array()[i].sanitize (SANITIZE_ARG, base))
568 inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
570 if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
571 unsigned int count = len;
572 for (unsigned int i = 0; i < count; i++)
573 if (!array()[i].sanitize (SANITIZE_ARG, base, base2))
577 inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
579 if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
580 unsigned int count = len;
581 for (unsigned int i = 0; i < count; i++)
582 if (!array()[i].sanitize (SANITIZE_ARG, base, user_data))
591 /* An array with a USHORT number of elements. */
592 template <typename Type>
593 struct ArrayOf : GenericArrayOf<USHORT, Type> {};
595 /* An array with a ULONG number of elements. */
596 template <typename Type>
597 struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
599 /* Array of Offset's */
600 template <typename Type>
601 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
603 /* Array of LongOffset's */
604 template <typename Type>
605 struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
607 /* LongArray of LongOffset's */
608 template <typename Type>
609 struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
611 /* Array of offsets relative to the beginning of the array itself. */
612 template <typename Type>
613 struct OffsetListOf : OffsetArrayOf<Type>
615 inline const Type& operator [] (unsigned int i) const
617 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
618 return this+this->array()[i];
621 inline bool sanitize (SANITIZE_ARG_DEF) {
623 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this));
625 inline bool sanitize (SANITIZE_ARG_DEF, unsigned int user_data) {
627 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this), user_data);
632 /* An array with a USHORT number of elements,
633 * starting at second element. */
634 template <typename Type>
635 struct HeadlessArrayOf
637 const Type *array(void) const { return &StructAfter<Type> (len); }
638 Type *array(void) { return &StructAfter<Type> (len); }
640 inline const Type& operator [] (unsigned int i) const
642 if (HB_UNLIKELY (i >= len || !i)) return Null(Type);
645 inline unsigned int get_size () const
646 { return len.get_size () + (len ? len - 1 : 0) * Type::get_size (); }
648 inline bool sanitize_shallow (SANITIZE_ARG_DEF) {
650 return SANITIZE_SELF() && SANITIZE_ARRAY (this, Type::get_size (), len);
653 inline bool sanitize (SANITIZE_ARG_DEF) {
655 if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
656 /* Note: for structs that do not reference other structs,
657 * we do not need to call their sanitize() as we already did
658 * a bound check on the aggregate array size, hence the return.
661 /* We do keep this code though to make sure the structs pointed
662 * to do have a simple sanitize(), ie. they do not reference
664 unsigned int count = len ? len - 1 : 0;
666 for (unsigned int i = 0; i < count; i++)
667 if (!SANITIZE (a[i]))
677 #endif /* HB_OPEN_TYPE_PRIVATE_HH */