michael@0: // -*- mode: c++ -*- michael@0: michael@0: // Copyright (c) 2010, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // Original author: Jim Blandy michael@0: michael@0: // byte_cursor.h: Classes for parsing values from a buffer of bytes. michael@0: // The ByteCursor class provides a convenient interface for reading michael@0: // fixed-size integers of arbitrary endianness, being thorough about michael@0: // checking for buffer overruns. michael@0: michael@0: #ifndef COMMON_BYTE_CURSOR_H_ michael@0: #define COMMON_BYTE_CURSOR_H_ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "common/using_std_string.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: // A buffer holding a series of bytes. michael@0: struct ByteBuffer { michael@0: ByteBuffer() : start(0), end(0) { } michael@0: ByteBuffer(const uint8_t *set_start, size_t set_size) michael@0: : start(set_start), end(set_start + set_size) { } michael@0: ~ByteBuffer() { }; michael@0: michael@0: // Equality operators. Useful in unit tests, and when we're using michael@0: // ByteBuffers to refer to regions of a larger buffer. michael@0: bool operator==(const ByteBuffer &that) const { michael@0: return start == that.start && end == that.end; michael@0: } michael@0: bool operator!=(const ByteBuffer &that) const { michael@0: return start != that.start || end != that.end; michael@0: } michael@0: michael@0: // Not C++ style guide compliant, but this definitely belongs here. michael@0: size_t Size() const { michael@0: assert(start <= end); michael@0: return end - start; michael@0: } michael@0: michael@0: const uint8_t *start, *end; michael@0: }; michael@0: michael@0: // A cursor pointing into a ByteBuffer that can parse numbers of various michael@0: // widths and representations, strings, and data blocks, advancing through michael@0: // the buffer as it goes. All ByteCursor operations check that accesses michael@0: // haven't gone beyond the end of the enclosing ByteBuffer. michael@0: class ByteCursor { michael@0: public: michael@0: // Create a cursor reading bytes from the start of BUFFER. By default, the michael@0: // cursor reads multi-byte values in little-endian form. michael@0: ByteCursor(const ByteBuffer *buffer, bool big_endian = false) michael@0: : buffer_(buffer), here_(buffer->start), michael@0: big_endian_(big_endian), complete_(true) { } michael@0: michael@0: // Accessor and setter for this cursor's endianness flag. michael@0: bool big_endian() const { return big_endian_; } michael@0: void set_big_endian(bool big_endian) { big_endian_ = big_endian; } michael@0: michael@0: // Accessor and setter for this cursor's current position. The setter michael@0: // returns a reference to this cursor. michael@0: const uint8_t *here() const { return here_; } michael@0: ByteCursor &set_here(const uint8_t *here) { michael@0: assert(buffer_->start <= here && here <= buffer_->end); michael@0: here_ = here; michael@0: return *this; michael@0: } michael@0: michael@0: // Return the number of bytes available to read at the cursor. michael@0: size_t Available() const { return size_t(buffer_->end - here_); } michael@0: michael@0: // Return true if this cursor is at the end of its buffer. michael@0: bool AtEnd() const { return Available() == 0; } michael@0: michael@0: // When used as a boolean value this cursor converts to true if all michael@0: // prior reads have been completed, or false if we ran off the end michael@0: // of the buffer. michael@0: operator bool() const { return complete_; } michael@0: michael@0: // Read a SIZE-byte integer at this cursor, signed if IS_SIGNED is true, michael@0: // unsigned otherwise, using the cursor's established endianness, and set michael@0: // *RESULT to the number. If we read off the end of our buffer, clear michael@0: // this cursor's complete_ flag, and store a dummy value in *RESULT. michael@0: // Return a reference to this cursor. michael@0: template michael@0: ByteCursor &Read(size_t size, bool is_signed, T *result) { michael@0: if (CheckAvailable(size)) { michael@0: T v = 0; michael@0: if (big_endian_) { michael@0: for (size_t i = 0; i < size; i++) michael@0: v = (v << 8) + here_[i]; michael@0: } else { michael@0: // This loop condition looks weird, but size_t is unsigned, so michael@0: // decrementing i after it is zero yields the largest size_t value. michael@0: for (size_t i = size - 1; i < size; i--) michael@0: v = (v << 8) + here_[i]; michael@0: } michael@0: if (is_signed && size < sizeof(T)) { michael@0: size_t sign_bit = (T)1 << (size * 8 - 1); michael@0: v = (v ^ sign_bit) - sign_bit; michael@0: } michael@0: here_ += size; michael@0: *result = v; michael@0: } else { michael@0: *result = (T) 0xdeadbeef; michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: // Read an integer, using the cursor's established endianness and michael@0: // *RESULT's size and signedness, and set *RESULT to the number. If we michael@0: // read off the end of our buffer, clear this cursor's complete_ flag. michael@0: // Return a reference to this cursor. michael@0: template michael@0: ByteCursor &operator>>(T &result) { michael@0: bool T_is_signed = (T)-1 < 0; michael@0: return Read(sizeof(T), T_is_signed, &result); michael@0: } michael@0: michael@0: // Copy the SIZE bytes at the cursor to BUFFER, and advance this michael@0: // cursor to the end of them. If we read off the end of our buffer, michael@0: // clear this cursor's complete_ flag, and set *POINTER to NULL. michael@0: // Return a reference to this cursor. michael@0: ByteCursor &Read(uint8_t *buffer, size_t size) { michael@0: if (CheckAvailable(size)) { michael@0: memcpy(buffer, here_, size); michael@0: here_ += size; michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: // Set STR to a copy of the '\0'-terminated string at the cursor. If the michael@0: // byte buffer does not contain a terminating zero, clear this cursor's michael@0: // complete_ flag, and set STR to the empty string. Return a reference to michael@0: // this cursor. michael@0: ByteCursor &CString(string *str) { michael@0: const uint8_t *end michael@0: = static_cast(memchr(here_, '\0', Available())); michael@0: if (end) { michael@0: str->assign(reinterpret_cast(here_), end - here_); michael@0: here_ = end + 1; michael@0: } else { michael@0: str->clear(); michael@0: here_ = buffer_->end; michael@0: complete_ = false; michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: // Like CString(STR), but extract the string from a fixed-width buffer michael@0: // LIMIT bytes long, which may or may not contain a terminating '\0' michael@0: // byte. Specifically: michael@0: // michael@0: // - If there are not LIMIT bytes available at the cursor, clear the michael@0: // cursor's complete_ flag and set STR to the empty string. michael@0: // michael@0: // - Otherwise, if the LIMIT bytes at the cursor contain any '\0' michael@0: // characters, set *STR to a copy of the bytes before the first '\0', michael@0: // and advance the cursor by LIMIT bytes. michael@0: // michael@0: // - Otherwise, set *STR to a copy of those LIMIT bytes, and advance the michael@0: // cursor by LIMIT bytes. michael@0: ByteCursor &CString(string *str, size_t limit) { michael@0: if (CheckAvailable(limit)) { michael@0: const uint8_t *end michael@0: = static_cast(memchr(here_, '\0', limit)); michael@0: if (end) michael@0: str->assign(reinterpret_cast(here_), end - here_); michael@0: else michael@0: str->assign(reinterpret_cast(here_), limit); michael@0: here_ += limit; michael@0: } else { michael@0: str->clear(); michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: // Set *POINTER to point to the SIZE bytes at the cursor, and advance michael@0: // this cursor to the end of them. If SIZE is omitted, don't move the michael@0: // cursor. If we read off the end of our buffer, clear this cursor's michael@0: // complete_ flag, and set *POINTER to NULL. Return a reference to this michael@0: // cursor. michael@0: ByteCursor &PointTo(const uint8_t **pointer, size_t size = 0) { michael@0: if (CheckAvailable(size)) { michael@0: *pointer = here_; michael@0: here_ += size; michael@0: } else { michael@0: *pointer = NULL; michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: // Skip SIZE bytes at the cursor. If doing so would advance us off michael@0: // the end of our buffer, clear this cursor's complete_ flag, and michael@0: // set *POINTER to NULL. Return a reference to this cursor. michael@0: ByteCursor &Skip(size_t size) { michael@0: if (CheckAvailable(size)) michael@0: here_ += size; michael@0: return *this; michael@0: } michael@0: michael@0: private: michael@0: // If there are at least SIZE bytes available to read from the buffer, michael@0: // return true. Otherwise, set here_ to the end of the buffer, set michael@0: // complete_ to false, and return false. michael@0: bool CheckAvailable(size_t size) { michael@0: if (Available() >= size) { michael@0: return true; michael@0: } else { michael@0: here_ = buffer_->end; michael@0: complete_ = false; michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // The buffer we're reading bytes from. michael@0: const ByteBuffer *buffer_; michael@0: michael@0: // The next byte within buffer_ that we'll read. michael@0: const uint8_t *here_; michael@0: michael@0: // True if we should read numbers in big-endian form; false if we michael@0: // should read in little-endian form. michael@0: bool big_endian_; michael@0: michael@0: // True if we've been able to read all we've been asked to. michael@0: bool complete_; michael@0: }; michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #endif // COMMON_BYTE_CURSOR_H_