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