Towards normalization
[framework/uifw/harfbuzz.git] / src / hb-open-type-private.hh
1 /*
2  * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
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.
11  *
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
16  * DAMAGE.
17  *
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.
23  *
24  * Red Hat Author(s): Behdad Esfahbod
25  */
26
27 #ifndef HB_OPEN_TYPE_PRIVATE_HH
28 #define HB_OPEN_TYPE_PRIVATE_HH
29
30 #include "hb-private.hh"
31
32 #include "hb-blob.h"
33
34 HB_BEGIN_DECLS
35 HB_END_DECLS
36
37
38 /*
39  * Casts
40  */
41
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); }
49
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); }
57
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); }
66
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()); }
75
76
77
78 /*
79  * Size checking
80  */
81
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 \
89   { _code; }
90
91
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)
96
97 /* Size signifying variable-sized array */
98 #define VAR 1
99
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)
103
104 #define DEFINE_SIZE_MIN(size) \
105   _DEFINE_SIZE_ASSERTION (sizeof (*this) >= (size)); \
106   static const unsigned int min_size = (size)
107
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)
112
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)
117
118
119
120 /*
121  * Null objects
122  */
123
124 /* Global nul-content Null pool.  Enlarge as necessary. */
125 static const void *_NullPool[64 / sizeof (void *)];
126
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);
132 }
133
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 */ \
137 template <> \
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))
142
143 /* Accessor macro. */
144 #define Null(Type) Null<Type>()
145
146
147 /*
148  * Trace
149  */
150
151
152 template <int max_depth>
153 struct hb_trace_t {
154   explicit hb_trace_t (unsigned int *pdepth_, const char *what, const char *function, const void *obj) : pdepth(pdepth_) {
155     (void) (*pdepth < max_depth &&
156             fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, *pdepth, *pdepth, function));
157     if (max_depth) ++*pdepth;
158   }
159   ~hb_trace_t (void) { if (max_depth) --*pdepth; }
160
161   private:
162   unsigned int *pdepth;
163 };
164 template <> /* Optimize when tracing is disabled */
165 struct hb_trace_t<0> {
166   explicit hb_trace_t (unsigned int *pdepth HB_UNUSED, const char *what HB_UNUSED, const char *function HB_UNUSED, const void *obj HB_UNUSED) {}
167 };
168
169
170
171 /*
172  * Sanitize
173  */
174
175 #ifndef HB_DEBUG_SANITIZE
176 #define HB_DEBUG_SANITIZE (HB_DEBUG+0)
177 #endif
178
179
180 #define TRACE_SANITIZE() \
181         hb_trace_t<HB_DEBUG_SANITIZE> trace (&c->debug_depth, "SANITIZE", HB_FUNC, this); \
182
183
184 struct hb_sanitize_context_t
185 {
186   inline void init (hb_blob_t *b)
187   {
188     this->blob = hb_blob_reference (b);
189     this->writable = false;
190   }
191
192   inline void setup (void)
193   {
194     this->start = hb_blob_get_data (this->blob, NULL);
195     this->end = this->start + hb_blob_get_length (this->blob);
196     this->edit_count = 0;
197     this->debug_depth = 0;
198
199     (void) (HB_DEBUG_SANITIZE &&
200       fprintf (stderr, "sanitize %p init [%p..%p] (%lu bytes)\n",
201                (void *) this->blob, this->start, this->end,
202                (unsigned long) (this->end - this->start)));
203   }
204
205   inline void finish (void)
206   {
207     (void) (HB_DEBUG_SANITIZE &&
208       fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
209                (void *) this->blob, this->start, this->end, this->edit_count));
210
211     hb_blob_destroy (this->blob);
212     this->blob = NULL;
213     this->start = this->end = NULL;
214   }
215
216   inline bool check_range (const void *base, unsigned int len) const
217   {
218     const char *p = (const char *) base;
219     bool ret = this->start <= p &&
220                p <= this->end &&
221                (unsigned int) (this->end - p) >= len;
222
223     (void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE &&
224       fprintf (stderr, "SANITIZE(%p) %-*d-> range [%p..%p] (%d bytes) in [%p..%p] -> %s\n",
225                p,
226                this->debug_depth, this->debug_depth,
227                p, p + len, len,
228                this->start, this->end,
229                ret ? "pass" : "FAIL"));
230
231     return likely (ret);
232   }
233
234   inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
235   {
236     const char *p = (const char *) base;
237     bool overflows = _hb_unsigned_int_mul_overflows (len, record_size);
238
239     (void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE &&
240       fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n",
241                p,
242                this->debug_depth, this->debug_depth,
243                p, p + (record_size * len), record_size, len, (unsigned long) record_size * len,
244                this->start, this->end,
245                !overflows ? "does not overflow" : "OVERFLOWS FAIL"));
246
247     return likely (!overflows && this->check_range (base, record_size * len));
248   }
249
250   template <typename Type>
251   inline bool check_struct (const Type *obj) const
252   {
253     return likely (this->check_range (obj, obj->min_size));
254   }
255
256   inline bool can_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
257   {
258     const char *p = (const char *) base;
259     this->edit_count++;
260
261     (void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE &&
262       fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n",
263                p,
264                this->debug_depth, this->debug_depth,
265                this->edit_count,
266                p, p + len, len,
267                this->start, this->end,
268                this->writable ? "granted" : "REJECTED"));
269
270     return this->writable;
271   }
272
273   unsigned int debug_depth;
274   const char *start, *end;
275   bool writable;
276   unsigned int edit_count;
277   hb_blob_t *blob;
278 };
279
280
281
282 /* Template to sanitize an object. */
283 template <typename Type>
284 struct Sanitizer
285 {
286   static hb_blob_t *sanitize (hb_blob_t *blob) {
287     hb_sanitize_context_t c[1] = {{0}};
288     bool sane;
289
290     /* TODO is_sane() stuff */
291
292     c->init (blob);
293
294   retry:
295     (void) (HB_DEBUG_SANITIZE &&
296       fprintf (stderr, "Sanitizer %p start %s\n", (void *) blob, HB_FUNC));
297
298     c->setup ();
299
300     if (unlikely (!c->start)) {
301       c->finish ();
302       return blob;
303     }
304
305     Type *t = CastP<Type> (const_cast<char *> (c->start));
306
307     sane = t->sanitize (c);
308     if (sane) {
309       if (c->edit_count) {
310         (void) (HB_DEBUG_SANITIZE &&
311           fprintf (stderr, "Sanitizer %p passed first round with %d edits; doing a second round %s\n",
312                    (void *) blob, c->edit_count, HB_FUNC));
313
314         /* sanitize again to ensure no toe-stepping */
315         c->edit_count = 0;
316         sane = t->sanitize (c);
317         if (c->edit_count) {
318           (void) (HB_DEBUG_SANITIZE &&
319             fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
320                      (void *) blob, c->edit_count, HB_FUNC));
321           sane = false;
322         }
323       }
324     } else {
325       unsigned int edit_count = c->edit_count;
326       if (edit_count && !c->writable) {
327         c->start = hb_blob_get_data_writable (blob, NULL);
328         c->end = c->start + hb_blob_get_length (blob);
329
330         if (c->start) {
331           c->writable = true;
332           /* ok, we made it writable by relocating.  try again */
333           (void) (HB_DEBUG_SANITIZE &&
334             fprintf (stderr, "Sanitizer %p retry %s\n", (void *) blob, HB_FUNC));
335           goto retry;
336         }
337       }
338     }
339
340     c->finish ();
341
342     (void) (HB_DEBUG_SANITIZE &&
343       fprintf (stderr, "Sanitizer %p %s %s\n", (void *) blob, sane ? "passed" : "FAILED", HB_FUNC));
344     if (sane)
345       return blob;
346     else {
347       hb_blob_destroy (blob);
348       return hb_blob_get_empty ();
349     }
350   }
351
352   static const Type* lock_instance (hb_blob_t *blob) {
353     hb_blob_make_immutable (blob);
354     const char *base = hb_blob_get_data (blob, NULL);
355     return unlikely (!base) ? &Null(Type) : CastP<Type> (base);
356   }
357 };
358
359
360
361
362 /*
363  *
364  * The OpenType Font File: Data Types
365  */
366
367
368 /* "The following data types are used in the OpenType font file.
369  *  All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
370
371 /*
372  * Int types
373  */
374
375
376 template <typename Type, int Bytes> class BEInt;
377
378 /* LONGTERMTODO: On machines allowing unaligned access, we can make the
379  * following tighter by using byteswap instructions on ints directly. */
380 template <typename Type>
381 class BEInt<Type, 2>
382 {
383   public:
384   inline void set (Type i) { hb_be_uint16_put (v,i); }
385   inline operator Type (void) const { return hb_be_uint16_get (v); }
386   inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_eq (v, o.v); }
387   inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
388   private: uint8_t v[2];
389 };
390 template <typename Type>
391 class BEInt<Type, 4>
392 {
393   public:
394   inline void set (Type i) { hb_be_uint32_put (v,i); }
395   inline operator Type (void) const { return hb_be_uint32_get (v); }
396   inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_eq (v, o.v); }
397   inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
398   private: uint8_t v[4];
399 };
400
401 /* Integer types in big-endian order and no alignment requirement */
402 template <typename Type>
403 struct IntType
404 {
405   inline void set (Type i) { v.set (i); }
406   inline operator Type(void) const { return v; }
407   inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
408   inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
409   inline int cmp (Type a) const { Type b = v; return a < b ? -1 : a == b ? 0 : +1; }
410   inline bool sanitize (hb_sanitize_context_t *c) {
411     TRACE_SANITIZE ();
412     return likely (c->check_struct (this));
413   }
414   protected:
415   BEInt<Type, sizeof (Type)> v;
416   public:
417   DEFINE_SIZE_STATIC (sizeof (Type));
418 };
419
420 typedef IntType<uint16_t> USHORT;       /* 16-bit unsigned integer. */
421 typedef IntType<int16_t>  SHORT;        /* 16-bit signed integer. */
422 typedef IntType<uint32_t> ULONG;        /* 32-bit unsigned integer. */
423 typedef IntType<int32_t>  LONG;         /* 32-bit signed integer. */
424
425 /* Date represented in number of seconds since 12:00 midnight, January 1,
426  * 1904. The value is represented as a signed 64-bit integer. */
427 struct LONGDATETIME
428 {
429   inline bool sanitize (hb_sanitize_context_t *c) {
430     TRACE_SANITIZE ();
431     return likely (c->check_struct (this));
432   }
433   private:
434   LONG major;
435   ULONG minor;
436   public:
437   DEFINE_SIZE_STATIC (8);
438 };
439
440 /* Array of four uint8s (length = 32 bits) used to identify a script, language
441  * system, feature, or baseline */
442 struct Tag : ULONG
443 {
444   /* What the char* converters return is NOT nul-terminated.  Print using "%.4s" */
445   inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); }
446   inline operator char* (void) { return reinterpret_cast<char *> (&this->v); }
447   public:
448   DEFINE_SIZE_STATIC (4);
449 };
450 DEFINE_NULL_DATA (Tag, "    ");
451
452 /* Glyph index number, same as uint16 (length = 16 bits) */
453 typedef USHORT GlyphID;
454
455 /* Script/language-system/feature index */
456 struct Index : USHORT {
457   static const unsigned int NOT_FOUND_INDEX = 0xFFFF;
458 };
459 DEFINE_NULL_DATA (Index, "\xff\xff");
460
461 /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
462 typedef USHORT Offset;
463
464 /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
465 typedef ULONG LongOffset;
466
467
468 /* CheckSum */
469 struct CheckSum : ULONG
470 {
471   static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
472   {
473     uint32_t Sum = 0L;
474     ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
475
476     while (Table < EndPtr)
477       Sum += *Table++;
478     return Sum;
479   }
480   public:
481   DEFINE_SIZE_STATIC (4);
482 };
483
484
485 /*
486  * Version Numbers
487  */
488
489 struct FixedVersion
490 {
491   inline uint32_t to_int (void) const { return (major << 16) + minor; }
492
493   inline bool sanitize (hb_sanitize_context_t *c) {
494     TRACE_SANITIZE ();
495     return c->check_struct (this);
496   }
497
498   USHORT major;
499   USHORT minor;
500   public:
501   DEFINE_SIZE_STATIC (4);
502 };
503
504
505
506 /*
507  * Template subclasses of Offset and LongOffset that do the dereferencing.
508  * Use: (base+offset)
509  */
510
511 template <typename OffsetType, typename Type>
512 struct GenericOffsetTo : OffsetType
513 {
514   inline const Type& operator () (const void *base) const
515   {
516     unsigned int offset = *this;
517     if (unlikely (!offset)) return Null(Type);
518     return StructAtOffset<Type> (base, offset);
519   }
520
521   inline bool sanitize (hb_sanitize_context_t *c, void *base) {
522     TRACE_SANITIZE ();
523     if (unlikely (!c->check_struct (this))) return false;
524     unsigned int offset = *this;
525     if (unlikely (!offset)) return true;
526     Type &obj = StructAtOffset<Type> (base, offset);
527     return likely (obj.sanitize (c)) || neuter (c);
528   }
529   template <typename T>
530   inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) {
531     TRACE_SANITIZE ();
532     if (unlikely (!c->check_struct (this))) return false;
533     unsigned int offset = *this;
534     if (unlikely (!offset)) return true;
535     Type &obj = StructAtOffset<Type> (base, offset);
536     return likely (obj.sanitize (c, user_data)) || neuter (c);
537   }
538
539   private:
540   /* Set the offset to Null */
541   inline bool neuter (hb_sanitize_context_t *c) {
542     if (c->can_edit (this, this->static_size)) {
543       this->set (0); /* 0 is Null offset */
544       return true;
545     }
546     return false;
547   }
548 };
549 template <typename Base, typename OffsetType, typename Type>
550 inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
551
552 template <typename Type>
553 struct OffsetTo : GenericOffsetTo<Offset, Type> {};
554
555 template <typename Type>
556 struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
557
558
559 /*
560  * Array Types
561  */
562
563 template <typename LenType, typename Type>
564 struct GenericArrayOf
565 {
566   const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
567   {
568     unsigned int count = len;
569     if (unlikely (start_offset > count))
570       count = 0;
571     else
572       count -= start_offset;
573     count = MIN (count, *pcount);
574     *pcount = count;
575     return array + start_offset;
576   }
577
578   inline const Type& operator [] (unsigned int i) const
579   {
580     if (unlikely (i >= len)) return Null(Type);
581     return array[i];
582   }
583   inline unsigned int get_size (void) const
584   { return len.static_size + len * Type::static_size; }
585
586   inline bool sanitize (hb_sanitize_context_t *c) {
587     TRACE_SANITIZE ();
588     if (unlikely (!sanitize_shallow (c))) return false;
589
590     /* Note: for structs that do not reference other structs,
591      * we do not need to call their sanitize() as we already did
592      * a bound check on the aggregate array size.  We just include
593      * a small unreachable expression to make sure the structs
594      * pointed to do have a simple sanitize(), ie. they do not
595      * reference other structs via offsets.
596      */
597     (void) (false && array[0].sanitize (c));
598
599     return true;
600   }
601   inline bool sanitize (hb_sanitize_context_t *c, void *base) {
602     TRACE_SANITIZE ();
603     if (unlikely (!sanitize_shallow (c))) return false;
604     unsigned int count = len;
605     for (unsigned int i = 0; i < count; i++)
606       if (unlikely (!array[i].sanitize (c, base)))
607         return false;
608     return true;
609   }
610   template <typename T>
611   inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) {
612     TRACE_SANITIZE ();
613     if (unlikely (!sanitize_shallow (c))) return false;
614     unsigned int count = len;
615     for (unsigned int i = 0; i < count; i++)
616       if (unlikely (!array[i].sanitize (c, base, user_data)))
617         return false;
618     return true;
619   }
620
621   private:
622   inline bool sanitize_shallow (hb_sanitize_context_t *c) {
623     TRACE_SANITIZE ();
624     return c->check_struct (this)
625         && c->check_array (this, Type::static_size, len);
626   }
627
628   public:
629   LenType len;
630   Type array[VAR];
631   public:
632   DEFINE_SIZE_ARRAY (sizeof (LenType), array);
633 };
634
635 /* An array with a USHORT number of elements. */
636 template <typename Type>
637 struct ArrayOf : GenericArrayOf<USHORT, Type> {};
638
639 /* An array with a ULONG number of elements. */
640 template <typename Type>
641 struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
642
643 /* Array of Offset's */
644 template <typename Type>
645 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
646
647 /* Array of LongOffset's */
648 template <typename Type>
649 struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
650
651 /* LongArray of LongOffset's */
652 template <typename Type>
653 struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
654
655 /* Array of offsets relative to the beginning of the array itself. */
656 template <typename Type>
657 struct OffsetListOf : OffsetArrayOf<Type>
658 {
659   inline const Type& operator [] (unsigned int i) const
660   {
661     if (unlikely (i >= this->len)) return Null(Type);
662     return this+this->array[i];
663   }
664
665   inline bool sanitize (hb_sanitize_context_t *c) {
666     TRACE_SANITIZE ();
667     return OffsetArrayOf<Type>::sanitize (c, this);
668   }
669   template <typename T>
670   inline bool sanitize (hb_sanitize_context_t *c, T user_data) {
671     TRACE_SANITIZE ();
672     return OffsetArrayOf<Type>::sanitize (c, this, user_data);
673   }
674 };
675
676
677 /* An array with a USHORT number of elements,
678  * starting at second element. */
679 template <typename Type>
680 struct HeadlessArrayOf
681 {
682   inline const Type& operator [] (unsigned int i) const
683   {
684     if (unlikely (i >= len || !i)) return Null(Type);
685     return array[i-1];
686   }
687   inline unsigned int get_size (void) const
688   { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
689
690   inline bool sanitize_shallow (hb_sanitize_context_t *c) {
691     return c->check_struct (this)
692         && c->check_array (this, Type::static_size, len);
693   }
694
695   inline bool sanitize (hb_sanitize_context_t *c) {
696     TRACE_SANITIZE ();
697     if (unlikely (!sanitize_shallow (c))) return false;
698
699     /* Note: for structs that do not reference other structs,
700      * we do not need to call their sanitize() as we already did
701      * a bound check on the aggregate array size.  We just include
702      * a small unreachable expression to make sure the structs
703      * pointed to do have a simple sanitize(), ie. they do not
704      * reference other structs via offsets.
705      */
706     (void) (false && array[0].sanitize (c));
707
708     return true;
709   }
710
711   USHORT len;
712   Type array[VAR];
713   public:
714   DEFINE_SIZE_ARRAY (sizeof (USHORT), array);
715 };
716
717
718 /* An array with sorted elements.  Supports binary searching. */
719 template <typename Type>
720 struct SortedArrayOf : ArrayOf<Type> {
721
722   template <typename SearchType>
723   inline int search (const SearchType &x) const {
724     class Cmp {
725       public: static int cmp (const SearchType *a, const Type *b) { return b->cmp (*a); }
726     };
727     const Type *p = (const Type *) bsearch (&x, this->array, this->len, sizeof (this->array[0]), (hb_compare_func_t) Cmp::cmp);
728     return p ? p - this->array : -1;
729   }
730 };
731
732
733 HB_BEGIN_DECLS
734 HB_END_DECLS
735
736 #endif /* HB_OPEN_TYPE_PRIVATE_HH */