hb-open-type-private.hh revision f9b37727985191c9b4aedb0e9835736027e59260
164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod/*
2f9b37727985191c9b4aedb0e9835736027e59260Behdad Esfahbod * Copyright (C) 2007,2008,2009,2010  Red Hat, Inc.
364aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *
464aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod *  This is part of HarfBuzz, an OpenType Layout engine 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
35706ab25a4cb043d46e6088aa0a7184ee200276c9Behdad Esfahbod#define NO_INDEX		((unsigned int) 0xFFFF)
365a0b791184cf6ef39eae0570e14aca21abc32845Behdad Esfahbod
37706ab25a4cb043d46e6088aa0a7184ee200276c9Behdad Esfahbod
38196598bbccff08415ff5192314cba044df258cadBehdad Esfahbod/*
39196598bbccff08415ff5192314cba044df258cadBehdad Esfahbod * Casts
40196598bbccff08415ff5192314cba044df258cadBehdad Esfahbod */
41196598bbccff08415ff5192314cba044df258cadBehdad Esfahbod
422b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod#define CONST_CHARP(X)		(reinterpret_cast<const char *>(X))
432b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod#define DECONST_CHARP(X)	((char *)reinterpret_cast<const char *>(X))
442b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod#define CHARP(X)		(reinterpret_cast<char *>(X))
452b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod
462b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod#define CONST_CAST(T,X,Ofs)	(*(reinterpret_cast<const T *>(CONST_CHARP(&(X)) + Ofs)))
472b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod#define DECONST_CAST(T,X,Ofs)	(*(reinterpret_cast<T *>((char *)CONST_CHARP(&(X)) + Ofs)))
482b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod#define CAST(T,X,Ofs) 		(*(reinterpret_cast<T *>(CHARP(&(X)) + Ofs)))
49196598bbccff08415ff5192314cba044df258cadBehdad Esfahbod
50284899ccbe79fda7405ab09d3092fc25fd89e810Behdad Esfahbod#define CONST_NEXT(T,X)		(*(reinterpret_cast<const T *>(CONST_CHARP(&(X)) + (X).get_size ())))
51284899ccbe79fda7405ab09d3092fc25fd89e810Behdad Esfahbod#define NEXT(T,X)		(*(reinterpret_cast<T *>(CHARP(&(X)) + (X).get_size ())))
5270de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
534b8487d83e0c10076a6c573cb3487790ce366607Behdad Esfahbod#define CONST_ARRAY_AFTER(T,X)	((reinterpret_cast<const T *>(CONST_CHARP(&(X)) + X.get_size ())))
544b8487d83e0c10076a6c573cb3487790ce366607Behdad Esfahbod#define ARRAY_AFTER(T,X)	((reinterpret_cast<T *>(CHARP(&(X)) + X.get_size ())))
55d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod
5670de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod/*
57600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod * Class features
58600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod */
59600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
608b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
618b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Null objects */
628b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
638b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Global nul-content Null pool.  Enlarge as necessary. */
64ecd2e996d2137fef30011a41dd57ea45f9a796deBehdad Esfahbodstatic const void *NullPool[32 / sizeof (void *)];
658b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
668b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Generic template for nul-content sizeof-sized Null objects. */
678b8358033184198ff638ee1379093717596e162dBehdad Esfahbodtemplate <typename Type>
6860d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct Null
6960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
70f8dc67b3c24dfc805da756a73cb217b36e16b4b8Behdad Esfahbod  ASSERT_STATIC (sizeof (Type) <= sizeof (NullPool));
718b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod  static inline const Type &get () { return CONST_CAST (Type, *NullPool, 0); }
728b8358033184198ff638ee1379093717596e162dBehdad Esfahbod};
738b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
748b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
758b8358033184198ff638ee1379093717596e162dBehdad Esfahbod#define DEFINE_NULL_DATA(Type, size, data) \
76cc6c644ff2af5f6669b6ec100ff13e904872b21cBehdad Esfahbodstatic const char _Null##Type[size] = data; \
778b8358033184198ff638ee1379093717596e162dBehdad Esfahbodtemplate <> \
7860d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct Null <Type> \
7960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{ \
808b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod  static inline const Type &get () { return CONST_CAST (Type, *_Null##Type, 0); } \
818b8358033184198ff638ee1379093717596e162dBehdad Esfahbod}
828b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
838b8358033184198ff638ee1379093717596e162dBehdad Esfahbod/* Accessor macro. */
848b8358033184198ff638ee1379093717596e162dBehdad Esfahbod#define Null(Type) (Null<Type>::get())
858b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
868b8358033184198ff638ee1379093717596e162dBehdad Esfahbod
87600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod/* get_for_data() is a static class method returning a reference to an
88600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod * instance of Type located at the input data location.  It's just a
8954e5aac5e2947d4e2864c6f2987e4d275da73100Behdad Esfahbod * fancy, NULL-safe, cast! */
90fd92a3dde32fd10df30c9eeb97641bc3c15b1e9bBehdad Esfahbod#define STATIC_DEFINE_GET_FOR_DATA(Type) \
9160d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  static inline const Type& get_for_data (const char *data) \
9260d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  { \
938b8358033184198ff638ee1379093717596e162dBehdad Esfahbod    if (HB_UNLIKELY (data == NULL)) return Null(Type); \
948b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod    return CONST_CAST (Type, *data, 0); \
95212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod  }
96212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod/* Like get_for_data(), but checks major version first. */
97cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod#define STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION(Type, MajorMin, MajorMax) \
98212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod  static inline const Type& get_for_data (const char *data) \
99212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod  { \
100212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod    if (HB_UNLIKELY (data == NULL)) return Null(Type); \
1018b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod    const Type& t = CONST_CAST (Type, *data, 0); \
102cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod    if (HB_UNLIKELY (t.version.major < MajorMin || t.version.major > MajorMax)) return Null(Type); \
103212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod    return t; \
104212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod  }
105600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
106600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
107577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod/*
108577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod * Sanitize
109577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod */
110577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
11195e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#ifndef HB_DEBUG_SANITIZE
11295e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#define HB_DEBUG_SANITIZE HB_DEBUG
11395e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#endif
11495e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod
11595e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
116d0351314cd29fbdf0efb5c7f89a569648f7a7fc7Behdad Esfahbod#include <stdio.h>
1170e206de98621ed8a55824b42e9e6bf320f4c6cc8Behdad Esfahbod#define TRACE_SANITIZE_ARG_DEF	, unsigned int sanitize_depth HB_GNUC_UNUSED
1183e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG	, sanitize_depth + 1
1193e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG_INIT	, 1
1203e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE() \
121b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod	HB_STMT_START { \
12295e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod	    if (sanitize_depth < HB_DEBUG_SANITIZE) \
1237acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod		fprintf (stderr, "SANITIZE(%p) %-*d-> %s\n", \
124d5943407a4251cb947fbfc130c0facb2f6216bd6Behdad Esfahbod			 (CONST_CHARP (this) == CONST_CHARP (&NullPool)) ? 0 : this, \
1259b76a290a94f2603f3cb9498ae976125347cf54bBehdad Esfahbod			 sanitize_depth, sanitize_depth, \
1267acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod			 __PRETTY_FUNCTION__); \
127b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod	} HB_STMT_END
128b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod#else
1293e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG_DEF
1303e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG
1313e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG_INIT
1323e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE() HB_STMT_START {} HB_STMT_END
133b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod#endif
134b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod
13541895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod#define SANITIZE_ARG_DEF \
1363e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod	hb_sanitize_context_t *context TRACE_SANITIZE_ARG_DEF
13741895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod#define SANITIZE_ARG \
1383e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod	context TRACE_SANITIZE_ARG
13941895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod#define SANITIZE_ARG_INIT \
1403e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod	&context TRACE_SANITIZE_ARG_INIT
14141895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod
142577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodtypedef struct _hb_sanitize_context_t hb_sanitize_context_t;
143577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodstruct _hb_sanitize_context_t
144577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
145577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  const char *start, *end;
146577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  int edit_count;
147577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  hb_blob_t *blob;
148577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod};
149577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
150577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodstatic HB_GNUC_UNUSED void
1514e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod_hb_sanitize_init (hb_sanitize_context_t *context,
1524e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod		   hb_blob_t *blob)
153577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
154577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  context->blob = blob;
155577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  context->start = hb_blob_lock (blob);
156577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  context->end = context->start + hb_blob_get_length (blob);
157577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  context->edit_count = 0;
1584f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
15995e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
1607acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod  fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
1617f96b39a9d5a81ba77e0c3dea8fe2cdb9957c4c7Behdad Esfahbod	   context->blob, context->start, context->end, context->end - context->start);
1624f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
163577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod}
164577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
165577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodstatic HB_GNUC_UNUSED void
1664e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod_hb_sanitize_fini (hb_sanitize_context_t *context,
1674e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod		   bool unlock)
168577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
16995e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
1707acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod  fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
1717acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod	   context->blob, context->start, context->end, context->edit_count);
1724f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
1734f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
174577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  if (unlock)
175577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod    hb_blob_unlock (context->blob);
176577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod}
177577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
1784f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbodstatic HB_GNUC_UNUSED inline bool
17941895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod_hb_sanitize_check (SANITIZE_ARG_DEF,
18041895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod		    const char *base,
18141895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod		    unsigned int len)
18241895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod{
183ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod  bool ret = context->start <= base &&
184ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     base <= context->end &&
185ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     (unsigned int) (context->end - base) >= len;
186ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod
18795e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
18895e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod  if (sanitize_depth < HB_DEBUG_SANITIZE) \
189ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod    fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
190ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     base,
191ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     sanitize_depth, sanitize_depth,
192ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     base, base+len, len,
193ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     context->start, context->end,
194ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     ret ? "pass" : "FAIL");
195ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod#endif
196ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod  return ret;
19741895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod}
19841895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod
19941895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbodstatic HB_GNUC_UNUSED inline bool
200815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod_hb_sanitize_array (SANITIZE_ARG_DEF,
201815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod		    const char *base,
202815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod		    unsigned int record_size,
203815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod		    unsigned int len)
204815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod{
205815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod  bool overflows = len >= ((unsigned int) -1) / record_size;
206815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
20795e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
20895e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod  if (sanitize_depth < HB_DEBUG_SANITIZE) \
209815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod    fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
210815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     base,
211815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     sanitize_depth, sanitize_depth,
212815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     base, base + (record_size * len), record_size, len, (unsigned long) record_size * len,
213815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     context->start, context->end,
214815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     !overflows ? "does not overflow" : "OVERFLOWS FAIL");
215815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod#endif
216815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod  return HB_LIKELY (!overflows) && _hb_sanitize_check (SANITIZE_ARG, base, record_size * len);
217815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod}
218815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
219815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbodstatic HB_GNUC_UNUSED inline bool
22041895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod_hb_sanitize_edit (SANITIZE_ARG_DEF,
2214f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod		   const char *base HB_GNUC_UNUSED,
2224f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod		   unsigned int len HB_GNUC_UNUSED)
223577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
224977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod  bool perm = hb_blob_try_writable_inplace (context->blob);
225f4b58d3fc2956a9d1b6178588d809c781f7a5c0cBehdad Esfahbod  context->edit_count++;
2264f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
22795e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
228ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod  fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
229ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   base,
230ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   sanitize_depth, sanitize_depth,
231ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   context->edit_count,
232ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   base, base+len, len,
233ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   context->start, context->end,
234ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   perm ? "granted" : "REJECTED");
2354f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
2364f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod  return perm;
237577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod}
238577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
239577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG))
240577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE2(X,Y) (SANITIZE (X) && SANITIZE (Y))
241577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
242577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_THIS(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG, CONST_CHARP(this)))
243577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_THIS2(X,Y) (SANITIZE_THIS (X) && SANITIZE_THIS (Y))
244577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_THIS3(X,Y,Z) (SANITIZE_THIS (X) && SANITIZE_THIS (Y) && SANITIZE_THIS(Z))
245577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
246577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_BASE(X,B) HB_LIKELY ((X).sanitize (SANITIZE_ARG, B))
247577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_BASE2(X,Y,B) (SANITIZE_BASE (X,B) && SANITIZE_BASE (Y,B))
248577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
249577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_SELF() SANITIZE_OBJ (*this)
250577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_OBJ(X) SANITIZE_MEM(&(X), sizeof (X))
251577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_GET_SIZE() SANITIZE_SELF() && SANITIZE_MEM (this, this->get_size ())
252577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
253d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod/* TODO Optimize this if L is fixed (gcc magic) */
25441895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod#define SANITIZE_MEM(B,L) HB_LIKELY (_hb_sanitize_check (SANITIZE_ARG, CONST_CHARP(B), (L)))
255577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
256815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod#define SANITIZE_ARRAY(A,S,L) HB_LIKELY (_hb_sanitize_array (SANITIZE_ARG, CONST_CHARP(A), S, L))
257815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
258d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod#define NEUTER(Var, Val) \
259d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod	(SANITIZE_OBJ (Var) && \
26041895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod	 _hb_sanitize_edit (SANITIZE_ARG, CONST_CHARP(&(Var)), sizeof (Var)) && \
261807b8aa486753474e05e09f4fcca8ac94021b97cBehdad Esfahbod	 ((Var).set (Val), true))
262577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
263577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
2644e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod/* Template to sanitize an object. */
2654e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbodtemplate <typename Type>
2664e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbodstruct Sanitizer
2674e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod{
2684e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  static hb_blob_t *sanitize (hb_blob_t *blob) {
2694e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    hb_sanitize_context_t context;
2704e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    bool sane;
2714e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
272d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod    /* TODO is_sane() stuff */
2734e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
2744e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  retry:
27595e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
2767acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod    fprintf (stderr, "Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__);
2774f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
2784f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
2794e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    _hb_sanitize_init (&context, blob);
2804e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
2818cd6fa28d1b77100491519b8dedb2e113508bf59Behdad Esfahbod    Type *t = &CAST (Type, *DECONST_CHARP(context.start), 0);
2824e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
283b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod    sane = t->sanitize (SANITIZE_ARG_INIT);
2844e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane) {
2854e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      if (context.edit_count) {
28695e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
2870d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod	fprintf (stderr, "Sanitizer %p passed first round with %d edits; going a second round %s\n",
2880d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod		 blob, context.edit_count, __PRETTY_FUNCTION__);
2890d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod#endif
2908b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod        /* sanitize again to ensure no toe-stepping */
2914e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod        context.edit_count = 0;
292b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod	sane = t->sanitize (SANITIZE_ARG_INIT);
2934e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	if (context.edit_count) {
29495e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
295ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	  fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
2960d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod		   blob, context.edit_count, __PRETTY_FUNCTION__);
2970d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod#endif
2984e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	  sane = false;
2994e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	}
3004e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
3014e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      _hb_sanitize_fini (&context, true);
3024e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    } else {
3034f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod      unsigned int edit_count = context.edit_count;
3044e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      _hb_sanitize_fini (&context, true);
305977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod      if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
306977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod        /* ok, we made it writable by relocating.  try again */
30795e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
3087acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod	fprintf (stderr, "Sanitizer %p retry %s\n", blob, __PRETTY_FUNCTION__);
3094f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
3104e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod        goto retry;
3114e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
3124e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3134e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
31495e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
315ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod    fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", __PRETTY_FUNCTION__);
3164f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
3174e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane)
3184e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return blob;
3194e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    else {
3204e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      hb_blob_destroy (blob);
3214e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return hb_blob_create_empty ();
3224e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3234e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  }
3244e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
325d60bb8ca2ae6edf29b2227b56c57f0d16879370bBehdad Esfahbod  static const Type& lock_instance (hb_blob_t *blob) {
3264e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    return Type::get_for_data (hb_blob_lock (blob));
3274e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  }
3284e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod};
3294e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
3302d15e72c75931398db5e027e660f1320bb979117Behdad Esfahbod
331b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/*
332b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *
333bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod * The OpenType Font File: Data Types
334b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod */
335b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
336b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
337b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/* "The following data types are used in the OpenType font file.
338b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *  All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
339b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
3405f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
3415f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod * Int types
3425f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
3435f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
3449e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod/* TODO On machines that allow unaligned access, use this version. */
3459e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod#define _DEFINE_INT_TYPE1_UNALIGNED(NAME, TYPE, BIG_ENDIAN, BYTES) \
34660d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  struct NAME \
34760d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  { \
348807b8aa486753474e05e09f4fcca8ac94021b97cBehdad Esfahbod    inline NAME& set (TYPE i) { (TYPE&) v = BIG_ENDIAN (i); return *this; } \
34920cc86b3592db33731de671f008d7d222776be49Behdad Esfahbod    inline operator TYPE(void) const { return BIG_ENDIAN ((TYPE&) v); } \
350f9b37727985191c9b4aedb0e9835736027e59260Behdad Esfahbod    inline bool operator== (const NAME &o) const { return (TYPE&) v == (TYPE&) o.v; } \
351b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod    inline bool sanitize (SANITIZE_ARG_DEF) { \
3523e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod      TRACE_SANITIZE (); \
353b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod      return SANITIZE_SELF (); \
354b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod    } \
3559e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod    private: unsigned char v[BYTES]; \
3569e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod  }; \
3579e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod  ASSERT_SIZE (NAME, BYTES)
3589e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod
3599e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod#define DEFINE_INT_TYPE1(NAME, TYPE, BIG_ENDIAN, BYTES) \
3609e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod  struct NAME \
3619e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod  { \
3624b8487d83e0c10076a6c573cb3487790ce366607Behdad Esfahbod    static inline unsigned int get_size () { return BYTES; } \
363807b8aa486753474e05e09f4fcca8ac94021b97cBehdad Esfahbod    inline NAME& set (TYPE i) { BIG_ENDIAN##_put_unaligned(v, i); return *this; } \
3649e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod    inline operator TYPE(void) const { return BIG_ENDIAN##_get_unaligned (v); } \
365f9b37727985191c9b4aedb0e9835736027e59260Behdad Esfahbod    inline bool operator== (const NAME &o) const { return BIG_ENDIAN##_cmp_unaligned (v, o.v); } \
3669e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod    inline bool sanitize (SANITIZE_ARG_DEF) { \
3673e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod      TRACE_SANITIZE (); \
3689e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod      return SANITIZE_SELF (); \
3699e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod    } \
3709e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod    private: unsigned char v[BYTES]; \
3715f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  }; \
37220cc86b3592db33731de671f008d7d222776be49Behdad Esfahbod  ASSERT_SIZE (NAME, BYTES)
373df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define DEFINE_INT_TYPE0(NAME, type, b)	DEFINE_INT_TYPE1 (NAME, type##_t, hb_be_##type, b)
374df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define DEFINE_INT_TYPE(NAME, u, w)	DEFINE_INT_TYPE0 (NAME, u##int##w, (w / 8))
3755f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
376b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
377f85ec1c7da36d2c2c9b1c94a988563697dcc79c9Behdad EsfahbodDEFINE_INT_TYPE (USHORT, u, 16);	/* 16-bit unsigned integer. */
37820cc86b3592db33731de671f008d7d222776be49Behdad EsfahbodDEFINE_INT_TYPE (SHORT,	  , 16);	/* 16-bit signed integer. */
37920cc86b3592db33731de671f008d7d222776be49Behdad EsfahbodDEFINE_INT_TYPE (ULONG,	 u, 32);	/* 32-bit unsigned integer. */
38020cc86b3592db33731de671f008d7d222776be49Behdad EsfahbodDEFINE_INT_TYPE (LONG,	  , 32);	/* 32-bit signed integer. */
3816b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
382bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
3836b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* Array of four uint8s (length = 32 bits) used to identify a script, language
3846b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod * system, feature, or baseline */
38520cc86b3592db33731de671f008d7d222776be49Behdad Esfahbodstruct Tag : ULONG
38660d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
3874497af0069a94c69fc1518b1db2c1282721b732aBehdad Esfahbod  inline Tag (const Tag &o) { *(ULONG*)this = (ULONG&) o; }
388807b8aa486753474e05e09f4fcca8ac94021b97cBehdad Esfahbod  inline Tag (uint32_t i) { (*(ULONG*)this).set (i); }
3894497af0069a94c69fc1518b1db2c1282721b732aBehdad Esfahbod  inline Tag (const char *c) { *(ULONG*)this = *(ULONG*)c; }
3904497af0069a94c69fc1518b1db2c1282721b732aBehdad Esfahbod  inline bool operator== (const char *c) const { return *(ULONG*)this == *(ULONG*)c; }
391befc022affd2386b3f46cd7d11e4262f6c8bce9fBehdad Esfahbod  /* What the char* converters return is NOT nul-terminated.  Print using "%.4s" */
3922b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod  inline operator const char* (void) const { return CONST_CHARP(this); }
3932b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod  inline operator char* (void) { return CHARP(this); }
394738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod
395738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
3963e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
397738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod    /* Note: Only accept ASCII-visible tags (mind DEL)
3989b39755d104603d1163738f77637cc1923d4055bBehdad Esfahbod     * This is one of the few places (only place?) that we check
3999b39755d104603d1163738f77637cc1923d4055bBehdad Esfahbod     * for data integrity, as opposed to just boundary checks.
400738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod     */
401738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod    return SANITIZE_SELF () && (((uint32_t) *this) & 0x80808080) == 0;
402738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod  }
4036b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
404303fe62824d4e99df554b6bfaacba05d068522fbBehdad EsfahbodASSERT_SIZE (Tag, 4);
405da1097bc3b1995776c205707fd2b17603b804646Behdad Esfahbod#define _NULL_TAG_INIT  {' ', ' ', ' ', ' '}
406da1097bc3b1995776c205707fd2b17603b804646Behdad EsfahbodDEFINE_NULL_DATA (Tag, 4, _NULL_TAG_INIT);
407da1097bc3b1995776c205707fd2b17603b804646Behdad Esfahbod#undef _NULL_TAG_INIT
4086b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4096b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* Glyph index number, same as uint16 (length = 16 bits) */
4106ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT GlyphID;
4116b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4121f437e6f47fb6c15761021bd2078f31778f2179cBehdad Esfahbod/* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
4136ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT Offset;
4146ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4156ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
4166ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef ULONG LongOffset;
4176ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4186b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4196b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* CheckSum */
42060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct CheckSum : ULONG
42160d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
42260d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
42360d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
4246b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    uint32_t Sum = 0L;
4254b8487d83e0c10076a6c573cb3487790ce366607Behdad Esfahbod    ULONG *EndPtr = Table+((Length+3) & ~3) / ULONG::get_size ();
4266b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4276b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    while (Table < EndPtr)
4286b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod      Sum += *Table++;
4296b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    return Sum;
4306b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod  }
4316b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
4328b8358033184198ff638ee1379093717596e162dBehdad EsfahbodASSERT_SIZE (CheckSum, 4);
4336b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4346b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4356b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/*
4366b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod * Version Numbers
4376b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod */
4386b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
43987fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbodstruct FixedVersion
44060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
44109c292e3b688a67fbae67b645d1e6ffcf8d8eb6eBehdad Esfahbod  inline operator uint32_t (void) const { return (major << 16) + minor; }
44296908b898476ca5d7da5f386b15be76f9e83d76eBehdad Esfahbod
443cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
4443e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
445cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod    return SANITIZE_SELF ();
446cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod  }
447cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod
4486ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod  USHORT major;
44987fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbod  USHORT minor;
4506b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
45187fcdcbe3644da10154688765db2d62eb9ac079aBehdad EsfahbodASSERT_SIZE (FixedVersion, 4);
4526b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
45392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
45492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
4555f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
45692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Template subclasses of Offset and LongOffset that do the dereferencing.
45792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Use: (this+memberName)
4585f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
4595f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
46092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename OffsetType, typename Type>
46192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericOffsetTo : OffsetType
46292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod{
46392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  inline const Type& operator() (const void *base) const
46492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  {
46592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
46692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    if (HB_UNLIKELY (!offset)) return Null(Type);
46795639fccc1ef18eadeb737e8b611d1d1f1315fc2Behdad Esfahbod    return CONST_CAST(Type, *CONST_CHARP(base), offset);
46892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
46992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
47020b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
4713e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
47295528131b5ab9fc9e265ace715832135ebd457a4Behdad Esfahbod    if (!SANITIZE_SELF ()) return false;
47392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
47492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    if (HB_UNLIKELY (!offset)) return true;
475ac26e2a838d1266bb5f39c43245eb2a52c5e072dBehdad Esfahbod    return SANITIZE (CAST(Type, *DECONST_CHARP(base), offset)) || NEUTER (DECONST_CAST(OffsetType,*this,0), 0);
47692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
47720b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base, const void *base2) {
4783e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
47995528131b5ab9fc9e265ace715832135ebd457a4Behdad Esfahbod    if (!SANITIZE_SELF ()) return false;
480b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    unsigned int offset = *this;
481b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    if (HB_UNLIKELY (!offset)) return true;
482b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    return SANITIZE_BASE (CAST(Type, *DECONST_CHARP(base), offset), base2) || NEUTER (DECONST_CAST(OffsetType,*this,0), 0);
483b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod  }
48420b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base, unsigned int user_data) {
4853e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
48695528131b5ab9fc9e265ace715832135ebd457a4Behdad Esfahbod    if (!SANITIZE_SELF ()) return false;
48742b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    unsigned int offset = *this;
48842b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    if (HB_UNLIKELY (!offset)) return true;
489ac26e2a838d1266bb5f39c43245eb2a52c5e072dBehdad Esfahbod    return SANITIZE_BASE (CAST(Type, *DECONST_CHARP(base), offset), user_data) || NEUTER (DECONST_CAST(OffsetType,*this,0), 0);
49042b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod  }
49192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod};
49292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Base, typename OffsetType, typename Type>
49392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodinline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
49492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
4955f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbodtemplate <typename Type>
49692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetTo : GenericOffsetTo<Offset, Type> {};
49792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
49892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
49992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
500bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
501bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
50292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/*
50392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Array Types
50492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod */
50592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
50692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename LenType, typename Type>
50792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericArrayOf
50860d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
509d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod  const Type *const_array(void) const { return CONST_ARRAY_AFTER (Type, len); }
510d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod  Type *array(void) { return ARRAY_AFTER (Type, len); }
511d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod
51248de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod  const Type *const_sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
51348de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod  {
51448de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    unsigned int count = len;
51548de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    if (HB_UNLIKELY (start_offset > count))
51648de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod      count = 0;
51748de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    else
51848de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod      count -= start_offset;
51948de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    count = MIN (count, *pcount);
52048de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    *pcount = count;
52148de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    return const_array() + start_offset;
52248de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod  }
52348de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod
52460d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
52560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
5265f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod    if (HB_UNLIKELY (i >= len)) return Null(Type);
527d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod    return const_array()[i];
5285f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  }
52960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline unsigned int get_size () const
5304b8487d83e0c10076a6c573cb3487790ce366607Behdad Esfahbod  { return len.get_size () + len * Type::get_size (); }
531e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod
53220b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
5333e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
534dc9c4d93cd0f3ac991f32df08c1c17fc389054c0Behdad Esfahbod    if (!SANITIZE_GET_SIZE()) return false;
5353564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    /* Note:
5363564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * for non-recursive types, this is not much needed.
5373564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * But we keep the code to make sure the objects pointed to
5383564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * do have a simple sanitize(). */
5393564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    return true;
54070de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    unsigned int count = len;
54170de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
542d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!SANITIZE (array()[i]))
54370de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod        return false;
5449bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
54570de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod  }
54620b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
5473e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
548dc9c4d93cd0f3ac991f32df08c1c17fc389054c0Behdad Esfahbod    if (!SANITIZE_GET_SIZE()) return false;
549e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod    unsigned int count = len;
550e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod    for (unsigned int i = 0; i < count; i++)
551d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!array()[i].sanitize (SANITIZE_ARG, base))
552e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod        return false;
5539bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
554e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod  }
55520b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base, const void *base2) {
5563e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
557b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    if (!SANITIZE_GET_SIZE()) return false;
558b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    unsigned int count = len;
559b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    for (unsigned int i = 0; i < count; i++)
560d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!array()[i].sanitize (SANITIZE_ARG, base, base2))
561b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod        return false;
5629bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
563b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod  }
56420b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base, unsigned int user_data) {
5653e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
56642b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    if (!SANITIZE_GET_SIZE()) return false;
56742b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    unsigned int count = len;
56842b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
569d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!array()[i].sanitize (SANITIZE_ARG, base, user_data))
57042b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod        return false;
5719bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
57242b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod  }
57370de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
57492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  LenType len;
575d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod/*Type array[VAR];*/
576e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod};
577e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod
57892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a USHORT number of elements. */
57992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
58092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct ArrayOf : GenericArrayOf<USHORT, Type> {};
58192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
58292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a ULONG number of elements. */
58392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
58492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongArrayOf : GenericArrayOf<ULONG, Type> {};
58592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
58692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of Offset's */
58792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
58892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
58992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
59092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of LongOffset's */
59192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
59292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
59392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
59492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* LongArray of LongOffset's */
59592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
59692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
59792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
59880e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod/* Array of offsets relative to the beginning of the array itself. */
59980e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodtemplate <typename Type>
60080e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodstruct OffsetListOf : OffsetArrayOf<Type>
60180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod{
60280e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
60380e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  {
60480e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod    if (HB_UNLIKELY (i >= this->len)) return Null(Type);
605d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod    return this+this->const_array()[i];
60680e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
60780e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
60880e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
6093e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
61080e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod    return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
61180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
61280e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, unsigned int user_data) {
6133e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
61480e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod    return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this), user_data);
61580e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
61680e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod};
61780e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
61880e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
619e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod/* An array with a USHORT number of elements,
620e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod * starting at second element. */
621e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbodtemplate <typename Type>
62260d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct HeadlessArrayOf
62360d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
624d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod  const Type *const_array(void) const { return CONST_ARRAY_AFTER (Type, len); }
625d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod  Type *array(void) { return ARRAY_AFTER (Type, len); }
626d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod
62760d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
62860d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
629e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod    if (HB_UNLIKELY (i >= len || !i)) return Null(Type);
630d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod    return const_array()[i-1];
631e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod  }
63260d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline unsigned int get_size () const
6334b8487d83e0c10076a6c573cb3487790ce366607Behdad Esfahbod  { return len.get_size () + (len ? len - 1 : 0) * Type::get_size (); }
6345f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
63520b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
6363e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
637dc9c4d93cd0f3ac991f32df08c1c17fc389054c0Behdad Esfahbod    if (!SANITIZE_GET_SIZE()) return false;
6383564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    /* Note:
6393564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * for non-recursive types, this is not much needed.
6403564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * But we keep the code to make sure the objects pointed to
6413564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * do have a simple sanitize(). */
6423564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    return true;
64315164d9258a74122a4db748d35532bd72c47cec2Behdad Esfahbod    unsigned int count = len ? len - 1 : 0;
644d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod    Type *a = array();
64570de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
646d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!SANITIZE (a[i]))
64770de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod        return false;
6489bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
64970de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod  }
65070de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
6515f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  USHORT len;
652d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod/*Type array[VAR];*/
6535f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod};
6545f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
6556b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
6561e91434569a9e9535ef021ca52b60b2e2af75d19Behdad Esfahbod#endif /* HB_OPEN_TYPE_PRIVATE_HH */
657