Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / woff2 / src / buffer.h
blob3595e845b3190ada78e39b7eb8f5869347550330
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.
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_
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
40 #include <cstdio>
41 #include <cstdlib>
42 #include <cstring>
43 #include <limits>
45 namespace woff2 {
47 #if defined(_MSC_VER) || !defined(FONT_COMPRESSION_DEBUG)
48 #define FONT_COMPRESSION_FAILURE() false
49 #else
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);
54 fflush(stderr);
55 return false;
57 #endif
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 // -----------------------------------------------------------------------------
66 class Buffer {
67 public:
68 Buffer(const uint8_t *buffer, size_t len)
69 : buffer_(buffer),
70 length_(len),
71 offset_(0) { }
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();
85 if (buffer) {
86 std::memcpy(buffer, buffer_ + offset_, n_bytes);
88 offset_ += n_bytes;
89 return true;
92 inline bool ReadU8(uint8_t *value) {
93 if (offset_ + 1 > length_) {
94 return FONT_COMPRESSION_FAILURE();
96 *value = buffer_[offset_];
97 ++offset_;
98 return true;
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);
107 offset_ += 2;
108 return true;
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]);
122 offset_ += 3;
123 return true;
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);
132 offset_ += 4;
133 return true;
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));
145 offset_ += 4;
146 return true;
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));
154 offset_ += 8;
155 return true;
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; }
164 private:
165 const uint8_t * const buffer_;
166 const size_t length_;
167 size_t offset_;
170 } // namespace woff2
172 #endif // WOFF2_BUFFER_H_