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