Rename const_sub_array to sub_array since all consts are implicit now
[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; \
86 template <> \
87 inline const Type& Null<Type> () { \
88   return CONST_CAST (Type, *_Null##Type, 0); \
89 }
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 #define SANITIZE_GET_SIZE() SANITIZE_SELF() && SANITIZE_MEM (this, this->get_size ())
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 (SANITIZE_ARG_DEF) {
545     TRACE_SANITIZE ();
546     if (!SANITIZE_GET_SIZE()) return false;
547     /* Note: for structs that do not reference other structs,
548      * we do not need to call their sanitize() as we already did
549      * a bound check on the aggregate array size, hence the return.
550      */
551     return true;
552     /* We do keep this code though to make sure the structs pointed
553      * to do have a simple sanitize(), ie. they do not reference
554      * other structs. */
555     unsigned int count = len;
556     for (unsigned int i = 0; i < count; i++)
557       if (!SANITIZE (array()[i]))
558         return false;
559     return true;
560   }
561   inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
562     TRACE_SANITIZE ();
563     if (!SANITIZE_GET_SIZE()) return false;
564     unsigned int count = len;
565     for (unsigned int i = 0; i < count; i++)
566       if (!array()[i].sanitize (SANITIZE_ARG, base))
567         return false;
568     return true;
569   }
570   inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
571     TRACE_SANITIZE ();
572     if (!SANITIZE_GET_SIZE()) return false;
573     unsigned int count = len;
574     for (unsigned int i = 0; i < count; i++)
575       if (!array()[i].sanitize (SANITIZE_ARG, base, base2))
576         return false;
577     return true;
578   }
579   inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
580     TRACE_SANITIZE ();
581     if (!SANITIZE_GET_SIZE()) return false;
582     unsigned int count = len;
583     for (unsigned int i = 0; i < count; i++)
584       if (!array()[i].sanitize (SANITIZE_ARG, base, user_data))
585         return false;
586     return true;
587   }
588
589   LenType len;
590 /*Type array[VAR];*/
591 };
592
593 /* An array with a USHORT number of elements. */
594 template <typename Type>
595 struct ArrayOf : GenericArrayOf<USHORT, Type> {};
596
597 /* An array with a ULONG number of elements. */
598 template <typename Type>
599 struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
600
601 /* Array of Offset's */
602 template <typename Type>
603 struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
604
605 /* Array of LongOffset's */
606 template <typename Type>
607 struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
608
609 /* LongArray of LongOffset's */
610 template <typename Type>
611 struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
612
613 /* Array of offsets relative to the beginning of the array itself. */
614 template <typename Type>
615 struct OffsetListOf : OffsetArrayOf<Type>
616 {
617   inline const Type& operator [] (unsigned int i) const
618   {
619     if (HB_UNLIKELY (i >= this->len)) return Null(Type);
620     return this+this->array()[i];
621   }
622
623   inline bool sanitize (SANITIZE_ARG_DEF) {
624     TRACE_SANITIZE ();
625     return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this));
626   }
627   inline bool sanitize (SANITIZE_ARG_DEF, unsigned int user_data) {
628     TRACE_SANITIZE ();
629     return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this), user_data);
630   }
631 };
632
633
634 /* An array with a USHORT number of elements,
635  * starting at second element. */
636 template <typename Type>
637 struct HeadlessArrayOf
638 {
639   const Type *array(void) const { return &StructAfter<Type> (len); }
640   Type *array(void) { return &StructAfter<Type> (len); }
641
642   inline const Type& operator [] (unsigned int i) const
643   {
644     if (HB_UNLIKELY (i >= len || !i)) return Null(Type);
645     return array()[i-1];
646   }
647   inline unsigned int get_size () const
648   { return len.get_size () + (len ? len - 1 : 0) * Type::get_size (); }
649
650   inline bool sanitize (SANITIZE_ARG_DEF) {
651     TRACE_SANITIZE ();
652     if (!SANITIZE_GET_SIZE()) return false;
653     /* Note: for structs that do not reference other structs,
654      * we do not need to call their sanitize() as we already did
655      * a bound check on the aggregate array size, hence the return.
656      */
657     return true;
658     /* We do keep this code though to make sure the structs pointed
659      * to do have a simple sanitize(), ie. they do not reference
660      * other structs. */
661     unsigned int count = len ? len - 1 : 0;
662     Type *a = array();
663     for (unsigned int i = 0; i < count; i++)
664       if (!SANITIZE (a[i]))
665         return false;
666     return true;
667   }
668
669   USHORT len;
670 /*Type array[VAR];*/
671 };
672
673
674 #endif /* HB_OPEN_TYPE_PRIVATE_HH */