1 /*
2  * Copyright © 2007,2008,2009,2010  Red Hat, Inc.
3  * Copyright © 2012  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
7  * Permission is hereby granted, without written agreement and without
8  * license or royalty fees, to use, copy, modify, and distribute this
9  * software and its documentation for any purpose, provided that the
10  * above copyright notice and the following two paragraphs appear in
11  * all copies of this software.
12  *
13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17  * DAMAGE.
18  *
19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24  *
25  * Red Hat Author(s): Behdad Esfahbod
26  * Google Author(s): Behdad Esfahbod
27  */
28 
29 #ifndef HB_OPEN_TYPE_HH
30 #define HB_OPEN_TYPE_HH
31 
32 #include "hb.hh"
33 #include "hb-blob.hh"
34 #include "hb-face.hh"
35 #include "hb-machinery.hh"
36 #include "hb-subset.hh"
37 
38 
39 namespace OT {
40 
41 
42 /*
43  *
44  * The OpenType Font File: Data Types
45  */
46 
47 
48 /* "The following data types are used in the OpenType font file.
49  *  All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
50 
51 /*
52  * Int types
53  */
54 
55 /* Integer types in big-endian order and no alignment requirement */
56 template <typename Type,
57 	  unsigned int Size = sizeof (Type)>
58 struct IntType
59 {
60   typedef Type type;
61 
62   IntType () = default;
IntTypeOT::IntType63   explicit constexpr IntType (Type V) : v {V} {}
operator =OT::IntType64   IntType& operator = (Type i) { v = i; return *this; }
65   /* For reason we define cast out operator for signed/unsigned, instead of Type, see:
66    * https://github.com/harfbuzz/harfbuzz/pull/2875/commits/09836013995cab2b9f07577a179ad7b024130467 */
operator hb_conditional<hb_is_signed(Type),signed,unsigned>OT::IntType67   operator hb_conditional<hb_is_signed (Type), signed, unsigned> () const { return v; }
68 
operator ==OT::IntType69   bool operator == (const IntType &o) const { return (Type) v == (Type) o.v; }
operator !=OT::IntType70   bool operator != (const IntType &o) const { return !(*this == o); }
71 
operator +=OT::IntType72   IntType& operator += (unsigned count) { *this = *this + count; return *this; }
operator -=OT::IntType73   IntType& operator -= (unsigned count) { *this = *this - count; return *this; }
operator ++OT::IntType74   IntType& operator ++ () { *this += 1; return *this; }
operator --OT::IntType75   IntType& operator -- () { *this -= 1; return *this; }
operator ++OT::IntType76   IntType operator ++ (int) { IntType c (*this); ++*this; return c; }
operator --OT::IntType77   IntType operator -- (int) { IntType c (*this); --*this; return c; }
78 
cmpOT::IntType79   HB_INTERNAL static int cmp (const IntType *a, const IntType *b)
80   { return b->cmp (*a); }
cmpOT::IntType81   HB_INTERNAL static int cmp (const void *a, const void *b)
82   {
83     IntType *pa = (IntType *) a;
84     IntType *pb = (IntType *) b;
85 
86     return pb->cmp (*pa);
87   }
88   template <typename Type2,
89 	    hb_enable_if (hb_is_integral (Type2) &&
90 			  sizeof (Type2) < sizeof (int) &&
91 			  sizeof (Type) < sizeof (int))>
cmpOT::IntType92   int cmp (Type2 a) const
93   {
94     Type b = v;
95     return (int) a - (int) b;
96   }
97   template <typename Type2,
98 	    hb_enable_if (hb_is_convertible (Type2, Type))>
cmpOT::IntType99   int cmp (Type2 a) const
100   {
101     Type b = v;
102     return a < b ? -1 : a == b ? 0 : +1;
103   }
sanitizeOT::IntType104   bool sanitize (hb_sanitize_context_t *c) const
105   {
106     TRACE_SANITIZE (this);
107     return_trace (likely (c->check_struct (this)));
108   }
109   protected:
110   BEInt<Type, Size> v;
111   public:
112   DEFINE_SIZE_STATIC (Size);
113 };
114 
115 typedef IntType<uint8_t>  HBUINT8;	/* 8-bit unsigned integer. */
116 typedef IntType<int8_t>   HBINT8;	/* 8-bit signed integer. */
117 typedef IntType<uint16_t> HBUINT16;	/* 16-bit unsigned integer. */
118 typedef IntType<int16_t>  HBINT16;	/* 16-bit signed integer. */
119 typedef IntType<uint32_t> HBUINT32;	/* 32-bit unsigned integer. */
120 typedef IntType<int32_t>  HBINT32;	/* 32-bit signed integer. */
121 /* Note: we cannot defined a signed HBINT24 because there's no corresponding C type.
122  * Works for unsigned, but not signed, since we rely on compiler for sign-extension. */
123 typedef IntType<uint32_t, 3> HBUINT24;	/* 24-bit unsigned integer. */
124 
125 /* 16-bit signed integer (HBINT16) that describes a quantity in FUnits. */
126 typedef HBINT16 FWORD;
127 
128 /* 32-bit signed integer (HBINT32) that describes a quantity in FUnits. */
129 typedef HBINT32 FWORD32;
130 
131 /* 16-bit unsigned integer (HBUINT16) that describes a quantity in FUnits. */
132 typedef HBUINT16 UFWORD;
133 
134 /* 16-bit signed fixed number with the low 14 bits of fraction (2.14). */
135 struct F2DOT14 : HBINT16
136 {
operator =OT::F2DOT14137   F2DOT14& operator = (uint16_t i ) { HBINT16::operator= (i); return *this; }
138   // 16384 means 1<<14
to_floatOT::F2DOT14139   float to_float () const  { return ((int32_t) v) / 16384.f; }
set_floatOT::F2DOT14140   void set_float (float f) { v = roundf (f * 16384.f); }
141   public:
142   DEFINE_SIZE_STATIC (2);
143 };
144 
145 /* 32-bit signed fixed-point number (16.16). */
146 struct HBFixed : HBINT32
147 {
operator =OT::HBFixed148   HBFixed& operator = (uint32_t i) { HBINT32::operator= (i); return *this; }
149   // 65536 means 1<<16
to_floatOT::HBFixed150   float to_float () const  { return ((int32_t) v) / 65536.f; }
set_floatOT::HBFixed151   void set_float (float f) { v = roundf (f * 65536.f); }
152   public:
153   DEFINE_SIZE_STATIC (4);
154 };
155 
156 /* Date represented in number of seconds since 12:00 midnight, January 1,
157  * 1904. The value is represented as a signed 64-bit integer. */
158 struct LONGDATETIME
159 {
sanitizeOT::LONGDATETIME160   bool sanitize (hb_sanitize_context_t *c) const
161   {
162     TRACE_SANITIZE (this);
163     return_trace (likely (c->check_struct (this)));
164   }
165   protected:
166   HBINT32 major;
167   HBUINT32 minor;
168   public:
169   DEFINE_SIZE_STATIC (8);
170 };
171 
172 /* Array of four uint8s (length = 32 bits) used to identify a script, language
173  * system, feature, or baseline */
174 struct Tag : HBUINT32
175 {
operator =OT::Tag176   Tag& operator = (hb_tag_t i) { HBUINT32::operator= (i); return *this; }
177   /* What the char* converters return is NOT nul-terminated.  Print using "%.4s" */
operator const char*OT::Tag178   operator const char* () const { return reinterpret_cast<const char *> (this); }
operator char*OT::Tag179   operator char* ()             { return reinterpret_cast<char *> (this); }
180   public:
181   DEFINE_SIZE_STATIC (4);
182 };
183 
184 /* Glyph index number, same as uint16 (length = 16 bits) */
185 struct HBGlyphID : HBUINT16
186 {
operator =OT::HBGlyphID187   HBGlyphID& operator = (uint16_t i) { HBUINT16::operator= (i); return *this; }
188 };
189 
190 /* Script/language-system/feature index */
191 struct Index : HBUINT16 {
192   static constexpr unsigned NOT_FOUND_INDEX = 0xFFFFu;
operator =OT::Index193   Index& operator = (uint16_t i) { HBUINT16::operator= (i); return *this; }
194 };
195 DECLARE_NULL_NAMESPACE_BYTES (OT, Index);
196 
197 typedef Index NameID;
198 
199 struct VarIdx : HBUINT32 {
200   static constexpr unsigned NO_VARIATION = 0xFFFFFFFFu;
operator =OT::VarIdx201   VarIdx& operator = (uint32_t i) { HBUINT32::operator= (i); return *this; }
202 };
203 DECLARE_NULL_NAMESPACE_BYTES (OT, VarIdx);
204 
205 /* Offset, Null offset = 0 */
206 template <typename Type, bool has_null=true>
207 struct Offset : Type
208 {
operator =OT::Offset209   Offset& operator = (typename Type::type i) { Type::operator= (i); return *this; }
210 
211   typedef Type type;
212 
is_nullOT::Offset213   bool is_null () const { return has_null && 0 == *this; }
214 
serializeOT::Offset215   void *serialize (hb_serialize_context_t *c, const void *base)
216   {
217     void *t = c->start_embed<void> ();
218     c->check_assign (*this,
219                      (unsigned) ((char *) t - (char *) base),
220                      HB_SERIALIZE_ERROR_OFFSET_OVERFLOW);
221     return t;
222   }
223 
224   public:
225   DEFINE_SIZE_STATIC (sizeof (Type));
226 };
227 
228 typedef Offset<HBUINT16> Offset16;
229 typedef Offset<HBUINT24> Offset24;
230 typedef Offset<HBUINT32> Offset32;
231 
232 
233 /* CheckSum */
234 struct CheckSum : HBUINT32
235 {
operator =OT::CheckSum236   CheckSum& operator = (uint32_t i) { HBUINT32::operator= (i); return *this; }
237 
238   /* This is reference implementation from the spec. */
CalcTableChecksumOT::CheckSum239   static uint32_t CalcTableChecksum (const HBUINT32 *Table, uint32_t Length)
240   {
241     uint32_t Sum = 0L;
242     assert (0 == (Length & 3));
243     const HBUINT32 *EndPtr = Table + Length / HBUINT32::static_size;
244 
245     while (Table < EndPtr)
246       Sum += *Table++;
247     return Sum;
248   }
249 
250   /* Note: data should be 4byte aligned and have 4byte padding at the end. */
set_for_dataOT::CheckSum251   void set_for_data (const void *data, unsigned int length)
252   { *this = CalcTableChecksum ((const HBUINT32 *) data, length); }
253 
254   public:
255   DEFINE_SIZE_STATIC (4);
256 };
257 
258 
259 /*
260  * Version Numbers
261  */
262 
263 template <typename FixedType=HBUINT16>
264 struct FixedVersion
265 {
to_intOT::FixedVersion266   uint32_t to_int () const { return (major << (sizeof (FixedType) * 8)) + minor; }
267 
sanitizeOT::FixedVersion268   bool sanitize (hb_sanitize_context_t *c) const
269   {
270     TRACE_SANITIZE (this);
271     return_trace (c->check_struct (this));
272   }
273 
274   FixedType major;
275   FixedType minor;
276   public:
277   DEFINE_SIZE_STATIC (2 * sizeof (FixedType));
278 };
279 
280 
281 /*
282  * Template subclasses of Offset that do the dereferencing.
283  * Use: (base+offset)
284  */
285 
286 template <typename Type, bool has_null>
287 struct _hb_has_null
288 {
get_nullOT::_hb_has_null289   static const Type *get_null () { return nullptr; }
get_crapOT::_hb_has_null290   static Type *get_crap ()       { return nullptr; }
291 };
292 template <typename Type>
293 struct _hb_has_null<Type, true>
294 {
get_nullOT::_hb_has_null295   static const Type *get_null () { return &Null (Type); }
get_crapOT::_hb_has_null296   static       Type *get_crap () { return &Crap (Type); }
297 };
298 
299 template <typename Type, typename OffsetType, bool has_null=true>
300 struct OffsetTo : Offset<OffsetType, has_null>
301 {
302   HB_DELETE_COPY_ASSIGN (OffsetTo);
303   OffsetTo () = default;
304 
operator =OT::OffsetTo305   OffsetTo& operator = (typename OffsetType::type i) { OffsetType::operator= (i); return *this; }
306 
operator ()OT::OffsetTo307   const Type& operator () (const void *base) const
308   {
309     if (unlikely (this->is_null ())) return *_hb_has_null<Type, has_null>::get_null ();
310     return StructAtOffset<const Type> (base, *this);
311   }
operator ()OT::OffsetTo312   Type& operator () (void *base) const
313   {
314     if (unlikely (this->is_null ())) return *_hb_has_null<Type, has_null>::get_crap ();
315     return StructAtOffset<Type> (base, *this);
316   }
317 
318   template <typename Base,
319 	    hb_enable_if (hb_is_convertible (const Base, const void *))>
operator +(const Base & base,const OffsetTo & offset)320   friend const Type& operator + (const Base &base, const OffsetTo &offset) { return offset ((const void *) base); }
321   template <typename Base,
322 	    hb_enable_if (hb_is_convertible (const Base, const void *))>
operator +(const OffsetTo & offset,const Base & base)323   friend const Type& operator + (const OffsetTo &offset, const Base &base) { return offset ((const void *) base); }
324   template <typename Base,
325 	    hb_enable_if (hb_is_convertible (Base, void *))>
operator +(Base && base,OffsetTo & offset)326   friend Type& operator + (Base &&base, OffsetTo &offset) { return offset ((void *) base); }
327   template <typename Base,
328 	    hb_enable_if (hb_is_convertible (Base, void *))>
operator +(OffsetTo & offset,Base && base)329   friend Type& operator + (OffsetTo &offset, Base &&base) { return offset ((void *) base); }
330 
serializeOT::OffsetTo331   Type& serialize (hb_serialize_context_t *c, const void *base)
332   {
333     return * (Type *) Offset<OffsetType>::serialize (c, base);
334   }
335 
336   template <typename ...Ts>
serialize_subsetOT::OffsetTo337   bool serialize_subset (hb_subset_context_t *c, const OffsetTo& src,
338 			 const void *src_base, Ts&&... ds)
339   {
340     *this = 0;
341     if (src.is_null ())
342       return false;
343 
344     auto *s = c->serializer;
345 
346     s->push ();
347 
348     bool ret = c->dispatch (src_base+src, hb_forward<Ts> (ds)...);
349 
350     if (ret || !has_null)
351       s->add_link (*this, s->pop_pack ());
352     else
353       s->pop_discard ();
354 
355     return ret;
356   }
357 
358   /* TODO: Somehow merge this with previous function into a serialize_dispatch(). */
359   /* Workaround clang bug: https://bugs.llvm.org/show_bug.cgi?id=23029
360    * Can't compile: whence = hb_serialize_context_t::Head followed by Ts&&...
361    */
362   template <typename ...Ts>
serialize_copyOT::OffsetTo363   bool serialize_copy (hb_serialize_context_t *c, const OffsetTo& src,
364 		       const void *src_base, unsigned dst_bias,
365 		       hb_serialize_context_t::whence_t whence,
366 		       Ts&&... ds)
367   {
368     *this = 0;
369     if (src.is_null ())
370       return false;
371 
372     c->push ();
373 
374     bool ret = c->copy (src_base+src, hb_forward<Ts> (ds)...);
375 
376     c->add_link (*this, c->pop_pack (), whence, dst_bias);
377 
378     return ret;
379   }
380 
serialize_copyOT::OffsetTo381   bool serialize_copy (hb_serialize_context_t *c, const OffsetTo& src,
382 		       const void *src_base, unsigned dst_bias = 0)
383   { return serialize_copy (c, src, src_base, dst_bias, hb_serialize_context_t::Head); }
384 
sanitize_shallowOT::OffsetTo385   bool sanitize_shallow (hb_sanitize_context_t *c, const void *base) const
386   {
387     TRACE_SANITIZE (this);
388     if (unlikely (!c->check_struct (this))) return_trace (false);
389     if (unlikely (this->is_null ())) return_trace (true);
390     if (unlikely ((const char *) base + (unsigned) *this < (const char *) base)) return_trace (false);
391     return_trace (true);
392   }
393 
394   template <typename ...Ts>
sanitizeOT::OffsetTo395   bool sanitize (hb_sanitize_context_t *c, const void *base, Ts&&... ds) const
396   {
397     TRACE_SANITIZE (this);
398     return_trace (sanitize_shallow (c, base) &&
399 		  (this->is_null () ||
400 		   c->dispatch (StructAtOffset<Type> (base, *this), hb_forward<Ts> (ds)...) ||
401 		   neuter (c)));
402   }
403 
404   /* Set the offset to Null */
neuterOT::OffsetTo405   bool neuter (hb_sanitize_context_t *c) const
406   {
407     if (!has_null) return false;
408     return c->try_set (this, 0);
409   }
410   DEFINE_SIZE_STATIC (sizeof (OffsetType));
411 };
412 /* Partial specializations. */
413 template <typename Type, bool has_null=true> using Offset16To = OffsetTo<Type, HBUINT16, has_null>;
414 template <typename Type, bool has_null=true> using Offset24To = OffsetTo<Type, HBUINT24, has_null>;
415 template <typename Type, bool has_null=true> using Offset32To = OffsetTo<Type, HBUINT32, has_null>;
416 
417 template <typename Type, typename OffsetType> using NNOffsetTo = OffsetTo<Type, OffsetType, false>;
418 template <typename Type> using NNOffset16To = Offset16To<Type, false>;
419 template <typename Type> using NNOffset24To = Offset24To<Type, false>;
420 template <typename Type> using NNOffset32To = Offset32To<Type, false>;
421 
422 
423 /*
424  * Array Types
425  */
426 
427 template <typename Type>
428 struct UnsizedArrayOf
429 {
430   typedef Type item_t;
431   static constexpr unsigned item_size = hb_static_size (Type);
432 
433   HB_DELETE_CREATE_COPY_ASSIGN (UnsizedArrayOf);
434 
operator []OT::UnsizedArrayOf435   const Type& operator [] (int i_) const
436   {
437     unsigned int i = (unsigned int) i_;
438     const Type *p = &arrayZ[i];
439     if (unlikely (p < arrayZ)) return Null (Type); /* Overflowed. */
440     return *p;
441   }
operator []OT::UnsizedArrayOf442   Type& operator [] (int i_)
443   {
444     unsigned int i = (unsigned int) i_;
445     Type *p = &arrayZ[i];
446     if (unlikely (p < arrayZ)) return Crap (Type); /* Overflowed. */
447     return *p;
448   }
449 
get_sizeOT::UnsizedArrayOf450   unsigned int get_size (unsigned int len) const
451   { return len * Type::static_size; }
452 
operator T*OT::UnsizedArrayOf453   template <typename T> operator T * () { return arrayZ; }
operator const T*OT::UnsizedArrayOf454   template <typename T> operator const T * () const { return arrayZ; }
as_arrayOT::UnsizedArrayOf455   hb_array_t<Type> as_array (unsigned int len)
456   { return hb_array (arrayZ, len); }
as_arrayOT::UnsizedArrayOf457   hb_array_t<const Type> as_array (unsigned int len) const
458   { return hb_array (arrayZ, len); }
459 
460   template <typename T>
lsearchOT::UnsizedArrayOf461   Type &lsearch (unsigned int len, const T &x, Type &not_found = Crap (Type))
462   { return *as_array (len).lsearch (x, &not_found); }
463   template <typename T>
lsearchOT::UnsizedArrayOf464   const Type &lsearch (unsigned int len, const T &x, const Type &not_found = Null (Type)) const
465   { return *as_array (len).lsearch (x, &not_found); }
466   template <typename T>
lfindOT::UnsizedArrayOf467   bool lfind (unsigned int len, const T &x, unsigned *pos = nullptr) const
468   { return as_array (len).lfind (x, pos); }
469 
qsortOT::UnsizedArrayOf470   void qsort (unsigned int len, unsigned int start = 0, unsigned int end = (unsigned int) -1)
471   { as_array (len).qsort (start, end); }
472 
serializeOT::UnsizedArrayOf473   bool serialize (hb_serialize_context_t *c, unsigned int items_len)
474   {
475     TRACE_SERIALIZE (this);
476     if (unlikely (!c->extend (*this, items_len))) return_trace (false);
477     return_trace (true);
478   }
479   template <typename Iterator,
480 	    hb_requires (hb_is_source_of (Iterator, Type))>
serializeOT::UnsizedArrayOf481   bool serialize (hb_serialize_context_t *c, Iterator items)
482   {
483     TRACE_SERIALIZE (this);
484     unsigned count = items.len ();
485     if (unlikely (!serialize (c, count))) return_trace (false);
486     /* TODO Umm. Just exhaust the iterator instead?  Being extra
487      * cautious right now.. */
488     for (unsigned i = 0; i < count; i++, ++items)
489       arrayZ[i] = *items;
490     return_trace (true);
491   }
492 
copyOT::UnsizedArrayOf493   UnsizedArrayOf* copy (hb_serialize_context_t *c, unsigned count) const
494   {
495     TRACE_SERIALIZE (this);
496     auto *out = c->start_embed (this);
497     if (unlikely (!as_array (count).copy (c))) return_trace (nullptr);
498     return_trace (out);
499   }
500 
501   template <typename ...Ts>
sanitizeOT::UnsizedArrayOf502   bool sanitize (hb_sanitize_context_t *c, unsigned int count, Ts&&... ds) const
503   {
504     TRACE_SANITIZE (this);
505     if (unlikely (!sanitize_shallow (c, count))) return_trace (false);
506     if (!sizeof... (Ts) && hb_is_trivially_copyable (Type)) return_trace (true);
507     for (unsigned int i = 0; i < count; i++)
508       if (unlikely (!c->dispatch (arrayZ[i], hb_forward<Ts> (ds)...)))
509 	return_trace (false);
510     return_trace (true);
511   }
512 
sanitize_shallowOT::UnsizedArrayOf513   bool sanitize_shallow (hb_sanitize_context_t *c, unsigned int count) const
514   {
515     TRACE_SANITIZE (this);
516     return_trace (c->check_array (arrayZ, count));
517   }
518 
519   public:
520   Type		arrayZ[HB_VAR_ARRAY];
521   public:
522   DEFINE_SIZE_UNBOUNDED (0);
523 };
524 
525 /* Unsized array of offset's */
526 template <typename Type, typename OffsetType, bool has_null=true>
527 using UnsizedArray16OfOffsetTo = UnsizedArrayOf<OffsetTo<Type, OffsetType, has_null>>;
528 
529 /* Unsized array of offsets relative to the beginning of the array itself. */
530 template <typename Type, typename OffsetType, bool has_null=true>
531 struct UnsizedListOfOffset16To : UnsizedArray16OfOffsetTo<Type, OffsetType, has_null>
532 {
operator []OT::UnsizedListOfOffset16To533   const Type& operator [] (int i_) const
534   {
535     unsigned int i = (unsigned int) i_;
536     const OffsetTo<Type, OffsetType, has_null> *p = &this->arrayZ[i];
537     if (unlikely (p < this->arrayZ)) return Null (Type); /* Overflowed. */
538     return this+*p;
539   }
operator []OT::UnsizedListOfOffset16To540   Type& operator [] (int i_)
541   {
542     unsigned int i = (unsigned int) i_;
543     const OffsetTo<Type, OffsetType, has_null> *p = &this->arrayZ[i];
544     if (unlikely (p < this->arrayZ)) return Crap (Type); /* Overflowed. */
545     return this+*p;
546   }
547 
548   template <typename ...Ts>
sanitizeOT::UnsizedListOfOffset16To549   bool sanitize (hb_sanitize_context_t *c, unsigned int count, Ts&&... ds) const
550   {
551     TRACE_SANITIZE (this);
552     return_trace ((UnsizedArray16OfOffsetTo<Type, OffsetType, has_null>
553 		   ::sanitize (c, count, this, hb_forward<Ts> (ds)...)));
554   }
555 };
556 
557 /* An array with sorted elements.  Supports binary searching. */
558 template <typename Type>
559 struct SortedUnsizedArrayOf : UnsizedArrayOf<Type>
560 {
as_arrayOT::SortedUnsizedArrayOf561   hb_sorted_array_t<Type> as_array (unsigned int len)
562   { return hb_sorted_array (this->arrayZ, len); }
as_arrayOT::SortedUnsizedArrayOf563   hb_sorted_array_t<const Type> as_array (unsigned int len) const
564   { return hb_sorted_array (this->arrayZ, len); }
operator hb_sorted_array_t<Type>OT::SortedUnsizedArrayOf565   operator hb_sorted_array_t<Type> ()             { return as_array (); }
operator hb_sorted_array_t<const Type>OT::SortedUnsizedArrayOf566   operator hb_sorted_array_t<const Type> () const { return as_array (); }
567 
568   template <typename T>
bsearchOT::SortedUnsizedArrayOf569   Type &bsearch (unsigned int len, const T &x, Type &not_found = Crap (Type))
570   { return *as_array (len).bsearch (x, &not_found); }
571   template <typename T>
bsearchOT::SortedUnsizedArrayOf572   const Type &bsearch (unsigned int len, const T &x, const Type &not_found = Null (Type)) const
573   { return *as_array (len).bsearch (x, &not_found); }
574   template <typename T>
bfindOT::SortedUnsizedArrayOf575   bool bfind (unsigned int len, const T &x, unsigned int *i = nullptr,
576 	      hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
577 	      unsigned int to_store = (unsigned int) -1) const
578   { return as_array (len).bfind (x, i, not_found, to_store); }
579 };
580 
581 
582 /* An array with a number of elements. */
583 template <typename Type, typename LenType>
584 struct ArrayOf
585 {
586   typedef Type item_t;
587   static constexpr unsigned item_size = hb_static_size (Type);
588 
589   HB_DELETE_CREATE_COPY_ASSIGN (ArrayOf);
590 
operator []OT::ArrayOf591   const Type& operator [] (int i_) const
592   {
593     unsigned int i = (unsigned int) i_;
594     if (unlikely (i >= len)) return Null (Type);
595     return arrayZ[i];
596   }
operator []OT::ArrayOf597   Type& operator [] (int i_)
598   {
599     unsigned int i = (unsigned int) i_;
600     if (unlikely (i >= len)) return Crap (Type);
601     return arrayZ[i];
602   }
603 
get_sizeOT::ArrayOf604   unsigned int get_size () const
605   { return len.static_size + len * Type::static_size; }
606 
operator boolOT::ArrayOf607   explicit operator bool () const { return len; }
608 
popOT::ArrayOf609   void pop () { len--; }
610 
as_arrayOT::ArrayOf611   hb_array_t<      Type> as_array ()       { return hb_array (arrayZ, len); }
as_arrayOT::ArrayOf612   hb_array_t<const Type> as_array () const { return hb_array (arrayZ, len); }
613 
614   /* Iterator. */
615   typedef hb_array_t<const Type>   iter_t;
616   typedef hb_array_t<      Type> writer_t;
iterOT::ArrayOf617     iter_t   iter () const { return as_array (); }
writerOT::ArrayOf618   writer_t writer ()       { return as_array (); }
operator iter_tOT::ArrayOf619   operator   iter_t () const { return   iter (); }
operator writer_tOT::ArrayOf620   operator writer_t ()       { return writer (); }
621 
sub_arrayOT::ArrayOf622   hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int count) const
623   { return as_array ().sub_array (start_offset, count); }
sub_arrayOT::ArrayOf624   hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const
625   { return as_array ().sub_array (start_offset, count); }
sub_arrayOT::ArrayOf626   hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int count)
627   { return as_array ().sub_array (start_offset, count); }
sub_arrayOT::ArrayOf628   hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
629   { return as_array ().sub_array (start_offset, count); }
630 
631   template <typename T>
lsearchOT::ArrayOf632   Type &lsearch (const T &x, Type &not_found = Crap (Type))
633   { return *as_array ().lsearch (x, &not_found); }
634   template <typename T>
lsearchOT::ArrayOf635   const Type &lsearch (const T &x, const Type &not_found = Null (Type)) const
636   { return *as_array ().lsearch (x, &not_found); }
637   template <typename T>
lfindOT::ArrayOf638   bool lfind (const T &x, unsigned *pos = nullptr) const
639   { return as_array ().lfind (x, pos); }
640 
qsortOT::ArrayOf641   void qsort (unsigned int start = 0, unsigned int end = (unsigned int) -1)
642   { as_array ().qsort (start, end); }
643 
serializeOT::ArrayOf644   HB_NODISCARD bool serialize (hb_serialize_context_t *c, unsigned items_len)
645   {
646     TRACE_SERIALIZE (this);
647     if (unlikely (!c->extend_min (*this))) return_trace (false);
648     c->check_assign (len, items_len, HB_SERIALIZE_ERROR_ARRAY_OVERFLOW);
649     if (unlikely (!c->extend (*this))) return_trace (false);
650     return_trace (true);
651   }
652   template <typename Iterator,
653 	    hb_requires (hb_is_source_of (Iterator, Type))>
serializeOT::ArrayOf654   HB_NODISCARD bool serialize (hb_serialize_context_t *c, Iterator items)
655   {
656     TRACE_SERIALIZE (this);
657     unsigned count = items.len ();
658     if (unlikely (!serialize (c, count))) return_trace (false);
659     /* TODO Umm. Just exhaust the iterator instead?  Being extra
660      * cautious right now.. */
661     for (unsigned i = 0; i < count; i++, ++items)
662       arrayZ[i] = *items;
663     return_trace (true);
664   }
665 
serialize_appendOT::ArrayOf666   Type* serialize_append (hb_serialize_context_t *c)
667   {
668     TRACE_SERIALIZE (this);
669     len++;
670     if (unlikely (!len || !c->extend (*this)))
671     {
672       len--;
673       return_trace (nullptr);
674     }
675     return_trace (&arrayZ[len - 1]);
676   }
677 
copyOT::ArrayOf678   ArrayOf* copy (hb_serialize_context_t *c) const
679   {
680     TRACE_SERIALIZE (this);
681     auto *out = c->start_embed (this);
682     if (unlikely (!c->extend_min (out))) return_trace (nullptr);
683     c->check_assign (out->len, len, HB_SERIALIZE_ERROR_ARRAY_OVERFLOW);
684     if (unlikely (!as_array ().copy (c))) return_trace (nullptr);
685     return_trace (out);
686   }
687 
688   template <typename ...Ts>
sanitizeOT::ArrayOf689   bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
690   {
691     TRACE_SANITIZE (this);
692     if (unlikely (!sanitize_shallow (c))) return_trace (false);
693     if (!sizeof... (Ts) && hb_is_trivially_copyable (Type)) return_trace (true);
694     unsigned int count = len;
695     for (unsigned int i = 0; i < count; i++)
696       if (unlikely (!c->dispatch (arrayZ[i], hb_forward<Ts> (ds)...)))
697 	return_trace (false);
698     return_trace (true);
699   }
700 
sanitize_shallowOT::ArrayOf701   bool sanitize_shallow (hb_sanitize_context_t *c) const
702   {
703     TRACE_SANITIZE (this);
704     return_trace (len.sanitize (c) && c->check_array (arrayZ, len));
705   }
706 
707   public:
708   LenType	len;
709   Type		arrayZ[HB_VAR_ARRAY];
710   public:
711   DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
712 };
713 template <typename Type> using Array16Of = ArrayOf<Type, HBUINT16>;
714 template <typename Type> using Array32Of = ArrayOf<Type, HBUINT32>;
715 using PString = ArrayOf<HBUINT8, HBUINT8>;
716 
717 /* Array of Offset's */
718 template <typename Type> using Array16OfOffset16To = ArrayOf<OffsetTo<Type, HBUINT16>, HBUINT16>;
719 template <typename Type> using Array16OfOffset32To = ArrayOf<OffsetTo<Type, HBUINT32>, HBUINT16>;
720 template <typename Type> using Array32OfOffset32To = ArrayOf<OffsetTo<Type, HBUINT32>, HBUINT32>;
721 
722 /* Array of offsets relative to the beginning of the array itself. */
723 template <typename Type>
724 struct List16OfOffset16To : Array16OfOffset16To<Type>
725 {
operator []OT::List16OfOffset16To726   const Type& operator [] (int i_) const
727   {
728     unsigned int i = (unsigned int) i_;
729     if (unlikely (i >= this->len)) return Null (Type);
730     return this+this->arrayZ[i];
731   }
operator []OT::List16OfOffset16To732   const Type& operator [] (int i_)
733   {
734     unsigned int i = (unsigned int) i_;
735     if (unlikely (i >= this->len)) return Crap (Type);
736     return this+this->arrayZ[i];
737   }
738 
subsetOT::List16OfOffset16To739   bool subset (hb_subset_context_t *c) const
740   {
741     TRACE_SUBSET (this);
742     struct List16OfOffset16To<Type> *out = c->serializer->embed (*this);
743     if (unlikely (!out)) return_trace (false);
744     unsigned int count = this->len;
745     for (unsigned int i = 0; i < count; i++)
746       out->arrayZ[i].serialize_subset (c, this->arrayZ[i], this, out);
747     return_trace (true);
748   }
749 
750   template <typename ...Ts>
sanitizeOT::List16OfOffset16To751   bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
752   {
753     TRACE_SANITIZE (this);
754     return_trace (Array16OfOffset16To<Type>::sanitize (c, this, hb_forward<Ts> (ds)...));
755   }
756 };
757 
758 /* An array starting at second element. */
759 template <typename Type, typename LenType=HBUINT16>
760 struct HeadlessArrayOf
761 {
762   static constexpr unsigned item_size = Type::static_size;
763 
764   HB_DELETE_CREATE_COPY_ASSIGN (HeadlessArrayOf);
765 
operator []OT::HeadlessArrayOf766   const Type& operator [] (int i_) const
767   {
768     unsigned int i = (unsigned int) i_;
769     if (unlikely (i >= lenP1 || !i)) return Null (Type);
770     return arrayZ[i-1];
771   }
operator []OT::HeadlessArrayOf772   Type& operator [] (int i_)
773   {
774     unsigned int i = (unsigned int) i_;
775     if (unlikely (i >= lenP1 || !i)) return Crap (Type);
776     return arrayZ[i-1];
777   }
get_sizeOT::HeadlessArrayOf778   unsigned int get_size () const
779   { return lenP1.static_size + get_length () * Type::static_size; }
780 
get_lengthOT::HeadlessArrayOf781   unsigned get_length () const { return lenP1 ? lenP1 - 1 : 0; }
782 
as_arrayOT::HeadlessArrayOf783   hb_array_t<      Type> as_array ()       { return hb_array (arrayZ, get_length ()); }
as_arrayOT::HeadlessArrayOf784   hb_array_t<const Type> as_array () const { return hb_array (arrayZ, get_length ()); }
785 
786   /* Iterator. */
787   typedef hb_array_t<const Type>   iter_t;
788   typedef hb_array_t<      Type> writer_t;
iterOT::HeadlessArrayOf789     iter_t   iter () const { return as_array (); }
writerOT::HeadlessArrayOf790   writer_t writer ()       { return as_array (); }
operator iter_tOT::HeadlessArrayOf791   operator   iter_t () const { return   iter (); }
operator writer_tOT::HeadlessArrayOf792   operator writer_t ()       { return writer (); }
793 
serializeOT::HeadlessArrayOf794   bool serialize (hb_serialize_context_t *c, unsigned int items_len)
795   {
796     TRACE_SERIALIZE (this);
797     if (unlikely (!c->extend_min (*this))) return_trace (false);
798     c->check_assign (lenP1, items_len + 1, HB_SERIALIZE_ERROR_ARRAY_OVERFLOW);
799     if (unlikely (!c->extend (*this))) return_trace (false);
800     return_trace (true);
801   }
802   template <typename Iterator,
803 	    hb_requires (hb_is_source_of (Iterator, Type))>
serializeOT::HeadlessArrayOf804   bool serialize (hb_serialize_context_t *c, Iterator items)
805   {
806     TRACE_SERIALIZE (this);
807     unsigned count = items.len ();
808     if (unlikely (!serialize (c, count))) return_trace (false);
809     /* TODO Umm. Just exhaust the iterator instead?  Being extra
810      * cautious right now.. */
811     for (unsigned i = 0; i < count; i++, ++items)
812       arrayZ[i] = *items;
813     return_trace (true);
814   }
815 
816   template <typename ...Ts>
sanitizeOT::HeadlessArrayOf817   bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
818   {
819     TRACE_SANITIZE (this);
820     if (unlikely (!sanitize_shallow (c))) return_trace (false);
821     if (!sizeof... (Ts) && hb_is_trivially_copyable (Type)) return_trace (true);
822     unsigned int count = get_length ();
823     for (unsigned int i = 0; i < count; i++)
824       if (unlikely (!c->dispatch (arrayZ[i], hb_forward<Ts> (ds)...)))
825 	return_trace (false);
826     return_trace (true);
827   }
828 
829   private:
sanitize_shallowOT::HeadlessArrayOf830   bool sanitize_shallow (hb_sanitize_context_t *c) const
831   {
832     TRACE_SANITIZE (this);
833     return_trace (lenP1.sanitize (c) &&
834 		  (!lenP1 || c->check_array (arrayZ, lenP1 - 1)));
835   }
836 
837   public:
838   LenType	lenP1;
839   Type		arrayZ[HB_VAR_ARRAY];
840   public:
841   DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
842 };
843 
844 /* An array storing length-1. */
845 template <typename Type, typename LenType=HBUINT16>
846 struct ArrayOfM1
847 {
848   HB_DELETE_CREATE_COPY_ASSIGN (ArrayOfM1);
849 
operator []OT::ArrayOfM1850   const Type& operator [] (int i_) const
851   {
852     unsigned int i = (unsigned int) i_;
853     if (unlikely (i > lenM1)) return Null (Type);
854     return arrayZ[i];
855   }
operator []OT::ArrayOfM1856   Type& operator [] (int i_)
857   {
858     unsigned int i = (unsigned int) i_;
859     if (unlikely (i > lenM1)) return Crap (Type);
860     return arrayZ[i];
861   }
get_sizeOT::ArrayOfM1862   unsigned int get_size () const
863   { return lenM1.static_size + (lenM1 + 1) * Type::static_size; }
864 
865   template <typename ...Ts>
sanitizeOT::ArrayOfM1866   bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
867   {
868     TRACE_SANITIZE (this);
869     if (unlikely (!sanitize_shallow (c))) return_trace (false);
870     if (!sizeof... (Ts) && hb_is_trivially_copyable (Type)) return_trace (true);
871     unsigned int count = lenM1 + 1;
872     for (unsigned int i = 0; i < count; i++)
873       if (unlikely (!c->dispatch (arrayZ[i], hb_forward<Ts> (ds)...)))
874 	return_trace (false);
875     return_trace (true);
876   }
877 
878   private:
sanitize_shallowOT::ArrayOfM1879   bool sanitize_shallow (hb_sanitize_context_t *c) const
880   {
881     TRACE_SANITIZE (this);
882     return_trace (lenM1.sanitize (c) &&
883 		  (c->check_array (arrayZ, lenM1 + 1)));
884   }
885 
886   public:
887   LenType	lenM1;
888   Type		arrayZ[HB_VAR_ARRAY];
889   public:
890   DEFINE_SIZE_ARRAY (sizeof (LenType), arrayZ);
891 };
892 
893 /* An array with sorted elements.  Supports binary searching. */
894 template <typename Type, typename LenType>
895 struct SortedArrayOf : ArrayOf<Type, LenType>
896 {
as_arrayOT::SortedArrayOf897   hb_sorted_array_t<      Type> as_array ()       { return hb_sorted_array (this->arrayZ, this->len); }
as_arrayOT::SortedArrayOf898   hb_sorted_array_t<const Type> as_array () const { return hb_sorted_array (this->arrayZ, this->len); }
899 
900   /* Iterator. */
901   typedef hb_sorted_array_t<const Type>   iter_t;
902   typedef hb_sorted_array_t<      Type> writer_t;
iterOT::SortedArrayOf903     iter_t   iter () const { return as_array (); }
writerOT::SortedArrayOf904   writer_t writer ()       { return as_array (); }
operator iter_tOT::SortedArrayOf905   operator   iter_t () const { return   iter (); }
operator writer_tOT::SortedArrayOf906   operator writer_t ()       { return writer (); }
907 
sub_arrayOT::SortedArrayOf908   hb_sorted_array_t<const Type> sub_array (unsigned int start_offset, unsigned int count) const
909   { return as_array ().sub_array (start_offset, count); }
sub_arrayOT::SortedArrayOf910   hb_sorted_array_t<const Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const
911   { return as_array ().sub_array (start_offset, count); }
sub_arrayOT::SortedArrayOf912   hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int count)
913   { return as_array ().sub_array (start_offset, count); }
sub_arrayOT::SortedArrayOf914   hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
915   { return as_array ().sub_array (start_offset, count); }
916 
serializeOT::SortedArrayOf917   bool serialize (hb_serialize_context_t *c, unsigned int items_len)
918   {
919     TRACE_SERIALIZE (this);
920     bool ret = ArrayOf<Type, LenType>::serialize (c, items_len);
921     return_trace (ret);
922   }
923   template <typename Iterator,
924 	    hb_requires (hb_is_sorted_source_of (Iterator, Type))>
serializeOT::SortedArrayOf925   bool serialize (hb_serialize_context_t *c, Iterator items)
926   {
927     TRACE_SERIALIZE (this);
928     bool ret = ArrayOf<Type, LenType>::serialize (c, items);
929     return_trace (ret);
930   }
931 
932   template <typename T>
bsearchOT::SortedArrayOf933   Type &bsearch (const T &x, Type &not_found = Crap (Type))
934   { return *as_array ().bsearch (x, &not_found); }
935   template <typename T>
bsearchOT::SortedArrayOf936   const Type &bsearch (const T &x, const Type &not_found = Null (Type)) const
937   { return *as_array ().bsearch (x, &not_found); }
938   template <typename T>
bfindOT::SortedArrayOf939   bool bfind (const T &x, unsigned int *i = nullptr,
940 	      hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
941 	      unsigned int to_store = (unsigned int) -1) const
942   { return as_array ().bfind (x, i, not_found, to_store); }
943 };
944 
945 template <typename Type> using SortedArray16Of = SortedArrayOf<Type, HBUINT16>;
946 template <typename Type> using SortedArray32Of = SortedArrayOf<Type, HBUINT32>;
947 
948 /*
949  * Binary-search arrays
950  */
951 
952 template <typename LenType=HBUINT16>
953 struct BinSearchHeader
954 {
operator uint32_tOT::BinSearchHeader955   operator uint32_t () const { return len; }
956 
sanitizeOT::BinSearchHeader957   bool sanitize (hb_sanitize_context_t *c) const
958   {
959     TRACE_SANITIZE (this);
960     return_trace (c->check_struct (this));
961   }
962 
operator =OT::BinSearchHeader963   BinSearchHeader& operator = (unsigned int v)
964   {
965     len = v;
966     assert (len == v);
967     entrySelector = hb_max (1u, hb_bit_storage (v)) - 1;
968     searchRange = 16 * (1u << entrySelector);
969     rangeShift = v * 16 > searchRange
970 		 ? 16 * v - searchRange
971 		 : 0;
972     return *this;
973   }
974 
975   protected:
976   LenType	len;
977   LenType	searchRange;
978   LenType	entrySelector;
979   LenType	rangeShift;
980 
981   public:
982   DEFINE_SIZE_STATIC (8);
983 };
984 
985 template <typename Type, typename LenType=HBUINT16>
986 using BinSearchArrayOf = SortedArrayOf<Type, BinSearchHeader<LenType>>;
987 
988 
989 struct VarSizedBinSearchHeader
990 {
991 
sanitizeOT::VarSizedBinSearchHeader992   bool sanitize (hb_sanitize_context_t *c) const
993   {
994     TRACE_SANITIZE (this);
995     return_trace (c->check_struct (this));
996   }
997 
998   HBUINT16	unitSize;	/* Size of a lookup unit for this search in bytes. */
999   HBUINT16	nUnits;		/* Number of units of the preceding size to be searched. */
1000   HBUINT16	searchRange;	/* The value of unitSize times the largest power of 2
1001 				 * that is less than or equal to the value of nUnits. */
1002   HBUINT16	entrySelector;	/* The log base 2 of the largest power of 2 less than
1003 				 * or equal to the value of nUnits. */
1004   HBUINT16	rangeShift;	/* The value of unitSize times the difference of the
1005 				 * value of nUnits minus the largest power of 2 less
1006 				 * than or equal to the value of nUnits. */
1007   public:
1008   DEFINE_SIZE_STATIC (10);
1009 };
1010 
1011 template <typename Type>
1012 struct VarSizedBinSearchArrayOf
1013 {
1014   static constexpr unsigned item_size = Type::static_size;
1015 
1016   HB_DELETE_CREATE_COPY_ASSIGN (VarSizedBinSearchArrayOf);
1017 
last_is_terminatorOT::VarSizedBinSearchArrayOf1018   bool last_is_terminator () const
1019   {
1020     if (unlikely (!header.nUnits)) return false;
1021 
1022     /* Gah.
1023      *
1024      * "The number of termination values that need to be included is table-specific.
1025      * The value that indicates binary search termination is 0xFFFF." */
1026     const HBUINT16 *words = &StructAtOffset<HBUINT16> (&bytesZ, (header.nUnits - 1) * header.unitSize);
1027     unsigned int count = Type::TerminationWordCount;
1028     for (unsigned int i = 0; i < count; i++)
1029       if (words[i] != 0xFFFFu)
1030 	return false;
1031     return true;
1032   }
1033 
operator []OT::VarSizedBinSearchArrayOf1034   const Type& operator [] (int i_) const
1035   {
1036     unsigned int i = (unsigned int) i_;
1037     if (unlikely (i >= get_length ())) return Null (Type);
1038     return StructAtOffset<Type> (&bytesZ, i * header.unitSize);
1039   }
operator []OT::VarSizedBinSearchArrayOf1040   Type& operator [] (int i_)
1041   {
1042     unsigned int i = (unsigned int) i_;
1043     if (unlikely (i >= get_length ())) return Crap (Type);
1044     return StructAtOffset<Type> (&bytesZ, i * header.unitSize);
1045   }
get_lengthOT::VarSizedBinSearchArrayOf1046   unsigned int get_length () const
1047   { return header.nUnits - last_is_terminator (); }
get_sizeOT::VarSizedBinSearchArrayOf1048   unsigned int get_size () const
1049   { return header.static_size + header.nUnits * header.unitSize; }
1050 
1051   template <typename ...Ts>
sanitizeOT::VarSizedBinSearchArrayOf1052   bool sanitize (hb_sanitize_context_t *c, Ts&&... ds) const
1053   {
1054     TRACE_SANITIZE (this);
1055     if (unlikely (!sanitize_shallow (c))) return_trace (false);
1056     if (!sizeof... (Ts) && hb_is_trivially_copyable (Type)) return_trace (true);
1057     unsigned int count = get_length ();
1058     for (unsigned int i = 0; i < count; i++)
1059       if (unlikely (!(*this)[i].sanitize (c, hb_forward<Ts> (ds)...)))
1060 	return_trace (false);
1061     return_trace (true);
1062   }
1063 
1064   template <typename T>
bsearchOT::VarSizedBinSearchArrayOf1065   const Type *bsearch (const T &key) const
1066   {
1067     unsigned pos;
1068     return hb_bsearch_impl (&pos,
1069 			    key,
1070 			    (const void *) bytesZ,
1071 			    get_length (),
1072 			    header.unitSize,
1073 			    _hb_cmp_method<T, Type>)
1074 	   ? (const Type *) (((const char *) &bytesZ) + (pos * header.unitSize))
1075 	   : nullptr;
1076   }
1077 
1078   private:
sanitize_shallowOT::VarSizedBinSearchArrayOf1079   bool sanitize_shallow (hb_sanitize_context_t *c) const
1080   {
1081     TRACE_SANITIZE (this);
1082     return_trace (header.sanitize (c) &&
1083 		  Type::static_size <= header.unitSize &&
1084 		  c->check_range (bytesZ.arrayZ,
1085 				  header.nUnits,
1086 				  header.unitSize));
1087   }
1088 
1089   protected:
1090   VarSizedBinSearchHeader	header;
1091   UnsizedArrayOf<HBUINT8>	bytesZ;
1092   public:
1093   DEFINE_SIZE_ARRAY (10, bytesZ);
1094 };
1095 
1096 
1097 } /* namespace OT */
1098 
1099 
1100 #endif /* HB_OPEN_TYPE_HH */
1101