buffer.h (4185B)
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 #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 : buffer_(data), 62 length_(len), 63 offset_(0) { } 64 65 bool Skip(size_t n_bytes) { 66 return Read(NULL, n_bytes); 67 } 68 69 bool Read(uint8_t *data, size_t n_bytes) { 70 if (n_bytes > 1024 * 1024 * 1024) { 71 return FONT_COMPRESSION_FAILURE(); 72 } 73 if ((offset_ + n_bytes > length_) || 74 (offset_ > length_ - n_bytes)) { 75 return FONT_COMPRESSION_FAILURE(); 76 } 77 if (data) { 78 std::memcpy(data, buffer_ + offset_, n_bytes); 79 } 80 offset_ += n_bytes; 81 return true; 82 } 83 84 inline bool ReadU8(uint8_t *value) { 85 if (offset_ + 1 > length_) { 86 return FONT_COMPRESSION_FAILURE(); 87 } 88 *value = buffer_[offset_]; 89 ++offset_; 90 return true; 91 } 92 93 bool ReadU16(uint16_t *value) { 94 if (offset_ + 2 > length_) { 95 return FONT_COMPRESSION_FAILURE(); 96 } 97 std::memcpy(value, buffer_ + offset_, sizeof(uint16_t)); 98 *value = ntohs(*value); 99 offset_ += 2; 100 return true; 101 } 102 103 bool ReadS16(int16_t *value) { 104 return ReadU16(reinterpret_cast<uint16_t*>(value)); 105 } 106 107 bool ReadU24(uint32_t *value) { 108 if (offset_ + 3 > length_) { 109 return FONT_COMPRESSION_FAILURE(); 110 } 111 *value = static_cast<uint32_t>(buffer_[offset_]) << 16 | 112 static_cast<uint32_t>(buffer_[offset_ + 1]) << 8 | 113 static_cast<uint32_t>(buffer_[offset_ + 2]); 114 offset_ += 3; 115 return true; 116 } 117 118 bool ReadU32(uint32_t *value) { 119 if (offset_ + 4 > length_) { 120 return FONT_COMPRESSION_FAILURE(); 121 } 122 std::memcpy(value, buffer_ + offset_, sizeof(uint32_t)); 123 *value = ntohl(*value); 124 offset_ += 4; 125 return true; 126 } 127 128 bool ReadS32(int32_t *value) { 129 return ReadU32(reinterpret_cast<uint32_t*>(value)); 130 } 131 132 bool ReadTag(uint32_t *value) { 133 if (offset_ + 4 > length_) { 134 return FONT_COMPRESSION_FAILURE(); 135 } 136 std::memcpy(value, buffer_ + offset_, sizeof(uint32_t)); 137 offset_ += 4; 138 return true; 139 } 140 141 bool ReadR64(uint64_t *value) { 142 if (offset_ + 8 > length_) { 143 return FONT_COMPRESSION_FAILURE(); 144 } 145 std::memcpy(value, buffer_ + offset_, sizeof(uint64_t)); 146 offset_ += 8; 147 return true; 148 } 149 150 const uint8_t *buffer() const { return buffer_; } 151 size_t offset() const { return offset_; } 152 size_t length() const { return length_; } 153 154 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_