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