blob: 0f1021bbebe37b66adddd5076ccdc99e9913c36c [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 Esfahbodacdba3f2010-07-23 15:11:18 -040034HB_BEGIN_DECLS
35HB_END_DECLS
Behdad Esfahboda16ecbf2008-01-23 17:01:55 -050036
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040037
Behdad Esfahbod196598b2009-08-04 11:04:32 -040038/*
39 * Casts
40 */
41
Behdad Esfahbod187454c2010-04-23 16:35:01 -040042/* Cast to struct T, reference to reference */
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040043template<typename Type, typename TObject>
Behdad Esfahbod187454c2010-04-23 16:35:01 -040044inline const Type& CastR(const TObject &X)
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040045{ return reinterpret_cast<const Type&> (X); }
46template<typename Type, typename TObject>
Behdad Esfahbod187454c2010-04-23 16:35:01 -040047inline Type& CastR(TObject &X)
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040048{ return reinterpret_cast<Type&> (X); }
Behdad Esfahbod196598b2009-08-04 11:04:32 -040049
Behdad Esfahbod187454c2010-04-23 16:35:01 -040050/* Cast to struct T, pointer to pointer */
51template<typename Type, typename TObject>
52inline const Type* CastP(const TObject *X)
53{ return reinterpret_cast<const Type*> (X); }
54template<typename Type, typename TObject>
55inline Type* CastP(TObject *X)
56{ return reinterpret_cast<Type*> (X); }
57
Behdad Esfahbod09766b12010-05-10 17:36:03 -040058/* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
59 * location pointed to by P plus Ofs bytes. */
60template<typename Type>
61inline const Type& StructAtOffset(const void *P, unsigned int offset)
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -040062{ return * reinterpret_cast<const Type*> ((const char *) P + offset); }
Behdad Esfahbod09766b12010-05-10 17:36:03 -040063template<typename Type>
64inline Type& StructAtOffset(void *P, unsigned int offset)
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -040065{ return * reinterpret_cast<Type*> ((char *) P + offset); }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -040066
Behdad Esfahbod2e2f43e2010-04-21 22:30:36 -040067/* StructAfter<T>(X) returns the struct T& that is placed after X.
Behdad Esfahbod29c3f5e2010-04-21 23:01:00 -040068 * Works with X of variable size also. X must implement get_size() */
Behdad Esfahbode961c862010-04-21 15:56:11 -040069template<typename Type, typename TObject>
70inline const Type& StructAfter(const TObject &X)
Behdad Esfahbod09766b12010-05-10 17:36:03 -040071{ return StructAtOffset<Type>(&X, X.get_size()); }
Behdad Esfahbode961c862010-04-21 15:56:11 -040072template<typename Type, typename TObject>
73inline Type& StructAfter(TObject &X)
Behdad Esfahbod09766b12010-05-10 17:36:03 -040074{ return StructAtOffset<Type>(&X, X.get_size()); }
Behdad Esfahboda3263aa2010-04-22 18:29:09 -040075
Behdad Esfahbode961c862010-04-21 15:56:11 -040076
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -050077
Behdad Esfahbod70de50c2009-08-04 00:58:28 -040078/*
Behdad Esfahbode45d3f82010-05-06 19:33:31 -040079 * Size checking
80 */
81
Behdad Esfahbodf6796352010-05-13 13:34:17 -040082/* Check _assertion in a method environment */
Behdad Esfahbod596e4712010-05-10 18:47:48 -040083#define _DEFINE_SIZE_ASSERTION(_assertion) \
Behdad Esfahbod33afa4e2010-05-10 18:35:02 -040084 inline void _size_assertion (void) const \
Behdad Esfahbod596e4712010-05-10 18:47:48 -040085 { ASSERT_STATIC (_assertion); }
Behdad Esfahbodf6796352010-05-13 13:34:17 -040086/* Check that _code compiles in a method environment */
87#define _DEFINE_COMPILES_ASSERTION(_code) \
88 inline void _compiles_assertion (void) const \
89 { _code; }
Behdad Esfahbod0abcc3b2010-05-10 17:04:20 -040090
91
Behdad Esfahbode45d3f82010-05-06 19:33:31 -040092#define DEFINE_SIZE_STATIC(size) \
Behdad Esfahbod596e4712010-05-10 18:47:48 -040093 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size)); \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -040094 static const unsigned int static_size = (size); \
95 static const unsigned int min_size = (size)
96
Behdad Esfahbodb3651232010-05-10 16:57:29 -040097/* Size signifying variable-sized array */
98#define VAR 1
Behdad Esfahbodb3651232010-05-10 16:57:29 -040099
Behdad Esfahbod596e4712010-05-10 18:47:48 -0400100#define DEFINE_SIZE_UNION(size, _member) \
101 _DEFINE_SIZE_ASSERTION (this->u._member.static_size == (size)); \
102 static const unsigned int min_size = (size)
103
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400104#define DEFINE_SIZE_MIN(size) \
Behdad Esfahbod596e4712010-05-10 18:47:48 -0400105 _DEFINE_SIZE_ASSERTION (sizeof (*this) >= (size)); \
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400106 static const unsigned int min_size = (size)
107
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400108#define DEFINE_SIZE_ARRAY(size, array) \
Behdad Esfahbodf6796352010-05-13 13:34:17 -0400109 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \
110 _DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400111 static const unsigned int min_size = (size)
112
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400113#define DEFINE_SIZE_ARRAY2(size, array1, array2) \
Behdad Esfahbodf6796352010-05-13 13:34:17 -0400114 _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \
115 _DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].static_size) \
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400116 static const unsigned int min_size = (size)
117
118
119
120/*
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400121 * Null objects
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400122 */
123
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400124/* Global nul-content Null pool. Enlarge as necessary. */
Behdad Esfahbodcf5585c2010-05-19 12:03:35 -0400125static const void *_NullPool[64 / sizeof (void *)];
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400126
Behdad Esfahbodd2c2ca82010-05-10 19:58:25 -0400127/* Generic nul-content Null objects. */
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400128template <typename Type>
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400129static inline const Type& Null (void) {
Behdad Esfahboded074222010-05-10 18:08:46 -0400130 ASSERT_STATIC (Type::min_size <= sizeof (_NullPool));
Behdad Esfahbod187454c2010-04-23 16:35:01 -0400131 return *CastP<Type> (_NullPool);
Behdad Esfahbod9d367782010-04-21 00:32:47 -0400132}
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400133
134/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
Behdad Esfahbod65f46b02010-05-06 19:35:19 -0400135#define DEFINE_NULL_DATA(Type, data) \
136static const char _Null##Type[Type::min_size + 1] = data; /* +1 is for nul-termination in data */ \
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400137template <> \
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400138inline const Type& Null<Type> (void) { \
Behdad Esfahbod187454c2010-04-23 16:35:01 -0400139 return *CastP<Type> (_Null##Type); \
Behdad Esfahbod565c80b2010-04-22 10:26:35 -0400140} /* The following line really exists such that we end in a place needing semicolon */ \
Behdad Esfahbodbea34c72010-05-10 17:28:16 -0400141ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type))
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400142
143/* Accessor macro. */
Behdad Esfahbod9d367782010-04-21 00:32:47 -0400144#define Null(Type) Null<Type>()
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400145
146
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400147/*
Behdad Esfahboddfc8cbe2010-05-05 00:19:46 -0400148 * Trace
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400149 */
150
Behdad Esfahboddfc8cbe2010-05-05 00:19:46 -0400151
152template <int max_depth>
153struct hb_trace_t {
Behdad Esfahbod458ecbb2010-05-10 21:11:35 -0400154 explicit hb_trace_t (unsigned int *pdepth, const char *what, const char *function, const void *obj) : pdepth(pdepth) {
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400155 (void) (*pdepth < max_depth &&
156 fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, *pdepth, *pdepth, function));
Behdad Esfahbod458ecbb2010-05-10 21:11:35 -0400157 if (max_depth) ++*pdepth;
Behdad Esfahboddfc8cbe2010-05-05 00:19:46 -0400158 }
Behdad Esfahbod458ecbb2010-05-10 21:11:35 -0400159 ~hb_trace_t (void) { if (max_depth) --*pdepth; }
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400160
161 private:
Behdad Esfahboddfc8cbe2010-05-05 00:19:46 -0400162 unsigned int *pdepth;
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400163};
Behdad Esfahboddfc8cbe2010-05-05 00:19:46 -0400164template <> /* Optimize when tracing is disabled */
165struct hb_trace_t<0> {
Behdad Esfahbod75651b22010-05-10 23:44:51 -0400166 explicit hb_trace_t (unsigned int *pdepth HB_UNUSED, const char *what HB_UNUSED, const char *function HB_UNUSED, const void *obj HB_UNUSED) {}
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400167};
168
169
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400170
171/*
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400172 * Sanitize
173 */
174
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400175#ifndef HB_DEBUG_SANITIZE
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400176#define HB_DEBUG_SANITIZE (HB_DEBUG+0)
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400177#endif
178
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400179
Behdad Esfahbodbc200452010-04-29 01:40:26 -0400180#define TRACE_SANITIZE() \
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400181 hb_trace_t<HB_DEBUG_SANITIZE> trace (&c->debug_depth, "SANITIZE", HB_FUNC, this); \
Behdad Esfahbod807c5b02010-04-28 20:25:22 -0400182
183
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400184struct hb_sanitize_context_t
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400185{
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400186 inline void init (hb_blob_t *blob)
187 {
188 this->blob = hb_blob_reference (blob);
189 this->start = hb_blob_lock (blob);
190 this->end = this->start + hb_blob_get_length (blob);
191 this->writable = hb_blob_is_writable (blob);
192 this->edit_count = 0;
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400193 this->debug_depth = 0;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400194
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400195 (void) (HB_DEBUG_SANITIZE &&
Behdad Esfahbod17e9ff92010-07-15 11:21:34 -0700196 fprintf (stderr, "sanitize %p init [%p..%p] (%lu bytes)\n",
197 this->blob, this->start, this->end,
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400198 (unsigned long) (this->end - this->start)));
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400199 }
200
201 inline void finish (void)
202 {
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400203 (void) (HB_DEBUG_SANITIZE &&
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400204 fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400205 this->blob, this->start, this->end, this->edit_count));
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400206
207 hb_blob_unlock (this->blob);
208 hb_blob_destroy (this->blob);
209 this->blob = NULL;
210 this->start = this->end = NULL;
211 }
212
Behdad Esfahbod4ad2cc52010-05-06 09:24:24 -0400213 inline bool check_range (const void *base, unsigned int len) const
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400214 {
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400215 const char *p = (const char *) base;
216 bool ret = this->start <= p &&
217 p <= this->end &&
218 (unsigned int) (this->end - p) >= len;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400219
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400220 (void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE &&
221 fprintf (stderr, "SANITIZE(%p) %-*d-> range [%p..%p] (%d bytes) in [%p..%p] -> %s\n",
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400222 p,
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400223 this->debug_depth, this->debug_depth,
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400224 p, p + len, len,
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400225 this->start, this->end,
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400226 ret ? "pass" : "FAIL"));
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400227
Behdad Esfahbod27e302d2010-05-05 00:26:16 -0400228 return likely (ret);
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400229 }
230
Behdad Esfahbod1cd1e112010-05-05 20:15:14 -0400231 inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400232 {
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400233 const char *p = (const char *) base;
Behdad Esfahbod4f801bd2010-07-21 16:37:01 -0400234 bool overflows = record_size > 0 && len >= ((unsigned int) -1) / record_size;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400235
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400236 (void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE &&
237 fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n",
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400238 p,
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400239 this->debug_depth, this->debug_depth,
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400240 p, p + (record_size * len), record_size, len, (unsigned long) record_size * len,
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400241 this->start, this->end,
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400242 !overflows ? "does not overflow" : "OVERFLOWS FAIL"));
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400243
Behdad Esfahbod4ad2cc52010-05-06 09:24:24 -0400244 return likely (!overflows && this->check_range (base, record_size * len));
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400245 }
246
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400247 template <typename Type>
248 inline bool check_struct (const Type *obj) const
249 {
Behdad Esfahbod54842372010-05-10 18:13:32 -0400250 return likely (this->check_range (obj, obj->min_size));
Behdad Esfahbodb1576172010-05-06 14:48:27 -0400251 }
252
Behdad Esfahbod40cbefe2010-05-10 17:47:22 -0400253 inline bool can_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400254 {
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400255 const char *p = (const char *) base;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400256 this->edit_count++;
257
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400258 (void) (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE &&
259 fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n",
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400260 p,
Behdad Esfahbod20e3dd52010-05-04 23:21:57 -0400261 this->debug_depth, this->debug_depth,
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400262 this->edit_count,
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400263 p, p + len, len,
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400264 this->start, this->end,
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400265 this->writable ? "granted" : "REJECTED"));
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400266
267 return this->writable;
268 }
269
Behdad Esfahbod705e2152010-05-05 01:40:25 -0400270 unsigned int debug_depth;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400271 const char *start, *end;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400272 bool writable;
Behdad Esfahbod254933c2010-04-23 13:57:10 -0400273 unsigned int edit_count;
Behdad Esfahbod98daaf12010-05-04 22:42:49 -0400274 hb_blob_t *blob;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400275};
276
Behdad Esfahbod1376fb72010-04-29 02:19:21 -0400277
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400278
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400279/* Template to sanitize an object. */
280template <typename Type>
281struct Sanitizer
282{
283 static hb_blob_t *sanitize (hb_blob_t *blob) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400284 hb_sanitize_context_t c[1] = {{0}};
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400285 bool sane;
286
Behdad Esfahbodd0b65732009-08-06 18:34:47 -0400287 /* TODO is_sane() stuff */
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400288
289 retry:
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400290 (void) (HB_DEBUG_SANITIZE &&
291 fprintf (stderr, "Sanitizer %p start %s\n", blob, HB_FUNC));
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400292
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400293 c->init (blob);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400294
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400295 if (unlikely (!c->start)) {
296 c->finish ();
Behdad Esfahbod48146e52010-05-10 20:07:56 -0400297 return blob;
298 }
299
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400300 Type *t = CastP<Type> (const_cast<char *> (c->start));
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400301
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400302 sane = t->sanitize (c);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400303 if (sane) {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400304 if (c->edit_count) {
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400305 (void) (HB_DEBUG_SANITIZE &&
Behdad Esfahbodfa030172010-04-29 13:48:26 -0400306 fprintf (stderr, "Sanitizer %p passed first round with %d edits; doing a second round %s\n",
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400307 blob, c->edit_count, HB_FUNC));
Behdad Esfahbodfa030172010-04-29 13:48:26 -0400308
Behdad Esfahbod8b534612009-08-19 18:16:50 -0400309 /* sanitize again to ensure no toe-stepping */
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400310 c->edit_count = 0;
311 sane = t->sanitize (c);
312 if (c->edit_count) {
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400313 (void) (HB_DEBUG_SANITIZE &&
Behdad Esfahbodfa030172010-04-29 13:48:26 -0400314 fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400315 blob, c->edit_count, HB_FUNC));
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400316 sane = false;
317 }
318 }
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400319 c->finish ();
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400320 } else {
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400321 unsigned int edit_count = c->edit_count;
322 c->finish ();
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400323 if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
324 /* ok, we made it writable by relocating. try again */
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400325 (void) (HB_DEBUG_SANITIZE &&
326 fprintf (stderr, "Sanitizer %p retry %s\n", blob, HB_FUNC));
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400327 goto retry;
328 }
329 }
330
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400331 (void) (HB_DEBUG_SANITIZE &&
332 fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", HB_FUNC));
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400333 if (sane)
334 return blob;
335 else {
336 hb_blob_destroy (blob);
337 return hb_blob_create_empty ();
338 }
339 }
Behdad Esfahbodb435ab72010-05-10 19:51:57 -0400340
341 static const Type* lock_instance (hb_blob_t *blob) {
342 const char *base = hb_blob_lock (blob);
343 return unlikely (!base) ? &Null(Type) : CastP<Type> (base);
344 }
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400345};
346
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400347
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400348
349
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500350/*
351 *
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400352 * The OpenType Font File: Data Types
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500353 */
354
355
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500356/* "The following data types are used in the OpenType font file.
357 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500358
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400359/*
360 * Int types
361 */
362
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400363
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400364template <typename Type, int Bytes> class BEInt;
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500365
Behdad Esfahbodf1aaa2a2010-04-23 15:19:50 -0400366/* LONGTERMTODO: On machines allowing unaligned access, we can make the
367 * following tighter by using byteswap instructions on ints directly. */
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400368template <typename Type>
369class BEInt<Type, 2>
370{
371 public:
Behdad Esfahbod81408cd2010-07-23 14:46:57 -0400372 inline void set (Type i) { hb_be_uint16_put (v,i); }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400373 inline operator Type (void) const { return hb_be_uint16_get (v); }
Behdad Esfahbod01c01612010-04-21 22:49:56 -0400374 inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_cmp (v, o.v); }
375 inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400376 private: uint8_t v[2];
377};
378template <typename Type>
379class BEInt<Type, 4>
380{
381 public:
Behdad Esfahbod81408cd2010-07-23 14:46:57 -0400382 inline void set (Type i) { hb_be_uint32_put (v,i); }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400383 inline operator Type (void) const { return hb_be_uint32_get (v); }
Behdad Esfahbod01c01612010-04-21 22:49:56 -0400384 inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_cmp (v, o.v); }
385 inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400386 private: uint8_t v[4];
387};
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500388
Behdad Esfahbod2467c662010-04-21 23:11:45 -0400389/* Integer types in big-endian order and no alignment requirement */
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400390template <typename Type>
391struct IntType
392{
Behdad Esfahbod81408cd2010-07-23 14:46:57 -0400393 inline void set (Type i) { v.set (i); }
Behdad Esfahbod01c01612010-04-21 22:49:56 -0400394 inline operator Type(void) const { return v; }
395 inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
396 inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
Behdad Esfahbod4e573712010-09-28 16:23:58 -0400397 inline int cmp (Type a) const { Type b = v; return a < b ? -1 : a == b ? 0 : +1; }
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400398 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400399 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400400 return likely (c->check_struct (this));
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400401 }
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400402 protected:
Behdad Esfahbod569da922010-05-10 16:38:32 -0400403 BEInt<Type, sizeof (Type)> v;
404 public:
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400405 DEFINE_SIZE_STATIC (sizeof (Type));
Behdad Esfahbode032ed92010-04-21 03:11:46 -0400406};
407
408typedef IntType<uint16_t> USHORT; /* 16-bit unsigned integer. */
409typedef IntType<int16_t> SHORT; /* 16-bit signed integer. */
410typedef IntType<uint32_t> ULONG; /* 32-bit unsigned integer. */
411typedef IntType<int32_t> LONG; /* 32-bit signed integer. */
412
Behdad Esfahbode29caf32010-05-19 11:47:17 -0400413/* Date represented in number of seconds since 12:00 midnight, January 1,
414 * 1904. The value is represented as a signed 64-bit integer. */
415struct LONGDATETIME
416{
417 inline bool sanitize (hb_sanitize_context_t *c) {
418 TRACE_SANITIZE ();
419 return likely (c->check_struct (this));
420 }
421 private:
422 LONG major;
423 ULONG minor;
424 public:
425 DEFINE_SIZE_STATIC (8);
426};
427
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500428/* Array of four uint8s (length = 32 bits) used to identify a script, language
429 * system, feature, or baseline */
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400430struct Tag : ULONG
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400431{
Behdad Esfahbodbefc0222006-12-25 09:14:52 -0500432 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
Behdad Esfahboda82ef7a2010-05-10 17:55:03 -0400433 inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); }
434 inline operator char* (void) { return reinterpret_cast<char *> (&this->v); }
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400435 public:
436 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500437};
Behdad Esfahbod65f46b02010-05-06 19:35:19 -0400438DEFINE_NULL_DATA (Tag, " ");
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500439
440/* Glyph index number, same as uint16 (length = 16 bits) */
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400441typedef USHORT GlyphID;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500442
Behdad Esfahbodb5db4f12010-05-10 22:22:22 -0400443/* Script/language-system/feature index */
444struct Index : USHORT {
445 static const unsigned int NOT_FOUND_INDEX = 0xFFFF;
446};
447DEFINE_NULL_DATA (Index, "\xff\xff");
448
Behdad Esfahbod1f437e62008-01-23 04:36:40 -0500449/* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400450typedef USHORT Offset;
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400451
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400452/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
453typedef ULONG LongOffset;
454
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500455
456/* CheckSum */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400457struct CheckSum : ULONG
458{
459 static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
460 {
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500461 uint32_t Sum = 0L;
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400462 ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500463
464 while (Table < EndPtr)
465 Sum += *Table++;
466 return Sum;
467 }
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400468 public:
469 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500470};
471
472
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500473/*
474 * Version Numbers
475 */
476
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400477struct FixedVersion
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400478{
Behdad Esfahbod09c292e2009-05-26 19:48:16 -0400479 inline operator uint32_t (void) const { return (major << 16) + minor; }
Behdad Esfahbod96908b82009-05-24 12:30:40 -0400480
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400481 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400482 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400483 return c->check_struct (this);
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400484 }
485
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400486 USHORT major;
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400487 USHORT minor;
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400488 public:
489 DEFINE_SIZE_STATIC (4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500490};
491
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400492
493
494/*
495 * Template subclasses of Offset and LongOffset that do the dereferencing.
Behdad Esfahbodf0abcd62010-05-02 18:14:25 -0400496 * Use: (base+offset)
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400497 */
498
499template <typename OffsetType, typename Type>
500struct GenericOffsetTo : OffsetType
501{
Behdad Esfahbod00e23fc2010-04-20 23:50:45 -0400502 inline const Type& operator () (const void *base) const
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400503 {
504 unsigned int offset = *this;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400505 if (unlikely (!offset)) return Null(Type);
Behdad Esfahbod09766b12010-05-10 17:36:03 -0400506 return StructAtOffset<Type> (base, offset);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400507 }
508
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400509 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400510 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400511 if (unlikely (!c->check_struct (this))) return false;
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400512 unsigned int offset = *this;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400513 if (unlikely (!offset)) return true;
Behdad Esfahbod09766b12010-05-10 17:36:03 -0400514 Type &obj = StructAtOffset<Type> (base, offset);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400515 return likely (obj.sanitize (c)) || neuter (c);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400516 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400517 template <typename T>
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400518 inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400519 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400520 if (unlikely (!c->check_struct (this))) return false;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400521 unsigned int offset = *this;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400522 if (unlikely (!offset)) return true;
Behdad Esfahbod09766b12010-05-10 17:36:03 -0400523 Type &obj = StructAtOffset<Type> (base, offset);
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400524 return likely (obj.sanitize (c, user_data)) || neuter (c);
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400525 }
526
527 private:
528 /* Set the offset to Null */
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400529 inline bool neuter (hb_sanitize_context_t *c) {
530 if (c->can_edit (this, this->static_size)) {
Behdad Esfahbodc9f14682010-05-04 14:38:08 -0400531 this->set (0); /* 0 is Null offset */
532 return true;
533 }
534 return false;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400535 }
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400536};
537template <typename Base, typename OffsetType, typename Type>
538inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
539
540template <typename Type>
541struct OffsetTo : GenericOffsetTo<Offset, Type> {};
542
543template <typename Type>
544struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400545
546
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400547/*
548 * Array Types
549 */
550
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400551template <typename LenType, typename Type>
552struct GenericArrayOf
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400553{
Behdad Esfahbod4f5f1c32010-04-22 00:27:39 -0400554 const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500555 {
556 unsigned int count = len;
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400557 if (unlikely (start_offset > count))
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500558 count = 0;
559 else
560 count -= start_offset;
561 count = MIN (count, *pcount);
562 *pcount = count;
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400563 return array + start_offset;
Behdad Esfahbod48de3732009-11-04 16:59:50 -0500564 }
565
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400566 inline const Type& operator [] (unsigned int i) const
567 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400568 if (unlikely (i >= len)) return Null(Type);
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400569 return array[i];
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400570 }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400571 inline unsigned int get_size (void) const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400572 { return len.static_size + len * Type::static_size; }
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400573
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400574 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400575 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400576 if (unlikely (!sanitize_shallow (c))) return false;
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400577
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400578 /* Note: for structs that do not reference other structs,
579 * we do not need to call their sanitize() as we already did
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400580 * a bound check on the aggregate array size. We just include
581 * a small unreachable expression to make sure the structs
582 * pointed to do have a simple sanitize(), ie. they do not
583 * reference other structs via offsets.
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400584 */
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400585 (void) (false && array[0].sanitize (c));
586
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400587 return true;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400588 }
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400589 inline bool sanitize (hb_sanitize_context_t *c, void *base) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400590 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400591 if (unlikely (!sanitize_shallow (c))) return false;
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400592 unsigned int count = len;
593 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400594 if (unlikely (!array[i].sanitize (c, base)))
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400595 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400596 return true;
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400597 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400598 template <typename T>
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400599 inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400600 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400601 if (unlikely (!sanitize_shallow (c))) return false;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400602 unsigned int count = len;
603 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400604 if (unlikely (!array[i].sanitize (c, base, user_data)))
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400605 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400606 return true;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400607 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400608
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400609 private:
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400610 inline bool sanitize_shallow (hb_sanitize_context_t *c) {
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400611 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400612 return c->check_struct (this)
613 && c->check_array (this, Type::static_size, len);
Behdad Esfahbod30fa2822010-05-04 14:28:18 -0400614 }
615
616 public:
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400617 LenType len;
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400618 Type array[VAR];
Behdad Esfahbodb3651232010-05-10 16:57:29 -0400619 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400620 DEFINE_SIZE_ARRAY (sizeof (LenType), array);
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400621};
622
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400623/* An array with a USHORT number of elements. */
624template <typename Type>
625struct ArrayOf : GenericArrayOf<USHORT, Type> {};
626
627/* An array with a ULONG number of elements. */
628template <typename Type>
629struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
630
631/* Array of Offset's */
632template <typename Type>
633struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
634
635/* Array of LongOffset's */
636template <typename Type>
637struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
638
639/* LongArray of LongOffset's */
640template <typename Type>
641struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
642
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400643/* Array of offsets relative to the beginning of the array itself. */
644template <typename Type>
645struct OffsetListOf : OffsetArrayOf<Type>
646{
647 inline const Type& operator [] (unsigned int i) const
648 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400649 if (unlikely (i >= this->len)) return Null(Type);
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400650 return this+this->array[i];
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400651 }
652
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400653 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400654 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400655 return OffsetArrayOf<Type>::sanitize (c, this);
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400656 }
Behdad Esfahbod4a446ac2010-05-04 22:46:21 -0400657 template <typename T>
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400658 inline bool sanitize (hb_sanitize_context_t *c, T user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400659 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400660 return OffsetArrayOf<Type>::sanitize (c, this, user_data);
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400661 }
662};
663
664
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400665/* An array with a USHORT number of elements,
666 * starting at second element. */
667template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400668struct HeadlessArrayOf
669{
670 inline const Type& operator [] (unsigned int i) const
671 {
Behdad Esfahbod64d3fc82010-05-03 22:51:19 -0400672 if (unlikely (i >= len || !i)) return Null(Type);
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400673 return array[i-1];
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400674 }
Behdad Esfahbod7f97d2c2010-10-01 18:58:50 -0400675 inline unsigned int get_size (void) const
Behdad Esfahbode45d3f82010-05-06 19:33:31 -0400676 { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400677
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400678 inline bool sanitize_shallow (hb_sanitize_context_t *c) {
679 return c->check_struct (this)
680 && c->check_array (this, Type::static_size, len);
Behdad Esfahbode5546a42010-04-22 00:45:42 -0400681 }
682
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400683 inline bool sanitize (hb_sanitize_context_t *c) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400684 TRACE_SANITIZE ();
Behdad Esfahbodd7cfb3b2010-05-13 14:18:49 -0400685 if (unlikely (!sanitize_shallow (c))) return false;
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400686
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400687 /* Note: for structs that do not reference other structs,
688 * we do not need to call their sanitize() as we already did
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400689 * a bound check on the aggregate array size. We just include
690 * a small unreachable expression to make sure the structs
691 * pointed to do have a simple sanitize(), ie. they do not
692 * reference other structs via offsets.
Behdad Esfahbod40d73bc2010-04-21 00:49:40 -0400693 */
Behdad Esfahbod11e3ec42010-11-03 15:11:04 -0400694 (void) (false && array[0].sanitize (c));
695
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400696 return true;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400697 }
698
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400699 USHORT len;
Behdad Esfahbodb9615182010-05-10 18:20:54 -0400700 Type array[VAR];
Behdad Esfahboded074222010-05-10 18:08:46 -0400701 public:
Behdad Esfahbod0eb9fc62010-05-10 19:01:17 -0400702 DEFINE_SIZE_ARRAY (sizeof (USHORT), array);
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400703};
704
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500705
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -0400706/* An array with sorted elements. Supports binary searching. */
707template <typename Type>
708struct SortedArrayOf : ArrayOf<Type> {
709
710 template <typename SearchType>
711 inline int search (const SearchType &x) const {
712 class Cmp {
Behdad Esfahbod8f08c322010-10-08 19:43:48 -0400713 public: static int cmp (const SearchType *a, const Type *b) { return b->cmp (*a); }
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -0400714 };
Behdad Esfahbod8f08c322010-10-08 19:43:48 -0400715 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 -0400716 return p ? p - this->array : -1;
717 }
Behdad Esfahbodcc8a4ab2010-07-08 00:40:04 -0400718};
719
720
Behdad Esfahbodacdba3f2010-07-23 15:11:18 -0400721HB_BEGIN_DECLS
722HB_END_DECLS
723
Behdad Esfahbod1e914342009-11-04 18:12:09 -0500724#endif /* HB_OPEN_TYPE_PRIVATE_HH */