michael@0: /* michael@0: * Copyright © 2007,2008,2009,2010 Red Hat, Inc. michael@0: * Copyright © 2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #ifndef HB_OPEN_TYPE_PRIVATE_HH michael@0: #define HB_OPEN_TYPE_PRIVATE_HH michael@0: michael@0: #include "hb-private.hh" michael@0: michael@0: michael@0: namespace OT { michael@0: michael@0: michael@0: michael@0: /* michael@0: * Casts michael@0: */ michael@0: michael@0: /* Cast to struct T, reference to reference */ michael@0: template michael@0: inline const Type& CastR(const TObject &X) michael@0: { return reinterpret_cast (X); } michael@0: template michael@0: inline Type& CastR(TObject &X) michael@0: { return reinterpret_cast (X); } michael@0: michael@0: /* Cast to struct T, pointer to pointer */ michael@0: template michael@0: inline const Type* CastP(const TObject *X) michael@0: { return reinterpret_cast (X); } michael@0: template michael@0: inline Type* CastP(TObject *X) michael@0: { return reinterpret_cast (X); } michael@0: michael@0: /* StructAtOffset(P,Ofs) returns the struct T& that is placed at memory michael@0: * location pointed to by P plus Ofs bytes. */ michael@0: template michael@0: inline const Type& StructAtOffset(const void *P, unsigned int offset) michael@0: { return * reinterpret_cast ((const char *) P + offset); } michael@0: template michael@0: inline Type& StructAtOffset(void *P, unsigned int offset) michael@0: { return * reinterpret_cast ((char *) P + offset); } michael@0: michael@0: /* StructAfter(X) returns the struct T& that is placed after X. michael@0: * Works with X of variable size also. X must implement get_size() */ michael@0: template michael@0: inline const Type& StructAfter(const TObject &X) michael@0: { return StructAtOffset(&X, X.get_size()); } michael@0: template michael@0: inline Type& StructAfter(TObject &X) michael@0: { return StructAtOffset(&X, X.get_size()); } michael@0: michael@0: michael@0: michael@0: /* michael@0: * Size checking michael@0: */ michael@0: michael@0: /* Check _assertion in a method environment */ michael@0: #define _DEFINE_INSTANCE_ASSERTION1(_line, _assertion) \ michael@0: inline void _instance_assertion_on_line_##_line (void) const \ michael@0: { \ michael@0: ASSERT_STATIC (_assertion); \ michael@0: ASSERT_INSTANCE_POD (*this); /* Make sure it's POD. */ \ michael@0: } michael@0: # define _DEFINE_INSTANCE_ASSERTION0(_line, _assertion) _DEFINE_INSTANCE_ASSERTION1 (_line, _assertion) michael@0: # define DEFINE_INSTANCE_ASSERTION(_assertion) _DEFINE_INSTANCE_ASSERTION0 (__LINE__, _assertion) michael@0: michael@0: /* Check that _code compiles in a method environment */ michael@0: #define _DEFINE_COMPILES_ASSERTION1(_line, _code) \ michael@0: inline void _compiles_assertion_on_line_##_line (void) const \ michael@0: { _code; } michael@0: # define _DEFINE_COMPILES_ASSERTION0(_line, _code) _DEFINE_COMPILES_ASSERTION1 (_line, _code) michael@0: # define DEFINE_COMPILES_ASSERTION(_code) _DEFINE_COMPILES_ASSERTION0 (__LINE__, _code) michael@0: michael@0: michael@0: #define DEFINE_SIZE_STATIC(size) \ michael@0: DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size)); \ michael@0: static const unsigned int static_size = (size); \ michael@0: static const unsigned int min_size = (size) michael@0: michael@0: /* Size signifying variable-sized array */ michael@0: #define VAR 1 michael@0: michael@0: #define DEFINE_SIZE_UNION(size, _member) \ michael@0: DEFINE_INSTANCE_ASSERTION (this->u._member.static_size == (size)); \ michael@0: static const unsigned int min_size = (size) michael@0: michael@0: #define DEFINE_SIZE_MIN(size) \ michael@0: DEFINE_INSTANCE_ASSERTION (sizeof (*this) >= (size)); \ michael@0: static const unsigned int min_size = (size) michael@0: michael@0: #define DEFINE_SIZE_ARRAY(size, array) \ michael@0: DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (array[0])); \ michael@0: DEFINE_COMPILES_ASSERTION ((void) array[0].static_size) \ michael@0: static const unsigned int min_size = (size) michael@0: michael@0: #define DEFINE_SIZE_ARRAY2(size, array1, array2) \ michael@0: DEFINE_INSTANCE_ASSERTION (sizeof (*this) == (size) + sizeof (this->array1[0]) + sizeof (this->array2[0])); \ michael@0: DEFINE_COMPILES_ASSERTION ((void) array1[0].static_size; (void) array2[0].static_size) \ michael@0: static const unsigned int min_size = (size) michael@0: michael@0: michael@0: michael@0: /* michael@0: * Null objects michael@0: */ michael@0: michael@0: /* Global nul-content Null pool. Enlarge as necessary. */ michael@0: /* TODO This really should be a extern HB_INTERNAL and defined somewhere... */ michael@0: static const void *_NullPool[64 / sizeof (void *)]; michael@0: michael@0: /* Generic nul-content Null objects. */ michael@0: template michael@0: static inline const Type& Null (void) { michael@0: ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool)); michael@0: return *CastP (_NullPool); michael@0: } michael@0: michael@0: /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */ michael@0: #define DEFINE_NULL_DATA(Type, data) \ michael@0: static const char _Null##Type[sizeof (Type) + 1] = data; /* +1 is for nul-termination in data */ \ michael@0: template <> \ michael@0: inline const Type& Null (void) { \ michael@0: return *CastP (_Null##Type); \ michael@0: } /* The following line really exists such that we end in a place needing semicolon */ \ michael@0: ASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type)) michael@0: michael@0: /* Accessor macro. */ michael@0: #define Null(Type) Null() michael@0: michael@0: michael@0: michael@0: /* michael@0: * Sanitize michael@0: */ michael@0: michael@0: #ifndef HB_DEBUG_SANITIZE michael@0: #define HB_DEBUG_SANITIZE (HB_DEBUG+0) michael@0: #endif michael@0: michael@0: michael@0: #define TRACE_SANITIZE(this) \ michael@0: hb_auto_trace_t trace \ michael@0: (&c->debug_depth, c->get_name (), this, HB_FUNC, \ michael@0: ""); michael@0: michael@0: /* This limits sanitizing time on really broken fonts. */ michael@0: #ifndef HB_SANITIZE_MAX_EDITS michael@0: #define HB_SANITIZE_MAX_EDITS 100 michael@0: #endif michael@0: michael@0: struct hb_sanitize_context_t michael@0: { michael@0: inline const char *get_name (void) { return "SANITIZE"; } michael@0: static const unsigned int max_debug_depth = HB_DEBUG_SANITIZE; michael@0: typedef bool return_t; michael@0: template michael@0: inline return_t dispatch (const T &obj) { return obj.sanitize (this); } michael@0: static return_t default_return_value (void) { return true; } michael@0: bool stop_sublookup_iteration (const return_t r HB_UNUSED) const { return false; } michael@0: michael@0: inline void init (hb_blob_t *b) michael@0: { michael@0: this->blob = hb_blob_reference (b); michael@0: this->writable = false; michael@0: } michael@0: michael@0: inline void start_processing (void) michael@0: { michael@0: this->start = hb_blob_get_data (this->blob, NULL); michael@0: this->end = this->start + hb_blob_get_length (this->blob); michael@0: this->edit_count = 0; michael@0: this->debug_depth = 0; michael@0: michael@0: DEBUG_MSG_LEVEL (SANITIZE, this->blob, 0, +1, michael@0: "start [%p..%p] (%lu bytes)", michael@0: this->start, this->end, michael@0: (unsigned long) (this->end - this->start)); michael@0: } michael@0: michael@0: inline void end_processing (void) michael@0: { michael@0: DEBUG_MSG_LEVEL (SANITIZE, this->blob, 0, -1, michael@0: "end [%p..%p] %u edit requests", michael@0: this->start, this->end, this->edit_count); michael@0: michael@0: hb_blob_destroy (this->blob); michael@0: this->blob = NULL; michael@0: this->start = this->end = NULL; michael@0: } michael@0: michael@0: inline bool check_range (const void *base, unsigned int len) const michael@0: { michael@0: const char *p = (const char *) base; michael@0: michael@0: hb_auto_trace_t trace michael@0: (&this->debug_depth, "SANITIZE", this->blob, NULL, michael@0: "check_range [%p..%p] (%d bytes) in [%p..%p]", michael@0: p, p + len, len, michael@0: this->start, this->end); michael@0: michael@0: return TRACE_RETURN (likely (this->start <= p && p <= this->end && (unsigned int) (this->end - p) >= len)); michael@0: } michael@0: michael@0: inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const michael@0: { michael@0: const char *p = (const char *) base; michael@0: bool overflows = _hb_unsigned_int_mul_overflows (len, record_size); michael@0: michael@0: hb_auto_trace_t trace michael@0: (&this->debug_depth, "SANITIZE", this->blob, NULL, michael@0: "check_array [%p..%p] (%d*%d=%ld bytes) in [%p..%p]", michael@0: p, p + (record_size * len), record_size, len, (unsigned long) record_size * len, michael@0: this->start, this->end); michael@0: michael@0: return TRACE_RETURN (likely (!overflows && this->check_range (base, record_size * len))); michael@0: } michael@0: michael@0: template michael@0: inline bool check_struct (const Type *obj) const michael@0: { michael@0: return likely (this->check_range (obj, obj->min_size)); michael@0: } michael@0: michael@0: inline bool may_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED) michael@0: { michael@0: if (this->edit_count >= HB_SANITIZE_MAX_EDITS) michael@0: return false; michael@0: michael@0: const char *p = (const char *) base; michael@0: this->edit_count++; michael@0: michael@0: hb_auto_trace_t trace michael@0: (&this->debug_depth, "SANITIZE", this->blob, NULL, michael@0: "may_edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s", michael@0: this->edit_count, michael@0: p, p + len, len, michael@0: this->start, this->end, michael@0: this->writable ? "GRANTED" : "DENIED"); michael@0: michael@0: return TRACE_RETURN (this->writable); michael@0: } michael@0: michael@0: mutable unsigned int debug_depth; michael@0: const char *start, *end; michael@0: bool writable; michael@0: unsigned int edit_count; michael@0: hb_blob_t *blob; michael@0: }; michael@0: michael@0: michael@0: michael@0: /* Template to sanitize an object. */ michael@0: template michael@0: struct Sanitizer michael@0: { michael@0: static hb_blob_t *sanitize (hb_blob_t *blob) { michael@0: hb_sanitize_context_t c[1] = {{0}}; michael@0: bool sane; michael@0: michael@0: /* TODO is_sane() stuff */ michael@0: michael@0: c->init (blob); michael@0: michael@0: retry: michael@0: DEBUG_MSG_FUNC (SANITIZE, blob, "start"); michael@0: michael@0: c->start_processing (); michael@0: michael@0: if (unlikely (!c->start)) { michael@0: c->end_processing (); michael@0: return blob; michael@0: } michael@0: michael@0: Type *t = CastP (const_cast (c->start)); michael@0: michael@0: sane = t->sanitize (c); michael@0: if (sane) { michael@0: if (c->edit_count) { michael@0: DEBUG_MSG_FUNC (SANITIZE, blob, "passed first round with %d edits; going for second round", c->edit_count); michael@0: michael@0: /* sanitize again to ensure no toe-stepping */ michael@0: c->edit_count = 0; michael@0: sane = t->sanitize (c); michael@0: if (c->edit_count) { michael@0: DEBUG_MSG_FUNC (SANITIZE, blob, "requested %d edits in second round; FAILLING", c->edit_count); michael@0: sane = false; michael@0: } michael@0: } michael@0: } else { michael@0: unsigned int edit_count = c->edit_count; michael@0: if (edit_count && !c->writable) { michael@0: c->start = hb_blob_get_data_writable (blob, NULL); michael@0: c->end = c->start + hb_blob_get_length (blob); michael@0: michael@0: if (c->start) { michael@0: c->writable = true; michael@0: /* ok, we made it writable by relocating. try again */ michael@0: DEBUG_MSG_FUNC (SANITIZE, blob, "retry"); michael@0: goto retry; michael@0: } michael@0: } michael@0: } michael@0: michael@0: c->end_processing (); michael@0: michael@0: DEBUG_MSG_FUNC (SANITIZE, blob, sane ? "PASSED" : "FAILED"); michael@0: if (sane) michael@0: return blob; michael@0: else { michael@0: hb_blob_destroy (blob); michael@0: return hb_blob_get_empty (); michael@0: } michael@0: } michael@0: michael@0: static const Type* lock_instance (hb_blob_t *blob) { michael@0: hb_blob_make_immutable (blob); michael@0: const char *base = hb_blob_get_data (blob, NULL); michael@0: return unlikely (!base) ? &Null(Type) : CastP (base); michael@0: } michael@0: }; michael@0: michael@0: michael@0: michael@0: /* michael@0: * Serialize michael@0: */ michael@0: michael@0: #ifndef HB_DEBUG_SERIALIZE michael@0: #define HB_DEBUG_SERIALIZE (HB_DEBUG+0) michael@0: #endif michael@0: michael@0: michael@0: #define TRACE_SERIALIZE(this) \ michael@0: hb_auto_trace_t trace \ michael@0: (&c->debug_depth, "SERIALIZE", c, HB_FUNC, \ michael@0: ""); michael@0: michael@0: michael@0: struct hb_serialize_context_t michael@0: { michael@0: inline hb_serialize_context_t (void *start, unsigned int size) michael@0: { michael@0: this->start = (char *) start; michael@0: this->end = this->start + size; michael@0: michael@0: this->ran_out_of_room = false; michael@0: this->head = this->start; michael@0: this->debug_depth = 0; michael@0: } michael@0: michael@0: template michael@0: inline Type *start_serialize (void) michael@0: { michael@0: DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, +1, michael@0: "start [%p..%p] (%lu bytes)", michael@0: this->start, this->end, michael@0: (unsigned long) (this->end - this->start)); michael@0: michael@0: return start_embed (); michael@0: } michael@0: michael@0: inline void end_serialize (void) michael@0: { michael@0: DEBUG_MSG_LEVEL (SERIALIZE, this->start, 0, -1, michael@0: "end [%p..%p] serialized %d bytes; %s", michael@0: this->start, this->end, michael@0: (int) (this->head - this->start), michael@0: this->ran_out_of_room ? "RAN OUT OF ROOM" : "did not ran out of room"); michael@0: michael@0: } michael@0: michael@0: template michael@0: inline Type *copy (void) michael@0: { michael@0: assert (!this->ran_out_of_room); michael@0: unsigned int len = this->head - this->start; michael@0: void *p = malloc (len); michael@0: if (p) michael@0: memcpy (p, this->start, len); michael@0: return reinterpret_cast (p); michael@0: } michael@0: michael@0: template michael@0: inline Type *allocate_size (unsigned int size) michael@0: { michael@0: if (unlikely (this->ran_out_of_room || this->end - this->head < ptrdiff_t (size))) { michael@0: this->ran_out_of_room = true; michael@0: return NULL; michael@0: } michael@0: memset (this->head, 0, size); michael@0: char *ret = this->head; michael@0: this->head += size; michael@0: return reinterpret_cast (ret); michael@0: } michael@0: michael@0: template michael@0: inline Type *allocate_min (void) michael@0: { michael@0: return this->allocate_size (Type::min_size); michael@0: } michael@0: michael@0: template michael@0: inline Type *start_embed (void) michael@0: { michael@0: Type *ret = reinterpret_cast (this->head); michael@0: return ret; michael@0: } michael@0: michael@0: template michael@0: inline Type *embed (const Type &obj) michael@0: { michael@0: unsigned int size = obj.get_size (); michael@0: Type *ret = this->allocate_size (size); michael@0: if (unlikely (!ret)) return NULL; michael@0: memcpy (ret, obj, size); michael@0: return ret; michael@0: } michael@0: michael@0: template michael@0: inline Type *extend_min (Type &obj) michael@0: { michael@0: unsigned int size = obj.min_size; michael@0: assert (this->start <= (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head); michael@0: if (unlikely (!this->allocate_size (((char *) &obj) + size - this->head))) return NULL; michael@0: return reinterpret_cast (&obj); michael@0: } michael@0: michael@0: template michael@0: inline Type *extend (Type &obj) michael@0: { michael@0: unsigned int size = obj.get_size (); michael@0: assert (this->start < (char *) &obj && (char *) &obj <= this->head && (char *) &obj + size >= this->head); michael@0: if (unlikely (!this->allocate_size (((char *) &obj) + size - this->head))) return NULL; michael@0: return reinterpret_cast (&obj); michael@0: } michael@0: michael@0: inline void truncate (void *head) michael@0: { michael@0: assert (this->start < head && head <= this->head); michael@0: this->head = (char *) head; michael@0: } michael@0: michael@0: unsigned int debug_depth; michael@0: char *start, *end, *head; michael@0: bool ran_out_of_room; michael@0: }; michael@0: michael@0: template michael@0: struct Supplier michael@0: { michael@0: inline Supplier (const Type *array, unsigned int len_) michael@0: { michael@0: head = array; michael@0: len = len_; michael@0: } michael@0: inline const Type operator [] (unsigned int i) const michael@0: { michael@0: if (unlikely (i >= len)) return Type (); michael@0: return head[i]; michael@0: } michael@0: michael@0: inline void advance (unsigned int count) michael@0: { michael@0: if (unlikely (count > len)) michael@0: count = len; michael@0: len -= count; michael@0: head += count; michael@0: } michael@0: michael@0: private: michael@0: inline Supplier (const Supplier &); /* Disallow copy */ michael@0: inline Supplier& operator= (const Supplier &); /* Disallow copy */ michael@0: michael@0: unsigned int len; michael@0: const Type *head; michael@0: }; michael@0: michael@0: michael@0: michael@0: michael@0: /* michael@0: * michael@0: * The OpenType Font File: Data Types michael@0: */ michael@0: michael@0: michael@0: /* "The following data types are used in the OpenType font file. michael@0: * All OpenType fonts use Motorola-style byte ordering (Big Endian):" */ michael@0: michael@0: /* michael@0: * Int types michael@0: */ michael@0: michael@0: michael@0: template struct BEInt; michael@0: michael@0: template michael@0: struct BEInt michael@0: { michael@0: public: michael@0: inline void set (Type i) { hb_be_uint16_put (v,i); } michael@0: inline operator Type (void) const { return hb_be_uint16_get (v); } michael@0: inline bool operator == (const BEInt& o) const { return hb_be_uint16_eq (v, o.v); } michael@0: inline bool operator != (const BEInt& o) const { return !(*this == o); } michael@0: private: uint8_t v[2]; michael@0: }; michael@0: template michael@0: struct BEInt michael@0: { michael@0: public: michael@0: inline void set (Type i) { hb_be_uint32_put (v,i); } michael@0: inline operator Type (void) const { return hb_be_uint32_get (v); } michael@0: inline bool operator == (const BEInt& o) const { return hb_be_uint32_eq (v, o.v); } michael@0: inline bool operator != (const BEInt& o) const { return !(*this == o); } michael@0: private: uint8_t v[4]; michael@0: }; michael@0: template michael@0: struct BEInt michael@0: { michael@0: public: michael@0: inline void set (Type i) { hb_be_uint24_put (v,i); } michael@0: inline operator Type (void) const { return hb_be_uint24_get (v); } michael@0: inline bool operator == (const BEInt& o) const { return hb_be_uint24_eq (v, o.v); } michael@0: inline bool operator != (const BEInt& o) const { return !(*this == o); } michael@0: private: uint8_t v[3]; michael@0: }; michael@0: michael@0: /* Integer types in big-endian order and no alignment requirement */ michael@0: template michael@0: struct IntType michael@0: { michael@0: inline void set (Type i) { v.set (i); } michael@0: inline operator Type(void) const { return v; } michael@0: inline bool operator == (const IntType &o) const { return v == o.v; } michael@0: inline bool operator != (const IntType &o) const { return v != o.v; } michael@0: static inline int cmp (const IntType *a, const IntType *b) { return b->cmp (*a); } michael@0: inline int cmp (IntType va) const { Type a = va; Type b = v; return a < b ? -1 : a == b ? 0 : +1; } michael@0: inline int cmp (Type a) const { Type b = v; return a < b ? -1 : a == b ? 0 : +1; } michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (likely (c->check_struct (this))); michael@0: } michael@0: protected: michael@0: BEInt v; michael@0: public: michael@0: DEFINE_SIZE_STATIC (Size); michael@0: }; michael@0: michael@0: typedef IntType USHORT; /* 16-bit unsigned integer. */ michael@0: typedef IntType SHORT; /* 16-bit signed integer. */ michael@0: typedef IntType ULONG; /* 32-bit unsigned integer. */ michael@0: typedef IntType LONG; /* 32-bit signed integer. */ michael@0: typedef IntType UINT24; /* 24-bit unsigned integer. */ michael@0: michael@0: /* 16-bit signed integer (SHORT) that describes a quantity in FUnits. */ michael@0: typedef SHORT FWORD; michael@0: michael@0: /* 16-bit unsigned integer (USHORT) that describes a quantity in FUnits. */ michael@0: typedef USHORT UFWORD; michael@0: michael@0: /* Date represented in number of seconds since 12:00 midnight, January 1, michael@0: * 1904. The value is represented as a signed 64-bit integer. */ michael@0: struct LONGDATETIME michael@0: { michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (likely (c->check_struct (this))); michael@0: } michael@0: protected: michael@0: LONG major; michael@0: ULONG minor; michael@0: public: michael@0: DEFINE_SIZE_STATIC (8); michael@0: }; michael@0: michael@0: /* Array of four uint8s (length = 32 bits) used to identify a script, language michael@0: * system, feature, or baseline */ michael@0: struct Tag : ULONG michael@0: { michael@0: /* What the char* converters return is NOT nul-terminated. Print using "%.4s" */ michael@0: inline operator const char* (void) const { return reinterpret_cast (&this->v); } michael@0: inline operator char* (void) { return reinterpret_cast (&this->v); } michael@0: public: michael@0: DEFINE_SIZE_STATIC (4); michael@0: }; michael@0: DEFINE_NULL_DATA (Tag, " "); michael@0: michael@0: /* Glyph index number, same as uint16 (length = 16 bits) */ michael@0: typedef USHORT GlyphID; michael@0: michael@0: /* Script/language-system/feature index */ michael@0: struct Index : USHORT { michael@0: static const unsigned int NOT_FOUND_INDEX = 0xFFFF; michael@0: }; michael@0: DEFINE_NULL_DATA (Index, "\xff\xff"); michael@0: michael@0: /* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */ michael@0: struct Offset : USHORT michael@0: { michael@0: inline bool is_null (void) const { return 0 == *this; } michael@0: public: michael@0: DEFINE_SIZE_STATIC (2); michael@0: }; michael@0: michael@0: /* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */ michael@0: struct LongOffset : ULONG michael@0: { michael@0: inline bool is_null (void) const { return 0 == *this; } michael@0: public: michael@0: DEFINE_SIZE_STATIC (4); michael@0: }; michael@0: michael@0: michael@0: /* CheckSum */ michael@0: struct CheckSum : ULONG michael@0: { michael@0: /* This is reference implementation from the spec. */ michael@0: static inline uint32_t CalcTableChecksum (const ULONG *Table, uint32_t Length) michael@0: { michael@0: uint32_t Sum = 0L; michael@0: const ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size; michael@0: michael@0: while (Table < EndPtr) michael@0: Sum += *Table++; michael@0: return Sum; michael@0: } michael@0: michael@0: /* Note: data should be 4byte aligned and have 4byte padding at the end. */ michael@0: inline void set_for_data (const void *data, unsigned int length) michael@0: { set (CalcTableChecksum ((const ULONG *) data, length)); } michael@0: michael@0: public: michael@0: DEFINE_SIZE_STATIC (4); michael@0: }; michael@0: michael@0: michael@0: /* michael@0: * Version Numbers michael@0: */ michael@0: michael@0: struct FixedVersion michael@0: { michael@0: inline uint32_t to_int (void) const { return (major << 16) + minor; } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (c->check_struct (this)); michael@0: } michael@0: michael@0: USHORT major; michael@0: USHORT minor; michael@0: public: michael@0: DEFINE_SIZE_STATIC (4); michael@0: }; michael@0: michael@0: michael@0: michael@0: /* michael@0: * Template subclasses of Offset and LongOffset that do the dereferencing. michael@0: * Use: (base+offset) michael@0: */ michael@0: michael@0: template michael@0: struct GenericOffsetTo : OffsetType michael@0: { michael@0: inline const Type& operator () (const void *base) const michael@0: { michael@0: unsigned int offset = *this; michael@0: if (unlikely (!offset)) return Null(Type); michael@0: return StructAtOffset (base, offset); michael@0: } michael@0: michael@0: inline Type& serialize (hb_serialize_context_t *c, void *base) michael@0: { michael@0: Type *t = c->start_embed (); michael@0: this->set ((char *) t - (char *) base); /* TODO(serialize) Overflow? */ michael@0: return *t; michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c, void *base) { michael@0: TRACE_SANITIZE (this); michael@0: if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false); michael@0: unsigned int offset = *this; michael@0: if (unlikely (!offset)) return TRACE_RETURN (true); michael@0: Type &obj = StructAtOffset (base, offset); michael@0: return TRACE_RETURN (likely (obj.sanitize (c)) || neuter (c)); michael@0: } michael@0: template michael@0: inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) { michael@0: TRACE_SANITIZE (this); michael@0: if (unlikely (!c->check_struct (this))) return TRACE_RETURN (false); michael@0: unsigned int offset = *this; michael@0: if (unlikely (!offset)) return TRACE_RETURN (true); michael@0: Type &obj = StructAtOffset (base, offset); michael@0: return TRACE_RETURN (likely (obj.sanitize (c, user_data)) || neuter (c)); michael@0: } michael@0: michael@0: inline bool try_set (hb_sanitize_context_t *c, const OffsetType &v) { michael@0: if (c->may_edit (this, this->static_size)) { michael@0: this->set (v); michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: /* Set the offset to Null */ michael@0: inline bool neuter (hb_sanitize_context_t *c) { michael@0: if (c->may_edit (this, this->static_size)) { michael@0: this->set (0); /* 0 is Null offset */ michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: }; michael@0: template michael@0: inline const Type& operator + (const Base &base, const GenericOffsetTo &offset) { return offset (base); } michael@0: template michael@0: inline Type& operator + (Base &base, GenericOffsetTo &offset) { return offset (base); } michael@0: michael@0: template michael@0: struct OffsetTo : GenericOffsetTo {}; michael@0: michael@0: template michael@0: struct LongOffsetTo : GenericOffsetTo {}; michael@0: michael@0: michael@0: /* michael@0: * Array Types michael@0: */ michael@0: michael@0: template michael@0: struct GenericArrayOf michael@0: { michael@0: const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const michael@0: { michael@0: unsigned int count = len; michael@0: if (unlikely (start_offset > count)) michael@0: count = 0; michael@0: else michael@0: count -= start_offset; michael@0: count = MIN (count, *pcount); michael@0: *pcount = count; michael@0: return array + start_offset; michael@0: } michael@0: michael@0: inline const Type& operator [] (unsigned int i) const michael@0: { michael@0: if (unlikely (i >= len)) return Null(Type); michael@0: return array[i]; michael@0: } michael@0: inline Type& operator [] (unsigned int i) michael@0: { michael@0: return array[i]; michael@0: } michael@0: inline unsigned int get_size (void) const michael@0: { return len.static_size + len * Type::static_size; } michael@0: michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: unsigned int items_len) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); michael@0: len.set (items_len); /* TODO(serialize) Overflow? */ michael@0: if (unlikely (!c->extend (*this))) return TRACE_RETURN (false); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &items, michael@0: unsigned int items_len) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!serialize (c, items_len))) return TRACE_RETURN (false); michael@0: for (unsigned int i = 0; i < items_len; i++) michael@0: array[i] = items[i]; michael@0: items.advance (items_len); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); michael@0: michael@0: /* Note: for structs that do not reference other structs, michael@0: * we do not need to call their sanitize() as we already did michael@0: * a bound check on the aggregate array size. We just include michael@0: * a small unreachable expression to make sure the structs michael@0: * pointed to do have a simple sanitize(), ie. they do not michael@0: * reference other structs via offsets. michael@0: */ michael@0: (void) (false && array[0].sanitize (c)); michael@0: michael@0: return TRACE_RETURN (true); michael@0: } michael@0: inline bool sanitize (hb_sanitize_context_t *c, void *base) { michael@0: TRACE_SANITIZE (this); michael@0: if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); michael@0: unsigned int count = len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: if (unlikely (!array[i].sanitize (c, base))) michael@0: return TRACE_RETURN (false); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: template michael@0: inline bool sanitize (hb_sanitize_context_t *c, void *base, T user_data) { michael@0: TRACE_SANITIZE (this); michael@0: if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); michael@0: unsigned int count = len; michael@0: for (unsigned int i = 0; i < count; i++) michael@0: if (unlikely (!array[i].sanitize (c, base, user_data))) michael@0: return TRACE_RETURN (false); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: private: michael@0: inline bool sanitize_shallow (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (c->check_struct (this) && c->check_array (this, Type::static_size, len)); michael@0: } michael@0: michael@0: public: michael@0: LenType len; michael@0: Type array[VAR]; michael@0: public: michael@0: DEFINE_SIZE_ARRAY (sizeof (LenType), array); michael@0: }; michael@0: michael@0: /* An array with a USHORT number of elements. */ michael@0: template michael@0: struct ArrayOf : GenericArrayOf {}; michael@0: michael@0: /* An array with a ULONG number of elements. */ michael@0: template michael@0: struct LongArrayOf : GenericArrayOf {}; michael@0: michael@0: /* Array of Offset's */ michael@0: template michael@0: struct OffsetArrayOf : ArrayOf > {}; michael@0: michael@0: /* Array of LongOffset's */ michael@0: template michael@0: struct LongOffsetArrayOf : ArrayOf > {}; michael@0: michael@0: /* LongArray of LongOffset's */ michael@0: template michael@0: struct LongOffsetLongArrayOf : LongArrayOf > {}; michael@0: michael@0: /* Array of offsets relative to the beginning of the array itself. */ michael@0: template michael@0: struct OffsetListOf : OffsetArrayOf michael@0: { michael@0: inline const Type& operator [] (unsigned int i) const michael@0: { michael@0: if (unlikely (i >= this->len)) return Null(Type); michael@0: return this+this->array[i]; michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (OffsetArrayOf::sanitize (c, this)); michael@0: } michael@0: template michael@0: inline bool sanitize (hb_sanitize_context_t *c, T user_data) { michael@0: TRACE_SANITIZE (this); michael@0: return TRACE_RETURN (OffsetArrayOf::sanitize (c, this, user_data)); michael@0: } michael@0: }; michael@0: michael@0: michael@0: /* An array with a USHORT number of elements, michael@0: * starting at second element. */ michael@0: template michael@0: struct HeadlessArrayOf michael@0: { michael@0: inline const Type& operator [] (unsigned int i) const michael@0: { michael@0: if (unlikely (i >= len || !i)) return Null(Type); michael@0: return array[i-1]; michael@0: } michael@0: inline unsigned int get_size (void) const michael@0: { return len.static_size + (len ? len - 1 : 0) * Type::static_size; } michael@0: michael@0: inline bool serialize (hb_serialize_context_t *c, michael@0: Supplier &items, michael@0: unsigned int items_len) michael@0: { michael@0: TRACE_SERIALIZE (this); michael@0: if (unlikely (!c->extend_min (*this))) return TRACE_RETURN (false); michael@0: len.set (items_len); /* TODO(serialize) Overflow? */ michael@0: if (unlikely (!items_len)) return TRACE_RETURN (true); michael@0: if (unlikely (!c->extend (*this))) return TRACE_RETURN (false); michael@0: for (unsigned int i = 0; i < items_len - 1; i++) michael@0: array[i] = items[i]; michael@0: items.advance (items_len - 1); michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: inline bool sanitize_shallow (hb_sanitize_context_t *c) { michael@0: return c->check_struct (this) michael@0: && c->check_array (this, Type::static_size, len); michael@0: } michael@0: michael@0: inline bool sanitize (hb_sanitize_context_t *c) { michael@0: TRACE_SANITIZE (this); michael@0: if (unlikely (!sanitize_shallow (c))) return TRACE_RETURN (false); michael@0: michael@0: /* Note: for structs that do not reference other structs, michael@0: * we do not need to call their sanitize() as we already did michael@0: * a bound check on the aggregate array size. We just include michael@0: * a small unreachable expression to make sure the structs michael@0: * pointed to do have a simple sanitize(), ie. they do not michael@0: * reference other structs via offsets. michael@0: */ michael@0: (void) (false && array[0].sanitize (c)); michael@0: michael@0: return TRACE_RETURN (true); michael@0: } michael@0: michael@0: USHORT len; michael@0: Type array[VAR]; michael@0: public: michael@0: DEFINE_SIZE_ARRAY (sizeof (USHORT), array); michael@0: }; michael@0: michael@0: michael@0: /* An array with sorted elements. Supports binary searching. */ michael@0: template michael@0: struct SortedArrayOf : ArrayOf { michael@0: michael@0: template michael@0: inline int search (const SearchType &x) const michael@0: { michael@0: /* Hand-coded bsearch here since this is in the hot inner loop. */ michael@0: int min = 0, max = (int) this->len - 1; michael@0: while (min <= max) michael@0: { michael@0: int mid = (min + max) / 2; michael@0: int c = this->array[mid].cmp (x); michael@0: if (c < 0) michael@0: max = mid - 1; michael@0: else if (c > 0) michael@0: min = mid + 1; michael@0: else michael@0: return mid; michael@0: } michael@0: return -1; michael@0: } michael@0: }; michael@0: michael@0: michael@0: } /* namespace OT */ michael@0: michael@0: michael@0: #endif /* HB_OPEN_TYPE_PRIVATE_HH */