OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef OPENTYPE_SANITISER_H_ | 5 #ifndef OPENTYPE_SANITISER_H_ |
6 #define OPENTYPE_SANITISER_H_ | 6 #define OPENTYPE_SANITISER_H_ |
7 | 7 |
8 #if defined(_WIN32) | 8 #if defined(_WIN32) |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 typedef signed char int8_t; | 10 typedef signed char int8_t; |
(...skipping 11 matching lines...) Expand all Loading... |
22 #else | 22 #else |
23 #include <arpa/inet.h> | 23 #include <arpa/inet.h> |
24 #include <stdint.h> | 24 #include <stdint.h> |
25 #endif | 25 #endif |
26 | 26 |
27 #include <algorithm> | 27 #include <algorithm> |
28 #include <cassert> | 28 #include <cassert> |
29 #include <cstddef> | 29 #include <cstddef> |
30 #include <cstring> | 30 #include <cstring> |
31 | 31 |
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)), ((u
int8_t)((tag)>>8)), ((uint8_t)(tag)) | |
34 | |
35 namespace ots { | 32 namespace ots { |
36 | 33 |
37 // ----------------------------------------------------------------------------- | 34 // ----------------------------------------------------------------------------- |
38 // This is an interface for an abstract stream class which is used for writing | 35 // This is an interface for an abstract stream class which is used for writing |
39 // the serialised results out. | 36 // the serialised results out. |
40 // ----------------------------------------------------------------------------- | 37 // ----------------------------------------------------------------------------- |
41 class OTSStream { | 38 class OTSStream { |
42 public: | 39 public: |
43 OTSStream() : chksum_(0) {} | 40 OTSStream() { |
| 41 ResetChecksum(); |
| 42 } |
44 | 43 |
45 virtual ~OTSStream() {} | 44 virtual ~OTSStream() {} |
46 | 45 |
47 // This should be implemented to perform the actual write. | 46 // This should be implemented to perform the actual write. |
48 virtual bool WriteRaw(const void *data, size_t length) = 0; | 47 virtual bool WriteRaw(const void *data, size_t length) = 0; |
49 | 48 |
50 bool Write(const void *data, size_t length) { | 49 bool Write(const void *data, size_t length) { |
51 if (!length) return false; | 50 if (!length) return false; |
52 | 51 |
53 const size_t orig_length = length; | 52 const size_t orig_length = length; |
54 size_t offset = 0; | 53 size_t offset = 0; |
| 54 if (chksum_buffer_offset_) { |
| 55 const size_t l = |
| 56 std::min(length, static_cast<size_t>(4) - chksum_buffer_offset_); |
| 57 std::memcpy(chksum_buffer_ + chksum_buffer_offset_, data, l); |
| 58 chksum_buffer_offset_ += l; |
| 59 offset += l; |
| 60 length -= l; |
| 61 } |
55 | 62 |
56 size_t chksum_offset = Tell() & 3; | 63 if (chksum_buffer_offset_ == 4) { |
57 if (chksum_offset) { | 64 uint32_t tmp; |
58 const size_t l = std::min(length, static_cast<size_t>(4) - chksum_offset); | 65 std::memcpy(&tmp, chksum_buffer_, 4); |
59 uint32_t tmp = 0; | |
60 std::memcpy(reinterpret_cast<uint8_t *>(&tmp) + chksum_offset, data, l); | |
61 chksum_ += ntohl(tmp); | 66 chksum_ += ntohl(tmp); |
62 length -= l; | 67 chksum_buffer_offset_ = 0; |
63 offset += l; | |
64 } | 68 } |
65 | 69 |
66 while (length >= 4) { | 70 while (length >= 4) { |
67 uint32_t tmp; | 71 uint32_t tmp; |
68 std::memcpy(&tmp, reinterpret_cast<const uint8_t *>(data) + offset, | 72 std::memcpy(&tmp, reinterpret_cast<const uint8_t *>(data) + offset, |
69 sizeof(uint32_t)); | 73 sizeof(uint32_t)); |
70 chksum_ += ntohl(tmp); | 74 chksum_ += ntohl(tmp); |
71 length -= 4; | 75 length -= 4; |
72 offset += 4; | 76 offset += 4; |
73 } | 77 } |
74 | 78 |
75 if (length) { | 79 if (length) { |
| 80 if (chksum_buffer_offset_ != 0) return false; // not reached |
76 if (length > 4) return false; // not reached | 81 if (length > 4) return false; // not reached |
77 uint32_t tmp = 0; | 82 std::memcpy(chksum_buffer_, |
78 std::memcpy(&tmp, | 83 reinterpret_cast<const uint8_t*>(data) + offset, length); |
79 reinterpret_cast<const uint8_t*>(data) + offset, length); | 84 chksum_buffer_offset_ = length; |
80 chksum_ += ntohl(tmp); | |
81 } | 85 } |
82 | 86 |
83 return WriteRaw(data, orig_length); | 87 return WriteRaw(data, orig_length); |
84 } | 88 } |
85 | 89 |
86 virtual bool Seek(off_t position) = 0; | 90 virtual bool Seek(off_t position) = 0; |
87 virtual off_t Tell() const = 0; | 91 virtual off_t Tell() const = 0; |
88 | 92 |
89 virtual bool Pad(size_t bytes) { | 93 virtual bool Pad(size_t bytes) { |
90 static const uint32_t kZero = 0; | 94 static const uint32_t kZero = 0; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 | 135 |
132 bool WriteR64(uint64_t v) { | 136 bool WriteR64(uint64_t v) { |
133 return Write(&v, sizeof(v)); | 137 return Write(&v, sizeof(v)); |
134 } | 138 } |
135 | 139 |
136 bool WriteTag(uint32_t v) { | 140 bool WriteTag(uint32_t v) { |
137 return Write(&v, sizeof(v)); | 141 return Write(&v, sizeof(v)); |
138 } | 142 } |
139 | 143 |
140 void ResetChecksum() { | 144 void ResetChecksum() { |
141 assert((Tell() & 3) == 0); | |
142 chksum_ = 0; | 145 chksum_ = 0; |
| 146 chksum_buffer_offset_ = 0; |
143 } | 147 } |
144 | 148 |
145 uint32_t chksum() const { | 149 uint32_t chksum() const { |
| 150 assert(chksum_buffer_offset_ == 0); |
146 return chksum_; | 151 return chksum_; |
147 } | 152 } |
148 | 153 |
| 154 struct ChecksumState { |
| 155 uint32_t chksum; |
| 156 uint8_t chksum_buffer[4]; |
| 157 unsigned chksum_buffer_offset; |
| 158 }; |
| 159 |
| 160 ChecksumState SaveChecksumState() const { |
| 161 ChecksumState s; |
| 162 s.chksum = chksum_; |
| 163 s.chksum_buffer_offset = chksum_buffer_offset_; |
| 164 std::memcpy(s.chksum_buffer, chksum_buffer_, 4); |
| 165 |
| 166 return s; |
| 167 } |
| 168 |
| 169 void RestoreChecksum(const ChecksumState &s) { |
| 170 assert(chksum_buffer_offset_ == 0); |
| 171 chksum_ += s.chksum; |
| 172 chksum_buffer_offset_ = s.chksum_buffer_offset; |
| 173 std::memcpy(chksum_buffer_, s.chksum_buffer, 4); |
| 174 } |
| 175 |
149 protected: | 176 protected: |
150 uint32_t chksum_; | 177 uint32_t chksum_; |
| 178 uint8_t chksum_buffer_[4]; |
| 179 unsigned chksum_buffer_offset_; |
151 }; | 180 }; |
152 | 181 |
153 #ifdef __GCC__ | 182 #ifdef __GCC__ |
154 #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3))) | 183 #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3))) |
155 #else | 184 #else |
156 #define MSGFUNC_FMT_ATTR | 185 #define MSGFUNC_FMT_ATTR |
157 #endif | 186 #endif |
158 | 187 |
159 enum TableAction { | 188 enum TableAction { |
160 TABLE_ACTION_DEFAULT, // Use OTS's default action for that table | 189 TABLE_ACTION_DEFAULT, // Use OTS's default action for that table |
(...skipping 21 matching lines...) Expand all Loading... |
182 // 1: warning messages about issue OTS fixed in the sanitized font. | 211 // 1: warning messages about issue OTS fixed in the sanitized font. |
183 virtual void Message(int level, const char *format, ...) MSGFUNC_FMT_ATTR {} | 212 virtual void Message(int level, const char *format, ...) MSGFUNC_FMT_ATTR {} |
184 | 213 |
185 // This function will be called when OTS needs to decide what to do for a | 214 // This function will be called when OTS needs to decide what to do for a |
186 // font table. | 215 // font table. |
187 // tag: table tag as an integer in big-endian byte order, independent of | 216 // tag: table tag as an integer in big-endian byte order, independent of |
188 // platform endianness | 217 // platform endianness |
189 virtual TableAction GetTableAction(uint32_t tag) { return ots::TABLE_ACTION_
DEFAULT; } | 218 virtual TableAction GetTableAction(uint32_t tag) { return ots::TABLE_ACTION_
DEFAULT; } |
190 }; | 219 }; |
191 | 220 |
| 221 // For backward compatibility - remove once Chrome switches over to the new API. |
| 222 bool Process(OTSStream *output, const uint8_t *input, size_t length); |
| 223 |
192 } // namespace ots | 224 } // namespace ots |
193 | 225 |
194 #endif // OPENTYPE_SANITISER_H_ | 226 #endif // OPENTYPE_SANITISER_H_ |
OLD | NEW |