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 | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 34 | #include "hb-blob.h" |
| 35 | |
Behdad Esfahbod | a16ecbf | 2008-01-23 17:01:55 -0500 | [diff] [blame] | 36 | |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 37 | |
Behdad Esfahbod | 196598b | 2009-08-04 11:04:32 -0400 | [diff] [blame] | 38 | /* |
| 39 | * Casts |
| 40 | */ |
| 41 | |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 42 | /* Cast to struct T, reference to reference */ |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 43 | template<typename Type, typename TObject> |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 44 | inline const Type& CastR(const TObject &X) |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 45 | { return reinterpret_cast<const Type&> (X); } |
| 46 | template<typename Type, typename TObject> |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 47 | inline Type& CastR(TObject &X) |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 48 | { return reinterpret_cast<Type&> (X); } |
Behdad Esfahbod | 196598b | 2009-08-04 11:04:32 -0400 | [diff] [blame] | 49 | |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 50 | /* Cast to struct T, pointer to pointer */ |
| 51 | template<typename Type, typename TObject> |
| 52 | inline const Type* CastP(const TObject *X) |
| 53 | { return reinterpret_cast<const Type*> (X); } |
| 54 | template<typename Type, typename TObject> |
| 55 | inline Type* CastP(TObject *X) |
| 56 | { return reinterpret_cast<Type*> (X); } |
| 57 | |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 58 | /* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory |
| 59 | * location pointed to by P plus Ofs bytes. */ |
| 60 | template<typename Type> |
| 61 | inline const Type& StructAtOffset(const void *P, unsigned int offset) |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 62 | { return * reinterpret_cast<const Type*> ((const char *) P + offset); } |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 63 | template<typename Type> |
| 64 | inline Type& StructAtOffset(void *P, unsigned int offset) |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 65 | { return * reinterpret_cast<Type*> ((char *) P + offset); } |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 66 | |
Behdad Esfahbod | 2e2f43e | 2010-04-21 22:30:36 -0400 | [diff] [blame] | 67 | /* StructAfter<T>(X) returns the struct T& that is placed after X. |
Behdad Esfahbod | 29c3f5e | 2010-04-21 23:01:00 -0400 | [diff] [blame] | 68 | * Works with X of variable size also. X must implement get_size() */ |
Behdad Esfahbod | e961c86 | 2010-04-21 15:56:11 -0400 | [diff] [blame] | 69 | template<typename Type, typename TObject> |
| 70 | inline const Type& StructAfter(const TObject &X) |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 71 | { return StructAtOffset<Type>(&X, X.get_size()); } |
Behdad Esfahbod | e961c86 | 2010-04-21 15:56:11 -0400 | [diff] [blame] | 72 | template<typename Type, typename TObject> |
| 73 | inline Type& StructAfter(TObject &X) |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 74 | { return StructAtOffset<Type>(&X, X.get_size()); } |
Behdad Esfahbod | a3263aa | 2010-04-22 18:29:09 -0400 | [diff] [blame] | 75 | |
Behdad Esfahbod | e961c86 | 2010-04-21 15:56:11 -0400 | [diff] [blame] | 76 | |
Behdad Esfahbod | d3480ba | 2009-11-03 10:47:29 -0500 | [diff] [blame] | 77 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 78 | /* |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 79 | * Size checking |
| 80 | */ |
| 81 | |
Behdad Esfahbod | f679635 | 2010-05-13 13:34:17 -0400 | [diff] [blame] | 82 | /* Check _assertion in a method environment */ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 83 | #define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \ |
| 84 | inline void _instance_assertion_on_line_##_line (void) const \ |
| 85 | { \ |
| 86 | ASSERT_STATIC (_assertion); \ |
| 87 | ASSERT_INSTANCE_POD (*this); /* Make sure it's POD. */ \ |
| 88 | } |
| 89 | # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion) |
| 90 | # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion) |
| 91 | |
Behdad Esfahbod | f679635 | 2010-05-13 13:34:17 -0400 | [diff] [blame] | 92 | /* Check that _code compiles in a method environment */ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 93 | #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \ |
| 94 | inline void _compiles_assertion_on_line_##_line (void) const \ |
Behdad Esfahbod | f679635 | 2010-05-13 13:34:17 -0400 | [diff] [blame] | 95 | { _code; } |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 96 | # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code) |
| 97 | # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code) |
Behdad Esfahbod | 0abcc3b | 2010-05-10 17:04:20 -0400 | [diff] [blame] | 98 | |
| 99 | |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 100 | #define DEFINE_SIZE_STATIC(size) \ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 101 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)); \ |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 102 | static const unsigned int static_size = (size); \ |
| 103 | static const unsigned int min_size = (size) |
| 104 | |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 105 | /* Size signifying variable-sized array */ |
| 106 | #define VAR 1 |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 107 | |
Behdad Esfahbod | 596e471 | 2010-05-10 18:47:48 -0400 | [diff] [blame] | 108 | #define DEFINE_SIZE_UNION(size, _member) \ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 109 | DEFINE_INSTANCE_ASSERTION (this->u._member.static_size == (size)); \ |
Behdad Esfahbod | 596e471 | 2010-05-10 18:47:48 -0400 | [diff] [blame] | 110 | static const unsigned int min_size = (size) |
| 111 | |
Behdad Esfahbod | bea34c7 | 2010-05-10 17:28:16 -0400 | [diff] [blame] | 112 | #define DEFINE_SIZE_MIN(size) \ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 113 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)); \ |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 114 | static const unsigned int min_size = (size) |
| 115 | |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 116 | #define DEFINE_SIZE_ARRAY(size, array) \ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 117 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \ |
| 118 | DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \ |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 119 | static const unsigned int min_size = (size) |
| 120 | |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 121 | #define DEFINE_SIZE_ARRAY2(size, array1, array2) \ |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 122 | DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \ |
| 123 | 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] | 124 | static const unsigned int min_size = (size) |
| 125 | |
| 126 | |
| 127 | |
| 128 | /* |
Behdad Esfahbod | f0abcd6 | 2010-05-02 18:14:25 -0400 | [diff] [blame] | 129 | * Null objects |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 130 | */ |
| 131 | |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 132 | /* Global nul-content Null pool. Enlarge as necessary. */ |
Behdad Esfahbod | 0bb0f5d | 2012-06-07 17:42:48 -0400 | [diff] [blame] | 133 | /* TODO This really should be a extern HB_INTERNAL and defined somewhere... */ |
Behdad Esfahbod | cf5585c | 2010-05-19 12:03:35 -0400 | [diff] [blame] | 134 | static const void *_NullPool[64 / sizeof (void *)]; |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 135 | |
Behdad Esfahbod | d2c2ca8 | 2010-05-10 19:58:25 -0400 | [diff] [blame] | 136 | /* Generic nul-content Null objects. */ |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 137 | template <typename Type> |
Behdad Esfahbod | 7f97d2c | 2010-10-01 18:58:50 -0400 | [diff] [blame] | 138 | static inline const Type& Null (void) { |
Behdad Esfahbod | ed07422 | 2010-05-10 18:08:46 -0400 | [diff] [blame] | 139 | ASSERT_STATIC (Type::min_size <= sizeof (_NullPool)); |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 140 | return *CastP<Type> (_NullPool); |
Behdad Esfahbod | 9d36778 | 2010-04-21 00:32:47 -0400 | [diff] [blame] | 141 | } |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 142 | |
| 143 | /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */ |
Behdad Esfahbod | 65f46b0 | 2010-05-06 19:35:19 -0400 | [diff] [blame] | 144 | #define DEFINE_NULL_DATA(Type, data) \ |
| 145 | static const char _Null##Type[Type::min_size + 1] = data; /* +1 is for nul-termination in data */ \ |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 146 | template <> \ |
Behdad Esfahbod | 7f97d2c | 2010-10-01 18:58:50 -0400 | [diff] [blame] | 147 | inline const Type& Null<Type> (void) { \ |
Behdad Esfahbod | 187454c | 2010-04-23 16:35:01 -0400 | [diff] [blame] | 148 | return *CastP<Type> (_Null##Type); \ |
Behdad Esfahbod | 565c80b | 2010-04-22 10:26:35 -0400 | [diff] [blame] | 149 | } /* 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] | 150 | ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type)) |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 151 | |
| 152 | /* Accessor macro. */ |
Behdad Esfahbod | 9d36778 | 2010-04-21 00:32:47 -0400 | [diff] [blame] | 153 | #define Null(Type) Null<Type>() |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 154 | |
| 155 | |
Behdad Esfahbod | 577c111 | 2009-08-04 19:31:02 -0400 | [diff] [blame] | 156 | |
| 157 | /* |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 158 | * Sanitize |
| 159 | */ |
| 160 | |
Behdad Esfahbod | 95e2024 | 2009-08-28 16:31:20 -0400 | [diff] [blame] | 161 | #ifndef HB_DEBUG_SANITIZE |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 162 | #define HB_DEBUG_SANITIZE (HB_DEBUG+0) |
Behdad Esfahbod | 95e2024 | 2009-08-28 16:31:20 -0400 | [diff] [blame] | 163 | #endif |
| 164 | |
Behdad Esfahbod | 20e3dd5 | 2010-05-04 23:21:57 -0400 | [diff] [blame] | 165 | |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 166 | #define TRACE_SANITIZE() \ |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 167 | hb_auto_trace_t<HB_DEBUG_SANITIZE> trace (&c->debug_depth, "SANITIZE", this, HB_FUNC, ""); |
Behdad Esfahbod | 807c5b0 | 2010-04-28 20:25:22 -0400 | [diff] [blame] | 168 | |
| 169 | |
Behdad Esfahbod | 1376fb7 | 2010-04-29 02:19:21 -0400 | [diff] [blame] | 170 | struct hb_sanitize_context_t |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 171 | { |
Behdad Esfahbod | 31f18ab | 2011-06-15 09:49:58 -0400 | [diff] [blame] | 172 | inline void init (hb_blob_t *b) |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 173 | { |
Behdad Esfahbod | 31f18ab | 2011-06-15 09:49:58 -0400 | [diff] [blame] | 174 | this->blob = hb_blob_reference (b); |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 175 | this->writable = false; |
| 176 | } |
| 177 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 178 | inline void start_processing (void) |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 179 | { |
Behdad Esfahbod | 4101ca7 | 2011-05-11 14:30:56 -0400 | [diff] [blame] | 180 | this->start = hb_blob_get_data (this->blob, NULL); |
| 181 | this->end = this->start + hb_blob_get_length (this->blob); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 182 | this->edit_count = 0; |
Behdad Esfahbod | 20e3dd5 | 2010-05-04 23:21:57 -0400 | [diff] [blame] | 183 | this->debug_depth = 0; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 184 | |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 185 | DEBUG_MSG_LEVEL (SANITIZE, this->blob, 0, +1, |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 186 | "start [%p..%p] (%lu bytes)", |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 187 | this->start, this->end, |
| 188 | (unsigned long) (this->end - this->start)); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 191 | inline void end_processing (void) |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 192 | { |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 193 | DEBUG_MSG_LEVEL (SANITIZE, this->blob, 0, -1, |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 194 | "end [%p..%p] %u edit requests", |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 195 | this->start, this->end, this->edit_count); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 196 | |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 197 | hb_blob_destroy (this->blob); |
| 198 | this->blob = NULL; |
| 199 | this->start = this->end = NULL; |
| 200 | } |
| 201 | |
Behdad Esfahbod | 4ad2cc5 | 2010-05-06 09:24:24 -0400 | [diff] [blame] | 202 | inline bool check_range (const void *base, unsigned int len) const |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 203 | { |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 204 | const char *p = (const char *) base; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 205 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 206 | hb_auto_trace_t<HB_DEBUG_SANITIZE> trace (&this->debug_depth, "SANITIZE", this->blob, NULL, |
| 207 | "check_range [%p..%p] (%d bytes) in [%p..%p]", |
| 208 | p, p + len, len, |
| 209 | this->start, this->end); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 210 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 211 | return TRACE_RETURN (likely (this->start <= p && p <= this->end && (unsigned int) (this->end - p) >= len)); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Behdad Esfahbod | 1cd1e11 | 2010-05-05 20:15:14 -0400 | [diff] [blame] | 214 | 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] | 215 | { |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 216 | const char *p = (const char *) base; |
Behdad Esfahbod | 080a0eb | 2011-04-28 16:01:01 -0400 | [diff] [blame] | 217 | bool overflows = _hb_unsigned_int_mul_overflows (len, record_size); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 218 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 219 | hb_auto_trace_t<HB_DEBUG_SANITIZE> trace (&this->debug_depth, "SANITIZE", this->blob, NULL, |
| 220 | "check_array [%p..%p] (%d*%d=%ld bytes) in [%p..%p]", |
| 221 | p, p + (record_size * len), record_size, len, (unsigned long) record_size * len, |
| 222 | this->start, this->end); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 223 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 224 | return TRACE_RETURN (likely (!overflows && this->check_range (base, record_size * len))); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 225 | } |
| 226 | |
Behdad Esfahbod | b157617 | 2010-05-06 14:48:27 -0400 | [diff] [blame] | 227 | template <typename Type> |
| 228 | inline bool check_struct (const Type *obj) const |
| 229 | { |
Behdad Esfahbod | 5484237 | 2010-05-10 18:13:32 -0400 | [diff] [blame] | 230 | return likely (this->check_range (obj, obj->min_size)); |
Behdad Esfahbod | b157617 | 2010-05-06 14:48:27 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 233 | 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] | 234 | { |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 235 | const char *p = (const char *) base; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 236 | this->edit_count++; |
| 237 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 238 | hb_auto_trace_t<HB_DEBUG_SANITIZE> trace (&this->debug_depth, "SANITIZE", this->blob, NULL, |
| 239 | "may_edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s", |
| 240 | this->edit_count, |
| 241 | p, p + len, len, |
| 242 | this->start, this->end); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 243 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 244 | return TRACE_RETURN (this->writable); |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 247 | mutable unsigned int debug_depth; |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 248 | const char *start, *end; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 249 | bool writable; |
Behdad Esfahbod | 254933c | 2010-04-23 13:57:10 -0400 | [diff] [blame] | 250 | unsigned int edit_count; |
Behdad Esfahbod | 98daaf1 | 2010-05-04 22:42:49 -0400 | [diff] [blame] | 251 | hb_blob_t *blob; |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 252 | }; |
| 253 | |
Behdad Esfahbod | 1376fb7 | 2010-04-29 02:19:21 -0400 | [diff] [blame] | 254 | |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 255 | |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 256 | /* Template to sanitize an object. */ |
| 257 | template <typename Type> |
| 258 | struct Sanitizer |
| 259 | { |
| 260 | static hb_blob_t *sanitize (hb_blob_t *blob) { |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 261 | hb_sanitize_context_t c[1] = {{0}}; |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 262 | bool sane; |
| 263 | |
Behdad Esfahbod | d0b6573 | 2009-08-06 18:34:47 -0400 | [diff] [blame] | 264 | /* TODO is_sane() stuff */ |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 265 | |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 266 | c->init (blob); |
| 267 | |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 268 | retry: |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 269 | DEBUG_MSG_FUNC (SANITIZE, blob, "start"); |
Behdad Esfahbod | 4f3ad91 | 2009-08-04 23:01:23 -0400 | [diff] [blame] | 270 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 271 | c->start_processing (); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 272 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 273 | if (unlikely (!c->start)) { |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 274 | c->end_processing (); |
Behdad Esfahbod | 48146e5 | 2010-05-10 20:07:56 -0400 | [diff] [blame] | 275 | return blob; |
| 276 | } |
| 277 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 278 | Type *t = CastP<Type> (const_cast<char *> (c->start)); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 279 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 280 | sane = t->sanitize (c); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 281 | if (sane) { |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 282 | if (c->edit_count) { |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 283 | DEBUG_MSG_FUNC (SANITIZE, blob, "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] | 284 | |
Behdad Esfahbod | 8b53461 | 2009-08-19 18:16:50 -0400 | [diff] [blame] | 285 | /* sanitize again to ensure no toe-stepping */ |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 286 | c->edit_count = 0; |
| 287 | sane = t->sanitize (c); |
| 288 | if (c->edit_count) { |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 289 | DEBUG_MSG_FUNC (SANITIZE, blob, "requested %d edits in second round; FAILLING", c->edit_count); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 290 | sane = false; |
| 291 | } |
| 292 | } |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 293 | } else { |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 294 | unsigned int edit_count = c->edit_count; |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 295 | if (edit_count && !c->writable) { |
| 296 | c->start = hb_blob_get_data_writable (blob, NULL); |
| 297 | c->end = c->start + hb_blob_get_length (blob); |
| 298 | |
| 299 | if (c->start) { |
| 300 | c->writable = true; |
| 301 | /* ok, we made it writable by relocating. try again */ |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 302 | DEBUG_MSG_FUNC (SANITIZE, blob, "retry"); |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 303 | goto retry; |
| 304 | } |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 308 | c->end_processing (); |
Behdad Esfahbod | 4101ca7 | 2011-05-11 14:30:56 -0400 | [diff] [blame] | 309 | |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 310 | DEBUG_MSG_FUNC (SANITIZE, blob, sane ? "PASSED" : "FAILED"); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 311 | if (sane) |
| 312 | return blob; |
| 313 | else { |
| 314 | hb_blob_destroy (blob); |
Behdad Esfahbod | 4911062 | 2011-05-02 19:36:39 -0400 | [diff] [blame] | 315 | return hb_blob_get_empty (); |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 316 | } |
| 317 | } |
Behdad Esfahbod | b435ab7 | 2010-05-10 19:51:57 -0400 | [diff] [blame] | 318 | |
| 319 | static const Type* lock_instance (hb_blob_t *blob) { |
Behdad Esfahbod | 1c9f871 | 2011-05-06 22:28:26 -0400 | [diff] [blame] | 320 | hb_blob_make_immutable (blob); |
| 321 | const char *base = hb_blob_get_data (blob, NULL); |
Behdad Esfahbod | b435ab7 | 2010-05-10 19:51:57 -0400 | [diff] [blame] | 322 | return unlikely (!base) ? &Null(Type) : CastP<Type> (base); |
| 323 | } |
Behdad Esfahbod | 4e8a060 | 2009-08-04 20:52:47 -0400 | [diff] [blame] | 324 | }; |
| 325 | |
Behdad Esfahbod | 2d15e72 | 2009-04-15 19:50:16 -0400 | [diff] [blame] | 326 | |
Behdad Esfahbod | f0abcd6 | 2010-05-02 18:14:25 -0400 | [diff] [blame] | 327 | |
| 328 | |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 329 | /* |
| 330 | * |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 331 | * The OpenType Font File: Data Types |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 332 | */ |
| 333 | |
| 334 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 335 | /* "The following data types are used in the OpenType font file. |
| 336 | * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */ |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 337 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 338 | /* |
| 339 | * Int types |
| 340 | */ |
| 341 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 342 | |
Behdad Esfahbod | d7bf473 | 2011-08-05 18:18:21 -0400 | [diff] [blame] | 343 | template <typename Type, int Bytes> struct BEInt; |
Behdad Esfahbod | f78e70c | 2006-12-21 22:30:38 -0500 | [diff] [blame] | 344 | |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 345 | template <typename Type> |
Behdad Esfahbod | d7bf473 | 2011-08-05 18:18:21 -0400 | [diff] [blame] | 346 | struct BEInt<Type, 2> |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 347 | { |
| 348 | public: |
Behdad Esfahbod | 81408cd | 2010-07-23 14:46:57 -0400 | [diff] [blame] | 349 | inline void set (Type i) { hb_be_uint16_put (v,i); } |
Behdad Esfahbod | 7f97d2c | 2010-10-01 18:58:50 -0400 | [diff] [blame] | 350 | inline operator Type (void) const { return hb_be_uint16_get (v); } |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 351 | inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_eq (v, o.v); } |
Behdad Esfahbod | 01c0161 | 2010-04-21 22:49:56 -0400 | [diff] [blame] | 352 | inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); } |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 353 | private: uint8_t v[2]; |
| 354 | }; |
| 355 | template <typename Type> |
Behdad Esfahbod | d7bf473 | 2011-08-05 18:18:21 -0400 | [diff] [blame] | 356 | struct BEInt<Type, 4> |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 357 | { |
| 358 | public: |
Behdad Esfahbod | 81408cd | 2010-07-23 14:46:57 -0400 | [diff] [blame] | 359 | inline void set (Type i) { hb_be_uint32_put (v,i); } |
Behdad Esfahbod | 7f97d2c | 2010-10-01 18:58:50 -0400 | [diff] [blame] | 360 | inline operator Type (void) const { return hb_be_uint32_get (v); } |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 361 | inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_eq (v, o.v); } |
Behdad Esfahbod | 01c0161 | 2010-04-21 22:49:56 -0400 | [diff] [blame] | 362 | inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); } |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 363 | private: uint8_t v[4]; |
| 364 | }; |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 365 | |
Behdad Esfahbod | 2467c66 | 2010-04-21 23:11:45 -0400 | [diff] [blame] | 366 | /* Integer types in big-endian order and no alignment requirement */ |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 367 | template <typename Type> |
| 368 | struct IntType |
| 369 | { |
Behdad Esfahbod | 81408cd | 2010-07-23 14:46:57 -0400 | [diff] [blame] | 370 | inline void set (Type i) { v.set (i); } |
Behdad Esfahbod | 01c0161 | 2010-04-21 22:49:56 -0400 | [diff] [blame] | 371 | inline operator Type(void) const { return v; } |
| 372 | inline bool operator == (const IntType<Type> &o) const { return v == o.v; } |
| 373 | inline bool operator != (const IntType<Type> &o) const { return v != o.v; } |
Behdad Esfahbod | 4e57371 | 2010-09-28 16:23:58 -0400 | [diff] [blame] | 374 | inline int cmp (Type a) const { Type b = v; return a < b ? -1 : a == b ? 0 : +1; } |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 375 | inline bool sanitize (hb_sanitize_context_t *c) { |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 376 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 377 | return TRACE_RETURN (likely (c->check_struct (this))); |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 378 | } |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 379 | protected: |
Behdad Esfahbod | 569da92 | 2010-05-10 16:38:32 -0400 | [diff] [blame] | 380 | BEInt<Type, sizeof (Type)> v; |
| 381 | public: |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 382 | DEFINE_SIZE_STATIC (sizeof (Type)); |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 383 | }; |
| 384 | |
Behdad Esfahbod | f60271c | 2011-08-02 09:56:30 -0400 | [diff] [blame] | 385 | /* Typedef these to avoid clash with windows.h */ |
| 386 | #define USHORT HB_USHORT |
| 387 | #define SHORT HB_SHORT |
| 388 | #define ULONG HB_ULONG |
| 389 | #define LONG HB_LONG |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 390 | typedef IntType<uint16_t> USHORT; /* 16-bit unsigned integer. */ |
| 391 | typedef IntType<int16_t> SHORT; /* 16-bit signed integer. */ |
| 392 | typedef IntType<uint32_t> ULONG; /* 32-bit unsigned integer. */ |
| 393 | typedef IntType<int32_t> LONG; /* 32-bit signed integer. */ |
| 394 | |
Behdad Esfahbod | ae9877d | 2011-08-17 14:43:45 +0200 | [diff] [blame] | 395 | /* 16-bit signed integer (SHORT) that describes a quantity in FUnits. */ |
| 396 | typedef SHORT FWORD; |
| 397 | |
| 398 | /* 16-bit unsigned integer (USHORT) that describes a quantity in FUnits. */ |
| 399 | typedef USHORT UFWORD; |
| 400 | |
Behdad Esfahbod | e29caf3 | 2010-05-19 11:47:17 -0400 | [diff] [blame] | 401 | /* Date represented in number of seconds since 12:00 midnight, January 1, |
| 402 | * 1904. The value is represented as a signed 64-bit integer. */ |
| 403 | struct LONGDATETIME |
| 404 | { |
| 405 | inline bool sanitize (hb_sanitize_context_t *c) { |
| 406 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 407 | return TRACE_RETURN (likely (c->check_struct (this))); |
Behdad Esfahbod | e29caf3 | 2010-05-19 11:47:17 -0400 | [diff] [blame] | 408 | } |
| 409 | private: |
| 410 | LONG major; |
| 411 | ULONG minor; |
| 412 | public: |
| 413 | DEFINE_SIZE_STATIC (8); |
| 414 | }; |
| 415 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 416 | /* Array of four uint8s (length = 32 bits) used to identify a script, language |
| 417 | * system, feature, or baseline */ |
Behdad Esfahbod | 20cc86b | 2009-05-25 02:41:49 -0400 | [diff] [blame] | 418 | struct Tag : ULONG |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 419 | { |
Behdad Esfahbod | befc022 | 2006-12-25 09:14:52 -0500 | [diff] [blame] | 420 | /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */ |
Behdad Esfahbod | a82ef7a | 2010-05-10 17:55:03 -0400 | [diff] [blame] | 421 | inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); } |
| 422 | inline operator char* (void) { return reinterpret_cast<char *> (&this->v); } |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 423 | public: |
| 424 | DEFINE_SIZE_STATIC (4); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 425 | }; |
Behdad Esfahbod | 65f46b0 | 2010-05-06 19:35:19 -0400 | [diff] [blame] | 426 | DEFINE_NULL_DATA (Tag, " "); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 427 | |
| 428 | /* Glyph index number, same as uint16 (length = 16 bits) */ |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 429 | typedef USHORT GlyphID; |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 430 | |
Behdad Esfahbod | b5db4f1 | 2010-05-10 22:22:22 -0400 | [diff] [blame] | 431 | /* Script/language-system/feature index */ |
| 432 | struct Index : USHORT { |
| 433 | static const unsigned int NOT_FOUND_INDEX = 0xFFFF; |
| 434 | }; |
| 435 | DEFINE_NULL_DATA (Index, "\xff\xff"); |
| 436 | |
Behdad Esfahbod | 1f437e6 | 2008-01-23 04:36:40 -0500 | [diff] [blame] | 437 | /* 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] | 438 | typedef USHORT Offset; |
Behdad Esfahbod | 8b83580 | 2009-05-16 22:48:14 -0400 | [diff] [blame] | 439 | |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 440 | /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */ |
| 441 | typedef ULONG LongOffset; |
| 442 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 443 | |
| 444 | /* CheckSum */ |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 445 | struct CheckSum : ULONG |
| 446 | { |
| 447 | static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length) |
| 448 | { |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 449 | uint32_t Sum = 0L; |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 450 | ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size; |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 451 | |
| 452 | while (Table < EndPtr) |
| 453 | Sum += *Table++; |
| 454 | return Sum; |
| 455 | } |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 456 | public: |
| 457 | DEFINE_SIZE_STATIC (4); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 458 | }; |
| 459 | |
| 460 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 461 | /* |
| 462 | * Version Numbers |
| 463 | */ |
| 464 | |
Behdad Esfahbod | 87fcdcb | 2009-05-24 01:03:24 -0400 | [diff] [blame] | 465 | struct FixedVersion |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 466 | { |
Behdad Esfahbod | 4f28fbd | 2011-05-31 12:33:11 -0400 | [diff] [blame] | 467 | inline uint32_t to_int (void) const { return (major << 16) + minor; } |
Behdad Esfahbod | 96908b8 | 2009-05-24 12:30:40 -0400 | [diff] [blame] | 468 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 469 | inline bool sanitize (hb_sanitize_context_t *c) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 470 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 471 | return TRACE_RETURN (c->check_struct (this)); |
Behdad Esfahbod | cd3827e | 2009-08-04 02:09:34 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Behdad Esfahbod | 6ad8d5f | 2009-05-25 02:27:29 -0400 | [diff] [blame] | 474 | USHORT major; |
Behdad Esfahbod | 87fcdcb | 2009-05-24 01:03:24 -0400 | [diff] [blame] | 475 | USHORT minor; |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 476 | public: |
| 477 | DEFINE_SIZE_STATIC (4); |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 478 | }; |
| 479 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 480 | |
| 481 | |
| 482 | /* |
| 483 | * Template subclasses of Offset and LongOffset that do the dereferencing. |
Behdad Esfahbod | f0abcd6 | 2010-05-02 18:14:25 -0400 | [diff] [blame] | 484 | * Use: (base+offset) |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 485 | */ |
| 486 | |
| 487 | template <typename OffsetType, typename Type> |
| 488 | struct GenericOffsetTo : OffsetType |
| 489 | { |
Behdad Esfahbod | 00e23fc | 2010-04-20 23:50:45 -0400 | [diff] [blame] | 490 | inline const Type& operator () (const void *base) const |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 491 | { |
| 492 | unsigned int offset = *this; |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 493 | if (unlikely (!offset)) return Null(Type); |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 494 | return StructAtOffset<Type> (base, offset); |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 495 | } |
| 496 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 497 | inline bool sanitize (hb_sanitize_context_t *c, void *base) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 498 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 499 | if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false); |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 500 | unsigned int offset = *this; |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 501 | if (unlikely (!offset)) return TRACE_RETURN (true); |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 502 | Type &obj = StructAtOffset<Type> (base, offset); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 503 | return TRACE_RETURN (likely (obj.sanitize (c)) || neuter (c)); |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 504 | } |
Behdad Esfahbod | 4a446ac | 2010-05-04 22:46:21 -0400 | [diff] [blame] | 505 | template <typename T> |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 506 | inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 507 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 508 | if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false); |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 509 | unsigned int offset = *this; |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 510 | if (unlikely (!offset)) return TRACE_RETURN (true); |
Behdad Esfahbod | 09766b1 | 2010-05-10 17:36:03 -0400 | [diff] [blame] | 511 | Type &obj = StructAtOffset<Type> (base, offset); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 512 | return TRACE_RETURN (likely (obj.sanitize (c, user_data)) || neuter (c)); |
Behdad Esfahbod | c9f1468 | 2010-05-04 14:38:08 -0400 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | private: |
| 516 | /* Set the offset to Null */ |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 517 | inline bool neuter (hb_sanitize_context_t *c) { |
Behdad Esfahbod | cf26e88 | 2012-05-11 03:16:57 +0200 | [diff] [blame] | 518 | if (c->may_edit (this, this->static_size)) { |
Behdad Esfahbod | c9f1468 | 2010-05-04 14:38:08 -0400 | [diff] [blame] | 519 | this->set (0); /* 0 is Null offset */ |
| 520 | return true; |
| 521 | } |
| 522 | return false; |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 523 | } |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 524 | }; |
| 525 | template <typename Base, typename OffsetType, typename Type> |
| 526 | inline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); } |
| 527 | |
| 528 | template <typename Type> |
| 529 | struct OffsetTo : GenericOffsetTo<Offset, Type> {}; |
| 530 | |
| 531 | template <typename Type> |
| 532 | struct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {}; |
Behdad Esfahbod | bff3c0f | 2009-08-07 19:46:30 -0400 | [diff] [blame] | 533 | |
| 534 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 535 | /* |
| 536 | * Array Types |
| 537 | */ |
| 538 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 539 | template <typename LenType, typename Type> |
| 540 | struct GenericArrayOf |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 541 | { |
Behdad Esfahbod | 4f5f1c3 | 2010-04-22 00:27:39 -0400 | [diff] [blame] | 542 | 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] | 543 | { |
| 544 | unsigned int count = len; |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 545 | if (unlikely (start_offset > count)) |
Behdad Esfahbod | 48de373 | 2009-11-04 16:59:50 -0500 | [diff] [blame] | 546 | count = 0; |
| 547 | else |
| 548 | count -= start_offset; |
| 549 | count = MIN (count, *pcount); |
| 550 | *pcount = count; |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 551 | return array + start_offset; |
Behdad Esfahbod | 48de373 | 2009-11-04 16:59:50 -0500 | [diff] [blame] | 552 | } |
| 553 | |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 554 | inline const Type& operator [] (unsigned int i) const |
| 555 | { |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 556 | if (unlikely (i >= len)) return Null(Type); |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 557 | return array[i]; |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 558 | } |
Behdad Esfahbod | 7f97d2c | 2010-10-01 18:58:50 -0400 | [diff] [blame] | 559 | inline unsigned int get_size (void) const |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 560 | { return len.static_size + len * Type::static_size; } |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 561 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 562 | inline bool sanitize (hb_sanitize_context_t *c) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 563 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 564 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 565 | |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 566 | /* Note: for structs that do not reference other structs, |
| 567 | * we do not need to call their sanitize() as we already did |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 568 | * a bound check on the aggregate array size. We just include |
| 569 | * a small unreachable expression to make sure the structs |
| 570 | * pointed to do have a simple sanitize(), ie. they do not |
| 571 | * reference other structs via offsets. |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 572 | */ |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 573 | (void) (false && array[0].sanitize (c)); |
| 574 | |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 575 | return TRACE_RETURN (true); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 576 | } |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 577 | inline bool sanitize (hb_sanitize_context_t *c, void *base) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 578 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 579 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
Behdad Esfahbod | e6ab2c5 | 2009-08-04 10:23:01 -0400 | [diff] [blame] | 580 | unsigned int count = len; |
| 581 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 582 | if (unlikely (!array[i].sanitize (c, base))) |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 583 | return TRACE_RETURN (false); |
| 584 | return TRACE_RETURN (true); |
Behdad Esfahbod | e6ab2c5 | 2009-08-04 10:23:01 -0400 | [diff] [blame] | 585 | } |
Behdad Esfahbod | 4a446ac | 2010-05-04 22:46:21 -0400 | [diff] [blame] | 586 | template <typename T> |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 587 | inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 588 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 589 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 590 | unsigned int count = len; |
| 591 | for (unsigned int i = 0; i < count; i++) |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 592 | if (unlikely (!array[i].sanitize (c, base, user_data))) |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 593 | return TRACE_RETURN (false); |
| 594 | return TRACE_RETURN (true); |
Behdad Esfahbod | 42b778f | 2009-08-04 13:30:49 -0400 | [diff] [blame] | 595 | } |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 596 | |
Behdad Esfahbod | 30fa282 | 2010-05-04 14:28:18 -0400 | [diff] [blame] | 597 | private: |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 598 | inline bool sanitize_shallow (hb_sanitize_context_t *c) { |
Behdad Esfahbod | 30fa282 | 2010-05-04 14:28:18 -0400 | [diff] [blame] | 599 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 600 | 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] | 601 | } |
| 602 | |
| 603 | public: |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 604 | LenType len; |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 605 | Type array[VAR]; |
Behdad Esfahbod | b365123 | 2010-05-10 16:57:29 -0400 | [diff] [blame] | 606 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 607 | DEFINE_SIZE_ARRAY (sizeof (LenType), array); |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 608 | }; |
| 609 | |
Behdad Esfahbod | 92b5dd8 | 2009-08-04 10:41:32 -0400 | [diff] [blame] | 610 | /* An array with a USHORT number of elements. */ |
| 611 | template <typename Type> |
| 612 | struct ArrayOf : GenericArrayOf<USHORT, Type> {}; |
| 613 | |
| 614 | /* An array with a ULONG number of elements. */ |
| 615 | template <typename Type> |
| 616 | struct LongArrayOf : GenericArrayOf<ULONG, Type> {}; |
| 617 | |
| 618 | /* Array of Offset's */ |
| 619 | template <typename Type> |
| 620 | struct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {}; |
| 621 | |
| 622 | /* Array of LongOffset's */ |
| 623 | template <typename Type> |
| 624 | struct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {}; |
| 625 | |
| 626 | /* LongArray of LongOffset's */ |
| 627 | template <typename Type> |
| 628 | struct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {}; |
| 629 | |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 630 | /* Array of offsets relative to the beginning of the array itself. */ |
| 631 | template <typename Type> |
| 632 | struct OffsetListOf : OffsetArrayOf<Type> |
| 633 | { |
| 634 | inline const Type& operator [] (unsigned int i) const |
| 635 | { |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 636 | if (unlikely (i >= this->len)) return Null(Type); |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 637 | return this+this->array[i]; |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 638 | } |
| 639 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 640 | inline bool sanitize (hb_sanitize_context_t *c) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 641 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 642 | return TRACE_RETURN (OffsetArrayOf<Type>::sanitize (c, this)); |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 643 | } |
Behdad Esfahbod | 4a446ac | 2010-05-04 22:46:21 -0400 | [diff] [blame] | 644 | template <typename T> |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 645 | inline bool sanitize (hb_sanitize_context_t *c, T user_data) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 646 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 647 | return TRACE_RETURN (OffsetArrayOf<Type>::sanitize (c, this, user_data)); |
Behdad Esfahbod | 80e2aa2 | 2009-08-14 18:40:56 -0400 | [diff] [blame] | 648 | } |
| 649 | }; |
| 650 | |
| 651 | |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 652 | /* An array with a USHORT number of elements, |
| 653 | * starting at second element. */ |
| 654 | template <typename Type> |
Behdad Esfahbod | 60d77cf | 2009-05-19 23:58:54 -0400 | [diff] [blame] | 655 | struct HeadlessArrayOf |
| 656 | { |
| 657 | inline const Type& operator [] (unsigned int i) const |
| 658 | { |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 659 | if (unlikely (i >= len || !i)) return Null(Type); |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 660 | return array[i-1]; |
Behdad Esfahbod | e8cbaaf | 2009-05-18 02:03:58 -0400 | [diff] [blame] | 661 | } |
Behdad Esfahbod | 7f97d2c | 2010-10-01 18:58:50 -0400 | [diff] [blame] | 662 | inline unsigned int get_size (void) const |
Behdad Esfahbod | e45d3f8 | 2010-05-06 19:33:31 -0400 | [diff] [blame] | 663 | { return len.static_size + (len ? len - 1 : 0) * Type::static_size; } |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 664 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 665 | inline bool sanitize_shallow (hb_sanitize_context_t *c) { |
| 666 | return c->check_struct (this) |
| 667 | && c->check_array (this, Type::static_size, len); |
Behdad Esfahbod | e5546a4 | 2010-04-22 00:45:42 -0400 | [diff] [blame] | 668 | } |
| 669 | |
Behdad Esfahbod | d7cfb3b | 2010-05-13 14:18:49 -0400 | [diff] [blame] | 670 | inline bool sanitize (hb_sanitize_context_t *c) { |
Behdad Esfahbod | 3e2401f | 2009-08-28 17:17:11 -0400 | [diff] [blame] | 671 | TRACE_SANITIZE (); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 672 | if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 673 | |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 674 | /* Note: for structs that do not reference other structs, |
| 675 | * we do not need to call their sanitize() as we already did |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 676 | * a bound check on the aggregate array size. We just include |
| 677 | * a small unreachable expression to make sure the structs |
| 678 | * pointed to do have a simple sanitize(), ie. they do not |
| 679 | * reference other structs via offsets. |
Behdad Esfahbod | 40d73bc | 2010-04-21 00:49:40 -0400 | [diff] [blame] | 680 | */ |
Behdad Esfahbod | 11e3ec4 | 2010-11-03 15:11:04 -0400 | [diff] [blame] | 681 | (void) (false && array[0].sanitize (c)); |
| 682 | |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 683 | return TRACE_RETURN (true); |
Behdad Esfahbod | 70de50c | 2009-08-04 00:58:28 -0400 | [diff] [blame] | 684 | } |
| 685 | |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 686 | USHORT len; |
Behdad Esfahbod | b961518 | 2010-05-10 18:20:54 -0400 | [diff] [blame] | 687 | Type array[VAR]; |
Behdad Esfahbod | ed07422 | 2010-05-10 18:08:46 -0400 | [diff] [blame] | 688 | public: |
Behdad Esfahbod | 0eb9fc6 | 2010-05-10 19:01:17 -0400 | [diff] [blame] | 689 | DEFINE_SIZE_ARRAY (sizeof (USHORT), array); |
Behdad Esfahbod | 5f81036 | 2009-05-17 00:54:25 -0400 | [diff] [blame] | 690 | }; |
| 691 | |
Behdad Esfahbod | 6b4ce01 | 2006-12-21 22:31:10 -0500 | [diff] [blame] | 692 | |
Behdad Esfahbod | cc8a4ab | 2010-07-08 00:40:04 -0400 | [diff] [blame] | 693 | /* An array with sorted elements. Supports binary searching. */ |
| 694 | template <typename Type> |
| 695 | struct SortedArrayOf : ArrayOf<Type> { |
| 696 | |
| 697 | template <typename SearchType> |
| 698 | inline int search (const SearchType &x) const { |
Behdad Esfahbod | 99159e5 | 2012-06-09 00:50:40 -0400 | [diff] [blame] | 699 | unsigned int count = this->len; |
| 700 | /* Linear search is *much* faster for small counts. */ |
| 701 | if (likely (count < 32)) { |
| 702 | for (unsigned int i = 0; i < count; i++) |
| 703 | if (this->array[i].cmp (x) == 0) |
| 704 | return i; |
| 705 | return -1; |
| 706 | } else { |
| 707 | struct Cmp { |
| 708 | static int cmp (const SearchType *a, const Type *b) { return b->cmp (*a); } |
| 709 | }; |
| 710 | const Type *p = (const Type *) bsearch (&x, this->array, this->len, sizeof (this->array[0]), (hb_compare_func_t) Cmp::cmp); |
| 711 | return p ? p - this->array : -1; |
| 712 | } |
Behdad Esfahbod | cc8a4ab | 2010-07-08 00:40:04 -0400 | [diff] [blame] | 713 | } |
Behdad Esfahbod | cc8a4ab | 2010-07-08 00:40:04 -0400 | [diff] [blame] | 714 | }; |
| 715 | |
| 716 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 717 | |
Behdad Esfahbod | 1e91434 | 2009-11-04 18:12:09 -0500 | [diff] [blame] | 718 | #endif /* HB_OPEN_TYPE_PRIVATE_HH */ |