blob: ae44ed76e4bf9d842b3459a4ccd51907429b5055 [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 Esfahbod64aef3a2008-01-23 16:14:38 -05003 *
Behdad Esfahbodc755cb32010-04-22 00:11:43 -04004 * This is part of HarfBuzz, a text shaping library.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05005 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Red Hat Author(s): Behdad Esfahbod
25 */
26
Behdad Esfahbod0f0cd9d2010-06-09 06:32:56 -040027#ifndef HB_OPEN_TYPE_PRIVATE_HH
28#define HB_OPEN_TYPE_PRIVATE_HH
Behdad Esfahbod12c45682006-12-28 06:10:59 -050029
Behdad Esfahbodc57d4542011-04-20 18:50:27 -040030#include "hb-private.hh"
Behdad Esfahbod12c45682006-12-28 06:10:59 -050031
Behdad Esfahbod70de50c2009-08-04 00:58:28 -040032#include "hb-blob.h"
33
Behdad Esfahboda16ecbf2008-01-23 17:01:55 -050034
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040035
Behdad Esfahbod196598b2009-08-04 11:04:32 -040036/*
37 * Casts
38 */
39
Behdad Esfahbod187454c2010-04-23 16:35:01 -040040/* Cast to struct T, reference to reference */
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040041template<typename Type, typename TObject>
Behdad Esfahbod187454c2010-04-23 16:35:01 -040042inline const Type& CastR(const TObject &X)
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040043{ return reinterpret_cast<const Type&> (X); }
44template<typename Type, typename TObject>
Behdad Esfahbod187454c2010-04-23 16:35:01 -040045inline Type& CastR(TObject &X)
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040046{ return reinterpret_cast<Type&> (X); }
Behdad Esfahbod196598b2009-08-04 11:04:32 -040047
Behdad Esfahbod187454c2010-04-23 16:35:01 -040048/* Cast to struct T, pointer to pointer */
49template<typename Type, typename TObject>
50inline const Type* CastP(const TObject *X)
51{ return reinterpret_cast<const Type*> (X); }
52template<typename Type, typename TObject>
53inline Type* CastP(TObject *X)
54{ return reinterpret_cast<Type*> (X); }
55
Behdad Esfahbod09766b12010-05-10 17:36:03 -040056/* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
57 * location pointed to by P plus Ofs bytes. */
58template<typename Type>
59inline const Type& StructAtOffset(const void *P, unsigned int offset)
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -040060{ return * reinterpret_cast<const Type*> ((const char *) P + offset); }
Behdad Esfahbod09766b12010-05-10 17:36:03 -040061template<typename Type>
62inline Type& StructAtOffset(void *P, unsigned int offset)
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -040063{ return * reinterpret_cast<Type*> ((char *) P + offset); }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -040064
Behdad Esfahbod2e2f43e2010-04-21 22:30:36 -040065/* StructAfter<T>(X) returns the struct T& that is placed after X.
Behdad Esfahbod29c3f5e2010-04-21 23:01:00 -040066 * Works with X of variable size also. X must implement get_size() */
Behdad Esfahbode961c862010-04-21 15:56:11 -040067template<typename Type, typename TObject>
68inline const Type& StructAfter(const TObject &X)
Behdad Esfahbod09766b12010-05-10 17:36:03 -040069{ return StructAtOffset<Type>(&X, X.get_size()); }
Behdad Esfahbode961c862010-04-21 15:56:11 -040070template<typename Type, typename TObject>
71inline Type& StructAfter(TObject &X)
Behdad Esfahbod09766b12010-05-10 17:36:03 -040072{ return StructAtOffset<Type>(&X, X.get_size()); }
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040073
Behdad Esfahbode961c862010-04-21 15:56:11 -040074
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -050075
Behdad Esfahbod70de50c2009-08-04 00:58:28 -040076/*
Behdad Esfahbode45d3f82010-05-06 19:33:31 -040077 * Size checking
78 */
79
Behdad Esfahbodf6796352010-05-13 13:34:17 -040080/* Check _assertion in a method environment */
Behdad Esfahbod596e4712010-05-10 18:47:48 -040081#define _DEFINE_SIZE_ASSERTION(_assertion) \
Behdad Esfahbod33afa4e2010-05-10 18:35:02 -040082 inline void _size_assertion (void) const \
Behdad Esfahbod596e4712010-05-10 18:47:48 -040083 { ASSERT_STATIC (_assertion); }
Behdad Esfahbodf6796352010-05-13 13:34:17 -040084/* Check that _code compiles in a method environment */
85#define _DEFINE_COMPILES_ASSERTION(_code) \
86 inline void _compiles_assertion (void) const \
87 { _code; }
Behdad Esfahbod0abcc3b2010-05-10 17:04:20 -040088
89
Behdad Esfahbode45d3f82010-05-06 19:33:31 -040090#define DEFINE_SIZE_STATIC(size) \
Behdad Esfahbod596e4712010-05-10 18:47:48 -040091 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size)); \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -040092 static const unsigned int static_size = (size); \
93 static const unsigned int min_size = (size)
94
Behdad Esfahbodb3651232010-05-10 16:57:29 -040095/* Size signifying variable-sized array */
96#define VAR 1
Behdad Esfahbodb3651232010-05-10 16:57:29 -040097
Behdad Esfahbod596e4712010-05-10 18:47:48 -040098#define DEFINE_SIZE_UNION(size, _member) \
99 _DEFINE_SIZE_ASSERTION (this->u._member.static_size == (size)); \
100 static const unsigned int min_size = (size)
101
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400102#define DEFINE_SIZE_MIN(size) \
Behdad Esfahbod596e4712010-05-10 18:47:48 -0400103 _DEFINE_SIZE_ASSERTION (sizeof (*this) >= (size)); \
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400104 static const unsigned int min_size = (size)
105
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400106#define DEFINE_SIZE_ARRAY(size, array) \
Behdad Esfahbodf6796352010-05-13 13:34:17 -0400107 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \
108 _DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400109 static const unsigned int min_size = (size)
110
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400111#define DEFINE_SIZE_ARRAY2(size, array1, array2) \
Behdad Esfahbodf6796352010-05-13 13:34:17 -0400112 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \
113 _DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].static_size) \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400114 static const unsigned int min_size = (size)
115
116
117
118/*
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400119 * Null objects
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400120 */
121
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400122/* Global nul-content Null pool. Enlarge as necessary. */
Behdad Esfahbodcf5585c2010-05-19 12:03:35 -0400123static const void *_NullPool[64 / sizeof (void *)];
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400124
Behdad Esfahbodd2c2ca82010-05-10 19:58:25 -0400125/* Generic nul-content Null objects. */
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400126template <typename Type>
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400127static inline const Type& Null (void) {
Behdad Esfahboded074222010-05-10 18:08:46 -0400128 ASSERT_STATIC (Type::min_size <= sizeof (_NullPool));
Behdad Esfahbod187454c2010-04-23 16:35:01 -0400129 return *CastP<Type> (_NullPool);
Behdad Esfahbod9d367782010-04-21 00:32:47 -0400130}
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400131
132/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
Behdad Esfahbod65f46b02010-05-06 19:35:19 -0400133#define DEFINE_NULL_DATA(Type, data) \
134static const char _Null##Type[Type::min_size + 1] = data; /* +1 is for nul-termination in data */ \
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400135template <> \
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400136inline const Type& Null<Type> (void) { \
Behdad Esfahbod187454c2010-04-23 16:35:01 -0400137 return *CastP<Type> (_Null##Type); \
Behdad Esfahbod565c80b2010-04-22 10:26:35 -0400138} /* The following line really exists such that we end in a place needing semicolon */ \
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400139ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type))
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400140
141/* Accessor macro. */
Behdad Esfahbod9d367782010-04-21 00:32:47 -0400142#define Null(Type) Null<Type>()
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400143
144
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400145
146/*
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400147 * Sanitize
148 */
149
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400150#ifndef HB_DEBUG_SANITIZE
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400151#define HB_DEBUG_SANITIZE (HB_DEBUG+0)
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400152#endif
153
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400154
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400155#define TRACE_SANITIZE() \
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400156 hb_auto_trace_t<HB_DEBUG_SANITIZE> trace (&c->debug_depth, "SANITIZE", this, NULL, HB_FUNC);
Behdad Esfahbod807c5b02010-04-28 20:25:22 -0400157
158
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400159struct hb_sanitize_context_t
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400160{
Behdad Esfahbod31f18ab2011-06-15 09:49:58 -0400161 inline void init (hb_blob_t *b)
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400162 {
Behdad Esfahbod31f18ab2011-06-15 09:49:58 -0400163 this->blob = hb_blob_reference (b);
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400164 this->writable = false;
165 }
166
167 inline void setup (void)
168 {
Behdad Esfahbod4101ca72011-05-11 14:30:56 -0400169 this->start = hb_blob_get_data (this->blob, NULL);
170 this->end = this->start + hb_blob_get_length (this->blob);
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400171 this->edit_count = 0;
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400172 this->debug_depth = 0;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400173
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400174 DEBUG_MSG (SANITIZE, this->blob,
175 "init [%p..%p] (%lu bytes)",
176 this->start, this->end,
177 (unsigned long) (this->end - this->start));
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400178 }
179
180 inline void finish (void)
181 {
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400182 DEBUG_MSG (SANITIZE, this->blob,
183 "fini [%p..%p] %u edit requests",
184 this->start, this->end, this->edit_count);
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400185
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400186 hb_blob_destroy (this->blob);
187 this->blob = NULL;
188 this->start = this->end = NULL;
189 }
190
Behdad Esfahbod4ad2cc52010-05-06 09:24:24 -0400191 inline bool check_range (const void *base, unsigned int len) const
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400192 {
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400193 const char *p = (const char *) base;
194 bool ret = this->start <= p &&
195 p <= this->end &&
196 (unsigned int) (this->end - p) >= len;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400197
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400198 DEBUG_MSG_LEVEL (SANITIZE, this->blob, this->debug_depth,
199 "%-*d-> range [%p..%p] (%d bytes) in [%p..%p] -> %s",
Behdad Esfahbod43ff2032011-07-25 17:35:24 -0400200 this->debug_depth, this->debug_depth,
201 p, p + len, len,
202 this->start, this->end,
203 ret ? "pass" : "FAIL");
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400204
Behdad Esfahbod27e302d2010-05-05 00:26:16 -0400205 return likely (ret);
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400206 }
207
Behdad Esfahbod1cd1e112010-05-05 20:15:14 -0400208 inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400209 {
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400210 const char *p = (const char *) base;
Behdad Esfahbod080a0eb2011-04-28 16:01:01 -0400211 bool overflows = _hb_unsigned_int_mul_overflows (len, record_size);
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400212
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400213 DEBUG_MSG_LEVEL (SANITIZE, this->blob, this->debug_depth,
214 "%-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s",
Behdad Esfahbod43ff2032011-07-25 17:35:24 -0400215 this->debug_depth, this->debug_depth,
216 p, p + (record_size * len), record_size, len, (unsigned long) record_size * len,
217 this->start, this->end,
218 !overflows ? "does not overflow" : "OVERFLOWS FAIL");
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400219
Behdad Esfahbod4ad2cc52010-05-06 09:24:24 -0400220 return likely (!overflows && this->check_range (base, record_size * len));
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400221 }
222
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400223 template <typename Type>
224 inline bool check_struct (const Type *obj) const
225 {
Behdad Esfahbod54842372010-05-10 18:13:32 -0400226 return likely (this->check_range (obj, obj->min_size));
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400227 }
228
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400229 inline bool can_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400230 {
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400231 const char *p = (const char *) base;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400232 this->edit_count++;
233
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400234 DEBUG_MSG_LEVEL (SANITIZE, this->blob, this->debug_depth,
235 "%-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s",
Behdad Esfahbod43ff2032011-07-25 17:35:24 -0400236 this->debug_depth, this->debug_depth,
237 this->edit_count,
238 p, p + len, len,
239 this->start, this->end,
240 this->writable ? "granted" : "REJECTED");
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400241
242 return this->writable;
243 }
244
Behdad Esfahbod705e2152010-05-05 01:40:25 -0400245 unsigned int debug_depth;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400246 const char *start, *end;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400247 bool writable;
Behdad Esfahbod254933c2010-04-23 13:57:10 -0400248 unsigned int edit_count;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400249 hb_blob_t *blob;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400250};
251
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400252
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400253
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400254/* Template to sanitize an object. */
255template <typename Type>
256struct Sanitizer
257{
258 static hb_blob_t *sanitize (hb_blob_t *blob) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400259 hb_sanitize_context_t c[1] = {{0}};
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400260 bool sane;
261
Behdad Esfahbodd0b65732009-08-06 18:34:47 -0400262 /* TODO is_sane() stuff */
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400263
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400264 c->init (blob);
265
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400266 retry:
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400267 DEBUG_MSG_FUNC (SANITIZE, blob, "start");
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400268
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400269 c->setup ();
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400270
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400271 if (unlikely (!c->start)) {
272 c->finish ();
Behdad Esfahbod48146e52010-05-10 20:07:56 -0400273 return blob;
274 }
275
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400276 Type *t = CastP<Type> (const_cast<char *> (c->start));
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400277
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400278 sane = t->sanitize (c);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400279 if (sane) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400280 if (c->edit_count) {
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400281 DEBUG_MSG_FUNC (SANITIZE, blob, "passed first round with %d edits; going for second round", c->edit_count);
Behdad Esfahbodfa030172010-04-29 13:48:26 -0400282
Behdad Esfahbod8b534612009-08-19 18:16:50 -0400283 /* sanitize again to ensure no toe-stepping */
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400284 c->edit_count = 0;
285 sane = t->sanitize (c);
286 if (c->edit_count) {
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400287 DEBUG_MSG_FUNC (SANITIZE, blob, "requested %d edits in second round; FAILLING", c->edit_count);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400288 sane = false;
289 }
290 }
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400291 } else {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400292 unsigned int edit_count = c->edit_count;
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400293 if (edit_count && !c->writable) {
294 c->start = hb_blob_get_data_writable (blob, NULL);
295 c->end = c->start + hb_blob_get_length (blob);
296
297 if (c->start) {
298 c->writable = true;
299 /* ok, we made it writable by relocating. try again */
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400300 DEBUG_MSG_FUNC (SANITIZE, blob, "retry");
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400301 goto retry;
302 }
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400303 }
304 }
305
Behdad Esfahbod4101ca72011-05-11 14:30:56 -0400306 c->finish ();
307
Behdad Esfahbodcc06c242011-07-25 20:25:44 -0400308 DEBUG_MSG_FUNC (SANITIZE, blob, sane ? "PASSED" : "FAILED");
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400309 if (sane)
310 return blob;
311 else {
312 hb_blob_destroy (blob);
Behdad Esfahbod49110622011-05-02 19:36:39 -0400313 return hb_blob_get_empty ();
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400314 }
315 }
Behdad Esfahbodb435ab72010-05-10 19:51:57 -0400316
317 static const Type* lock_instance (hb_blob_t *blob) {
Behdad Esfahbod1c9f8712011-05-06 22:28:26 -0400318 hb_blob_make_immutable (blob);
319 const char *base = hb_blob_get_data (blob, NULL);
Behdad Esfahbodb435ab72010-05-10 19:51:57 -0400320 return unlikely (!base) ? &Null(Type) : CastP<Type> (base);
321 }
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400322};
323
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400324
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400325
326
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500327/*
328 *
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400329 * The OpenType Font File: Data Types
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500330 */
331
332
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500333/* "The following data types are used in the OpenType font file.
334 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500335
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400336/*
337 * Int types
338 */
339
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400340
Behdad Esfahbodd7bf4732011-08-05 18:18:21 -0400341template <typename Type, int Bytes> struct BEInt;
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500342
Behdad Esfahbodf1aaa2a2010-04-23 15:19:50 -0400343/* LONGTERMTODO: On machines allowing unaligned access, we can make the
344 * following tighter by using byteswap instructions on ints directly. */
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400345template <typename Type>
Behdad Esfahbodd7bf4732011-08-05 18:18:21 -0400346struct BEInt<Type, 2>
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400347{
348 public:
Behdad Esfahbod81408cd2010-07-23 14:46:57 -0400349 inline void set (Type i) { hb_be_uint16_put (v,i); }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400350 inline operator Type (void) const { return hb_be_uint16_get (v); }
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400351 inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_eq (v, o.v); }
Behdad Esfahbod01c01612010-04-21 22:49:56 -0400352 inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400353 private: uint8_t v[2];
354};
355template <typename Type>
Behdad Esfahbodd7bf4732011-08-05 18:18:21 -0400356struct BEInt<Type, 4>
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400357{
358 public:
Behdad Esfahbod81408cd2010-07-23 14:46:57 -0400359 inline void set (Type i) { hb_be_uint32_put (v,i); }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400360 inline operator Type (void) const { return hb_be_uint32_get (v); }
Behdad Esfahbod153142d2011-04-27 01:49:03 -0400361 inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_eq (v, o.v); }
Behdad Esfahbod01c01612010-04-21 22:49:56 -0400362 inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400363 private: uint8_t v[4];
364};
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500365
Behdad Esfahbod2467c662010-04-21 23:11:45 -0400366/* Integer types in big-endian order and no alignment requirement */
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400367template <typename Type>
368struct IntType
369{
Behdad Esfahbod81408cd2010-07-23 14:46:57 -0400370 inline void set (Type i) { v.set (i); }
Behdad Esfahbod01c01612010-04-21 22:49:56 -0400371 inline operator Type(void) const { return v; }
372 inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
373 inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
Behdad Esfahbod4e573712010-09-28 16:23:58 -0400374 inline int cmp (Type a) const { Type b = v; return a < b ? -1 : a == b ? 0 : +1; }
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400375 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400376 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400377 return likely (c->check_struct (this));
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400378 }
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400379 protected:
Behdad Esfahbod569da922010-05-10 16:38:32 -0400380 BEInt<Type, sizeof (Type)> v;
381 public:
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400382 DEFINE_SIZE_STATIC (sizeof (Type));
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400383};
384
Behdad Esfahbodf60271c2011-08-02 09:56:30 -0400385/* Typedef these to avoid clash with windows.h */
386#define USHORT HB_USHORT
387#define SHORT HB_SHORT
388#define ULONG HB_ULONG
389#define LONG HB_LONG
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400390typedef IntType<uint16_t> USHORT; /* 16-bit unsigned integer. */
391typedef IntType<int16_t> SHORT; /* 16-bit signed integer. */
392typedef IntType<uint32_t> ULONG; /* 32-bit unsigned integer. */
393typedef IntType<int32_t> LONG; /* 32-bit signed integer. */
394
Behdad Esfahbode29caf32010-05-19 11:47:17 -0400395/* Date represented in number of seconds since 12:00 midnight, January 1,
396 * 1904. The value is represented as a signed 64-bit integer. */
397struct LONGDATETIME
398{
399 inline bool sanitize (hb_sanitize_context_t *c) {
400 TRACE_SANITIZE ();
401 return likely (c->check_struct (this));
402 }
403 private:
404 LONG major;
405 ULONG minor;
406 public:
407 DEFINE_SIZE_STATIC (8);
408};
409
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500410/* Array of four uint8s (length = 32 bits) used to identify a script, language
411 * system, feature, or baseline */
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400412struct Tag : ULONG
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400413{
Behdad Esfahbodbefc0222006-12-25 09:14:52 -0500414 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400415 inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); }
416 inline operator char* (void) { return reinterpret_cast<char *> (&this->v); }
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400417 public:
418 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500419};
Behdad Esfahbod65f46b02010-05-06 19:35:19 -0400420DEFINE_NULL_DATA (Tag, " ");
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500421
422/* Glyph index number, same as uint16 (length = 16 bits) */
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400423typedef USHORT GlyphID;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500424
Behdad Esfahbodb5db4f12010-05-10 22:22:22 -0400425/* Script/language-system/feature index */
426struct Index : USHORT {
427 static const unsigned int NOT_FOUND_INDEX = 0xFFFF;
428};
429DEFINE_NULL_DATA (Index, "\xff\xff");
430
Behdad Esfahbod1f437e62008-01-23 04:36:40 -0500431/* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400432typedef USHORT Offset;
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400433
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400434/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
435typedef ULONG LongOffset;
436
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500437
438/* CheckSum */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400439struct CheckSum : ULONG
440{
441 static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
442 {
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500443 uint32_t Sum = 0L;
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400444 ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500445
446 while (Table < EndPtr)
447 Sum += *Table++;
448 return Sum;
449 }
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400450 public:
451 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500452};
453
454
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500455/*
456 * Version Numbers
457 */
458
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400459struct FixedVersion
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400460{
Behdad Esfahbod4f28fbd2011-05-31 12:33:11 -0400461 inline uint32_t to_int (void) const { return (major << 16) + minor; }
Behdad Esfahbod96908b82009-05-24 12:30:40 -0400462
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400463 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400464 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400465 return c->check_struct (this);
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400466 }
467
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400468 USHORT major;
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400469 USHORT minor;
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400470 public:
471 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500472};
473
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400474
475
476/*
477 * Template subclasses of Offset and LongOffset that do the dereferencing.
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400478 * Use: (base+offset)
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400479 */
480
481template <typename OffsetType, typename Type>
482struct GenericOffsetTo : OffsetType
483{
Behdad Esfahbod00e23fc2010-04-20 23:50:45 -0400484 inline const Type& operator () (const void *base) const
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400485 {
486 unsigned int offset = *this;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400487 if (unlikely (!offset)) return Null(Type);
Behdad Esfahbod09766b12010-05-10 17:36:03 -0400488 return StructAtOffset<Type> (base, offset);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400489 }
490
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400491 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400492 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400493 if (unlikely (!c->check_struct (this))) return false;
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400494 unsigned int offset = *this;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400495 if (unlikely (!offset)) return true;
Behdad Esfahbod09766b12010-05-10 17:36:03 -0400496 Type &obj = StructAtOffset<Type> (base, offset);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400497 return likely (obj.sanitize (c)) || neuter (c);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400498 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400499 template <typename T>
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400500 inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400501 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400502 if (unlikely (!c->check_struct (this))) return false;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400503 unsigned int offset = *this;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400504 if (unlikely (!offset)) return true;
Behdad Esfahbod09766b12010-05-10 17:36:03 -0400505 Type &obj = StructAtOffset<Type> (base, offset);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400506 return likely (obj.sanitize (c, user_data)) || neuter (c);
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400507 }
508
509 private:
510 /* Set the offset to Null */
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400511 inline bool neuter (hb_sanitize_context_t *c) {
512 if (c->can_edit (this, this->static_size)) {
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400513 this->set (0); /* 0 is Null offset */
514 return true;
515 }
516 return false;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400517 }
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400518};
519template <typename Base, typename OffsetType, typename Type>
520inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
521
522template <typename Type>
523struct OffsetTo : GenericOffsetTo<Offset, Type> {};
524
525template <typename Type>
526struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400527
528
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400529/*
530 * Array Types
531 */
532
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400533template <typename LenType, typename Type>
534struct GenericArrayOf
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400535{
Behdad Esfahbod4f5f1c32010-04-22 00:27:39 -0400536 const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500537 {
538 unsigned int count = len;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400539 if (unlikely (start_offset > count))
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500540 count = 0;
541 else
542 count -= start_offset;
543 count = MIN (count, *pcount);
544 *pcount = count;
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400545 return array + start_offset;
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500546 }
547
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400548 inline const Type& operator [] (unsigned int i) const
549 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400550 if (unlikely (i >= len)) return Null(Type);
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400551 return array[i];
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400552 }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400553 inline unsigned int get_size (void) const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400554 { return len.static_size + len * Type::static_size; }
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400555
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400556 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400557 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400558 if (unlikely (!sanitize_shallow (c))) return false;
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400559
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400560 /* Note: for structs that do not reference other structs,
561 * we do not need to call their sanitize() as we already did
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400562 * a bound check on the aggregate array size. We just include
563 * a small unreachable expression to make sure the structs
564 * pointed to do have a simple sanitize(), ie. they do not
565 * reference other structs via offsets.
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400566 */
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400567 (void) (false && array[0].sanitize (c));
568
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400569 return true;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400570 }
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400571 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400572 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400573 if (unlikely (!sanitize_shallow (c))) return false;
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400574 unsigned int count = len;
575 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400576 if (unlikely (!array[i].sanitize (c, base)))
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400577 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400578 return true;
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400579 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400580 template <typename T>
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400581 inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400582 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400583 if (unlikely (!sanitize_shallow (c))) return false;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400584 unsigned int count = len;
585 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400586 if (unlikely (!array[i].sanitize (c, base, user_data)))
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400587 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400588 return true;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400589 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400590
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400591 private:
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400592 inline bool sanitize_shallow (hb_sanitize_context_t *c) {
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400593 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400594 return c->check_struct (this)
595 && c->check_array (this, Type::static_size, len);
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400596 }
597
598 public:
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400599 LenType len;
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400600 Type array[VAR];
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400601 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400602 DEFINE_SIZE_ARRAY (sizeof (LenType), array);
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400603};
604
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400605/* An array with a USHORT number of elements. */
606template <typename Type>
607struct ArrayOf : GenericArrayOf<USHORT, Type> {};
608
609/* An array with a ULONG number of elements. */
610template <typename Type>
611struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
612
613/* Array of Offset's */
614template <typename Type>
615struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
616
617/* Array of LongOffset's */
618template <typename Type>
619struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
620
621/* LongArray of LongOffset's */
622template <typename Type>
623struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
624
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400625/* Array of offsets relative to the beginning of the array itself. */
626template <typename Type>
627struct OffsetListOf : OffsetArrayOf<Type>
628{
629 inline const Type& operator [] (unsigned int i) const
630 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400631 if (unlikely (i >= this->len)) return Null(Type);
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400632 return this+this->array[i];
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400633 }
634
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400635 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400636 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400637 return OffsetArrayOf<Type>::sanitize (c, this);
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400638 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400639 template <typename T>
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400640 inline bool sanitize (hb_sanitize_context_t *c, T user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400641 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400642 return OffsetArrayOf<Type>::sanitize (c, this, user_data);
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400643 }
644};
645
646
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400647/* An array with a USHORT number of elements,
648 * starting at second element. */
649template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400650struct HeadlessArrayOf
651{
652 inline const Type& operator [] (unsigned int i) const
653 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400654 if (unlikely (i >= len || !i)) return Null(Type);
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400655 return array[i-1];
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400656 }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400657 inline unsigned int get_size (void) const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400658 { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400659
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400660 inline bool sanitize_shallow (hb_sanitize_context_t *c) {
661 return c->check_struct (this)
662 && c->check_array (this, Type::static_size, len);
Behdad Esfahbode5546a42010-04-22 00:45:42 -0400663 }
664
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400665 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400666 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400667 if (unlikely (!sanitize_shallow (c))) return false;
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400668
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400669 /* Note: for structs that do not reference other structs,
670 * we do not need to call their sanitize() as we already did
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400671 * a bound check on the aggregate array size. We just include
672 * a small unreachable expression to make sure the structs
673 * pointed to do have a simple sanitize(), ie. they do not
674 * reference other structs via offsets.
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400675 */
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400676 (void) (false && array[0].sanitize (c));
677
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400678 return true;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400679 }
680
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400681 USHORT len;
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400682 Type array[VAR];
Behdad Esfahboded074222010-05-10 18:08:46 -0400683 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400684 DEFINE_SIZE_ARRAY (sizeof (USHORT), array);
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400685};
686
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500687
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -0400688/* An array with sorted elements. Supports binary searching. */
689template <typename Type>
690struct SortedArrayOf : ArrayOf<Type> {
691
692 template <typename SearchType>
693 inline int search (const SearchType &x) const {
Behdad Esfahbodd7bf4732011-08-05 18:18:21 -0400694 struct Cmp {
695 static int cmp (const SearchType *a, const Type *b) { return b->cmp (*a); }
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -0400696 };
Behdad Esfahbod8f08c322010-10-08 19:43:48 -0400697 const Type *p = (const Type *) bsearch (&x, this->array, this->len, sizeof (this->array[0]), (hb_compare_func_t) Cmp::cmp);
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -0400698 return p ? p - this->array : -1;
699 }
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -0400700};
701
702
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400703
Behdad Esfahbod1e914342009-11-04 18:12:09 -0500704#endif /* HB_OPEN_TYPE_PRIVATE_HH */