Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 1 | // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | // The parts of ots.h & opentype-sanitiser.h that we need, taken from the |
| 16 | // https://code.google.com/p/ots/ project. |
| 17 | |
Zoltan Szabadka | 494c85c | 2014-03-20 14:35:41 +0100 | [diff] [blame] | 18 | #ifndef WOFF2_OTS_H_ |
| 19 | #define WOFF2_OTS_H_ |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 20 | |
Roderick Sheeter | 2e5995b | 2013-12-12 10:43:05 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <arpa/inet.h> |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 23 | #include <cstdlib> |
| 24 | #include <cstring> |
| 25 | #include <limits> |
| 26 | |
| 27 | namespace ots { |
| 28 | |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 29 | #if defined(_MSC_VER) || !defined(OTS_DEBUG) |
| 30 | #define OTS_FAILURE() false |
| 31 | #else |
| 32 | #define OTS_FAILURE() ots::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__) |
| 33 | inline bool Failure(const char *f, int l, const char *fn) { |
| 34 | std::fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn); |
| 35 | std::fflush(stderr); |
| 36 | return false; |
| 37 | } |
| 38 | #endif |
| 39 | |
| 40 | // ----------------------------------------------------------------------------- |
| 41 | // Buffer helper class |
| 42 | // |
| 43 | // This class perform some trival buffer operations while checking for |
| 44 | // out-of-bounds errors. As a family they return false if anything is amiss, |
| 45 | // updating the current offset otherwise. |
| 46 | // ----------------------------------------------------------------------------- |
| 47 | class Buffer { |
| 48 | public: |
| 49 | Buffer(const uint8_t *buffer, size_t len) |
| 50 | : buffer_(buffer), |
| 51 | length_(len), |
| 52 | offset_(0) { } |
| 53 | |
| 54 | bool Skip(size_t n_bytes) { |
| 55 | return Read(NULL, n_bytes); |
| 56 | } |
| 57 | |
| 58 | bool Read(uint8_t *buffer, size_t n_bytes) { |
| 59 | if (n_bytes > 1024 * 1024 * 1024) { |
| 60 | return OTS_FAILURE(); |
| 61 | } |
| 62 | if ((offset_ + n_bytes > length_) || |
| 63 | (offset_ > length_ - n_bytes)) { |
| 64 | return OTS_FAILURE(); |
| 65 | } |
| 66 | if (buffer) { |
| 67 | std::memcpy(buffer, buffer_ + offset_, n_bytes); |
| 68 | } |
| 69 | offset_ += n_bytes; |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | inline bool ReadU8(uint8_t *value) { |
| 74 | if (offset_ + 1 > length_) { |
| 75 | return OTS_FAILURE(); |
| 76 | } |
| 77 | *value = buffer_[offset_]; |
| 78 | ++offset_; |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool ReadU16(uint16_t *value) { |
| 83 | if (offset_ + 2 > length_) { |
| 84 | return OTS_FAILURE(); |
| 85 | } |
| 86 | std::memcpy(value, buffer_ + offset_, sizeof(uint16_t)); |
| 87 | *value = ntohs(*value); |
| 88 | offset_ += 2; |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | bool ReadS16(int16_t *value) { |
| 93 | return ReadU16(reinterpret_cast<uint16_t*>(value)); |
| 94 | } |
| 95 | |
| 96 | bool ReadU24(uint32_t *value) { |
| 97 | if (offset_ + 3 > length_) { |
| 98 | return OTS_FAILURE(); |
| 99 | } |
| 100 | *value = static_cast<uint32_t>(buffer_[offset_]) << 16 | |
| 101 | static_cast<uint32_t>(buffer_[offset_ + 1]) << 8 | |
| 102 | static_cast<uint32_t>(buffer_[offset_ + 2]); |
| 103 | offset_ += 3; |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | bool ReadU32(uint32_t *value) { |
| 108 | if (offset_ + 4 > length_) { |
| 109 | return OTS_FAILURE(); |
| 110 | } |
| 111 | std::memcpy(value, buffer_ + offset_, sizeof(uint32_t)); |
| 112 | *value = ntohl(*value); |
| 113 | offset_ += 4; |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool ReadS32(int32_t *value) { |
| 118 | return ReadU32(reinterpret_cast<uint32_t*>(value)); |
| 119 | } |
| 120 | |
| 121 | bool ReadTag(uint32_t *value) { |
| 122 | if (offset_ + 4 > length_) { |
| 123 | return OTS_FAILURE(); |
| 124 | } |
| 125 | std::memcpy(value, buffer_ + offset_, sizeof(uint32_t)); |
| 126 | offset_ += 4; |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool ReadR64(uint64_t *value) { |
| 131 | if (offset_ + 8 > length_) { |
| 132 | return OTS_FAILURE(); |
| 133 | } |
| 134 | std::memcpy(value, buffer_ + offset_, sizeof(uint64_t)); |
| 135 | offset_ += 8; |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | const uint8_t *buffer() const { return buffer_; } |
| 140 | size_t offset() const { return offset_; } |
| 141 | size_t length() const { return length_; } |
| 142 | |
| 143 | void set_offset(size_t newoffset) { offset_ = newoffset; } |
| 144 | |
| 145 | private: |
| 146 | const uint8_t * const buffer_; |
| 147 | const size_t length_; |
| 148 | size_t offset_; |
| 149 | }; |
| 150 | |
| 151 | } // namespace ots |
| 152 | |
Zoltan Szabadka | 494c85c | 2014-03-20 14:35:41 +0100 | [diff] [blame] | 153 | #endif // WOFF2_OTS_H_ |