blob: fcc8c8093ce52961430c40d97a819b7fa0bb9aab [file] [log] [blame]
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05001/*
Behdad Esfahbodf9b37722010-04-20 15:51:53 -04002 * Copyright (C) 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 Esfahbod5f5b24f2009-08-02 20:03:12 -040027#ifndef HB_OPEN_TYPES_PRIVATE_HH
28#define HB_OPEN_TYPES_PRIVATE_HH
Behdad Esfahbod12c45682006-12-28 06:10:59 -050029
Behdad Esfahbod2098a022009-08-02 19:57:00 -040030#include "hb-private.h"
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 Esfahbodc85c3622010-04-21 23:12:54 -040035/* Table/script/language-system/feature/... not found */
Behdad Esfahbod706ab252008-01-28 05:58:50 -050036#define NO_INDEX ((unsigned int) 0xFFFF)
Behdad Esfahbod5a0b7912009-04-16 04:45:30 -040037
Behdad Esfahbod706ab252008-01-28 05:58:50 -050038
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040039
Behdad Esfahbod196598b2009-08-04 11:04:32 -040040/*
41 * Casts
42 */
43
Behdad Esfahbod0dfcc132010-04-21 23:41:26 -040044/* Cast to "const char *" and "char *" */
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040045template <typename Type>
46inline const char * CharP (const Type* X)
47{ return reinterpret_cast<const char *>(X); }
48template <typename Type>
49inline char * CharP (Type* X)
50{ return reinterpret_cast<char *>(X); }
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -040051
Behdad Esfahbod187454c2010-04-23 16:35:01 -040052/* Cast to struct T, reference to reference */
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040053template<typename Type, typename TObject>
Behdad Esfahbod187454c2010-04-23 16:35:01 -040054inline const Type& CastR(const TObject &X)
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040055{ return reinterpret_cast<const Type&> (X); }
56template<typename Type, typename TObject>
Behdad Esfahbod187454c2010-04-23 16:35:01 -040057inline Type& CastR(TObject &X)
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040058{ return reinterpret_cast<Type&> (X); }
Behdad Esfahbod196598b2009-08-04 11:04:32 -040059
Behdad Esfahbod187454c2010-04-23 16:35:01 -040060/* Cast to struct T, pointer to pointer */
61template<typename Type, typename TObject>
62inline const Type* CastP(const TObject *X)
63{ return reinterpret_cast<const Type*> (X); }
64template<typename Type, typename TObject>
65inline Type* CastP(TObject *X)
66{ return reinterpret_cast<Type*> (X); }
67
Behdad Esfahbod09766b12010-05-10 17:36:03 -040068/* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
69 * location pointed to by P plus Ofs bytes. */
70template<typename Type>
71inline const Type& StructAtOffset(const void *P, unsigned int offset)
72{ return * reinterpret_cast<const Type*> (CharP(P) + offset); }
73template<typename Type>
74inline Type& StructAtOffset(void *P, unsigned int offset)
75{ return * reinterpret_cast<Type*> (CharP(P) + offset); }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -040076
Behdad Esfahbod2e2f43e2010-04-21 22:30:36 -040077/* StructAfter<T>(X) returns the struct T& that is placed after X.
Behdad Esfahbod29c3f5e2010-04-21 23:01:00 -040078 * Works with X of variable size also. X must implement get_size() */
Behdad Esfahbode961c862010-04-21 15:56:11 -040079template<typename Type, typename TObject>
80inline const Type& StructAfter(const TObject &X)
Behdad Esfahbod09766b12010-05-10 17:36:03 -040081{ return StructAtOffset<Type>(&X, X.get_size()); }
Behdad Esfahbode961c862010-04-21 15:56:11 -040082template<typename Type, typename TObject>
83inline Type& StructAfter(TObject &X)
Behdad Esfahbod09766b12010-05-10 17:36:03 -040084{ return StructAtOffset<Type>(&X, X.get_size()); }
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040085
Behdad Esfahbode961c862010-04-21 15:56:11 -040086
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -050087
Behdad Esfahbod70de50c2009-08-04 00:58:28 -040088/*
Behdad Esfahbode45d3f82010-05-06 19:33:31 -040089 * Size checking
90 */
91
Behdad Esfahbod0abcc3b2010-05-10 17:04:20 -040092#define ASSERT_SIZE(_thing, _size) ASSERT_STATIC (sizeof (_thing) == (_size))
93
94#define _DEFINE_SIZE_ASSERTION(_size) \
95 inline void _size_assertion (void) const { ASSERT_SIZE (*this, _size); }
96
97
Behdad Esfahbode45d3f82010-05-06 19:33:31 -040098#define DEFINE_SIZE_STATIC(size) \
Behdad Esfahbod0abcc3b2010-05-10 17:04:20 -040099 _DEFINE_SIZE_ASSERTION (size); \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400100 static inline unsigned int get_size (void) { return (size); } \
101 static const unsigned int static_size = (size); \
102 static const unsigned int min_size = (size)
103
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400104/* Size signifying variable-sized array */
105#define VAR 1
106#define VAR0 (VAR+0)
107
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400108#define DEFINE_SIZE_MIN(size) \
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400109 static const unsigned int min_size = (size)
110
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400111#define DEFINE_SIZE_VAR(size, _var_type) \
Behdad Esfahbod0abcc3b2010-05-10 17:04:20 -0400112 _DEFINE_SIZE_ASSERTION ((size) + VAR0 * sizeof (_var_type)); \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400113 static const unsigned int min_size = (size)
114
Behdad Esfahbod569da922010-05-10 16:38:32 -0400115#define DEFINE_SIZE_VAR2(size, _var_type1, _var_type2) \
Behdad Esfahbod0abcc3b2010-05-10 17:04:20 -0400116 _DEFINE_SIZE_ASSERTION ((size) + VAR0 * sizeof (_var_type1) + VAR0 * sizeof (_var_type2)); \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400117 static const unsigned int min_size = (size)
118
119
120
121/*
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400122 * Null objects
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400123 */
124
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400125/* Global nul-content Null pool. Enlarge as necessary. */
Behdad Esfahbod9d367782010-04-21 00:32:47 -0400126static const void *_NullPool[32 / sizeof (void *)];
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400127
128/* Generic template for nul-content sizeof-sized Null objects. */
129template <typename Type>
Behdad Esfahbod9d367782010-04-21 00:32:47 -0400130static inline const Type& Null () {
131 ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool));
Behdad Esfahbod187454c2010-04-23 16:35:01 -0400132 return *CastP<Type> (_NullPool);
Behdad Esfahbod9d367782010-04-21 00:32:47 -0400133}
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400134
135/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
Behdad Esfahbod65f46b02010-05-06 19:35:19 -0400136#define DEFINE_NULL_DATA(Type, data) \
137static const char _Null##Type[Type::min_size + 1] = data; /* +1 is for nul-termination in data */ \
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400138template <> \
Behdad Esfahbod9d367782010-04-21 00:32:47 -0400139inline const Type& Null<Type> () { \
Behdad Esfahbod187454c2010-04-23 16:35:01 -0400140 return *CastP<Type> (_Null##Type); \
Behdad Esfahbod565c80b2010-04-22 10:26:35 -0400141} /* The following line really exists such that we end in a place needing semicolon */ \
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400142ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type))
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400143
144/* Accessor macro. */
Behdad Esfahbod9d367782010-04-21 00:32:47 -0400145#define Null(Type) Null<Type>()
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400146
147
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400148/*
Behdad Esfahboddfc8cbe2010-05-05 00:19:46 -0400149 * Trace
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400150 */
151
Behdad Esfahboddfc8cbe2010-05-05 00:19:46 -0400152
153template <int max_depth>
154struct hb_trace_t {
155 explicit hb_trace_t (unsigned int *pdepth) : pdepth(pdepth) { if (max_depth) ++*pdepth; }
156 ~hb_trace_t (void) { if (max_depth) --*pdepth; }
157
158 inline void log (const char *what, const char *function, const void *obj)
159 {
160 if (*pdepth < max_depth)
161 fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, *pdepth, *pdepth, function);
162 }
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400163
164 private:
Behdad Esfahboddfc8cbe2010-05-05 00:19:46 -0400165 unsigned int *pdepth;
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400166};
Behdad Esfahboddfc8cbe2010-05-05 00:19:46 -0400167template <> /* Optimize when tracing is disabled */
168struct hb_trace_t<0> {
169 explicit hb_trace_t (unsigned int *p) {}
170 inline void log (const char *what, const char *function, const void *obj) {};
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400171};
172
173
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400174
175/*
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400176 * Sanitize
177 */
178
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400179#ifndef HB_DEBUG_SANITIZE
Behdad Esfahbod807c5b02010-04-28 20:25:22 -0400180#define HB_DEBUG_SANITIZE HB_DEBUG+0
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400181#endif
182
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400183
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400184#define TRACE_SANITIZE() \
Behdad Esfahboddfc8cbe2010-05-05 00:19:46 -0400185 hb_trace_t<HB_DEBUG_SANITIZE> trace (&context->debug_depth); \
186 trace.log ("SANITIZE", HB_FUNC, this);
Behdad Esfahbod807c5b02010-04-28 20:25:22 -0400187
188
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400189struct hb_sanitize_context_t
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400190{
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400191 inline void init (hb_blob_t *blob)
192 {
193 this->blob = hb_blob_reference (blob);
194 this->start = hb_blob_lock (blob);
195 this->end = this->start + hb_blob_get_length (blob);
196 this->writable = hb_blob_is_writable (blob);
197 this->edit_count = 0;
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400198 this->debug_depth = 0;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400199
200 if (HB_DEBUG_SANITIZE)
201 fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
202 this->blob, this->start, this->end, this->end - this->start);
203 }
204
205 inline void finish (void)
206 {
207 if (HB_DEBUG_SANITIZE)
208 fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
209 this->blob, this->start, this->end, this->edit_count);
210
211 hb_blob_unlock (this->blob);
212 hb_blob_destroy (this->blob);
213 this->blob = NULL;
214 this->start = this->end = NULL;
215 }
216
Behdad Esfahbod4ad2cc52010-05-06 09:24:24 -0400217 inline bool check_range (const void *base, unsigned int len) const
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400218 {
219 bool ret = this->start <= base &&
220 base <= this->end &&
Behdad Esfahbod1cd1e112010-05-05 20:15:14 -0400221 (unsigned int) (this->end - CharP(base)) >= len;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400222
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400223 if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE) \
Behdad Esfahbod4ad2cc52010-05-06 09:24:24 -0400224 fprintf (stderr, "SANITIZE(%p) %-*d-> range [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400225 base,
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400226 this->debug_depth, this->debug_depth,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400227 base, CharP(base) + len, len,
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400228 this->start, this->end,
229 ret ? "pass" : "FAIL");
230
Behdad Esfahbod27e302d2010-05-05 00:26:16 -0400231 return likely (ret);
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400232 }
233
Behdad Esfahbod1cd1e112010-05-05 20:15:14 -0400234 inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400235 {
236 bool overflows = len >= ((unsigned int) -1) / record_size;
237
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400238 if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400239 fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
240 base,
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400241 this->debug_depth, this->debug_depth,
Behdad Esfahbod1cd1e112010-05-05 20:15:14 -0400242 base, CharP(base) + (record_size * len), record_size, len, (unsigned long) record_size * len,
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400243 this->start, this->end,
244 !overflows ? "does not overflow" : "OVERFLOWS FAIL");
245
Behdad Esfahbod4ad2cc52010-05-06 09:24:24 -0400246 return likely (!overflows && this->check_range (base, record_size * len));
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400247 }
248
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400249 template <typename Type>
250 inline bool check_struct (const Type *obj) const
251 {
252 return likely (this->check_range (obj, sizeof (*obj)));
253 }
254
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400255 inline bool can_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400256 {
257 this->edit_count++;
258
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400259 if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400260 fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
261 base,
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400262 this->debug_depth, this->debug_depth,
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400263 this->edit_count,
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400264 base, CharP(base)+len, len,
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400265 this->start, this->end,
266 this->writable ? "granted" : "REJECTED");
267
268 return this->writable;
269 }
270
Behdad Esfahbod705e2152010-05-05 01:40:25 -0400271 unsigned int debug_depth;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400272 const char *start, *end;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400273 bool writable;
Behdad Esfahbod254933c2010-04-23 13:57:10 -0400274 unsigned int edit_count;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400275 hb_blob_t *blob;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400276};
277
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400278
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400279
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400280/* Template to sanitize an object. */
281template <typename Type>
282struct Sanitizer
283{
284 static hb_blob_t *sanitize (hb_blob_t *blob) {
Behdad Esfahbod705e2152010-05-05 01:40:25 -0400285 hb_sanitize_context_t context[1] = {{0}};
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400286 bool sane;
287
Behdad Esfahbodd0b65732009-08-06 18:34:47 -0400288 /* TODO is_sane() stuff */
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400289
290 retry:
Behdad Esfahbodfa030172010-04-29 13:48:26 -0400291 if (HB_DEBUG_SANITIZE)
Behdad Esfahbod7d3a1262010-04-29 13:54:01 -0400292 fprintf (stderr, "Sanitizer %p start %s\n", blob, HB_FUNC);
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400293
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400294 context->init (blob);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400295
Behdad Esfahbod27e302d2010-05-05 00:26:16 -0400296 Type *t = CastP<Type> (const_cast<char *> (context->start));
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400297
Behdad Esfahbod39840472010-05-05 00:23:19 -0400298 sane = t->sanitize (context);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400299 if (sane) {
Behdad Esfahbod27e302d2010-05-05 00:26:16 -0400300 if (context->edit_count) {
Behdad Esfahbodfa030172010-04-29 13:48:26 -0400301 if (HB_DEBUG_SANITIZE)
302 fprintf (stderr, "Sanitizer %p passed first round with %d edits; doing a second round %s\n",
Behdad Esfahbod27e302d2010-05-05 00:26:16 -0400303 blob, context->edit_count, HB_FUNC);
Behdad Esfahbodfa030172010-04-29 13:48:26 -0400304
Behdad Esfahbod8b534612009-08-19 18:16:50 -0400305 /* sanitize again to ensure no toe-stepping */
Behdad Esfahbod27e302d2010-05-05 00:26:16 -0400306 context->edit_count = 0;
Behdad Esfahbod39840472010-05-05 00:23:19 -0400307 sane = t->sanitize (context);
Behdad Esfahbod27e302d2010-05-05 00:26:16 -0400308 if (context->edit_count) {
Behdad Esfahbodfa030172010-04-29 13:48:26 -0400309 if (HB_DEBUG_SANITIZE)
310 fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
Behdad Esfahbod27e302d2010-05-05 00:26:16 -0400311 blob, context->edit_count, HB_FUNC);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400312 sane = false;
313 }
314 }
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400315 context->finish ();
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400316 } else {
Behdad Esfahbod27e302d2010-05-05 00:26:16 -0400317 unsigned int edit_count = context->edit_count;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400318 context->finish ();
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400319 if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
320 /* ok, we made it writable by relocating. try again */
Behdad Esfahbodfa030172010-04-29 13:48:26 -0400321 if (HB_DEBUG_SANITIZE)
Behdad Esfahbod7d3a1262010-04-29 13:54:01 -0400322 fprintf (stderr, "Sanitizer %p retry %s\n", blob, HB_FUNC);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400323 goto retry;
324 }
325 }
326
Behdad Esfahbodfa030172010-04-29 13:48:26 -0400327 if (HB_DEBUG_SANITIZE)
Behdad Esfahbod7d3a1262010-04-29 13:54:01 -0400328 fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", HB_FUNC);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400329 if (sane)
330 return blob;
331 else {
332 hb_blob_destroy (blob);
333 return hb_blob_create_empty ();
334 }
335 }
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400336};
337
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400338
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400339
340
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500341/*
342 *
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400343 * The OpenType Font File: Data Types
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500344 */
345
346
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500347/* "The following data types are used in the OpenType font file.
348 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500349
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400350/*
351 * Int types
352 */
353
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400354
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400355template <typename Type, int Bytes> class BEInt;
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500356
Behdad Esfahbodf1aaa2a2010-04-23 15:19:50 -0400357/* LONGTERMTODO: On machines allowing unaligned access, we can make the
358 * following tighter by using byteswap instructions on ints directly. */
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400359template <typename Type>
360class BEInt<Type, 2>
361{
362 public:
Behdad Esfahbod01c01612010-04-21 22:49:56 -0400363 inline class BEInt<Type,2>& operator = (Type i) { hb_be_uint16_put (v,i); return *this; }
364 inline operator Type () const { return hb_be_uint16_get (v); }
365 inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_cmp (v, o.v); }
366 inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400367 private: uint8_t v[2];
368};
369template <typename Type>
370class BEInt<Type, 4>
371{
372 public:
Behdad Esfahbod01c01612010-04-21 22:49:56 -0400373 inline class BEInt<Type,4>& operator = (Type i) { hb_be_uint32_put (v,i); return *this; }
374 inline operator Type () const { return hb_be_uint32_get (v); }
375 inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_cmp (v, o.v); }
376 inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400377 private: uint8_t v[4];
378};
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500379
Behdad Esfahbod2467c662010-04-21 23:11:45 -0400380/* Integer types in big-endian order and no alignment requirement */
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400381template <typename Type>
382struct IntType
383{
Behdad Esfahbod01c01612010-04-21 22:49:56 -0400384 inline void set (Type i) { v = i; }
385 inline operator Type(void) const { return v; }
386 inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
387 inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
Behdad Esfahbod39840472010-05-05 00:23:19 -0400388 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400389 TRACE_SANITIZE ();
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400390 return context->check_struct (this);
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400391 }
Behdad Esfahbod569da922010-05-10 16:38:32 -0400392 private:
393 BEInt<Type, sizeof (Type)> v;
394 public:
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400395 DEFINE_SIZE_STATIC (sizeof (Type));
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400396};
397
398typedef IntType<uint16_t> USHORT; /* 16-bit unsigned integer. */
399typedef IntType<int16_t> SHORT; /* 16-bit signed integer. */
400typedef IntType<uint32_t> ULONG; /* 32-bit unsigned integer. */
401typedef IntType<int32_t> LONG; /* 32-bit signed integer. */
402
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500403/* Array of four uint8s (length = 32 bits) used to identify a script, language
404 * system, feature, or baseline */
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400405struct Tag : ULONG
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400406{
Behdad Esfahbodbefc0222006-12-25 09:14:52 -0500407 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
Behdad Esfahbod0dfcc132010-04-21 23:41:26 -0400408 inline operator const char* (void) const { return CharP(this); }
Behdad Esfahbod198facd2010-04-21 13:35:36 -0400409 inline operator char* (void) { return CharP(this); }
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400410 public:
411 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500412};
Behdad Esfahbod65f46b02010-05-06 19:35:19 -0400413DEFINE_NULL_DATA (Tag, " ");
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500414
415/* Glyph index number, same as uint16 (length = 16 bits) */
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400416typedef USHORT GlyphID;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500417
Behdad Esfahbod1f437e62008-01-23 04:36:40 -0500418/* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400419typedef USHORT Offset;
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400420
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400421/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
422typedef ULONG LongOffset;
423
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500424
425/* CheckSum */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400426struct CheckSum : ULONG
427{
428 static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
429 {
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500430 uint32_t Sum = 0L;
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400431 ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500432
433 while (Table < EndPtr)
434 Sum += *Table++;
435 return Sum;
436 }
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400437 public:
438 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500439};
440
441
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500442/*
443 * Version Numbers
444 */
445
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400446struct FixedVersion
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400447{
Behdad Esfahbod09c292e2009-05-26 19:48:16 -0400448 inline operator uint32_t (void) const { return (major << 16) + minor; }
Behdad Esfahbod96908b82009-05-24 12:30:40 -0400449
Behdad Esfahbod39840472010-05-05 00:23:19 -0400450 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400451 TRACE_SANITIZE ();
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400452 return context->check_struct (this);
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400453 }
454
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400455 USHORT major;
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400456 USHORT minor;
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400457 public:
458 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500459};
460
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400461
462
463/*
464 * Template subclasses of Offset and LongOffset that do the dereferencing.
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400465 * Use: (base+offset)
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400466 */
467
468template <typename OffsetType, typename Type>
469struct GenericOffsetTo : OffsetType
470{
Behdad Esfahbod00e23fc2010-04-20 23:50:45 -0400471 inline const Type& operator () (const void *base) const
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400472 {
473 unsigned int offset = *this;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400474 if (unlikely (!offset)) return Null(Type);
Behdad Esfahbod09766b12010-05-10 17:36:03 -0400475 return StructAtOffset<Type> (base, offset);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400476 }
477
Behdad Esfahbod39840472010-05-05 00:23:19 -0400478 inline bool sanitize (hb_sanitize_context_t *context, void *base) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400479 TRACE_SANITIZE ();
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400480 if (!context->check_struct (this)) return false;
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400481 unsigned int offset = *this;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400482 if (unlikely (!offset)) return true;
Behdad Esfahbod09766b12010-05-10 17:36:03 -0400483 Type &obj = StructAtOffset<Type> (base, offset);
Behdad Esfahbod39840472010-05-05 00:23:19 -0400484 return likely (obj.sanitize (context)) || neuter (context);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400485 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400486 template <typename T>
Behdad Esfahbod39840472010-05-05 00:23:19 -0400487 inline bool sanitize (hb_sanitize_context_t *context, void *base, T user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400488 TRACE_SANITIZE ();
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400489 if (!context->check_struct (this)) return false;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400490 unsigned int offset = *this;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400491 if (unlikely (!offset)) return true;
Behdad Esfahbod09766b12010-05-10 17:36:03 -0400492 Type &obj = StructAtOffset<Type> (base, offset);
Behdad Esfahbod39840472010-05-05 00:23:19 -0400493 return likely (obj.sanitize (context, user_data)) || neuter (context);
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400494 }
495
496 private:
497 /* Set the offset to Null */
Behdad Esfahbod39840472010-05-05 00:23:19 -0400498 inline bool neuter (hb_sanitize_context_t *context) {
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400499 if (context->can_edit (this, this->static_size)) {
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400500 this->set (0); /* 0 is Null offset */
501 return true;
502 }
503 return false;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400504 }
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400505};
506template <typename Base, typename OffsetType, typename Type>
507inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
508
509template <typename Type>
510struct OffsetTo : GenericOffsetTo<Offset, Type> {};
511
512template <typename Type>
513struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400514
515
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400516/*
517 * Array Types
518 */
519
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400520template <typename LenType, typename Type>
521struct GenericArrayOf
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400522{
Behdad Esfahbod2cb08452010-04-21 22:37:31 -0400523 const Type *array(void) const { return &StructAfter<Type> (len); }
Behdad Esfahbod2e2f43e2010-04-21 22:30:36 -0400524 Type *array(void) { return &StructAfter<Type> (len); }
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500525
Behdad Esfahbod4f5f1c32010-04-22 00:27:39 -0400526 const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500527 {
528 unsigned int count = len;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400529 if (unlikely (start_offset > count))
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500530 count = 0;
531 else
532 count -= start_offset;
533 count = MIN (count, *pcount);
534 *pcount = count;
Behdad Esfahbod2cb08452010-04-21 22:37:31 -0400535 return array() + start_offset;
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500536 }
537
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400538 inline const Type& operator [] (unsigned int i) const
539 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400540 if (unlikely (i >= len)) return Null(Type);
Behdad Esfahbod2cb08452010-04-21 22:37:31 -0400541 return array()[i];
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400542 }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400543 inline unsigned int get_size () const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400544 { return len.static_size + len * Type::static_size; }
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400545
Behdad Esfahbod39840472010-05-05 00:23:19 -0400546 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400547 TRACE_SANITIZE ();
Behdad Esfahbod39840472010-05-05 00:23:19 -0400548 if (!likely (sanitize_shallow (context))) return false;
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400549 /* Note: for structs that do not reference other structs,
550 * we do not need to call their sanitize() as we already did
551 * a bound check on the aggregate array size, hence the return.
552 */
Behdad Esfahbod3564ee52009-08-14 18:32:56 -0400553 return true;
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400554 /* We do keep this code though to make sure the structs pointed
555 * to do have a simple sanitize(), ie. they do not reference
556 * other structs. */
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400557 unsigned int count = len;
558 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400559 if (array()[i].sanitize (context))
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400560 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400561 return true;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400562 }
Behdad Esfahbod39840472010-05-05 00:23:19 -0400563 inline bool sanitize (hb_sanitize_context_t *context, void *base) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400564 TRACE_SANITIZE ();
Behdad Esfahbod39840472010-05-05 00:23:19 -0400565 if (!likely (sanitize_shallow (context))) return false;
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400566 unsigned int count = len;
567 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod39840472010-05-05 00:23:19 -0400568 if (!array()[i].sanitize (context, base))
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400569 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400570 return true;
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400571 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400572 template <typename T>
Behdad Esfahbod39840472010-05-05 00:23:19 -0400573 inline bool sanitize (hb_sanitize_context_t *context, void *base, T user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400574 TRACE_SANITIZE ();
Behdad Esfahbod39840472010-05-05 00:23:19 -0400575 if (!likely (sanitize_shallow (context))) return false;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400576 unsigned int count = len;
577 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod39840472010-05-05 00:23:19 -0400578 if (!array()[i].sanitize (context, base, user_data))
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400579 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400580 return true;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400581 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400582
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400583 private:
Behdad Esfahbod39840472010-05-05 00:23:19 -0400584 inline bool sanitize_shallow (hb_sanitize_context_t *context) {
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400585 TRACE_SANITIZE ();
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400586 return context->check_struct (this)
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400587 && context->check_array (this, Type::static_size, len);
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400588 }
589
590 public:
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400591 LenType len;
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400592 private:
593 Type arrayX[VAR];
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400594 public:
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400595 DEFINE_SIZE_VAR (sizeof (LenType), Type);
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400596};
597
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400598/* An array with a USHORT number of elements. */
599template <typename Type>
600struct ArrayOf : GenericArrayOf<USHORT, Type> {};
601
602/* An array with a ULONG number of elements. */
603template <typename Type>
604struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
605
606/* Array of Offset's */
607template <typename Type>
608struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
609
610/* Array of LongOffset's */
611template <typename Type>
612struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
613
614/* LongArray of LongOffset's */
615template <typename Type>
616struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
617
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400618/* Array of offsets relative to the beginning of the array itself. */
619template <typename Type>
620struct OffsetListOf : OffsetArrayOf<Type>
621{
622 inline const Type& operator [] (unsigned int i) const
623 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400624 if (unlikely (i >= this->len)) return Null(Type);
Behdad Esfahbod2cb08452010-04-21 22:37:31 -0400625 return this+this->array()[i];
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400626 }
627
Behdad Esfahbod39840472010-05-05 00:23:19 -0400628 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400629 TRACE_SANITIZE ();
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400630 return OffsetArrayOf<Type>::sanitize (context, this);
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400631 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400632 template <typename T>
Behdad Esfahbod39840472010-05-05 00:23:19 -0400633 inline bool sanitize (hb_sanitize_context_t *context, T user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400634 TRACE_SANITIZE ();
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400635 return OffsetArrayOf<Type>::sanitize (context, this, user_data);
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400636 }
637};
638
639
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400640/* An array with a USHORT number of elements,
641 * starting at second element. */
642template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400643struct HeadlessArrayOf
644{
Behdad Esfahbod2cb08452010-04-21 22:37:31 -0400645 const Type *array(void) const { return &StructAfter<Type> (len); }
Behdad Esfahbod2e2f43e2010-04-21 22:30:36 -0400646 Type *array(void) { return &StructAfter<Type> (len); }
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500647
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400648 inline const Type& operator [] (unsigned int i) const
649 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400650 if (unlikely (i >= len || !i)) return Null(Type);
Behdad Esfahbod2cb08452010-04-21 22:37:31 -0400651 return array()[i-1];
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400652 }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400653 inline unsigned int get_size () const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400654 { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400655
Behdad Esfahbod39840472010-05-05 00:23:19 -0400656 inline bool sanitize_shallow (hb_sanitize_context_t *context) {
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400657 return context->check_struct (this)
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400658 && context->check_array (this, Type::static_size, len);
Behdad Esfahbode5546a42010-04-22 00:45:42 -0400659 }
660
Behdad Esfahbod39840472010-05-05 00:23:19 -0400661 inline bool sanitize (hb_sanitize_context_t *context) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400662 TRACE_SANITIZE ();
Behdad Esfahbod39840472010-05-05 00:23:19 -0400663 if (!likely (sanitize_shallow (context))) return false;
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400664 /* Note: for structs that do not reference other structs,
665 * we do not need to call their sanitize() as we already did
666 * a bound check on the aggregate array size, hence the return.
667 */
Behdad Esfahbod3564ee52009-08-14 18:32:56 -0400668 return true;
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400669 /* We do keep this code though to make sure the structs pointed
670 * to do have a simple sanitize(), ie. they do not reference
671 * other structs. */
Behdad Esfahbod15164d92009-08-04 13:57:41 -0400672 unsigned int count = len ? len - 1 : 0;
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500673 Type *a = array();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400674 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod4f252fe2010-05-06 13:30:23 -0400675 if (!a[i].sanitize (context))
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400676 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400677 return true;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400678 }
679
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400680 USHORT len;
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500681/*Type array[VAR];*/
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400682};
683
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500684
Behdad Esfahbod1e914342009-11-04 18:12:09 -0500685#endif /* HB_OPEN_TYPE_PRIVATE_HH */