| 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 |
| 32 namespace ots { | 35 namespace ots { |
| 33 | 36 |
| 34 // ----------------------------------------------------------------------------- | 37 // ----------------------------------------------------------------------------- |
| 35 // This is an interface for an abstract stream class which is used for writing | 38 // This is an interface for an abstract stream class which is used for writing |
| 36 // the serialised results out. | 39 // the serialised results out. |
| 37 // ----------------------------------------------------------------------------- | 40 // ----------------------------------------------------------------------------- |
| 38 class OTSStream { | 41 class OTSStream { |
| 39 public: | 42 public: |
| 40 OTSStream() { | 43 OTSStream() : chksum_(0) {} |
| 41 ResetChecksum(); | |
| 42 } | |
| 43 | 44 |
| 44 virtual ~OTSStream() {} | 45 virtual ~OTSStream() {} |
| 45 | 46 |
| 46 // This should be implemented to perform the actual write. | 47 // This should be implemented to perform the actual write. |
| 47 virtual bool WriteRaw(const void *data, size_t length) = 0; | 48 virtual bool WriteRaw(const void *data, size_t length) = 0; |
| 48 | 49 |
| 49 bool Write(const void *data, size_t length) { | 50 bool Write(const void *data, size_t length) { |
| 50 if (!length) return false; | 51 if (!length) return false; |
| 51 | 52 |
| 52 const size_t orig_length = length; | 53 const size_t orig_length = length; |
| 53 size_t offset = 0; | 54 size_t offset = 0; |
| 54 if (chksum_buffer_offset_) { | 55 |
| 55 const size_t l = | 56 size_t chksum_offset = Tell() & 3; |
| 56 std::min(length, static_cast<size_t>(4) - chksum_buffer_offset_); | 57 if (chksum_offset) { |
| 57 std::memcpy(chksum_buffer_ + chksum_buffer_offset_, data, l); | 58 const size_t l = std::min(length, static_cast<size_t>(4) - chksum_offset); |
| 58 chksum_buffer_offset_ += l; | 59 uint32_t tmp = 0; |
| 60 std::memcpy(reinterpret_cast<uint8_t *>(&tmp) + chksum_offset, data, l); |
| 61 chksum_ += ntohl(tmp); |
| 62 length -= l; |
| 59 offset += l; | 63 offset += l; |
| 60 length -= l; | |
| 61 } | |
| 62 | |
| 63 if (chksum_buffer_offset_ == 4) { | |
| 64 uint32_t tmp; | |
| 65 std::memcpy(&tmp, chksum_buffer_, 4); | |
| 66 chksum_ += ntohl(tmp); | |
| 67 chksum_buffer_offset_ = 0; | |
| 68 } | 64 } |
| 69 | 65 |
| 70 while (length >= 4) { | 66 while (length >= 4) { |
| 71 uint32_t tmp; | 67 uint32_t tmp; |
| 72 std::memcpy(&tmp, reinterpret_cast<const uint8_t *>(data) + offset, | 68 std::memcpy(&tmp, reinterpret_cast<const uint8_t *>(data) + offset, |
| 73 sizeof(uint32_t)); | 69 sizeof(uint32_t)); |
| 74 chksum_ += ntohl(tmp); | 70 chksum_ += ntohl(tmp); |
| 75 length -= 4; | 71 length -= 4; |
| 76 offset += 4; | 72 offset += 4; |
| 77 } | 73 } |
| 78 | 74 |
| 79 if (length) { | 75 if (length) { |
| 80 if (chksum_buffer_offset_ != 0) return false; // not reached | |
| 81 if (length > 4) return false; // not reached | 76 if (length > 4) return false; // not reached |
| 82 std::memcpy(chksum_buffer_, | 77 uint32_t tmp = 0; |
| 83 reinterpret_cast<const uint8_t*>(data) + offset, length); | 78 std::memcpy(&tmp, |
| 84 chksum_buffer_offset_ = length; | 79 reinterpret_cast<const uint8_t*>(data) + offset, length); |
| 80 chksum_ += ntohl(tmp); |
| 85 } | 81 } |
| 86 | 82 |
| 87 return WriteRaw(data, orig_length); | 83 return WriteRaw(data, orig_length); |
| 88 } | 84 } |
| 89 | 85 |
| 90 virtual bool Seek(off_t position) = 0; | 86 virtual bool Seek(off_t position) = 0; |
| 91 virtual off_t Tell() const = 0; | 87 virtual off_t Tell() const = 0; |
| 92 | 88 |
| 93 virtual bool Pad(size_t bytes) { | 89 virtual bool Pad(size_t bytes) { |
| 94 static const uint32_t kZero = 0; | 90 static const uint32_t kZero = 0; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 131 |
| 136 bool WriteR64(uint64_t v) { | 132 bool WriteR64(uint64_t v) { |
| 137 return Write(&v, sizeof(v)); | 133 return Write(&v, sizeof(v)); |
| 138 } | 134 } |
| 139 | 135 |
| 140 bool WriteTag(uint32_t v) { | 136 bool WriteTag(uint32_t v) { |
| 141 return Write(&v, sizeof(v)); | 137 return Write(&v, sizeof(v)); |
| 142 } | 138 } |
| 143 | 139 |
| 144 void ResetChecksum() { | 140 void ResetChecksum() { |
| 141 assert((Tell() & 3) == 0); |
| 145 chksum_ = 0; | 142 chksum_ = 0; |
| 146 chksum_buffer_offset_ = 0; | |
| 147 } | 143 } |
| 148 | 144 |
| 149 uint32_t chksum() const { | 145 uint32_t chksum() const { |
| 150 assert(chksum_buffer_offset_ == 0); | |
| 151 return chksum_; | 146 return chksum_; |
| 152 } | 147 } |
| 153 | 148 |
| 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 | |
| 176 protected: | 149 protected: |
| 177 uint32_t chksum_; | 150 uint32_t chksum_; |
| 178 uint8_t chksum_buffer_[4]; | |
| 179 unsigned chksum_buffer_offset_; | |
| 180 }; | 151 }; |
| 181 | 152 |
| 182 #ifdef __GCC__ | 153 #ifdef __GCC__ |
| 183 #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3))) | 154 #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3))) |
| 184 #else | 155 #else |
| 185 #define MSGFUNC_FMT_ATTR | 156 #define MSGFUNC_FMT_ATTR |
| 186 #endif | 157 #endif |
| 187 | 158 |
| 188 enum TableAction { | 159 enum TableAction { |
| 189 TABLE_ACTION_DEFAULT, // Use OTS's default action for that table | 160 TABLE_ACTION_DEFAULT, // Use OTS's default action for that table |
| (...skipping 21 matching lines...) Expand all Loading... |
| 211 // 1: warning messages about issue OTS fixed in the sanitized font. | 182 // 1: warning messages about issue OTS fixed in the sanitized font. |
| 212 virtual void Message(int level, const char *format, ...) MSGFUNC_FMT_ATTR {} | 183 virtual void Message(int level, const char *format, ...) MSGFUNC_FMT_ATTR {} |
| 213 | 184 |
| 214 // This function will be called when OTS needs to decide what to do for a | 185 // This function will be called when OTS needs to decide what to do for a |
| 215 // font table. | 186 // font table. |
| 216 // tag: table tag as an integer in big-endian byte order, independent of | 187 // tag: table tag as an integer in big-endian byte order, independent of |
| 217 // platform endianness | 188 // platform endianness |
| 218 virtual TableAction GetTableAction(uint32_t tag) { return ots::TABLE_ACTION_
DEFAULT; } | 189 virtual TableAction GetTableAction(uint32_t tag) { return ots::TABLE_ACTION_
DEFAULT; } |
| 219 }; | 190 }; |
| 220 | 191 |
| 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 | |
| 224 } // namespace ots | 192 } // namespace ots |
| 225 | 193 |
| 226 #endif // OPENTYPE_SANITISER_H_ | 194 #endif // OPENTYPE_SANITISER_H_ |
| OLD | NEW |