Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / third_party / ots / include / opentype-sanitiser.h
blob285a438979ca704e712625e9225378c52f02c58d
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.
5 #ifndef OPENTYPE_SANITISER_H_
6 #define OPENTYPE_SANITISER_H_
8 #if defined(_WIN32)
9 #include <stdlib.h>
10 typedef signed char int8_t;
11 typedef unsigned char uint8_t;
12 typedef short int16_t;
13 typedef unsigned short uint16_t;
14 typedef int int32_t;
15 typedef unsigned int uint32_t;
16 typedef __int64 int64_t;
17 typedef unsigned __int64 uint64_t;
18 #define ntohl(x) _byteswap_ulong (x)
19 #define ntohs(x) _byteswap_ushort (x)
20 #define htonl(x) _byteswap_ulong (x)
21 #define htons(x) _byteswap_ushort (x)
22 #else
23 #include <arpa/inet.h>
24 #include <stdint.h>
25 #endif
27 #include <algorithm>
28 #include <cassert>
29 #include <cstddef>
30 #include <cstring>
32 #define OTS_TAG(c1,c2,c3,c4) ((uint32_t)((((uint8_t)(c1))<<24)|(((uint8_t)(c2))<<16)|(((uint8_t)(c3))<<8)|((uint8_t)(c4))))
33 #define OTS_UNTAG(tag) ((uint8_t)((tag)>>24)), ((uint8_t)((tag)>>16)), ((uint8_t)((tag)>>8)), ((uint8_t)(tag))
35 namespace ots {
37 // -----------------------------------------------------------------------------
38 // This is an interface for an abstract stream class which is used for writing
39 // the serialised results out.
40 // -----------------------------------------------------------------------------
41 class OTSStream {
42 public:
43 OTSStream() : chksum_(0) {}
45 virtual ~OTSStream() {}
47 // This should be implemented to perform the actual write.
48 virtual bool WriteRaw(const void *data, size_t length) = 0;
50 bool Write(const void *data, size_t length) {
51 if (!length) return false;
53 const size_t orig_length = length;
54 size_t offset = 0;
56 size_t chksum_offset = Tell() & 3;
57 if (chksum_offset) {
58 const size_t l = std::min(length, static_cast<size_t>(4) - chksum_offset);
59 uint32_t tmp = 0;
60 std::memcpy(reinterpret_cast<uint8_t *>(&tmp) + chksum_offset, data, l);
61 chksum_ += ntohl(tmp);
62 length -= l;
63 offset += l;
66 while (length >= 4) {
67 uint32_t tmp;
68 std::memcpy(&tmp, reinterpret_cast<const uint8_t *>(data) + offset,
69 sizeof(uint32_t));
70 chksum_ += ntohl(tmp);
71 length -= 4;
72 offset += 4;
75 if (length) {
76 if (length > 4) return false; // not reached
77 uint32_t tmp = 0;
78 std::memcpy(&tmp,
79 reinterpret_cast<const uint8_t*>(data) + offset, length);
80 chksum_ += ntohl(tmp);
83 return WriteRaw(data, orig_length);
86 virtual bool Seek(off_t position) = 0;
87 virtual off_t Tell() const = 0;
89 virtual bool Pad(size_t bytes) {
90 static const uint32_t kZero = 0;
91 while (bytes >= 4) {
92 if (!WriteTag(kZero)) return false;
93 bytes -= 4;
95 while (bytes) {
96 static const uint8_t kZerob = 0;
97 if (!Write(&kZerob, 1)) return false;
98 bytes--;
100 return true;
103 bool WriteU8(uint8_t v) {
104 return Write(&v, sizeof(v));
107 bool WriteU16(uint16_t v) {
108 v = htons(v);
109 return Write(&v, sizeof(v));
112 bool WriteS16(int16_t v) {
113 v = htons(v);
114 return Write(&v, sizeof(v));
117 bool WriteU24(uint32_t v) {
118 v = htonl(v);
119 return Write(reinterpret_cast<uint8_t*>(&v)+1, 3);
122 bool WriteU32(uint32_t v) {
123 v = htonl(v);
124 return Write(&v, sizeof(v));
127 bool WriteS32(int32_t v) {
128 v = htonl(v);
129 return Write(&v, sizeof(v));
132 bool WriteR64(uint64_t v) {
133 return Write(&v, sizeof(v));
136 bool WriteTag(uint32_t v) {
137 return Write(&v, sizeof(v));
140 void ResetChecksum() {
141 assert((Tell() & 3) == 0);
142 chksum_ = 0;
145 uint32_t chksum() const {
146 return chksum_;
149 protected:
150 uint32_t chksum_;
153 #ifdef __GCC__
154 #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3)))
155 #else
156 #define MSGFUNC_FMT_ATTR
157 #endif
159 enum TableAction {
160 TABLE_ACTION_DEFAULT, // Use OTS's default action for that table
161 TABLE_ACTION_SANITIZE, // Sanitize the table, potentially droping it
162 TABLE_ACTION_PASSTHRU, // Serialize the table unchanged
163 TABLE_ACTION_DROP // Drop the table
166 class OTSContext {
167 public:
168 OTSContext() {}
169 virtual ~OTSContext() {}
171 // Process a given OpenType file and write out a sanitised version
172 // output: a pointer to an object implementing the OTSStream interface. The
173 // sanitisied output will be written to this. In the even of a failure,
174 // partial output may have been written.
175 // input: the OpenType file
176 // length: the size, in bytes, of |input|
177 bool Process(OTSStream *output, const uint8_t *input, size_t length);
179 // This function will be called when OTS is reporting an error.
180 // level: the severity of the generated message:
181 // 0: error messages in case OTS fails to sanitize the font.
182 // 1: warning messages about issue OTS fixed in the sanitized font.
183 virtual void Message(int level, const char *format, ...) MSGFUNC_FMT_ATTR {}
185 // This function will be called when OTS needs to decide what to do for a
186 // font table.
187 // tag: table tag as an integer in big-endian byte order, independent of
188 // platform endianness
189 virtual TableAction GetTableAction(uint32_t tag) { return ots::TABLE_ACTION_DEFAULT; }
192 } // namespace ots
194 #endif // OPENTYPE_SANITISER_H_