Imported Upstream version 0.9.3
[platform/upstream/harfbuzz.git] / src / hb-open-type-private.hh
1 /*
2  * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
3  * Copyright © 2012  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #ifndef HB_OPEN_TYPE_PRIVATE_HH
30 #define HB_OPEN_TYPE_PRIVATE_HH
31
32 #include "hb-private.hh"
33
34 #include "hb-blob.h"
35
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_INSTANCE_ASSERTION1(_line, _assertion) \
84   inline void _instance_assertion_on_line_##_line (void) const \
85   { \
86     ASSERT_STATIC (_assertion); \
87     ASSERT_INSTANCE_POD (*this); /* Make sure it's POD. */ \
88   }
89 # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion)
90 # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion)
91
92 /* Check that _code compiles in a method environment */
93 #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \
94   inline void _compiles_assertion_on_line_##_line (void) const \
95   { _code; }
96 # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code)
97 # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code)
98
99
100 #define DEFINE_SIZE_STATIC(size) \
101   DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)); \
102   static const unsigned int static_size = (size); \
103   static const unsigned int min_size = (size)
104
105 /* Size signifying variable-sized array */
106 #define VAR 1
107
108 #define DEFINE_SIZE_UNION(size, _member) \
109   DEFINE_INSTANCE_ASSERTION (this->u._member.static_size == (size)); \
110   static const unsigned int min_size = (size)
111
112 #define DEFINE_SIZE_MIN(size) \
113   DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)); \
114   static const unsigned int min_size = (size)
115
116 #define DEFINE_SIZE_ARRAY(size, array) \
117   DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \
118   DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \
119   static const unsigned int min_size = (size)
120
121 #define DEFINE_SIZE_ARRAY2(size, array1, array2) \
122   DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \
123   DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].static_size) \
124   static const unsigned int min_size = (size)
125
126
127
128 /*
129  * Null objects
130  */
131
132 /* Global nul-content Null pool.  Enlarge as necessary. */
133 /* TODO This really should be a extern HB_INTERNAL and defined somewhere... */
134 static const void *_NullPool[64 / sizeof (void *)];
135
136 /* Generic nul-content Null objects. */
137 template <typename Type>
138 static inline const Type& Null (void) {
139   ASSERT_STATIC (Type::min_size <= sizeof (_NullPool));
140   return *CastP<Type> (_NullPool);
141 }
142
143 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
144 #define DEFINE_NULL_DATA(Type, data) \
145 static const char _Null##Type[Type::min_size + 1] = data; /* +1 is for nul-termination in data */ \
146 template <> \
147 inline const Type& Null<Type> (void) { \
148   return *CastP<Type> (_Null##Type); \
149 } /* The following line really exists such that we end in a place needing semicolon */ \
150 ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type))
151
152 /* Accessor macro. */
153 #define Null(Type) Null<Type>()
154
155
156
157 /*
158  * Sanitize
159  */
160
161 #ifndef HB_DEBUG_SANITIZE
162 #define HB_DEBUG_SANITIZE (HB_DEBUG+0)
163 #endif
164
165
166 #define TRACE_SANITIZE() \
167         hb_auto_trace_t<HB_DEBUG_SANITIZE> trace (&c->debug_depth, "SANITIZE", this, HB_FUNC, "");
168
169
170 struct hb_sanitize_context_t
171 {
172   inline void init (hb_blob_t *b)
173   {
174     this->blob = hb_blob_reference (b);
175     this->writable = false;
176   }
177
178   inline void start_processing (void)
179   {
180     this->start = hb_blob_get_data (this->blob, NULL);
181     this->end = this->start + hb_blob_get_length (this->blob);
182     this->edit_count = 0;
183     this->debug_depth = 0;
184
185     DEBUG_MSG_LEVEL (SANITIZE, this->blob, 0, +1,
186                      "start [%p..%p] (%lu bytes)",
187                      this->start, this->end,
188                      (unsigned long) (this->end - this->start));
189   }
190
191   inline void end_processing (void)
192   {
193     DEBUG_MSG_LEVEL (SANITIZE, this->blob, 0, -1,
194                      "end [%p..%p] %u edit requests",
195                      this->start, this->end, this->edit_count);
196
197     hb_blob_destroy (this->blob);
198     this->blob = NULL;
199     this->start = this->end = NULL;
200   }
201
202   inline bool check_range (const void *base, unsigned int len) const
203   {
204     const char *p = (const char *) base;
205
206     hb_auto_trace_t<HB_DEBUG_SANITIZE> trace (&this->debug_depth, "SANITIZE", this->blob, NULL,
207                                               "check_range [%p..%p] (%d bytes) in [%p..%p]",
208                                               p, p + len, len,
209                                               this->start, this->end);
210
211     return TRACE_RETURN (likely (this->start <= p && p <= this->end && (unsigned int) (this->end - p) >= len));
212   }
213
214   inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
215   {
216     const char *p = (const char *) base;
217     bool overflows = _hb_unsigned_int_mul_overflows (len, record_size);
218
219     hb_auto_trace_t<HB_DEBUG_SANITIZE> trace (&this->debug_depth, "SANITIZE", this->blob, NULL,
220                                               "check_array [%p..%p] (%d*%d=%ld bytes) in [%p..%p]",
221                                               p, p + (record_size * len), record_size, len, (unsigned long) record_size * len,
222                                               this->start, this->end);
223
224     return TRACE_RETURN (likely (!overflows && this->check_range (base, record_size * len)));
225   }
226
227   template <typename Type>
228   inline bool check_struct (const Type *obj) const
229   {
230     return likely (this->check_range (obj, obj->min_size));
231   }
232
233   inline bool may_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
234   {
235     const char *p = (const char *) base;
236     this->edit_count++;
237
238     hb_auto_trace_t<HB_DEBUG_SANITIZE> trace (&this->debug_depth, "SANITIZE", this->blob, NULL,
239                                               "may_edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s",
240                                               this->edit_count,
241                                               p, p + len, len,
242                                               this->start, this->end);
243
244     return TRACE_RETURN (this->writable);
245   }
246
247   mutable unsigned int debug_depth;
248   const char *start, *end;
249   bool writable;
250   unsigned int edit_count;
251   hb_blob_t *blob;
252 };
253
254
255
256 /* Template to sanitize an object. */
257 template <typename Type>
258 struct Sanitizer
259 {
260   static hb_blob_t *sanitize (hb_blob_t *blob) {
261     hb_sanitize_context_t c[1] = {{0}};
262     bool sane;
263
264     /* TODO is_sane() stuff */
265
266     c->init (blob);
267
268   retry:
269     DEBUG_MSG_FUNC (SANITIZE, blob, "start");
270
271     c->start_processing ();
272
273     if (unlikely (!c->start)) {
274       c->end_processing ();
275       return blob;
276     }
277
278     Type *t = CastP<Type> (const_cast<char *> (c->start));
279
280     sane = t->sanitize (c);
281     if (sane) {
282       if (c->edit_count) {
283         DEBUG_MSG_FUNC (SANITIZE, blob, "passed first round with %d edits; going for second round", c->edit_count);
284
285         /* sanitize again to ensure no toe-stepping */
286         c->edit_count = 0;
287         sane = t->sanitize (c);
288         if (c->edit_count) {
289           DEBUG_MSG_FUNC (SANITIZE, blob, "requested %d edits in second round; FAILLING", c->edit_count);
290           sane = false;
291         }
292       }
293     } else {
294       unsigned int edit_count = c->edit_count;
295       if (edit_count && !c->writable) {
296         c->start = hb_blob_get_data_writable (blob, NULL);
297         c->end = c->start + hb_blob_get_length (blob);
298
299         if (c->start) {
300           c->writable = true;
301           /* ok, we made it writable by relocating.  try again */
302           DEBUG_MSG_FUNC (SANITIZE, blob, "retry");
303           goto retry;
304         }
305       }
306     }
307
308     c->end_processing ();
309
310     DEBUG_MSG_FUNC (SANITIZE, blob, sane ? "PASSED" : "FAILED");
311     if (sane)
312       return blob;
313     else {
314       hb_blob_destroy (blob);
315       return hb_blob_get_empty ();
316     }
317   }
318
319   static const Type* lock_instance (hb_blob_t *blob) {
320     hb_blob_make_immutable (blob);
321     const char *base = hb_blob_get_data (blob, NULL);
322     return unlikely (!base) ? &Null(Type) : CastP<Type> (base);
323   }
324 };
325
326
327
328
329 /*
330  *
331  * The OpenType Font File: Data Types
332  */
333
334
335 /* "The following data types are used in the OpenType font file.
336  *  All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
337
338 /*
339  * Int types
340  */
341
342
343 template <typename Type, int Bytes> struct BEInt;
344
345 template <typename Type>
346 struct BEInt<Type, 2>
347 {
348   public:
349   inline void set (Type i) { hb_be_uint16_put (v,i); }
350   inline operator Type (void) const { return hb_be_uint16_get (v); }
351   inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_eq (v, o.v); }
352   inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
353   private: uint8_t v[2];
354 };
355 template <typename Type>
356 struct BEInt<Type, 4>
357 {
358   public:
359   inline void set (Type i) { hb_be_uint32_put (v,i); }
360   inline operator Type (void) const { return hb_be_uint32_get (v); }
361   inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_eq (v, o.v); }
362   inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
363   private: uint8_t v[4];
364 };
365
366 /* Integer types in big-endian order and no alignment requirement */
367 template <typename Type>
368 struct IntType
369 {
370   inline void set (Type i) { v.set (i); }
371   inline operator Type(void) const { return v; }
372   inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
373   inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
374   inline int cmp (Type a) const { Type b = v; return a < b ? -1 : a == b ? 0 : +1; }
375   inline bool sanitize (hb_sanitize_context_t *c) {
376     TRACE_SANITIZE ();
377     return TRACE_RETURN (likely (c->check_struct (this)));
378   }
379   protected:
380   BEInt<Type, sizeof (Type)> v;
381   public:
382   DEFINE_SIZE_STATIC (sizeof (Type));
383 };
384
385 /* Typedef these to avoid clash with windows.h */
386 #define USHORT  HB_USHORT
387 #define SHORT   HB_SHORT
388 #define ULONG   HB_ULONG
389 #define LONG    HB_LONG
390 typedef IntType<uint16_t> USHORT;       /* 16-bit unsigned integer. */
391 typedef IntType<int16_t>  SHORT;        /* 16-bit signed integer. */
392 typedef IntType<uint32_t> ULONG;        /* 32-bit unsigned integer. */
393 typedef IntType<int32_t>  LONG;         /* 32-bit signed integer. */
394
395 /* 16-bit signed integer (SHORT) that describes a quantity in FUnits. */
396 typedef SHORT FWORD;
397
398 /* 16-bit unsigned integer (USHORT) that describes a quantity in FUnits. */
399 typedef USHORT UFWORD;
400
401 /* Date represented in number of seconds since 12:00 midnight, January 1,
402  * 1904. The value is represented as a signed 64-bit integer. */
403 struct LONGDATETIME
404 {
405   inline bool sanitize (hb_sanitize_context_t *c) {
406     TRACE_SANITIZE ();
407     return TRACE_RETURN (likely (c->check_struct (this)));
408   }
409   private:
410   LONG major;
411   ULONG minor;
412   public:
413   DEFINE_SIZE_STATIC (8);
414 };
415
416 /* Array of four uint8s (length = 32 bits) used to identify a script, language
417  * system, feature, or baseline */
418 struct Tag : ULONG
419 {
420   /* What the char* converters return is NOT nul-terminated.  Print using "%.4s" */
421   inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); }
422   inline operator char* (void) { return reinterpret_cast<char *> (&this->v); }
423   public:
424   DEFINE_SIZE_STATIC (4);
425 };
426 DEFINE_NULL_DATA (Tag, "    ");
427
428 /* Glyph index number, same as uint16 (length = 16 bits) */
429 typedef USHORT GlyphID;
430
431 /* Script/language-system/feature index */
432 struct Index : USHORT {
433   static const unsigned int NOT_FOUND_INDEX = 0xFFFF;
434 };
435 DEFINE_NULL_DATA (Index, "\xff\xff");
436
437 /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
438 typedef USHORT Offset;
439
440 /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
441 typedef ULONG LongOffset;
442
443
444 /* CheckSum */
445 struct CheckSum : ULONG
446 {
447   static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
448   {
449     uint32_t Sum = 0L;
450     ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
451
452     while (Table < EndPtr)
453       Sum += *Table++;
454     return Sum;
455   }
456   public:
457   DEFINE_SIZE_STATIC (4);
458 };
459
460
461 /*
462  * Version Numbers
463  */
464
465 struct FixedVersion
466 {
467   inline uint32_t to_int (void) const { return (major << 16) + minor; }
468
469   inline bool sanitize (hb_sanitize_context_t *c) {
470     TRACE_SANITIZE ();
471     return TRACE_RETURN (c->check_struct (this));
472   }
473
474   USHORT major;
475   USHORT minor;
476   public:
477   DEFINE_SIZE_STATIC (4);
478 };
479
480
481
482 /*
483  * Template subclasses of Offset and LongOffset that do the dereferencing.
484  * Use: (base+offset)
485  */
486
487 template <typename OffsetType, typename Type>
488 struct GenericOffsetTo : OffsetType
489 {
490   inline const Type& operator () (const void *base) const
491   {
492     unsigned int offset = *this;
493     if (unlikely (!offset)) return Null(Type);
494     return StructAtOffset<Type> (base, offset);
495   }
496
497   inline bool sanitize (hb_sanitize_context_t *c, void *base) {
498     TRACE_SANITIZE ();
499     if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false);
500     unsigned int offset = *this;
501     if (unlikely (!offset)) return TRACE_RETURN (true);
502     Type &obj = StructAtOffset<Type> (base, offset);
503     return TRACE_RETURN (likely (obj.sanitize (c)) || neuter (c));
504   }
505   template <typename T>
506   inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) {
507     TRACE_SANITIZE ();
508     if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false);
509     unsigned int offset = *this;
510     if (unlikely (!offset)) return TRACE_RETURN (true);
511     Type &obj = StructAtOffset<Type> (base, offset);
512     return TRACE_RETURN (likely (obj.sanitize (c, user_data)) || neuter (c));
513   }
514
515   private:
516   /* Set the offset to Null */
517   inline bool neuter (hb_sanitize_context_t *c) {
518     if (c->may_edit (this, this->static_size)) {
519       this->set (0); /* 0 is Null offset */
520       return true;
521     }
522     return false;
523   }
524 };
525 template <typename Base, typename OffsetType, typename Type>
526 inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
527
528 template <typename Type>
529 struct OffsetTo : GenericOffsetTo<Offset, Type> {};
530
531 template <typename Type>
532 struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
533
534
535 /*
536  * Array Types
537  */
538
539 template <typename LenType, typename Type>
540 struct GenericArrayOf
541 {
542   const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
543   {
544     unsigned int count = len;
545     if (unlikely (start_offset > count))
546       count = 0;
547     else
548       count -= start_offset;
549     count = MIN (count, *pcount);
550     *pcount = count;
551     return array + start_offset;
552   }
553
554   inline const Type& operator [] (unsigned int i) const
555   {
556     if (unlikely (i >= len)) return Null(Type);
557     return array[i];
558   }
559   inline unsigned int get_size (void) const
560   { return len.static_size + len * Type::static_size; }
561
562   inline bool sanitize (hb_sanitize_context_t *c) {
563     TRACE_SANITIZE ();
564     if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false);
565
566     /* Note: for structs that do not reference other structs,
567      * we do not need to call their sanitize() as we already did
568      * a bound check on the aggregate array size.  We just include
569      * a small unreachable expression to make sure the structs
570      * pointed to do have a simple sanitize(), ie. they do not
571      * reference other structs via offsets.
572      */
573     (void) (false && array[0].sanitize (c));
574
575     return TRACE_RETURN (true);
576   }
577   inline bool sanitize (hb_sanitize_context_t *c, void *base) {
578     TRACE_SANITIZE ();
579     if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false);
580     unsigned int count = len;
581     for (unsigned int i = 0; i < count; i++)
582       if (unlikely (!array[i].sanitize (c, base)))
583         return TRACE_RETURN (false);
584     return TRACE_RETURN (true);
585   }
586   template <typename T>
587   inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) {
588     TRACE_SANITIZE ();
589     if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false);
590     unsigned int count = len;
591     for (unsigned int i = 0; i < count; i++)
592       if (unlikely (!array[i].sanitize (c, base, user_data)))
593         return TRACE_RETURN (false);
594     return TRACE_RETURN (true);
595   }
596
597   private:
598   inline bool sanitize_shallow (hb_sanitize_context_t *c) {
599     TRACE_SANITIZE ();
600     return TRACE_RETURN (c->check_struct (this) && c->check_array (this, Type::static_size, len));
601   }
602
603   public:
604   LenType len;
605   Type array[VAR];
606   public:
607   DEFINE_SIZE_ARRAY (sizeof (LenType), array);
608 };
609
610 /* An array with a USHORT number of elements. */
611 template <typename Type>
612 struct ArrayOf : GenericArrayOf<USHORT, Type> {};
613
614 /* An array with a ULONG number of elements. */
615 template <typename Type>
616 struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
617
618 /* Array of Offset's */
619 template <typename Type>
620 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
621
622 /* Array of LongOffset's */
623 template <typename Type>
624 struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
625
626 /* LongArray of LongOffset's */
627 template <typename Type>
628 struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
629
630 /* Array of offsets relative to the beginning of the array itself. */
631 template <typename Type>
632 struct OffsetListOf : OffsetArrayOf<Type>
633 {
634   inline const Type& operator [] (unsigned int i) const
635   {
636     if (unlikely (i >= this->len)) return Null(Type);
637     return this+this->array[i];
638   }
639
640   inline bool sanitize (hb_sanitize_context_t *c) {
641     TRACE_SANITIZE ();
642     return TRACE_RETURN (OffsetArrayOf<Type>::sanitize (c, this));
643   }
644   template <typename T>
645   inline bool sanitize (hb_sanitize_context_t *c, T user_data) {
646     TRACE_SANITIZE ();
647     return TRACE_RETURN (OffsetArrayOf<Type>::sanitize (c, this, user_data));
648   }
649 };
650
651
652 /* An array with a USHORT number of elements,
653  * starting at second element. */
654 template <typename Type>
655 struct HeadlessArrayOf
656 {
657   inline const Type& operator [] (unsigned int i) const
658   {
659     if (unlikely (i >= len || !i)) return Null(Type);
660     return array[i-1];
661   }
662   inline unsigned int get_size (void) const
663   { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
664
665   inline bool sanitize_shallow (hb_sanitize_context_t *c) {
666     return c->check_struct (this)
667         && c->check_array (this, Type::static_size, len);
668   }
669
670   inline bool sanitize (hb_sanitize_context_t *c) {
671     TRACE_SANITIZE ();
672     if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false);
673
674     /* Note: for structs that do not reference other structs,
675      * we do not need to call their sanitize() as we already did
676      * a bound check on the aggregate array size.  We just include
677      * a small unreachable expression to make sure the structs
678      * pointed to do have a simple sanitize(), ie. they do not
679      * reference other structs via offsets.
680      */
681     (void) (false && array[0].sanitize (c));
682
683     return TRACE_RETURN (true);
684   }
685
686   USHORT len;
687   Type array[VAR];
688   public:
689   DEFINE_SIZE_ARRAY (sizeof (USHORT), array);
690 };
691
692
693 /* An array with sorted elements.  Supports binary searching. */
694 template <typename Type>
695 struct SortedArrayOf : ArrayOf<Type> {
696
697   template <typename SearchType>
698   inline int search (const SearchType &x) const {
699     unsigned int count = this->len;
700     /* Linear search is *much* faster for small counts. */
701     if (likely (count < 32)) {
702       for (unsigned int i = 0; i < count; i++)
703         if (this->array[i].cmp (x) == 0)
704           return i;
705       return -1;
706     } else {
707       struct Cmp {
708         static int cmp (const SearchType *a, const Type *b) { return b->cmp (*a); }
709       };
710       const Type *p = (const Type *) bsearch (&x, this->array, this->len, sizeof (this->array[0]), (hb_compare_func_t) Cmp::cmp);
711       return p ? p - this->array : -1;
712     }
713   }
714 };
715
716
717
718 #endif /* HB_OPEN_TYPE_PRIVATE_HH */