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)
43 /* Cast to "const char *" and "char *" */
44 template <typename Type> inline const char * CharP (const Type* X) { return reinterpret_cast<const char *>(X); }
45 template <typename Type> inline char * CharP (Type* X) { return reinterpret_cast<char *>(X); }
47 #define CONST_CAST(T,X,Ofs) (*(reinterpret_cast<const T *>(CharP(&(X)) + Ofs)))
48 #define CAST(T,X,Ofs) (*(reinterpret_cast<T *>(CharP(&(X)) + Ofs)))
51 /* StructAfter<T>(X) returns the struct T& that is placed after X.
52 * Works with X of variable size also. X must implement get_size() */
53 template<typename Type, typename TObject>
54 inline const Type& StructAfter(const TObject &X)
56 return * reinterpret_cast<const Type*> (CharP (&X) + X.get_size());
58 template<typename Type, typename TObject>
59 inline Type& StructAfter(TObject &X)
61 return * reinterpret_cast<Type*> (CharP (&X) + X.get_size());
72 /* Global nul-content Null pool. Enlarge as necessary. */
73 static const void *_NullPool[32 / sizeof (void *)];
75 /* Generic template for nul-content sizeof-sized Null objects. */
76 template <typename Type>
77 static inline const Type& Null () {
78 ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool));
79 return CONST_CAST (Type, *_NullPool, 0);
82 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
83 #define DEFINE_NULL_DATA(Type, size, data) \
84 static const char _Null##Type[size + 1] = data; /* +1 is for nul-termination in data */ \
86 inline const Type& Null<Type> () { \
87 return CONST_CAST (Type, *_Null##Type, 0); \
88 } /* The following line really exists such that we end in a place needing semicolon */ \
89 ASSERT_STATIC (sizeof (Type) + 1 <= sizeof (_Null##Type))
92 #define Null(Type) Null<Type>()
95 /* get_for_data() is a static class method returning a reference to an
96 * instance of Type located at the input data location. It's just a
97 * fancy, NULL-safe, cast! */
98 #define STATIC_DEFINE_GET_FOR_DATA(Type) \
99 static inline const Type& get_for_data (const char *data) \
101 if (HB_UNLIKELY (data == NULL)) return Null(Type); \
102 return CONST_CAST (Type, *data, 0); \
104 /* Like get_for_data(), but checks major version first. */
105 #define STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION(Type, MajorMin, MajorMax) \
106 static inline const Type& get_for_data (const char *data) \
108 if (HB_UNLIKELY (data == NULL)) return Null(Type); \
109 const Type& t = CONST_CAST (Type, *data, 0); \
110 if (HB_UNLIKELY (t.version.major < MajorMin || t.version.major > MajorMax)) return Null(Type); \
119 #ifndef HB_DEBUG_SANITIZE
120 #define HB_DEBUG_SANITIZE HB_DEBUG
123 #if HB_DEBUG_SANITIZE
125 #define TRACE_SANITIZE_ARG_DEF , unsigned int sanitize_depth HB_GNUC_UNUSED
126 #define TRACE_SANITIZE_ARG , sanitize_depth + 1
127 #define TRACE_SANITIZE_ARG_INIT , 1
128 #define TRACE_SANITIZE() \
130 if (sanitize_depth < HB_DEBUG_SANITIZE) \
131 fprintf (stderr, "SANITIZE(%p) %-*d-> %s\n", \
132 (CharP (this) == CharP (&NullPool)) ? 0 : this, \
133 sanitize_depth, sanitize_depth, \
134 __PRETTY_FUNCTION__); \
137 #define TRACE_SANITIZE_ARG_DEF
138 #define TRACE_SANITIZE_ARG
139 #define TRACE_SANITIZE_ARG_INIT
140 #define TRACE_SANITIZE() HB_STMT_START {} HB_STMT_END
143 #define SANITIZE_ARG_DEF \
144 hb_sanitize_context_t *context TRACE_SANITIZE_ARG_DEF
145 #define SANITIZE_ARG \
146 context TRACE_SANITIZE_ARG
147 #define SANITIZE_ARG_INIT \
148 &context TRACE_SANITIZE_ARG_INIT
150 typedef struct _hb_sanitize_context_t hb_sanitize_context_t;
151 struct _hb_sanitize_context_t
153 const char *start, *end;
158 static HB_GNUC_UNUSED void
159 _hb_sanitize_init (hb_sanitize_context_t *context,
162 context->blob = blob;
163 context->start = hb_blob_lock (blob);
164 context->end = context->start + hb_blob_get_length (blob);
165 context->edit_count = 0;
167 #if HB_DEBUG_SANITIZE
168 fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
169 context->blob, context->start, context->end, context->end - context->start);
173 static HB_GNUC_UNUSED void
174 _hb_sanitize_fini (hb_sanitize_context_t *context,
177 #if HB_DEBUG_SANITIZE
178 fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
179 context->blob, context->start, context->end, context->edit_count);
183 hb_blob_unlock (context->blob);
186 static HB_GNUC_UNUSED inline bool
187 _hb_sanitize_check (SANITIZE_ARG_DEF,
191 bool ret = context->start <= base &&
192 base <= context->end &&
193 (unsigned int) (context->end - base) >= len;
195 #if HB_DEBUG_SANITIZE
196 if (sanitize_depth < HB_DEBUG_SANITIZE) \
197 fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
199 sanitize_depth, sanitize_depth,
201 context->start, context->end,
202 ret ? "pass" : "FAIL");
207 static HB_GNUC_UNUSED inline bool
208 _hb_sanitize_array (SANITIZE_ARG_DEF,
210 unsigned int record_size,
213 bool overflows = len >= ((unsigned int) -1) / record_size;
215 #if HB_DEBUG_SANITIZE
216 if (sanitize_depth < HB_DEBUG_SANITIZE) \
217 fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
219 sanitize_depth, sanitize_depth,
220 base, base + (record_size * len), record_size, len, (unsigned long) record_size * len,
221 context->start, context->end,
222 !overflows ? "does not overflow" : "OVERFLOWS FAIL");
224 return HB_LIKELY (!overflows) && _hb_sanitize_check (SANITIZE_ARG, base, record_size * len);
227 static HB_GNUC_UNUSED inline bool
228 _hb_sanitize_edit (SANITIZE_ARG_DEF,
229 const char *base HB_GNUC_UNUSED,
230 unsigned int len HB_GNUC_UNUSED)
232 bool perm = hb_blob_try_writable_inplace (context->blob);
233 context->edit_count++;
235 #if HB_DEBUG_SANITIZE
236 fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
238 sanitize_depth, sanitize_depth,
241 context->start, context->end,
242 perm ? "granted" : "REJECTED");
247 #define SANITIZE(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG))
248 #define SANITIZE2(X,Y) (SANITIZE (X) && SANITIZE (Y))
250 #define SANITIZE_THIS(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG, CharP(this)))
251 #define SANITIZE_THIS2(X,Y) (SANITIZE_THIS (X) && SANITIZE_THIS (Y))
252 #define SANITIZE_THIS3(X,Y,Z) (SANITIZE_THIS (X) && SANITIZE_THIS (Y) && SANITIZE_THIS(Z))
254 #define SANITIZE_BASE(X,B) HB_LIKELY ((X).sanitize (SANITIZE_ARG, B))
255 #define SANITIZE_BASE2(X,Y,B) (SANITIZE_BASE (X,B) && SANITIZE_BASE (Y,B))
257 #define SANITIZE_SELF() SANITIZE_OBJ (*this)
258 #define SANITIZE_OBJ(X) SANITIZE_MEM(&(X), sizeof (X))
260 #define SANITIZE_MEM(B,L) HB_LIKELY (_hb_sanitize_check (SANITIZE_ARG, CharP(B), (L)))
262 #define SANITIZE_ARRAY(A,S,L) HB_LIKELY (_hb_sanitize_array (SANITIZE_ARG, CharP(A), S, L))
264 #define NEUTER(Var, Val) \
265 (SANITIZE_OBJ (Var) && \
266 _hb_sanitize_edit (SANITIZE_ARG, CharP(&(Var)), (Var).get_size ()) && \
267 ((Var).set (Val), true))
270 /* Template to sanitize an object. */
271 template <typename Type>
274 static hb_blob_t *sanitize (hb_blob_t *blob) {
275 hb_sanitize_context_t context;
278 /* TODO is_sane() stuff */
281 #if HB_DEBUG_SANITIZE
282 fprintf (stderr, "Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__);
285 _hb_sanitize_init (&context, blob);
287 /* We drop const here */
288 Type *t = &CAST (Type, * (char *) CharP(context.start), 0);
290 sane = t->sanitize (SANITIZE_ARG_INIT);
292 if (context.edit_count) {
293 #if HB_DEBUG_SANITIZE
294 fprintf (stderr, "Sanitizer %p passed first round with %d edits; going a second round %s\n",
295 blob, context.edit_count, __PRETTY_FUNCTION__);
297 /* sanitize again to ensure no toe-stepping */
298 context.edit_count = 0;
299 sane = t->sanitize (SANITIZE_ARG_INIT);
300 if (context.edit_count) {
301 #if HB_DEBUG_SANITIZE
302 fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
303 blob, context.edit_count, __PRETTY_FUNCTION__);
308 _hb_sanitize_fini (&context, true);
310 unsigned int edit_count = context.edit_count;
311 _hb_sanitize_fini (&context, true);
312 if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
313 /* ok, we made it writable by relocating. try again */
314 #if HB_DEBUG_SANITIZE
315 fprintf (stderr, "Sanitizer %p retry %s\n", blob, __PRETTY_FUNCTION__);
321 #if HB_DEBUG_SANITIZE
322 fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", __PRETTY_FUNCTION__);
327 hb_blob_destroy (blob);
328 return hb_blob_create_empty ();
332 static const Type& lock_instance (hb_blob_t *blob) {
333 return Type::get_for_data (hb_blob_lock (blob));
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 template <typename Type>
358 inline class BEInt<Type,2>& operator = (Type i) { hb_be_uint16_put (v,i); return *this; }
359 inline operator Type () const { return hb_be_uint16_get (v); }
360 inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_cmp (v, o.v); }
361 inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
362 private: uint8_t v[2];
364 template <typename Type>
368 inline class BEInt<Type,4>& operator = (Type i) { hb_be_uint32_put (v,i); return *this; }
369 inline operator Type () const { return hb_be_uint32_get (v); }
370 inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_cmp (v, o.v); }
371 inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
372 private: uint8_t v[4];
375 /* Integer types in big-endian order and no alignment requirement */
376 template <typename Type>
379 static inline unsigned int get_size () { return sizeof (Type); }
380 inline void set (Type i) { v = i; }
381 inline operator Type(void) const { return v; }
382 inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
383 inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
384 inline bool sanitize (SANITIZE_ARG_DEF) {
386 return SANITIZE_SELF ();
388 private: BEInt<Type, sizeof (Type)> v;
391 typedef IntType<uint16_t> USHORT; /* 16-bit unsigned integer. */
392 typedef IntType<int16_t> SHORT; /* 16-bit signed integer. */
393 typedef IntType<uint32_t> ULONG; /* 32-bit unsigned integer. */
394 typedef IntType<int32_t> LONG; /* 32-bit signed integer. */
396 ASSERT_SIZE (USHORT, 2);
397 ASSERT_SIZE (SHORT, 2);
398 ASSERT_SIZE (ULONG, 4);
399 ASSERT_SIZE (LONG, 4);
401 /* Array of four uint8s (length = 32 bits) used to identify a script, language
402 * system, feature, or baseline */
405 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
406 inline operator const char* (void) const { return CharP(this); }
407 inline operator char* (void) { return CharP(this); }
409 ASSERT_SIZE (Tag, 4);
410 DEFINE_NULL_DATA (Tag, 4, " ");
412 /* Glyph index number, same as uint16 (length = 16 bits) */
413 typedef USHORT GlyphID;
415 /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
416 typedef USHORT Offset;
418 /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
419 typedef ULONG LongOffset;
423 struct CheckSum : ULONG
425 static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
428 ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::get_size ();
430 while (Table < EndPtr)
435 ASSERT_SIZE (CheckSum, 4);
444 inline operator uint32_t (void) const { return (major << 16) + minor; }
446 inline bool sanitize (SANITIZE_ARG_DEF) {
448 return SANITIZE_SELF ();
454 ASSERT_SIZE (FixedVersion, 4);
459 * Template subclasses of Offset and LongOffset that do the dereferencing.
460 * Use: (this+memberName)
463 template <typename OffsetType, typename Type>
464 struct GenericOffsetTo : OffsetType
466 inline const Type& operator () (const void *base) const
468 unsigned int offset = *this;
469 if (HB_UNLIKELY (!offset)) return Null(Type);
470 return CONST_CAST(Type, *CharP(base), offset);
473 inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
475 if (!SANITIZE_SELF ()) return false;
476 unsigned int offset = *this;
477 if (HB_UNLIKELY (!offset)) return true;
478 return SANITIZE (CAST(Type, *CharP(base), offset)) || NEUTER (*this, 0);
480 inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
482 if (!SANITIZE_SELF ()) return false;
483 unsigned int offset = *this;
484 if (HB_UNLIKELY (!offset)) return true;
485 return SANITIZE_BASE (CAST(Type, *CharP(base), offset), base2) || NEUTER (*this, 0);
487 inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
489 if (!SANITIZE_SELF ()) return false;
490 unsigned int offset = *this;
491 if (HB_UNLIKELY (!offset)) return true;
492 return SANITIZE_BASE (CAST(Type, *CharP(base), offset), user_data) || NEUTER (*this, 0);
495 template <typename Base, typename OffsetType, typename Type>
496 inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
498 template <typename Type>
499 struct OffsetTo : GenericOffsetTo<Offset, Type> {};
501 template <typename Type>
502 struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
509 template <typename LenType, typename Type>
510 struct GenericArrayOf
512 const Type *array(void) const { return &StructAfter<Type> (len); }
513 Type *array(void) { return &StructAfter<Type> (len); }
515 const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
517 unsigned int count = len;
518 if (HB_UNLIKELY (start_offset > count))
521 count -= start_offset;
522 count = MIN (count, *pcount);
524 return array() + start_offset;
527 inline const Type& operator [] (unsigned int i) const
529 if (HB_UNLIKELY (i >= len)) return Null(Type);
532 inline unsigned int get_size () const
533 { return len.get_size () + len * Type::get_size (); }
535 inline bool sanitize_shallow (SANITIZE_ARG_DEF) {
537 return SANITIZE_SELF() && SANITIZE_ARRAY (this, Type::get_size (), len);
540 inline bool sanitize (SANITIZE_ARG_DEF) {
542 if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
543 /* Note: for structs that do not reference other structs,
544 * we do not need to call their sanitize() as we already did
545 * a bound check on the aggregate array size, hence the return.
548 /* We do keep this code though to make sure the structs pointed
549 * to do have a simple sanitize(), ie. they do not reference
551 unsigned int count = len;
552 for (unsigned int i = 0; i < count; i++)
553 if (!SANITIZE (array()[i]))
557 inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
559 if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
560 unsigned int count = len;
561 for (unsigned int i = 0; i < count; i++)
562 if (!array()[i].sanitize (SANITIZE_ARG, base))
566 inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
568 if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
569 unsigned int count = len;
570 for (unsigned int i = 0; i < count; i++)
571 if (!array()[i].sanitize (SANITIZE_ARG, base, base2))
575 inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
577 if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
578 unsigned int count = len;
579 for (unsigned int i = 0; i < count; i++)
580 if (!array()[i].sanitize (SANITIZE_ARG, base, user_data))
589 /* An array with a USHORT number of elements. */
590 template <typename Type>
591 struct ArrayOf : GenericArrayOf<USHORT, Type> {};
593 /* An array with a ULONG number of elements. */
594 template <typename Type>
595 struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
597 /* Array of Offset's */
598 template <typename Type>
599 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
601 /* Array of LongOffset's */
602 template <typename Type>
603 struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
605 /* LongArray of LongOffset's */
606 template <typename Type>
607 struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
609 /* Array of offsets relative to the beginning of the array itself. */
610 template <typename Type>
611 struct OffsetListOf : OffsetArrayOf<Type>
613 inline const Type& operator [] (unsigned int i) const
615 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
616 return this+this->array()[i];
619 inline bool sanitize (SANITIZE_ARG_DEF) {
621 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this));
623 inline bool sanitize (SANITIZE_ARG_DEF, unsigned int user_data) {
625 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this), user_data);
630 /* An array with a USHORT number of elements,
631 * starting at second element. */
632 template <typename Type>
633 struct HeadlessArrayOf
635 const Type *array(void) const { return &StructAfter<Type> (len); }
636 Type *array(void) { return &StructAfter<Type> (len); }
638 inline const Type& operator [] (unsigned int i) const
640 if (HB_UNLIKELY (i >= len || !i)) return Null(Type);
643 inline unsigned int get_size () const
644 { return len.get_size () + (len ? len - 1 : 0) * Type::get_size (); }
646 inline bool sanitize_shallow (SANITIZE_ARG_DEF) {
648 return SANITIZE_SELF() && SANITIZE_ARRAY (this, Type::get_size (), len);
651 inline bool sanitize (SANITIZE_ARG_DEF) {
653 if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
654 /* Note: for structs that do not reference other structs,
655 * we do not need to call their sanitize() as we already did
656 * a bound check on the aggregate array size, hence the return.
659 /* We do keep this code though to make sure the structs pointed
660 * to do have a simple sanitize(), ie. they do not reference
662 unsigned int count = len ? len - 1 : 0;
664 for (unsigned int i = 0; i < count; i++)
665 if (!SANITIZE (a[i]))
675 #endif /* HB_OPEN_TYPE_PRIVATE_HH */