| 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; |
| 11 typedef unsigned char uint8_t; | 11 typedef unsigned char uint8_t; |
| 12 typedef short int16_t; | 12 typedef short int16_t; |
| 13 typedef unsigned short uint16_t; | 13 typedef unsigned short uint16_t; |
| 14 typedef int int32_t; | 14 typedef int int32_t; |
| 15 typedef unsigned int uint32_t; | 15 typedef unsigned int uint32_t; |
| 16 typedef __int64 int64_t; | 16 typedef __int64 int64_t; |
| 17 typedef unsigned __int64 uint64_t; | 17 typedef unsigned __int64 uint64_t; |
| 18 #define ntohl(x) _byteswap_ulong (x) | 18 #define ntohl(x) _byteswap_ulong (x) |
| 19 #define ntohs(x) _byteswap_ushort (x) | 19 #define ntohs(x) _byteswap_ushort (x) |
| 20 #define htonl(x) _byteswap_ulong (x) | 20 #define htonl(x) _byteswap_ulong (x) |
| 21 #define htons(x) _byteswap_ushort (x) | 21 #define htons(x) _byteswap_ushort (x) |
| 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> // for std::min | 27 #include <algorithm> |
| 28 #include <cassert> | 28 #include <cassert> |
| 29 #include <cstddef> | 29 #include <cstddef> |
| 30 #include <cstring> | 30 #include <cstring> |
| 31 | 31 |
| 32 namespace ots { | 32 namespace ots { |
| 33 | 33 |
| 34 // ----------------------------------------------------------------------------- | 34 // ----------------------------------------------------------------------------- |
| 35 // 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 |
| 36 // the serialised results out. | 36 // the serialised results out. |
| 37 // ----------------------------------------------------------------------------- | 37 // ----------------------------------------------------------------------------- |
| (...skipping 16 matching lines...) Expand all Loading... |
| 54 if (chksum_buffer_offset_) { | 54 if (chksum_buffer_offset_) { |
| 55 const size_t l = | 55 const size_t l = |
| 56 std::min(length, static_cast<size_t>(4) - chksum_buffer_offset_); | 56 std::min(length, static_cast<size_t>(4) - chksum_buffer_offset_); |
| 57 std::memcpy(chksum_buffer_ + chksum_buffer_offset_, data, l); | 57 std::memcpy(chksum_buffer_ + chksum_buffer_offset_, data, l); |
| 58 chksum_buffer_offset_ += l; | 58 chksum_buffer_offset_ += l; |
| 59 offset += l; | 59 offset += l; |
| 60 length -= l; | 60 length -= l; |
| 61 } | 61 } |
| 62 | 62 |
| 63 if (chksum_buffer_offset_ == 4) { | 63 if (chksum_buffer_offset_ == 4) { |
| 64 uint32_t chksum; | 64 uint32_t tmp; |
| 65 std::memcpy(&chksum, chksum_buffer_, 4); | 65 std::memcpy(&tmp, chksum_buffer_, 4); |
| 66 chksum_ += ntohl(chksum); | 66 chksum_ += ntohl(tmp); |
| 67 chksum_buffer_offset_ = 0; | 67 chksum_buffer_offset_ = 0; |
| 68 } | 68 } |
| 69 | 69 |
| 70 while (length >= 4) { | 70 while (length >= 4) { |
| 71 chksum_ += ntohl(*reinterpret_cast<const uint32_t*>( | 71 uint32_t tmp; |
| 72 reinterpret_cast<const uint8_t*>(data) + offset)); | 72 std::memcpy(&tmp, reinterpret_cast<const uint8_t *>(data) + offset, |
| 73 sizeof(uint32_t)); |
| 74 chksum_ += ntohl(tmp); |
| 73 length -= 4; | 75 length -= 4; |
| 74 offset += 4; | 76 offset += 4; |
| 75 } | 77 } |
| 76 | 78 |
| 77 if (length) { | 79 if (length) { |
| 78 if (chksum_buffer_offset_ != 0) return false; // not reached | 80 if (chksum_buffer_offset_ != 0) return false; // not reached |
| 79 if (length > 4) return false; // not reached | 81 if (length > 4) return false; // not reached |
| 80 std::memcpy(chksum_buffer_, | 82 std::memcpy(chksum_buffer_, |
| 81 reinterpret_cast<const uint8_t*>(data) + offset, length); | 83 reinterpret_cast<const uint8_t*>(data) + offset, length); |
| 82 chksum_buffer_offset_ = length; | 84 chksum_buffer_offset_ = length; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 chksum_buffer_offset_ = s.chksum_buffer_offset; | 172 chksum_buffer_offset_ = s.chksum_buffer_offset; |
| 171 std::memcpy(chksum_buffer_, s.chksum_buffer, 4); | 173 std::memcpy(chksum_buffer_, s.chksum_buffer, 4); |
| 172 } | 174 } |
| 173 | 175 |
| 174 protected: | 176 protected: |
| 175 uint32_t chksum_; | 177 uint32_t chksum_; |
| 176 uint8_t chksum_buffer_[4]; | 178 uint8_t chksum_buffer_[4]; |
| 177 unsigned chksum_buffer_offset_; | 179 unsigned chksum_buffer_offset_; |
| 178 }; | 180 }; |
| 179 | 181 |
| 180 // ----------------------------------------------------------------------------- | 182 #ifdef __GCC__ |
| 181 // Process a given OpenType file and write out a sanitised version | 183 #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3))) |
| 182 // output: a pointer to an object implementing the OTSStream interface. The | 184 #else |
| 183 // sanitisied output will be written to this. In the even of a failure, | 185 #define MSGFUNC_FMT_ATTR |
| 184 // partial output may have been written. | 186 #endif |
| 185 // input: the OpenType file | 187 |
| 186 // length: the size, in bytes, of |input| | 188 enum TableAction { |
| 187 // ----------------------------------------------------------------------------- | 189 TABLE_ACTION_DEFAULT, // Use OTS's default action for that table |
| 190 TABLE_ACTION_SANITIZE, // Sanitize the table, potentially droping it |
| 191 TABLE_ACTION_PASSTHRU, // Serialize the table unchanged |
| 192 TABLE_ACTION_DROP // Drop the table |
| 193 }; |
| 194 |
| 195 class OTSContext { |
| 196 public: |
| 197 OTSContext() {} |
| 198 ~OTSContext() {} |
| 199 |
| 200 // Process a given OpenType file and write out a sanitised version |
| 201 // output: a pointer to an object implementing the OTSStream interface. Th
e |
| 202 // sanitisied output will be written to this. In the even of a failure, |
| 203 // partial output may have been written. |
| 204 // input: the OpenType file |
| 205 // length: the size, in bytes, of |input| |
| 206 // context: optional context that holds various OTS settings like user cal
lbacks |
| 207 bool Process(OTSStream *output, const uint8_t *input, size_t length); |
| 208 |
| 209 // This function will be called when OTS is reporting an error. |
| 210 // level: the severity of the generated message: |
| 211 // 0: error messages in case OTS fails to sanitize the font. |
| 212 // 1: warning messages about issue OTS fixed in the sanitized font. |
| 213 virtual void Message(int level, const char *format, ...) MSGFUNC_FMT_ATTR {} |
| 214 |
| 215 // This function will be called when OTS needs to decide what to do for a |
| 216 // font table. |
| 217 // tag: table tag as an integer in big-endian byte order, independent of |
| 218 // platform endianness |
| 219 virtual TableAction GetTableAction(uint32_t tag) { return ots::TABLE_ACTION_
DEFAULT; } |
| 220 }; |
| 221 |
| 222 // For backward compatibility - remove once Chrome switches over to the new API. |
| 188 bool Process(OTSStream *output, const uint8_t *input, size_t length); | 223 bool Process(OTSStream *output, const uint8_t *input, size_t length); |
| 189 | 224 |
| 190 // Force to disable debug output even when the library is compiled with | 225 // Force to disable debug output even when the library is compiled with |
| 191 // -DOTS_DEBUG. | 226 // -DOTS_DEBUG. |
| 192 void DisableDebugOutput(); | 227 void DisableDebugOutput(); |
| 193 | 228 |
| 194 // Enable WOFF2 support(experimental). | 229 // Enable WOFF2 support(experimental). |
| 195 // TODO(bashi): Remove WOFF2 from OTS. | |
| 196 void EnableWOFF2(); | 230 void EnableWOFF2(); |
| 197 | 231 |
| 198 // Force to disable dropping CBDT/CBLC tables. | 232 // Force to disable dropping CBDT/CBLC tables. |
| 199 void DoNotDropColorBitmapTables(); | 233 void DoNotDropColorBitmapTables(); |
| 200 | 234 |
| 201 } // namespace ots | 235 } // namespace ots |
| 202 | 236 |
| 203 #endif // OPENTYPE_SANITISER_H_ | 237 #endif // OPENTYPE_SANITISER_H_ |
| OLD | NEW |