blob: e80a752bddf9237c7bbbadcaba3c8c1da55bf7b0 [file] [log] [blame]
yusukes@chromium.orgd257d182009-11-04 04:56:32 +00001// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef OTS_H_
6#define OTS_H_
7
8#include <cstdarg>
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12
13#include "opentype-sanitiser.h"
14
15namespace ots {
16
17#if defined(_MSC_VER) || !defined(OTS_DEBUG)
18#define OTS_FAILURE() false
19#else
20#define OTS_FAILURE() ots::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
21bool Failure(const char *f, int l, const char *fn);
22#endif
23
24#if defined(_MSC_VER)
25// MSVC supports C99 style variadic macros.
26#define OTS_WARNING(format, ...)
27#else
28// GCC
29#if defined(OTS_DEBUG)
30#define OTS_WARNING(format, args...) \
31 ots::Warning(__FILE__, __LINE__, format, ##args)
32void Warning(const char *f, int l, const char *format, ...)
33 __attribute__((format(printf, 3, 4)));
34#else
35#define OTS_WARNING(format, args...)
36#endif
37#endif
38
39// Define OTS_NO_TRANSCODE_HINTS (i.e., g++ -DOTS_NO_TRANSCODE_HINTS) if you
40// want to omit TrueType hinting instructions and variables in glyf, fpgm, prep,
41// and cvt tables.
42#if defined(OTS_NO_TRANSCODE_HINTS)
43const bool g_transcode_hints = false;
44#else
45const bool g_transcode_hints = true;
46#endif
47
48// -----------------------------------------------------------------------------
49// Buffer helper class
50//
51// This class perform some trival buffer operations while checking for
52// out-of-bounds errors. As a family they return false if anything is amiss,
53// updating the current offset otherwise.
54// -----------------------------------------------------------------------------
55class Buffer {
56 public:
57 Buffer(const uint8_t *buffer, size_t len)
58 : buffer_(buffer),
59 length_(len),
60 offset_(0) { }
61
62 bool Skip(size_t n_bytes) {
63 return Read(NULL, n_bytes);
64 }
65
66 bool Read(uint8_t *buffer, size_t n_bytes) {
67 if (n_bytes > 1024 * 1024 * 1024) {
68 return OTS_FAILURE();
69 }
70 if ((offset_ + n_bytes > length_) ||
71 (offset_ > length_ - n_bytes)) {
72 return OTS_FAILURE();
73 }
74 if (buffer) {
75 std::memcpy(buffer, buffer_ + offset_, n_bytes);
76 }
77 offset_ += n_bytes;
78 return true;
79 }
80
81 inline bool ReadU8(uint8_t *value) {
82 if (offset_ + 1 > length_) {
83 return OTS_FAILURE();
84 }
85 *value = buffer_[offset_];
86 ++offset_;
87 return true;
88 }
89
90 bool ReadU16(uint16_t *value) {
91 if (offset_ + 2 > length_) {
92 return OTS_FAILURE();
93 }
94 std::memcpy(value, buffer_ + offset_, sizeof(uint16_t));
95 *value = ntohs(*value);
96 offset_ += 2;
97 return true;
98 }
99
100 bool ReadS16(int16_t *value) {
101 return ReadU16(reinterpret_cast<uint16_t*>(value));
102 }
103
104 bool ReadU32(uint32_t *value) {
105 if (offset_ + 4 > length_) {
106 return OTS_FAILURE();
107 }
108 std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
109 *value = ntohl(*value);
110 offset_ += 4;
111 return true;
112 }
113
114 bool ReadS32(int32_t *value) {
115 return ReadU32(reinterpret_cast<uint32_t*>(value));
116 }
117
118 bool ReadTag(uint32_t *value) {
119 if (offset_ + 4 > length_) {
120 return OTS_FAILURE();
121 }
122 std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
123 offset_ += 4;
124 return true;
125 }
126
127 bool ReadR64(uint64_t *value) {
128 if (offset_ + 8 > length_) {
129 return OTS_FAILURE();
130 }
131 std::memcpy(value, buffer_ + offset_, sizeof(uint64_t));
132 offset_ += 8;
133 return true;
134 }
135
136 size_t offset() const { return offset_; }
137 size_t length() const { return length_; }
138
139 void set_offset(size_t newoffset) { offset_ = newoffset; }
140
141 private:
142 const uint8_t * const buffer_;
143 const size_t length_;
144 size_t offset_;
145};
146
147#define FOR_EACH_TABLE_TYPE \
148 F(cff, CFF) \
149 F(cmap, CMAP) \
150 F(cvt, CVT) \
151 F(fpgm, FPGM) \
152 F(gasp, GASP) \
153 F(glyf, GLYF) \
154 F(hdmx, HDMX) \
155 F(head, HEAD) \
156 F(hhea, HHEA) \
157 F(hmtx, HMTX) \
yusukes@chromium.orgf12575f2010-02-02 21:26:17 +0000158 F(kern, KERN) \
yusukes@chromium.orgd257d182009-11-04 04:56:32 +0000159 F(loca, LOCA) \
160 F(ltsh, LTSH) \
161 F(maxp, MAXP) \
162 F(name, NAME) \
163 F(os2, OS2) \
164 F(post, POST) \
165 F(prep, PREP) \
166 F(vdmx, VDMX) \
167 F(vorg, VORG)
168
169#define F(name, capname) struct OpenType##capname;
170FOR_EACH_TABLE_TYPE
171#undef F
172
173struct OpenTypeFile {
174 OpenTypeFile() {
175#define F(name, capname) name = NULL;
176 FOR_EACH_TABLE_TYPE
177#undef F
178 }
179
180 uint32_t version;
181 uint16_t num_tables;
182 uint16_t search_range;
183 uint16_t entry_selector;
184 uint16_t range_shift;
185
186#define F(name, capname) OpenType##capname *name;
187FOR_EACH_TABLE_TYPE
188#undef F
189};
190
191} // namespace ots
192
193#endif // OTS_H_