hb-open-type-private.hh revision 807b8aa486753474e05e09f4fcca8ac94021b97c
164aef3a54999496fd1de4f5aa5b019e4c03b3836Behdad Esfahbod/*
2ee58aaebd296ea8237516754fd4e825d524b11b7Behdad Esfahbod * Copyright (C) 2007,2008,2009  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
53d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod#define CONST_ARRAY_AFTER(T,X)	((reinterpret_cast<const T *>(CONST_CHARP(&(X)) + sizeof (X))))
54d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod#define ARRAY_AFTER(T,X)	((reinterpret_cast<T *>(CHARP(&(X)) + sizeof (X))))
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. */
64d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbodstatic const char NullPool[32] = "";
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
878b8358033184198ff638ee1379093717596e162dBehdad Esfahbod#define ASSERT_SIZE_DATA(Type, size, data) \
881f437e6f47fb6c15761021bd2078f31778f2179cBehdad Esfahbod  ASSERT_SIZE (Type, size); \
891f437e6f47fb6c15761021bd2078f31778f2179cBehdad Esfahbod  DEFINE_NULL_DATA (Type, size, data)
901f437e6f47fb6c15761021bd2078f31778f2179cBehdad Esfahbod
91600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod/* get_for_data() is a static class method returning a reference to an
92600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod * instance of Type located at the input data location.  It's just a
9354e5aac5e2947d4e2864c6f2987e4d275da73100Behdad Esfahbod * fancy, NULL-safe, cast! */
94fd92a3dde32fd10df30c9eeb97641bc3c15b1e9bBehdad Esfahbod#define STATIC_DEFINE_GET_FOR_DATA(Type) \
9560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  static inline const Type& get_for_data (const char *data) \
9660d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  { \
978b8358033184198ff638ee1379093717596e162dBehdad Esfahbod    if (HB_UNLIKELY (data == NULL)) return Null(Type); \
988b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod    return CONST_CAST (Type, *data, 0); \
99212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod  }
100212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod/* Like get_for_data(), but checks major version first. */
101cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod#define STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION(Type, MajorMin, MajorMax) \
102212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod  static inline const Type& get_for_data (const char *data) \
103212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod  { \
104212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod    if (HB_UNLIKELY (data == NULL)) return Null(Type); \
1058b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod    const Type& t = CONST_CAST (Type, *data, 0); \
106cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod    if (HB_UNLIKELY (t.version.major < MajorMin || t.version.major > MajorMax)) return Null(Type); \
107212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod    return t; \
108212aba6189d7aaac0bab169b77ae6bdab16800a5Behdad Esfahbod  }
109600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
110600e5eb80f553ea8eb862e6784133574c74ca513Behdad Esfahbod
111577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod/*
112577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod * Sanitize
113577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod */
114577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
11595e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#ifndef HB_DEBUG_SANITIZE
11695e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#define HB_DEBUG_SANITIZE HB_DEBUG
11795e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#endif
11895e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod
11995e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
120d0351314cd29fbdf0efb5c7f89a569648f7a7fc7Behdad Esfahbod#include <stdio.h>
1213e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG_DEF	, unsigned int sanitize_depth
1223e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG	, sanitize_depth + 1
1233e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG_INIT	, 1
1243e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE() \
125b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod	HB_STMT_START { \
12695e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod	    if (sanitize_depth < HB_DEBUG_SANITIZE) \
1277acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod		fprintf (stderr, "SANITIZE(%p) %-*d-> %s\n", \
1287acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod			 (CONST_CHARP (this) == NullPool) ? 0 : this, \
1299b76a290a94f2603f3cb9498ae976125347cf54bBehdad Esfahbod			 sanitize_depth, sanitize_depth, \
1307acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod			 __PRETTY_FUNCTION__); \
131b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod	} HB_STMT_END
132b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod#else
1333e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG_DEF
1343e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG
1353e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE_ARG_INIT
1363e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod#define TRACE_SANITIZE() HB_STMT_START {} HB_STMT_END
137b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod#endif
138b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod
13941895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod#define SANITIZE_ARG_DEF \
1403e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod	hb_sanitize_context_t *context TRACE_SANITIZE_ARG_DEF
14141895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod#define SANITIZE_ARG \
1423e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod	context TRACE_SANITIZE_ARG
14341895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod#define SANITIZE_ARG_INIT \
1443e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod	&context TRACE_SANITIZE_ARG_INIT
14541895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod
146577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodtypedef struct _hb_sanitize_context_t hb_sanitize_context_t;
147577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodstruct _hb_sanitize_context_t
148577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
149577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  const char *start, *end;
150577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  int edit_count;
151577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  hb_blob_t *blob;
152577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod};
153577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
154577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodstatic HB_GNUC_UNUSED void
1554e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod_hb_sanitize_init (hb_sanitize_context_t *context,
1564e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod		   hb_blob_t *blob)
157577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
158577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  context->blob = blob;
159577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  context->start = hb_blob_lock (blob);
160577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  context->end = context->start + hb_blob_get_length (blob);
161577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  context->edit_count = 0;
1624f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
16395e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
1647acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod  fprintf (stderr, "sanitize %p init [%p..%p] (%u bytes)\n",
1657f96b39a9d5a81ba77e0c3dea8fe2cdb9957c4c7Behdad Esfahbod	   context->blob, context->start, context->end, context->end - context->start);
1664f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
167577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod}
168577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
169577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbodstatic HB_GNUC_UNUSED void
1704e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod_hb_sanitize_fini (hb_sanitize_context_t *context,
1714e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod		   bool unlock)
172577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
17395e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
1747acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod  fprintf (stderr, "sanitize %p fini [%p..%p] %u edit requests\n",
1757acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod	   context->blob, context->start, context->end, context->edit_count);
1764f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
1774f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
178577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod  if (unlock)
179577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod    hb_blob_unlock (context->blob);
180577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod}
181577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
1824f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbodstatic HB_GNUC_UNUSED inline bool
18341895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod_hb_sanitize_check (SANITIZE_ARG_DEF,
18441895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod		    const char *base,
18541895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod		    unsigned int len)
18641895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod{
187ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod  bool ret = context->start <= base &&
188ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     base <= context->end &&
189ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     (unsigned int) (context->end - base) >= len;
190ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod
19195e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
19295e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod  if (sanitize_depth < HB_DEBUG_SANITIZE) \
193ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod    fprintf (stderr, "SANITIZE(%p) %-*d-> check [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
194ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     base,
195ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     sanitize_depth, sanitize_depth,
196ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     base, base+len, len,
197ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     context->start, context->end,
198ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	     ret ? "pass" : "FAIL");
199ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod#endif
200ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod  return ret;
20141895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod}
20241895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod
20341895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbodstatic HB_GNUC_UNUSED inline bool
204815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod_hb_sanitize_array (SANITIZE_ARG_DEF,
205815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod		    const char *base,
206815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod		    unsigned int record_size,
207815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod		    unsigned int len)
208815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod{
209815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod  bool overflows = len >= ((unsigned int) -1) / record_size;
210815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
21195e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
21295e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod  if (sanitize_depth < HB_DEBUG_SANITIZE) \
213815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod    fprintf (stderr, "SANITIZE(%p) %-*d-> array [%p..%p] (%d*%d=%ld bytes) in [%p..%p] -> %s\n", \
214815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     base,
215815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     sanitize_depth, sanitize_depth,
216815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     base, base + (record_size * len), record_size, len, (unsigned long) record_size * len,
217815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     context->start, context->end,
218815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod	     !overflows ? "does not overflow" : "OVERFLOWS FAIL");
219815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod#endif
220815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod  return HB_LIKELY (!overflows) && _hb_sanitize_check (SANITIZE_ARG, base, record_size * len);
221815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod}
222815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
223815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbodstatic HB_GNUC_UNUSED inline bool
22441895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod_hb_sanitize_edit (SANITIZE_ARG_DEF,
2254f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod		   const char *base HB_GNUC_UNUSED,
2264f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod		   unsigned int len HB_GNUC_UNUSED)
227577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod{
228977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod  bool perm = hb_blob_try_writable_inplace (context->blob);
229f4b58d3fc2956a9d1b6178588d809c781f7a5c0cBehdad Esfahbod  context->edit_count++;
2304f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
23195e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
232ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod  fprintf (stderr, "SANITIZE(%p) %-*d-> edit(%u) [%p..%p] (%d bytes) in [%p..%p] -> %s\n", \
233ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   base,
234ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   sanitize_depth, sanitize_depth,
235ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   context->edit_count,
236ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   base, base+len, len,
237ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   context->start, context->end,
238ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	   perm ? "granted" : "REJECTED");
2394f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
2404f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod  return perm;
241577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod}
242577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
243577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG))
244577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE2(X,Y) (SANITIZE (X) && SANITIZE (Y))
245577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
246577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_THIS(X) HB_LIKELY ((X).sanitize (SANITIZE_ARG, CONST_CHARP(this)))
247577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_THIS2(X,Y) (SANITIZE_THIS (X) && SANITIZE_THIS (Y))
248577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_THIS3(X,Y,Z) (SANITIZE_THIS (X) && SANITIZE_THIS (Y) && SANITIZE_THIS(Z))
249577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
250577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_BASE(X,B) HB_LIKELY ((X).sanitize (SANITIZE_ARG, B))
251577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_BASE2(X,Y,B) (SANITIZE_BASE (X,B) && SANITIZE_BASE (Y,B))
252577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
253577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_SELF() SANITIZE_OBJ (*this)
254577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_OBJ(X) SANITIZE_MEM(&(X), sizeof (X))
255577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod#define SANITIZE_GET_SIZE() SANITIZE_SELF() && SANITIZE_MEM (this, this->get_size ())
256577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
257d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod/* TODO Optimize this if L is fixed (gcc magic) */
25841895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod#define SANITIZE_MEM(B,L) HB_LIKELY (_hb_sanitize_check (SANITIZE_ARG, CONST_CHARP(B), (L)))
259577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
260815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod#define SANITIZE_ARRAY(A,S,L) HB_LIKELY (_hb_sanitize_array (SANITIZE_ARG, CONST_CHARP(A), S, L))
261815a73e4202ca17677f12e862b70ca8724cf2f57Behdad Esfahbod
262d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod#define NEUTER(Var, Val) \
263d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod	(SANITIZE_OBJ (Var) && \
26441895506cb6a41b1a833866f8822261ea449ea0bBehdad Esfahbod	 _hb_sanitize_edit (SANITIZE_ARG, CONST_CHARP(&(Var)), sizeof (Var)) && \
265807b8aa486753474e05e09f4fcca8ac94021b97cBehdad Esfahbod	 ((Var).set (Val), true))
266577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
267577c1116493d785d3455626612f97dabb383abf0Behdad Esfahbod
2684e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod/* Template to sanitize an object. */
2694e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbodtemplate <typename Type>
2704e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbodstruct Sanitizer
2714e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod{
2724e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  static hb_blob_t *sanitize (hb_blob_t *blob) {
2734e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    hb_sanitize_context_t context;
2744e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    bool sane;
2754e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
276d0b657379bbe63602953412d6bc944b2a0f430ebBehdad Esfahbod    /* TODO is_sane() stuff */
2774e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
2784e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  retry:
27995e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
2807acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod    fprintf (stderr, "Sanitizer %p start %s\n", blob, __PRETTY_FUNCTION__);
2814f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
2824f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod
2834e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    _hb_sanitize_init (&context, blob);
2844e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
2858cd6fa28d1b77100491519b8dedb2e113508bf59Behdad Esfahbod    Type *t = &CAST (Type, *DECONST_CHARP(context.start), 0);
2864e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
287b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod    sane = t->sanitize (SANITIZE_ARG_INIT);
2884e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane) {
2894e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      if (context.edit_count) {
29095e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
2910d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod	fprintf (stderr, "Sanitizer %p passed first round with %d edits; going a second round %s\n",
2920d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod		 blob, context.edit_count, __PRETTY_FUNCTION__);
2930d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod#endif
2948b5346130425c7c101f6ff2432874ba2fd372edcBehdad Esfahbod        /* sanitize again to ensure no toe-stepping */
2954e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod        context.edit_count = 0;
296b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod	sane = t->sanitize (SANITIZE_ARG_INIT);
2974e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	if (context.edit_count) {
29895e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
299ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod	  fprintf (stderr, "Sanitizer %p requested %d edits in second round; FAILLING %s\n",
3000d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod		   blob, context.edit_count, __PRETTY_FUNCTION__);
3010d77ab8a73f57c9fca4c6f9301dae394d79526e3Behdad Esfahbod#endif
3024e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	  sane = false;
3034e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod	}
3044e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
3054e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      _hb_sanitize_fini (&context, true);
3064e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    } else {
3074f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod      unsigned int edit_count = context.edit_count;
3084e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      _hb_sanitize_fini (&context, true);
309977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod      if (edit_count && !hb_blob_is_writable (blob) && hb_blob_try_writable (blob)) {
310977eeb714454630bd045bb11f58ff6397f10b143Behdad Esfahbod        /* ok, we made it writable by relocating.  try again */
31195e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
3127acb389569cf99c6bae9db31a8ed7c7007fbb566Behdad Esfahbod	fprintf (stderr, "Sanitizer %p retry %s\n", blob, __PRETTY_FUNCTION__);
3134f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
3144e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod        goto retry;
3154e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      }
3164e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3174e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
31895e202403ffa543c817f45cca21fbc116eb8e807Behdad Esfahbod#if HB_DEBUG_SANITIZE
319ae728e51e94d18d731b7c8dc524da1a4f427d63bBehdad Esfahbod    fprintf (stderr, "Sanitizer %p %s %s\n", blob, sane ? "passed" : "FAILED", __PRETTY_FUNCTION__);
3204f3ad9115a4161fc23fa559c26082440196217ecBehdad Esfahbod#endif
3214e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    if (sane)
3224e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return blob;
3234e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    else {
3244e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      hb_blob_destroy (blob);
3254e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod      return hb_blob_create_empty ();
3264e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    }
3274e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  }
3284e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
329d60bb8ca2ae6edf29b2227b56c57f0d16879370bBehdad Esfahbod  static const Type& lock_instance (hb_blob_t *blob) {
3304e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod    return Type::get_for_data (hb_blob_lock (blob));
3314e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod  }
3324e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod};
3334e8a0602bb0b3cbf7f26cc38790e37cdec7b0b37Behdad Esfahbod
3342d15e72c75931398db5e027e660f1320bb979117Behdad Esfahbod
335b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/*
336b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *
337bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod * The OpenType Font File: Data Types
338b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod */
339b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
340b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
341b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod/* "The following data types are used in the OpenType font file.
342b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod *  All OpenType fonts use Motorola-style byte ordering (Big Endian):" */
343b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
3445f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
3455f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod * Int types
3465f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
3475f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
3489e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod/* TODO On machines that allow unaligned access, use this version. */
3499e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod#define _DEFINE_INT_TYPE1_UNALIGNED(NAME, TYPE, BIG_ENDIAN, BYTES) \
35060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  struct NAME \
35160d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  { \
352807b8aa486753474e05e09f4fcca8ac94021b97cBehdad Esfahbod    inline NAME& set (TYPE i) { (TYPE&) v = BIG_ENDIAN (i); return *this; } \
35320cc86b3592db33731de671f008d7d222776be49Behdad Esfahbod    inline operator TYPE(void) const { return BIG_ENDIAN ((TYPE&) v); } \
35420cc86b3592db33731de671f008d7d222776be49Behdad Esfahbod    inline bool operator== (NAME o) const { return (TYPE&) v == (TYPE&) o.v; } \
355b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod    inline bool sanitize (SANITIZE_ARG_DEF) { \
3563e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod      TRACE_SANITIZE (); \
357b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod      return SANITIZE_SELF (); \
358b28815c1f6e46d38471cacbc31248ca6fda8c4d1Behdad Esfahbod    } \
3599e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod    private: unsigned char v[BYTES]; \
3609e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod  }; \
3619e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod  ASSERT_SIZE (NAME, BYTES)
3629e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod
3639e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod#define DEFINE_INT_TYPE1(NAME, TYPE, BIG_ENDIAN, BYTES) \
3649e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod  struct NAME \
3659e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod  { \
366807b8aa486753474e05e09f4fcca8ac94021b97cBehdad Esfahbod    inline NAME& set (TYPE i) { BIG_ENDIAN##_put_unaligned(v, i); return *this; } \
3679e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod    inline operator TYPE(void) const { return BIG_ENDIAN##_get_unaligned (v); } \
3689e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod    inline bool operator== (NAME o) const { return BIG_ENDIAN##_cmp_unaligned (v, o.v); } \
3699e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod    inline bool sanitize (SANITIZE_ARG_DEF) { \
3703e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod      TRACE_SANITIZE (); \
3719e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod      return SANITIZE_SELF (); \
3729e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod    } \
3739e826ea2832f0444bcef9075b445d481a58a09c2Behdad Esfahbod    private: unsigned char v[BYTES]; \
3745f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  }; \
37520cc86b3592db33731de671f008d7d222776be49Behdad Esfahbod  ASSERT_SIZE (NAME, BYTES)
376df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define DEFINE_INT_TYPE0(NAME, type, b)	DEFINE_INT_TYPE1 (NAME, type##_t, hb_be_##type, b)
377df66028781a7609a515980e64396e6f1044d764aBehdad Esfahbod#define DEFINE_INT_TYPE(NAME, u, w)	DEFINE_INT_TYPE0 (NAME, u##int##w, (w / 8))
3785f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
379b6e62bc5db76ae342177b2b646c37f45eccad975Behdad Esfahbod
38020cc86b3592db33731de671f008d7d222776be49Behdad EsfahbodDEFINE_INT_TYPE (USHORT,  u, 16);	/* 16-bit unsigned integer. */
38120cc86b3592db33731de671f008d7d222776be49Behdad EsfahbodDEFINE_INT_TYPE (SHORT,	  , 16);	/* 16-bit signed integer. */
38220cc86b3592db33731de671f008d7d222776be49Behdad EsfahbodDEFINE_INT_TYPE (ULONG,	 u, 32);	/* 32-bit unsigned integer. */
38320cc86b3592db33731de671f008d7d222776be49Behdad EsfahbodDEFINE_INT_TYPE (LONG,	  , 32);	/* 32-bit signed integer. */
3846b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
385bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
3866b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* Array of four uint8s (length = 32 bits) used to identify a script, language
3876b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod * system, feature, or baseline */
38820cc86b3592db33731de671f008d7d222776be49Behdad Esfahbodstruct Tag : ULONG
38960d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
3904497af0069a94c69fc1518b1db2c1282721b732aBehdad Esfahbod  inline Tag (const Tag &o) { *(ULONG*)this = (ULONG&) o; }
391807b8aa486753474e05e09f4fcca8ac94021b97cBehdad Esfahbod  inline Tag (uint32_t i) { (*(ULONG*)this).set (i); }
3924497af0069a94c69fc1518b1db2c1282721b732aBehdad Esfahbod  inline Tag (const char *c) { *(ULONG*)this = *(ULONG*)c; }
3934497af0069a94c69fc1518b1db2c1282721b732aBehdad Esfahbod  inline bool operator== (const char *c) const { return *(ULONG*)this == *(ULONG*)c; }
394befc022affd2386b3f46cd7d11e4262f6c8bce9fBehdad Esfahbod  /* What the char* converters return is NOT nul-terminated.  Print using "%.4s" */
3952b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod  inline operator const char* (void) const { return CONST_CHARP(this); }
3962b5a59c277f4c5bf7aac9a9005054763e322e02dBehdad Esfahbod  inline operator char* (void) { return CHARP(this); }
397738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod
398738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
3993e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
400738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod    /* Note: Only accept ASCII-visible tags (mind DEL)
401738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod     * This is one of the few times (only time?) we check
402738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod     * for data integrity, as opposed o just boundary checks
403738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod     */
404738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod    return SANITIZE_SELF () && (((uint32_t) *this) & 0x80808080) == 0;
405738c54d9caa3affc4b434e56bfb810ff6dc9b0b3Behdad Esfahbod  }
4066b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
407303fe62824d4e99df554b6bfaacba05d068522fbBehdad EsfahbodASSERT_SIZE (Tag, 4);
408da1097bc3b1995776c205707fd2b17603b804646Behdad Esfahbod#define _NULL_TAG_INIT  {' ', ' ', ' ', ' '}
409da1097bc3b1995776c205707fd2b17603b804646Behdad EsfahbodDEFINE_NULL_DATA (Tag, 4, _NULL_TAG_INIT);
410da1097bc3b1995776c205707fd2b17603b804646Behdad Esfahbod#undef _NULL_TAG_INIT
4116b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4126b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* Glyph index number, same as uint16 (length = 16 bits) */
4136ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT GlyphID;
4146b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4151f437e6f47fb6c15761021bd2078f31778f2179cBehdad Esfahbod/* Offset to a table, same as uint16 (length = 16 bits), Null offset = 0x0000 */
4166ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef USHORT Offset;
4176ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4186ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod/* LongOffset to a table, same as uint32 (length = 32 bits), Null offset = 0x00000000 */
4196ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbodtypedef ULONG LongOffset;
4206ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod
4216b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4226b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/* CheckSum */
42360d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct CheckSum : ULONG
42460d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
42560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  static uint32_t CalcTableChecksum (ULONG *Table, uint32_t Length)
42660d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
4276b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    uint32_t Sum = 0L;
42801e4fcb032be601f272e62228881e2aabfb9d925Behdad Esfahbod    ULONG *EndPtr = Table+((Length+3) & ~3) / sizeof(ULONG);
4296b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4306b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    while (Table < EndPtr)
4316b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod      Sum += *Table++;
4326b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod    return Sum;
4336b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod  }
4346b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
4358b8358033184198ff638ee1379093717596e162dBehdad EsfahbodASSERT_SIZE (CheckSum, 4);
4366b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4376b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
4386b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod/*
4396b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod * Version Numbers
4406b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod */
4416b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
44287fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbodstruct FixedVersion
44360d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
44409c292e3b688a67fbae67b645d1e6ffcf8d8eb6eBehdad Esfahbod  inline operator uint32_t (void) const { return (major << 16) + minor; }
44596908b898476ca5d7da5f386b15be76f9e83d76eBehdad Esfahbod
446cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
4473e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
448cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod    return SANITIZE_SELF ();
449cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod  }
450cd3827ee567612c5500206b62840702fc956e0f5Behdad Esfahbod
4516ad8d5f3c7028147b371137ae4bca6aae66b3489Behdad Esfahbod  USHORT major;
45287fcdcbe3644da10154688765db2d62eb9ac079aBehdad Esfahbod  USHORT minor;
4536b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod};
45487fcdcbe3644da10154688765db2d62eb9ac079aBehdad EsfahbodASSERT_SIZE (FixedVersion, 4);
4556b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
45692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
45792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
4585f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod/*
45992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Template subclasses of Offset and LongOffset that do the dereferencing.
46092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Use: (this+memberName)
4615f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod */
4625f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
46392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename OffsetType, typename Type>
46492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericOffsetTo : OffsetType
46592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod{
46692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  inline const Type& operator() (const void *base) const
46792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  {
46892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
46992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    if (HB_UNLIKELY (!offset)) return Null(Type);
47095639fccc1ef18eadeb737e8b611d1d1f1315fc2Behdad Esfahbod    return CONST_CAST(Type, *CONST_CHARP(base), offset);
47192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
47292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
47320b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
4743e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
47595528131b5ab9fc9e265ace715832135ebd457a4Behdad Esfahbod    if (!SANITIZE_SELF ()) return false;
47692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    unsigned int offset = *this;
47792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod    if (HB_UNLIKELY (!offset)) return true;
478ac26e2a838d1266bb5f39c43245eb2a52c5e072dBehdad Esfahbod    return SANITIZE (CAST(Type, *DECONST_CHARP(base), offset)) || NEUTER (DECONST_CAST(OffsetType,*this,0), 0);
47992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  }
48020b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base, const void *base2) {
4813e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
48295528131b5ab9fc9e265ace715832135ebd457a4Behdad Esfahbod    if (!SANITIZE_SELF ()) return false;
483b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    unsigned int offset = *this;
484b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    if (HB_UNLIKELY (!offset)) return true;
485b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    return SANITIZE_BASE (CAST(Type, *DECONST_CHARP(base), offset), base2) || NEUTER (DECONST_CAST(OffsetType,*this,0), 0);
486b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod  }
48720b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base, unsigned int user_data) {
4883e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
48995528131b5ab9fc9e265ace715832135ebd457a4Behdad Esfahbod    if (!SANITIZE_SELF ()) return false;
49042b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    unsigned int offset = *this;
49142b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    if (HB_UNLIKELY (!offset)) return true;
492ac26e2a838d1266bb5f39c43245eb2a52c5e072dBehdad Esfahbod    return SANITIZE_BASE (CAST(Type, *DECONST_CHARP(base), offset), user_data) || NEUTER (DECONST_CAST(OffsetType,*this,0), 0);
49342b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod  }
49492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod};
49592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Base, typename OffsetType, typename Type>
49692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodinline const Type& operator + (const Base &base, GenericOffsetTo<OffsetType, Type> offset) { return offset (base); }
49792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
4985f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbodtemplate <typename Type>
49992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetTo : GenericOffsetTo<Offset, Type> {};
50092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
50192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
50292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetTo : GenericOffsetTo<LongOffset, Type> {};
503bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
504bff3c0fde5da04a70d1f7aeeb9fa2a75fe5c07f6Behdad Esfahbod
50592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/*
50692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod * Array Types
50792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod */
50892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
50992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename LenType, typename Type>
51092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct GenericArrayOf
51160d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
512d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod  const Type *const_array(void) const { return CONST_ARRAY_AFTER (Type, len); }
513d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod  Type *array(void) { return ARRAY_AFTER (Type, len); }
514d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod
51548de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod  const Type *const_sub_array (unsigned int start_offset, unsigned int *pcount /* IN/OUT */) const
51648de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod  {
51748de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    unsigned int count = len;
51848de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    if (HB_UNLIKELY (start_offset > count))
51948de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod      count = 0;
52048de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    else
52148de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod      count -= start_offset;
52248de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    count = MIN (count, *pcount);
52348de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    *pcount = count;
52448de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod    return const_array() + start_offset;
52548de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod  }
52648de3730cdf91b9f6473509e22260a902ccec992Behdad Esfahbod
52760d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
52860d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
5295f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod    if (HB_UNLIKELY (i >= len)) return Null(Type);
530d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod    return const_array()[i];
5315f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  }
53260d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline unsigned int get_size () const
533d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod  { return sizeof (len) + len * sizeof (Type); }
534e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod
53520b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
5363e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
537dc9c4d93cd0f3ac991f32df08c1c17fc389054c0Behdad Esfahbod    if (!SANITIZE_GET_SIZE()) return false;
5383564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    /* Note:
5393564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * for non-recursive types, this is not much needed.
5403564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * But we keep the code to make sure the objects pointed to
5413564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * do have a simple sanitize(). */
5423564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    return true;
54370de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    unsigned int count = len;
54470de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
545d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!SANITIZE (array()[i]))
54670de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod        return false;
5479bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
54870de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod  }
54920b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base) {
5503e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
551dc9c4d93cd0f3ac991f32df08c1c17fc389054c0Behdad Esfahbod    if (!SANITIZE_GET_SIZE()) return false;
552e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod    unsigned int count = len;
553e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod    for (unsigned int i = 0; i < count; i++)
554d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!array()[i].sanitize (SANITIZE_ARG, base))
555e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod        return false;
5569bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
557e6ab2c59ba2d37942ac5fcbfe61d38b7e359ac8cBehdad Esfahbod  }
55820b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base, const void *base2) {
5593e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
560b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    if (!SANITIZE_GET_SIZE()) return false;
561b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    unsigned int count = len;
562b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod    for (unsigned int i = 0; i < count; i++)
563d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!array()[i].sanitize (SANITIZE_ARG, base, base2))
564b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod        return false;
5659bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
566b508e5ccd528f3f0f49f545bd5f30a525d5abd5aBehdad Esfahbod  }
56720b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, const void *base, unsigned int user_data) {
5683e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
56942b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    if (!SANITIZE_GET_SIZE()) return false;
57042b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    unsigned int count = len;
57142b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
572d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!array()[i].sanitize (SANITIZE_ARG, base, user_data))
57342b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod        return false;
5749bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
57542b778f89e0818fe06910ce04e2203485823da09Behdad Esfahbod  }
57670de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
57792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod  LenType len;
578d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod/*Type array[VAR];*/
579e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod};
580e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod
58192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a USHORT number of elements. */
58292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
58392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct ArrayOf : GenericArrayOf<USHORT, Type> {};
58492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
58592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* An array with a ULONG number of elements. */
58692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
58792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongArrayOf : GenericArrayOf<ULONG, Type> {};
58892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
58992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of Offset's */
59092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
59192b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct OffsetArrayOf : ArrayOf<OffsetTo<Type> > {};
59292b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
59392b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* Array of LongOffset's */
59492b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
59592b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetArrayOf : ArrayOf<LongOffsetTo<Type> > {};
59692b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
59792b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod/* LongArray of LongOffset's */
59892b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodtemplate <typename Type>
59992b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbodstruct LongOffsetLongArrayOf : LongArrayOf<LongOffsetTo<Type> > {};
60092b5dd8e71e1bdeaa6e86a53f167683a3f5f4289Behdad Esfahbod
60180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod/* Array of offsets relative to the beginning of the array itself. */
60280e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodtemplate <typename Type>
60380e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbodstruct OffsetListOf : OffsetArrayOf<Type>
60480e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod{
60580e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
60680e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  {
60780e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod    if (HB_UNLIKELY (i >= this->len)) return Null(Type);
608d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod    return this+this->const_array()[i];
60980e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
61080e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
61180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
6123e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
61380e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod    return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this));
61480e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
61580e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF, unsigned int user_data) {
6163e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
61780e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod    return OffsetArrayOf<Type>::sanitize (SANITIZE_ARG, CONST_CHARP(this), user_data);
61880e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod  }
61980e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod};
62080e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
62180e2aa2e1bfa2c8ecedcfa4cce8cadeb15792ac3Behdad Esfahbod
622e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod/* An array with a USHORT number of elements,
623e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod * starting at second element. */
624e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbodtemplate <typename Type>
62560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbodstruct HeadlessArrayOf
62660d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod{
627d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod  const Type *const_array(void) const { return CONST_ARRAY_AFTER (Type, len); }
628d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod  Type *array(void) { return ARRAY_AFTER (Type, len); }
629d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod
63060d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline const Type& operator [] (unsigned int i) const
63160d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  {
632e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod    if (HB_UNLIKELY (i >= len || !i)) return Null(Type);
633d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod    return const_array()[i-1];
634e8cbaaf6d538036ff9b880b018db402e0895ed01Behdad Esfahbod  }
63560d77cf05fddc5304b4b1fc19cdedba15cbee1e9Behdad Esfahbod  inline unsigned int get_size () const
636d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod  { return sizeof (len) + (len ? len - 1 : 0) * sizeof (Type); }
6375f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
63820b035dad41247076815a2bbb0346d63058b322fBehdad Esfahbod  inline bool sanitize (SANITIZE_ARG_DEF) {
6393e2401f6c53d2b047954d75c37aef5e5e7cdc51aBehdad Esfahbod    TRACE_SANITIZE ();
640dc9c4d93cd0f3ac991f32df08c1c17fc389054c0Behdad Esfahbod    if (!SANITIZE_GET_SIZE()) return false;
6413564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    /* Note:
6423564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * for non-recursive types, this is not much needed.
6433564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * But we keep the code to make sure the objects pointed to
6443564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod     * do have a simple sanitize(). */
6453564ee5216004d45d30b0ded61184cf8dde5dd89Behdad Esfahbod    return true;
64615164d9258a74122a4db748d35532bd72c47cec2Behdad Esfahbod    unsigned int count = len ? len - 1 : 0;
647d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod    Type *a = array();
64870de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
649d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod      if (!SANITIZE (a[i]))
65070de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod        return false;
6519bd629ccd064e739789e504c41ad875eed93abbaBehdad Esfahbod    return true;
65270de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod  }
65370de50c11ed7037b20eb6814ff60f6e32a9944e4Behdad Esfahbod
6545f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod  USHORT len;
655d3480ba37fbb5d4be75b094060f5b2f1ce98fb53Behdad Esfahbod/*Type array[VAR];*/
6565f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod};
6575f810363acc3ad3cba631a68620e3d37e54c95c4Behdad Esfahbod
6586b4ce01da121e12e1c78ad7eaedf469f35f3568dBehdad Esfahbod
6591e91434569a9e9535ef021ca52b60b2e2af75d19Behdad Esfahbod#endif /* HB_OPEN_TYPE_PRIVATE_HH */
660