Make sure semicolon is expected after DEFINE_NULL_DATA()
[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 DECONST_CAST(T,X,Ofs)   (*(reinterpret_cast<T *>((char *)CharP(&(X)) + Ofs)))
49 #define CAST(T,X,Ofs)           (*(reinterpret_cast<T *>(CharP(&(X)) + Ofs)))
50
51
52 /* StructAfter<T>(X) returns the struct T& that is placed after X.
53  * Works with X of variable size also.  X must implement get_size() */
54 template<typename Type, typename TObject>
55 inline const Type& StructAfter(const TObject &X)
56 {
57   return * reinterpret_cast<const Type*> (CharP (&X) + X.get_size());
58 }
59 template<typename Type, typename TObject>
60 inline Type& StructAfter(TObject &X)
61 {
62   return * reinterpret_cast<Type*> (CharP (&X) + X.get_size());
63 }
64
65
66 /*
67  * Class features
68  */
69
70
71 /* Null objects */
72
73 /* Global nul-content Null pool.  Enlarge as necessary. */
74 static const void *_NullPool[32 / sizeof (void *)];
75
76 /* Generic template for nul-content sizeof-sized Null objects. */
77 template <typename Type>
78 static inline const Type& Null () {
79   ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool));
80   return CONST_CAST (Type, *_NullPool, 0);
81 }
82
83 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
84 #define DEFINE_NULL_DATA(Type, size, data) \
85 static const char _Null##Type[size + 1] = data; /* +1 is for nul-termination in data */ \
86 template <> \
87 inline const Type& Null<Type> () { \
88   return CONST_CAST (Type, *_Null##Type, 0); \
89 } /* The following line really exists such that we end in a place needing semicolon */ \
90 ASSERT_STATIC (sizeof (Type) + 1 <= sizeof (_Null##Type))
91
92 /* Accessor macro. */
93 #define Null(Type) Null<Type>()
94
95
96 /* get_for_data() is a static class method returning a reference to an
97  * instance of Type located at the input data location.  It's just a
98  * fancy, NULL-safe, cast! */
99 #define STATIC_DEFINE_GET_FOR_DATA(Type) \
100   static inline const Type& get_for_data (const char *data) \
101   { \
102     if (HB_UNLIKELY (data == NULL)) return Null(Type); \
103     return CONST_CAST (Type, *data, 0); \
104   }
105 /* Like get_for_data(), but checks major version first. */
106 #define STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION(Type, MajorMin, MajorMax) \
107   static inline const Type& get_for_data (const char *data) \
108   { \
109     if (HB_UNLIKELY (data == NULL)) return Null(Type); \
110     const Type& t = CONST_CAST (Type, *data, 0); \
111     if (HB_UNLIKELY (t.version.major < MajorMin || t.version.major > MajorMax)) return Null(Type); \
112     return t; \
113   }
114
115
116 /*
117  * Sanitize
118  */
119
120 #ifndef HB_DEBUG_SANITIZE
121 #define HB_DEBUG_SANITIZE HB_DEBUG
122 #endif
123
124 #if HB_DEBUG_SANITIZE
125 #include <stdio.h>
126 #define TRACE_SANITIZE_ARG_DEF  , unsigned int sanitize_depth HB_GNUC_UNUSED
127 #define TRACE_SANITIZE_ARG      , sanitize_depth + 1
128 #define TRACE_SANITIZE_ARG_INIT , 1
129 #define TRACE_SANITIZE() \
130         HB_STMT_START { \
131             if (sanitize_depth < HB_DEBUG_SANITIZE) \
132                 fprintf (stderr, "SANITIZE(%p) %-*d-> %s\n", \
133                          (CharP (this) == CharP (&NullPool)) ? 0 : this, \
134                          sanitize_depth, sanitize_depth, \
135                          __PRETTY_FUNCTION__); \
136         } HB_STMT_END
137 #else
138 #define TRACE_SANITIZE_ARG_DEF
139 #define TRACE_SANITIZE_ARG
140 #define TRACE_SANITIZE_ARG_INIT
141 #define TRACE_SANITIZE() HB_STMT_START {} HB_STMT_END
142 #endif
143
144 #define SANITIZE_ARG_DEF \
145         hb_sanitize_context_t *context TRACE_SANITIZE_ARG_DEF
146 #define SANITIZE_ARG \
147         context TRACE_SANITIZE_ARG
148 #define SANITIZE_ARG_INIT \
149         &context TRACE_SANITIZE_ARG_INIT
150
151 typedef struct _hb_sanitize_context_t hb_sanitize_context_t;
152 struct _hb_sanitize_context_t
153 {
154   const char *start, *end;
155   int edit_count;
156   hb_blob_t *blob;
157 };
158
159 static HB_GNUC_UNUSED void
160 _hb_sanitize_init (hb_sanitize_context_t *context,
161                    hb_blob_t *blob)
162 {
163   context->blob = blob;
164   context->start = hb_blob_lock (blob);
165   context->end = context->start + hb_blob_get_length (blob);
166   context->edit_count = 0;
167
168 #if HB_DEBUG_SANITIZE
169   fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
170            context->blob, context->start, context->end, context->end - context->start);
171 #endif
172 }
173
174 static HB_GNUC_UNUSED void
175 _hb_sanitize_fini (hb_sanitize_context_t *context,
176                    bool unlock)
177 {
178 #if HB_DEBUG_SANITIZE
179   fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
180            context->blob, context->start, context->end, context->edit_count);
181 #endif
182
183   if (unlock)
184     hb_blob_unlock (context->blob);
185 }
186
187 static HB_GNUC_UNUSED inline bool
188 _hb_sanitize_check (SANITIZE_ARG_DEF,
189                     const char *base,
190                     unsigned int len)
191 {
192   bool ret = context->start <= base &&
193              base <= context->end &&
194              (unsigned int) (context->end - base) >= len;
195
196 #if HB_DEBUG_SANITIZE
197   if (sanitize_depth < HB_DEBUG_SANITIZE) \
198     fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
199              base,
200              sanitize_depth, sanitize_depth,
201              base, base+len, len,
202              context->start, context->end,
203              ret ? "pass" : "FAIL");
204 #endif
205   return ret;
206 }
207
208 static HB_GNUC_UNUSED inline bool
209 _hb_sanitize_array (SANITIZE_ARG_DEF,
210                     const char *base,
211                     unsigned int record_size,
212                     unsigned int len)
213 {
214   bool overflows = len >= ((unsigned int) -1) / record_size;
215
216 #if HB_DEBUG_SANITIZE
217   if (sanitize_depth < HB_DEBUG_SANITIZE) \
218     fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
219              base,
220              sanitize_depth, sanitize_depth,
221              base, base + (record_size * len), record_size, len, (unsigned long) record_size * len,
222              context->start, context->end,
223              !overflows ? "does not overflow" : "OVERFLOWS FAIL");
224 #endif
225   return HB_LIKELY (!overflows) && _hb_sanitize_check (SANITIZE_ARG, base, record_size * len);
226 }
227
228 static HB_GNUC_UNUSED inline bool
229 _hb_sanitize_edit (SANITIZE_ARG_DEF,
230                    const char *base HB_GNUC_UNUSED,
231                    unsigned int len HB_GNUC_UNUSED)
232 {
233   bool perm = hb_blob_try_writable_inplace (context->blob);
234   context->edit_count++;
235
236 #if HB_DEBUG_SANITIZE
237   fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
238            base,
239            sanitize_depth, sanitize_depth,
240            context->edit_count,
241            base, base+len, len,
242            context->start, context->end,
243            perm ? "granted" : "REJECTED");
244 #endif
245   return perm;
246 }
247
248 #define SANITIZE(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG))
249 #define SANITIZE2(X,Y) (SANITIZE (X) && SANITIZE (Y))
250
251 #define SANITIZE_THIS(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG, CharP(this)))
252 #define SANITIZE_THIS2(X,Y) (SANITIZE_THIS (X) && SANITIZE_THIS (Y))
253 #define SANITIZE_THIS3(X,Y,Z) (SANITIZE_THIS (X) && SANITIZE_THIS (Y) && SANITIZE_THIS(Z))
254
255 #define SANITIZE_BASE(X,B) HB_LIKELY ((X).sanitize (SANITIZE_ARG, B))
256 #define SANITIZE_BASE2(X,Y,B) (SANITIZE_BASE (X,B) && SANITIZE_BASE (Y,B))
257
258 #define SANITIZE_SELF() SANITIZE_OBJ (*this)
259 #define SANITIZE_OBJ(X) SANITIZE_MEM(&(X), sizeof (X))
260
261 #define SANITIZE_MEM(B,L) HB_LIKELY (_hb_sanitize_check (SANITIZE_ARG, CharP(B), (L)))
262
263 #define SANITIZE_ARRAY(A,S,L) HB_LIKELY (_hb_sanitize_array (SANITIZE_ARG, CharP(A), S, L))
264
265 #define NEUTER(Var, Val) \
266         (SANITIZE_OBJ (Var) && \
267          _hb_sanitize_edit (SANITIZE_ARG, CharP(&(Var)), sizeof (Var)) && \
268          ((Var).set (Val), true))
269
270
271 /* Template to sanitize an object. */
272 template <typename Type>
273 struct Sanitizer
274 {
275   static hb_blob_t *sanitize (hb_blob_t *blob) {
276     hb_sanitize_context_t context;
277     bool sane;
278
279     /* TODO is_sane() stuff */
280
281   retry:
282 #if HB_DEBUG_SANITIZE
283     fprintf (stderr, "Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__);
284 #endif
285
286     _hb_sanitize_init (&context, blob);
287
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   inline bool sanitize (SANITIZE_ARG_DEF) {
410     TRACE_SANITIZE ();
411     /* Note: Only accept ASCII-visible tags (mind DEL)
412      * This is one of the few places (only place?) that we check
413      * for data integrity, as opposed to just boundary checks.
414      */
415     return SANITIZE_SELF () && (((uint32_t) *this) & 0x80808080) == 0;
416   }
417 };
418 ASSERT_SIZE (Tag, 4);
419 DEFINE_NULL_DATA (Tag, 4, "    ");
420
421 /* Glyph index number, same as uint16 (length = 16 bits) */
422 typedef USHORT GlyphID;
423
424 /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
425 typedef USHORT Offset;
426
427 /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
428 typedef ULONG LongOffset;
429
430
431 /* CheckSum */
432 struct CheckSum : ULONG
433 {
434   static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
435   {
436     uint32_t Sum = 0L;
437     ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::get_size ();
438
439     while (Table < EndPtr)
440       Sum += *Table++;
441     return Sum;
442   }
443 };
444 ASSERT_SIZE (CheckSum, 4);
445
446
447 /*
448  * Version Numbers
449  */
450
451 struct FixedVersion
452 {
453   inline operator uint32_t (void) const { return (major << 16) + minor; }
454
455   inline bool sanitize (SANITIZE_ARG_DEF) {
456     TRACE_SANITIZE ();
457     return SANITIZE_SELF ();
458   }
459
460   USHORT major;
461   USHORT minor;
462 };
463 ASSERT_SIZE (FixedVersion, 4);
464
465
466
467 /*
468  * Template subclasses of Offset and LongOffset that do the dereferencing.
469  * Use: (this+memberName)
470  */
471
472 template <typename OffsetType, typename Type>
473 struct GenericOffsetTo : OffsetType
474 {
475   inline const Type& operator () (const void *base) const
476   {
477     unsigned int offset = *this;
478     if (HB_UNLIKELY (!offset)) return Null(Type);
479     return CONST_CAST(Type, *CharP(base), offset);
480   }
481
482   inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
483     TRACE_SANITIZE ();
484     if (!SANITIZE_SELF ()) return false;
485     unsigned int offset = *this;
486     if (HB_UNLIKELY (!offset)) return true;
487     return SANITIZE (CAST(Type, *CharP(base), offset)) || NEUTER (CAST(OffsetType,*this,0), 0);
488   }
489   inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
490     TRACE_SANITIZE ();
491     if (!SANITIZE_SELF ()) return false;
492     unsigned int offset = *this;
493     if (HB_UNLIKELY (!offset)) return true;
494     return SANITIZE_BASE (CAST(Type, *CharP(base), offset), base2) || NEUTER (CAST(OffsetType,*this,0), 0);
495   }
496   inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
497     TRACE_SANITIZE ();
498     if (!SANITIZE_SELF ()) return false;
499     unsigned int offset = *this;
500     if (HB_UNLIKELY (!offset)) return true;
501     return SANITIZE_BASE (CAST(Type, *CharP(base), offset), user_data) || NEUTER (CAST(OffsetType,*this,0), 0);
502   }
503 };
504 template <typename Base, typename OffsetType, typename Type>
505 inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
506
507 template <typename Type>
508 struct OffsetTo : GenericOffsetTo<Offset, Type> {};
509
510 template <typename Type>
511 struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
512
513
514 /*
515  * Array Types
516  */
517
518 template <typename LenType, typename Type>
519 struct GenericArrayOf
520 {
521   const Type *array(void) const { return &StructAfter<Type> (len); }
522   Type *array(void) { return &StructAfter<Type> (len); }
523
524   const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
525   {
526     unsigned int count = len;
527     if (HB_UNLIKELY (start_offset > count))
528       count = 0;
529     else
530       count -= start_offset;
531     count = MIN (count, *pcount);
532     *pcount = count;
533     return array() + start_offset;
534   }
535
536   inline const Type& operator [] (unsigned int i) const
537   {
538     if (HB_UNLIKELY (i >= len)) return Null(Type);
539     return array()[i];
540   }
541   inline unsigned int get_size () const
542   { return len.get_size () + len * Type::get_size (); }
543
544   inline bool sanitize_shallow (SANITIZE_ARG_DEF) {
545     TRACE_SANITIZE ();
546     return SANITIZE_SELF() && SANITIZE_ARRAY (this, Type::get_size (), len);
547   }
548
549   inline bool sanitize (SANITIZE_ARG_DEF) {
550     TRACE_SANITIZE ();
551     if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
552     /* Note: for structs that do not reference other structs,
553      * we do not need to call their sanitize() as we already did
554      * a bound check on the aggregate array size, hence the return.
555      */
556     return true;
557     /* We do keep this code though to make sure the structs pointed
558      * to do have a simple sanitize(), ie. they do not reference
559      * other structs. */
560     unsigned int count = len;
561     for (unsigned int i = 0; i < count; i++)
562       if (!SANITIZE (array()[i]))
563         return false;
564     return true;
565   }
566   inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
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))
572         return false;
573     return true;
574   }
575   inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
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, base2))
581         return false;
582     return true;
583   }
584   inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
585     TRACE_SANITIZE ();
586     if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
587     unsigned int count = len;
588     for (unsigned int i = 0; i < count; i++)
589       if (!array()[i].sanitize (SANITIZE_ARG, base, user_data))
590         return false;
591     return true;
592   }
593
594   LenType len;
595 /*Type array[VAR];*/
596 };
597
598 /* An array with a USHORT number of elements. */
599 template <typename Type>
600 struct ArrayOf : GenericArrayOf<USHORT, Type> {};
601
602 /* An array with a ULONG number of elements. */
603 template <typename Type>
604 struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
605
606 /* Array of Offset's */
607 template <typename Type>
608 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
609
610 /* Array of LongOffset's */
611 template <typename Type>
612 struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
613
614 /* LongArray of LongOffset's */
615 template <typename Type>
616 struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
617
618 /* Array of offsets relative to the beginning of the array itself. */
619 template <typename Type>
620 struct OffsetListOf : OffsetArrayOf<Type>
621 {
622   inline const Type& operator [] (unsigned int i) const
623   {
624     if (HB_UNLIKELY (i >= this->len)) return Null(Type);
625     return this+this->array()[i];
626   }
627
628   inline bool sanitize (SANITIZE_ARG_DEF) {
629     TRACE_SANITIZE ();
630     return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this));
631   }
632   inline bool sanitize (SANITIZE_ARG_DEF, unsigned int user_data) {
633     TRACE_SANITIZE ();
634     return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this), user_data);
635   }
636 };
637
638
639 /* An array with a USHORT number of elements,
640  * starting at second element. */
641 template <typename Type>
642 struct HeadlessArrayOf
643 {
644   const Type *array(void) const { return &StructAfter<Type> (len); }
645   Type *array(void) { return &StructAfter<Type> (len); }
646
647   inline const Type& operator [] (unsigned int i) const
648   {
649     if (HB_UNLIKELY (i >= len || !i)) return Null(Type);
650     return array()[i-1];
651   }
652   inline unsigned int get_size () const
653   { return len.get_size () + (len ? len - 1 : 0) * Type::get_size (); }
654
655   inline bool sanitize_shallow (SANITIZE_ARG_DEF) {
656     TRACE_SANITIZE ();
657     return SANITIZE_SELF() && SANITIZE_ARRAY (this, Type::get_size (), len);
658   }
659
660   inline bool sanitize (SANITIZE_ARG_DEF) {
661     TRACE_SANITIZE ();
662     if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
663     /* Note: for structs that do not reference other structs,
664      * we do not need to call their sanitize() as we already did
665      * a bound check on the aggregate array size, hence the return.
666      */
667     return true;
668     /* We do keep this code though to make sure the structs pointed
669      * to do have a simple sanitize(), ie. they do not reference
670      * other structs. */
671     unsigned int count = len ? len - 1 : 0;
672     Type *a = array();
673     for (unsigned int i = 0; i < count; i++)
674       if (!SANITIZE (a[i]))
675         return false;
676     return true;
677   }
678
679   USHORT len;
680 /*Type array[VAR];*/
681 };
682
683
684 #endif /* HB_OPEN_TYPE_PRIVATE_HH */