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