hb-open-type-private.hh revision 33afa4e2dc352f08cc094703e3f01d3ecd83b354
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
8433afa4e2dc352f08cc094703e3f01d3ecd83b354Behdad Esfahbod#define _DEFINE_SIZE_ASSERTION(_compare) \
8533afa4e2dc352f08cc094703e3f01d3ecd83b354Behdad Esfahbod  inline void _size_assertion (void) const \
8633afa4e2dc352f08cc094703e3f01d3ecd83b354Behdad Esfahbod  { ASSERT_STATIC ((sizeof (*this)) _compare); }
870abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod
880abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod
89e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod#define DEFINE_SIZE_STATIC(size) \
9033afa4e2dc352f08cc094703e3f01d3ecd83b354Behdad Esfahbod  _DEFINE_SIZE_ASSERTION (== (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#define VAR0 (VAR+0)
97b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod
98bea34c7cbb583cf7660776e95cab3171590b8427Behdad Esfahbod#define DEFINE_SIZE_MIN(size) \
9933afa4e2dc352f08cc094703e3f01d3ecd83b354Behdad Esfahbod  _DEFINE_SIZE_ASSERTION (>= (size)); \
100b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  static const unsigned int min_size = (size)
101b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod
102e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod#define DEFINE_SIZE_VAR(size, _var_type) \
10333afa4e2dc352f08cc094703e3f01d3ecd83b354Behdad Esfahbod  _DEFINE_SIZE_ASSERTION (== (size) + VAR0 * sizeof (_var_type)); \
104e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  static const unsigned int min_size = (size)
105e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
106569da92bc6956f42d9b2d65c784e184fb6380efeBehdad Esfahbod#define DEFINE_SIZE_VAR2(size, _var_type1, _var_type2) \
10733afa4e2dc352f08cc094703e3f01d3ecd83b354Behdad Esfahbod  _DEFINE_SIZE_ASSERTION (== (size) + VAR0 * sizeof (_var_type1) + VAR0 * sizeof (_var_type2)); \
108e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  static const unsigned int min_size = (size)
109e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
110e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
111e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
112e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod/*
113f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod * Null objects
114600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod */
115600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
1168b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Global nul-content Null pool.  Enlarge as necessary. */
1179d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodstatic const void *_NullPool[32 / sizeof (void *)];
1188b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1198b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Generic template for nul-content sizeof-sized Null objects. */
1208b8358033184198ff638ee1379093717596e162dBehdad Esfahbodtemplate <typename Type>
1219d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodstatic inline const Type& Null () {
122ed07422c33bbb52ff4d79e65986171e3f07697d8Behdad Esfahbod  ASSERT_STATIC (Type::min_size <= sizeof (_NullPool));
123187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod  return *CastP<Type> (_NullPool);
1249d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbod}
1258b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1268b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
12765f46b00333e20ab8a52a4b350747507541ec1dbBehdad Esfahbod#define DEFINE_NULL_DATA(Type, data) \
12865f46b00333e20ab8a52a4b350747507541ec1dbBehdad Esfahbodstatic const char _Null##Type[Type::min_size + 1] = data; /* +1 is for nul-termination in data */ \
1298b8358033184198ff638ee1379093717596e162dBehdad Esfahbodtemplate <> \
1309d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodinline const Type& Null<Type> () { \
131187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod  return *CastP<Type> (_Null##Type); \
132565c80bd2960366ace2d10dd71beaaf2a80213c8Behdad Esfahbod} /* The following line really exists such that we end in a place needing semicolon */ \
133bea34c7cbb583cf7660776e95cab3171590b8427Behdad EsfahbodASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type))
1348b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1358b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Accessor macro. */
1369d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbod#define Null(Type) Null<Type>()
1378b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1388b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
13920e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod/*
140dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod * Trace
14120e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod */
14220e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
143dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod
144dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodtemplate <int max_depth>
145dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodstruct hb_trace_t {
146dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  explicit hb_trace_t (unsigned int *pdepth) : pdepth(pdepth) { if (max_depth) ++*pdepth; }
147dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  ~hb_trace_t (void) { if (max_depth) --*pdepth; }
148dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod
149dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  inline void log (const char *what, const char *function, const void *obj)
150dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  {
151dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod    if (*pdepth < max_depth)
152dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod      fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, *pdepth, *pdepth, function);
153dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  }
15420e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
15520e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod  private:
156dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  unsigned int *pdepth;
15720e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod};
158dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodtemplate <> /* Optimize when tracing is disabled */
159dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodstruct hb_trace_t<0> {
160dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  explicit hb_trace_t (unsigned int *p) {}
161dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  inline void log (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() \
176dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod	hb_trace_t<HB_DEBUG_SANITIZE> trace (&context->debug_depth); \
177dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod	trace.log ("SANITIZE", HB_FUNC, this);
178807c5b03a2251a3c29a520852639421783101b55Behdad Esfahbod
179b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod
1801376fb7bf9ef07970f0ba13dc64d6a8ab8252762Behdad Esfahbodstruct hb_sanitize_context_t
181577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
18298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  inline void init (hb_blob_t *blob)
18398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
18498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->blob = hb_blob_reference (blob);
18598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->start = hb_blob_lock (blob);
18698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->end = this->start + hb_blob_get_length (blob);
18798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->writable = hb_blob_is_writable (blob);
18898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->edit_count = 0;
18920e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    this->debug_depth = 0;
190577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
19198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
19298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
19398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->blob, this->start, this->end, this->end - this->start);
19498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
1951376fb7bf9ef07970f0ba13dc64d6a8ab8252762Behdad Esfahbod
19698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  inline void finish (void)
19798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
19898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
19998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
20098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->blob, this->start, this->end, this->edit_count);
201577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
20298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    hb_blob_unlock (this->blob);
20398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    hb_blob_destroy (this->blob);
20498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->blob = NULL;
20598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->start = this->end = NULL;
20698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
2074f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
2084ad2cc5dec6b0639da2b1846282bdd99d06d5ff1Behdad Esfahbod  inline bool check_range (const void *base, unsigned int len) const
20998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
210a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod    const char *p = (const char *) base;
211a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod    bool ret = this->start <= p &&
212a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p <= this->end &&
213a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       (unsigned int) (this->end - p) >= len;
21498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
21520e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE) \
2164ad2cc5dec6b0639da2b1846282bdd99d06d5ff1Behdad Esfahbod      fprintf (stderr, "SANITIZE(%p) %-*d-> range [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
217a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p,
21820e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod	       this->debug_depth, this->debug_depth,
219a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p, p + len, len,
22098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->start, this->end,
22198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       ret ? "pass" : "FAIL");
22298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
22327e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod    return likely (ret);
22498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
225577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
2261cd1e117d060d38e314618b627d7663cb01ed584Behdad Esfahbod  inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
22798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
228a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod    const char *p = (const char *) base;
22998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    bool overflows = len >= ((unsigned int) -1) / record_size;
23041895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod
23120e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
23298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
233a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p,
23420e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod	       this->debug_depth, this->debug_depth,
235a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p, p + (record_size * len), record_size, len, (unsigned long) record_size * len,
23698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->start, this->end,
23798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       !overflows ? "does not overflow" : "OVERFLOWS FAIL");
238fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod
2394ad2cc5dec6b0639da2b1846282bdd99d06d5ff1Behdad Esfahbod    return likely (!overflows && this->check_range (base, record_size * len));
24098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
241254933c397f1ce9796f59689a25f9fc2e58df4eaBehdad Esfahbod
242b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  template <typename Type>
243b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  inline bool check_struct (const Type *obj) const
244b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  {
24554842374c2b291ef208c51ae1d853ec0403ccf84Behdad Esfahbod    return likely (this->check_range (obj, obj->min_size));
246b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  }
247b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod
24840cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod  inline bool can_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
24998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
250a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod    const char *p = (const char *) base;
25198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->edit_count++;
25298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
25320e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
25498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
255a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p,
25620e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod	       this->debug_depth, this->debug_depth,
25798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->edit_count,
258a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod	       p, p + len, len,
25998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->start, this->end,
26098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->writable ? "granted" : "REJECTED");
26198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
26298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    return this->writable;
26398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
26498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
265705e215268aa95c2bc6af8af9b48b72b690ec1f7Behdad Esfahbod  unsigned int debug_depth;
26698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  const char *start, *end;
26798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  bool writable;
26898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  unsigned int edit_count;
26998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  hb_blob_t *blob;
27098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod};
271815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
272577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
273577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
2744e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod/* Template to sanitize an object. */
2754e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbodtemplate <typename Type>
2764e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbodstruct Sanitizer
2774e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod{
2784e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  static hb_blob_t *sanitize (hb_blob_t *blob) {
279705e215268aa95c2bc6af8af9b48b72b690ec1f7Behdad Esfahbod    hb_sanitize_context_t context[1] = {{0}};
2804e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    bool sane;
2814e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
282d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod    /* TODO is_sane() stuff */
2834e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
2844e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  retry:
285fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
2867d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod      fprintf (stderr, "Sanitizer %p start %s\n", blob, HB_FUNC);
2874f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
28898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    context->init (blob);
2894e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
29027e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod    Type *t = CastP<Type> (const_cast<char *> (context->start));
2914e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
29239840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    sane = t->sanitize (context);
2934e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane) {
29427e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod      if (context->edit_count) {
295fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	if (HB_DEBUG_SANITIZE)
296fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	  fprintf (stderr, "Sanitizer %p passed first round with %d edits; doing a second round %s\n",
29727e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod		   blob, context->edit_count, HB_FUNC);
298fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod
2998b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod        /* sanitize again to ensure no toe-stepping */
30027e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod        context->edit_count = 0;
30139840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod	sane = t->sanitize (context);
30227e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod	if (context->edit_count) {
303fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	  if (HB_DEBUG_SANITIZE)
304fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	    fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
30527e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod		     blob, context->edit_count, HB_FUNC);
3064e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	  sane = false;
3074e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	}
3084e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
30998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      context->finish ();
3104e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    } else {
31127e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod      unsigned int edit_count = context->edit_count;
31298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      context->finish ();
313977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod      if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
314977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod        /* ok, we made it writable by relocating.  try again */
315fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	if (HB_DEBUG_SANITIZE)
3167d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod	  fprintf (stderr, "Sanitizer %p retry %s\n", blob, HB_FUNC);
3174e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod        goto retry;
3184e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
3194e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3204e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
321fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
3227d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod      fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", HB_FUNC);
3234e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane)
3244e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return blob;
3254e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    else {
3264e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      hb_blob_destroy (blob);
3274e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return hb_blob_create_empty ();
3284e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3294e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  }
3304e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod};
3314e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
3322d15e72c75931398db5e027e660f1320bb979117Behdad Esfahbod
333f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod
334f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod
335b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/*
336b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *
337bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod * The OpenType Font File: Data Types
338b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod */
339b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
340b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
341b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/* "The following data types are used in the OpenType font file.
342b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *  All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
343b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
3445f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
3455f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod * Int types
3465f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
3475f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
3486b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
349e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type, int Bytes> class BEInt;
350e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
351f1aaa2a43654c28405ffd393de2cb127437c99a5Behdad Esfahbod/* LONGTERMTODO: On machines allowing unaligned access, we can make the
352f1aaa2a43654c28405ffd393de2cb127437c99a5Behdad Esfahbod * following tighter by using byteswap instructions on ints directly. */
353e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
354e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodclass BEInt<Type, 2>
355e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
356e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  public:
35701c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline class BEInt<Type,2>& operator = (Type i) { hb_be_uint16_put (v,i); return *this; }
35801c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type () const { return hb_be_uint16_get (v); }
35901c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_cmp (v, o.v); }
36001c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
361e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  private: uint8_t v[2];
362e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
363e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
364e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodclass BEInt<Type, 4>
365e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
366e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  public:
36701c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline class BEInt<Type,4>& operator = (Type i) { hb_be_uint32_put (v,i); return *this; }
36801c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type () const { return hb_be_uint32_get (v); }
36901c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_cmp (v, o.v); }
37001c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
371e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  private: uint8_t v[4];
372e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
373e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
3742467c669c2aee4de2a6621a9d06cba0262376d41Behdad Esfahbod/* Integer types in big-endian order and no alignment requirement */
375e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
376e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodstruct IntType
377e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
37801c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline void set (Type i) { v = i; }
37901c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type(void) const { return v; }
38001c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
38101c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
38239840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
383e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod    TRACE_SANITIZE ();
384b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return context->check_struct (this);
385e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  }
386a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod  protected:
387569da92bc6956f42d9b2d65c784e184fb6380efeBehdad Esfahbod  BEInt<Type, sizeof (Type)> v;
388569da92bc6956f42d9b2d65c784e184fb6380efeBehdad Esfahbod  public:
389e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  DEFINE_SIZE_STATIC (sizeof (Type));
390e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
391e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
392e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<uint16_t> USHORT;	/* 16-bit unsigned integer. */
393e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<int16_t>  SHORT;	/* 16-bit signed integer. */
394e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<uint32_t> ULONG;	/* 32-bit unsigned integer. */
395e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<int32_t>  LONG;		/* 32-bit signed integer. */
396e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
3976b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* Array of four uint8s (length = 32 bits) used to identify a script, language
3986b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod * system, feature, or baseline */
39920cc86b3592db33731de671f008d7d222776be49Behdad Esfahbodstruct Tag : ULONG
40060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
401befc022affd2386b3f46cd7d11e4262f6c8bce9fBehdad Esfahbod  /* What the char* converters return is NOT nul-terminated.  Print using "%.4s" */
402a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod  inline operator const char* (void) const { return reinterpret_cast<const char *> (&this->v); }
403a82ef7a893b773a17f7548375de9f588dfc83abaBehdad Esfahbod  inline operator char* (void) { return reinterpret_cast<char *> (&this->v); }
404b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
405b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  DEFINE_SIZE_STATIC (4);
4066b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
40765f46b00333e20ab8a52a4b350747507541ec1dbBehdad EsfahbodDEFINE_NULL_DATA (Tag, "    ");
4086b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4096b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* Glyph index number, same as uint16 (length = 16 bits) */
4106ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT GlyphID;
4116b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4121f437e6f47fb6c15761021bd2078f31778f2179cBehdad Esfahbod/* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
4136ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT Offset;
4146ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4156ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
4166ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef ULONG LongOffset;
4176ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4186b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4196b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* CheckSum */
42060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct CheckSum : ULONG
42160d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
42260d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
42360d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
4246b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    uint32_t Sum = 0L;
425e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod    ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
4266b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4276b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    while (Table < EndPtr)
4286b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod      Sum += *Table++;
4296b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    return Sum;
4306b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod  }
431b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
432b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  DEFINE_SIZE_STATIC (4);
4336b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
4346b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4356b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4366b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/*
4376b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod * Version Numbers
4386b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod */
4396b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
44087fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbodstruct FixedVersion
44160d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
44209c292e3b688a67fbae67b645d1e6ffcf8d8eb6eBehdad Esfahbod  inline operator uint32_t (void) const { return (major << 16) + minor; }
44396908b898476ca5d7da5f386b15be76f9e83d76eBehdad Esfahbod
44439840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
4453e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
446b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return context->check_struct (this);
447cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod  }
448cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod
4496ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod  USHORT major;
45087fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbod  USHORT minor;
451b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
452b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  DEFINE_SIZE_STATIC (4);
4536b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
4546b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
45592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
45692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
4575f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
45892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Template subclasses of Offset and LongOffset that do the dereferencing.
459f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod * Use: (base+offset)
4605f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
4615f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
46292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename OffsetType, typename Type>
46392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericOffsetTo : OffsetType
46492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod{
46500e23fcc6fd0eee5c582251bf3de6a2703fbbd3eBehdad Esfahbod  inline const Type& operator () (const void *base) const
46692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  {
46792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
46864d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (!offset)) return Null(Type);
46909766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod    return StructAtOffset<Type> (base, offset);
47092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
47192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
47239840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, void *base) {
4733e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
474b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    if (!context->check_struct (this)) return false;
47592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
47664d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (!offset)) return true;
47709766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod    Type &obj = StructAtOffset<Type> (base, offset);
47839840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    return likely (obj.sanitize (context)) || neuter (context);
47992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
4804a446ac35136eff23d55f47bdd7b40095ad707abBehdad Esfahbod  template <typename T>
48139840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, void *base, T user_data) {
4823e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
483b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    if (!context->check_struct (this)) return false;
48442b778f89e0818fe06910ce04e2203485823da09Behdad 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, user_data)) || neuter (context);
488c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod  }
489c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod
490c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod  private:
491c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod  /* Set the offset to Null */
49239840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool neuter (hb_sanitize_context_t *context) {
49340cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod    if (context->can_edit (this, this->static_size)) {
494c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod      this->set (0); /* 0 is Null offset */
495c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod      return true;
496c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod    }
497c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod    return false;
49842b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod  }
49992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod};
50092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Base, typename OffsetType, typename Type>
50192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodinline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
50292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
5035f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbodtemplate <typename Type>
50492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetTo : GenericOffsetTo<Offset, Type> {};
50592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
50692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
50792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
508bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
509bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
51092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/*
51192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Array Types
51292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod */
51392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
51492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename LenType, typename Type>
51592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericArrayOf
51660d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
5174f5f1c34dda1e0629bfa6d7b0ffa2e1ce003b7c7Behdad Esfahbod  const Type *sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
51848de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod  {
51948de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    unsigned int count = len;
52064d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (start_offset > count))
52148de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod      count = 0;
52248de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    else
52348de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod      count -= start_offset;
52448de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    count = MIN (count, *pcount);
52548de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    *pcount = count;
526b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod    return array + start_offset;
52748de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod  }
52848de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod
52960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
53060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
53164d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (i >= len)) return Null(Type);
532b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod    return array[i];
5335f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  }
53460d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline unsigned int get_size () const
535e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  { return len.static_size + len * Type::static_size; }
536e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod
53739840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
5383e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
53939840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    if (!likely (sanitize_shallow (context))) return false;
54040d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* Note: for structs that do not reference other structs,
54140d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * we do not need to call their sanitize() as we already did
54240d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * a bound check on the aggregate array size, hence the return.
54340d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     */
5443564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    return true;
54540d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* We do keep this code though to make sure the structs pointed
54640d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * to do have a simple sanitize(), ie. they do not reference
54740d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * other structs. */
54870de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    unsigned int count = len;
54970de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
550b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod      if (array[i].sanitize (context))
55170de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod        return false;
5529bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
55370de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod  }
55439840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, void *base) {
5553e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
55639840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    if (!likely (sanitize_shallow (context))) return false;
557e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod    unsigned int count = len;
558e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod    for (unsigned int i = 0; i < count; i++)
559b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod      if (!array[i].sanitize (context, base))
560e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod        return false;
5619bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
562e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod  }
5634a446ac35136eff23d55f47bdd7b40095ad707abBehdad Esfahbod  template <typename T>
56439840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, void *base, T user_data) {
5653e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
56639840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    if (!likely (sanitize_shallow (context))) return false;
56742b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    unsigned int count = len;
56842b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
569b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod      if (!array[i].sanitize (context, base, user_data))
57042b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod        return false;
5719bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
57242b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod  }
57370de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
57430fa2821c277df99a14089749313dfe2b541e2d0Behdad Esfahbod  private:
57539840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize_shallow (hb_sanitize_context_t *context) {
57630fa2821c277df99a14089749313dfe2b541e2d0Behdad Esfahbod    TRACE_SANITIZE ();
577b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return context->check_struct (this)
578e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod	&& context->check_array (this, Type::static_size, len);
57930fa2821c277df99a14089749313dfe2b541e2d0Behdad Esfahbod  }
58030fa2821c277df99a14089749313dfe2b541e2d0Behdad Esfahbod
58130fa2821c277df99a14089749313dfe2b541e2d0Behdad Esfahbod  public:
58292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  LenType len;
583b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod  Type array[VAR];
584b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
585bea34c7cbb583cf7660776e95cab3171590b8427Behdad Esfahbod  DEFINE_SIZE_VAR (sizeof (LenType), Type);
586e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod};
587e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod
58892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a USHORT number of elements. */
58992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
59092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct ArrayOf : GenericArrayOf<USHORT, Type> {};
59192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
59292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a ULONG number of elements. */
59392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
59492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongArrayOf : GenericArrayOf<ULONG, Type> {};
59592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
59692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of Offset's */
59792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
59892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
59992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
60092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of LongOffset's */
60192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
60292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
60392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
60492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* LongArray of LongOffset's */
60592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
60692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
60792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
60880e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod/* Array of offsets relative to the beginning of the array itself. */
60980e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodtemplate <typename Type>
61080e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodstruct OffsetListOf : OffsetArrayOf<Type>
61180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod{
61280e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
61380e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  {
61464d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (i >= this->len)) return Null(Type);
615b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod    return this+this->array[i];
61680e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
61780e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
61839840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
6193e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
62040cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod    return OffsetArrayOf<Type>::sanitize (context, this);
62180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
6224a446ac35136eff23d55f47bdd7b40095ad707abBehdad Esfahbod  template <typename T>
62339840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, T user_data) {
6243e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
62540cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod    return OffsetArrayOf<Type>::sanitize (context, this, user_data);
62680e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
62780e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod};
62880e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
62980e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
630e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod/* An array with a USHORT number of elements,
631e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod * starting at second element. */
632e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbodtemplate <typename Type>
63360d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct HeadlessArrayOf
63460d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
63560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
63660d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
63764d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (i >= len || !i)) return Null(Type);
638b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod    return array[i-1];
639e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod  }
64060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline unsigned int get_size () const
641e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
6425f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
64339840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize_shallow (hb_sanitize_context_t *context) {
644b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return context->check_struct (this)
645e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod	&& context->check_array (this, Type::static_size, len);
646e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod  }
647e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod
64839840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
6493e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
65039840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    if (!likely (sanitize_shallow (context))) return false;
65140d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* Note: for structs that do not reference other structs,
65240d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * we do not need to call their sanitize() as we already did
65340d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * a bound check on the aggregate array size, hence the return.
65440d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     */
6553564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    return true;
65640d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* We do keep this code though to make sure the structs pointed
65740d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * to do have a simple sanitize(), ie. they do not reference
65840d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * other structs. */
65915164d9258a74122a4db748d35532bd72c47cec2Behdad Esfahbod    unsigned int count = len ? len - 1 : 0;
660b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod    Type *a = array;
66170de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
6624f252fedc7136c66a9d7fbcb2978581986da6227Behdad Esfahbod      if (!a[i].sanitize (context))
66370de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod        return false;
6649bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
66570de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod  }
66670de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
6675f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  USHORT len;
668b961518b9611471ff7060e97686e5625974847ebBehdad Esfahbod  Type array[VAR];
669ed07422c33bbb52ff4d79e65986171e3f07697d8Behdad Esfahbod  public:
670ed07422c33bbb52ff4d79e65986171e3f07697d8Behdad Esfahbod  DEFINE_SIZE_VAR (sizeof (USHORT), Type);
6715f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod};
6725f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
6736b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
6741e91434569a9e9535ef021ca52b60b2e2af75d19Behdad Esfahbod#endif /* HB_OPEN_TYPE_PRIVATE_HH */
675