-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathbuffer.h
More file actions
183 lines (159 loc) · 5.11 KB
/
buffer.h
File metadata and controls
183 lines (159 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* Copyright 2013 Google Inc. All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/
/* The parts of ots.h & opentype-sanitiser.h that we need, taken from the
https://code.google.com/p/ots/ project. */
#ifndef WOFF2_BUFFER_H_
#define WOFF2_BUFFER_H_
#if defined(_WIN32)
#include <stdlib.h>
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned int uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#define ntohl(x) _byteswap_ulong (x)
#define ntohs(x) _byteswap_ushort (x)
#define htonl(x) _byteswap_ulong (x)
#define htons(x) _byteswap_ushort (x)
#else
#include <arpa/inet.h>
#include <stdint.h>
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <span>
namespace woff2 {
#if defined(_MSC_VER) || !defined(FONT_COMPRESSION_DEBUG)
#define FONT_COMPRESSION_FAILURE() false
#else
#define FONT_COMPRESSION_FAILURE() \
woff2::Failure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
inline bool Failure(const char *f, int l, const char *fn) {
fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn);
fflush(stderr);
return false;
}
#endif
// -----------------------------------------------------------------------------
// Buffer helper class
//
// This class perform some trival buffer operations while checking for
// out-of-bounds errors. As a family they return false if anything is amiss,
// updating the current offset otherwise.
// -----------------------------------------------------------------------------
class Buffer {
public:
Buffer(const uint8_t *data, size_t len)
: buffer_(data, len),
offset_(0) { }
Buffer(std::span<const uint8_t> data) : buffer_(data), offset_(0) { }
bool Skip(size_t n_bytes) {
return Read(std::span<uint8_t>(), n_bytes);
}
bool Read(std::span<uint8_t> data, size_t n_bytes) {
if (n_bytes > 1024 * 1024 * 1024) {
return FONT_COMPRESSION_FAILURE();
}
if (n_bytes > remaining_length()) {
return FONT_COMPRESSION_FAILURE();
}
if (data.size() != 0) {
std::span<const uint8_t> buffer_view =
remaining_buffer().subspan(0, n_bytes);
std::copy(buffer_view.begin(), buffer_view.end(), data.begin());
}
offset_ += n_bytes;
return true;
}
inline bool ReadU8(uint8_t *value) {
if (1 > remaining_length()) {
return FONT_COMPRESSION_FAILURE();
}
*value = buffer_[offset_];
++offset_;
return true;
}
bool ReadU16(uint16_t *value) {
if (2 > remaining_length()) {
return FONT_COMPRESSION_FAILURE();
}
*value = static_cast<uint32_t>(buffer_[offset_]) << 8 |
static_cast<uint32_t>(buffer_[offset_ + 1]);
offset_ += 2;
return true;
}
bool ReadS16(int16_t *value) {
return ReadU16(reinterpret_cast<uint16_t*>(value));
}
bool ReadU24(uint32_t *value) {
if (3 > remaining_length()) {
return FONT_COMPRESSION_FAILURE();
}
*value = static_cast<uint32_t>(buffer_[offset_]) << 16 |
static_cast<uint32_t>(buffer_[offset_ + 1]) << 8 |
static_cast<uint32_t>(buffer_[offset_ + 2]);
offset_ += 3;
return true;
}
bool ReadU32(uint32_t *value) {
if (4 > remaining_length()) {
return FONT_COMPRESSION_FAILURE();
}
*value = static_cast<uint32_t>(buffer_[offset_]) << 24 |
static_cast<uint32_t>(buffer_[offset_ + 1]) << 16 |
static_cast<uint32_t>(buffer_[offset_ + 2]) << 8 |
static_cast<uint32_t>(buffer_[offset_ + 3]);
offset_ += 4;
return true;
}
bool ReadS32(int32_t *value) {
return ReadU32(reinterpret_cast<uint32_t*>(value));
}
bool ReadTag(uint32_t *value) {
if (4 > remaining_length()) {
return FONT_COMPRESSION_FAILURE();
}
*value = static_cast<uint32_t>(buffer_[offset_]) |
static_cast<uint32_t>(buffer_[offset_ + 1]) << 8 |
static_cast<uint32_t>(buffer_[offset_ + 2]) << 16 |
static_cast<uint32_t>(buffer_[offset_ + 3]) << 24;
offset_ += 4;
return true;
}
bool ReadR64(uint64_t *value) {
if (8 > remaining_length()) {
return FONT_COMPRESSION_FAILURE();
}
*value = static_cast<uint64_t>(buffer_[offset_]) << 56 |
static_cast<uint64_t>(buffer_[offset_ + 1]) << 48 |
static_cast<uint64_t>(buffer_[offset_ + 2]) << 40 |
static_cast<uint64_t>(buffer_[offset_ + 3]) << 32 |
static_cast<uint64_t>(buffer_[offset_ + 4]) << 24 |
static_cast<uint64_t>(buffer_[offset_ + 5]) << 16 |
static_cast<uint64_t>(buffer_[offset_ + 6]) << 8 |
static_cast<uint64_t>(buffer_[offset_ + 7]);
offset_ += 8;
return true;
}
inline std::span<const uint8_t> remaining_buffer() const {
return buffer_.subspan(offset_);
};
inline size_t remaining_length() const { return remaining_buffer().size(); }
inline size_t offset() { return offset_; }
void set_offset(size_t newoffset) {
offset_ = newoffset;
}
private:
// A view of the unowned buffer.
const std::span<const uint8_t> buffer_;
size_t offset_;
};
} // namespace woff2
#endif // WOFF2_BUFFER_H_