Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | f9b3772 | 2010-04-20 15:51:53 -0400 | [diff] [blame] | 2 | * Copyright (C) 2007,2008,2009,2010 Red Hat, Inc. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 3 | * |
Behdad Esfahbod | c755cb3 | 2010-04-22 00:11:43 -0400 | [diff] [blame] | 4 | * This is part of HarfBuzz, a text shaping library. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 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 Esfahbod | 5f5b24f | 2009-08-02 20:03:12 -0400 | [diff] [blame] | 27 | #ifndef HB_OPEN_TYPES_PRIVATE_HH |
| 28 | #define HB_OPEN_TYPES_PRIVATE_HH |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 29 | |
Behdad Esfahbod | 2098a02 | 2009-08-02 19:57:00 -0400 | [diff] [blame] | 30 | #include "hb-private.h" |
Behdad Esfahbod | 12c4568 | 2006-12-28 06:10:59 -0500 | [diff] [blame] | 31 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 32 | #include "hb-blob.h" |
| 33 | |
Behdad Esfahbod | a16ecbf | 2008-01-23 17:01:55 -0500 | [diff] [blame] | 34 | |
Behdad Esfahbod | c85c362 | 2010-04-21 23:12:54 -0400 | [diff] [blame] | 35 | /* Table/script/language-system/feature/... not found */ |
Behdad Esfahbod | 706ab25 | 2008-01-28 05:58:50 -0500 | [diff] [blame] | 36 | #define NO_INDEX ((unsigned int) 0xFFFF) |
Behdad Esfahbod | 5a0b791 | 2009-04-16 04:45:30 -0400 | [diff] [blame] | 37 | |
Behdad Esfahbod | 706ab25 | 2008-01-28 05:58:50 -0500 | [diff] [blame] | 38 | |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 39 | |
Behdad Esfahbod | 196598b | 2009-08-04 11:04:32 -0400 | [diff] [blame] | 40 | /* |
| 41 | * Casts |
| 42 | */ |
| 43 | |
Behdad Esfahbod | 0dfcc13 | 2010-04-21 23:41:26 -0400 | [diff] [blame] | 44 | /* Cast to "const char *" and "char *" */ |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 45 | template <typename Type> |
| 46 | inline const char * CharP (const Type* X) |
| 47 | { return reinterpret_cast<const char *>(X); } |
| 48 | template <typename Type> |
| 49 | inline char * CharP (Type* X) |
| 50 | { return reinterpret_cast<char *>(X); } |
Behdad Esfahbod | 2b5a59c | 2009-08-04 11:38:50 -0400 | [diff] [blame] | 51 | |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 52 | /* Cast to struct T, reference to reference */ |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 53 | template<typename Type, typename TObject> |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 54 | inline const Type& CastR(const TObject &X) |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 55 | { return reinterpret_cast<const Type&> (X); } |
| 56 | template<typename Type, typename TObject> |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 57 | inline Type& CastR(TObject &X) |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 58 | { return reinterpret_cast<Type&> (X); } |
Behdad Esfahbod | 196598b | 2009-08-04 11:04:32 -0400 | [diff] [blame] | 59 | |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 60 | /* Cast to struct T, pointer to pointer */ |
| 61 | template<typename Type, typename TObject> |
| 62 | inline const Type* CastP(const TObject *X) |
| 63 | { return reinterpret_cast<const Type*> (X); } |
| 64 | template<typename Type, typename TObject> |
| 65 | inline Type* CastP(TObject *X) |
| 66 | { return reinterpret_cast<Type*> (X); } |
| 67 | |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 68 | /* StructAtOffset<T>(X,Ofs) returns the struct T& that is placed at memory |
| 69 | * location of X plus Ofs bytes. */ |
| 70 | template<typename Type, typename TObject> |
| 71 | inline const Type& StructAtOffset(const TObject &X, unsigned int offset) |
Behdad Esfahbod | d632ec4 | 2010-04-22 18:33:12 -0400 | [diff] [blame] | 72 | { return * reinterpret_cast<const Type*> (CharP(&X) + offset); } |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 73 | template<typename Type, typename TObject> |
| 74 | inline Type& StructAtOffset(TObject &X, unsigned int offset) |
Behdad Esfahbod | d632ec4 | 2010-04-22 18:33:12 -0400 | [diff] [blame] | 75 | { return * reinterpret_cast<Type*> (CharP(&X) + offset); } |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 76 | |
Behdad Esfahbod | 2e2f43e | 2010-04-21 22:30:36 -0400 | [diff] [blame] | 77 | /* StructAfter<T>(X) returns the struct T& that is placed after X. |
Behdad Esfahbod | 29c3f5e | 2010-04-21 23:01:00 -0400 | [diff] [blame] | 78 | * Works with X of variable size also. X must implement get_size() */ |
Behdad Esfahbod | e961c86 | 2010-04-21 15:56:11 -0400 | [diff] [blame] | 79 | template<typename Type, typename TObject> |
| 80 | inline const Type& StructAfter(const TObject &X) |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 81 | { return StructAtOffset<Type>(X, X.get_size()); } |
Behdad Esfahbod | e961c86 | 2010-04-21 15:56:11 -0400 | [diff] [blame] | 82 | template<typename Type, typename TObject> |
| 83 | inline Type& StructAfter(TObject &X) |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 84 | { return StructAtOffset<Type>(X, X.get_size()); } |
| 85 | |
Behdad Esfahbod | e961c86 | 2010-04-21 15:56:11 -0400 | [diff] [blame] | 86 | |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 87 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 88 | /* |
Behdad Esfahbod | f0abcd6 | 2010-05-02 18:14:25 -0400 | [diff] [blame] | 89 | * Null objects |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 90 | */ |
| 91 | |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 92 | /* Global nul-content Null pool. Enlarge as necessary. */ |
Behdad Esfahbod | 9d36778 | 2010-04-21 00:32:47 -0400 | [diff] [blame] | 93 | static const void *_NullPool[32 / sizeof (void *)]; |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 94 | |
| 95 | /* Generic template for nul-content sizeof-sized Null objects. */ |
| 96 | template <typename Type> |
Behdad Esfahbod | 9d36778 | 2010-04-21 00:32:47 -0400 | [diff] [blame] | 97 | static inline const Type& Null () { |
| 98 | ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool)); |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 99 | return *CastP<Type> (_NullPool); |
Behdad Esfahbod | 9d36778 | 2010-04-21 00:32:47 -0400 | [diff] [blame] | 100 | } |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 101 | |
| 102 | /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */ |
| 103 | #define DEFINE_NULL_DATA(Type, size, data) \ |
Behdad Esfahbod | 565c80b | 2010-04-22 10:26:35 -0400 | [diff] [blame] | 104 | static const char _Null##Type[size + 1] = data; /* +1 is for nul-termination in data */ \ |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 105 | template <> \ |
Behdad Esfahbod | 9d36778 | 2010-04-21 00:32:47 -0400 | [diff] [blame] | 106 | inline const Type& Null<Type> () { \ |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 107 | return *CastP<Type> (_Null##Type); \ |
Behdad Esfahbod | 565c80b | 2010-04-22 10:26:35 -0400 | [diff] [blame] | 108 | } /* The following line really exists such that we end in a place needing semicolon */ \ |
| 109 | ASSERT_STATIC (sizeof (Type) + 1 <= sizeof (_Null##Type)) |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 110 | |
| 111 | /* Accessor macro. */ |
Behdad Esfahbod | 9d36778 | 2010-04-21 00:32:47 -0400 | [diff] [blame] | 112 | #define Null(Type) Null<Type>() |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 113 | |
| 114 | |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 115 | |
| 116 | /* |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 117 | * Sanitize |
| 118 | */ |
| 119 | |
Behdad Esfahbod | 95e2024 | 2009-08-28 16:31:20 -0400 | [diff] [blame] | 120 | #ifndef HB_DEBUG_SANITIZE |
Behdad Esfahbod | 807c5b0 | 2010-04-28 20:25:22 -0400 | [diff] [blame] | 121 | #define HB_DEBUG_SANITIZE HB_DEBUG+0 |
Behdad Esfahbod | 95e2024 | 2009-08-28 16:31:20 -0400 | [diff] [blame] | 122 | #endif |
| 123 | |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 124 | #define TRACE_SANITIZE() \ |
| 125 | HB_STMT_START { \ |
| 126 | if (HB_DEBUG_SANITIZE) \ |
Behdad Esfahbod | 7d3a126 | 2010-04-29 13:54:01 -0400 | [diff] [blame] | 127 | _hb_trace ("SANITIZE", HB_FUNC, this, sanitize_depth, HB_DEBUG_SANITIZE); \ |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 128 | } HB_STMT_END |
Behdad Esfahbod | 807c5b0 | 2010-04-28 20:25:22 -0400 | [diff] [blame] | 129 | |
| 130 | |
Behdad Esfahbod | 4189550 | 2009-08-14 16:25:33 -0400 | [diff] [blame] | 131 | #define SANITIZE_ARG_DEF \ |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 132 | hb_sanitize_context_t *context, \ |
Behdad Esfahbod | 33d13fd | 2010-04-29 13:56:44 -0400 | [diff] [blame] | 133 | unsigned int sanitize_depth HB_UNUSED |
Behdad Esfahbod | 4189550 | 2009-08-14 16:25:33 -0400 | [diff] [blame] | 134 | #define SANITIZE_ARG \ |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 135 | context, \ |
| 136 | (HB_DEBUG_SANITIZE ? sanitize_depth + 1 : 0) |
Behdad Esfahbod | 173fde7 | 2010-04-29 01:47:30 -0400 | [diff] [blame] | 137 | |
Behdad Esfahbod | 1376fb7 | 2010-04-29 02:19:21 -0400 | [diff] [blame] | 138 | struct hb_sanitize_context_t |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 139 | { |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 140 | inline void init (hb_blob_t *blob) |
| 141 | { |
| 142 | this->blob = hb_blob_reference (blob); |
| 143 | this->start = hb_blob_lock (blob); |
| 144 | this->end = this->start + hb_blob_get_length (blob); |
| 145 | this->writable = hb_blob_is_writable (blob); |
| 146 | this->edit_count = 0; |
| 147 | |
| 148 | if (HB_DEBUG_SANITIZE) |
| 149 | fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n", |
| 150 | this->blob, this->start, this->end, this->end - this->start); |
| 151 | } |
| 152 | |
| 153 | inline void finish (void) |
| 154 | { |
| 155 | if (HB_DEBUG_SANITIZE) |
| 156 | fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n", |
| 157 | this->blob, this->start, this->end, this->edit_count); |
| 158 | |
| 159 | hb_blob_unlock (this->blob); |
| 160 | hb_blob_destroy (this->blob); |
| 161 | this->blob = NULL; |
| 162 | this->start = this->end = NULL; |
| 163 | } |
| 164 | |
| 165 | inline bool check (unsigned int sanitize_depth, |
| 166 | const char *base, |
| 167 | unsigned int len) const |
| 168 | { |
| 169 | bool ret = this->start <= base && |
| 170 | base <= this->end && |
| 171 | (unsigned int) (this->end - base) >= len; |
| 172 | |
| 173 | if (HB_DEBUG_SANITIZE && (int) sanitize_depth < (int) HB_DEBUG_SANITIZE) \ |
| 174 | fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \ |
| 175 | base, |
| 176 | sanitize_depth, sanitize_depth, |
| 177 | base, base+len, len, |
| 178 | this->start, this->end, |
| 179 | ret ? "pass" : "FAIL"); |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | inline bool check_array (unsigned int sanitize_depth, |
| 185 | const char *base, |
| 186 | unsigned int record_size, |
| 187 | unsigned int len) const |
| 188 | { |
| 189 | bool overflows = len >= ((unsigned int) -1) / record_size; |
| 190 | |
| 191 | |
| 192 | if (HB_DEBUG_SANITIZE && (int) sanitize_depth < (int) HB_DEBUG_SANITIZE) |
| 193 | fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \ |
| 194 | base, |
| 195 | sanitize_depth, sanitize_depth, |
| 196 | base, base + (record_size * len), record_size, len, (unsigned long) record_size * len, |
| 197 | this->start, this->end, |
| 198 | !overflows ? "does not overflow" : "OVERFLOWS FAIL"); |
| 199 | |
| 200 | return likely (!overflows) && this->check (sanitize_depth, base, record_size * len); |
| 201 | } |
| 202 | |
| 203 | inline bool can_edit (unsigned int sanitize_depth, |
| 204 | const char *base HB_UNUSED, |
| 205 | unsigned int len HB_UNUSED) |
| 206 | { |
| 207 | this->edit_count++; |
| 208 | |
| 209 | if (HB_DEBUG_SANITIZE && (int) sanitize_depth < (int) HB_DEBUG_SANITIZE) |
| 210 | fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \ |
| 211 | base, |
| 212 | sanitize_depth, sanitize_depth, |
| 213 | this->edit_count, |
| 214 | base, base+len, len, |
| 215 | this->start, this->end, |
| 216 | this->writable ? "granted" : "REJECTED"); |
| 217 | |
| 218 | return this->writable; |
| 219 | } |
| 220 | |
| 221 | inline const char *get_start (void) const { return start; } |
| 222 | inline const char *get_end (void) const { return end; } |
| 223 | inline bool is_writable (void) const { return writable; } |
| 224 | inline unsigned int get_edit_count (void) const { return this->edit_count; } |
| 225 | |
| 226 | inline void reset_edit_count (void) { this->edit_count = 0; } |
| 227 | |
| 228 | private: |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 229 | const char *start, *end; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 230 | bool writable; |
Behdad Esfahbod | 254933c | 2010-04-23 13:57:10 -0400 | [diff] [blame] | 231 | unsigned int edit_count; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 232 | hb_blob_t *blob; |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 233 | }; |
| 234 | |
Behdad Esfahbod | 1376fb7 | 2010-04-29 02:19:21 -0400 | [diff] [blame] | 235 | |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 236 | #define SANITIZE(X) likely ((X).sanitize (SANITIZE_ARG)) |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 237 | |
Behdad Esfahbod | bb029af | 2010-05-04 15:28:52 -0400 | [diff] [blame] | 238 | #define SANITIZE_WITH_BASE(B,X) likely ((X).sanitize (SANITIZE_ARG, CharP(B))) |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 239 | |
Behdad Esfahbod | 89da134 | 2010-05-04 15:01:45 -0400 | [diff] [blame] | 240 | #define SANITIZE_SELF() SANITIZE_MEM(this, sizeof (*this)) |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 241 | |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 242 | #define SANITIZE_MEM(B,L) likely (context->check (sanitize_depth, CharP(B), (L))) |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 243 | |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 244 | #define SANITIZE_ARRAY(A,S,L) likely (context->check_array (sanitize_depth, CharP(A), S, L)) |
Behdad Esfahbod | 815a73e | 2009-08-14 17:31:16 -0400 | [diff] [blame] | 245 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 246 | |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 247 | /* Template to sanitize an object. */ |
| 248 | template <typename Type> |
| 249 | struct Sanitizer |
| 250 | { |
| 251 | static hb_blob_t *sanitize (hb_blob_t *blob) { |
Behdad Esfahbod | 173fde7 | 2010-04-29 01:47:30 -0400 | [diff] [blame] | 252 | hb_sanitize_context_t context[1]; |
| 253 | unsigned int sanitize_depth = 0; |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 254 | bool sane; |
| 255 | |
Behdad Esfahbod | d0b6573 | 2009-08-06 18:34:47 -0400 | [diff] [blame] | 256 | /* TODO is_sane() stuff */ |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 257 | |
| 258 | retry: |
Behdad Esfahbod | fa03017 | 2010-04-29 13:48:26 -0400 | [diff] [blame] | 259 | if (HB_DEBUG_SANITIZE) |
Behdad Esfahbod | 7d3a126 | 2010-04-29 13:54:01 -0400 | [diff] [blame] | 260 | fprintf (stderr, "Sanitizer %p start %s\n", blob, HB_FUNC); |
Behdad Esfahbod | 4f3ad91 | 2009-08-04 23:01:23 -0400 | [diff] [blame] | 261 | |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 262 | context->init (blob); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 263 | |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 264 | Type *t = CastP<Type> (const_cast<char *> (context->get_start ())); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 265 | |
Behdad Esfahbod | 173fde7 | 2010-04-29 01:47:30 -0400 | [diff] [blame] | 266 | sane = t->sanitize (SANITIZE_ARG); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 267 | if (sane) { |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 268 | if (context->get_edit_count ()) { |
Behdad Esfahbod | fa03017 | 2010-04-29 13:48:26 -0400 | [diff] [blame] | 269 | if (HB_DEBUG_SANITIZE) |
| 270 | fprintf (stderr, "Sanitizer %p passed first round with %d edits; doing a second round %s\n", |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 271 | blob, context->get_edit_count (), HB_FUNC); |
Behdad Esfahbod | fa03017 | 2010-04-29 13:48:26 -0400 | [diff] [blame] | 272 | |
Behdad Esfahbod | 8b53461 | 2009-08-19 18:16:50 -0400 | [diff] [blame] | 273 | /* sanitize again to ensure no toe-stepping */ |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 274 | context->reset_edit_count (); |
Behdad Esfahbod | 173fde7 | 2010-04-29 01:47:30 -0400 | [diff] [blame] | 275 | sane = t->sanitize (SANITIZE_ARG); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 276 | if (context->get_edit_count ()) { |
Behdad Esfahbod | fa03017 | 2010-04-29 13:48:26 -0400 | [diff] [blame] | 277 | if (HB_DEBUG_SANITIZE) |
| 278 | fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n", |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 279 | blob, context->get_edit_count (), HB_FUNC); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 280 | sane = false; |
| 281 | } |
| 282 | } |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 283 | context->finish (); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 284 | } else { |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 285 | unsigned int edit_count = context->get_edit_count (); |
| 286 | context->finish (); |
Behdad Esfahbod | 977eeb7 | 2009-08-19 16:17:24 -0400 | [diff] [blame] | 287 | if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) { |
| 288 | /* ok, we made it writable by relocating. try again */ |
Behdad Esfahbod | fa03017 | 2010-04-29 13:48:26 -0400 | [diff] [blame] | 289 | if (HB_DEBUG_SANITIZE) |
Behdad Esfahbod | 7d3a126 | 2010-04-29 13:54:01 -0400 | [diff] [blame] | 290 | fprintf (stderr, "Sanitizer %p retry %s\n", blob, HB_FUNC); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 291 | goto retry; |
| 292 | } |
| 293 | } |
| 294 | |
Behdad Esfahbod | fa03017 | 2010-04-29 13:48:26 -0400 | [diff] [blame] | 295 | if (HB_DEBUG_SANITIZE) |
Behdad Esfahbod | 7d3a126 | 2010-04-29 13:54:01 -0400 | [diff] [blame] | 296 | fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", HB_FUNC); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 297 | if (sane) |
| 298 | return blob; |
| 299 | else { |
| 300 | hb_blob_destroy (blob); |
| 301 | return hb_blob_create_empty (); |
| 302 | } |
| 303 | } |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 304 | }; |
| 305 | |
Behdad Esfahbod | 2d15e72 | 2009-04-15 19:50:16 -0400 | [diff] [blame] | 306 | |
Behdad Esfahbod | f0abcd6 | 2010-05-02 18:14:25 -0400 | [diff] [blame] | 307 | |
| 308 | |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 309 | /* |
| 310 | * |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 311 | * The OpenType Font File: Data Types |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 312 | */ |
| 313 | |
| 314 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 315 | /* "The following data types are used in the OpenType font file. |
| 316 | * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */ |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 317 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 318 | /* |
| 319 | * Int types |
| 320 | */ |
| 321 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 322 | |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 323 | template <typename Type, int Bytes> class BEInt; |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 324 | |
Behdad Esfahbod | f1aaa2a | 2010-04-23 15:19:50 -0400 | [diff] [blame] | 325 | /* LONGTERMTODO: On machines allowing unaligned access, we can make the |
| 326 | * following tighter by using byteswap instructions on ints directly. */ |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 327 | template <typename Type> |
| 328 | class BEInt<Type, 2> |
| 329 | { |
| 330 | public: |
Behdad Esfahbod | 01c0161 | 2010-04-21 22:49:56 -0400 | [diff] [blame] | 331 | inline class BEInt<Type,2>& operator = (Type i) { hb_be_uint16_put (v,i); return *this; } |
| 332 | inline operator Type () const { return hb_be_uint16_get (v); } |
| 333 | inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_cmp (v, o.v); } |
| 334 | inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); } |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 335 | private: uint8_t v[2]; |
| 336 | }; |
| 337 | template <typename Type> |
| 338 | class BEInt<Type, 4> |
| 339 | { |
| 340 | public: |
Behdad Esfahbod | 01c0161 | 2010-04-21 22:49:56 -0400 | [diff] [blame] | 341 | inline class BEInt<Type,4>& operator = (Type i) { hb_be_uint32_put (v,i); return *this; } |
| 342 | inline operator Type () const { return hb_be_uint32_get (v); } |
| 343 | inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_cmp (v, o.v); } |
| 344 | inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); } |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 345 | private: uint8_t v[4]; |
| 346 | }; |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 347 | |
Behdad Esfahbod | 2467c66 | 2010-04-21 23:11:45 -0400 | [diff] [blame] | 348 | /* Integer types in big-endian order and no alignment requirement */ |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 349 | template <typename Type> |
| 350 | struct IntType |
| 351 | { |
| 352 | static inline unsigned int get_size () { return sizeof (Type); } |
Behdad Esfahbod | 01c0161 | 2010-04-21 22:49:56 -0400 | [diff] [blame] | 353 | inline void set (Type i) { v = i; } |
| 354 | inline operator Type(void) const { return v; } |
| 355 | inline bool operator == (const IntType<Type> &o) const { return v == o.v; } |
| 356 | inline bool operator != (const IntType<Type> &o) const { return v != o.v; } |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 357 | inline bool sanitize (SANITIZE_ARG_DEF) { |
| 358 | TRACE_SANITIZE (); |
| 359 | return SANITIZE_SELF (); |
| 360 | } |
| 361 | private: BEInt<Type, sizeof (Type)> v; |
| 362 | }; |
| 363 | |
| 364 | typedef IntType<uint16_t> USHORT; /* 16-bit unsigned integer. */ |
| 365 | typedef IntType<int16_t> SHORT; /* 16-bit signed integer. */ |
| 366 | typedef IntType<uint32_t> ULONG; /* 32-bit unsigned integer. */ |
| 367 | typedef IntType<int32_t> LONG; /* 32-bit signed integer. */ |
| 368 | |
| 369 | ASSERT_SIZE (USHORT, 2); |
| 370 | ASSERT_SIZE (SHORT, 2); |
| 371 | ASSERT_SIZE (ULONG, 4); |
| 372 | ASSERT_SIZE (LONG, 4); |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 373 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 374 | /* Array of four uint8s (length = 32 bits) used to identify a script, language |
| 375 | * system, feature, or baseline */ |
Behdad Esfahbod | 20cc86b | 2009-05-25 02:41:49 -0400 | [diff] [blame] | 376 | struct Tag : ULONG |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 377 | { |
Behdad Esfahbod | befc022 | 2006-12-25 09:14:52 -0500 | [diff] [blame] | 378 | /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */ |
Behdad Esfahbod | 0dfcc13 | 2010-04-21 23:41:26 -0400 | [diff] [blame] | 379 | inline operator const char* (void) const { return CharP(this); } |
Behdad Esfahbod | 198facd | 2010-04-21 13:35:36 -0400 | [diff] [blame] | 380 | inline operator char* (void) { return CharP(this); } |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 381 | }; |
Behdad Esfahbod | 303fe62 | 2008-01-23 00:20:48 -0500 | [diff] [blame] | 382 | ASSERT_SIZE (Tag, 4); |
Behdad Esfahbod | 9d36778 | 2010-04-21 00:32:47 -0400 | [diff] [blame] | 383 | DEFINE_NULL_DATA (Tag, 4, " "); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 384 | |
| 385 | /* Glyph index number, same as uint16 (length = 16 bits) */ |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 386 | typedef USHORT GlyphID; |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 387 | |
Behdad Esfahbod | 1f437e6 | 2008-01-23 04:36:40 -0500 | [diff] [blame] | 388 | /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */ |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 389 | typedef USHORT Offset; |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 390 | |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 391 | /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */ |
| 392 | typedef ULONG LongOffset; |
| 393 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 394 | |
| 395 | /* CheckSum */ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 396 | struct CheckSum : ULONG |
| 397 | { |
| 398 | static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length) |
| 399 | { |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 400 | uint32_t Sum = 0L; |
Behdad Esfahbod | 4b8487d | 2010-03-16 03:46:17 -0400 | [diff] [blame] | 401 | ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::get_size (); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 402 | |
| 403 | while (Table < EndPtr) |
| 404 | Sum += *Table++; |
| 405 | return Sum; |
| 406 | } |
| 407 | }; |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 408 | ASSERT_SIZE (CheckSum, 4); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 409 | |
| 410 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 411 | /* |
| 412 | * Version Numbers |
| 413 | */ |
| 414 | |
Behdad Esfahbod | 87fcdcb | 2009-05-24 01:03:24 -0400 | [diff] [blame] | 415 | struct FixedVersion |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 416 | { |
Behdad Esfahbod | 09c292e | 2009-05-26 19:48:16 -0400 | [diff] [blame] | 417 | inline operator uint32_t (void) const { return (major << 16) + minor; } |
Behdad Esfahbod | 96908b8 | 2009-05-24 12:30:40 -0400 | [diff] [blame] | 418 | |
Behdad Esfahbod | cd3827e | 2009-08-04 02:09:34 -0400 | [diff] [blame] | 419 | inline bool sanitize (SANITIZE_ARG_DEF) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 420 | TRACE_SANITIZE (); |
Behdad Esfahbod | cd3827e | 2009-08-04 02:09:34 -0400 | [diff] [blame] | 421 | return SANITIZE_SELF (); |
| 422 | } |
| 423 | |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 424 | USHORT major; |
Behdad Esfahbod | 87fcdcb | 2009-05-24 01:03:24 -0400 | [diff] [blame] | 425 | USHORT minor; |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 426 | }; |
Behdad Esfahbod | 87fcdcb | 2009-05-24 01:03:24 -0400 | [diff] [blame] | 427 | ASSERT_SIZE (FixedVersion, 4); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 428 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 429 | |
| 430 | |
| 431 | /* |
| 432 | * Template subclasses of Offset and LongOffset that do the dereferencing. |
Behdad Esfahbod | f0abcd6 | 2010-05-02 18:14:25 -0400 | [diff] [blame] | 433 | * Use: (base+offset) |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 434 | */ |
| 435 | |
| 436 | template <typename OffsetType, typename Type> |
| 437 | struct GenericOffsetTo : OffsetType |
| 438 | { |
Behdad Esfahbod | 00e23fc | 2010-04-20 23:50:45 -0400 | [diff] [blame] | 439 | inline const Type& operator () (const void *base) const |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 440 | { |
| 441 | unsigned int offset = *this; |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 442 | if (unlikely (!offset)) return Null(Type); |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 443 | return StructAtOffset<Type> (*CharP(base), offset); |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 444 | } |
| 445 | |
Behdad Esfahbod | 62c0fd7 | 2010-04-21 23:30:48 -0400 | [diff] [blame] | 446 | inline bool sanitize (SANITIZE_ARG_DEF, void *base) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 447 | TRACE_SANITIZE (); |
Behdad Esfahbod | 9552813 | 2009-08-14 16:17:32 -0400 | [diff] [blame] | 448 | if (!SANITIZE_SELF ()) return false; |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 449 | unsigned int offset = *this; |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 450 | if (unlikely (!offset)) return true; |
Behdad Esfahbod | 2226fc9 | 2010-05-04 15:12:17 -0400 | [diff] [blame] | 451 | Type &obj = StructAtOffset<Type> (*CharP(base), offset); |
| 452 | return likely (obj.sanitize (SANITIZE_ARG)) || neuter (SANITIZE_ARG); |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 453 | } |
Behdad Esfahbod | 62c0fd7 | 2010-04-21 23:30:48 -0400 | [diff] [blame] | 454 | inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 455 | TRACE_SANITIZE (); |
Behdad Esfahbod | 9552813 | 2009-08-14 16:17:32 -0400 | [diff] [blame] | 456 | if (!SANITIZE_SELF ()) return false; |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 457 | unsigned int offset = *this; |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 458 | if (unlikely (!offset)) return true; |
Behdad Esfahbod | 2226fc9 | 2010-05-04 15:12:17 -0400 | [diff] [blame] | 459 | Type &obj = StructAtOffset<Type> (*CharP(base), offset); |
| 460 | return likely (obj.sanitize (SANITIZE_ARG, base2)) || neuter (SANITIZE_ARG); |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 461 | } |
Behdad Esfahbod | 62c0fd7 | 2010-04-21 23:30:48 -0400 | [diff] [blame] | 462 | inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 463 | TRACE_SANITIZE (); |
Behdad Esfahbod | 9552813 | 2009-08-14 16:17:32 -0400 | [diff] [blame] | 464 | if (!SANITIZE_SELF ()) return false; |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 465 | unsigned int offset = *this; |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 466 | if (unlikely (!offset)) return true; |
Behdad Esfahbod | 2226fc9 | 2010-05-04 15:12:17 -0400 | [diff] [blame] | 467 | Type &obj = StructAtOffset<Type> (*CharP(base), offset); |
| 468 | return likely (obj.sanitize (SANITIZE_ARG, user_data)) || neuter (SANITIZE_ARG); |
Behdad Esfahbod | c9f1468 | 2010-05-04 14:38:08 -0400 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | private: |
| 472 | /* Set the offset to Null */ |
| 473 | inline bool neuter (SANITIZE_ARG_DEF) { |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame^] | 474 | if (context->can_edit (sanitize_depth, CharP(this), this->get_size ())) { |
Behdad Esfahbod | c9f1468 | 2010-05-04 14:38:08 -0400 | [diff] [blame] | 475 | this->set (0); /* 0 is Null offset */ |
| 476 | return true; |
| 477 | } |
| 478 | return false; |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 479 | } |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 480 | }; |
| 481 | template <typename Base, typename OffsetType, typename Type> |
| 482 | inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); } |
| 483 | |
| 484 | template <typename Type> |
| 485 | struct OffsetTo : GenericOffsetTo<Offset, Type> {}; |
| 486 | |
| 487 | template <typename Type> |
| 488 | struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {}; |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 489 | |
| 490 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 491 | /* |
| 492 | * Array Types |
| 493 | */ |
| 494 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 495 | template <typename LenType, typename Type> |
| 496 | struct GenericArrayOf |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 497 | { |
Behdad Esfahbod | 2cb0845 | 2010-04-21 22:37:31 -0400 | [diff] [blame] | 498 | const Type *array(void) const { return &StructAfter<Type> (len); } |
Behdad Esfahbod | 2e2f43e | 2010-04-21 22:30:36 -0400 | [diff] [blame] | 499 | Type *array(void) { return &StructAfter<Type> (len); } |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 500 | |
Behdad Esfahbod | 4f5f1c3 | 2010-04-22 00:27:39 -0400 | [diff] [blame] | 501 | 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] | 502 | { |
| 503 | unsigned int count = len; |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 504 | if (unlikely (start_offset > count)) |
Behdad Esfahbod | 48de373 | 2009-11-04 16:59:50 -0500 | [diff] [blame] | 505 | count = 0; |
| 506 | else |
| 507 | count -= start_offset; |
| 508 | count = MIN (count, *pcount); |
| 509 | *pcount = count; |
Behdad Esfahbod | 2cb0845 | 2010-04-21 22:37:31 -0400 | [diff] [blame] | 510 | return array() + start_offset; |
Behdad Esfahbod | 48de373 | 2009-11-04 16:59:50 -0500 | [diff] [blame] | 511 | } |
| 512 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 513 | inline const Type& operator [] (unsigned int i) const |
| 514 | { |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 515 | if (unlikely (i >= len)) return Null(Type); |
Behdad Esfahbod | 2cb0845 | 2010-04-21 22:37:31 -0400 | [diff] [blame] | 516 | return array()[i]; |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 517 | } |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 518 | inline unsigned int get_size () const |
Behdad Esfahbod | 4b8487d | 2010-03-16 03:46:17 -0400 | [diff] [blame] | 519 | { return len.get_size () + len * Type::get_size (); } |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 520 | |
Behdad Esfahbod | 20b035d | 2009-08-10 19:00:36 -0400 | [diff] [blame] | 521 | inline bool sanitize (SANITIZE_ARG_DEF) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 522 | TRACE_SANITIZE (); |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 523 | if (!likely (sanitize_shallow (SANITIZE_ARG))) return false; |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 524 | /* Note: for structs that do not reference other structs, |
| 525 | * we do not need to call their sanitize() as we already did |
| 526 | * a bound check on the aggregate array size, hence the return. |
| 527 | */ |
Behdad Esfahbod | 3564ee5 | 2009-08-14 18:32:56 -0400 | [diff] [blame] | 528 | return true; |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 529 | /* We do keep this code though to make sure the structs pointed |
| 530 | * to do have a simple sanitize(), ie. they do not reference |
| 531 | * other structs. */ |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 532 | unsigned int count = len; |
| 533 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 534 | if (!SANITIZE (array()[i])) |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 535 | return false; |
Behdad Esfahbod | 9bd629c | 2009-08-04 21:42:23 -0400 | [diff] [blame] | 536 | return true; |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 537 | } |
Behdad Esfahbod | 62c0fd7 | 2010-04-21 23:30:48 -0400 | [diff] [blame] | 538 | inline bool sanitize (SANITIZE_ARG_DEF, void *base) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 539 | TRACE_SANITIZE (); |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 540 | if (!likely (sanitize_shallow (SANITIZE_ARG))) return false; |
Behdad Esfahbod | e6ab2c5 | 2009-08-04 10:23:01 -0400 | [diff] [blame] | 541 | unsigned int count = len; |
| 542 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 543 | if (!array()[i].sanitize (SANITIZE_ARG, base)) |
Behdad Esfahbod | e6ab2c5 | 2009-08-04 10:23:01 -0400 | [diff] [blame] | 544 | return false; |
Behdad Esfahbod | 9bd629c | 2009-08-04 21:42:23 -0400 | [diff] [blame] | 545 | return true; |
Behdad Esfahbod | e6ab2c5 | 2009-08-04 10:23:01 -0400 | [diff] [blame] | 546 | } |
Behdad Esfahbod | 62c0fd7 | 2010-04-21 23:30:48 -0400 | [diff] [blame] | 547 | inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 548 | TRACE_SANITIZE (); |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 549 | if (!likely (sanitize_shallow (SANITIZE_ARG))) return false; |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 550 | unsigned int count = len; |
| 551 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 552 | if (!array()[i].sanitize (SANITIZE_ARG, base, base2)) |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 553 | return false; |
Behdad Esfahbod | 9bd629c | 2009-08-04 21:42:23 -0400 | [diff] [blame] | 554 | return true; |
Behdad Esfahbod | b508e5c | 2009-08-04 15:07:24 -0400 | [diff] [blame] | 555 | } |
Behdad Esfahbod | 62c0fd7 | 2010-04-21 23:30:48 -0400 | [diff] [blame] | 556 | inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 557 | TRACE_SANITIZE (); |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 558 | if (!likely (sanitize_shallow (SANITIZE_ARG))) return false; |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 559 | unsigned int count = len; |
| 560 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 561 | if (!array()[i].sanitize (SANITIZE_ARG, base, user_data)) |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 562 | return false; |
Behdad Esfahbod | 9bd629c | 2009-08-04 21:42:23 -0400 | [diff] [blame] | 563 | return true; |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 564 | } |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 565 | |
Behdad Esfahbod | 30fa282 | 2010-05-04 14:28:18 -0400 | [diff] [blame] | 566 | private: |
| 567 | inline bool sanitize_shallow (SANITIZE_ARG_DEF) { |
| 568 | TRACE_SANITIZE (); |
| 569 | return SANITIZE_SELF() && SANITIZE_ARRAY (this, Type::get_size (), len); |
| 570 | } |
| 571 | |
| 572 | public: |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 573 | LenType len; |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 574 | /*Type array[VAR];*/ |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 575 | }; |
| 576 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 577 | /* An array with a USHORT number of elements. */ |
| 578 | template <typename Type> |
| 579 | struct ArrayOf : GenericArrayOf<USHORT, Type> {}; |
| 580 | |
| 581 | /* An array with a ULONG number of elements. */ |
| 582 | template <typename Type> |
| 583 | struct LongArrayOf : GenericArrayOf<ULONG, Type> {}; |
| 584 | |
| 585 | /* Array of Offset's */ |
| 586 | template <typename Type> |
| 587 | struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {}; |
| 588 | |
| 589 | /* Array of LongOffset's */ |
| 590 | template <typename Type> |
| 591 | struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {}; |
| 592 | |
| 593 | /* LongArray of LongOffset's */ |
| 594 | template <typename Type> |
| 595 | struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {}; |
| 596 | |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 597 | /* Array of offsets relative to the beginning of the array itself. */ |
| 598 | template <typename Type> |
| 599 | struct OffsetListOf : OffsetArrayOf<Type> |
| 600 | { |
| 601 | inline const Type& operator [] (unsigned int i) const |
| 602 | { |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 603 | if (unlikely (i >= this->len)) return Null(Type); |
Behdad Esfahbod | 2cb0845 | 2010-04-21 22:37:31 -0400 | [diff] [blame] | 604 | return this+this->array()[i]; |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | inline bool sanitize (SANITIZE_ARG_DEF) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 608 | TRACE_SANITIZE (); |
Behdad Esfahbod | 62c0fd7 | 2010-04-21 23:30:48 -0400 | [diff] [blame] | 609 | return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this)); |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 610 | } |
| 611 | inline bool sanitize (SANITIZE_ARG_DEF, unsigned int user_data) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 612 | TRACE_SANITIZE (); |
Behdad Esfahbod | 62c0fd7 | 2010-04-21 23:30:48 -0400 | [diff] [blame] | 613 | return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this), user_data); |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 614 | } |
| 615 | }; |
| 616 | |
| 617 | |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 618 | /* An array with a USHORT number of elements, |
| 619 | * starting at second element. */ |
| 620 | template <typename Type> |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 621 | struct HeadlessArrayOf |
| 622 | { |
Behdad Esfahbod | 2cb0845 | 2010-04-21 22:37:31 -0400 | [diff] [blame] | 623 | const Type *array(void) const { return &StructAfter<Type> (len); } |
Behdad Esfahbod | 2e2f43e | 2010-04-21 22:30:36 -0400 | [diff] [blame] | 624 | Type *array(void) { return &StructAfter<Type> (len); } |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 625 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 626 | inline const Type& operator [] (unsigned int i) const |
| 627 | { |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 628 | if (unlikely (i >= len || !i)) return Null(Type); |
Behdad Esfahbod | 2cb0845 | 2010-04-21 22:37:31 -0400 | [diff] [blame] | 629 | return array()[i-1]; |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 630 | } |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 631 | inline unsigned int get_size () const |
Behdad Esfahbod | 4b8487d | 2010-03-16 03:46:17 -0400 | [diff] [blame] | 632 | { return len.get_size () + (len ? len - 1 : 0) * Type::get_size (); } |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 633 | |
Behdad Esfahbod | e5546a4 | 2010-04-22 00:45:42 -0400 | [diff] [blame] | 634 | inline bool sanitize_shallow (SANITIZE_ARG_DEF) { |
| 635 | TRACE_SANITIZE (); |
| 636 | return SANITIZE_SELF() && SANITIZE_ARRAY (this, Type::get_size (), len); |
| 637 | } |
| 638 | |
Behdad Esfahbod | 20b035d | 2009-08-10 19:00:36 -0400 | [diff] [blame] | 639 | inline bool sanitize (SANITIZE_ARG_DEF) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 640 | TRACE_SANITIZE (); |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 641 | if (!likely (sanitize_shallow (SANITIZE_ARG))) return false; |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 642 | /* Note: for structs that do not reference other structs, |
| 643 | * we do not need to call their sanitize() as we already did |
| 644 | * a bound check on the aggregate array size, hence the return. |
| 645 | */ |
Behdad Esfahbod | 3564ee5 | 2009-08-14 18:32:56 -0400 | [diff] [blame] | 646 | return true; |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 647 | /* We do keep this code though to make sure the structs pointed |
| 648 | * to do have a simple sanitize(), ie. they do not reference |
| 649 | * other structs. */ |
Behdad Esfahbod | 15164d9 | 2009-08-04 13:57:41 -0400 | [diff] [blame] | 650 | unsigned int count = len ? len - 1 : 0; |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 651 | Type *a = array(); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 652 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 653 | if (!SANITIZE (a[i])) |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 654 | return false; |
Behdad Esfahbod | 9bd629c | 2009-08-04 21:42:23 -0400 | [diff] [blame] | 655 | return true; |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 656 | } |
| 657 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 658 | USHORT len; |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 659 | /*Type array[VAR];*/ |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 660 | }; |
| 661 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 662 | |
Behdad Esfahbod | 1e91434 | 2009-11-04 18:12:09 -0500 | [diff] [blame] | 663 | #endif /* HB_OPEN_TYPE_PRIVATE_HH */ |