/thirdparty/breakpad/common/byte_cursor.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 263 lines · 133 code · 30 blank · 100 comment · 27 complexity · 86ae9302358a01ab4a7694ad2677516e MD5 · raw file

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