Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 2007,2008,2009,2010 Red Hat, Inc. |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 3 | * Copyright © 2012 Google, Inc. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 4 | * |
Behdad Esfahbod | c755cb3 | 2010-04-22 00:11:43 -0400 | [diff] [blame] | 5 | * This is part of HarfBuzz, a text shaping library. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 6 | * |
| 7 | * Permission is hereby granted, without written agreement and without |
| 8 | * license or royalty fees, to use, copy, modify, and distribute this |
| 9 | * software and its documentation for any purpose, provided that the |
| 10 | * above copyright notice and the following two paragraphs appear in |
| 11 | * all copies of this software. |
| 12 | * |
| 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 14 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 15 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 16 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 17 | * DAMAGE. |
| 18 | * |
| 19 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 20 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 22 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 23 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 24 | * |
| 25 | * Red Hat Author(s): Behdad Esfahbod |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 26 | * Google Author(s): Behdad Esfahbod |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 27 | */ |
| 28 | |
Behdad Esfahbod | 0f0cd9d | 2010-06-09 06:32:56 -0400 | [diff] [blame] | 29 | #ifndef HB_OPEN_TYPE_PRIVATE_HH |
| 30 | #define HB_OPEN_TYPE_PRIVATE_HH |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 31 | |
Behdad Esfahbod | c57d454 | 2011-04-20 18:50:27 -0400 | [diff] [blame] | 32 | #include "hb-private.hh" |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 33 | |
Behdad Esfahbod | a16ecbf | 2008-01-23 17:01:55 -0500 | [diff] [blame] | 34 | |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 35 | namespace OT { |
| 36 | |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 37 | |
Behdad Esfahbod | 8182252 | 2012-11-23 13:27:16 -0500 | [diff] [blame] | 38 | |
| 39 | /* |
Behdad Esfahbod | 196598b | 2009-08-04 11:04:32 -0400 | [diff] [blame] | 40 | * Casts |
| 41 | */ |
| 42 | |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 43 | /* Cast to struct T, reference to reference */ |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 44 | template<typename Type, typename TObject> |
Behdad Esfahbod | 6faff8e | 2014-04-28 14:29:39 -0700 | [diff] [blame] | 45 | static inline const Type& CastR(const TObject &X) |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 46 | { return reinterpret_cast<const Type&> (X); } |
| 47 | template<typename Type, typename TObject> |
Behdad Esfahbod | 6faff8e | 2014-04-28 14:29:39 -0700 | [diff] [blame] | 48 | static inline Type& CastR(TObject &X) |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 49 | { return reinterpret_cast<Type&> (X); } |
Behdad Esfahbod | 196598b | 2009-08-04 11:04:32 -0400 | [diff] [blame] | 50 | |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 51 | /* Cast to struct T, pointer to pointer */ |
| 52 | template<typename Type, typename TObject> |
Behdad Esfahbod | 6faff8e | 2014-04-28 14:29:39 -0700 | [diff] [blame] | 53 | static inline const Type* CastP(const TObject *X) |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 54 | { return reinterpret_cast<const Type*> (X); } |
| 55 | template<typename Type, typename TObject> |
Behdad Esfahbod | 6faff8e | 2014-04-28 14:29:39 -0700 | [diff] [blame] | 56 | static inline Type* CastP(TObject *X) |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 57 | { return reinterpret_cast<Type*> (X); } |
| 58 | |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 59 | /* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory |
| 60 | * location pointed to by P plus Ofs bytes. */ |
| 61 | template<typename Type> |
Behdad Esfahbod | 6faff8e | 2014-04-28 14:29:39 -0700 | [diff] [blame] | 62 | static inline const Type& StructAtOffset(const void *P, unsigned int offset) |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 63 | { return * reinterpret_cast<const Type*> ((const char *) P + offset); } |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 64 | template<typename Type> |
Behdad Esfahbod | 6faff8e | 2014-04-28 14:29:39 -0700 | [diff] [blame] | 65 | static inline Type& StructAtOffset(void *P, unsigned int offset) |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 66 | { return * reinterpret_cast<Type*> ((char *) P + offset); } |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 67 | |
Behdad Esfahbod | 2e2f43e | 2010-04-21 22:30:36 -0400 | [diff] [blame] | 68 | /* StructAfter<T>(X) returns the struct T& that is placed after X. |
Behdad Esfahbod | 29c3f5e | 2010-04-21 23:01:00 -0400 | [diff] [blame] | 69 | * Works with X of variable size also. X must implement get_size() */ |
Behdad Esfahbod | e961c86 | 2010-04-21 15:56:11 -0400 | [diff] [blame] | 70 | template<typename Type, typename TObject> |
Behdad Esfahbod | 6faff8e | 2014-04-28 14:29:39 -0700 | [diff] [blame] | 71 | static inline const Type& StructAfter(const TObject &X) |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 72 | { return StructAtOffset<Type>(&X, X.get_size()); } |
Behdad Esfahbod | e961c86 | 2010-04-21 15:56:11 -0400 | [diff] [blame] | 73 | template<typename Type, typename TObject> |
Behdad Esfahbod | 6faff8e | 2014-04-28 14:29:39 -0700 | [diff] [blame] | 74 | static inline Type& StructAfter(TObject &X) |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 75 | { return StructAtOffset<Type>(&X, X.get_size()); } |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 76 | |
Behdad Esfahbod | e961c86 | 2010-04-21 15:56:11 -0400 | [diff] [blame] | 77 | |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 78 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 79 | /* |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 80 | * Size checking |
| 81 | */ |
| 82 | |
Behdad Esfahbod | f679635 | 2010-05-13 13:34:17 -0400 | [diff] [blame] | 83 | /* Check _assertion in a method environment */ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 84 | #define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \ |
| 85 | inline void _instance_assertion_on_line_##_line (void) const \ |
| 86 | { \ |
| 87 | ASSERT_STATIC (_assertion); \ |
| 88 | ASSERT_INSTANCE_POD (*this); /* Make sure it's POD. */ \ |
| 89 | } |
| 90 | # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion) |
| 91 | # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion) |
| 92 | |
Behdad Esfahbod | f679635 | 2010-05-13 13:34:17 -0400 | [diff] [blame] | 93 | /* Check that _code compiles in a method environment */ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 94 | #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \ |
| 95 | inline void _compiles_assertion_on_line_##_line (void) const \ |
Behdad Esfahbod | f679635 | 2010-05-13 13:34:17 -0400 | [diff] [blame] | 96 | { _code; } |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 97 | # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code) |
| 98 | # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code) |
Behdad Esfahbod | 0abcc3b | 2010-05-10 17:04:20 -0400 | [diff] [blame] | 99 | |
| 100 | |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 101 | #define DEFINE_SIZE_STATIC(size) \ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 102 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)); \ |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 103 | static const unsigned int static_size = (size); \ |
| 104 | static const unsigned int min_size = (size) |
| 105 | |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 106 | /* Size signifying variable-sized array */ |
| 107 | #define VAR 1 |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 108 | |
Behdad Esfahbod | 596e471 | 2010-05-10 18:47:48 -0400 | [diff] [blame] | 109 | #define DEFINE_SIZE_UNION(size, _member) \ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 110 | DEFINE_INSTANCE_ASSERTION (this->u._member.static_size == (size)); \ |
Behdad Esfahbod | 596e471 | 2010-05-10 18:47:48 -0400 | [diff] [blame] | 111 | static const unsigned int min_size = (size) |
| 112 | |
Behdad Esfahbod | bea34c7 | 2010-05-10 17:28:16 -0400 | [diff] [blame] | 113 | #define DEFINE_SIZE_MIN(size) \ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 114 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)); \ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 115 | static const unsigned int min_size = (size) |
| 116 | |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 117 | #define DEFINE_SIZE_ARRAY(size, array) \ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 118 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \ |
| 119 | DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \ |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 120 | static const unsigned int min_size = (size) |
| 121 | |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 122 | #define DEFINE_SIZE_ARRAY2(size, array1, array2) \ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 123 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \ |
| 124 | DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].static_size) \ |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 125 | static const unsigned int min_size = (size) |
| 126 | |
| 127 | |
| 128 | |
| 129 | /* |
Behdad Esfahbod | f0abcd6 | 2010-05-02 18:14:25 -0400 | [diff] [blame] | 130 | * Null objects |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 131 | */ |
| 132 | |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 133 | /* Global nul-content Null pool. Enlarge as necessary. */ |
Behdad Esfahbod | 0bb0f5d | 2012-06-07 17:42:48 -0400 | [diff] [blame] | 134 | /* TODO This really should be a extern HB_INTERNAL and defined somewhere... */ |
Behdad Esfahbod | b7878cd | 2014-05-13 21:47:51 -0400 | [diff] [blame] | 135 | static const void *_NullPool[(256+8) / sizeof (void *)]; |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 136 | |
Behdad Esfahbod | d2c2ca8 | 2010-05-10 19:58:25 -0400 | [diff] [blame] | 137 | /* Generic nul-content Null objects. */ |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 138 | template <typename Type> |
Behdad Esfahbod | 7f97d2c | 2010-10-01 18:58:50 -0400 | [diff] [blame] | 139 | static inline const Type& Null (void) { |
Behdad Esfahbod | fd0de88 | 2012-11-15 10:47:14 -0800 | [diff] [blame] | 140 | ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool)); |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 141 | return *CastP<Type> (_NullPool); |
Behdad Esfahbod | 9d36778 | 2010-04-21 00:32:47 -0400 | [diff] [blame] | 142 | } |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 143 | |
| 144 | /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */ |
Behdad Esfahbod | 65f46b0 | 2010-05-06 19:35:19 -0400 | [diff] [blame] | 145 | #define DEFINE_NULL_DATA(Type, data) \ |
Behdad Esfahbod | fd0de88 | 2012-11-15 10:47:14 -0800 | [diff] [blame] | 146 | static const char _Null##Type[sizeof (Type) + 1] = data; /* +1 is for nul-termination in data */ \ |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 147 | template <> \ |
Behdad Esfahbod | 6faff8e | 2014-04-28 14:29:39 -0700 | [diff] [blame] | 148 | /*static*/ inline const Type& Null<Type> (void) { \ |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 149 | return *CastP<Type> (_Null##Type); \ |
Behdad Esfahbod | 565c80b | 2010-04-22 10:26:35 -0400 | [diff] [blame] | 150 | } /* The following line really exists such that we end in a place needing semicolon */ \ |
Behdad Esfahbod | bea34c7 | 2010-05-10 17:28:16 -0400 | [diff] [blame] | 151 | ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type)) |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 152 | |
| 153 | /* Accessor macro. */ |
Behdad Esfahbod | 9d36778 | 2010-04-21 00:32:47 -0400 | [diff] [blame] | 154 | #define Null(Type) Null<Type>() |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 155 | |
| 156 | |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 157 | |
| 158 | /* |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 159 | * Sanitize |
| 160 | */ |
| 161 | |
Behdad Esfahbod | 95e2024 | 2009-08-28 16:31:20 -0400 | [diff] [blame] | 162 | #ifndef HB_DEBUG_SANITIZE |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 163 | #define HB_DEBUG_SANITIZE (HB_DEBUG+0) |
Behdad Esfahbod | 95e2024 | 2009-08-28 16:31:20 -0400 | [diff] [blame] | 164 | #endif |
| 165 | |
Behdad Esfahbod | 20e3dd5 | 2010-05-04 23:21:57 -0400 | [diff] [blame] | 166 | |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 167 | #define TRACE_SANITIZE(this) \ |
Behdad Esfahbod | 902cc8a | 2012-11-23 15:06:59 -0500 | [diff] [blame] | 168 | hb_auto_trace_t<HB_DEBUG_SANITIZE, bool> trace \ |
Behdad Esfahbod | 2c53bd3 | 2012-11-23 17:29:05 -0500 | [diff] [blame] | 169 | (&c->debug_depth, c->get_name (), this, HB_FUNC, \ |
Behdad Esfahbod | 902cc8a | 2012-11-23 15:06:59 -0500 | [diff] [blame] | 170 | ""); |
Behdad Esfahbod | 807c5b0 | 2010-04-28 20:25:22 -0400 | [diff] [blame] | 171 | |
Behdad Esfahbod | 07a52b6 | 2013-02-25 19:09:57 -0500 | [diff] [blame] | 172 | /* This limits sanitizing time on really broken fonts. */ |
| 173 | #ifndef HB_SANITIZE_MAX_EDITS |
| 174 | #define HB_SANITIZE_MAX_EDITS 100 |
| 175 | #endif |
Behdad Esfahbod | 807c5b0 | 2010-04-28 20:25:22 -0400 | [diff] [blame] | 176 | |
Behdad Esfahbod | 1376fb7 | 2010-04-29 02:19:21 -0400 | [diff] [blame] | 177 | struct hb_sanitize_context_t |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 178 | { |
Behdad Esfahbod | 2c53bd3 | 2012-11-23 17:29:05 -0500 | [diff] [blame] | 179 | inline const char *get_name (void) { return "SANITIZE"; } |
| 180 | static const unsigned int max_debug_depth = HB_DEBUG_SANITIZE; |
| 181 | typedef bool return_t; |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 182 | template <typename T, typename F> |
| 183 | inline bool may_dispatch (const T *obj, const F *format) |
| 184 | { return format->sanitize (this); } |
Behdad Esfahbod | 2c53bd3 | 2012-11-23 17:29:05 -0500 | [diff] [blame] | 185 | template <typename T> |
Behdad Esfahbod | 9c5a9ee | 2013-03-09 01:55:04 -0500 | [diff] [blame] | 186 | inline return_t dispatch (const T &obj) { return obj.sanitize (this); } |
Behdad Esfahbod | 2c53bd3 | 2012-11-23 17:29:05 -0500 | [diff] [blame] | 187 | static return_t default_return_value (void) { return true; } |
Behdad Esfahbod | 095a125 | 2015-02-19 10:29:41 +0300 | [diff] [blame] | 188 | bool stop_sublookup_iteration (const return_t r) const { return !r; } |
Behdad Esfahbod | 2c53bd3 | 2012-11-23 17:29:05 -0500 | [diff] [blame] | 189 | |
Behdad Esfahbod | 31f18ab | 2011-06-15 09:49:58 -0400 | [diff] [blame] | 190 | inline void init (hb_blob_t *b) |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 191 | { |
Behdad Esfahbod | 31f18ab | 2011-06-15 09:49:58 -0400 | [diff] [blame] | 192 | this->blob = hb_blob_reference (b); |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 193 | this->writable = false; |
| 194 | } |
| 195 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 196 | inline void start_processing (void) |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 197 | { |
Behdad Esfahbod | 4101ca7 | 2011-05-11 14:30:56 -0400 | [diff] [blame] | 198 | this->start = hb_blob_get_data (this->blob, NULL); |
| 199 | this->end = this->start + hb_blob_get_length (this->blob); |
Behdad Esfahbod | d5a5052 | 2014-12-18 18:09:41 -0800 | [diff] [blame] | 200 | assert (this->start <= this->end); /* Must not overflow. */ |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 201 | this->edit_count = 0; |
Behdad Esfahbod | 20e3dd5 | 2010-05-04 23:21:57 -0400 | [diff] [blame] | 202 | this->debug_depth = 0; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 203 | |
Behdad Esfahbod | 0766ee1 | 2014-12-12 18:23:20 -0800 | [diff] [blame] | 204 | DEBUG_MSG_LEVEL (SANITIZE, start, 0, +1, |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 205 | "start [%p..%p] (%lu bytes)", |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 206 | this->start, this->end, |
| 207 | (unsigned long) (this->end - this->start)); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 210 | inline void end_processing (void) |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 211 | { |
Behdad Esfahbod | 0766ee1 | 2014-12-12 18:23:20 -0800 | [diff] [blame] | 212 | DEBUG_MSG_LEVEL (SANITIZE, this->start, 0, -1, |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 213 | "end [%p..%p] %u edit requests", |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 214 | this->start, this->end, this->edit_count); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 215 | |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 216 | hb_blob_destroy (this->blob); |
| 217 | this->blob = NULL; |
| 218 | this->start = this->end = NULL; |
| 219 | } |
| 220 | |
Behdad Esfahbod | 4ad2cc5 | 2010-05-06 09:24:24 -0400 | [diff] [blame] | 221 | inline bool check_range (const void *base, unsigned int len) const |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 222 | { |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 223 | const char *p = (const char *) base; |
Behdad Esfahbod | 282b13f | 2014-12-12 19:32:46 -0800 | [diff] [blame] | 224 | bool ok = this->start <= p && p <= this->end && (unsigned int) (this->end - p) >= len; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 225 | |
Behdad Esfahbod | 282b13f | 2014-12-12 19:32:46 -0800 | [diff] [blame] | 226 | DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0, |
| 227 | "check_range [%p..%p] (%d bytes) in [%p..%p] -> %s", |
Behdad Esfahbod | 902cc8a | 2012-11-23 15:06:59 -0500 | [diff] [blame] | 228 | p, p + len, len, |
Behdad Esfahbod | 282b13f | 2014-12-12 19:32:46 -0800 | [diff] [blame] | 229 | this->start, this->end, |
| 230 | ok ? "OK" : "OUT-OF-RANGE"); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 231 | |
Behdad Esfahbod | 282b13f | 2014-12-12 19:32:46 -0800 | [diff] [blame] | 232 | return likely (ok); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Behdad Esfahbod | 1cd1e11 | 2010-05-05 20:15:14 -0400 | [diff] [blame] | 235 | inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 236 | { |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 237 | const char *p = (const char *) base; |
Behdad Esfahbod | 080a0eb | 2011-04-28 16:01:01 -0400 | [diff] [blame] | 238 | bool overflows = _hb_unsigned_int_mul_overflows (len, record_size); |
Behdad Esfahbod | 282b13f | 2014-12-12 19:32:46 -0800 | [diff] [blame] | 239 | unsigned int array_size = record_size * len; |
| 240 | bool ok = !overflows && this->check_range (base, array_size); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 241 | |
Behdad Esfahbod | 282b13f | 2014-12-12 19:32:46 -0800 | [diff] [blame] | 242 | DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0, |
| 243 | "check_array [%p..%p] (%d*%d=%d bytes) in [%p..%p] -> %s", |
| 244 | p, p + (record_size * len), record_size, len, (unsigned int) array_size, |
| 245 | this->start, this->end, |
| 246 | overflows ? "OVERFLOWS" : ok ? "OK" : "OUT-OF-RANGE"); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 247 | |
Behdad Esfahbod | 282b13f | 2014-12-12 19:32:46 -0800 | [diff] [blame] | 248 | return likely (ok); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 249 | } |
| 250 | |
Behdad Esfahbod | b157617 | 2010-05-06 14:48:27 -0400 | [diff] [blame] | 251 | template <typename Type> |
| 252 | inline bool check_struct (const Type *obj) const |
| 253 | { |
Behdad Esfahbod | 5484237 | 2010-05-10 18:13:32 -0400 | [diff] [blame] | 254 | return likely (this->check_range (obj, obj->min_size)); |
Behdad Esfahbod | b157617 | 2010-05-06 14:48:27 -0400 | [diff] [blame] | 255 | } |
| 256 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 257 | inline bool may_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED) |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 258 | { |
Behdad Esfahbod | 07a52b6 | 2013-02-25 19:09:57 -0500 | [diff] [blame] | 259 | if (this->edit_count >= HB_SANITIZE_MAX_EDITS) |
| 260 | return false; |
| 261 | |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 262 | const char *p = (const char *) base; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 263 | this->edit_count++; |
| 264 | |
Behdad Esfahbod | 282b13f | 2014-12-12 19:32:46 -0800 | [diff] [blame] | 265 | DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0, |
Behdad Esfahbod | 902cc8a | 2012-11-23 15:06:59 -0500 | [diff] [blame] | 266 | "may_edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s", |
| 267 | this->edit_count, |
| 268 | p, p + len, len, |
Behdad Esfahbod | e77b442 | 2012-12-17 18:42:59 -0500 | [diff] [blame] | 269 | this->start, this->end, |
| 270 | this->writable ? "GRANTED" : "DENIED"); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 271 | |
Behdad Esfahbod | 282b13f | 2014-12-12 19:32:46 -0800 | [diff] [blame] | 272 | return this->writable; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Behdad Esfahbod | 51f5635 | 2014-06-04 18:42:32 -0400 | [diff] [blame] | 275 | template <typename Type, typename ValueType> |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 276 | inline bool try_set (const Type *obj, const ValueType &v) { |
Behdad Esfahbod | 51f5635 | 2014-06-04 18:42:32 -0400 | [diff] [blame] | 277 | if (this->may_edit (obj, obj->static_size)) { |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 278 | const_cast<Type *> (obj)->set (v); |
Behdad Esfahbod | 51f5635 | 2014-06-04 18:42:32 -0400 | [diff] [blame] | 279 | return true; |
| 280 | } |
| 281 | return false; |
| 282 | } |
| 283 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 284 | mutable unsigned int debug_depth; |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 285 | const char *start, *end; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 286 | bool writable; |
Behdad Esfahbod | 254933c | 2010-04-23 13:57:10 -0400 | [diff] [blame] | 287 | unsigned int edit_count; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 288 | hb_blob_t *blob; |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 289 | }; |
| 290 | |
Behdad Esfahbod | 1376fb7 | 2010-04-29 02:19:21 -0400 | [diff] [blame] | 291 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 292 | |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 293 | /* Template to sanitize an object. */ |
| 294 | template <typename Type> |
| 295 | struct Sanitizer |
| 296 | { |
| 297 | static hb_blob_t *sanitize (hb_blob_t *blob) { |
Behdad Esfahbod | 7e8c389 | 2014-07-25 11:18:11 -0400 | [diff] [blame] | 298 | hb_sanitize_context_t c[1] = {{0, NULL, NULL, false, 0, NULL}}; |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 299 | bool sane; |
| 300 | |
Behdad Esfahbod | d0b6573 | 2009-08-06 18:34:47 -0400 | [diff] [blame] | 301 | /* TODO is_sane() stuff */ |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 302 | |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 303 | c->init (blob); |
| 304 | |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 305 | retry: |
Behdad Esfahbod | 0766ee1 | 2014-12-12 18:23:20 -0800 | [diff] [blame] | 306 | DEBUG_MSG_FUNC (SANITIZE, c->start, "start"); |
Behdad Esfahbod | 4f3ad91 | 2009-08-04 23:01:23 -0400 | [diff] [blame] | 307 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 308 | c->start_processing (); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 309 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 310 | if (unlikely (!c->start)) { |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 311 | c->end_processing (); |
Behdad Esfahbod | 48146e5 | 2010-05-10 20:07:56 -0400 | [diff] [blame] | 312 | return blob; |
| 313 | } |
| 314 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 315 | Type *t = CastP<Type> (const_cast<char *> (c->start)); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 316 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 317 | sane = t->sanitize (c); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 318 | if (sane) { |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 319 | if (c->edit_count) { |
Behdad Esfahbod | 0766ee1 | 2014-12-12 18:23:20 -0800 | [diff] [blame] | 320 | DEBUG_MSG_FUNC (SANITIZE, c->start, "passed first round with %d edits; going for second round", c->edit_count); |
Behdad Esfahbod | fa03017 | 2010-04-29 13:48:26 -0400 | [diff] [blame] | 321 | |
Behdad Esfahbod | 8b53461 | 2009-08-19 18:16:50 -0400 | [diff] [blame] | 322 | /* sanitize again to ensure no toe-stepping */ |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 323 | c->edit_count = 0; |
| 324 | sane = t->sanitize (c); |
| 325 | if (c->edit_count) { |
Behdad Esfahbod | 0766ee1 | 2014-12-12 18:23:20 -0800 | [diff] [blame] | 326 | DEBUG_MSG_FUNC (SANITIZE, c->start, "requested %d edits in second round; FAILLING", c->edit_count); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 327 | sane = false; |
| 328 | } |
| 329 | } |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 330 | } else { |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 331 | unsigned int edit_count = c->edit_count; |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 332 | if (edit_count && !c->writable) { |
| 333 | c->start = hb_blob_get_data_writable (blob, NULL); |
| 334 | c->end = c->start + hb_blob_get_length (blob); |
| 335 | |
| 336 | if (c->start) { |
| 337 | c->writable = true; |
| 338 | /* ok, we made it writable by relocating. try again */ |
Behdad Esfahbod | 0766ee1 | 2014-12-12 18:23:20 -0800 | [diff] [blame] | 339 | DEBUG_MSG_FUNC (SANITIZE, c->start, "retry"); |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 340 | goto retry; |
| 341 | } |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 345 | c->end_processing (); |
Behdad Esfahbod | 4101ca7 | 2011-05-11 14:30:56 -0400 | [diff] [blame] | 346 | |
Behdad Esfahbod | 0766ee1 | 2014-12-12 18:23:20 -0800 | [diff] [blame] | 347 | DEBUG_MSG_FUNC (SANITIZE, c->start, sane ? "PASSED" : "FAILED"); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 348 | if (sane) |
| 349 | return blob; |
| 350 | else { |
| 351 | hb_blob_destroy (blob); |
Behdad Esfahbod | 4911062 | 2011-05-02 19:36:39 -0400 | [diff] [blame] | 352 | return hb_blob_get_empty (); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 353 | } |
| 354 | } |
Behdad Esfahbod | b435ab7 | 2010-05-10 19:51:57 -0400 | [diff] [blame] | 355 | |
| 356 | static const Type* lock_instance (hb_blob_t *blob) { |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 357 | hb_blob_make_immutable (blob); |
| 358 | const char *base = hb_blob_get_data (blob, NULL); |
Behdad Esfahbod | b435ab7 | 2010-05-10 19:51:57 -0400 | [diff] [blame] | 359 | return unlikely (!base) ? &Null(Type) : CastP<Type> (base); |
| 360 | } |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 361 | }; |
| 362 | |
Behdad Esfahbod | 2d15e72 | 2009-04-15 19:50:16 -0400 | [diff] [blame] | 363 | |
Behdad Esfahbod | f0abcd6 | 2010-05-02 18:14:25 -0400 | [diff] [blame] | 364 | |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 365 | /* |
| 366 | * Serialize |
| 367 | */ |
| 368 | |
| 369 | #ifndef HB_DEBUG_SERIALIZE |
| 370 | #define HB_DEBUG_SERIALIZE (HB_DEBUG+0) |
| 371 | #endif |
| 372 | |
| 373 | |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 374 | #define TRACE_SERIALIZE(this) \ |
Behdad Esfahbod | 902cc8a | 2012-11-23 15:06:59 -0500 | [diff] [blame] | 375 | hb_auto_trace_t<HB_DEBUG_SERIALIZE, bool> trace \ |
| 376 | (&c->debug_depth, "SERIALIZE", c, HB_FUNC, \ |
| 377 | ""); |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 378 | |
| 379 | |
| 380 | struct hb_serialize_context_t |
| 381 | { |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 382 | inline hb_serialize_context_t (void *start, unsigned int size) |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 383 | { |
| 384 | this->start = (char *) start; |
| 385 | this->end = this->start + size; |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 386 | |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 387 | this->ran_out_of_room = false; |
| 388 | this->head = this->start; |
| 389 | this->debug_depth = 0; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 390 | } |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 391 | |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 392 | template <typename Type> |
| 393 | inline Type *start_serialize (void) |
| 394 | { |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 395 | DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1, |
| 396 | "start [%p..%p] (%lu bytes)", |
| 397 | this->start, this->end, |
| 398 | (unsigned long) (this->end - this->start)); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 399 | |
| 400 | return start_embed<Type> (); |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 403 | inline void end_serialize (void) |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 404 | { |
| 405 | DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1, |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 406 | "end [%p..%p] serialized %d bytes; %s", |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 407 | this->start, this->end, |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 408 | (int) (this->head - this->start), |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 409 | this->ran_out_of_room ? "RAN OUT OF ROOM" : "did not ran out of room"); |
| 410 | |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | template <typename Type> |
| 414 | inline Type *copy (void) |
| 415 | { |
| 416 | assert (!this->ran_out_of_room); |
| 417 | unsigned int len = this->head - this->start; |
| 418 | void *p = malloc (len); |
| 419 | if (p) |
| 420 | memcpy (p, this->start, len); |
| 421 | return reinterpret_cast<Type *> (p); |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | template <typename Type> |
Behdad Esfahbod | 4b312fb | 2012-09-01 21:56:06 -0400 | [diff] [blame] | 425 | inline Type *allocate_size (unsigned int size) |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 426 | { |
Behdad Esfahbod | 05d5d3c | 2013-02-25 23:57:51 -0500 | [diff] [blame] | 427 | if (unlikely (this->ran_out_of_room || this->end - this->head < ptrdiff_t (size))) { |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 428 | this->ran_out_of_room = true; |
| 429 | return NULL; |
| 430 | } |
Behdad Esfahbod | 4b312fb | 2012-09-01 21:56:06 -0400 | [diff] [blame] | 431 | memset (this->head, 0, size); |
Behdad Esfahbod | 9f2348d | 2012-08-29 21:08:59 -0400 | [diff] [blame] | 432 | char *ret = this->head; |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 433 | this->head += size; |
| 434 | return reinterpret_cast<Type *> (ret); |
| 435 | } |
| 436 | |
| 437 | template <typename Type> |
Behdad Esfahbod | 4b312fb | 2012-09-01 21:56:06 -0400 | [diff] [blame] | 438 | inline Type *allocate_min (void) |
Behdad Esfahbod | 9f2348d | 2012-08-29 21:08:59 -0400 | [diff] [blame] | 439 | { |
Behdad Esfahbod | 4b312fb | 2012-09-01 21:56:06 -0400 | [diff] [blame] | 440 | return this->allocate_size<Type> (Type::min_size); |
Behdad Esfahbod | 9f2348d | 2012-08-29 21:08:59 -0400 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | template <typename Type> |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 444 | inline Type *start_embed (void) |
| 445 | { |
| 446 | Type *ret = reinterpret_cast<Type *> (this->head); |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | template <typename Type> |
Behdad Esfahbod | 4b312fb | 2012-09-01 21:56:06 -0400 | [diff] [blame] | 451 | inline Type *embed (const Type &obj) |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 452 | { |
Behdad Esfahbod | bc5be24 | 2012-09-01 20:48:22 -0400 | [diff] [blame] | 453 | unsigned int size = obj.get_size (); |
Behdad Esfahbod | 4b312fb | 2012-09-01 21:56:06 -0400 | [diff] [blame] | 454 | Type *ret = this->allocate_size<Type> (size); |
Behdad Esfahbod | bc5be24 | 2012-09-01 20:48:22 -0400 | [diff] [blame] | 455 | if (unlikely (!ret)) return NULL; |
| 456 | memcpy (ret, obj, size); |
| 457 | return ret; |
Behdad Esfahbod | 9f2348d | 2012-08-29 21:08:59 -0400 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | template <typename Type> |
Behdad Esfahbod | 4b312fb | 2012-09-01 21:56:06 -0400 | [diff] [blame] | 461 | inline Type *extend_min (Type &obj) |
Behdad Esfahbod | 9f2348d | 2012-08-29 21:08:59 -0400 | [diff] [blame] | 462 | { |
Behdad Esfahbod | bc5be24 | 2012-09-01 20:48:22 -0400 | [diff] [blame] | 463 | unsigned int size = obj.min_size; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 464 | assert (this->start <= (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head); |
| 465 | if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return NULL; |
Behdad Esfahbod | 9f2348d | 2012-08-29 21:08:59 -0400 | [diff] [blame] | 466 | return reinterpret_cast<Type *> (&obj); |
| 467 | } |
| 468 | |
| 469 | template <typename Type> |
Behdad Esfahbod | 4b312fb | 2012-09-01 21:56:06 -0400 | [diff] [blame] | 470 | inline Type *extend (Type &obj) |
Behdad Esfahbod | 9f2348d | 2012-08-29 21:08:59 -0400 | [diff] [blame] | 471 | { |
Behdad Esfahbod | bc5be24 | 2012-09-01 20:48:22 -0400 | [diff] [blame] | 472 | unsigned int size = obj.get_size (); |
| 473 | assert (this->start < (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head); |
Behdad Esfahbod | 811eefe | 2012-09-10 09:56:27 -0400 | [diff] [blame] | 474 | if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return NULL; |
Behdad Esfahbod | bc5be24 | 2012-09-01 20:48:22 -0400 | [diff] [blame] | 475 | return reinterpret_cast<Type *> (&obj); |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | inline void truncate (void *head) |
| 479 | { |
| 480 | assert (this->start < head && head <= this->head); |
| 481 | this->head = (char *) head; |
| 482 | } |
| 483 | |
| 484 | unsigned int debug_depth; |
| 485 | char *start, *end, *head; |
| 486 | bool ran_out_of_room; |
| 487 | }; |
| 488 | |
Behdad Esfahbod | a930c68 | 2012-09-04 18:17:57 -0400 | [diff] [blame] | 489 | template <typename Type> |
| 490 | struct Supplier |
| 491 | { |
Behdad Esfahbod | a930c68 | 2012-09-04 18:17:57 -0400 | [diff] [blame] | 492 | inline Supplier (const Type *array, unsigned int len_) |
| 493 | { |
| 494 | head = array; |
| 495 | len = len_; |
| 496 | } |
| 497 | inline const Type operator [] (unsigned int i) const |
| 498 | { |
| 499 | if (unlikely (i >= len)) return Type (); |
| 500 | return head[i]; |
| 501 | } |
| 502 | |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 503 | inline void advance (unsigned int count) |
| 504 | { |
Behdad Esfahbod | a930c68 | 2012-09-04 18:17:57 -0400 | [diff] [blame] | 505 | if (unlikely (count > len)) |
| 506 | count = len; |
| 507 | len -= count; |
| 508 | head += count; |
| 509 | } |
| 510 | |
| 511 | private: |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 512 | inline Supplier (const Supplier<Type> &); /* Disallow copy */ |
| 513 | inline Supplier<Type>& operator= (const Supplier<Type> &); /* Disallow copy */ |
| 514 | |
Behdad Esfahbod | a930c68 | 2012-09-04 18:17:57 -0400 | [diff] [blame] | 515 | unsigned int len; |
| 516 | const Type *head; |
| 517 | }; |
| 518 | |
| 519 | |
Behdad Esfahbod | e901b95 | 2012-08-29 20:26:08 -0400 | [diff] [blame] | 520 | |
Behdad Esfahbod | f0abcd6 | 2010-05-02 18:14:25 -0400 | [diff] [blame] | 521 | |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 522 | /* |
| 523 | * |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 524 | * The OpenType Font File: Data Types |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 525 | */ |
| 526 | |
| 527 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 528 | /* "The following data types are used in the OpenType font file. |
| 529 | * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */ |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 530 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 531 | /* |
| 532 | * Int types |
| 533 | */ |
| 534 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 535 | |
Behdad Esfahbod | d7bf473 | 2011-08-05 18:18:21 -0400 | [diff] [blame] | 536 | template <typename Type, int Bytes> struct BEInt; |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 537 | |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 538 | template <typename Type> |
Behdad Esfahbod | d7bf473 | 2011-08-05 18:18:21 -0400 | [diff] [blame] | 539 | struct BEInt<Type, 2> |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 540 | { |
| 541 | public: |
Behdad Esfahbod | 666b42f | 2014-10-14 21:24:59 -0700 | [diff] [blame] | 542 | inline void set (Type V) |
| 543 | { |
| 544 | v[0] = (V >> 8) & 0xFF; |
| 545 | v[1] = (V ) & 0xFF; |
| 546 | } |
| 547 | inline operator Type (void) const |
| 548 | { |
| 549 | return (v[0] << 8) |
| 550 | + (v[1] ); |
| 551 | } |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 552 | private: uint8_t v[2]; |
| 553 | }; |
| 554 | template <typename Type> |
Behdad Esfahbod | 5a5640d | 2014-10-14 21:26:13 -0700 | [diff] [blame] | 555 | struct BEInt<Type, 3> |
| 556 | { |
| 557 | public: |
| 558 | inline void set (Type V) |
| 559 | { |
| 560 | v[0] = (V >> 16) & 0xFF; |
| 561 | v[1] = (V >> 8) & 0xFF; |
| 562 | v[2] = (V ) & 0xFF; |
| 563 | } |
| 564 | inline operator Type (void) const |
| 565 | { |
| 566 | return (v[0] << 16) |
| 567 | + (v[1] << 8) |
| 568 | + (v[2] ); |
| 569 | } |
Behdad Esfahbod | 5a5640d | 2014-10-14 21:26:13 -0700 | [diff] [blame] | 570 | private: uint8_t v[3]; |
| 571 | }; |
| 572 | template <typename Type> |
Behdad Esfahbod | d7bf473 | 2011-08-05 18:18:21 -0400 | [diff] [blame] | 573 | struct BEInt<Type, 4> |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 574 | { |
| 575 | public: |
Behdad Esfahbod | 666b42f | 2014-10-14 21:24:59 -0700 | [diff] [blame] | 576 | inline void set (Type V) |
| 577 | { |
| 578 | v[0] = (V >> 24) & 0xFF; |
| 579 | v[1] = (V >> 16) & 0xFF; |
| 580 | v[2] = (V >> 8) & 0xFF; |
| 581 | v[3] = (V ) & 0xFF; |
| 582 | } |
| 583 | inline operator Type (void) const |
| 584 | { |
| 585 | return (v[0] << 24) |
| 586 | + (v[1] << 16) |
| 587 | + (v[2] << 8) |
| 588 | + (v[3] ); |
| 589 | } |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 590 | private: uint8_t v[4]; |
| 591 | }; |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 592 | |
Behdad Esfahbod | 2467c66 | 2010-04-21 23:11:45 -0400 | [diff] [blame] | 593 | /* Integer types in big-endian order and no alignment requirement */ |
Behdad Esfahbod | bd61bc1 | 2012-12-11 16:00:43 -0500 | [diff] [blame] | 594 | template <typename Type, unsigned int Size> |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 595 | struct IntType |
| 596 | { |
Behdad Esfahbod | 81408cd | 2010-07-23 14:46:57 -0400 | [diff] [blame] | 597 | inline void set (Type i) { v.set (i); } |
Behdad Esfahbod | 01c0161 | 2010-04-21 22:49:56 -0400 | [diff] [blame] | 598 | inline operator Type(void) const { return v; } |
Behdad Esfahbod | bbdd6fd | 2015-02-19 17:03:02 +0300 | [diff] [blame^] | 599 | inline bool operator == (const IntType<Type,Size> &o) const { return (Type) v == (Type) o.v; } |
| 600 | inline bool operator != (const IntType<Type,Size> &o) const { return !(*this == o); } |
Behdad Esfahbod | bd61bc1 | 2012-12-11 16:00:43 -0500 | [diff] [blame] | 601 | static inline int cmp (const IntType<Type,Size> *a, const IntType<Type,Size> *b) { return b->cmp (*a); } |
Behdad Esfahbod | 37de2d5 | 2015-02-19 16:55:51 +0300 | [diff] [blame] | 602 | inline int cmp (IntType<Type,Size> va) const { Type a = va; return cmp (va); } |
Behdad Esfahbod | 88a399a | 2015-02-19 16:57:12 +0300 | [diff] [blame] | 603 | inline int cmp (Type a) const |
| 604 | { |
| 605 | Type b = v; |
| 606 | if (sizeof (Type) < sizeof (int)) |
| 607 | return (int) a - (int) b; |
| 608 | else |
| 609 | return a < b ? -1 : a == b ? 0 : +1; |
| 610 | } |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 611 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 612 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 613 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 614 | return TRACE_RETURN (likely (c->check_struct (this))); |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 615 | } |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 616 | protected: |
Behdad Esfahbod | bd61bc1 | 2012-12-11 16:00:43 -0500 | [diff] [blame] | 617 | BEInt<Type, Size> v; |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 618 | public: |
Behdad Esfahbod | bd61bc1 | 2012-12-11 16:00:43 -0500 | [diff] [blame] | 619 | DEFINE_SIZE_STATIC (Size); |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 620 | }; |
| 621 | |
Behdad Esfahbod | b7878cd | 2014-05-13 21:47:51 -0400 | [diff] [blame] | 622 | typedef uint8_t BYTE; /* 8-bit unsigned integer. */ |
Behdad Esfahbod | bd61bc1 | 2012-12-11 16:00:43 -0500 | [diff] [blame] | 623 | typedef IntType<uint16_t, 2> USHORT; /* 16-bit unsigned integer. */ |
| 624 | typedef IntType<int16_t, 2> SHORT; /* 16-bit signed integer. */ |
| 625 | typedef IntType<uint32_t, 4> ULONG; /* 32-bit unsigned integer. */ |
| 626 | typedef IntType<int32_t, 4> LONG; /* 32-bit signed integer. */ |
| 627 | typedef IntType<uint32_t, 3> UINT24; /* 24-bit unsigned integer. */ |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 628 | |
Behdad Esfahbod | ae9877d | 2011-08-17 14:43:45 +0200 | [diff] [blame] | 629 | /* 16-bit signed integer (SHORT) that describes a quantity in FUnits. */ |
| 630 | typedef SHORT FWORD; |
| 631 | |
| 632 | /* 16-bit unsigned integer (USHORT) that describes a quantity in FUnits. */ |
| 633 | typedef USHORT UFWORD; |
| 634 | |
Behdad Esfahbod | e29caf3 | 2010-05-19 11:47:17 -0400 | [diff] [blame] | 635 | /* Date represented in number of seconds since 12:00 midnight, January 1, |
| 636 | * 1904. The value is represented as a signed 64-bit integer. */ |
| 637 | struct LONGDATETIME |
| 638 | { |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 639 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 640 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 641 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 642 | return TRACE_RETURN (likely (c->check_struct (this))); |
Behdad Esfahbod | e29caf3 | 2010-05-19 11:47:17 -0400 | [diff] [blame] | 643 | } |
Behdad Esfahbod | 6775da3 | 2014-01-23 14:18:49 -0500 | [diff] [blame] | 644 | protected: |
Behdad Esfahbod | e29caf3 | 2010-05-19 11:47:17 -0400 | [diff] [blame] | 645 | LONG major; |
| 646 | ULONG minor; |
| 647 | public: |
| 648 | DEFINE_SIZE_STATIC (8); |
| 649 | }; |
| 650 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 651 | /* Array of four uint8s (length = 32 bits) used to identify a script, language |
| 652 | * system, feature, or baseline */ |
Behdad Esfahbod | 20cc86b | 2009-05-25 02:41:49 -0400 | [diff] [blame] | 653 | struct Tag : ULONG |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 654 | { |
Behdad Esfahbod | befc022 | 2006-12-25 09:14:52 -0500 | [diff] [blame] | 655 | /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */ |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 656 | inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); } |
| 657 | inline operator char* (void) { return reinterpret_cast<char *> (&this->v); } |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 658 | public: |
| 659 | DEFINE_SIZE_STATIC (4); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 660 | }; |
Behdad Esfahbod | 65f46b0 | 2010-05-06 19:35:19 -0400 | [diff] [blame] | 661 | DEFINE_NULL_DATA (Tag, " "); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 662 | |
| 663 | /* Glyph index number, same as uint16 (length = 16 bits) */ |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 664 | typedef USHORT GlyphID; |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 665 | |
Behdad Esfahbod | b5db4f1 | 2010-05-10 22:22:22 -0400 | [diff] [blame] | 666 | /* Script/language-system/feature index */ |
| 667 | struct Index : USHORT { |
Behdad Esfahbod | 7627100 | 2014-07-11 14:54:42 -0400 | [diff] [blame] | 668 | static const unsigned int NOT_FOUND_INDEX = 0xFFFFu; |
Behdad Esfahbod | b5db4f1 | 2010-05-10 22:22:22 -0400 | [diff] [blame] | 669 | }; |
| 670 | DEFINE_NULL_DATA (Index, "\xff\xff"); |
| 671 | |
Behdad Esfahbod | 99d2817 | 2014-06-27 15:12:52 -0400 | [diff] [blame] | 672 | /* Offset, Null offset = 0 */ |
| 673 | template <typename Type=USHORT> |
| 674 | struct Offset : Type |
Behdad Esfahbod | e95e031 | 2013-01-08 16:15:46 -0600 | [diff] [blame] | 675 | { |
| 676 | inline bool is_null (void) const { return 0 == *this; } |
| 677 | public: |
Behdad Esfahbod | 0394ec1 | 2014-06-27 14:40:35 -0400 | [diff] [blame] | 678 | DEFINE_SIZE_STATIC (sizeof(Type)); |
Behdad Esfahbod | e95e031 | 2013-01-08 16:15:46 -0600 | [diff] [blame] | 679 | }; |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 680 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 681 | |
| 682 | /* CheckSum */ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 683 | struct CheckSum : ULONG |
| 684 | { |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 685 | /* This is reference implementation from the spec. */ |
| 686 | static inline uint32_t CalcTableChecksum (const ULONG *Table, uint32_t Length) |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 687 | { |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 688 | uint32_t Sum = 0L; |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 689 | const ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size; |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 690 | |
| 691 | while (Table < EndPtr) |
| 692 | Sum += *Table++; |
| 693 | return Sum; |
| 694 | } |
Behdad Esfahbod | 05bad3b | 2013-07-21 17:05:02 -0400 | [diff] [blame] | 695 | |
| 696 | /* Note: data should be 4byte aligned and have 4byte padding at the end. */ |
| 697 | inline void set_for_data (const void *data, unsigned int length) |
| 698 | { set (CalcTableChecksum ((const ULONG *) data, length)); } |
| 699 | |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 700 | public: |
| 701 | DEFINE_SIZE_STATIC (4); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 702 | }; |
| 703 | |
| 704 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 705 | /* |
| 706 | * Version Numbers |
| 707 | */ |
| 708 | |
Behdad Esfahbod | 87fcdcb | 2009-05-24 01:03:24 -0400 | [diff] [blame] | 709 | struct FixedVersion |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 710 | { |
Behdad Esfahbod | 4f28fbd | 2011-05-31 12:33:11 -0400 | [diff] [blame] | 711 | inline uint32_t to_int (void) const { return (major << 16) + minor; } |
Behdad Esfahbod | 96908b8 | 2009-05-24 12:30:40 -0400 | [diff] [blame] | 712 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 713 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 714 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 715 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 716 | return TRACE_RETURN (c->check_struct (this)); |
Behdad Esfahbod | cd3827e | 2009-08-04 02:09:34 -0400 | [diff] [blame] | 717 | } |
| 718 | |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 719 | USHORT major; |
Behdad Esfahbod | 87fcdcb | 2009-05-24 01:03:24 -0400 | [diff] [blame] | 720 | USHORT minor; |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 721 | public: |
| 722 | DEFINE_SIZE_STATIC (4); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 723 | }; |
| 724 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 725 | |
| 726 | |
| 727 | /* |
Behdad Esfahbod | 99d2817 | 2014-06-27 15:12:52 -0400 | [diff] [blame] | 728 | * Template subclasses of Offset that do the dereferencing. |
Behdad Esfahbod | f0abcd6 | 2010-05-02 18:14:25 -0400 | [diff] [blame] | 729 | * Use: (base+offset) |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 730 | */ |
| 731 | |
Behdad Esfahbod | 9da552d | 2014-06-27 15:09:42 -0400 | [diff] [blame] | 732 | template <typename Type, typename OffsetType=USHORT> |
Behdad Esfahbod | 99d2817 | 2014-06-27 15:12:52 -0400 | [diff] [blame] | 733 | struct OffsetTo : Offset<OffsetType> |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 734 | { |
Behdad Esfahbod | 00e23fc | 2010-04-20 23:50:45 -0400 | [diff] [blame] | 735 | inline const Type& operator () (const void *base) const |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 736 | { |
| 737 | unsigned int offset = *this; |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 738 | if (unlikely (!offset)) return Null(Type); |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 739 | return StructAtOffset<Type> (base, offset); |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 740 | } |
Behdad Esfahbod | bc5be24 | 2012-09-01 20:48:22 -0400 | [diff] [blame] | 741 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 742 | inline Type& serialize (hb_serialize_context_t *c, const void *base) |
Behdad Esfahbod | bc5be24 | 2012-09-01 20:48:22 -0400 | [diff] [blame] | 743 | { |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 744 | Type *t = c->start_embed<Type> (); |
Behdad Esfahbod | 4912030 | 2012-09-03 20:58:03 -0400 | [diff] [blame] | 745 | this->set ((char *) t - (char *) base); /* TODO(serialize) Overflow? */ |
Behdad Esfahbod | abcc5ac | 2012-09-01 21:30:17 -0400 | [diff] [blame] | 746 | return *t; |
Behdad Esfahbod | bc5be24 | 2012-09-01 20:48:22 -0400 | [diff] [blame] | 747 | } |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 748 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 749 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 750 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 751 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 752 | if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false); |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 753 | unsigned int offset = *this; |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 754 | if (unlikely (!offset)) return TRACE_RETURN (true); |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 755 | const Type &obj = StructAtOffset<Type> (base, offset); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 756 | return TRACE_RETURN (likely (obj.sanitize (c)) || neuter (c)); |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 757 | } |
Behdad Esfahbod | 4a446ac | 2010-05-04 22:46:21 -0400 | [diff] [blame] | 758 | template <typename T> |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 759 | inline bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const |
| 760 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 761 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 762 | if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false); |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 763 | unsigned int offset = *this; |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 764 | if (unlikely (!offset)) return TRACE_RETURN (true); |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 765 | const Type &obj = StructAtOffset<Type> (base, offset); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 766 | return TRACE_RETURN (likely (obj.sanitize (c, user_data)) || neuter (c)); |
Behdad Esfahbod | c9f1468 | 2010-05-04 14:38:08 -0400 | [diff] [blame] | 767 | } |
| 768 | |
Behdad Esfahbod | c9f1468 | 2010-05-04 14:38:08 -0400 | [diff] [blame] | 769 | /* Set the offset to Null */ |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 770 | inline bool neuter (hb_sanitize_context_t *c) const { |
Behdad Esfahbod | 51f5635 | 2014-06-04 18:42:32 -0400 | [diff] [blame] | 771 | return c->try_set (this, 0); |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 772 | } |
Behdad Esfahbod | 586b606 | 2014-06-27 15:39:47 -0400 | [diff] [blame] | 773 | DEFINE_SIZE_STATIC (sizeof(OffsetType)); |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 774 | }; |
| 775 | template <typename Base, typename OffsetType, typename Type> |
Behdad Esfahbod | 0d1b341 | 2014-06-26 19:13:34 -0400 | [diff] [blame] | 776 | static inline const Type& operator + (const Base &base, const OffsetTo<Type, OffsetType> &offset) { return offset (base); } |
Behdad Esfahbod | bc5be24 | 2012-09-01 20:48:22 -0400 | [diff] [blame] | 777 | template <typename Base, typename OffsetType, typename Type> |
Behdad Esfahbod | 0d1b341 | 2014-06-26 19:13:34 -0400 | [diff] [blame] | 778 | static inline Type& operator + (Base &base, OffsetTo<Type, OffsetType> &offset) { return offset (base); } |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 779 | |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 780 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 781 | /* |
| 782 | * Array Types |
| 783 | */ |
| 784 | |
Behdad Esfahbod | 9da552d | 2014-06-27 15:09:42 -0400 | [diff] [blame] | 785 | /* An array with a number of elements. */ |
| 786 | template <typename Type, typename LenType=USHORT> |
| 787 | struct ArrayOf |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 788 | { |
Behdad Esfahbod | 4f5f1c3 | 2010-04-22 00:27:39 -0400 | [diff] [blame] | 789 | const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const |
Behdad Esfahbod | 48de373 | 2009-11-04 16:59:50 -0500 | [diff] [blame] | 790 | { |
| 791 | unsigned int count = len; |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 792 | if (unlikely (start_offset > count)) |
Behdad Esfahbod | 48de373 | 2009-11-04 16:59:50 -0500 | [diff] [blame] | 793 | count = 0; |
| 794 | else |
| 795 | count -= start_offset; |
| 796 | count = MIN (count, *pcount); |
| 797 | *pcount = count; |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 798 | return array + start_offset; |
Behdad Esfahbod | 48de373 | 2009-11-04 16:59:50 -0500 | [diff] [blame] | 799 | } |
| 800 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 801 | inline const Type& operator [] (unsigned int i) const |
| 802 | { |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 803 | if (unlikely (i >= len)) return Null(Type); |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 804 | return array[i]; |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 805 | } |
Behdad Esfahbod | 9f2348d | 2012-08-29 21:08:59 -0400 | [diff] [blame] | 806 | inline Type& operator [] (unsigned int i) |
| 807 | { |
| 808 | return array[i]; |
| 809 | } |
Behdad Esfahbod | 7f97d2c | 2010-10-01 18:58:50 -0400 | [diff] [blame] | 810 | inline unsigned int get_size (void) const |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 811 | { return len.static_size + len * Type::static_size; } |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 812 | |
Behdad Esfahbod | c61be03 | 2012-09-01 21:43:38 -0400 | [diff] [blame] | 813 | inline bool serialize (hb_serialize_context_t *c, |
Behdad Esfahbod | 1f07e33 | 2012-09-03 23:28:34 -0400 | [diff] [blame] | 814 | unsigned int items_len) |
| 815 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 816 | TRACE_SERIALIZE (this); |
Behdad Esfahbod | 1f07e33 | 2012-09-03 23:28:34 -0400 | [diff] [blame] | 817 | if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); |
| 818 | len.set (items_len); /* TODO(serialize) Overflow? */ |
| 819 | if (unlikely (!c->extend (*this))) return TRACE_RETURN (false); |
| 820 | return TRACE_RETURN (true); |
| 821 | } |
| 822 | |
| 823 | inline bool serialize (hb_serialize_context_t *c, |
Behdad Esfahbod | a930c68 | 2012-09-04 18:17:57 -0400 | [diff] [blame] | 824 | Supplier<Type> &items, |
Behdad Esfahbod | c61be03 | 2012-09-01 21:43:38 -0400 | [diff] [blame] | 825 | unsigned int items_len) |
| 826 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 827 | TRACE_SERIALIZE (this); |
Behdad Esfahbod | 715e03b | 2012-09-04 20:10:17 -0400 | [diff] [blame] | 828 | if (unlikely (!serialize (c, items_len))) return TRACE_RETURN (false); |
| 829 | for (unsigned int i = 0; i < items_len; i++) |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 830 | array[i] = items[i]; |
Behdad Esfahbod | a930c68 | 2012-09-04 18:17:57 -0400 | [diff] [blame] | 831 | items.advance (items_len); |
Behdad Esfahbod | c61be03 | 2012-09-01 21:43:38 -0400 | [diff] [blame] | 832 | return TRACE_RETURN (true); |
| 833 | } |
| 834 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 835 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 836 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 837 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 838 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 839 | |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 840 | /* Note: for structs that do not reference other structs, |
| 841 | * we do not need to call their sanitize() as we already did |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 842 | * a bound check on the aggregate array size. We just include |
| 843 | * a small unreachable expression to make sure the structs |
| 844 | * pointed to do have a simple sanitize(), ie. they do not |
| 845 | * reference other structs via offsets. |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 846 | */ |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 847 | (void) (false && array[0].sanitize (c)); |
| 848 | |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 849 | return TRACE_RETURN (true); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 850 | } |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 851 | inline bool sanitize (hb_sanitize_context_t *c, const void *base) const |
| 852 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 853 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 854 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
Behdad Esfahbod | e6ab2c5 | 2009-08-04 10:23:01 -0400 | [diff] [blame] | 855 | unsigned int count = len; |
| 856 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 857 | if (unlikely (!array[i].sanitize (c, base))) |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 858 | return TRACE_RETURN (false); |
| 859 | return TRACE_RETURN (true); |
Behdad Esfahbod | e6ab2c5 | 2009-08-04 10:23:01 -0400 | [diff] [blame] | 860 | } |
Behdad Esfahbod | 4a446ac | 2010-05-04 22:46:21 -0400 | [diff] [blame] | 861 | template <typename T> |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 862 | inline bool sanitize (hb_sanitize_context_t *c, const void *base, T user_data) const |
| 863 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 864 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 865 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 866 | unsigned int count = len; |
| 867 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 868 | if (unlikely (!array[i].sanitize (c, base, user_data))) |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 869 | return TRACE_RETURN (false); |
| 870 | return TRACE_RETURN (true); |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 871 | } |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 872 | |
Behdad Esfahbod | c7074b8 | 2014-05-08 18:24:31 -0400 | [diff] [blame] | 873 | template <typename SearchType> |
Behdad Esfahbod | df554af | 2014-06-19 15:39:18 -0400 | [diff] [blame] | 874 | inline int lsearch (const SearchType &x) const |
Behdad Esfahbod | c7074b8 | 2014-05-08 18:24:31 -0400 | [diff] [blame] | 875 | { |
Behdad Esfahbod | c7074b8 | 2014-05-08 18:24:31 -0400 | [diff] [blame] | 876 | unsigned int count = len; |
| 877 | for (unsigned int i = 0; i < count; i++) |
| 878 | if (!this->array[i].cmp (x)) |
| 879 | return i; |
| 880 | return -1; |
| 881 | } |
| 882 | |
Behdad Esfahbod | 30fa282 | 2010-05-04 14:28:18 -0400 | [diff] [blame] | 883 | private: |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 884 | inline bool sanitize_shallow (hb_sanitize_context_t *c) const |
| 885 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 886 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 887 | return TRACE_RETURN (c->check_struct (this) && c->check_array (this, Type::static_size, len)); |
Behdad Esfahbod | 30fa282 | 2010-05-04 14:28:18 -0400 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | public: |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 891 | LenType len; |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 892 | Type array[VAR]; |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 893 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 894 | DEFINE_SIZE_ARRAY (sizeof (LenType), array); |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 895 | }; |
| 896 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 897 | /* Array of Offset's */ |
| 898 | template <typename Type> |
| 899 | struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {}; |
| 900 | |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 901 | /* Array of offsets relative to the beginning of the array itself. */ |
| 902 | template <typename Type> |
| 903 | struct OffsetListOf : OffsetArrayOf<Type> |
| 904 | { |
| 905 | inline const Type& operator [] (unsigned int i) const |
| 906 | { |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 907 | if (unlikely (i >= this->len)) return Null(Type); |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 908 | return this+this->array[i]; |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 909 | } |
| 910 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 911 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 912 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 913 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 914 | return TRACE_RETURN (OffsetArrayOf<Type>::sanitize (c, this)); |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 915 | } |
Behdad Esfahbod | 4a446ac | 2010-05-04 22:46:21 -0400 | [diff] [blame] | 916 | template <typename T> |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 917 | inline bool sanitize (hb_sanitize_context_t *c, T user_data) const |
| 918 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 919 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 920 | return TRACE_RETURN (OffsetArrayOf<Type>::sanitize (c, this, user_data)); |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 921 | } |
| 922 | }; |
| 923 | |
| 924 | |
Behdad Esfahbod | 51d9ba0 | 2014-06-27 15:27:15 -0400 | [diff] [blame] | 925 | /* An array starting at second element. */ |
| 926 | template <typename Type, typename LenType=USHORT> |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 927 | struct HeadlessArrayOf |
| 928 | { |
| 929 | inline const Type& operator [] (unsigned int i) const |
| 930 | { |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 931 | if (unlikely (i >= len || !i)) return Null(Type); |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 932 | return array[i-1]; |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 933 | } |
Behdad Esfahbod | 7f97d2c | 2010-10-01 18:58:50 -0400 | [diff] [blame] | 934 | inline unsigned int get_size (void) const |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 935 | { return len.static_size + (len ? len - 1 : 0) * Type::static_size; } |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 936 | |
Behdad Esfahbod | a930c68 | 2012-09-04 18:17:57 -0400 | [diff] [blame] | 937 | inline bool serialize (hb_serialize_context_t *c, |
| 938 | Supplier<Type> &items, |
| 939 | unsigned int items_len) |
| 940 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 941 | TRACE_SERIALIZE (this); |
Behdad Esfahbod | a930c68 | 2012-09-04 18:17:57 -0400 | [diff] [blame] | 942 | if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); |
| 943 | len.set (items_len); /* TODO(serialize) Overflow? */ |
Behdad Esfahbod | a930c68 | 2012-09-04 18:17:57 -0400 | [diff] [blame] | 944 | if (unlikely (!items_len)) return TRACE_RETURN (true); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 945 | if (unlikely (!c->extend (*this))) return TRACE_RETURN (false); |
| 946 | for (unsigned int i = 0; i < items_len - 1; i++) |
| 947 | array[i] = items[i]; |
Behdad Esfahbod | a930c68 | 2012-09-04 18:17:57 -0400 | [diff] [blame] | 948 | items.advance (items_len - 1); |
| 949 | return TRACE_RETURN (true); |
| 950 | } |
| 951 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 952 | inline bool sanitize_shallow (hb_sanitize_context_t *c) const |
| 953 | { |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 954 | return c->check_struct (this) |
| 955 | && c->check_array (this, Type::static_size, len); |
Behdad Esfahbod | e5546a4 | 2010-04-22 00:45:42 -0400 | [diff] [blame] | 956 | } |
| 957 | |
Behdad Esfahbod | de2118e | 2015-02-17 17:27:44 +0300 | [diff] [blame] | 958 | inline bool sanitize (hb_sanitize_context_t *c) const |
| 959 | { |
Behdad Esfahbod | be218c6 | 2012-11-23 15:32:14 -0500 | [diff] [blame] | 960 | TRACE_SANITIZE (this); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 961 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 962 | |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 963 | /* Note: for structs that do not reference other structs, |
| 964 | * we do not need to call their sanitize() as we already did |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 965 | * a bound check on the aggregate array size. We just include |
| 966 | * a small unreachable expression to make sure the structs |
| 967 | * pointed to do have a simple sanitize(), ie. they do not |
| 968 | * reference other structs via offsets. |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 969 | */ |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 970 | (void) (false && array[0].sanitize (c)); |
| 971 | |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 972 | return TRACE_RETURN (true); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 973 | } |
| 974 | |
Behdad Esfahbod | 51d9ba0 | 2014-06-27 15:27:15 -0400 | [diff] [blame] | 975 | LenType len; |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 976 | Type array[VAR]; |
Behdad Esfahbod | ed07422 | 2010-05-10 18:08:46 -0400 | [diff] [blame] | 977 | public: |
Behdad Esfahbod | 51d9ba0 | 2014-06-27 15:27:15 -0400 | [diff] [blame] | 978 | DEFINE_SIZE_ARRAY (sizeof (LenType), array); |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 979 | }; |
| 980 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 981 | |
Behdad Esfahbod | cc8a4ab | 2010-07-08 00:40:04 -0400 | [diff] [blame] | 982 | /* An array with sorted elements. Supports binary searching. */ |
Behdad Esfahbod | 9da552d | 2014-06-27 15:09:42 -0400 | [diff] [blame] | 983 | template <typename Type, typename LenType=USHORT> |
| 984 | struct SortedArrayOf : ArrayOf<Type, LenType> |
Behdad Esfahbod | 40a4797 | 2014-05-08 18:21:04 -0400 | [diff] [blame] | 985 | { |
Behdad Esfahbod | cc8a4ab | 2010-07-08 00:40:04 -0400 | [diff] [blame] | 986 | template <typename SearchType> |
Behdad Esfahbod | df554af | 2014-06-19 15:39:18 -0400 | [diff] [blame] | 987 | inline int bsearch (const SearchType &x) const |
Behdad Esfahbod | 8659c63 | 2013-04-19 14:33:17 -0400 | [diff] [blame] | 988 | { |
| 989 | /* Hand-coded bsearch here since this is in the hot inner loop. */ |
| 990 | int min = 0, max = (int) this->len - 1; |
| 991 | while (min <= max) |
| 992 | { |
| 993 | int mid = (min + max) / 2; |
| 994 | int c = this->array[mid].cmp (x); |
| 995 | if (c < 0) |
| 996 | max = mid - 1; |
| 997 | else if (c > 0) |
| 998 | min = mid + 1; |
| 999 | else |
| 1000 | return mid; |
Behdad Esfahbod | 99159e5 | 2012-06-09 00:50:40 -0400 | [diff] [blame] | 1001 | } |
Behdad Esfahbod | 8659c63 | 2013-04-19 14:33:17 -0400 | [diff] [blame] | 1002 | return -1; |
Behdad Esfahbod | cc8a4ab | 2010-07-08 00:40:04 -0400 | [diff] [blame] | 1003 | } |
Behdad Esfahbod | cc8a4ab | 2010-07-08 00:40:04 -0400 | [diff] [blame] | 1004 | }; |
| 1005 | |
| 1006 | |
Behdad Esfahbod | 7d52e66 | 2012-11-16 18:49:54 -0800 | [diff] [blame] | 1007 | } /* namespace OT */ |
Behdad Esfahbod | 7c8e844 | 2012-08-28 17:57:49 -0400 | [diff] [blame] | 1008 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 1009 | |
Behdad Esfahbod | 1e91434 | 2009-11-04 18:12:09 -0500 | [diff] [blame] | 1010 | #endif /* HB_OPEN_TYPE_PRIVATE_HH */ |