1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/byte_cursor.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,265 @@ 1.4 +// -*- mode: c++ -*- 1.5 + 1.6 +// Copyright (c) 2010, Google Inc. 1.7 +// All rights reserved. 1.8 +// 1.9 +// Redistribution and use in source and binary forms, with or without 1.10 +// modification, are permitted provided that the following conditions are 1.11 +// met: 1.12 +// 1.13 +// * Redistributions of source code must retain the above copyright 1.14 +// notice, this list of conditions and the following disclaimer. 1.15 +// * Redistributions in binary form must reproduce the above 1.16 +// copyright notice, this list of conditions and the following disclaimer 1.17 +// in the documentation and/or other materials provided with the 1.18 +// distribution. 1.19 +// * Neither the name of Google Inc. nor the names of its 1.20 +// contributors may be used to endorse or promote products derived from 1.21 +// this software without specific prior written permission. 1.22 +// 1.23 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.27 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.28 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.29 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.30 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.31 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.32 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.33 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + 1.35 +// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> 1.36 + 1.37 +// byte_cursor.h: Classes for parsing values from a buffer of bytes. 1.38 +// The ByteCursor class provides a convenient interface for reading 1.39 +// fixed-size integers of arbitrary endianness, being thorough about 1.40 +// checking for buffer overruns. 1.41 + 1.42 +#ifndef COMMON_BYTE_CURSOR_H_ 1.43 +#define COMMON_BYTE_CURSOR_H_ 1.44 + 1.45 +#include <assert.h> 1.46 +#include <stdint.h> 1.47 +#include <stdlib.h> 1.48 +#include <string.h> 1.49 +#include <string> 1.50 + 1.51 +#include "common/using_std_string.h" 1.52 + 1.53 +namespace google_breakpad { 1.54 + 1.55 +// A buffer holding a series of bytes. 1.56 +struct ByteBuffer { 1.57 + ByteBuffer() : start(0), end(0) { } 1.58 + ByteBuffer(const uint8_t *set_start, size_t set_size) 1.59 + : start(set_start), end(set_start + set_size) { } 1.60 + ~ByteBuffer() { }; 1.61 + 1.62 + // Equality operators. Useful in unit tests, and when we're using 1.63 + // ByteBuffers to refer to regions of a larger buffer. 1.64 + bool operator==(const ByteBuffer &that) const { 1.65 + return start == that.start && end == that.end; 1.66 + } 1.67 + bool operator!=(const ByteBuffer &that) const { 1.68 + return start != that.start || end != that.end; 1.69 + } 1.70 + 1.71 + // Not C++ style guide compliant, but this definitely belongs here. 1.72 + size_t Size() const { 1.73 + assert(start <= end); 1.74 + return end - start; 1.75 + } 1.76 + 1.77 + const uint8_t *start, *end; 1.78 +}; 1.79 + 1.80 +// A cursor pointing into a ByteBuffer that can parse numbers of various 1.81 +// widths and representations, strings, and data blocks, advancing through 1.82 +// the buffer as it goes. All ByteCursor operations check that accesses 1.83 +// haven't gone beyond the end of the enclosing ByteBuffer. 1.84 +class ByteCursor { 1.85 + public: 1.86 + // Create a cursor reading bytes from the start of BUFFER. By default, the 1.87 + // cursor reads multi-byte values in little-endian form. 1.88 + ByteCursor(const ByteBuffer *buffer, bool big_endian = false) 1.89 + : buffer_(buffer), here_(buffer->start), 1.90 + big_endian_(big_endian), complete_(true) { } 1.91 + 1.92 + // Accessor and setter for this cursor's endianness flag. 1.93 + bool big_endian() const { return big_endian_; } 1.94 + void set_big_endian(bool big_endian) { big_endian_ = big_endian; } 1.95 + 1.96 + // Accessor and setter for this cursor's current position. The setter 1.97 + // returns a reference to this cursor. 1.98 + const uint8_t *here() const { return here_; } 1.99 + ByteCursor &set_here(const uint8_t *here) { 1.100 + assert(buffer_->start <= here && here <= buffer_->end); 1.101 + here_ = here; 1.102 + return *this; 1.103 + } 1.104 + 1.105 + // Return the number of bytes available to read at the cursor. 1.106 + size_t Available() const { return size_t(buffer_->end - here_); } 1.107 + 1.108 + // Return true if this cursor is at the end of its buffer. 1.109 + bool AtEnd() const { return Available() == 0; } 1.110 + 1.111 + // When used as a boolean value this cursor converts to true if all 1.112 + // prior reads have been completed, or false if we ran off the end 1.113 + // of the buffer. 1.114 + operator bool() const { return complete_; } 1.115 + 1.116 + // Read a SIZE-byte integer at this cursor, signed if IS_SIGNED is true, 1.117 + // unsigned otherwise, using the cursor's established endianness, and set 1.118 + // *RESULT to the number. If we read off the end of our buffer, clear 1.119 + // this cursor's complete_ flag, and store a dummy value in *RESULT. 1.120 + // Return a reference to this cursor. 1.121 + template<typename T> 1.122 + ByteCursor &Read(size_t size, bool is_signed, T *result) { 1.123 + if (CheckAvailable(size)) { 1.124 + T v = 0; 1.125 + if (big_endian_) { 1.126 + for (size_t i = 0; i < size; i++) 1.127 + v = (v << 8) + here_[i]; 1.128 + } else { 1.129 + // This loop condition looks weird, but size_t is unsigned, so 1.130 + // decrementing i after it is zero yields the largest size_t value. 1.131 + for (size_t i = size - 1; i < size; i--) 1.132 + v = (v << 8) + here_[i]; 1.133 + } 1.134 + if (is_signed && size < sizeof(T)) { 1.135 + size_t sign_bit = (T)1 << (size * 8 - 1); 1.136 + v = (v ^ sign_bit) - sign_bit; 1.137 + } 1.138 + here_ += size; 1.139 + *result = v; 1.140 + } else { 1.141 + *result = (T) 0xdeadbeef; 1.142 + } 1.143 + return *this; 1.144 + } 1.145 + 1.146 + // Read an integer, using the cursor's established endianness and 1.147 + // *RESULT's size and signedness, and set *RESULT to the number. If we 1.148 + // read off the end of our buffer, clear this cursor's complete_ flag. 1.149 + // Return a reference to this cursor. 1.150 + template<typename T> 1.151 + ByteCursor &operator>>(T &result) { 1.152 + bool T_is_signed = (T)-1 < 0; 1.153 + return Read(sizeof(T), T_is_signed, &result); 1.154 + } 1.155 + 1.156 + // Copy the SIZE bytes at the cursor to BUFFER, and advance this 1.157 + // cursor to the end of them. If we read off the end of our buffer, 1.158 + // clear this cursor's complete_ flag, and set *POINTER to NULL. 1.159 + // Return a reference to this cursor. 1.160 + ByteCursor &Read(uint8_t *buffer, size_t size) { 1.161 + if (CheckAvailable(size)) { 1.162 + memcpy(buffer, here_, size); 1.163 + here_ += size; 1.164 + } 1.165 + return *this; 1.166 + } 1.167 + 1.168 + // Set STR to a copy of the '\0'-terminated string at the cursor. If the 1.169 + // byte buffer does not contain a terminating zero, clear this cursor's 1.170 + // complete_ flag, and set STR to the empty string. Return a reference to 1.171 + // this cursor. 1.172 + ByteCursor &CString(string *str) { 1.173 + const uint8_t *end 1.174 + = static_cast<const uint8_t *>(memchr(here_, '\0', Available())); 1.175 + if (end) { 1.176 + str->assign(reinterpret_cast<const char *>(here_), end - here_); 1.177 + here_ = end + 1; 1.178 + } else { 1.179 + str->clear(); 1.180 + here_ = buffer_->end; 1.181 + complete_ = false; 1.182 + } 1.183 + return *this; 1.184 + } 1.185 + 1.186 + // Like CString(STR), but extract the string from a fixed-width buffer 1.187 + // LIMIT bytes long, which may or may not contain a terminating '\0' 1.188 + // byte. Specifically: 1.189 + // 1.190 + // - If there are not LIMIT bytes available at the cursor, clear the 1.191 + // cursor's complete_ flag and set STR to the empty string. 1.192 + // 1.193 + // - Otherwise, if the LIMIT bytes at the cursor contain any '\0' 1.194 + // characters, set *STR to a copy of the bytes before the first '\0', 1.195 + // and advance the cursor by LIMIT bytes. 1.196 + // 1.197 + // - Otherwise, set *STR to a copy of those LIMIT bytes, and advance the 1.198 + // cursor by LIMIT bytes. 1.199 + ByteCursor &CString(string *str, size_t limit) { 1.200 + if (CheckAvailable(limit)) { 1.201 + const uint8_t *end 1.202 + = static_cast<const uint8_t *>(memchr(here_, '\0', limit)); 1.203 + if (end) 1.204 + str->assign(reinterpret_cast<const char *>(here_), end - here_); 1.205 + else 1.206 + str->assign(reinterpret_cast<const char *>(here_), limit); 1.207 + here_ += limit; 1.208 + } else { 1.209 + str->clear(); 1.210 + } 1.211 + return *this; 1.212 + } 1.213 + 1.214 + // Set *POINTER to point to the SIZE bytes at the cursor, and advance 1.215 + // this cursor to the end of them. If SIZE is omitted, don't move the 1.216 + // cursor. If we read off the end of our buffer, clear this cursor's 1.217 + // complete_ flag, and set *POINTER to NULL. Return a reference to this 1.218 + // cursor. 1.219 + ByteCursor &PointTo(const uint8_t **pointer, size_t size = 0) { 1.220 + if (CheckAvailable(size)) { 1.221 + *pointer = here_; 1.222 + here_ += size; 1.223 + } else { 1.224 + *pointer = NULL; 1.225 + } 1.226 + return *this; 1.227 + } 1.228 + 1.229 + // Skip SIZE bytes at the cursor. If doing so would advance us off 1.230 + // the end of our buffer, clear this cursor's complete_ flag, and 1.231 + // set *POINTER to NULL. Return a reference to this cursor. 1.232 + ByteCursor &Skip(size_t size) { 1.233 + if (CheckAvailable(size)) 1.234 + here_ += size; 1.235 + return *this; 1.236 + } 1.237 + 1.238 + private: 1.239 + // If there are at least SIZE bytes available to read from the buffer, 1.240 + // return true. Otherwise, set here_ to the end of the buffer, set 1.241 + // complete_ to false, and return false. 1.242 + bool CheckAvailable(size_t size) { 1.243 + if (Available() >= size) { 1.244 + return true; 1.245 + } else { 1.246 + here_ = buffer_->end; 1.247 + complete_ = false; 1.248 + return false; 1.249 + } 1.250 + } 1.251 + 1.252 + // The buffer we're reading bytes from. 1.253 + const ByteBuffer *buffer_; 1.254 + 1.255 + // The next byte within buffer_ that we'll read. 1.256 + const uint8_t *here_; 1.257 + 1.258 + // True if we should read numbers in big-endian form; false if we 1.259 + // should read in little-endian form. 1.260 + bool big_endian_; 1.261 + 1.262 + // True if we've been able to read all we've been asked to. 1.263 + bool complete_; 1.264 +}; 1.265 + 1.266 +} // namespace google_breakpad 1.267 + 1.268 +#endif // COMMON_BYTE_CURSOR_H_