blob: 9530c289b58619c95440ba1d823dd23bf1b309c4 [file] [log] [blame]
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05001/*
Behdad Esfahbodee58aae2009-05-17 05:14:33 -04002 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
Behdad Esfahbod64aef3a2008-01-23 16:14:38 -05003 *
4 * This is part of HarfBuzz, an OpenType Layout engine library.
5 *
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 Esfahbod706ab252008-01-28 05:58:50 -050035#define NO_INDEX ((unsigned int) 0xFFFF)
Behdad Esfahbod5a0b7912009-04-16 04:45:30 -040036
Behdad Esfahbod706ab252008-01-28 05:58:50 -050037
Behdad Esfahbod196598b2009-08-04 11:04:32 -040038/*
39 * Casts
40 */
41
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -040042#define CONST_CHARP(X) (reinterpret_cast<const char *>(X))
43#define DECONST_CHARP(X) ((char *)reinterpret_cast<const char *>(X))
44#define CHARP(X) (reinterpret_cast<char *>(X))
45
46#define CONST_CAST(T,X,Ofs) (*(reinterpret_cast<const T *>(CONST_CHARP(&(X)) + Ofs)))
47#define DECONST_CAST(T,X,Ofs) (*(reinterpret_cast<T *>((char *)CONST_CHARP(&(X)) + Ofs)))
48#define CAST(T,X,Ofs) (*(reinterpret_cast<T *>(CHARP(&(X)) + Ofs)))
Behdad Esfahbod196598b2009-08-04 11:04:32 -040049
Behdad Esfahbod284899c2009-08-09 22:10:39 -040050#define CONST_NEXT(T,X) (*(reinterpret_cast<const T *>(CONST_CHARP(&(X)) + (X).get_size ())))
51#define NEXT(T,X) (*(reinterpret_cast<T *>(CHARP(&(X)) + (X).get_size ())))
Behdad Esfahbod70de50c2009-08-04 00:58:28 -040052
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -050053#define CONST_ARRAY_AFTER(T,X) ((reinterpret_cast<const T *>(CONST_CHARP(&(X)) + sizeof (X))))
54#define ARRAY_AFTER(T,X) ((reinterpret_cast<T *>(CHARP(&(X)) + sizeof (X))))
55
Behdad Esfahbod70de50c2009-08-04 00:58:28 -040056/*
Behdad Esfahbod577c1112009-08-04 19:31:02 -040057 * Class features
58 */
59
60
61/* Null objects */
62
63/* Global nul-content Null pool. Enlarge as necessary. */
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -050064static const char NullPool[32] = "";
Behdad Esfahbod577c1112009-08-04 19:31:02 -040065
66/* Generic template for nul-content sizeof-sized Null objects. */
67template <typename Type>
68struct Null
69{
70 ASSERT_STATIC (sizeof (Type) <= sizeof (NullPool));
Behdad Esfahbod8b534612009-08-19 18:16:50 -040071 static inline const Type &get () { return CONST_CAST (Type, *NullPool, 0); }
Behdad Esfahbod577c1112009-08-04 19:31:02 -040072};
73
74/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
75#define DEFINE_NULL_DATA(Type, size, data) \
76static const char _Null##Type[size] = data; \
77template <> \
78struct Null <Type> \
79{ \
Behdad Esfahbod8b534612009-08-19 18:16:50 -040080 static inline const Type &get () { return CONST_CAST (Type, *_Null##Type, 0); } \
Behdad Esfahbod577c1112009-08-04 19:31:02 -040081}
82
83/* Accessor macro. */
84#define Null(Type) (Null<Type>::get())
85
86
87#define ASSERT_SIZE_DATA(Type, size, data) \
88 ASSERT_SIZE (Type, size); \
89 DEFINE_NULL_DATA (Type, size, data)
90
91/* get_for_data() is a static class method returning a reference to an
92 * instance of Type located at the input data location. It's just a
93 * fancy, NULL-safe, cast! */
94#define STATIC_DEFINE_GET_FOR_DATA(Type) \
95 static inline const Type& get_for_data (const char *data) \
96 { \
97 if (HB_UNLIKELY (data == NULL)) return Null(Type); \
Behdad Esfahbod8b534612009-08-19 18:16:50 -040098 return CONST_CAST (Type, *data, 0); \
Behdad Esfahbod577c1112009-08-04 19:31:02 -040099 }
100/* Like get_for_data(), but checks major version first. */
101#define STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION(Type, MajorMin, MajorMax) \
102 static inline const Type& get_for_data (const char *data) \
103 { \
104 if (HB_UNLIKELY (data == NULL)) return Null(Type); \
Behdad Esfahbod8b534612009-08-19 18:16:50 -0400105 const Type& t = CONST_CAST (Type, *data, 0); \
Behdad Esfahbod577c1112009-08-04 19:31:02 -0400106 if (HB_UNLIKELY (t.version.major < MajorMin || t.version.major > MajorMax)) return Null(Type); \
107 return t; \
108 }
109
110
111/*
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400112 * Sanitize
113 */
114
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400115#ifndef HB_DEBUG_SANITIZE
116#define HB_DEBUG_SANITIZE HB_DEBUG
117#endif
118
119#if HB_DEBUG_SANITIZE
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400120#define TRACE_SANITIZE_ARG_DEF , unsigned int sanitize_depth
121#define TRACE_SANITIZE_ARG , sanitize_depth + 1
122#define TRACE_SANITIZE_ARG_INIT , 1
123#define TRACE_SANITIZE() \
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400124 HB_STMT_START { \
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400125 if (sanitize_depth < HB_DEBUG_SANITIZE) \
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400126 fprintf (stderr, "SANITIZE(%p) %-*d-> %s\n", \
127 (CONST_CHARP (this) == NullPool) ? 0 : this, \
Behdad Esfahbod9b76a292009-08-06 10:27:38 -0400128 sanitize_depth, sanitize_depth, \
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400129 __PRETTY_FUNCTION__); \
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400130 } HB_STMT_END
131#else
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400132#define TRACE_SANITIZE_ARG_DEF
133#define TRACE_SANITIZE_ARG
134#define TRACE_SANITIZE_ARG_INIT
135#define TRACE_SANITIZE() HB_STMT_START {} HB_STMT_END
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400136#endif
137
Behdad Esfahbod41895502009-08-14 16:25:33 -0400138#define SANITIZE_ARG_DEF \
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400139 hb_sanitize_context_t *context TRACE_SANITIZE_ARG_DEF
Behdad Esfahbod41895502009-08-14 16:25:33 -0400140#define SANITIZE_ARG \
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400141 context TRACE_SANITIZE_ARG
Behdad Esfahbod41895502009-08-14 16:25:33 -0400142#define SANITIZE_ARG_INIT \
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400143 &context TRACE_SANITIZE_ARG_INIT
Behdad Esfahbod41895502009-08-14 16:25:33 -0400144
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400145typedef struct _hb_sanitize_context_t hb_sanitize_context_t;
146struct _hb_sanitize_context_t
147{
148 const char *start, *end;
Behdad Esfahbodb1e187f2009-08-04 15:28:49 -0400149 int edit_count;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400150 hb_blob_t *blob;
151};
152
Behdad Esfahbodb1e187f2009-08-04 15:28:49 -0400153static HB_GNUC_UNUSED void
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400154_hb_sanitize_init (hb_sanitize_context_t *context,
155 hb_blob_t *blob)
Behdad Esfahbodb1e187f2009-08-04 15:28:49 -0400156{
157 context->blob = blob;
158 context->start = hb_blob_lock (blob);
159 context->end = context->start + hb_blob_get_length (blob);
160 context->edit_count = 0;
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400161
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400162#if HB_DEBUG_SANITIZE
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400163 fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
Behdad Esfahbod7f96b392009-08-08 16:37:22 -0400164 context->blob, context->start, context->end, context->end - context->start);
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400165#endif
Behdad Esfahbodb1e187f2009-08-04 15:28:49 -0400166}
167
168static HB_GNUC_UNUSED void
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400169_hb_sanitize_fini (hb_sanitize_context_t *context,
170 bool unlock)
Behdad Esfahbodb1e187f2009-08-04 15:28:49 -0400171{
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400172#if HB_DEBUG_SANITIZE
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400173 fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
174 context->blob, context->start, context->end, context->edit_count);
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400175#endif
176
Behdad Esfahbodb1e187f2009-08-04 15:28:49 -0400177 if (unlock)
178 hb_blob_unlock (context->blob);
179}
180
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400181static HB_GNUC_UNUSED inline bool
Behdad Esfahbod41895502009-08-14 16:25:33 -0400182_hb_sanitize_check (SANITIZE_ARG_DEF,
183 const char *base,
184 unsigned int len)
185{
Behdad Esfahbodae728e52009-08-14 16:41:00 -0400186 bool ret = context->start <= base &&
187 base <= context->end &&
188 (unsigned int) (context->end - base) >= len;
189
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400190#if HB_DEBUG_SANITIZE
191 if (sanitize_depth < HB_DEBUG_SANITIZE) \
Behdad Esfahbodae728e52009-08-14 16:41:00 -0400192 fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
193 base,
194 sanitize_depth, sanitize_depth,
195 base, base+len, len,
196 context->start, context->end,
197 ret ? "pass" : "FAIL");
198#endif
199 return ret;
Behdad Esfahbod41895502009-08-14 16:25:33 -0400200}
201
202static HB_GNUC_UNUSED inline bool
Behdad Esfahbod815a73e2009-08-14 17:31:16 -0400203_hb_sanitize_array (SANITIZE_ARG_DEF,
204 const char *base,
205 unsigned int record_size,
206 unsigned int len)
207{
208 bool overflows = len >= ((unsigned int) -1) / record_size;
209
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400210#if HB_DEBUG_SANITIZE
211 if (sanitize_depth < HB_DEBUG_SANITIZE) \
Behdad Esfahbod815a73e2009-08-14 17:31:16 -0400212 fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
213 base,
214 sanitize_depth, sanitize_depth,
215 base, base + (record_size * len), record_size, len, (unsigned long) record_size * len,
216 context->start, context->end,
217 !overflows ? "does not overflow" : "OVERFLOWS FAIL");
218#endif
219 return HB_LIKELY (!overflows) && _hb_sanitize_check (SANITIZE_ARG, base, record_size * len);
220}
221
222static HB_GNUC_UNUSED inline bool
Behdad Esfahbod41895502009-08-14 16:25:33 -0400223_hb_sanitize_edit (SANITIZE_ARG_DEF,
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400224 const char *base HB_GNUC_UNUSED,
225 unsigned int len HB_GNUC_UNUSED)
Behdad Esfahbodb1e187f2009-08-04 15:28:49 -0400226{
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400227 bool perm = hb_blob_try_writable_inplace (context->blob);
Behdad Esfahbodf4b58d32009-08-04 21:47:29 -0400228 context->edit_count++;
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400229
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400230#if HB_DEBUG_SANITIZE
Behdad Esfahbodae728e52009-08-14 16:41:00 -0400231 fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
232 base,
233 sanitize_depth, sanitize_depth,
234 context->edit_count,
235 base, base+len, len,
236 context->start, context->end,
237 perm ? "granted" : "REJECTED");
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400238#endif
239 return perm;
Behdad Esfahbodb1e187f2009-08-04 15:28:49 -0400240}
241
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400242#define SANITIZE(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG))
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400243#define SANITIZE2(X,Y) (SANITIZE (X) && SANITIZE (Y))
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400244
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400245#define SANITIZE_THIS(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG, CONST_CHARP(this)))
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400246#define SANITIZE_THIS2(X,Y) (SANITIZE_THIS (X) && SANITIZE_THIS (Y))
247#define SANITIZE_THIS3(X,Y,Z) (SANITIZE_THIS (X) && SANITIZE_THIS (Y) && SANITIZE_THIS(Z))
248
249#define SANITIZE_BASE(X,B) HB_LIKELY ((X).sanitize (SANITIZE_ARG, B))
250#define SANITIZE_BASE2(X,Y,B) (SANITIZE_BASE (X,B) && SANITIZE_BASE (Y,B))
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400251
252#define SANITIZE_SELF() SANITIZE_OBJ (*this)
253#define SANITIZE_OBJ(X) SANITIZE_MEM(&(X), sizeof (X))
Behdad Esfahboddc9c4d92009-08-04 12:26:26 -0400254#define SANITIZE_GET_SIZE() SANITIZE_SELF() && SANITIZE_MEM (this, this->get_size ())
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400255
Behdad Esfahbodd0b65732009-08-06 18:34:47 -0400256/* TODO Optimize this if L is fixed (gcc magic) */
Behdad Esfahbod41895502009-08-14 16:25:33 -0400257#define SANITIZE_MEM(B,L) HB_LIKELY (_hb_sanitize_check (SANITIZE_ARG, CONST_CHARP(B), (L)))
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400258
Behdad Esfahbod815a73e2009-08-14 17:31:16 -0400259#define SANITIZE_ARRAY(A,S,L) HB_LIKELY (_hb_sanitize_array (SANITIZE_ARG, CONST_CHARP(A), S, L))
260
Behdad Esfahbodd0b65732009-08-06 18:34:47 -0400261#define NEUTER(Var, Val) \
262 (SANITIZE_OBJ (Var) && \
Behdad Esfahbod41895502009-08-14 16:25:33 -0400263 _hb_sanitize_edit (SANITIZE_ARG, CONST_CHARP(&(Var)), sizeof (Var)) && \
Behdad Esfahbodd0b65732009-08-06 18:34:47 -0400264 ((Var) = (Val), true))
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400265
266
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400267/* Template to sanitize an object. */
268template <typename Type>
269struct Sanitizer
270{
271 static hb_blob_t *sanitize (hb_blob_t *blob) {
272 hb_sanitize_context_t context;
273 bool sane;
274
Behdad Esfahbodd0b65732009-08-06 18:34:47 -0400275 /* TODO is_sane() stuff */
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400276
277 retry:
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400278#if HB_DEBUG_SANITIZE
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400279 fprintf (stderr, "Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__);
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400280#endif
281
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400282 _hb_sanitize_init (&context, blob);
283
Behdad Esfahbod8cd6fa22009-08-04 22:55:44 -0400284 Type *t = &CAST (Type, *DECONST_CHARP(context.start), 0);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400285
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400286 sane = t->sanitize (SANITIZE_ARG_INIT);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400287 if (sane) {
288 if (context.edit_count) {
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400289#if HB_DEBUG_SANITIZE
Behdad Esfahbod0d77ab82009-08-05 15:27:42 -0400290 fprintf (stderr, "Sanitizer %p passed first round with %d edits; going a second round %s\n",
291 blob, context.edit_count, __PRETTY_FUNCTION__);
292#endif
Behdad Esfahbod8b534612009-08-19 18:16:50 -0400293 /* sanitize again to ensure no toe-stepping */
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400294 context.edit_count = 0;
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400295 sane = t->sanitize (SANITIZE_ARG_INIT);
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400296 if (context.edit_count) {
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400297#if HB_DEBUG_SANITIZE
Behdad Esfahbodae728e52009-08-14 16:41:00 -0400298 fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
Behdad Esfahbod0d77ab82009-08-05 15:27:42 -0400299 blob, context.edit_count, __PRETTY_FUNCTION__);
300#endif
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400301 sane = false;
302 }
303 }
304 _hb_sanitize_fini (&context, true);
305 } else {
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400306 unsigned int edit_count = context.edit_count;
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400307 _hb_sanitize_fini (&context, true);
Behdad Esfahbod977eeb72009-08-19 16:17:24 -0400308 if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
309 /* ok, we made it writable by relocating. try again */
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400310#if HB_DEBUG_SANITIZE
Behdad Esfahbod7acb3892009-08-05 15:20:34 -0400311 fprintf (stderr, "Sanitizer %p retry %s\n", blob, __PRETTY_FUNCTION__);
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400312#endif
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400313 goto retry;
314 }
315 }
316
Behdad Esfahbod95e20242009-08-28 16:31:20 -0400317#if HB_DEBUG_SANITIZE
Behdad Esfahbodae728e52009-08-14 16:41:00 -0400318 fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", __PRETTY_FUNCTION__);
Behdad Esfahbod4f3ad912009-08-04 23:01:23 -0400319#endif
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400320 if (sane)
321 return blob;
322 else {
323 hb_blob_destroy (blob);
324 return hb_blob_create_empty ();
325 }
326 }
327
Behdad Esfahbodd60bb8c2009-08-04 21:32:57 -0400328 static const Type& lock_instance (hb_blob_t *blob) {
Behdad Esfahbod4e8a0602009-08-04 20:52:47 -0400329 return Type::get_for_data (hb_blob_lock (blob));
330 }
331};
332
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400333
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500334/*
335 *
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400336 * The OpenType Font File: Data Types
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500337 */
338
339
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500340/* "The following data types are used in the OpenType font file.
341 * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500342
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400343/*
344 * Int types
345 */
346
Behdad Esfahbod9e826ea2009-08-06 18:24:55 -0400347/* TODO On machines that allow unaligned access, use this version. */
348#define _DEFINE_INT_TYPE1_UNALIGNED(NAME, TYPE, BIG_ENDIAN, BYTES) \
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400349 struct NAME \
350 { \
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400351 inline NAME& operator = (TYPE i) { (TYPE&) v = BIG_ENDIAN (i); return *this; } \
352 inline operator TYPE(void) const { return BIG_ENDIAN ((TYPE&) v); } \
353 inline bool operator== (NAME o) const { return (TYPE&) v == (TYPE&) o.v; } \
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400354 inline bool sanitize (SANITIZE_ARG_DEF) { \
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400355 TRACE_SANITIZE (); \
Behdad Esfahbodb28815c2009-08-04 22:35:36 -0400356 return SANITIZE_SELF (); \
357 } \
Behdad Esfahbod9e826ea2009-08-06 18:24:55 -0400358 private: unsigned char v[BYTES]; \
359 }; \
360 ASSERT_SIZE (NAME, BYTES)
361
362#define DEFINE_INT_TYPE1(NAME, TYPE, BIG_ENDIAN, BYTES) \
363 struct NAME \
364 { \
365 inline NAME& operator = (TYPE i) { BIG_ENDIAN##_put_unaligned(v, i); return *this; } \
366 inline operator TYPE(void) const { return BIG_ENDIAN##_get_unaligned (v); } \
367 inline bool operator== (NAME o) const { return BIG_ENDIAN##_cmp_unaligned (v, o.v); } \
368 inline bool sanitize (SANITIZE_ARG_DEF) { \
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400369 TRACE_SANITIZE (); \
Behdad Esfahbod9e826ea2009-08-06 18:24:55 -0400370 return SANITIZE_SELF (); \
371 } \
372 private: unsigned char v[BYTES]; \
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400373 }; \
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400374 ASSERT_SIZE (NAME, BYTES)
Behdad Esfahboddf660282009-08-01 20:46:02 -0400375#define DEFINE_INT_TYPE0(NAME, type, b) DEFINE_INT_TYPE1 (NAME, type##_t, hb_be_##type, b)
376#define DEFINE_INT_TYPE(NAME, u, w) DEFINE_INT_TYPE0 (NAME, u##int##w, (w / 8))
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400377
Behdad Esfahbodf78e70c2006-12-21 22:30:38 -0500378
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400379DEFINE_INT_TYPE (USHORT, u, 16); /* 16-bit unsigned integer. */
380DEFINE_INT_TYPE (SHORT, , 16); /* 16-bit signed integer. */
381DEFINE_INT_TYPE (ULONG, u, 32); /* 32-bit unsigned integer. */
382DEFINE_INT_TYPE (LONG, , 32); /* 32-bit signed integer. */
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500383
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400384
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500385/* Array of four uint8s (length = 32 bits) used to identify a script, language
386 * system, feature, or baseline */
Behdad Esfahbod20cc86b2009-05-25 02:41:49 -0400387struct Tag : ULONG
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400388{
Behdad Esfahbod4497af02009-05-25 03:20:18 -0400389 inline Tag (const Tag &o) { *(ULONG*)this = (ULONG&) o; }
390 inline Tag (uint32_t i) { *(ULONG*)this = i; }
391 inline Tag (const char *c) { *(ULONG*)this = *(ULONG*)c; }
392 inline bool operator== (const char *c) const { return *(ULONG*)this == *(ULONG*)c; }
Behdad Esfahbodbefc0222006-12-25 09:14:52 -0500393 /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */
Behdad Esfahbod2b5a59c2009-08-04 11:38:50 -0400394 inline operator const char* (void) const { return CONST_CHARP(this); }
395 inline operator char* (void) { return CHARP(this); }
Behdad Esfahbod738c54d2009-08-04 14:42:46 -0400396
397 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400398 TRACE_SANITIZE ();
Behdad Esfahbod738c54d2009-08-04 14:42:46 -0400399 /* Note: Only accept ASCII-visible tags (mind DEL)
400 * This is one of the few times (only time?) we check
401 * for data integrity, as opposed o just boundary checks
402 */
403 return SANITIZE_SELF () && (((uint32_t) *this) & 0x80808080) == 0;
404 }
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500405};
Behdad Esfahbod303fe622008-01-23 00:20:48 -0500406ASSERT_SIZE (Tag, 4);
Behdad Esfahbodda1097b2009-05-17 19:31:18 -0400407#define _NULL_TAG_INIT {' ', ' ', ' ', ' '}
408DEFINE_NULL_DATA (Tag, 4, _NULL_TAG_INIT);
409#undef _NULL_TAG_INIT
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500410
411/* Glyph index number, same as uint16 (length = 16 bits) */
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400412typedef USHORT GlyphID;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500413
Behdad Esfahbod1f437e62008-01-23 04:36:40 -0500414/* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400415typedef USHORT Offset;
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400416
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400417/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
418typedef ULONG LongOffset;
419
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500420
421/* CheckSum */
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400422struct CheckSum : ULONG
423{
424 static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
425 {
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500426 uint32_t Sum = 0L;
Behdad Esfahbod01e4fcb2006-12-21 22:31:31 -0500427 ULONG *EndPtr = Table+((Length+3) & ~3) / sizeof(ULONG);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500428
429 while (Table < EndPtr)
430 Sum += *Table++;
431 return Sum;
432 }
433};
Behdad Esfahbod8b835802009-05-16 22:48:14 -0400434ASSERT_SIZE (CheckSum, 4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500435
436
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500437/*
438 * Version Numbers
439 */
440
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400441struct FixedVersion
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400442{
Behdad Esfahbod09c292e2009-05-26 19:48:16 -0400443 inline operator uint32_t (void) const { return (major << 16) + minor; }
Behdad Esfahbod96908b82009-05-24 12:30:40 -0400444
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400445 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400446 TRACE_SANITIZE ();
Behdad Esfahbodcd3827e2009-08-04 02:09:34 -0400447 return SANITIZE_SELF ();
448 }
449
Behdad Esfahbod6ad8d5f2009-05-25 02:27:29 -0400450 USHORT major;
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400451 USHORT minor;
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500452};
Behdad Esfahbod87fcdcb2009-05-24 01:03:24 -0400453ASSERT_SIZE (FixedVersion, 4);
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500454
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400455
456
457/*
458 * Template subclasses of Offset and LongOffset that do the dereferencing.
459 * Use: (this+memberName)
460 */
461
462template <typename OffsetType, typename Type>
463struct GenericOffsetTo : OffsetType
464{
465 inline const Type& operator() (const void *base) const
466 {
467 unsigned int offset = *this;
468 if (HB_UNLIKELY (!offset)) return Null(Type);
Behdad Esfahbod95639fc2009-08-04 12:05:24 -0400469 return CONST_CAST(Type, *CONST_CHARP(base), offset);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400470 }
471
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400472 inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400473 TRACE_SANITIZE ();
Behdad Esfahbod95528132009-08-14 16:17:32 -0400474 if (!SANITIZE_SELF ()) return false;
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400475 unsigned int offset = *this;
476 if (HB_UNLIKELY (!offset)) return true;
Behdad Esfahbodac26e2a2009-08-04 14:10:39 -0400477 return SANITIZE (CAST(Type, *DECONST_CHARP(base), offset)) || NEUTER (DECONST_CAST(OffsetType,*this,0), 0);
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400478 }
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400479 inline bool sanitize (SANITIZE_ARG_DEF, const void *base, const void *base2) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400480 TRACE_SANITIZE ();
Behdad Esfahbod95528132009-08-14 16:17:32 -0400481 if (!SANITIZE_SELF ()) return false;
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400482 unsigned int offset = *this;
483 if (HB_UNLIKELY (!offset)) return true;
484 return SANITIZE_BASE (CAST(Type, *DECONST_CHARP(base), offset), base2) || NEUTER (DECONST_CAST(OffsetType,*this,0), 0);
485 }
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400486 inline bool sanitize (SANITIZE_ARG_DEF, const void *base, unsigned int user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400487 TRACE_SANITIZE ();
Behdad Esfahbod95528132009-08-14 16:17:32 -0400488 if (!SANITIZE_SELF ()) return false;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400489 unsigned int offset = *this;
490 if (HB_UNLIKELY (!offset)) return true;
Behdad Esfahbodac26e2a2009-08-04 14:10:39 -0400491 return SANITIZE_BASE (CAST(Type, *DECONST_CHARP(base), offset), user_data) || NEUTER (DECONST_CAST(OffsetType,*this,0), 0);
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400492 }
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400493};
494template <typename Base, typename OffsetType, typename Type>
495inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
496
497template <typename Type>
498struct OffsetTo : GenericOffsetTo<Offset, Type> {};
499
500template <typename Type>
501struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
Behdad Esfahbodbff3c0f2009-08-07 19:46:30 -0400502
503
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400504/*
505 * Array Types
506 */
507
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400508template <typename LenType, typename Type>
509struct GenericArrayOf
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400510{
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500511 const Type *const_array(void) const { return CONST_ARRAY_AFTER (Type, len); }
512 Type *array(void) { return ARRAY_AFTER (Type, len); }
513
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400514 inline const Type& operator [] (unsigned int i) const
515 {
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400516 if (HB_UNLIKELY (i >= len)) return Null(Type);
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500517 return const_array()[i];
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400518 }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400519 inline unsigned int get_size () const
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500520 { return sizeof (len) + len * sizeof (Type); }
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400521
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400522 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400523 TRACE_SANITIZE ();
Behdad Esfahboddc9c4d92009-08-04 12:26:26 -0400524 if (!SANITIZE_GET_SIZE()) return false;
Behdad Esfahbod3564ee52009-08-14 18:32:56 -0400525 /* Note:
526 * for non-recursive types, this is not much needed.
527 * But we keep the code to make sure the objects pointed to
528 * do have a simple sanitize(). */
529 return true;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400530 unsigned int count = len;
531 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500532 if (!SANITIZE (array()[i]))
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400533 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400534 return true;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400535 }
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400536 inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400537 TRACE_SANITIZE ();
Behdad Esfahboddc9c4d92009-08-04 12:26:26 -0400538 if (!SANITIZE_GET_SIZE()) return false;
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400539 unsigned int count = len;
540 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500541 if (!array()[i].sanitize (SANITIZE_ARG, base))
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400542 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400543 return true;
Behdad Esfahbode6ab2c52009-08-04 10:23:01 -0400544 }
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400545 inline bool sanitize (SANITIZE_ARG_DEF, const void *base, const void *base2) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400546 TRACE_SANITIZE ();
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400547 if (!SANITIZE_GET_SIZE()) return false;
548 unsigned int count = len;
549 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500550 if (!array()[i].sanitize (SANITIZE_ARG, base, base2))
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400551 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400552 return true;
Behdad Esfahbodb508e5c2009-08-04 15:07:24 -0400553 }
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400554 inline bool sanitize (SANITIZE_ARG_DEF, const void *base, unsigned int user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400555 TRACE_SANITIZE ();
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400556 if (!SANITIZE_GET_SIZE()) return false;
557 unsigned int count = len;
558 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500559 if (!array()[i].sanitize (SANITIZE_ARG, base, user_data))
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400560 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400561 return true;
Behdad Esfahbod42b778f2009-08-04 13:30:49 -0400562 }
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400563
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400564 LenType len;
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500565/*Type array[VAR];*/
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400566};
567
Behdad Esfahbod92b5dd82009-08-04 10:41:32 -0400568/* An array with a USHORT number of elements. */
569template <typename Type>
570struct ArrayOf : GenericArrayOf<USHORT, Type> {};
571
572/* An array with a ULONG number of elements. */
573template <typename Type>
574struct LongArrayOf : GenericArrayOf<ULONG, Type> {};
575
576/* Array of Offset's */
577template <typename Type>
578struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
579
580/* Array of LongOffset's */
581template <typename Type>
582struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
583
584/* LongArray of LongOffset's */
585template <typename Type>
586struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
587
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400588/* Array of offsets relative to the beginning of the array itself. */
589template <typename Type>
590struct OffsetListOf : OffsetArrayOf<Type>
591{
592 inline const Type& operator [] (unsigned int i) const
593 {
594 if (HB_UNLIKELY (i >= this->len)) return Null(Type);
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500595 return this+this->const_array()[i];
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400596 }
597
598 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400599 TRACE_SANITIZE ();
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400600 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
601 }
602 inline bool sanitize (SANITIZE_ARG_DEF, unsigned int user_data) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400603 TRACE_SANITIZE ();
Behdad Esfahbod80e2aa22009-08-14 18:40:56 -0400604 return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this), user_data);
605 }
606};
607
608
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400609/* An array with a USHORT number of elements,
610 * starting at second element. */
611template <typename Type>
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400612struct HeadlessArrayOf
613{
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500614 const Type *const_array(void) const { return CONST_ARRAY_AFTER (Type, len); }
615 Type *array(void) { return ARRAY_AFTER (Type, len); }
616
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400617 inline const Type& operator [] (unsigned int i) const
618 {
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400619 if (HB_UNLIKELY (i >= len || !i)) return Null(Type);
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500620 return const_array()[i-1];
Behdad Esfahbode8cbaaf2009-05-18 02:03:58 -0400621 }
Behdad Esfahbod60d77cf2009-05-19 23:58:54 -0400622 inline unsigned int get_size () const
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500623 { return sizeof (len) + (len ? len - 1 : 0) * sizeof (Type); }
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400624
Behdad Esfahbod20b035d2009-08-10 19:00:36 -0400625 inline bool sanitize (SANITIZE_ARG_DEF) {
Behdad Esfahbod3e2401f2009-08-28 17:17:11 -0400626 TRACE_SANITIZE ();
Behdad Esfahboddc9c4d92009-08-04 12:26:26 -0400627 if (!SANITIZE_GET_SIZE()) return false;
Behdad Esfahbod3564ee52009-08-14 18:32:56 -0400628 /* Note:
629 * for non-recursive types, this is not much needed.
630 * But we keep the code to make sure the objects pointed to
631 * do have a simple sanitize(). */
632 return true;
Behdad Esfahbod15164d92009-08-04 13:57:41 -0400633 unsigned int count = len ? len - 1 : 0;
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500634 Type *a = array();
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400635 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500636 if (!SANITIZE (a[i]))
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400637 return false;
Behdad Esfahbod9bd629c2009-08-04 21:42:23 -0400638 return true;
Behdad Esfahbod70de50c2009-08-04 00:58:28 -0400639 }
640
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400641 USHORT len;
Behdad Esfahbodd3480ba2009-11-03 10:47:29 -0500642/*Type array[VAR];*/
Behdad Esfahbod5f810362009-05-17 00:54:25 -0400643};
644
Behdad Esfahbod6b4ce012006-12-21 22:31:10 -0500645
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -0400646#endif /* HB_OPEN_TYPES_PRIVATE_HH */