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 DECONST_CAST(T,X,Ofs) (*(reinterpret_cast<T *>((char *)CharP(&(X)) + Ofs)))
49 #define CAST(T,X,Ofs) (*(reinterpret_cast<T *>(CharP(&(X)) + Ofs)))
52 /* StructAfter<T>(X) returns the struct T& that is placed after X.
53 * Works with X of variable size also. X must implement get_size() */
54 template<typename Type, typename TObject>
55 inline const Type& StructAfter(const TObject &X)
57 return * reinterpret_cast<const Type*> (CharP (&X) + X.get_size());
59 template<typename Type, typename TObject>
60 inline Type& StructAfter(TObject &X)
62 return * reinterpret_cast<Type*> (CharP (&X) + X.get_size());
73 /* Global nul-content Null pool. Enlarge as necessary. */
74 static const void *_NullPool[32 / sizeof (void *)];
76 /* Generic template for nul-content sizeof-sized Null objects. */
77 template <typename Type>
78 static inline const Type& Null () {
79 ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool));
80 return CONST_CAST (Type, *_NullPool, 0);
83 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
84 #define DEFINE_NULL_DATA(Type, size, data) \
85 static const char _Null##Type[size + 1] = data; \
87 inline const Type& Null<Type> () { \
88 return CONST_CAST (Type, *_Null##Type, 0); \
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))
259 #define SANITIZE_GET_SIZE() SANITIZE_SELF() && SANITIZE_MEM (this, this->get_size ())
261 #define SANITIZE_MEM(B,L) HB_LIKELY (_hb_sanitize_check (SANITIZE_ARG, CharP(B), (L)))
263 #define SANITIZE_ARRAY(A,S,L) HB_LIKELY (_hb_sanitize_array (SANITIZE_ARG, CharP(A), S, L))
265 #define NEUTER(Var, Val) \
266 (SANITIZE_OBJ (Var) && \
267 _hb_sanitize_edit (SANITIZE_ARG, CharP(&(Var)), sizeof (Var)) && \
268 ((Var).set (Val), true))
271 /* Template to sanitize an object. */
272 template <typename Type>
275 static hb_blob_t *sanitize (hb_blob_t *blob) {
276 hb_sanitize_context_t context;
279 /* TODO is_sane() stuff */
282 #if HB_DEBUG_SANITIZE
283 fprintf (stderr, "Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__);
286 _hb_sanitize_init (&context, blob);
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 inline bool sanitize (SANITIZE_ARG_DEF) {
411 /* Note: Only accept ASCII-visible tags (mind DEL)
412 * This is one of the few places (only place?) that we check
413 * for data integrity, as opposed to just boundary checks.
415 return SANITIZE_SELF () && (((uint32_t) *this) & 0x80808080) == 0;
418 ASSERT_SIZE (Tag, 4);
419 DEFINE_NULL_DATA (Tag, 4, " ");
421 /* Glyph index number, same as uint16 (length = 16 bits) */
422 typedef USHORT GlyphID;
424 /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
425 typedef USHORT Offset;
427 /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
428 typedef ULONG LongOffset;
432 struct CheckSum : ULONG
434 static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
437 ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::get_size ();
439 while (Table < EndPtr)
444 ASSERT_SIZE (CheckSum, 4);
453 inline operator uint32_t (void) const { return (major << 16) + minor; }
455 inline bool sanitize (SANITIZE_ARG_DEF) {
457 return SANITIZE_SELF ();
463 ASSERT_SIZE (FixedVersion, 4);
468 * Template subclasses of Offset and LongOffset that do the dereferencing.
469 * Use: (this+memberName)
472 template <typename OffsetType, typename Type>
473 struct GenericOffsetTo : OffsetType
475 inline const Type& operator () (const void *base) const
477 unsigned int offset = *this;
478 if (HB_UNLIKELY (!offset)) return Null(Type);
479 return CONST_CAST(Type, *CharP(base), offset);
482 inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
484 if (!SANITIZE_SELF ()) return false;
485 unsigned int offset = *this;
486 if (HB_UNLIKELY (!offset)) return true;
487 return SANITIZE (CAST(Type, *CharP(base), offset)) || NEUTER (CAST(OffsetType,*this,0), 0);
489 inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
491 if (!SANITIZE_SELF ()) return false;
492 unsigned int offset = *this;
493 if (HB_UNLIKELY (!offset)) return true;
494 return SANITIZE_BASE (CAST(Type, *CharP(base), offset), base2) || NEUTER (CAST(OffsetType,*this,0), 0);
496 inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
498 if (!SANITIZE_SELF ()) return false;
499 unsigned int offset = *this;
500 if (HB_UNLIKELY (!offset)) return true;
501 return SANITIZE_BASE (CAST(Type, *CharP(base), offset), user_data) || NEUTER (CAST(OffsetType,*this,0), 0);
504 template <typename Base, typename OffsetType, typename Type>
505 inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
507 template <typename Type>
508 struct OffsetTo : GenericOffsetTo<Offset, Type> {};
510 template <typename Type>
511 struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
518 template <typename LenType, typename Type>
519 struct GenericArrayOf
521 const Type *array(void) const { return &StructAfter<Type> (len); }
522 Type *array(void) { return &StructAfter<Type> (len); }
524 const Type *const_sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
526 unsigned int count = len;
527 if (HB_UNLIKELY (start_offset > count))
530 count -= start_offset;
531 count = MIN (count, *pcount);
533 return array() + start_offset;
536 inline const Type& operator [] (unsigned int i) const
538 if (HB_UNLIKELY (i >= len)) return Null(Type);
541 inline unsigned int get_size () const
542 { return len.get_size () + len * Type::get_size (); }
544 inline bool sanitize (SANITIZE_ARG_DEF) {
546 if (!SANITIZE_GET_SIZE()) return false;
547 /* Note: for structs that do not reference other structs,
548 * we do not need to call their sanitize() as we already did
549 * a bound check on the aggregate array size, hence the return.
552 /* We do keep this code though to make sure the structs pointed
553 * to do have a simple sanitize(), ie. they do not reference
555 unsigned int count = len;
556 for (unsigned int i = 0; i < count; i++)
557 if (!SANITIZE (array()[i]))
561 inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
563 if (!SANITIZE_GET_SIZE()) return false;
564 unsigned int count = len;
565 for (unsigned int i = 0; i < count; i++)
566 if (!array()[i].sanitize (SANITIZE_ARG, base))
570 inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
572 if (!SANITIZE_GET_SIZE()) return false;
573 unsigned int count = len;
574 for (unsigned int i = 0; i < count; i++)
575 if (!array()[i].sanitize (SANITIZE_ARG, base, base2))
579 inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
581 if (!SANITIZE_GET_SIZE()) return false;
582 unsigned int count = len;
583 for (unsigned int i = 0; i < count; i++)
584 if (!array()[i].sanitize (SANITIZE_ARG, base, user_data))
593 /* An array with a USHORT number of elements. */
594 template <typename Type>
595 struct ArrayOf : GenericArrayOf<USHORT, Type> {};
597 /* An array with a ULONG number of elements. */
598 template <typename Type>
599 struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
601 /* Array of Offset's */
602 template <typename Type>
603 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
605 /* Array of LongOffset's */
606 template <typename Type>
607 struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
609 /* LongArray of LongOffset's */
610 template <typename Type>
611 struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
613 /* Array of offsets relative to the beginning of the array itself. */
614 template <typename Type>
615 struct OffsetListOf : OffsetArrayOf<Type>
617 inline const Type& operator [] (unsigned int i) const
619 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
620 return this+this->array()[i];
623 inline bool sanitize (SANITIZE_ARG_DEF) {
625 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this));
627 inline bool sanitize (SANITIZE_ARG_DEF, unsigned int user_data) {
629 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this), user_data);
634 /* An array with a USHORT number of elements,
635 * starting at second element. */
636 template <typename Type>
637 struct HeadlessArrayOf
639 const Type *array(void) const { return &StructAfter<Type> (len); }
640 Type *array(void) { return &StructAfter<Type> (len); }
642 inline const Type& operator [] (unsigned int i) const
644 if (HB_UNLIKELY (i >= len || !i)) return Null(Type);
647 inline unsigned int get_size () const
648 { return len.get_size () + (len ? len - 1 : 0) * Type::get_size (); }
650 inline bool sanitize (SANITIZE_ARG_DEF) {
652 if (!SANITIZE_GET_SIZE()) return false;
653 /* Note: for structs that do not reference other structs,
654 * we do not need to call their sanitize() as we already did
655 * a bound check on the aggregate array size, hence the return.
658 /* We do keep this code though to make sure the structs pointed
659 * to do have a simple sanitize(), ie. they do not reference
661 unsigned int count = len ? len - 1 : 0;
663 for (unsigned int i = 0; i < count; i++)
664 if (!SANITIZE (a[i]))
674 #endif /* HB_OPEN_TYPE_PRIVATE_HH */