1 // Copyright 2013 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // The parts of ots.h & opentype-sanitiser.h that we need, taken from the
16 // https://code.google.com/p/ots/ project.
18 #ifndef WOFF2_BUFFER_H_
19 #define WOFF2_BUFFER_H_
23 typedef signed char int8_t;
24 typedef unsigned char uint8_t;
25 typedef short int16_t;
26 typedef unsigned short uint16_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)
36 #include <arpa/inet.h>
47 #if defined(_MSC_VER) || !defined(FONT_COMPRESSION_DEBUG)
48 #define FONT_COMPRESSION_FAILURE() false
50 #define FONT_COMPRESSION_FAILURE() \
51 util::compression::font::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
);
59 // -----------------------------------------------------------------------------
60 // Buffer helper class
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 // -----------------------------------------------------------------------------
68 Buffer(const uint8_t *buffer
, size_t len
)
73 bool Skip(size_t n_bytes
) {
74 return Read(NULL
, n_bytes
);
77 bool Read(uint8_t *buffer
, size_t n_bytes
) {
78 if (n_bytes
> 1024 * 1024 * 1024) {
79 return FONT_COMPRESSION_FAILURE();
81 if ((offset_
+ n_bytes
> length_
) ||
82 (offset_
> length_
- n_bytes
)) {
83 return FONT_COMPRESSION_FAILURE();
86 std::memcpy(buffer
, buffer_
+ offset_
, n_bytes
);
92 inline bool ReadU8(uint8_t *value
) {
93 if (offset_
+ 1 > length_
) {
94 return FONT_COMPRESSION_FAILURE();
96 *value
= buffer_
[offset_
];
101 bool ReadU16(uint16_t *value
) {
102 if (offset_
+ 2 > length_
) {
103 return FONT_COMPRESSION_FAILURE();
105 std::memcpy(value
, buffer_
+ offset_
, sizeof(uint16_t));
106 *value
= ntohs(*value
);
111 bool ReadS16(int16_t *value
) {
112 return ReadU16(reinterpret_cast<uint16_t*>(value
));
115 bool ReadU24(uint32_t *value
) {
116 if (offset_
+ 3 > length_
) {
117 return FONT_COMPRESSION_FAILURE();
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]);
126 bool ReadU32(uint32_t *value
) {
127 if (offset_
+ 4 > length_
) {
128 return FONT_COMPRESSION_FAILURE();
130 std::memcpy(value
, buffer_
+ offset_
, sizeof(uint32_t));
131 *value
= ntohl(*value
);
136 bool ReadS32(int32_t *value
) {
137 return ReadU32(reinterpret_cast<uint32_t*>(value
));
140 bool ReadTag(uint32_t *value
) {
141 if (offset_
+ 4 > length_
) {
142 return FONT_COMPRESSION_FAILURE();
144 std::memcpy(value
, buffer_
+ offset_
, sizeof(uint32_t));
149 bool ReadR64(uint64_t *value
) {
150 if (offset_
+ 8 > length_
) {
151 return FONT_COMPRESSION_FAILURE();
153 std::memcpy(value
, buffer_
+ offset_
, sizeof(uint64_t));
158 const uint8_t *buffer() const { return buffer_
; }
159 size_t offset() const { return offset_
; }
160 size_t length() const { return length_
; }
162 void set_offset(size_t newoffset
) { offset_
= newoffset
; }
165 const uint8_t * const buffer_
;
166 const size_t length_
;
172 #endif // WOFF2_BUFFER_H_