hb-open-type-private.hh revision 458ecbb60bb7e8e32aca62a562586d921d5396aa
164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod/*
2f9b37727985191c9b4aedb0e9835736027e59260Behdad Esfahbod * Copyright (C) 2007,2008,2009,2010  Red Hat, Inc.
364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
4c755cb3e3ac55156d0d2ec05adea7a650b97cc41Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Permission is hereby granted, without written agreement and without
764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * software and its documentation for any purpose, provided that the
964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
1064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * all copies of this software.
1164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
1264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
1364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
1464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
1564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
1664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * DAMAGE.
1764aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
1864aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
1964aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
2064aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
2164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
2264aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
2364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
2464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod * Red Hat Author(s): Behdad Esfahbod
2564aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod */
2664aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod
275f5b24f99f52bbc922e238b65c06061ba07c8548Behdad Esfahbod#ifndef HB_OPEN_TYPES_PRIVATE_HH
285f5b24f99f52bbc922e238b65c06061ba07c8548Behdad Esfahbod#define HB_OPEN_TYPES_PRIVATE_HH
2912c4568c680ea2b9b98a16a8b7402ca185c90ef6Behdad Esfahbod
302098a021a826e76ee27d5db74e32738d7d1c3d30Behdad Esfahbod#include "hb-private.h"
3112c4568c680ea2b9b98a16a8b7402ca185c90ef6Behdad Esfahbod
3270de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod#include "hb-blob.h"
3370de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
34a16ecbf0564a6e2576da22c12827f3c0719da549Behdad Esfahbod
35c85c3620675f38ffdca59134aeec2641485f40caBehdad Esfahbod/* Table/script/language-system/feature/... not found */
36706ab25a4cb043d46e6088aa0a7184ee200276c9Behdad Esfahbod#define NO_INDEX		((unsigned int) 0xFFFF)
375a0b791184cf6ef39eae0570e14aca21abc32845Behdad Esfahbod
38706ab25a4cb043d46e6088aa0a7184ee200276c9Behdad Esfahbod
39a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod
40196598bbccff08415ff5192314cba044df258cadBehdad Esfahbod/*
41196598bbccff08415ff5192314cba044df258cadBehdad Esfahbod * Casts
42196598bbccff08415ff5192314cba044df258cadBehdad Esfahbod */
43196598bbccff08415ff5192314cba044df258cadBehdad Esfahbod
44187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod/* Cast to struct T, reference to reference */
45a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodtemplate<typename Type, typename TObject>
46187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodinline const Type& CastR(const TObject &X)
47a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod{ return reinterpret_cast<const Type&> (X); }
48a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodtemplate<typename Type, typename TObject>
49187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodinline Type& CastR(TObject &X)
50a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod{ return reinterpret_cast<Type&> (X); }
51196598bbccff08415ff5192314cba044df258cadBehdad Esfahbod
52187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod/* Cast to struct T, pointer to pointer */
53187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodtemplate<typename Type, typename TObject>
54187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodinline const Type* CastP(const TObject *X)
55187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod{ return reinterpret_cast<const Type*> (X); }
56187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodtemplate<typename Type, typename TObject>
57187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodinline Type* CastP(TObject *X)
58187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod{ return reinterpret_cast<Type*> (X); }
59187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod
6009766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod/* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
6109766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod * location pointed to by P plus Ofs bytes. */
6209766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbodtemplate<typename Type>
6309766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbodinline const Type& StructAtOffset(const void *P, unsigned int offset)
64a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod{ return * reinterpret_cast<const Type*> ((const char *) P + offset); }
6509766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbodtemplate<typename Type>
6609766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbodinline Type& StructAtOffset(void *P, unsigned int offset)
67a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod{ return * reinterpret_cast<Type*> ((char *) P + offset); }
6870de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
692e2f43edf2f49f4047e28b1ce2ea95938536de9cBehdad Esfahbod/* StructAfter<T>(X) returns the struct T& that is placed after X.
7029c3f5e1b6212c775a7b911becd44ba093b7b0ebBehdad Esfahbod * Works with X of variable size also.  X must implement get_size() */
71e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbodtemplate<typename Type, typename TObject>
72e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbodinline const Type& StructAfter(const TObject &X)
7309766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod{ return StructAtOffset<Type>(&X, X.get_size()); }
74e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbodtemplate<typename Type, typename TObject>
75e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbodinline Type& StructAfter(TObject &X)
7609766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod{ return StructAtOffset<Type>(&X, X.get_size()); }
77a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod
78e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbod
79d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod
8070de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod/*
81e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod * Size checking
82e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod */
83e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
84596e471aa5053d955fb5d5b5923088c8814469b1Behdad Esfahbod#define _DEFINE_SIZE_ASSERTION(_assertion) \
8533afa4e2dc352f08cc094703e3f01d3ecd83b354Behdad Esfahbod  inline void _size_assertion (void) const \
86596e471aa5053d955fb5d5b5923088c8814469b1Behdad Esfahbod  { ASSERT_STATIC (_assertion); }
870abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod
880abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod
89e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod#define DEFINE_SIZE_STATIC(size) \
90596e471aa5053d955fb5d5b5923088c8814469b1Behdad Esfahbod  _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size)); \
91e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  static const unsigned int static_size = (size); \
92e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  static const unsigned int min_size = (size)
93e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
94b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod/* Size signifying variable-sized array */
95b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod#define VAR 1
96b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod
97596e471aa5053d955fb5d5b5923088c8814469b1Behdad Esfahbod#define DEFINE_SIZE_UNION(size, _member) \
98596e471aa5053d955fb5d5b5923088c8814469b1Behdad Esfahbod  _DEFINE_SIZE_ASSERTION (this->u._member.static_size == (size)); \
99596e471aa5053d955fb5d5b5923088c8814469b1Behdad Esfahbod  static const unsigned int min_size = (size)
100596e471aa5053d955fb5d5b5923088c8814469b1Behdad Esfahbod
101bea34c7cbb583cf7660776e95cab3171590b8427Behdad Esfahbod#define DEFINE_SIZE_MIN(size) \
102596e471aa5053d955fb5d5b5923088c8814469b1Behdad Esfahbod  _DEFINE_SIZE_ASSERTION (sizeof (*this) >= (size)); \
103b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  static const unsigned int min_size = (size)
104b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod
1050eb9fc6e37935707dba2bf4b3705de2161a08cb7Behdad Esfahbod#define DEFINE_SIZE_ARRAY(size, array) \
1060eb9fc6e37935707dba2bf4b3705de2161a08cb7Behdad Esfahbod  _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size) + array[0].static_size); \
107e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  static const unsigned int min_size = (size)
108e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
1090eb9fc6e37935707dba2bf4b3705de2161a08cb7Behdad Esfahbod#define DEFINE_SIZE_ARRAY2(size, array1, array2) \
110596e471aa5053d955fb5d5b5923088c8814469b1Behdad Esfahbod  _DEFINE_SIZE_ASSERTION (sizeof (*this) == (size) + this->array1[0].static_size + this->array2[0].static_size); \
111e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  static const unsigned int min_size = (size)
112e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
113e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
114e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
115e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod/*
116f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod * Null objects
117600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod */
118600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
1198b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Global nul-content Null pool.  Enlarge as necessary. */
1209d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodstatic const void *_NullPool[32 / sizeof (void *)];
1218b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
122d2c2ca8faf62fc380d4717d286556139a62d2356Behdad Esfahbod/* Generic nul-content Null objects. */
1238b8358033184198ff638ee1379093717596e162dBehdad Esfahbodtemplate <typename Type>
1249d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodstatic inline const Type& Null () {
125ed07422c33bbb52ff4d79e65986171e3f07697d8Behdad Esfahbod  ASSERT_STATIC (Type::min_size <= sizeof (_NullPool));
126187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod  return *CastP<Type> (_NullPool);
1279d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbod}
1288b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1298b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
13065f46b00333e20ab8a52a4b350747507541ec1dbBehdad Esfahbod#define DEFINE_NULL_DATA(Type, data) \
13165f46b00333e20ab8a52a4b350747507541ec1dbBehdad Esfahbodstatic const char _Null##Type[Type::min_size + 1] = data; /* +1 is for nul-termination in data */ \
1328b8358033184198ff638ee1379093717596e162dBehdad Esfahbodtemplate <> \
1339d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodinline const Type& Null<Type> () { \
134187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod  return *CastP<Type> (_Null##Type); \
135565c80bd2960366ace2d10dd71beaaf2a80213c8Behdad Esfahbod} /* The following line really exists such that we end in a place needing semicolon */ \
136bea34c7cbb583cf7660776e95cab3171590b8427Behdad EsfahbodASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type))
1378b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1388b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Accessor macro. */
1399d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbod#define Null(Type) Null<Type>()
1408b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1418b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
14220e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod/*
143dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod * Trace
14420e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod */
14520e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
146dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod
147dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodtemplate <int max_depth>
148dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodstruct hb_trace_t {
149458ecbb60bb7e8e32aca62a562586d921d5396aaBehdad Esfahbod  explicit hb_trace_t (unsigned int *pdepth, const char *what, const char *function, const void *obj) : pdepth(pdepth) {
150dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod    if (*pdepth < max_depth)
151dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod      fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, *pdepth, *pdepth, function);
152458ecbb60bb7e8e32aca62a562586d921d5396aaBehdad Esfahbod    if (max_depth) ++*pdepth;
153dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  }
154458ecbb60bb7e8e32aca62a562586d921d5396aaBehdad Esfahbod  ~hb_trace_t (void) { if (max_depth) --*pdepth; }
15520e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
15620e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod  private:
157dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  unsigned int *pdepth;
15820e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod};
159dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodtemplate <> /* Optimize when tracing is disabled */
160dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodstruct hb_trace_t<0> {
161458ecbb60bb7e8e32aca62a562586d921d5396aaBehdad Esfahbod  explicit hb_trace_t (unsigned int *pdepth, const char *what, const char *function, const void *obj) {}
16220e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod};
16320e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
16420e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
165600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
166577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod/*
167577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod * Sanitize
168577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod */
169577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
17095e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#ifndef HB_DEBUG_SANITIZE
171807c5b03a2251a3c29a520852639421783101b55Behdad Esfahbod#define HB_DEBUG_SANITIZE HB_DEBUG+0
17295e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#endif
17395e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod
17420e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
175bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define TRACE_SANITIZE() \
176458ecbb60bb7e8e32aca62a562586d921d5396aaBehdad Esfahbod	hb_trace_t<HB_DEBUG_SANITIZE> trace (&context->debug_depth, "SANITIZE", HB_FUNC, this); \
177807c5b03a2251a3c29a520852639421783101b55Behdad Esfahbod
178b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod
1791376fb7bf9ef07970f0ba13dc64d6a8ab8252762Behdad Esfahbodstruct hb_sanitize_context_t
180577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
18198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  inline void init (hb_blob_t *blob)
18298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
18398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->blob = hb_blob_reference (blob);
18498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->start = hb_blob_lock (blob);
18598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->end = this->start + hb_blob_get_length (blob);
18698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->writable = hb_blob_is_writable (blob);
18798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->edit_count = 0;
18820e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    this->debug_depth = 0;
189577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
19098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
19198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
19298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->blob, this->start, this->end, this->end - this->start);
19398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
1941376fb7bf9ef07970f0ba13dc64d6a8ab8252762Behdad Esfahbod
19598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  inline void finish (void)
19698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
19798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
19898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
19998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->blob, this->start, this->end, this->edit_count);
200577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
20198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    hb_blob_unlock (this->blob);
20298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    hb_blob_destroy (this->blob);
20398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->blob = NULL;
20498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->start = this->end = NULL;
20598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
2064f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
2074ad2cc5dec6b0639da2b1846282bdd99d06d5ff1Behdad Esfahbod  inline bool check_range (const void *base, unsigned int len) const
20898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
209a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod    const char *p = (const char *) base;
210a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod    bool ret = this->start <= p &&
211a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p <= this->end &&
212a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       (unsigned int) (this->end - p) >= len;
21398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
21420e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE) \
2154ad2cc5dec6b0639da2b1846282bdd99d06d5ff1Behdad Esfahbod      fprintf (stderr, "SANITIZE(%p) %-*d-> range [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
216a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p,
21720e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod	       this->debug_depth, this->debug_depth,
218a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p, p + len, len,
21998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->start, this->end,
22098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       ret ? "pass" : "FAIL");
22198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
22227e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod    return likely (ret);
22398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
224577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
2251cd1e117d060d38e314618b627d7663cb01ed584Behdad Esfahbod  inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
22698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
227a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod    const char *p = (const char *) base;
22898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    bool overflows = len >= ((unsigned int) -1) / record_size;
22941895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod
23020e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
23198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
232a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p,
23320e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod	       this->debug_depth, this->debug_depth,
234a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p, p + (record_size * len), record_size, len, (unsigned long) record_size * len,
23598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->start, this->end,
23698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       !overflows ? "does not overflow" : "OVERFLOWS FAIL");
237fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod
2384ad2cc5dec6b0639da2b1846282bdd99d06d5ff1Behdad Esfahbod    return likely (!overflows && this->check_range (base, record_size * len));
23998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
240254933c397f1ce9796f59689a25f9fc2e58df4eaBehdad Esfahbod
241b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  template <typename Type>
242b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  inline bool check_struct (const Type *obj) const
243b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  {
24454842374c2b291ef208c51ae1d853ec0403ccf84Behdad Esfahbod    return likely (this->check_range (obj, obj->min_size));
245b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  }
246b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod
24740cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod  inline bool can_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
24898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
249a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod    const char *p = (const char *) base;
25098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->edit_count++;
25198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
25220e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
25398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
254a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p,
25520e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod	       this->debug_depth, this->debug_depth,
25698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->edit_count,
257a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p, p + len, len,
25898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->start, this->end,
25998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->writable ? "granted" : "REJECTED");
26098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
26198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    return this->writable;
26298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
26398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
264705e215268aa95c2bc6af8af9b48b72b690ec1f7Behdad Esfahbod  unsigned int debug_depth;
26598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  const char *start, *end;
26698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  bool writable;
26798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  unsigned int edit_count;
26898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  hb_blob_t *blob;
26998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod};
270815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
271577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
272577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
2734e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod/* Template to sanitize an object. */
2744e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbodtemplate <typename Type>
2754e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbodstruct Sanitizer
2764e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod{
2774e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  static hb_blob_t *sanitize (hb_blob_t *blob) {
278705e215268aa95c2bc6af8af9b48b72b690ec1f7Behdad Esfahbod    hb_sanitize_context_t context[1] = {{0}};
2794e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    bool sane;
2804e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
281d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod    /* TODO is_sane() stuff */
2824e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
2834e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  retry:
284fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
2857d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod      fprintf (stderr, "Sanitizer %p start %s\n", blob, HB_FUNC);
2864f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
28798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    context->init (blob);
2884e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
28948146e5612f6d272d6962f6829c6d64a31edef89Behdad Esfahbod    if (unlikely (!context->start)) {
29048146e5612f6d272d6962f6829c6d64a31edef89Behdad Esfahbod      context->finish ();
29148146e5612f6d272d6962f6829c6d64a31edef89Behdad Esfahbod      return blob;
29248146e5612f6d272d6962f6829c6d64a31edef89Behdad Esfahbod    }
29348146e5612f6d272d6962f6829c6d64a31edef89Behdad Esfahbod
29427e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod    Type *t = CastP<Type> (const_cast<char *> (context->start));
2954e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
29639840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    sane = t->sanitize (context);
2974e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane) {
29827e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod      if (context->edit_count) {
299fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	if (HB_DEBUG_SANITIZE)
300fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	  fprintf (stderr, "Sanitizer %p passed first round with %d edits; doing a second round %s\n",
30127e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod		   blob, context->edit_count, HB_FUNC);
302fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod
3038b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod        /* sanitize again to ensure no toe-stepping */
30427e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod        context->edit_count = 0;
30539840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod	sane = t->sanitize (context);
30627e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod	if (context->edit_count) {
307fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	  if (HB_DEBUG_SANITIZE)
308fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	    fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
30927e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod		     blob, context->edit_count, HB_FUNC);
3104e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	  sane = false;
3114e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	}
3124e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
31398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      context->finish ();
3144e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    } else {
31527e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod      unsigned int edit_count = context->edit_count;
31698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      context->finish ();
317977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod      if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
318977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod        /* ok, we made it writable by relocating.  try again */
319fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	if (HB_DEBUG_SANITIZE)
3207d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod	  fprintf (stderr, "Sanitizer %p retry %s\n", blob, HB_FUNC);
3214e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod        goto retry;
3224e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
3234e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3244e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
325fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
3267d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod      fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", HB_FUNC);
3274e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane)
3284e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return blob;
3294e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    else {
3304e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      hb_blob_destroy (blob);
3314e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return hb_blob_create_empty ();
3324e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3334e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  }
334b435ab7e29c388e3b100f729957319931625a3a8Behdad Esfahbod
335b435ab7e29c388e3b100f729957319931625a3a8Behdad Esfahbod  static const Type* lock_instance (hb_blob_t *blob) {
336b435ab7e29c388e3b100f729957319931625a3a8Behdad Esfahbod    const char *base = hb_blob_lock (blob);
337b435ab7e29c388e3b100f729957319931625a3a8Behdad Esfahbod    return unlikely (!base) ? &Null(Type) : CastP<Type> (base);
338b435ab7e29c388e3b100f729957319931625a3a8Behdad Esfahbod  }
3394e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod};
3404e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
3412d15e72c75931398db5e027e660f1320bb979117Behdad Esfahbod
342f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod
343f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod
344b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/*
345b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *
346bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod * The OpenType Font File: Data Types
347b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod */
348b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
349b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
350b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/* "The following data types are used in the OpenType font file.
351b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *  All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
352b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
3535f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
3545f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod * Int types
3555f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
3565f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
3576b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
358e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type, int Bytes> class BEInt;
359e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
360f1aaa2a43654c28405ffd393de2cb127437c99a5Behdad Esfahbod/* LONGTERMTODO: On machines allowing unaligned access, we can make the
361f1aaa2a43654c28405ffd393de2cb127437c99a5Behdad Esfahbod * following tighter by using byteswap instructions on ints directly. */
362e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
363e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodclass BEInt<Type, 2>
364e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
365e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  public:
36601c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline class BEInt<Type,2>& operator = (Type i) { hb_be_uint16_put (v,i); return *this; }
36701c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type () const { return hb_be_uint16_get (v); }
36801c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_cmp (v, o.v); }
36901c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
370e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  private: uint8_t v[2];
371e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
372e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
373e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodclass BEInt<Type, 4>
374e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
375e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  public:
37601c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline class BEInt<Type,4>& operator = (Type i) { hb_be_uint32_put (v,i); return *this; }
37701c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type () const { return hb_be_uint32_get (v); }
37801c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_cmp (v, o.v); }
37901c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
380e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  private: uint8_t v[4];
381e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
382e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
3832467c669c2aee4de2a6621a9d06cba0262376d41Behdad Esfahbod/* Integer types in big-endian order and no alignment requirement */
384e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
385e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodstruct IntType
386e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
38701c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline void set (Type i) { v = i; }
38801c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type(void) const { return v; }
38901c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
39001c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
39139840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
392e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod    TRACE_SANITIZE ();
393b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return context->check_struct (this);
394e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  }
395a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod  protected:
396569da92bc6956f42d9b2d65c784e184fb6380efeBehdad Esfahbod  BEInt<Type, sizeof (Type)> v;
397569da92bc6956f42d9b2d65c784e184fb6380efeBehdad Esfahbod  public:
398e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  DEFINE_SIZE_STATIC (sizeof (Type));
399e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
400e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
401e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<uint16_t> USHORT;	/* 16-bit unsigned integer. */
402e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<int16_t>  SHORT;	/* 16-bit signed integer. */
403e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<uint32_t> ULONG;	/* 32-bit unsigned integer. */
404e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<int32_t>  LONG;		/* 32-bit signed integer. */
405e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
4066b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* Array of four uint8s (length = 32 bits) used to identify a script, language
4076b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod * system, feature, or baseline */
40820cc86b3592db33731de671f008d7d222776be49Behdad Esfahbodstruct Tag : ULONG
40960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
410befc022affd2386b3f46cd7d11e4262f6c8bce9fBehdad Esfahbod  /* What the char* converters return is NOT nul-terminated.  Print using "%.4s" */
411a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod  inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); }
412a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod  inline operator char* (void) { return reinterpret_cast<char *> (&this->v); }
413b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
414b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  DEFINE_SIZE_STATIC (4);
4156b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
41665f46b00333e20ab8a52a4b350747507541ec1dbBehdad EsfahbodDEFINE_NULL_DATA (Tag, "    ");
4176b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4186b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* Glyph index number, same as uint16 (length = 16 bits) */
4196ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT GlyphID;
4206b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4211f437e6f47fb6c15761021bd2078f31778f2179cBehdad Esfahbod/* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
4226ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT Offset;
4236ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4246ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
4256ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef ULONG LongOffset;
4266ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4276b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4286b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* CheckSum */
42960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct CheckSum : ULONG
43060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
43160d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
43260d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
4336b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    uint32_t Sum = 0L;
434e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod    ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
4356b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4366b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    while (Table < EndPtr)
4376b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod      Sum += *Table++;
4386b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    return Sum;
4396b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod  }
440b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
441b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  DEFINE_SIZE_STATIC (4);
4426b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
4436b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4446b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4456b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/*
4466b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod * Version Numbers
4476b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod */
4486b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
44987fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbodstruct FixedVersion
45060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
45109c292e3b688a67fbae67b645d1e6ffcf8d8eb6eBehdad Esfahbod  inline operator uint32_t (void) const { return (major << 16) + minor; }
45296908b898476ca5d7da5f386b15be76f9e83d76eBehdad Esfahbod
45339840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
4543e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
455b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return context->check_struct (this);
456cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod  }
457cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod
4586ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod  USHORT major;
45987fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbod  USHORT minor;
460b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
461b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  DEFINE_SIZE_STATIC (4);
4626b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
4636b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
46492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
46592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
4665f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
46792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Template subclasses of Offset and LongOffset that do the dereferencing.
468f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod * Use: (base+offset)
4695f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
4705f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
47192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename OffsetType, typename Type>
47292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericOffsetTo : OffsetType
47392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod{
47400e23fcc6fd0eee5c582251bf3de6a2703fbbd3eBehdad Esfahbod  inline const Type& operator () (const void *base) const
47592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  {
47692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
47764d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (!offset)) return Null(Type);
47809766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod    return StructAtOffset<Type> (base, offset);
47992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
48092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
48139840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, void *base) {
4823e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
483b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    if (!context->check_struct (this)) return false;
48492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
48564d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (!offset)) return true;
48609766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod    Type &obj = StructAtOffset<Type> (base, offset);
48739840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    return likely (obj.sanitize (context)) || neuter (context);
48892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
4894a446ac35136eff23d55f47bdd7b40095ad707abBehdad Esfahbod  template <typename T>
49039840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, void *base, T user_data) {
4913e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
492b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    if (!context->check_struct (this)) return false;
49342b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    unsigned int offset = *this;
49464d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (!offset)) return true;
49509766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod    Type &obj = StructAtOffset<Type> (base, offset);
49639840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    return likely (obj.sanitize (context, user_data)) || neuter (context);
497c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod  }
498c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod
499c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod  private:
500c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod  /* Set the offset to Null */
50139840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool neuter (hb_sanitize_context_t *context) {
50240cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod    if (context->can_edit (this, this->static_size)) {
503c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod      this->set (0); /* 0 is Null offset */
504c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod      return true;
505c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod    }
506c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod    return false;
50742b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod  }
50892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod};
50992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Base, typename OffsetType, typename Type>
51092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodinline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
51192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
5125f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbodtemplate <typename Type>
51392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetTo : GenericOffsetTo<Offset, Type> {};
51492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
51592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
51692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
517bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
518bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
51992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/*
52092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Array Types
52192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod */
52292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
52392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename LenType, typename Type>
52492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericArrayOf
52560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
5264f5f1c34dda1e0629bfa6d7b0ffa2e1ce003b7c7Behdad Esfahbod  const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
52748de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod  {
52848de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    unsigned int count = len;
52964d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (start_offset > count))
53048de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod      count = 0;
53148de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    else
53248de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod      count -= start_offset;
53348de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    count = MIN (count, *pcount);
53448de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    *pcount = count;
535b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod    return array + start_offset;
53648de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod  }
53748de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod
53860d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
53960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
54064d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (i >= len)) return Null(Type);
541b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod    return array[i];
5425f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  }
54360d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline unsigned int get_size () const
544e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  { return len.static_size + len * Type::static_size; }
545e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod
54639840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
5473e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
54839840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    if (!likely (sanitize_shallow (context))) return false;
54940d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* Note: for structs that do not reference other structs,
55040d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * we do not need to call their sanitize() as we already did
55140d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * a bound check on the aggregate array size, hence the return.
55240d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     */
5533564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    return true;
55440d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* We do keep this code though to make sure the structs pointed
55540d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * to do have a simple sanitize(), ie. they do not reference
55640d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * other structs. */
55770de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    unsigned int count = len;
55870de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
559b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod      if (array[i].sanitize (context))
56070de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod        return false;
5619bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
56270de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod  }
56339840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, void *base) {
5643e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
56539840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    if (!likely (sanitize_shallow (context))) return false;
566e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod    unsigned int count = len;
567e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod    for (unsigned int i = 0; i < count; i++)
568b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod      if (!array[i].sanitize (context, base))
569e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod        return false;
5709bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
571e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod  }
5724a446ac35136eff23d55f47bdd7b40095ad707abBehdad Esfahbod  template <typename T>
57339840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, void *base, T user_data) {
5743e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
57539840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    if (!likely (sanitize_shallow (context))) return false;
57642b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    unsigned int count = len;
57742b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
578b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod      if (!array[i].sanitize (context, base, user_data))
57942b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod        return false;
5809bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
58142b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod  }
58270de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
58330fa2821c277df99a14089749313dfe2b541e2d0Behdad Esfahbod  private:
58439840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize_shallow (hb_sanitize_context_t *context) {
58530fa2821c277df99a14089749313dfe2b541e2d0Behdad Esfahbod    TRACE_SANITIZE ();
586b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return context->check_struct (this)
587e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod	&& context->check_array (this, Type::static_size, len);
58830fa2821c277df99a14089749313dfe2b541e2d0Behdad Esfahbod  }
58930fa2821c277df99a14089749313dfe2b541e2d0Behdad Esfahbod
59030fa2821c277df99a14089749313dfe2b541e2d0Behdad Esfahbod  public:
59192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  LenType len;
592b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod  Type array[VAR];
593b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
5940eb9fc6e37935707dba2bf4b3705de2161a08cb7Behdad Esfahbod  DEFINE_SIZE_ARRAY (sizeof (LenType), array);
595e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod};
596e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod
59792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a USHORT number of elements. */
59892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
59992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct ArrayOf : GenericArrayOf<USHORT, Type> {};
60092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
60192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a ULONG number of elements. */
60292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
60392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongArrayOf : GenericArrayOf<ULONG, Type> {};
60492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
60592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of Offset's */
60692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
60792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
60892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
60992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of LongOffset's */
61092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
61192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
61292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
61392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* LongArray of LongOffset's */
61492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
61592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
61692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
61780e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod/* Array of offsets relative to the beginning of the array itself. */
61880e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodtemplate <typename Type>
61980e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodstruct OffsetListOf : OffsetArrayOf<Type>
62080e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod{
62180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
62280e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  {
62364d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (i >= this->len)) return Null(Type);
624b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod    return this+this->array[i];
62580e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
62680e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
62739840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
6283e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
62940cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod    return OffsetArrayOf<Type>::sanitize (context, this);
63080e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
6314a446ac35136eff23d55f47bdd7b40095ad707abBehdad Esfahbod  template <typename T>
63239840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, T user_data) {
6333e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
63440cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod    return OffsetArrayOf<Type>::sanitize (context, this, user_data);
63580e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
63680e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod};
63780e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
63880e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
639e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod/* An array with a USHORT number of elements,
640e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod * starting at second element. */
641e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbodtemplate <typename Type>
64260d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct HeadlessArrayOf
64360d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
64460d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
64560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
64664d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (i >= len || !i)) return Null(Type);
647b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod    return array[i-1];
648e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod  }
64960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline unsigned int get_size () const
650e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
6515f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
65239840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize_shallow (hb_sanitize_context_t *context) {
653b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return context->check_struct (this)
654e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod	&& context->check_array (this, Type::static_size, len);
655e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod  }
656e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod
65739840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
6583e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
65939840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    if (!likely (sanitize_shallow (context))) return false;
66040d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* Note: for structs that do not reference other structs,
66140d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * we do not need to call their sanitize() as we already did
66240d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * a bound check on the aggregate array size, hence the return.
66340d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     */
6643564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    return true;
66540d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* We do keep this code though to make sure the structs pointed
66640d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * to do have a simple sanitize(), ie. they do not reference
66740d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * other structs. */
66815164d9258a74122a4db748d35532bd72c47cec2Behdad Esfahbod    unsigned int count = len ? len - 1 : 0;
669b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod    Type *a = array;
67070de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
6714f252fedc7136c66a9d7fbcb2978581986da6227Behdad Esfahbod      if (!a[i].sanitize (context))
67270de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod        return false;
6739bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
67470de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod  }
67570de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
6765f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  USHORT len;
677b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod  Type array[VAR];
678ed07422c33bbb52ff4d79e65986171e3f07697d8Behdad Esfahbod  public:
6790eb9fc6e37935707dba2bf4b3705de2161a08cb7Behdad Esfahbod  DEFINE_SIZE_ARRAY (sizeof (USHORT), array);
6805f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod};
6815f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
6826b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
6831e91434569a9e9535ef021ca52b60b2e2af75d19Behdad Esfahbod#endif /* HB_OPEN_TYPE_PRIVATE_HH */
684