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