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