Line data Source code
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 :
18 : #ifndef WOFF2_BUFFER_H_
19 : #define WOFF2_BUFFER_H_
20 :
21 : #if defined(_WIN32)
22 : #include <stdlib.h>
23 : typedef signed char int8_t;
24 : typedef unsigned char uint8_t;
25 : typedef short int16_t;
26 : typedef unsigned short uint16_t;
27 : typedef int int32_t;
28 : typedef unsigned int uint32_t;
29 : typedef __int64 int64_t;
30 : typedef unsigned __int64 uint64_t;
31 : #define ntohl(x) _byteswap_ulong (x)
32 : #define ntohs(x) _byteswap_ushort (x)
33 : #define htonl(x) _byteswap_ulong (x)
34 : #define htons(x) _byteswap_ushort (x)
35 : #else
36 : #include <arpa/inet.h>
37 : #include <stdint.h>
38 : #endif
39 :
40 : #include <cstdio>
41 : #include <cstdlib>
42 : #include <cstring>
43 : #include <limits>
44 :
45 : namespace woff2 {
46 :
47 : #if defined(_MSC_VER) || !defined(FONT_COMPRESSION_DEBUG)
48 : #define FONT_COMPRESSION_FAILURE() false
49 : #else
50 : #define FONT_COMPRESSION_FAILURE() \
51 : woff2::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
52 : inline bool Failure(const char *f, int l, const char *fn) {
53 : fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn);
54 : fflush(stderr);
55 : return false;
56 : }
57 : #endif
58 :
59 : // -----------------------------------------------------------------------------
60 : // Buffer helper class
61 : //
62 : // This class perform some trival buffer operations while checking for
63 : // out-of-bounds errors. As a family they return false if anything is amiss,
64 : // updating the current offset otherwise.
65 : // -----------------------------------------------------------------------------
66 : class Buffer {
67 : public:
68 0 : Buffer(const uint8_t *buffer, size_t len)
69 0 : : buffer_(buffer),
70 : length_(len),
71 0 : offset_(0) { }
72 :
73 0 : bool Skip(size_t n_bytes) {
74 0 : return Read(NULL, n_bytes);
75 : }
76 :
77 0 : bool Read(uint8_t *buffer, size_t n_bytes) {
78 0 : if (n_bytes > 1024 * 1024 * 1024) {
79 0 : return FONT_COMPRESSION_FAILURE();
80 : }
81 0 : if ((offset_ + n_bytes > length_) ||
82 0 : (offset_ > length_ - n_bytes)) {
83 0 : return FONT_COMPRESSION_FAILURE();
84 : }
85 0 : if (buffer) {
86 0 : std::memcpy(buffer, buffer_ + offset_, n_bytes);
87 : }
88 0 : offset_ += n_bytes;
89 0 : return true;
90 : }
91 :
92 0 : inline bool ReadU8(uint8_t *value) {
93 0 : if (offset_ + 1 > length_) {
94 0 : return FONT_COMPRESSION_FAILURE();
95 : }
96 0 : *value = buffer_[offset_];
97 0 : ++offset_;
98 0 : return true;
99 : }
100 :
101 0 : bool ReadU16(uint16_t *value) {
102 0 : if (offset_ + 2 > length_) {
103 0 : return FONT_COMPRESSION_FAILURE();
104 : }
105 0 : std::memcpy(value, buffer_ + offset_, sizeof(uint16_t));
106 0 : *value = ntohs(*value);
107 0 : offset_ += 2;
108 0 : return true;
109 : }
110 :
111 0 : bool ReadS16(int16_t *value) {
112 0 : return ReadU16(reinterpret_cast<uint16_t*>(value));
113 : }
114 :
115 : bool ReadU24(uint32_t *value) {
116 : if (offset_ + 3 > length_) {
117 : return FONT_COMPRESSION_FAILURE();
118 : }
119 : *value = static_cast<uint32_t>(buffer_[offset_]) << 16 |
120 : static_cast<uint32_t>(buffer_[offset_ + 1]) << 8 |
121 : static_cast<uint32_t>(buffer_[offset_ + 2]);
122 : offset_ += 3;
123 : return true;
124 : }
125 :
126 0 : bool ReadU32(uint32_t *value) {
127 0 : if (offset_ + 4 > length_) {
128 0 : return FONT_COMPRESSION_FAILURE();
129 : }
130 0 : std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
131 0 : *value = ntohl(*value);
132 0 : offset_ += 4;
133 0 : return true;
134 : }
135 :
136 : bool ReadS32(int32_t *value) {
137 : return ReadU32(reinterpret_cast<uint32_t*>(value));
138 : }
139 :
140 : bool ReadTag(uint32_t *value) {
141 : if (offset_ + 4 > length_) {
142 : return FONT_COMPRESSION_FAILURE();
143 : }
144 : std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
145 : offset_ += 4;
146 : return true;
147 : }
148 :
149 : bool ReadR64(uint64_t *value) {
150 : if (offset_ + 8 > length_) {
151 : return FONT_COMPRESSION_FAILURE();
152 : }
153 : std::memcpy(value, buffer_ + offset_, sizeof(uint64_t));
154 : offset_ += 8;
155 : return true;
156 : }
157 :
158 0 : const uint8_t *buffer() const { return buffer_; }
159 0 : size_t offset() const { return offset_; }
160 0 : size_t length() const { return length_; }
161 :
162 : void set_offset(size_t newoffset) { offset_ = newoffset; }
163 :
164 : private:
165 : const uint8_t * const buffer_;
166 : const size_t length_;
167 : size_t offset_;
168 : };
169 :
170 : } // namespace woff2
171 :
172 : #endif // WOFF2_BUFFER_H_
|