hb-open-type-private.hh revision a92f0fda7fee9d60b399b8865541bf73e6e30141
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
68a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod/* StructAtOffset<T>(X,Ofs) returns the struct T& that is placed at memory
69a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod * location of X plus Ofs bytes. */
70a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodtemplate<typename Type, typename TObject>
71a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodinline const Type& StructAtOffset(const TObject &X, unsigned int offset)
72d632ec4000b3079150e6424e88a3ab7509f7445cBehdad Esfahbod{ return * reinterpret_cast<const Type*> (CharP(&X) + offset); }
73a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodtemplate<typename Type, typename TObject>
74a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbodinline Type& StructAtOffset(TObject &X, unsigned int offset)
75d632ec4000b3079150e6424e88a3ab7509f7445cBehdad Esfahbod{ return * reinterpret_cast<Type*> (CharP(&X) + 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)
81a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod{ return StructAtOffset<Type>(X, X.get_size()); }
82e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbodtemplate<typename Type, typename TObject>
83e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbodinline Type& StructAfter(TObject &X)
84a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod{ return StructAtOffset<Type>(X, X.get_size()); }
85a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod
86e961c86c579fd98ee604342a9c70c4e7f8d4f220Behdad Esfahbod
87d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod
8870de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod/*
89600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod * Class features
90600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod */
91600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
928b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
938b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Null objects */
948b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
958b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Global nul-content Null pool.  Enlarge as necessary. */
969d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodstatic const void *_NullPool[32 / sizeof (void *)];
978b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
988b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Generic template for nul-content sizeof-sized Null objects. */
998b8358033184198ff638ee1379093717596e162dBehdad Esfahbodtemplate <typename Type>
1009d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodstatic inline const Type& Null () {
1019d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbod  ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool));
102187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod  return *CastP<Type> (_NullPool);
1039d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbod}
1048b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1058b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
1068b8358033184198ff638ee1379093717596e162dBehdad Esfahbod#define DEFINE_NULL_DATA(Type, size, data) \
107565c80bd2960366ace2d10dd71beaaf2a80213c8Behdad Esfahbodstatic const char _Null##Type[size + 1] = data; /* +1 is for nul-termination in data */ \
1088b8358033184198ff638ee1379093717596e162dBehdad Esfahbodtemplate <> \
1099d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbodinline const Type& Null<Type> () { \
110187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod  return *CastP<Type> (_Null##Type); \
111565c80bd2960366ace2d10dd71beaaf2a80213c8Behdad Esfahbod} /* The following line really exists such that we end in a place needing semicolon */ \
112565c80bd2960366ace2d10dd71beaaf2a80213c8Behdad EsfahbodASSERT_STATIC (sizeof (Type) + 1 <= sizeof (_Null##Type))
1138b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1148b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Accessor macro. */
1159d3677899f90abdc7fb3e3d854db654a8707a84bBehdad Esfahbod#define Null(Type) Null<Type>()
1168b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
1178b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
118600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
119577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod/*
120577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod * Sanitize
121577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod */
122577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
12395e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#ifndef HB_DEBUG_SANITIZE
12495e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#define HB_DEBUG_SANITIZE HB_DEBUG
12595e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#endif
12695e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod
12795e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
128d0351314cd29fbdf0efb5c7f89a569648f7a7fc7Behdad Esfahbod#include <stdio.h>
1290e206de98621ed8a55824b42e9e6bf320f4c6cc8Behdad Esfahbod#define TRACE_SANITIZE_ARG_DEF	, unsigned int sanitize_depth HB_GNUC_UNUSED
1303e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG	, sanitize_depth + 1
1313e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG_INIT	, 1
1323e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE() \
133b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod	HB_STMT_START { \
13495e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod	    if (sanitize_depth < HB_DEBUG_SANITIZE) \
1357acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod		fprintf (stderr, "SANITIZE(%p) %-*d-> %s\n", \
136a92f0fda7fee9d60b399b8865541bf73e6e30141Behdad Esfahbod			 (CharP(this) == CharP(&_NullPool)) ? 0 : this, \
1379b76a290a94f2603f3cb9498ae976125347cf54bBehdad Esfahbod			 sanitize_depth, sanitize_depth, \
1387acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod			 __PRETTY_FUNCTION__); \
139b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod	} HB_STMT_END
140b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod#else
1413e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG_DEF
1423e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG
1433e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG_INIT
1443e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE() HB_STMT_START {} HB_STMT_END
145b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod#endif
146b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod
14741895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod#define SANITIZE_ARG_DEF \
1483e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod	hb_sanitize_context_t *context TRACE_SANITIZE_ARG_DEF
14941895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod#define SANITIZE_ARG \
1503e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod	context TRACE_SANITIZE_ARG
15141895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod#define SANITIZE_ARG_INIT \
1523e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod	&context TRACE_SANITIZE_ARG_INIT
15341895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod
154577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodtypedef struct _hb_sanitize_context_t hb_sanitize_context_t;
155577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodstruct _hb_sanitize_context_t
156577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
157577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  const char *start, *end;
158254933c397f1ce9796f59689a25f9fc2e58df4eaBehdad Esfahbod  hb_bool_t writable;
159254933c397f1ce9796f59689a25f9fc2e58df4eaBehdad Esfahbod  unsigned int edit_count;
160577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod};
161577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
162577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodstatic HB_GNUC_UNUSED void
1634e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod_hb_sanitize_init (hb_sanitize_context_t *context,
1644e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod		   hb_blob_t *blob)
165577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
166577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  context->start = hb_blob_lock (blob);
167577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  context->end = context->start + hb_blob_get_length (blob);
168254933c397f1ce9796f59689a25f9fc2e58df4eaBehdad Esfahbod  context->writable = hb_blob_is_writable (blob);
169577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  context->edit_count = 0;
1704f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
17195e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
1727acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod  fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
173a92f0fda7fee9d60b399b8865541bf73e6e30141Behdad Esfahbod	   blob, context->start, context->end, context->end - context->start);
1744f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
175577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod}
176577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
177577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodstatic HB_GNUC_UNUSED void
1781d720192b193f48b44be0385eda3c2c5d5cd28adBehdad Esfahbod_hb_sanitize_fini (hb_sanitize_context_t *context HB_GNUC_UNUSED,
1798dfdca599c0a3ba5255131002910bca3b381acacBehdad Esfahbod		   hb_blob_t *blob)
180577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
18195e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
1827acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod  fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
1838dfdca599c0a3ba5255131002910bca3b381acacBehdad Esfahbod	   blob, context->start, context->end, context->edit_count);
1844f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
1854f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
1868dfdca599c0a3ba5255131002910bca3b381acacBehdad Esfahbod  hb_blob_unlock (blob);
187577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod}
188577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
1894f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbodstatic HB_GNUC_UNUSED inline bool
19041895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod_hb_sanitize_check (SANITIZE_ARG_DEF,
19141895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod		    const char *base,
19241895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod		    unsigned int len)
19341895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod{
194ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod  bool ret = context->start <= base &&
195ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     base <= context->end &&
196ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     (unsigned int) (context->end - base) >= len;
197ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod
19895e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
19995e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod  if (sanitize_depth < HB_DEBUG_SANITIZE) \
200ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod    fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
201ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     base,
202ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     sanitize_depth, sanitize_depth,
203ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     base, base+len, len,
204ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     context->start, context->end,
205ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     ret ? "pass" : "FAIL");
206ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod#endif
207ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod  return ret;
20841895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod}
20941895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod
21041895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbodstatic HB_GNUC_UNUSED inline bool
211815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod_hb_sanitize_array (SANITIZE_ARG_DEF,
212815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod		    const char *base,
213815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod		    unsigned int record_size,
214815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod		    unsigned int len)
215815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod{
216815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod  bool overflows = len >= ((unsigned int) -1) / record_size;
217815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
21895e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
21995e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod  if (sanitize_depth < HB_DEBUG_SANITIZE) \
220815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod    fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
221815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     base,
222815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     sanitize_depth, sanitize_depth,
223815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     base, base + (record_size * len), record_size, len, (unsigned long) record_size * len,
224815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     context->start, context->end,
225815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     !overflows ? "does not overflow" : "OVERFLOWS FAIL");
226815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod#endif
227254933c397f1ce9796f59689a25f9fc2e58df4eaBehdad Esfahbod
228815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod  return HB_LIKELY (!overflows) && _hb_sanitize_check (SANITIZE_ARG, base, record_size * len);
229815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod}
230815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
231815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbodstatic HB_GNUC_UNUSED inline bool
23241895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod_hb_sanitize_edit (SANITIZE_ARG_DEF,
2334f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod		   const char *base HB_GNUC_UNUSED,
2344f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod		   unsigned int len HB_GNUC_UNUSED)
235577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
236f4b58d3fc2956a9d1b6178588d809c781f7a5c0cBehdad Esfahbod  context->edit_count++;
2374f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
23895e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
239ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod  fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
240ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   base,
241ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   sanitize_depth, sanitize_depth,
242ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   context->edit_count,
243ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   base, base+len, len,
244ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   context->start, context->end,
245254933c397f1ce9796f59689a25f9fc2e58df4eaBehdad Esfahbod	   context->writable ? "granted" : "REJECTED");
2464f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
247254933c397f1ce9796f59689a25f9fc2e58df4eaBehdad Esfahbod
248254933c397f1ce9796f59689a25f9fc2e58df4eaBehdad Esfahbod  return context->writable;
249577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod}
250577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
251577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG))
252577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE2(X,Y) (SANITIZE (X) && SANITIZE (Y))
253577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
25462c0fd75737a69721dbf29e773405a4c529f8b6dBehdad Esfahbod#define SANITIZE_THIS(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG, CharP(this)))
255577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_THIS2(X,Y) (SANITIZE_THIS (X) && SANITIZE_THIS (Y))
256577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_THIS3(X,Y,Z) (SANITIZE_THIS (X) && SANITIZE_THIS (Y) && SANITIZE_THIS(Z))
257577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
258577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_BASE(X,B) HB_LIKELY ((X).sanitize (SANITIZE_ARG, B))
259577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_BASE2(X,Y,B) (SANITIZE_BASE (X,B) && SANITIZE_BASE (Y,B))
260577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
261577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_SELF() SANITIZE_OBJ (*this)
262577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_OBJ(X) SANITIZE_MEM(&(X), sizeof (X))
263577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
2640dfcc13a4668cdd2c2ebdd5f4a7540a51222cf2fBehdad Esfahbod#define SANITIZE_MEM(B,L) HB_LIKELY (_hb_sanitize_check (SANITIZE_ARG, CharP(B), (L)))
265577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
2660dfcc13a4668cdd2c2ebdd5f4a7540a51222cf2fBehdad Esfahbod#define SANITIZE_ARRAY(A,S,L) HB_LIKELY (_hb_sanitize_array (SANITIZE_ARG, CharP(A), S, L))
267815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
26866d6eb30eb0b8d61e00f86ea0c7829abaddb52faBehdad Esfahbod#define NEUTER(Obj, Val) \
26966d6eb30eb0b8d61e00f86ea0c7829abaddb52faBehdad Esfahbod	(SANITIZE_OBJ (Obj) && \
27066d6eb30eb0b8d61e00f86ea0c7829abaddb52faBehdad Esfahbod	 _hb_sanitize_edit (SANITIZE_ARG, CharP(&(Obj)), (Obj).get_size ()) && \
27166d6eb30eb0b8d61e00f86ea0c7829abaddb52faBehdad Esfahbod	 ((Obj).set (Val), true))
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) {
2794e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    hb_sanitize_context_t context;
2804e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    bool sane;
2814e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
282d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod    /* TODO is_sane() stuff */
2834e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
2844e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  retry:
28595e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
2867acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod    fprintf (stderr, "Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__);
2874f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
2884f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
2894e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    _hb_sanitize_init (&context, blob);
2904e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
291efb324a46ff64adb4ec8612b4089e8daff1e6d8eBehdad Esfahbod    /* Note: We drop const here */
292187454c595559ce48d072fee6bccb51f3de283d4Behdad Esfahbod    Type *t = CastP<Type> ((void *) context.start);
2934e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
294b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod    sane = t->sanitize (SANITIZE_ARG_INIT);
2954e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane) {
2964e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      if (context.edit_count) {
29795e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
298ad3653751b1e4a03f7058200cb83f64db46722d5Behdad Esfahbod	fprintf (stderr, "Sanitizer %p passed first round with %d edits; doing a second round %s\n",
2990d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod		 blob, context.edit_count, __PRETTY_FUNCTION__);
3000d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod#endif
3018b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod        /* sanitize again to ensure no toe-stepping */
3024e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod        context.edit_count = 0;
303b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod	sane = t->sanitize (SANITIZE_ARG_INIT);
3044e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	if (context.edit_count) {
30595e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
306ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	  fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
3070d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod		   blob, context.edit_count, __PRETTY_FUNCTION__);
3080d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod#endif
3094e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	  sane = false;
3104e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	}
3114e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
3128dfdca599c0a3ba5255131002910bca3b381acacBehdad Esfahbod      _hb_sanitize_fini (&context, blob);
3134e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    } else {
3144f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod      unsigned int edit_count = context.edit_count;
3158dfdca599c0a3ba5255131002910bca3b381acacBehdad Esfahbod      _hb_sanitize_fini (&context, blob);
316977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod      if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
317977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod        /* ok, we made it writable by relocating.  try again */
31895e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
3197acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod	fprintf (stderr, "Sanitizer %p retry %s\n", blob, __PRETTY_FUNCTION__);
3204f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
3214e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod        goto retry;
3224e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
3234e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3244e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
32595e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
326ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod    fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", __PRETTY_FUNCTION__);
3274f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
3284e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane)
3294e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return blob;
3304e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    else {
3314e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      hb_blob_destroy (blob);
3324e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return hb_blob_create_empty ();
3334e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3344e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  }
3354e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod};
3364e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
3372d15e72c75931398db5e027e660f1320bb979117Behdad Esfahbod
338b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/*
339b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *
340bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod * The OpenType Font File: Data Types
341b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod */
342b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
343b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
344b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/* "The following data types are used in the OpenType font file.
345b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *  All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
346b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
3475f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
3485f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod * Int types
3495f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
3505f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
3516b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
352e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type, int Bytes> class BEInt;
353e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
354f1aaa2a43654c28405ffd393de2cb127437c99a5Behdad Esfahbod/* LONGTERMTODO: On machines allowing unaligned access, we can make the
355f1aaa2a43654c28405ffd393de2cb127437c99a5Behdad Esfahbod * following tighter by using byteswap instructions on ints directly. */
356e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
357e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodclass BEInt<Type, 2>
358e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
359e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  public:
36001c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline class BEInt<Type,2>& operator = (Type i) { hb_be_uint16_put (v,i); return *this; }
36101c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type () const { return hb_be_uint16_get (v); }
36201c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const BEInt<Type, 2>& o) const { return hb_be_uint16_cmp (v, o.v); }
36301c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const BEInt<Type, 2>& o) const { return !(*this == o); }
364e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  private: uint8_t v[2];
365e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
366e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
367e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodclass BEInt<Type, 4>
368e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
369e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  public:
37001c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline class BEInt<Type,4>& operator = (Type i) { hb_be_uint32_put (v,i); return *this; }
37101c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type () const { return hb_be_uint32_get (v); }
37201c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const BEInt<Type, 4>& o) const { return hb_be_uint32_cmp (v, o.v); }
37301c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const BEInt<Type, 4>& o) const { return !(*this == o); }
374e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  private: uint8_t v[4];
375e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
376e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
3772467c669c2aee4de2a6621a9d06cba0262376d41Behdad Esfahbod/* Integer types in big-endian order and no alignment requirement */
378e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtemplate <typename Type>
379e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodstruct IntType
380e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod{
381e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  static inline unsigned int get_size () { return sizeof (Type); }
38201c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline void set (Type i) { v = i; }
38301c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline operator Type(void) const { return v; }
38401c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator == (const IntType<Type> &o) const { return v == o.v; }
38501c01618e98283611628cd54d5ba4bf122f24cd9Behdad Esfahbod  inline bool operator != (const IntType<Type> &o) const { return v != o.v; }
386e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
387e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod    TRACE_SANITIZE ();
388e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod    return SANITIZE_SELF ();
389e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  }
390e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod  private: BEInt<Type, sizeof (Type)> v;
391e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod};
392e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
393e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<uint16_t> USHORT;	/* 16-bit unsigned integer. */
394e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<int16_t>  SHORT;	/* 16-bit signed integer. */
395e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<uint32_t> ULONG;	/* 32-bit unsigned integer. */
396e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbodtypedef IntType<int32_t>  LONG;		/* 32-bit signed integer. */
397e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad Esfahbod
398e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad EsfahbodASSERT_SIZE (USHORT, 2);
399e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad EsfahbodASSERT_SIZE (SHORT, 2);
400e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad EsfahbodASSERT_SIZE (ULONG, 4);
401e032ed9f75d4a0f365649a25706871bbb5ae6651Behdad EsfahbodASSERT_SIZE (LONG, 4);
402bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad 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); }
4106b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
411303fe62824d4e99df554b6bfaacba05d068522fbBehdad EsfahbodASSERT_SIZE (Tag, 4);
4129d3677899f90abdc7fb3e3d854db654a8707a84bBehdad EsfahbodDEFINE_NULL_DATA (Tag, 4, "    ");
4136b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4146b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* Glyph index number, same as uint16 (length = 16 bits) */
4156ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT GlyphID;
4166b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4171f437e6f47fb6c15761021bd2078f31778f2179cBehdad Esfahbod/* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
4186ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT Offset;
4196ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4206ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
4216ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef ULONG LongOffset;
4226ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4236b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4246b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* CheckSum */
42560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct CheckSum : ULONG
42660d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
42760d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
42860d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
4296b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    uint32_t Sum = 0L;
4304b8487d83e0c10076a6c573cb3487790ce366607Behdad Esfahbod    ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::get_size ();
4316b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4326b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    while (Table < EndPtr)
4336b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod      Sum += *Table++;
4346b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    return Sum;
4356b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod  }
4366b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
4378b8358033184198ff638ee1379093717596e162dBehdad EsfahbodASSERT_SIZE (CheckSum, 4);
4386b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4396b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4406b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/*
4416b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod * Version Numbers
4426b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod */
4436b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
44487fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbodstruct FixedVersion
44560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
44609c292e3b688a67fbae67b645d1e6ffcf8d8eb6eBehdad Esfahbod  inline operator uint32_t (void) const { return (major << 16) + minor; }
44796908b898476ca5d7da5f386b15be76f9e83d76eBehdad Esfahbod
448cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
4493e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
450cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod    return SANITIZE_SELF ();
451cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod  }
452cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod
4536ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod  USHORT major;
45487fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbod  USHORT minor;
4556b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
45687fcdcbe3644da10154688765db2d62eb9ac079aBehdad EsfahbodASSERT_SIZE (FixedVersion, 4);
4576b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
45892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
45992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
4605f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
46192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Template subclasses of Offset and LongOffset that do the dereferencing.
46292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Use: (this+memberName)
4635f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
4645f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
46592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename OffsetType, typename Type>
46692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericOffsetTo : OffsetType
46792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod{
46800e23fcc6fd0eee5c582251bf3de6a2703fbbd3eBehdad Esfahbod  inline const Type& operator () (const void *base) const
46992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  {
47092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
47192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    if (HB_UNLIKELY (!offset)) return Null(Type);
472a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod    return StructAtOffset<Type> (*CharP(base), offset);
47392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
47492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
47562c0fd75737a69721dbf29e773405a4c529f8b6dBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
4763e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
47795528131b5ab9fc9e265ace715832135ebd457a4Behdad Esfahbod    if (!SANITIZE_SELF ()) return false;
47892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
47992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    if (HB_UNLIKELY (!offset)) return true;
480a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod    return SANITIZE (StructAtOffset<Type> (*CharP(base), offset)) || NEUTER (*this, 0);
48192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
48262c0fd75737a69721dbf29e773405a4c529f8b6dBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
4833e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
48495528131b5ab9fc9e265ace715832135ebd457a4Behdad Esfahbod    if (!SANITIZE_SELF ()) return false;
485b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    unsigned int offset = *this;
486b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    if (HB_UNLIKELY (!offset)) return true;
487a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod    return SANITIZE_BASE (StructAtOffset<Type> (*CharP(base), offset), base2) || NEUTER (*this, 0);
488b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod  }
48962c0fd75737a69721dbf29e773405a4c529f8b6dBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
4903e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
49195528131b5ab9fc9e265ace715832135ebd457a4Behdad Esfahbod    if (!SANITIZE_SELF ()) return false;
49242b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    unsigned int offset = *this;
49342b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    if (HB_UNLIKELY (!offset)) return true;
494a3263aa773ad7a914496792466c69047048b093cBehdad Esfahbod    return SANITIZE_BASE (StructAtOffset<Type> (*CharP(base), offset), user_data) || NEUTER (*this, 0);
49542b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod  }
49692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod};
49792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Base, typename OffsetType, typename Type>
49892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodinline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
49992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
5005f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbodtemplate <typename Type>
50192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetTo : GenericOffsetTo<Offset, Type> {};
50292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
50392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
50492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
505bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
506bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
50792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/*
50892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Array Types
50992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod */
51092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
51192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename LenType, typename Type>
51292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericArrayOf
51360d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
5142cb08458f674301cde9d962c13911035a251f7c5Behdad Esfahbod  const Type *array(void) const { return &StructAfter<Type> (len); }
5152e2f43edf2f49f4047e28b1ce2ea95938536de9cBehdad Esfahbod  Type *array(void) { return &StructAfter<Type> (len); }
516d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad 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;
52048de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    if (HB_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;
5262cb08458f674301cde9d962c13911035a251f7c5Behdad Esfahbod    return array() + start_offset;
52748de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod  }
52848de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod
52960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
53060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
5315f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod    if (HB_UNLIKELY (i >= len)) return Null(Type);
5322cb08458f674301cde9d962c13911035a251f7c5Behdad Esfahbod    return array()[i];
5335f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  }
53460d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline unsigned int get_size () const
5354b8487d83e0c10076a6c573cb3487790ce366607Behdad Esfahbod  { return len.get_size () + len * Type::get_size (); }
536e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod
537e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod  inline bool sanitize_shallow (SANITIZE_ARG_DEF) {
538e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod    TRACE_SANITIZE ();
539e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod    return SANITIZE_SELF() && SANITIZE_ARRAY (this, Type::get_size (), len);
540e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod  }
541e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod
54220b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
5433e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
544e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod    if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
54540d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* Note: for structs that do not reference other structs,
54640d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * we do not need to call their sanitize() as we already did
54740d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * a bound check on the aggregate array size, hence the return.
54840d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     */
5493564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    return true;
55040d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* We do keep this code though to make sure the structs pointed
55140d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * to do have a simple sanitize(), ie. they do not reference
55240d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * other structs. */
55370de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    unsigned int count = len;
55470de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
555d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!SANITIZE (array()[i]))
55670de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod        return false;
5579bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
55870de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod  }
55962c0fd75737a69721dbf29e773405a4c529f8b6dBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
5603e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
561e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod    if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
562e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod    unsigned int count = len;
563e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod    for (unsigned int i = 0; i < count; i++)
564d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!array()[i].sanitize (SANITIZE_ARG, base))
565e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod        return false;
5669bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
567e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod  }
56862c0fd75737a69721dbf29e773405a4c529f8b6dBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
5693e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
570e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod    if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
571b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    unsigned int count = len;
572b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    for (unsigned int i = 0; i < count; i++)
573d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!array()[i].sanitize (SANITIZE_ARG, base, base2))
574b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod        return false;
5759bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
576b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod  }
57762c0fd75737a69721dbf29e773405a4c529f8b6dBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
5783e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
579e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod    if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
58042b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    unsigned int count = len;
58142b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
582d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!array()[i].sanitize (SANITIZE_ARG, base, user_data))
58342b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod        return false;
5849bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
58542b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod  }
58670de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
58792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  LenType len;
588d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod/*Type array[VAR];*/
589e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod};
590e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod
59192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a USHORT number of elements. */
59292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
59392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct ArrayOf : GenericArrayOf<USHORT, Type> {};
59492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
59592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a ULONG number of elements. */
59692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
59792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongArrayOf : GenericArrayOf<ULONG, Type> {};
59892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
59992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of Offset's */
60092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
60192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
60292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
60392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of LongOffset's */
60492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
60592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
60692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
60792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* LongArray of LongOffset's */
60892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
60992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
61092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
61180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod/* Array of offsets relative to the beginning of the array itself. */
61280e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodtemplate <typename Type>
61380e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodstruct OffsetListOf : OffsetArrayOf<Type>
61480e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod{
61580e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
61680e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  {
61780e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod    if (HB_UNLIKELY (i >= this->len)) return Null(Type);
6182cb08458f674301cde9d962c13911035a251f7c5Behdad Esfahbod    return this+this->array()[i];
61980e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
62080e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
62180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
6223e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
62362c0fd75737a69721dbf29e773405a4c529f8b6dBehdad Esfahbod    return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this));
62480e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
62580e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, unsigned int user_data) {
6263e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
62762c0fd75737a69721dbf29e773405a4c529f8b6dBehdad Esfahbod    return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CharP(this), user_data);
62880e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
62980e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod};
63080e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
63180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
632e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod/* An array with a USHORT number of elements,
633e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod * starting at second element. */
634e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbodtemplate <typename Type>
63560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct HeadlessArrayOf
63660d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
6372cb08458f674301cde9d962c13911035a251f7c5Behdad Esfahbod  const Type *array(void) const { return &StructAfter<Type> (len); }
6382e2f43edf2f49f4047e28b1ce2ea95938536de9cBehdad Esfahbod  Type *array(void) { return &StructAfter<Type> (len); }
639d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod
64060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
64160d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
642e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod    if (HB_UNLIKELY (i >= len || !i)) return Null(Type);
6432cb08458f674301cde9d962c13911035a251f7c5Behdad Esfahbod    return array()[i-1];
644e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod  }
64560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline unsigned int get_size () const
6464b8487d83e0c10076a6c573cb3487790ce366607Behdad Esfahbod  { return len.get_size () + (len ? len - 1 : 0) * Type::get_size (); }
6475f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
648e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod  inline bool sanitize_shallow (SANITIZE_ARG_DEF) {
649e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod    TRACE_SANITIZE ();
650e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod    return SANITIZE_SELF() && SANITIZE_ARRAY (this, Type::get_size (), len);
651e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod  }
652e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod
65320b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
6543e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
655e5546a4352c54311ac4a9ef138b187378155ebe1Behdad Esfahbod    if (!HB_LIKELY (sanitize_shallow (SANITIZE_ARG))) return false;
65640d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* Note: for structs that do not reference other structs,
65740d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * we do not need to call their sanitize() as we already did
65840d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * a bound check on the aggregate array size, hence the return.
65940d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     */
6603564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    return true;
66140d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod    /* We do keep this code though to make sure the structs pointed
66240d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * to do have a simple sanitize(), ie. they do not reference
66340d73bc68dd828cf68f90fde0f9499a6ce9fbb19Behdad Esfahbod     * other structs. */
66415164d9258a74122a4db748d35532bd72c47cec2Behdad Esfahbod    unsigned int count = len ? len - 1 : 0;
665d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod    Type *a = array();
66670de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
667d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!SANITIZE (a[i]))
66870de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod        return false;
6699bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
67070de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod  }
67170de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
6725f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  USHORT len;
673d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod/*Type array[VAR];*/
6745f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod};
6755f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
6766b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
6771e91434569a9e9535ef021ca52b60b2e2af75d19Behdad Esfahbod#endif /* HB_OPEN_TYPE_PRIVATE_HH */
678