hb-open-type-private.hh revision 40cbefe858192531ed64dd51d402f7ca7b8153a3
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
440dfcc13a4668cdd2c2ebdd5f4a7540a51222cf2fBehdad Esfahbod/* Cast to "const char *" and "char *" */
45a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodtemplate <typename Type>
46a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodinline const char * CharP (const Type* X)
47a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod{ return reinterpret_cast<const char *>(X); }
48a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodtemplate <typename Type>
49a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodinline char * CharP (Type* X)
50a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod{ return reinterpret_cast<char *>(X); }
512b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod
52187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod/* Cast to struct T, reference to reference */
53a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodtemplate<typename Type, typename TObject>
54187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodinline const Type& CastR(const TObject &X)
55a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod{ return reinterpret_cast<const Type&> (X); }
56a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodtemplate<typename Type, typename TObject>
57187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodinline Type& CastR(TObject &X)
58a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod{ return reinterpret_cast<Type&> (X); }
59196598bbccff08415ff5192314cba044df258cadBehdad Esfahbod
60187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod/* Cast to struct T, pointer to pointer */
61187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodtemplate<typename Type, typename TObject>
62187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodinline const Type* CastP(const TObject *X)
63187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod{ return reinterpret_cast<const Type*> (X); }
64187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodtemplate<typename Type, typename TObject>
65187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbodinline Type* CastP(TObject *X)
66187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod{ return reinterpret_cast<Type*> (X); }
67187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod
6809766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod/* StructAtOffset<T>(P,Ofs) returns the struct T& that is placed at memory
6909766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod * location pointed to by P plus Ofs bytes. */
7009766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbodtemplate<typename Type>
7109766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbodinline const Type& StructAtOffset(const void *P, unsigned int offset)
7209766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod{ return * reinterpret_cast<const Type*> (CharP(P) + offset); }
7309766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbodtemplate<typename Type>
7409766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbodinline Type& StructAtOffset(void *P, unsigned int offset)
7509766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod{ return * reinterpret_cast<Type*> (CharP(P) + offset); }
7670de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
772e2f43edf2f49f4047e28b1ce2ea95938536de9cBehdad Esfahbod/* StructAfter<T>(X) returns the struct T& that is placed after X.
7829c3f5e1b6212c775a7b911becd44ba093b7b0ebBehdad Esfahbod * Works with X of variable size also.  X must implement get_size() */
79e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbodtemplate<typename Type, typename TObject>
80e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbodinline const Type& StructAfter(const TObject &X)
8109766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod{ return StructAtOffset<Type>(&X, X.get_size()); }
82e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbodtemplate<typename Type, typename TObject>
83e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbodinline Type& StructAfter(TObject &X)
8409766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod{ return StructAtOffset<Type>(&X, X.get_size()); }
85a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod
86e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbod
87d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod
8870de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod/*
89e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod * Size checking
90e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod */
91e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
920abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod#define ASSERT_SIZE(_thing, _size) ASSERT_STATIC (sizeof (_thing) == (_size))
930abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod
940abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod#define _DEFINE_SIZE_ASSERTION(_size) \
950abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod  inline void _size_assertion (void) const { ASSERT_SIZE (*this, _size); }
960abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod
970abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod
98e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod#define DEFINE_SIZE_STATIC(size) \
990abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod  _DEFINE_SIZE_ASSERTION (size); \
100e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  static inline unsigned int get_size (void) { return (size); } \
101e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  static const unsigned int static_size = (size); \
102e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  static const unsigned int min_size = (size)
103e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
104b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod/* Size signifying variable-sized array */
105b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod#define VAR 1
106b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod#define VAR0 (VAR+0)
107b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod
108bea34c7cbb583cf7660776e95cab3171590b8427Behdad Esfahbod#define DEFINE_SIZE_MIN(size) \
109b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  static const unsigned int min_size = (size)
110b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod
111e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod#define DEFINE_SIZE_VAR(size, _var_type) \
1120abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod  _DEFINE_SIZE_ASSERTION ((size) + VAR0 * sizeof (_var_type)); \
113e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  static const unsigned int min_size = (size)
114e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
115569da92bc6956f42d9b2d65c784e184fb6380efeBehdad Esfahbod#define DEFINE_SIZE_VAR2(size, _var_type1, _var_type2) \
1160abcc3b48cfd51a22695c9e988938b2f45cb19d8Behdad Esfahbod  _DEFINE_SIZE_ASSERTION ((size) + VAR0 * sizeof (_var_type1) + VAR0 * sizeof (_var_type2)); \
117e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  static const unsigned int min_size = (size)
118e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
119e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
120e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod
121e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod/*
122f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod * Null objects
123600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod */
124600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
1258b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Global nul-content Null pool.  Enlarge as necessary. */
1269d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodstatic const void *_NullPool[32 / sizeof (void *)];
1278b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1288b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Generic template for nul-content sizeof-sized Null objects. */
1298b8358033184198ff638ee1379093717596e162dBehdad Esfahbodtemplate <typename Type>
1309d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodstatic inline const Type& Null () {
1319d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbod  ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool));
132187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod  return *CastP<Type> (_NullPool);
1339d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbod}
1348b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1358b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
13665f46b00333e20ab8a52a4b350747507541ec1dbBehdad Esfahbod#define DEFINE_NULL_DATA(Type, data) \
13765f46b00333e20ab8a52a4b350747507541ec1dbBehdad Esfahbodstatic const char _Null##Type[Type::min_size + 1] = data; /* +1 is for nul-termination in data */ \
1388b8358033184198ff638ee1379093717596e162dBehdad Esfahbodtemplate <> \
1399d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodinline const Type& Null<Type> () { \
140187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod  return *CastP<Type> (_Null##Type); \
141565c80bd2960366ace2d10dd71beaaf2a80213c8Behdad Esfahbod} /* The following line really exists such that we end in a place needing semicolon */ \
142bea34c7cbb583cf7660776e95cab3171590b8427Behdad EsfahbodASSERT_STATIC (Type::min_size + 1 <= sizeof (_Null##Type))
1438b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1448b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Accessor macro. */
1459d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbod#define Null(Type) Null<Type>()
1468b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1478b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
14820e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod/*
149dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod * Trace
15020e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod */
15120e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
152dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod
153dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodtemplate <int max_depth>
154dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodstruct hb_trace_t {
155dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  explicit hb_trace_t (unsigned int *pdepth) : pdepth(pdepth) { if (max_depth) ++*pdepth; }
156dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  ~hb_trace_t (void) { if (max_depth) --*pdepth; }
157dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod
158dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  inline void log (const char *what, const char *function, const void *obj)
159dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  {
160dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod    if (*pdepth < max_depth)
161dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod      fprintf (stderr, "%s(%p) %-*d-> %s\n", what, obj, *pdepth, *pdepth, function);
162dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  }
16320e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
16420e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod  private:
165dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  unsigned int *pdepth;
16620e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod};
167dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodtemplate <> /* Optimize when tracing is disabled */
168dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbodstruct hb_trace_t<0> {
169dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  explicit hb_trace_t (unsigned int *p) {}
170dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod  inline void log (const char *what, const char *function, const void *obj) {};
17120e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod};
17220e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
17320e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
174600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
175577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod/*
176577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod * Sanitize
177577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod */
178577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
17995e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#ifndef HB_DEBUG_SANITIZE
180807c5b03a2251a3c29a520852639421783101b55Behdad Esfahbod#define HB_DEBUG_SANITIZE HB_DEBUG+0
18195e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#endif
18295e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod
18320e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod
184bc200457430c083914a64bf4b056153506749610Behdad Esfahbod#define TRACE_SANITIZE() \
185dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod	hb_trace_t<HB_DEBUG_SANITIZE> trace (&context->debug_depth); \
186dfc8cbe85479dde1ffdc6b2e73f4907331d77a19Behdad Esfahbod	trace.log ("SANITIZE", HB_FUNC, this);
187807c5b03a2251a3c29a520852639421783101b55Behdad Esfahbod
188b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod
1891376fb7bf9ef07970f0ba13dc64d6a8ab8252762Behdad Esfahbodstruct hb_sanitize_context_t
190577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
19198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  inline void init (hb_blob_t *blob)
19298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
19398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->blob = hb_blob_reference (blob);
19498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->start = hb_blob_lock (blob);
19598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->end = this->start + hb_blob_get_length (blob);
19698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->writable = hb_blob_is_writable (blob);
19798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->edit_count = 0;
19820e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    this->debug_depth = 0;
199577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
20098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
20198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
20298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->blob, this->start, this->end, this->end - this->start);
20398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
2041376fb7bf9ef07970f0ba13dc64d6a8ab8252762Behdad Esfahbod
20598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  inline void finish (void)
20698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
20798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
20898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
20998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->blob, this->start, this->end, this->edit_count);
210577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
21198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    hb_blob_unlock (this->blob);
21298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    hb_blob_destroy (this->blob);
21398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->blob = NULL;
21498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->start = this->end = NULL;
21598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
2164f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
2174ad2cc5dec6b0639da2b1846282bdd99d06d5ff1Behdad Esfahbod  inline bool check_range (const void *base, unsigned int len) const
21898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
21998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    bool ret = this->start <= base &&
22098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       base <= this->end &&
2211cd1e117d060d38e314618b627d7663cb01ed584Behdad Esfahbod	       (unsigned int) (this->end - CharP(base)) >= len;
22298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
22320e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE) \
2244ad2cc5dec6b0639da2b1846282bdd99d06d5ff1Behdad Esfahbod      fprintf (stderr, "SANITIZE(%p) %-*d-> range [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
22598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       base,
22620e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod	       this->debug_depth, this->debug_depth,
22740cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod	       base, CharP(base) + len, len,
22898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->start, this->end,
22998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       ret ? "pass" : "FAIL");
23098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
23127e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod    return likely (ret);
23298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
233577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
2341cd1e117d060d38e314618b627d7663cb01ed584Behdad Esfahbod  inline bool check_array (const void *base, unsigned int record_size, unsigned int len) const
23598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
23698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    bool overflows = len >= ((unsigned int) -1) / record_size;
23741895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod
23820e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
23998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
24098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       base,
24120e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod	       this->debug_depth, this->debug_depth,
2421cd1e117d060d38e314618b627d7663cb01ed584Behdad Esfahbod	       base, CharP(base) + (record_size * len), record_size, len, (unsigned long) record_size * len,
24398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->start, this->end,
24498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       !overflows ? "does not overflow" : "OVERFLOWS FAIL");
245fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod
2464ad2cc5dec6b0639da2b1846282bdd99d06d5ff1Behdad Esfahbod    return likely (!overflows && this->check_range (base, record_size * len));
24798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
248254933c397f1ce9796f59689a25f9fc2e58df4eaBehdad Esfahbod
249b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  template <typename Type>
250b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  inline bool check_struct (const Type *obj) const
251b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  {
252b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return likely (this->check_range (obj, sizeof (*obj)));
253b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod  }
254b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod
25540cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod  inline bool can_edit (const void *base HB_UNUSED, unsigned int len HB_UNUSED)
25698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  {
25798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    this->edit_count++;
25898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
25920e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod    if (HB_DEBUG_SANITIZE && (int) this->debug_depth < (int) HB_DEBUG_SANITIZE)
26098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
26198daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       base,
26220e3dd5d292b65f70d2eae63b8d8713a1c889d47Behdad Esfahbod	       this->debug_depth, this->debug_depth,
26398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->edit_count,
26440cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod	       base, CharP(base)+len, len,
26598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->start, this->end,
26698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod	       this->writable ? "granted" : "REJECTED");
26798daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
26898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    return this->writable;
26998daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  }
27098daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod
271705e215268aa95c2bc6af8af9b48b72b690ec1f7Behdad Esfahbod  unsigned int debug_depth;
27298daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  const char *start, *end;
27398daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  bool writable;
27498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  unsigned int edit_count;
27598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod  hb_blob_t *blob;
27698daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod};
277815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
278577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
279577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
2804e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod/* Template to sanitize an object. */
2814e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbodtemplate <typename Type>
2824e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbodstruct Sanitizer
2834e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod{
2844e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  static hb_blob_t *sanitize (hb_blob_t *blob) {
285705e215268aa95c2bc6af8af9b48b72b690ec1f7Behdad Esfahbod    hb_sanitize_context_t context[1] = {{0}};
2864e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    bool sane;
2874e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
288d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod    /* TODO is_sane() stuff */
2894e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
2904e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  retry:
291fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
2927d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod      fprintf (stderr, "Sanitizer %p start %s\n", blob, HB_FUNC);
2934f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
29498daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod    context->init (blob);
2954e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
29627e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod    Type *t = CastP<Type> (const_cast<char *> (context->start));
2974e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
29839840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    sane = t->sanitize (context);
2994e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane) {
30027e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod      if (context->edit_count) {
301fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	if (HB_DEBUG_SANITIZE)
302fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	  fprintf (stderr, "Sanitizer %p passed first round with %d edits; doing a second round %s\n",
30327e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod		   blob, context->edit_count, HB_FUNC);
304fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod
3058b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod        /* sanitize again to ensure no toe-stepping */
30627e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod        context->edit_count = 0;
30739840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod	sane = t->sanitize (context);
30827e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod	if (context->edit_count) {
309fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	  if (HB_DEBUG_SANITIZE)
310fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	    fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
31127e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod		     blob, context->edit_count, HB_FUNC);
3124e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	  sane = false;
3134e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	}
3144e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
31598daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      context->finish ();
3164e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    } else {
31727e302dc8e794ff6bf878bc76e17d336d510849eBehdad Esfahbod      unsigned int edit_count = context->edit_count;
31898daaf183d6dbf2b68959da608cd9876ba55d7aaBehdad Esfahbod      context->finish ();
319977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod      if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
320977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod        /* ok, we made it writable by relocating.  try again */
321fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod	if (HB_DEBUG_SANITIZE)
3227d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod	  fprintf (stderr, "Sanitizer %p retry %s\n", blob, HB_FUNC);
3234e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod        goto retry;
3244e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
3254e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3264e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
327fa030175ca998b00cc42cbced6e98de323ec01ecBehdad Esfahbod    if (HB_DEBUG_SANITIZE)
3287d3a126334f8e6f6441561c1bb592bd3fa7a2c5cBehdad Esfahbod      fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", HB_FUNC);
3294e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane)
3304e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return blob;
3314e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    else {
3324e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      hb_blob_destroy (blob);
3334e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return hb_blob_create_empty ();
3344e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3354e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  }
3364e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod};
3374e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
3382d15e72c75931398db5e027e660f1320bb979117Behdad Esfahbod
339f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod
340f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod
341b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/*
342b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *
343bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod * The OpenType Font File: Data Types
344b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod */
345b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
346b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
347b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/* "The following data types are used in the OpenType font file.
348b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *  All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
349b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
3505f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
3515f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod * Int types
3525f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
3535f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
3546b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
355e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type, int Bytes> class BEInt;
356e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
357f1aaa2a43654c28405ffd393de2cb127437c99a5Behdad Esfahbod/* LONGTERMTODO: On machines allowing unaligned access, we can make the
358f1aaa2a43654c28405ffd393de2cb127437c99a5Behdad Esfahbod * following tighter by using byteswap instructions on ints directly. */
359e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
360e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodclass BEInt<Type, 2>
361e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
362e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  public:
36301c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline class BEInt<Type,2>& operator = (Type i) { hb_be_uint16_put (v,i); return *this; }
36401c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type () const { return hb_be_uint16_get (v); }
36501c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_cmp (v, o.v); }
36601c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
367e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  private: uint8_t v[2];
368e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
369e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
370e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodclass BEInt<Type, 4>
371e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
372e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  public:
37301c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline class BEInt<Type,4>& operator = (Type i) { hb_be_uint32_put (v,i); return *this; }
37401c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type () const { return hb_be_uint32_get (v); }
37501c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_cmp (v, o.v); }
37601c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
377e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  private: uint8_t v[4];
378e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
379e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
3802467c669c2aee4de2a6621a9d06cba0262376d41Behdad Esfahbod/* Integer types in big-endian order and no alignment requirement */
381e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
382e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodstruct IntType
383e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
38401c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline void set (Type i) { v = i; }
38501c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type(void) const { return v; }
38601c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
38701c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
38839840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
389e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod    TRACE_SANITIZE ();
390b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return context->check_struct (this);
391e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  }
392569da92bc6956f42d9b2d65c784e184fb6380efeBehdad Esfahbod  private:
393569da92bc6956f42d9b2d65c784e184fb6380efeBehdad Esfahbod  BEInt<Type, sizeof (Type)> v;
394569da92bc6956f42d9b2d65c784e184fb6380efeBehdad Esfahbod  public:
395e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  DEFINE_SIZE_STATIC (sizeof (Type));
396e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
397e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
398e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<uint16_t> USHORT;	/* 16-bit unsigned integer. */
399e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<int16_t>  SHORT;	/* 16-bit signed integer. */
400e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<uint32_t> ULONG;	/* 32-bit unsigned integer. */
401e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<int32_t>  LONG;		/* 32-bit signed integer. */
402e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
4036b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* Array of four uint8s (length = 32 bits) used to identify a script, language
4046b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod * system, feature, or baseline */
40520cc86b3592db33731de671f008d7d222776be49Behdad Esfahbodstruct Tag : ULONG
40660d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
407befc022affd2386b3f46cd7d11e4262f6c8bce9fBehdad Esfahbod  /* What the char* converters return is NOT nul-terminated.  Print using "%.4s" */
4080dfcc13a4668cdd2c2ebdd5f4a7540a51222cf2fBehdad Esfahbod  inline operator const char* (void) const { return CharP(this); }
409198facdc55756cb48cdfb8ba7fa50916fac54ec3Behdad Esfahbod  inline operator char* (void) { return CharP(this); }
410b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
411b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  DEFINE_SIZE_STATIC (4);
4126b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
41365f46b00333e20ab8a52a4b350747507541ec1dbBehdad EsfahbodDEFINE_NULL_DATA (Tag, "    ");
4146b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4156b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* Glyph index number, same as uint16 (length = 16 bits) */
4166ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT GlyphID;
4176b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4181f437e6f47fb6c15761021bd2078f31778f2179cBehdad Esfahbod/* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
4196ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT Offset;
4206ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4216ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
4226ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef ULONG LongOffset;
4236ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4246b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4256b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* CheckSum */
42660d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct CheckSum : ULONG
42760d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
42860d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
42960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
4306b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    uint32_t Sum = 0L;
431e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod    ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::static_size;
4326b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4336b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    while (Table < EndPtr)
4346b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod      Sum += *Table++;
4356b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    return Sum;
4366b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod  }
437b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
438b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  DEFINE_SIZE_STATIC (4);
4396b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
4406b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4416b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4426b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/*
4436b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod * Version Numbers
4446b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod */
4456b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
44687fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbodstruct FixedVersion
44760d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
44809c292e3b688a67fbae67b645d1e6ffcf8d8eb6eBehdad Esfahbod  inline operator uint32_t (void) const { return (major << 16) + minor; }
44996908b898476ca5d7da5f386b15be76f9e83d76eBehdad Esfahbod
45039840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
4513e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
452b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return context->check_struct (this);
453cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod  }
454cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod
4556ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod  USHORT major;
45687fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbod  USHORT minor;
457b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
458b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  DEFINE_SIZE_STATIC (4);
4596b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
4606b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
46192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
46292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
4635f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
46492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Template subclasses of Offset and LongOffset that do the dereferencing.
465f0abcd69408a3af65207cdf8847575ade4579bd4Behdad Esfahbod * Use: (base+offset)
4665f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
4675f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
46892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename OffsetType, typename Type>
46992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericOffsetTo : OffsetType
47092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod{
47100e23fcc6fd0eee5c582251bf3de6a2703fbbd3eBehdad Esfahbod  inline const Type& operator () (const void *base) const
47292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  {
47392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
47464d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (!offset)) return Null(Type);
47509766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod    return StructAtOffset<Type> (base, offset);
47692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
47792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
47839840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, void *base) {
4793e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
480b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    if (!context->check_struct (this)) return false;
48192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
48264d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (!offset)) return true;
48309766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod    Type &obj = StructAtOffset<Type> (base, offset);
48439840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    return likely (obj.sanitize (context)) || neuter (context);
48592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
4864a446ac35136eff23d55f47bdd7b40095ad707abBehdad Esfahbod  template <typename T>
48739840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, void *base, T user_data) {
4883e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
489b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    if (!context->check_struct (this)) return false;
49042b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    unsigned int offset = *this;
49164d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (!offset)) return true;
49209766b1ec5ec55a61edbcd7a89ed3613cc92d4cbBehdad Esfahbod    Type &obj = StructAtOffset<Type> (base, offset);
49339840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    return likely (obj.sanitize (context, user_data)) || neuter (context);
494c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod  }
495c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod
496c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod  private:
497c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod  /* Set the offset to Null */
49839840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool neuter (hb_sanitize_context_t *context) {
49940cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod    if (context->can_edit (this, this->static_size)) {
500c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod      this->set (0); /* 0 is Null offset */
501c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod      return true;
502c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod    }
503c9f14687a30866181feb57ee2736a147ec9f25a1Behdad Esfahbod    return false;
50442b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod  }
50592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod};
50692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Base, typename OffsetType, typename Type>
50792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodinline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
50892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
5095f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbodtemplate <typename Type>
51092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetTo : GenericOffsetTo<Offset, Type> {};
51192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
51292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
51392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
514bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
515bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
51692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/*
51792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Array Types
51892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod */
51992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
52092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename LenType, typename Type>
52192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericArrayOf
52260d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
5232cb08458f674301cde9d962c13911035a251f7c5Behdad Esfahbod  const Type *array(void) const { return &StructAfter<Type> (len); }
5242e2f43edf2f49f4047e28b1ce2ea95938536de9cBehdad Esfahbod  Type *array(void) { return &StructAfter<Type> (len); }
525d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad 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;
5352cb08458f674301cde9d962c13911035a251f7c5Behdad 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);
5412cb08458f674301cde9d962c13911035a251f7c5Behdad 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++)
5594f252fedc7136c66a9d7fbcb2978581986da6227Behdad 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++)
56839840474afd2cda9ff576c08aff9c87095496c27Behdad 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++)
57839840474afd2cda9ff576c08aff9c87095496c27Behdad 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;
592bea34c7cbb583cf7660776e95cab3171590b8427Behdad Esfahbod  private:
593bea34c7cbb583cf7660776e95cab3171590b8427Behdad Esfahbod  Type arrayX[VAR];
594b3651231bf80bb7009214547a75ed90e21815c68Behdad Esfahbod  public:
595bea34c7cbb583cf7660776e95cab3171590b8427Behdad Esfahbod  DEFINE_SIZE_VAR (sizeof (LenType), Type);
596e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod};
597e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod
59892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a USHORT number of elements. */
59992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
60092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct ArrayOf : GenericArrayOf<USHORT, Type> {};
60192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
60292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a ULONG number of elements. */
60392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
60492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongArrayOf : GenericArrayOf<ULONG, Type> {};
60592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
60692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of Offset's */
60792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
60892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
60992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
61092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of LongOffset's */
61192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
61292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
61392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
61492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* LongArray of LongOffset's */
61592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
61692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
61792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
61880e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod/* Array of offsets relative to the beginning of the array itself. */
61980e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodtemplate <typename Type>
62080e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodstruct OffsetListOf : OffsetArrayOf<Type>
62180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod{
62280e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
62380e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  {
62464d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (i >= this->len)) return Null(Type);
6252cb08458f674301cde9d962c13911035a251f7c5Behdad Esfahbod    return this+this->array()[i];
62680e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
62780e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
62839840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
6293e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
63040cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod    return OffsetArrayOf<Type>::sanitize (context, this);
63180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
6324a446ac35136eff23d55f47bdd7b40095ad707abBehdad Esfahbod  template <typename T>
63339840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context, T user_data) {
6343e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
63540cbefe858192531ed64dd51d402f7ca7b8153a3Behdad Esfahbod    return OffsetArrayOf<Type>::sanitize (context, this, user_data);
63680e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
63780e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod};
63880e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
63980e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
640e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod/* An array with a USHORT number of elements,
641e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod * starting at second element. */
642e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbodtemplate <typename Type>
64360d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct HeadlessArrayOf
64460d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
6452cb08458f674301cde9d962c13911035a251f7c5Behdad Esfahbod  const Type *array(void) const { return &StructAfter<Type> (len); }
6462e2f43edf2f49f4047e28b1ce2ea95938536de9cBehdad Esfahbod  Type *array(void) { return &StructAfter<Type> (len); }
647d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod
64860d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
64960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
65064d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod    if (unlikely (i >= len || !i)) return Null(Type);
6512cb08458f674301cde9d962c13911035a251f7c5Behdad Esfahbod    return array()[i-1];
652e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod  }
65360d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline unsigned int get_size () const
654e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod  { return len.static_size + (len ? len - 1 : 0) * Type::static_size; }
6555f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
65639840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize_shallow (hb_sanitize_context_t *context) {
657b157617644d1e38f680163889d1dc2e2f64d9ba3Behdad Esfahbod    return context->check_struct (this)
658e45d3f86f9a5f3d29ca35a282de7f98e702878f9Behdad Esfahbod	&& context->check_array (this, Type::static_size, len);
659e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod  }
660e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod
66139840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod  inline bool sanitize (hb_sanitize_context_t *context) {
6623e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
66339840474afd2cda9ff576c08aff9c87095496c27Behdad Esfahbod    if (!likely (sanitize_shallow (context))) return false;
66440d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* Note: for structs that do not reference other structs,
66540d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * we do not need to call their sanitize() as we already did
66640d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * a bound check on the aggregate array size, hence the return.
66740d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     */
6683564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    return true;
66940d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* We do keep this code though to make sure the structs pointed
67040d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * to do have a simple sanitize(), ie. they do not reference
67140d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * other structs. */
67215164d9258a74122a4db748d35532bd72c47cec2Behdad Esfahbod    unsigned int count = len ? len - 1 : 0;
673d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod    Type *a = array();
67470de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
6754f252fedc7136c66a9d7fbcb2978581986da6227Behdad Esfahbod      if (!a[i].sanitize (context))
67670de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod        return false;
6779bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
67870de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod  }
67970de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
6805f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  USHORT len;
681d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod/*Type array[VAR];*/
6825f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod};
6835f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
6846b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
6851e91434569a9e9535ef021ca52b60b2e2af75d19Behdad Esfahbod#endif /* HB_OPEN_TYPE_PRIVATE_HH */
686