blob: 2572b3e99fe5bf44f0285fc7cd7c478ed1f873e4 [file] [log] [blame]
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2007,2008,2009,2010 Red Hat, Inc.
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +02003 * Copyright © 2012 Google, Inc.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05004 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04005 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05006 *
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
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +020026 * Google Author(s): Behdad Esfahbod
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -050027 */
28
Behdad Esfahbod0f0cd9d2010-06-09 06:32:56 -040029#ifndef HB_OPEN_TYPE_PRIVATE_HH
30#define HB_OPEN_TYPE_PRIVATE_HH
Behdad Esfahbod12c45682006-12-28 06:10:59 -050031
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040032#include "hb-private.hh"
Behdad Esfahbod12c45682006-12-28 06:10:59 -050033
Behdad Esfahboda16ecbf2008-01-23 17:01:55 -050034
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -040035namespace OT {
36
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040037
Behdad Esfahbod81822522012-11-23 13:27:16 -050038
39/*
Behdad Esfahbod196598b2009-08-04 11:04:32 -040040 * Casts
41 */
42
Behdad Esfahbod187454c2010-04-23 16:35:01 -040043/* Cast to struct T, reference to reference */
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040044template<typename Type, typename TObject>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -070045static inline const Type& CastR(const TObject &X)
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040046{ return reinterpret_cast<const Type&> (X); }
47template<typename Type, typename TObject>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -070048static inline Type& CastR(TObject &X)
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040049{ return reinterpret_cast<Type&> (X); }
Behdad Esfahbod196598b2009-08-04 11:04:32 -040050
Behdad Esfahbod187454c2010-04-23 16:35:01 -040051/* Cast to struct T, pointer to pointer */
52template<typename Type, typename TObject>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -070053static inline const Type* CastP(const TObject *X)
Behdad Esfahbod187454c2010-04-23 16:35:01 -040054{ return reinterpret_cast<const Type*> (X); }
55template<typename Type, typename TObject>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -070056static inline Type* CastP(TObject *X)
Behdad Esfahbod187454c2010-04-23 16:35:01 -040057{ return reinterpret_cast<Type*> (X); }
58
Behdad Esfahbod09766b12010-05-10 17:36:03 -040059/* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
60 * location pointed to by P plus Ofs bytes. */
61template<typename Type>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -070062static inline const Type& StructAtOffset(const void *P, unsigned int offset)
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -040063{ return * reinterpret_cast<const Type*> ((const char *) P + offset); }
Behdad Esfahbod09766b12010-05-10 17:36:03 -040064template<typename Type>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -070065static inline Type& StructAtOffset(void *P, unsigned int offset)
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -040066{ return * reinterpret_cast<Type*> ((char *) P + offset); }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -040067
Behdad Esfahbod2e2f43e2010-04-21 22:30:36 -040068/* StructAfter<T>(X) returns the struct T& that is placed after X.
Behdad Esfahbod29c3f5e2010-04-21 23:01:00 -040069 * Works with X of variable size also. X must implement get_size() */
Behdad Esfahbode961c862010-04-21 15:56:11 -040070template<typename Type, typename TObject>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -070071static inline const Type& StructAfter(const TObject &X)
Behdad Esfahbod09766b12010-05-10 17:36:03 -040072{ return StructAtOffset<Type>(&X, X.get_size()); }
Behdad Esfahbode961c862010-04-21 15:56:11 -040073template<typename Type, typename TObject>
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -070074static inline Type& StructAfter(TObject &X)
Behdad Esfahbod09766b12010-05-10 17:36:03 -040075{ return StructAtOffset<Type>(&X, X.get_size()); }
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040076
Behdad Esfahbode961c862010-04-21 15:56:11 -040077
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -050078
Behdad Esfahbod70de50c2009-08-04 00:58:28 -040079/*
Behdad Esfahbode45d3f82010-05-06 19:33:31 -040080 * Size checking
81 */
82
Behdad Esfahbodf6796352010-05-13 13:34:17 -040083/* Check _assertion in a method environment */
Behdad Esfahboda00a63b2012-06-06 03:07:01 -040084#define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \
85 inline void _instance_assertion_on_line_##_line (void) const \
86 { \
87 ASSERT_STATIC (_assertion); \
88 ASSERT_INSTANCE_POD (*this); /* Make sure it's POD. */ \
89 }
90# define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion)
91# define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion)
92
Behdad Esfahbodf6796352010-05-13 13:34:17 -040093/* Check that _code compiles in a method environment */
Behdad Esfahboda00a63b2012-06-06 03:07:01 -040094#define _DEFINE_COMPILES_ASSERTION1(_line, _code) \
95 inline void _compiles_assertion_on_line_##_line (void) const \
Behdad Esfahbodf6796352010-05-13 13:34:17 -040096 { _code; }
Behdad Esfahboda00a63b2012-06-06 03:07:01 -040097# define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code)
98# define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code)
Behdad Esfahbod0abcc3b2010-05-10 17:04:20 -040099
100
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400101#define DEFINE_SIZE_STATIC(size) \
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400102 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)); \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400103 static const unsigned int static_size = (size); \
104 static const unsigned int min_size = (size)
105
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400106/* Size signifying variable-sized array */
107#define VAR 1
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400108
Behdad Esfahbod596e4712010-05-10 18:47:48 -0400109#define DEFINE_SIZE_UNION(size, _member) \
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400110 DEFINE_INSTANCE_ASSERTION (this->u._member.static_size == (size)); \
Behdad Esfahbod596e4712010-05-10 18:47:48 -0400111 static const unsigned int min_size = (size)
112
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400113#define DEFINE_SIZE_MIN(size) \
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400114 DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)); \
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400115 static const unsigned int min_size = (size)
116
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400117#define DEFINE_SIZE_ARRAY(size, array) \
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400118 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \
119 DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400120 static const unsigned int min_size = (size)
121
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400122#define DEFINE_SIZE_ARRAY2(size, array1, array2) \
Behdad Esfahboda00a63b2012-06-06 03:07:01 -0400123 DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \
124 DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].static_size) \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400125 static const unsigned int min_size = (size)
126
127
128
129/*
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400130 * Null objects
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400131 */
132
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400133/* Global nul-content Null pool. Enlarge as necessary. */
Behdad Esfahbod0bb0f5d2012-06-07 17:42:48 -0400134/* TODO This really should be a extern HB_INTERNAL and defined somewhere... */
Behdad Esfahbodb7878cd2014-05-13 21:47:51 -0400135static const void *_NullPool[(256+8) / sizeof (void *)];
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400136
Behdad Esfahbodd2c2ca82010-05-10 19:58:25 -0400137/* Generic nul-content Null objects. */
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400138template <typename Type>
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400139static inline const Type& Null (void) {
Behdad Esfahbodfd0de882012-11-15 10:47:14 -0800140 ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool));
Behdad Esfahbod187454c2010-04-23 16:35:01 -0400141 return *CastP<Type> (_NullPool);
Behdad Esfahbod9d367782010-04-21 00:32:47 -0400142}
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400143
144/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
Behdad Esfahbod65f46b02010-05-06 19:35:19 -0400145#define DEFINE_NULL_DATA(Type, data) \
Behdad Esfahbodfd0de882012-11-15 10:47:14 -0800146static const char _Null##Type[sizeof (Type) + 1] = data; /* +1 is for nul-termination in data */ \
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400147template <> \
Behdad Esfahbod6faff8e2014-04-28 14:29:39 -0700148/*static*/ inline const Type& Null<Type> (void) { \
Behdad Esfahbod187454c2010-04-23 16:35:01 -0400149 return *CastP<Type> (_Null##Type); \
Behdad Esfahbod565c80b2010-04-22 10:26:35 -0400150} /* The following line really exists such that we end in a place needing semicolon */ \
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400151ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type))
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400152
153/* Accessor macro. */
Behdad Esfahbod9d367782010-04-21 00:32:47 -0400154#define Null(Type) Null<Type>()
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400155
156
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400157
158/*
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400159 * Sanitize
160 */
161
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400162#ifndef HB_DEBUG_SANITIZE
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400163#define HB_DEBUG_SANITIZE (HB_DEBUG+0)
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400164#endif
165
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400166
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500167#define TRACE_SANITIZE(this) \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500168 hb_auto_trace_t<HB_DEBUG_SANITIZE, bool> trace \
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500169 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500170 "");
Behdad Esfahbod807c5b02010-04-28 20:25:22 -0400171
Behdad Esfahbod07a52b62013-02-25 19:09:57 -0500172/* This limits sanitizing time on really broken fonts. */
173#ifndef HB_SANITIZE_MAX_EDITS
174#define HB_SANITIZE_MAX_EDITS 100
175#endif
Behdad Esfahbod807c5b02010-04-28 20:25:22 -0400176
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400177struct hb_sanitize_context_t
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400178{
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500179 inline const char *get_name (void) { return "SANITIZE"; }
180 static const unsigned int max_debug_depth = HB_DEBUG_SANITIZE;
181 typedef bool return_t;
Behdad Esfahbod095a1252015-02-19 10:29:41 +0300182 template <typename T, typename F>
183 inline bool may_dispatch (const T *obj, const F *format)
184 { return format->sanitize (this); }
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500185 template <typename T>
Behdad Esfahbod9c5a9ee2013-03-09 01:55:04 -0500186 inline return_t dispatch (const T &obj) { return obj.sanitize (this); }
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500187 static return_t default_return_value (void) { return true; }
Behdad Esfahbod095a1252015-02-19 10:29:41 +0300188 bool stop_sublookup_iteration (const return_t r) const { return !r; }
Behdad Esfahbod2c53bd32012-11-23 17:29:05 -0500189
Behdad Esfahbod31f18ab2011-06-15 09:49:58 -0400190 inline void init (hb_blob_t *b)
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400191 {
Behdad Esfahbod31f18ab2011-06-15 09:49:58 -0400192 this->blob = hb_blob_reference (b);
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400193 this->writable = false;
194 }
195
Behdad Esfahbodcf26e882012-05-11 03:16:57 +0200196 inline void start_processing (void)
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400197 {
Behdad Esfahbod4101ca72011-05-11 14:30:56 -0400198 this->start = hb_blob_get_data (this->blob, NULL);
199 this->end = this->start + hb_blob_get_length (this->blob);
Behdad Esfahbodd5a50522014-12-18 18:09:41 -0800200 assert (this->start <= this->end); /* Must not overflow. */
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400201 this->edit_count = 0;
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400202 this->debug_depth = 0;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400203
Behdad Esfahbod0766ee12014-12-12 18:23:20 -0800204 DEBUG_MSG_LEVEL (SANITIZE, start, 0, +1,
Behdad Esfahbodcf26e882012-05-11 03:16:57 +0200205 "start [%p..%p] (%lu bytes)",
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200206 this->start, this->end,
207 (unsigned long) (this->end - this->start));
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400208 }
209
Behdad Esfahbodcf26e882012-05-11 03:16:57 +0200210 inline void end_processing (void)
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400211 {
Behdad Esfahbod0766ee12014-12-12 18:23:20 -0800212 DEBUG_MSG_LEVEL (SANITIZE, this->start, 0, -1,
Behdad Esfahbodcf26e882012-05-11 03:16:57 +0200213 "end [%p..%p] %u edit requests",
Behdad Esfahbod1e088302012-05-11 00:16:40 +0200214 this->start, this->end, this->edit_count);
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400215
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400216 hb_blob_destroy (this->blob);
217 this->blob = NULL;
218 this->start = this->end = NULL;
219 }
220
Behdad Esfahbod4ad2cc52010-05-06 09:24:24 -0400221 inline bool check_range (const void *base, unsigned int len) const
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400222 {
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400223 const char *p = (const char *) base;
Behdad Esfahbod282b13f2014-12-12 19:32:46 -0800224 bool ok = this->start <= p && p <= this->end && (unsigned int) (this->end - p) >= len;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400225
Behdad Esfahbod282b13f2014-12-12 19:32:46 -0800226 DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
227 "check_range [%p..%p] (%d bytes) in [%p..%p] -> %s",
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500228 p, p + len, len,
Behdad Esfahbod282b13f2014-12-12 19:32:46 -0800229 this->start, this->end,
230 ok ? "OK" : "OUT-OF-RANGE");
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400231
Behdad Esfahbod282b13f2014-12-12 19:32:46 -0800232 return likely (ok);
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400233 }
234
Behdad Esfahbod1cd1e112010-05-05 20:15:14 -0400235 inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400236 {
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400237 const char *p = (const char *) base;
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400238 bool overflows = _hb_unsigned_int_mul_overflows (len, record_size);
Behdad Esfahbod282b13f2014-12-12 19:32:46 -0800239 unsigned int array_size = record_size * len;
240 bool ok = !overflows && this->check_range (base, array_size);
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400241
Behdad Esfahbod282b13f2014-12-12 19:32:46 -0800242 DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
243 "check_array [%p..%p] (%d*%d=%d bytes) in [%p..%p] -> %s",
244 p, p + (record_size * len), record_size, len, (unsigned int) array_size,
245 this->start, this->end,
246 overflows ? "OVERFLOWS" : ok ? "OK" : "OUT-OF-RANGE");
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400247
Behdad Esfahbod282b13f2014-12-12 19:32:46 -0800248 return likely (ok);
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400249 }
250
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400251 template <typename Type>
252 inline bool check_struct (const Type *obj) const
253 {
Behdad Esfahbod54842372010-05-10 18:13:32 -0400254 return likely (this->check_range (obj, obj->min_size));
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400255 }
256
Behdad Esfahbodcf26e882012-05-11 03:16:57 +0200257 inline bool may_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400258 {
Behdad Esfahbod07a52b62013-02-25 19:09:57 -0500259 if (this->edit_count >= HB_SANITIZE_MAX_EDITS)
260 return false;
261
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400262 const char *p = (const char *) base;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400263 this->edit_count++;
264
Behdad Esfahbod282b13f2014-12-12 19:32:46 -0800265 DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500266 "may_edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s",
267 this->edit_count,
268 p, p + len, len,
Behdad Esfahbode77b4422012-12-17 18:42:59 -0500269 this->start, this->end,
270 this->writable ? "GRANTED" : "DENIED");
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400271
Behdad Esfahbod282b13f2014-12-12 19:32:46 -0800272 return this->writable;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400273 }
274
Behdad Esfahbod51f56352014-06-04 18:42:32 -0400275 template <typename Type, typename ValueType>
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300276 inline bool try_set (const Type *obj, const ValueType &v) {
Behdad Esfahbod51f56352014-06-04 18:42:32 -0400277 if (this->may_edit (obj, obj->static_size)) {
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300278 const_cast<Type *> (obj)->set (v);
Behdad Esfahbod51f56352014-06-04 18:42:32 -0400279 return true;
280 }
281 return false;
282 }
283
Behdad Esfahbodcf26e882012-05-11 03:16:57 +0200284 mutable unsigned int debug_depth;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400285 const char *start, *end;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400286 bool writable;
Behdad Esfahbod254933c2010-04-23 13:57:10 -0400287 unsigned int edit_count;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400288 hb_blob_t *blob;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400289};
290
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400291
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400292
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400293/* Template to sanitize an object. */
294template <typename Type>
295struct Sanitizer
296{
297 static hb_blob_t *sanitize (hb_blob_t *blob) {
Behdad Esfahbod7e8c3892014-07-25 11:18:11 -0400298 hb_sanitize_context_t c[1] = {{0, NULL, NULL, false, 0, NULL}};
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400299 bool sane;
300
Behdad Esfahbodd0b65732009-08-06 18:34:47 -0400301 /* TODO is_sane() stuff */
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400302
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400303 c->init (blob);
304
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400305 retry:
Behdad Esfahbod0766ee12014-12-12 18:23:20 -0800306 DEBUG_MSG_FUNC (SANITIZE, c->start, "start");
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400307
Behdad Esfahbodcf26e882012-05-11 03:16:57 +0200308 c->start_processing ();
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400309
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400310 if (unlikely (!c->start)) {
Behdad Esfahbodcf26e882012-05-11 03:16:57 +0200311 c->end_processing ();
Behdad Esfahbod48146e52010-05-10 20:07:56 -0400312 return blob;
313 }
314
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400315 Type *t = CastP<Type> (const_cast<char *> (c->start));
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400316
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400317 sane = t->sanitize (c);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400318 if (sane) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400319 if (c->edit_count) {
Behdad Esfahbod0766ee12014-12-12 18:23:20 -0800320 DEBUG_MSG_FUNC (SANITIZE, c->start, "passed first round with %d edits; going for second round", c->edit_count);
Behdad Esfahbodfa030172010-04-29 13:48:26 -0400321
Behdad Esfahbod8b534612009-08-19 18:16:50 -0400322 /* sanitize again to ensure no toe-stepping */
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400323 c->edit_count = 0;
324 sane = t->sanitize (c);
325 if (c->edit_count) {
Behdad Esfahbod0766ee12014-12-12 18:23:20 -0800326 DEBUG_MSG_FUNC (SANITIZE, c->start, "requested %d edits in second round; FAILLING", c->edit_count);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400327 sane = false;
328 }
329 }
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400330 } else {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400331 unsigned int edit_count = c->edit_count;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400332 if (edit_count && !c->writable) {
333 c->start = hb_blob_get_data_writable (blob, NULL);
334 c->end = c->start + hb_blob_get_length (blob);
335
336 if (c->start) {
337 c->writable = true;
338 /* ok, we made it writable by relocating. try again */
Behdad Esfahbod0766ee12014-12-12 18:23:20 -0800339 DEBUG_MSG_FUNC (SANITIZE, c->start, "retry");
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400340 goto retry;
341 }
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400342 }
343 }
344
Behdad Esfahbodcf26e882012-05-11 03:16:57 +0200345 c->end_processing ();
Behdad Esfahbod4101ca72011-05-11 14:30:56 -0400346
Behdad Esfahbod0766ee12014-12-12 18:23:20 -0800347 DEBUG_MSG_FUNC (SANITIZE, c->start, sane ? "PASSED" : "FAILED");
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400348 if (sane)
349 return blob;
350 else {
351 hb_blob_destroy (blob);
Behdad Esfahbod49110622011-05-02 19:36:39 -0400352 return hb_blob_get_empty ();
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400353 }
354 }
Behdad Esfahbodb435ab72010-05-10 19:51:57 -0400355
356 static const Type* lock_instance (hb_blob_t *blob) {
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400357 hb_blob_make_immutable (blob);
358 const char *base = hb_blob_get_data (blob, NULL);
Behdad Esfahbodb435ab72010-05-10 19:51:57 -0400359 return unlikely (!base) ? &Null(Type) : CastP<Type> (base);
360 }
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400361};
362
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400363
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400364
Behdad Esfahbode901b952012-08-29 20:26:08 -0400365/*
366 * Serialize
367 */
368
369#ifndef HB_DEBUG_SERIALIZE
370#define HB_DEBUG_SERIALIZE (HB_DEBUG+0)
371#endif
372
373
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500374#define TRACE_SERIALIZE(this) \
Behdad Esfahbod902cc8a2012-11-23 15:06:59 -0500375 hb_auto_trace_t<HB_DEBUG_SERIALIZE, bool> trace \
376 (&c->debug_depth, "SERIALIZE", c, HB_FUNC, \
377 "");
Behdad Esfahbode901b952012-08-29 20:26:08 -0400378
379
380struct hb_serialize_context_t
381{
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400382 inline hb_serialize_context_t (void *start, unsigned int size)
Behdad Esfahbode901b952012-08-29 20:26:08 -0400383 {
384 this->start = (char *) start;
385 this->end = this->start + size;
Behdad Esfahbode901b952012-08-29 20:26:08 -0400386
Behdad Esfahbode901b952012-08-29 20:26:08 -0400387 this->ran_out_of_room = false;
388 this->head = this->start;
389 this->debug_depth = 0;
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400390 }
Behdad Esfahbode901b952012-08-29 20:26:08 -0400391
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400392 template <typename Type>
393 inline Type *start_serialize (void)
394 {
Behdad Esfahbode901b952012-08-29 20:26:08 -0400395 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1,
396 "start [%p..%p] (%lu bytes)",
397 this->start, this->end,
398 (unsigned long) (this->end - this->start));
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400399
400 return start_embed<Type> ();
Behdad Esfahbode901b952012-08-29 20:26:08 -0400401 }
402
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400403 inline void end_serialize (void)
Behdad Esfahbode901b952012-08-29 20:26:08 -0400404 {
405 DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1,
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400406 "end [%p..%p] serialized %d bytes; %s",
Behdad Esfahbode901b952012-08-29 20:26:08 -0400407 this->start, this->end,
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400408 (int) (this->head - this->start),
Behdad Esfahbode901b952012-08-29 20:26:08 -0400409 this->ran_out_of_room ? "RAN OUT OF ROOM" : "did not ran out of room");
410
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400411 }
412
413 template <typename Type>
414 inline Type *copy (void)
415 {
416 assert (!this->ran_out_of_room);
417 unsigned int len = this->head - this->start;
418 void *p = malloc (len);
419 if (p)
420 memcpy (p, this->start, len);
421 return reinterpret_cast<Type *> (p);
Behdad Esfahbode901b952012-08-29 20:26:08 -0400422 }
423
424 template <typename Type>
Behdad Esfahbod4b312fb2012-09-01 21:56:06 -0400425 inline Type *allocate_size (unsigned int size)
Behdad Esfahbode901b952012-08-29 20:26:08 -0400426 {
Behdad Esfahbod05d5d3c2013-02-25 23:57:51 -0500427 if (unlikely (this->ran_out_of_room || this->end - this->head < ptrdiff_t (size))) {
Behdad Esfahbode901b952012-08-29 20:26:08 -0400428 this->ran_out_of_room = true;
429 return NULL;
430 }
Behdad Esfahbod4b312fb2012-09-01 21:56:06 -0400431 memset (this->head, 0, size);
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400432 char *ret = this->head;
Behdad Esfahbode901b952012-08-29 20:26:08 -0400433 this->head += size;
434 return reinterpret_cast<Type *> (ret);
435 }
436
437 template <typename Type>
Behdad Esfahbod4b312fb2012-09-01 21:56:06 -0400438 inline Type *allocate_min (void)
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400439 {
Behdad Esfahbod4b312fb2012-09-01 21:56:06 -0400440 return this->allocate_size<Type> (Type::min_size);
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400441 }
442
443 template <typename Type>
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400444 inline Type *start_embed (void)
445 {
446 Type *ret = reinterpret_cast<Type *> (this->head);
447 return ret;
448 }
449
450 template <typename Type>
Behdad Esfahbod4b312fb2012-09-01 21:56:06 -0400451 inline Type *embed (const Type &obj)
Behdad Esfahbode901b952012-08-29 20:26:08 -0400452 {
Behdad Esfahbodbc5be242012-09-01 20:48:22 -0400453 unsigned int size = obj.get_size ();
Behdad Esfahbod4b312fb2012-09-01 21:56:06 -0400454 Type *ret = this->allocate_size<Type> (size);
Behdad Esfahbodbc5be242012-09-01 20:48:22 -0400455 if (unlikely (!ret)) return NULL;
456 memcpy (ret, obj, size);
457 return ret;
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400458 }
459
460 template <typename Type>
Behdad Esfahbod4b312fb2012-09-01 21:56:06 -0400461 inline Type *extend_min (Type &obj)
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400462 {
Behdad Esfahbodbc5be242012-09-01 20:48:22 -0400463 unsigned int size = obj.min_size;
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400464 assert (this->start <= (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head);
465 if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return NULL;
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400466 return reinterpret_cast<Type *> (&obj);
467 }
468
469 template <typename Type>
Behdad Esfahbod4b312fb2012-09-01 21:56:06 -0400470 inline Type *extend (Type &obj)
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400471 {
Behdad Esfahbodbc5be242012-09-01 20:48:22 -0400472 unsigned int size = obj.get_size ();
473 assert (this->start < (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head);
Behdad Esfahbod811eefe2012-09-10 09:56:27 -0400474 if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return NULL;
Behdad Esfahbodbc5be242012-09-01 20:48:22 -0400475 return reinterpret_cast<Type *> (&obj);
Behdad Esfahbode901b952012-08-29 20:26:08 -0400476 }
477
478 inline void truncate (void *head)
479 {
480 assert (this->start < head && head <= this->head);
481 this->head = (char *) head;
482 }
483
484 unsigned int debug_depth;
485 char *start, *end, *head;
486 bool ran_out_of_room;
487};
488
Behdad Esfahboda930c682012-09-04 18:17:57 -0400489template <typename Type>
490struct Supplier
491{
Behdad Esfahboda930c682012-09-04 18:17:57 -0400492 inline Supplier (const Type *array, unsigned int len_)
493 {
494 head = array;
495 len = len_;
496 }
497 inline const Type operator [] (unsigned int i) const
498 {
499 if (unlikely (i >= len)) return Type ();
500 return head[i];
501 }
502
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400503 inline void advance (unsigned int count)
504 {
Behdad Esfahboda930c682012-09-04 18:17:57 -0400505 if (unlikely (count > len))
506 count = len;
507 len -= count;
508 head += count;
509 }
510
511 private:
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400512 inline Supplier (const Supplier<Type> &); /* Disallow copy */
513 inline Supplier<Type>& operator= (const Supplier<Type> &); /* Disallow copy */
514
Behdad Esfahboda930c682012-09-04 18:17:57 -0400515 unsigned int len;
516 const Type *head;
517};
518
519
Behdad Esfahbode901b952012-08-29 20:26:08 -0400520
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400521
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500522/*
523 *
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400524 * The OpenType Font File: Data Types
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500525 */
526
527
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500528/* "The following data types are used in the OpenType font file.
529 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500530
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400531/*
532 * Int types
533 */
534
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400535
Behdad Esfahbodd7bf4732011-08-05 18:18:21 -0400536template <typename Type, int Bytes> struct BEInt;
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500537
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400538template <typename Type>
Behdad Esfahbodd7bf4732011-08-05 18:18:21 -0400539struct BEInt<Type, 2>
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400540{
541 public:
Behdad Esfahbod666b42f2014-10-14 21:24:59 -0700542 inline void set (Type V)
543 {
544 v[0] = (V >> 8) & 0xFF;
545 v[1] = (V ) & 0xFF;
546 }
547 inline operator Type (void) const
548 {
549 return (v[0] << 8)
550 + (v[1] );
551 }
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400552 private: uint8_t v[2];
553};
554template <typename Type>
Behdad Esfahbod5a5640d2014-10-14 21:26:13 -0700555struct BEInt<Type, 3>
556{
557 public:
558 inline void set (Type V)
559 {
560 v[0] = (V >> 16) & 0xFF;
561 v[1] = (V >> 8) & 0xFF;
562 v[2] = (V ) & 0xFF;
563 }
564 inline operator Type (void) const
565 {
566 return (v[0] << 16)
567 + (v[1] << 8)
568 + (v[2] );
569 }
Behdad Esfahbod5a5640d2014-10-14 21:26:13 -0700570 private: uint8_t v[3];
571};
572template <typename Type>
Behdad Esfahbodd7bf4732011-08-05 18:18:21 -0400573struct BEInt<Type, 4>
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400574{
575 public:
Behdad Esfahbod666b42f2014-10-14 21:24:59 -0700576 inline void set (Type V)
577 {
578 v[0] = (V >> 24) & 0xFF;
579 v[1] = (V >> 16) & 0xFF;
580 v[2] = (V >> 8) & 0xFF;
581 v[3] = (V ) & 0xFF;
582 }
583 inline operator Type (void) const
584 {
585 return (v[0] << 24)
586 + (v[1] << 16)
587 + (v[2] << 8)
588 + (v[3] );
589 }
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400590 private: uint8_t v[4];
591};
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500592
Behdad Esfahbod2467c662010-04-21 23:11:45 -0400593/* Integer types in big-endian order and no alignment requirement */
Behdad Esfahbodbd61bc12012-12-11 16:00:43 -0500594template <typename Type, unsigned int Size>
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400595struct IntType
596{
Behdad Esfahbod81408cd2010-07-23 14:46:57 -0400597 inline void set (Type i) { v.set (i); }
Behdad Esfahbod01c01612010-04-21 22:49:56 -0400598 inline operator Type(void) const { return v; }
Behdad Esfahbodbbdd6fd2015-02-19 17:03:02 +0300599 inline bool operator == (const IntType<Type,Size> &o) const { return (Type) v == (Type) o.v; }
600 inline bool operator != (const IntType<Type,Size> &o) const { return !(*this == o); }
Behdad Esfahbodbd61bc12012-12-11 16:00:43 -0500601 static inline int cmp (const IntType<Type,Size> *a, const IntType<Type,Size> *b) { return b->cmp (*a); }
Behdad Esfahbod37de2d52015-02-19 16:55:51 +0300602 inline int cmp (IntType<Type,Size> va) const { Type a = va; return cmp (va); }
Behdad Esfahbod88a399a2015-02-19 16:57:12 +0300603 inline int cmp (Type a) const
604 {
605 Type b = v;
606 if (sizeof (Type) < sizeof (int))
607 return (int) a - (int) b;
608 else
609 return a < b ? -1 : a == b ? 0 : +1;
610 }
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300611 inline bool sanitize (hb_sanitize_context_t *c) const
612 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500613 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200614 return TRACE_RETURN (likely (c->check_struct (this)));
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400615 }
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400616 protected:
Behdad Esfahbodbd61bc12012-12-11 16:00:43 -0500617 BEInt<Type, Size> v;
Behdad Esfahbod569da922010-05-10 16:38:32 -0400618 public:
Behdad Esfahbodbd61bc12012-12-11 16:00:43 -0500619 DEFINE_SIZE_STATIC (Size);
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400620};
621
Behdad Esfahbodb7878cd2014-05-13 21:47:51 -0400622typedef uint8_t BYTE; /* 8-bit unsigned integer. */
Behdad Esfahbodbd61bc12012-12-11 16:00:43 -0500623typedef IntType<uint16_t, 2> USHORT; /* 16-bit unsigned integer. */
624typedef IntType<int16_t, 2> SHORT; /* 16-bit signed integer. */
625typedef IntType<uint32_t, 4> ULONG; /* 32-bit unsigned integer. */
626typedef IntType<int32_t, 4> LONG; /* 32-bit signed integer. */
627typedef IntType<uint32_t, 3> UINT24; /* 24-bit unsigned integer. */
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400628
Behdad Esfahbodae9877d2011-08-17 14:43:45 +0200629/* 16-bit signed integer (SHORT) that describes a quantity in FUnits. */
630typedef SHORT FWORD;
631
632/* 16-bit unsigned integer (USHORT) that describes a quantity in FUnits. */
633typedef USHORT UFWORD;
634
Behdad Esfahbode29caf32010-05-19 11:47:17 -0400635/* Date represented in number of seconds since 12:00 midnight, January 1,
636 * 1904. The value is represented as a signed 64-bit integer. */
637struct LONGDATETIME
638{
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300639 inline bool sanitize (hb_sanitize_context_t *c) const
640 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500641 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200642 return TRACE_RETURN (likely (c->check_struct (this)));
Behdad Esfahbode29caf32010-05-19 11:47:17 -0400643 }
Behdad Esfahbod6775da32014-01-23 14:18:49 -0500644 protected:
Behdad Esfahbode29caf32010-05-19 11:47:17 -0400645 LONG major;
646 ULONG minor;
647 public:
648 DEFINE_SIZE_STATIC (8);
649};
650
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500651/* Array of four uint8s (length = 32 bits) used to identify a script, language
652 * system, feature, or baseline */
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400653struct Tag : ULONG
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400654{
Behdad Esfahbodbefc0222006-12-25 09:14:52 -0500655 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400656 inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); }
657 inline operator char* (void) { return reinterpret_cast<char *> (&this->v); }
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400658 public:
659 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500660};
Behdad Esfahbod65f46b02010-05-06 19:35:19 -0400661DEFINE_NULL_DATA (Tag, " ");
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500662
663/* Glyph index number, same as uint16 (length = 16 bits) */
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400664typedef USHORT GlyphID;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500665
Behdad Esfahbodb5db4f12010-05-10 22:22:22 -0400666/* Script/language-system/feature index */
667struct Index : USHORT {
Behdad Esfahbod76271002014-07-11 14:54:42 -0400668 static const unsigned int NOT_FOUND_INDEX = 0xFFFFu;
Behdad Esfahbodb5db4f12010-05-10 22:22:22 -0400669};
670DEFINE_NULL_DATA (Index, "\xff\xff");
671
Behdad Esfahbod99d28172014-06-27 15:12:52 -0400672/* Offset, Null offset = 0 */
673template <typename Type=USHORT>
674struct Offset : Type
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600675{
676 inline bool is_null (void) const { return 0 == *this; }
677 public:
Behdad Esfahbod0394ec12014-06-27 14:40:35 -0400678 DEFINE_SIZE_STATIC (sizeof(Type));
Behdad Esfahbode95e0312013-01-08 16:15:46 -0600679};
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400680
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500681
682/* CheckSum */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400683struct CheckSum : ULONG
684{
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400685 /* This is reference implementation from the spec. */
686 static inline uint32_t CalcTableChecksum (const ULONG *Table, uint32_t Length)
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400687 {
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500688 uint32_t Sum = 0L;
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400689 const ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500690
691 while (Table < EndPtr)
692 Sum += *Table++;
693 return Sum;
694 }
Behdad Esfahbod05bad3b2013-07-21 17:05:02 -0400695
696 /* Note: data should be 4byte aligned and have 4byte padding at the end. */
697 inline void set_for_data (const void *data, unsigned int length)
698 { set (CalcTableChecksum ((const ULONG *) data, length)); }
699
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400700 public:
701 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500702};
703
704
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500705/*
706 * Version Numbers
707 */
708
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400709struct FixedVersion
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400710{
Behdad Esfahbod4f28fbd2011-05-31 12:33:11 -0400711 inline uint32_t to_int (void) const { return (major << 16) + minor; }
Behdad Esfahbod96908b82009-05-24 12:30:40 -0400712
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300713 inline bool sanitize (hb_sanitize_context_t *c) const
714 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500715 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200716 return TRACE_RETURN (c->check_struct (this));
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400717 }
718
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400719 USHORT major;
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400720 USHORT minor;
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400721 public:
722 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500723};
724
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400725
726
727/*
Behdad Esfahbod99d28172014-06-27 15:12:52 -0400728 * Template subclasses of Offset that do the dereferencing.
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400729 * Use: (base+offset)
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400730 */
731
Behdad Esfahbod9da552d2014-06-27 15:09:42 -0400732template <typename Type, typename OffsetType=USHORT>
Behdad Esfahbod99d28172014-06-27 15:12:52 -0400733struct OffsetTo : Offset<OffsetType>
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400734{
Behdad Esfahbod00e23fc2010-04-20 23:50:45 -0400735 inline const Type& operator () (const void *base) const
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400736 {
737 unsigned int offset = *this;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400738 if (unlikely (!offset)) return Null(Type);
Behdad Esfahbod09766b12010-05-10 17:36:03 -0400739 return StructAtOffset<Type> (base, offset);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400740 }
Behdad Esfahbodbc5be242012-09-01 20:48:22 -0400741
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300742 inline Type& serialize (hb_serialize_context_t *c, const void *base)
Behdad Esfahbodbc5be242012-09-01 20:48:22 -0400743 {
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400744 Type *t = c->start_embed<Type> ();
Behdad Esfahbod49120302012-09-03 20:58:03 -0400745 this->set ((char *) t - (char *) base); /* TODO(serialize) Overflow? */
Behdad Esfahbodabcc5ac2012-09-01 21:30:17 -0400746 return *t;
Behdad Esfahbodbc5be242012-09-01 20:48:22 -0400747 }
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400748
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300749 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
750 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500751 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200752 if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400753 unsigned int offset = *this;
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200754 if (unlikely (!offset)) return TRACE_RETURN (true);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300755 const Type &obj = StructAtOffset<Type> (base, offset);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200756 return TRACE_RETURN (likely (obj.sanitize (c)) || neuter (c));
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400757 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400758 template <typename T>
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300759 inline bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const
760 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500761 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200762 if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400763 unsigned int offset = *this;
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200764 if (unlikely (!offset)) return TRACE_RETURN (true);
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300765 const Type &obj = StructAtOffset<Type> (base, offset);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200766 return TRACE_RETURN (likely (obj.sanitize (c, user_data)) || neuter (c));
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400767 }
768
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400769 /* Set the offset to Null */
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300770 inline bool neuter (hb_sanitize_context_t *c) const {
Behdad Esfahbod51f56352014-06-04 18:42:32 -0400771 return c->try_set (this, 0);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400772 }
Behdad Esfahbod586b6062014-06-27 15:39:47 -0400773 DEFINE_SIZE_STATIC (sizeof(OffsetType));
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400774};
775template <typename Base, typename OffsetType, typename Type>
Behdad Esfahbod0d1b3412014-06-26 19:13:34 -0400776static inline const Type& operator + (const Base &base, const OffsetTo<Type, OffsetType> &offset) { return offset (base); }
Behdad Esfahbodbc5be242012-09-01 20:48:22 -0400777template <typename Base, typename OffsetType, typename Type>
Behdad Esfahbod0d1b3412014-06-26 19:13:34 -0400778static inline Type& operator + (Base &base, OffsetTo<Type, OffsetType> &offset) { return offset (base); }
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400779
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400780
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400781/*
782 * Array Types
783 */
784
Behdad Esfahbod9da552d2014-06-27 15:09:42 -0400785/* An array with a number of elements. */
786template <typename Type, typename LenType=USHORT>
787struct ArrayOf
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400788{
Behdad Esfahbod4f5f1c32010-04-22 00:27:39 -0400789 const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500790 {
791 unsigned int count = len;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400792 if (unlikely (start_offset > count))
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500793 count = 0;
794 else
795 count -= start_offset;
796 count = MIN (count, *pcount);
797 *pcount = count;
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400798 return array + start_offset;
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500799 }
800
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400801 inline const Type& operator [] (unsigned int i) const
802 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400803 if (unlikely (i >= len)) return Null(Type);
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400804 return array[i];
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400805 }
Behdad Esfahbod9f2348d2012-08-29 21:08:59 -0400806 inline Type& operator [] (unsigned int i)
807 {
808 return array[i];
809 }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400810 inline unsigned int get_size (void) const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400811 { return len.static_size + len * Type::static_size; }
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400812
Behdad Esfahbodc61be032012-09-01 21:43:38 -0400813 inline bool serialize (hb_serialize_context_t *c,
Behdad Esfahbod1f07e332012-09-03 23:28:34 -0400814 unsigned int items_len)
815 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500816 TRACE_SERIALIZE (this);
Behdad Esfahbod1f07e332012-09-03 23:28:34 -0400817 if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
818 len.set (items_len); /* TODO(serialize) Overflow? */
819 if (unlikely (!c->extend (*this))) return TRACE_RETURN (false);
820 return TRACE_RETURN (true);
821 }
822
823 inline bool serialize (hb_serialize_context_t *c,
Behdad Esfahboda930c682012-09-04 18:17:57 -0400824 Supplier<Type> &items,
Behdad Esfahbodc61be032012-09-01 21:43:38 -0400825 unsigned int items_len)
826 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500827 TRACE_SERIALIZE (this);
Behdad Esfahbod715e03b2012-09-04 20:10:17 -0400828 if (unlikely (!serialize (c, items_len))) return TRACE_RETURN (false);
829 for (unsigned int i = 0; i < items_len; i++)
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400830 array[i] = items[i];
Behdad Esfahboda930c682012-09-04 18:17:57 -0400831 items.advance (items_len);
Behdad Esfahbodc61be032012-09-01 21:43:38 -0400832 return TRACE_RETURN (true);
833 }
834
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300835 inline bool sanitize (hb_sanitize_context_t *c) const
836 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500837 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200838 if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false);
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400839
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400840 /* Note: for structs that do not reference other structs,
841 * we do not need to call their sanitize() as we already did
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400842 * a bound check on the aggregate array size. We just include
843 * a small unreachable expression to make sure the structs
844 * pointed to do have a simple sanitize(), ie. they do not
845 * reference other structs via offsets.
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400846 */
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400847 (void) (false && array[0].sanitize (c));
848
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200849 return TRACE_RETURN (true);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400850 }
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300851 inline bool sanitize (hb_sanitize_context_t *c, const void *base) const
852 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500853 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200854 if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false);
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400855 unsigned int count = len;
856 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400857 if (unlikely (!array[i].sanitize (c, base)))
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200858 return TRACE_RETURN (false);
859 return TRACE_RETURN (true);
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400860 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400861 template <typename T>
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300862 inline bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const
863 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500864 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200865 if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400866 unsigned int count = len;
867 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400868 if (unlikely (!array[i].sanitize (c, base, user_data)))
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200869 return TRACE_RETURN (false);
870 return TRACE_RETURN (true);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400871 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400872
Behdad Esfahbodc7074b82014-05-08 18:24:31 -0400873 template <typename SearchType>
Behdad Esfahboddf554af2014-06-19 15:39:18 -0400874 inline int lsearch (const SearchType &x) const
Behdad Esfahbodc7074b82014-05-08 18:24:31 -0400875 {
Behdad Esfahbodc7074b82014-05-08 18:24:31 -0400876 unsigned int count = len;
877 for (unsigned int i = 0; i < count; i++)
878 if (!this->array[i].cmp (x))
879 return i;
880 return -1;
881 }
882
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400883 private:
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300884 inline bool sanitize_shallow (hb_sanitize_context_t *c) const
885 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500886 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200887 return TRACE_RETURN (c->check_struct (this) && c->check_array (this, Type::static_size, len));
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400888 }
889
890 public:
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400891 LenType len;
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400892 Type array[VAR];
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400893 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400894 DEFINE_SIZE_ARRAY (sizeof (LenType), array);
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400895};
896
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400897/* Array of Offset's */
898template <typename Type>
899struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
900
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400901/* Array of offsets relative to the beginning of the array itself. */
902template <typename Type>
903struct OffsetListOf : OffsetArrayOf<Type>
904{
905 inline const Type& operator [] (unsigned int i) const
906 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400907 if (unlikely (i >= this->len)) return Null(Type);
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400908 return this+this->array[i];
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400909 }
910
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300911 inline bool sanitize (hb_sanitize_context_t *c) const
912 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500913 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200914 return TRACE_RETURN (OffsetArrayOf<Type>::sanitize (c, this));
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400915 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400916 template <typename T>
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300917 inline bool sanitize (hb_sanitize_context_t *c, T user_data) const
918 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500919 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200920 return TRACE_RETURN (OffsetArrayOf<Type>::sanitize (c, this, user_data));
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400921 }
922};
923
924
Behdad Esfahbod51d9ba02014-06-27 15:27:15 -0400925/* An array starting at second element. */
926template <typename Type, typename LenType=USHORT>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400927struct HeadlessArrayOf
928{
929 inline const Type& operator [] (unsigned int i) const
930 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400931 if (unlikely (i >= len || !i)) return Null(Type);
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400932 return array[i-1];
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400933 }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400934 inline unsigned int get_size (void) const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400935 { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400936
Behdad Esfahboda930c682012-09-04 18:17:57 -0400937 inline bool serialize (hb_serialize_context_t *c,
938 Supplier<Type> &items,
939 unsigned int items_len)
940 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500941 TRACE_SERIALIZE (this);
Behdad Esfahboda930c682012-09-04 18:17:57 -0400942 if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false);
943 len.set (items_len); /* TODO(serialize) Overflow? */
Behdad Esfahboda930c682012-09-04 18:17:57 -0400944 if (unlikely (!items_len)) return TRACE_RETURN (true);
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400945 if (unlikely (!c->extend (*this))) return TRACE_RETURN (false);
946 for (unsigned int i = 0; i < items_len - 1; i++)
947 array[i] = items[i];
Behdad Esfahboda930c682012-09-04 18:17:57 -0400948 items.advance (items_len - 1);
949 return TRACE_RETURN (true);
950 }
951
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300952 inline bool sanitize_shallow (hb_sanitize_context_t *c) const
953 {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400954 return c->check_struct (this)
955 && c->check_array (this, Type::static_size, len);
Behdad Esfahbode5546a42010-04-22 00:45:42 -0400956 }
957
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300958 inline bool sanitize (hb_sanitize_context_t *c) const
959 {
Behdad Esfahbodbe218c62012-11-23 15:32:14 -0500960 TRACE_SANITIZE (this);
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200961 if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false);
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400962
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400963 /* Note: for structs that do not reference other structs,
964 * we do not need to call their sanitize() as we already did
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400965 * a bound check on the aggregate array size. We just include
966 * a small unreachable expression to make sure the structs
967 * pointed to do have a simple sanitize(), ie. they do not
968 * reference other structs via offsets.
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400969 */
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400970 (void) (false && array[0].sanitize (c));
971
Behdad Esfahbod0ab8c862012-05-11 01:25:34 +0200972 return TRACE_RETURN (true);
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400973 }
974
Behdad Esfahbod51d9ba02014-06-27 15:27:15 -0400975 LenType len;
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400976 Type array[VAR];
Behdad Esfahboded074222010-05-10 18:08:46 -0400977 public:
Behdad Esfahbod51d9ba02014-06-27 15:27:15 -0400978 DEFINE_SIZE_ARRAY (sizeof (LenType), array);
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400979};
980
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500981
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -0400982/* An array with sorted elements. Supports binary searching. */
Behdad Esfahbod9da552d2014-06-27 15:09:42 -0400983template <typename Type, typename LenType=USHORT>
984struct SortedArrayOf : ArrayOf<Type, LenType>
Behdad Esfahbod40a47972014-05-08 18:21:04 -0400985{
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -0400986 template <typename SearchType>
Behdad Esfahboddf554af2014-06-19 15:39:18 -0400987 inline int bsearch (const SearchType &x) const
Behdad Esfahbod8659c632013-04-19 14:33:17 -0400988 {
989 /* Hand-coded bsearch here since this is in the hot inner loop. */
990 int min = 0, max = (int) this->len - 1;
991 while (min <= max)
992 {
993 int mid = (min + max) / 2;
994 int c = this->array[mid].cmp (x);
995 if (c < 0)
996 max = mid - 1;
997 else if (c > 0)
998 min = mid + 1;
999 else
1000 return mid;
Behdad Esfahbod99159e52012-06-09 00:50:40 -04001001 }
Behdad Esfahbod8659c632013-04-19 14:33:17 -04001002 return -1;
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -04001003 }
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -04001004};
1005
1006
Behdad Esfahbod7d52e662012-11-16 18:49:54 -08001007} /* namespace OT */
Behdad Esfahbod7c8e8442012-08-28 17:57:49 -04001008
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -04001009
Behdad Esfahbod1e914342009-11-04 18:12:09 -05001010#endif /* HB_OPEN_TYPE_PRIVATE_HH */