yusukes@chromium.org | d257d18 | 2009-11-04 04:56:32 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | namespace 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__) |
| 21 | bool 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) |
| 32 | void 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) |
| 43 | const bool g_transcode_hints = false; |
| 44 | #else |
| 45 | const 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 | // ----------------------------------------------------------------------------- |
| 55 | class 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 | |
yusukes@chromium.org | 6263d06 | 2010-08-06 03:27:28 +0000 | [diff] [blame^] | 136 | const uint8_t *buffer() const { return buffer_; } |
yusukes@chromium.org | d257d18 | 2009-11-04 04:56:32 +0000 | [diff] [blame] | 137 | size_t offset() const { return offset_; } |
| 138 | size_t length() const { return length_; } |
| 139 | |
| 140 | void set_offset(size_t newoffset) { offset_ = newoffset; } |
| 141 | |
| 142 | private: |
| 143 | const uint8_t * const buffer_; |
| 144 | const size_t length_; |
| 145 | size_t offset_; |
| 146 | }; |
| 147 | |
| 148 | #define FOR_EACH_TABLE_TYPE \ |
| 149 | F(cff, CFF) \ |
| 150 | F(cmap, CMAP) \ |
| 151 | F(cvt, CVT) \ |
| 152 | F(fpgm, FPGM) \ |
| 153 | F(gasp, GASP) \ |
| 154 | F(glyf, GLYF) \ |
| 155 | F(hdmx, HDMX) \ |
| 156 | F(head, HEAD) \ |
| 157 | F(hhea, HHEA) \ |
| 158 | F(hmtx, HMTX) \ |
yusukes@chromium.org | f12575f | 2010-02-02 21:26:17 +0000 | [diff] [blame] | 159 | F(kern, KERN) \ |
yusukes@chromium.org | d257d18 | 2009-11-04 04:56:32 +0000 | [diff] [blame] | 160 | F(loca, LOCA) \ |
| 161 | F(ltsh, LTSH) \ |
| 162 | F(maxp, MAXP) \ |
| 163 | F(name, NAME) \ |
| 164 | F(os2, OS2) \ |
| 165 | F(post, POST) \ |
| 166 | F(prep, PREP) \ |
| 167 | F(vdmx, VDMX) \ |
| 168 | F(vorg, VORG) |
| 169 | |
| 170 | #define F(name, capname) struct OpenType##capname; |
| 171 | FOR_EACH_TABLE_TYPE |
| 172 | #undef F |
| 173 | |
| 174 | struct OpenTypeFile { |
| 175 | OpenTypeFile() { |
| 176 | #define F(name, capname) name = NULL; |
| 177 | FOR_EACH_TABLE_TYPE |
| 178 | #undef F |
| 179 | } |
| 180 | |
| 181 | uint32_t version; |
| 182 | uint16_t num_tables; |
| 183 | uint16_t search_range; |
| 184 | uint16_t entry_selector; |
| 185 | uint16_t range_shift; |
| 186 | |
| 187 | #define F(name, capname) OpenType##capname *name; |
| 188 | FOR_EACH_TABLE_TYPE |
| 189 | #undef F |
| 190 | }; |
| 191 | |
| 192 | } // namespace ots |
| 193 | |
| 194 | #endif // OTS_H_ |