Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // -*- mode: c++ -*- |
michael@0 | 2 | |
michael@0 | 3 | // Copyright (c) 2010, Google Inc. |
michael@0 | 4 | // All rights reserved. |
michael@0 | 5 | // |
michael@0 | 6 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | // modification, are permitted provided that the following conditions are |
michael@0 | 8 | // met: |
michael@0 | 9 | // |
michael@0 | 10 | // * Redistributions of source code must retain the above copyright |
michael@0 | 11 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | // * Redistributions in binary form must reproduce the above |
michael@0 | 13 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 14 | // in the documentation and/or other materials provided with the |
michael@0 | 15 | // distribution. |
michael@0 | 16 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 17 | // contributors may be used to endorse or promote products derived from |
michael@0 | 18 | // this software without specific prior written permission. |
michael@0 | 19 | // |
michael@0 | 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | |
michael@0 | 32 | // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
michael@0 | 33 | |
michael@0 | 34 | // byte_cursor.h: Classes for parsing values from a buffer of bytes. |
michael@0 | 35 | // The ByteCursor class provides a convenient interface for reading |
michael@0 | 36 | // fixed-size integers of arbitrary endianness, being thorough about |
michael@0 | 37 | // checking for buffer overruns. |
michael@0 | 38 | |
michael@0 | 39 | #ifndef COMMON_BYTE_CURSOR_H_ |
michael@0 | 40 | #define COMMON_BYTE_CURSOR_H_ |
michael@0 | 41 | |
michael@0 | 42 | #include <assert.h> |
michael@0 | 43 | #include <stdint.h> |
michael@0 | 44 | #include <stdlib.h> |
michael@0 | 45 | #include <string.h> |
michael@0 | 46 | #include <string> |
michael@0 | 47 | |
michael@0 | 48 | #include "common/using_std_string.h" |
michael@0 | 49 | |
michael@0 | 50 | namespace google_breakpad { |
michael@0 | 51 | |
michael@0 | 52 | // A buffer holding a series of bytes. |
michael@0 | 53 | struct ByteBuffer { |
michael@0 | 54 | ByteBuffer() : start(0), end(0) { } |
michael@0 | 55 | ByteBuffer(const uint8_t *set_start, size_t set_size) |
michael@0 | 56 | : start(set_start), end(set_start + set_size) { } |
michael@0 | 57 | ~ByteBuffer() { }; |
michael@0 | 58 | |
michael@0 | 59 | // Equality operators. Useful in unit tests, and when we're using |
michael@0 | 60 | // ByteBuffers to refer to regions of a larger buffer. |
michael@0 | 61 | bool operator==(const ByteBuffer &that) const { |
michael@0 | 62 | return start == that.start && end == that.end; |
michael@0 | 63 | } |
michael@0 | 64 | bool operator!=(const ByteBuffer &that) const { |
michael@0 | 65 | return start != that.start || end != that.end; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | // Not C++ style guide compliant, but this definitely belongs here. |
michael@0 | 69 | size_t Size() const { |
michael@0 | 70 | assert(start <= end); |
michael@0 | 71 | return end - start; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | const uint8_t *start, *end; |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | // A cursor pointing into a ByteBuffer that can parse numbers of various |
michael@0 | 78 | // widths and representations, strings, and data blocks, advancing through |
michael@0 | 79 | // the buffer as it goes. All ByteCursor operations check that accesses |
michael@0 | 80 | // haven't gone beyond the end of the enclosing ByteBuffer. |
michael@0 | 81 | class ByteCursor { |
michael@0 | 82 | public: |
michael@0 | 83 | // Create a cursor reading bytes from the start of BUFFER. By default, the |
michael@0 | 84 | // cursor reads multi-byte values in little-endian form. |
michael@0 | 85 | ByteCursor(const ByteBuffer *buffer, bool big_endian = false) |
michael@0 | 86 | : buffer_(buffer), here_(buffer->start), |
michael@0 | 87 | big_endian_(big_endian), complete_(true) { } |
michael@0 | 88 | |
michael@0 | 89 | // Accessor and setter for this cursor's endianness flag. |
michael@0 | 90 | bool big_endian() const { return big_endian_; } |
michael@0 | 91 | void set_big_endian(bool big_endian) { big_endian_ = big_endian; } |
michael@0 | 92 | |
michael@0 | 93 | // Accessor and setter for this cursor's current position. The setter |
michael@0 | 94 | // returns a reference to this cursor. |
michael@0 | 95 | const uint8_t *here() const { return here_; } |
michael@0 | 96 | ByteCursor &set_here(const uint8_t *here) { |
michael@0 | 97 | assert(buffer_->start <= here && here <= buffer_->end); |
michael@0 | 98 | here_ = here; |
michael@0 | 99 | return *this; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | // Return the number of bytes available to read at the cursor. |
michael@0 | 103 | size_t Available() const { return size_t(buffer_->end - here_); } |
michael@0 | 104 | |
michael@0 | 105 | // Return true if this cursor is at the end of its buffer. |
michael@0 | 106 | bool AtEnd() const { return Available() == 0; } |
michael@0 | 107 | |
michael@0 | 108 | // When used as a boolean value this cursor converts to true if all |
michael@0 | 109 | // prior reads have been completed, or false if we ran off the end |
michael@0 | 110 | // of the buffer. |
michael@0 | 111 | operator bool() const { return complete_; } |
michael@0 | 112 | |
michael@0 | 113 | // Read a SIZE-byte integer at this cursor, signed if IS_SIGNED is true, |
michael@0 | 114 | // unsigned otherwise, using the cursor's established endianness, and set |
michael@0 | 115 | // *RESULT to the number. If we read off the end of our buffer, clear |
michael@0 | 116 | // this cursor's complete_ flag, and store a dummy value in *RESULT. |
michael@0 | 117 | // Return a reference to this cursor. |
michael@0 | 118 | template<typename T> |
michael@0 | 119 | ByteCursor &Read(size_t size, bool is_signed, T *result) { |
michael@0 | 120 | if (CheckAvailable(size)) { |
michael@0 | 121 | T v = 0; |
michael@0 | 122 | if (big_endian_) { |
michael@0 | 123 | for (size_t i = 0; i < size; i++) |
michael@0 | 124 | v = (v << 8) + here_[i]; |
michael@0 | 125 | } else { |
michael@0 | 126 | // This loop condition looks weird, but size_t is unsigned, so |
michael@0 | 127 | // decrementing i after it is zero yields the largest size_t value. |
michael@0 | 128 | for (size_t i = size - 1; i < size; i--) |
michael@0 | 129 | v = (v << 8) + here_[i]; |
michael@0 | 130 | } |
michael@0 | 131 | if (is_signed && size < sizeof(T)) { |
michael@0 | 132 | size_t sign_bit = (T)1 << (size * 8 - 1); |
michael@0 | 133 | v = (v ^ sign_bit) - sign_bit; |
michael@0 | 134 | } |
michael@0 | 135 | here_ += size; |
michael@0 | 136 | *result = v; |
michael@0 | 137 | } else { |
michael@0 | 138 | *result = (T) 0xdeadbeef; |
michael@0 | 139 | } |
michael@0 | 140 | return *this; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | // Read an integer, using the cursor's established endianness and |
michael@0 | 144 | // *RESULT's size and signedness, and set *RESULT to the number. If we |
michael@0 | 145 | // read off the end of our buffer, clear this cursor's complete_ flag. |
michael@0 | 146 | // Return a reference to this cursor. |
michael@0 | 147 | template<typename T> |
michael@0 | 148 | ByteCursor &operator>>(T &result) { |
michael@0 | 149 | bool T_is_signed = (T)-1 < 0; |
michael@0 | 150 | return Read(sizeof(T), T_is_signed, &result); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | // Copy the SIZE bytes at the cursor to BUFFER, and advance this |
michael@0 | 154 | // cursor to the end of them. If we read off the end of our buffer, |
michael@0 | 155 | // clear this cursor's complete_ flag, and set *POINTER to NULL. |
michael@0 | 156 | // Return a reference to this cursor. |
michael@0 | 157 | ByteCursor &Read(uint8_t *buffer, size_t size) { |
michael@0 | 158 | if (CheckAvailable(size)) { |
michael@0 | 159 | memcpy(buffer, here_, size); |
michael@0 | 160 | here_ += size; |
michael@0 | 161 | } |
michael@0 | 162 | return *this; |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | // Set STR to a copy of the '\0'-terminated string at the cursor. If the |
michael@0 | 166 | // byte buffer does not contain a terminating zero, clear this cursor's |
michael@0 | 167 | // complete_ flag, and set STR to the empty string. Return a reference to |
michael@0 | 168 | // this cursor. |
michael@0 | 169 | ByteCursor &CString(string *str) { |
michael@0 | 170 | const uint8_t *end |
michael@0 | 171 | = static_cast<const uint8_t *>(memchr(here_, '\0', Available())); |
michael@0 | 172 | if (end) { |
michael@0 | 173 | str->assign(reinterpret_cast<const char *>(here_), end - here_); |
michael@0 | 174 | here_ = end + 1; |
michael@0 | 175 | } else { |
michael@0 | 176 | str->clear(); |
michael@0 | 177 | here_ = buffer_->end; |
michael@0 | 178 | complete_ = false; |
michael@0 | 179 | } |
michael@0 | 180 | return *this; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | // Like CString(STR), but extract the string from a fixed-width buffer |
michael@0 | 184 | // LIMIT bytes long, which may or may not contain a terminating '\0' |
michael@0 | 185 | // byte. Specifically: |
michael@0 | 186 | // |
michael@0 | 187 | // - If there are not LIMIT bytes available at the cursor, clear the |
michael@0 | 188 | // cursor's complete_ flag and set STR to the empty string. |
michael@0 | 189 | // |
michael@0 | 190 | // - Otherwise, if the LIMIT bytes at the cursor contain any '\0' |
michael@0 | 191 | // characters, set *STR to a copy of the bytes before the first '\0', |
michael@0 | 192 | // and advance the cursor by LIMIT bytes. |
michael@0 | 193 | // |
michael@0 | 194 | // - Otherwise, set *STR to a copy of those LIMIT bytes, and advance the |
michael@0 | 195 | // cursor by LIMIT bytes. |
michael@0 | 196 | ByteCursor &CString(string *str, size_t limit) { |
michael@0 | 197 | if (CheckAvailable(limit)) { |
michael@0 | 198 | const uint8_t *end |
michael@0 | 199 | = static_cast<const uint8_t *>(memchr(here_, '\0', limit)); |
michael@0 | 200 | if (end) |
michael@0 | 201 | str->assign(reinterpret_cast<const char *>(here_), end - here_); |
michael@0 | 202 | else |
michael@0 | 203 | str->assign(reinterpret_cast<const char *>(here_), limit); |
michael@0 | 204 | here_ += limit; |
michael@0 | 205 | } else { |
michael@0 | 206 | str->clear(); |
michael@0 | 207 | } |
michael@0 | 208 | return *this; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | // Set *POINTER to point to the SIZE bytes at the cursor, and advance |
michael@0 | 212 | // this cursor to the end of them. If SIZE is omitted, don't move the |
michael@0 | 213 | // cursor. If we read off the end of our buffer, clear this cursor's |
michael@0 | 214 | // complete_ flag, and set *POINTER to NULL. Return a reference to this |
michael@0 | 215 | // cursor. |
michael@0 | 216 | ByteCursor &PointTo(const uint8_t **pointer, size_t size = 0) { |
michael@0 | 217 | if (CheckAvailable(size)) { |
michael@0 | 218 | *pointer = here_; |
michael@0 | 219 | here_ += size; |
michael@0 | 220 | } else { |
michael@0 | 221 | *pointer = NULL; |
michael@0 | 222 | } |
michael@0 | 223 | return *this; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | // Skip SIZE bytes at the cursor. If doing so would advance us off |
michael@0 | 227 | // the end of our buffer, clear this cursor's complete_ flag, and |
michael@0 | 228 | // set *POINTER to NULL. Return a reference to this cursor. |
michael@0 | 229 | ByteCursor &Skip(size_t size) { |
michael@0 | 230 | if (CheckAvailable(size)) |
michael@0 | 231 | here_ += size; |
michael@0 | 232 | return *this; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | private: |
michael@0 | 236 | // If there are at least SIZE bytes available to read from the buffer, |
michael@0 | 237 | // return true. Otherwise, set here_ to the end of the buffer, set |
michael@0 | 238 | // complete_ to false, and return false. |
michael@0 | 239 | bool CheckAvailable(size_t size) { |
michael@0 | 240 | if (Available() >= size) { |
michael@0 | 241 | return true; |
michael@0 | 242 | } else { |
michael@0 | 243 | here_ = buffer_->end; |
michael@0 | 244 | complete_ = false; |
michael@0 | 245 | return false; |
michael@0 | 246 | } |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | // The buffer we're reading bytes from. |
michael@0 | 250 | const ByteBuffer *buffer_; |
michael@0 | 251 | |
michael@0 | 252 | // The next byte within buffer_ that we'll read. |
michael@0 | 253 | const uint8_t *here_; |
michael@0 | 254 | |
michael@0 | 255 | // True if we should read numbers in big-endian form; false if we |
michael@0 | 256 | // should read in little-endian form. |
michael@0 | 257 | bool big_endian_; |
michael@0 | 258 | |
michael@0 | 259 | // True if we've been able to read all we've been asked to. |
michael@0 | 260 | bool complete_; |
michael@0 | 261 | }; |
michael@0 | 262 | |
michael@0 | 263 | } // namespace google_breakpad |
michael@0 | 264 | |
michael@0 | 265 | #endif // COMMON_BYTE_CURSOR_H_ |