/thirdparty/breakpad/common/dwarf/bytereader-inl.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 175 lines · 114 code · 29 blank · 32 comment · 13 complexity · d66adffc093fb0996f85cbc1788b1e08 MD5 · raw file

  1. // Copyright 2006 Google Inc. All Rights Reserved.
  2. //
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions are
  5. // met:
  6. //
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above
  10. // copyright notice, this list of conditions and the following disclaimer
  11. // in the documentation and/or other materials provided with the
  12. // distribution.
  13. // * Neither the name of Google Inc. nor the names of its
  14. // contributors may be used to endorse or promote products derived from
  15. // this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. #ifndef UTIL_DEBUGINFO_BYTEREADER_INL_H__
  29. #define UTIL_DEBUGINFO_BYTEREADER_INL_H__
  30. #include "common/dwarf/bytereader.h"
  31. #include <assert.h>
  32. namespace dwarf2reader {
  33. inline uint8 ByteReader::ReadOneByte(const char* buffer) const {
  34. return buffer[0];
  35. }
  36. inline uint16 ByteReader::ReadTwoBytes(const char* signed_buffer) const {
  37. const unsigned char *buffer
  38. = reinterpret_cast<const unsigned char *>(signed_buffer);
  39. const uint16 buffer0 = buffer[0];
  40. const uint16 buffer1 = buffer[1];
  41. if (endian_ == ENDIANNESS_LITTLE) {
  42. return buffer0 | buffer1 << 8;
  43. } else {
  44. return buffer1 | buffer0 << 8;
  45. }
  46. }
  47. inline uint64 ByteReader::ReadFourBytes(const char* signed_buffer) const {
  48. const unsigned char *buffer
  49. = reinterpret_cast<const unsigned char *>(signed_buffer);
  50. const uint32 buffer0 = buffer[0];
  51. const uint32 buffer1 = buffer[1];
  52. const uint32 buffer2 = buffer[2];
  53. const uint32 buffer3 = buffer[3];
  54. if (endian_ == ENDIANNESS_LITTLE) {
  55. return buffer0 | buffer1 << 8 | buffer2 << 16 | buffer3 << 24;
  56. } else {
  57. return buffer3 | buffer2 << 8 | buffer1 << 16 | buffer0 << 24;
  58. }
  59. }
  60. inline uint64 ByteReader::ReadEightBytes(const char* signed_buffer) const {
  61. const unsigned char *buffer
  62. = reinterpret_cast<const unsigned char *>(signed_buffer);
  63. const uint64 buffer0 = buffer[0];
  64. const uint64 buffer1 = buffer[1];
  65. const uint64 buffer2 = buffer[2];
  66. const uint64 buffer3 = buffer[3];
  67. const uint64 buffer4 = buffer[4];
  68. const uint64 buffer5 = buffer[5];
  69. const uint64 buffer6 = buffer[6];
  70. const uint64 buffer7 = buffer[7];
  71. if (endian_ == ENDIANNESS_LITTLE) {
  72. return buffer0 | buffer1 << 8 | buffer2 << 16 | buffer3 << 24 |
  73. buffer4 << 32 | buffer5 << 40 | buffer6 << 48 | buffer7 << 56;
  74. } else {
  75. return buffer7 | buffer6 << 8 | buffer5 << 16 | buffer4 << 24 |
  76. buffer3 << 32 | buffer2 << 40 | buffer1 << 48 | buffer0 << 56;
  77. }
  78. }
  79. // Read an unsigned LEB128 number. Each byte contains 7 bits of
  80. // information, plus one bit saying whether the number continues or
  81. // not.
  82. inline uint64 ByteReader::ReadUnsignedLEB128(const char* buffer,
  83. size_t* len) const {
  84. uint64 result = 0;
  85. size_t num_read = 0;
  86. unsigned int shift = 0;
  87. unsigned char byte;
  88. do {
  89. byte = *buffer++;
  90. num_read++;
  91. result |= (static_cast<uint64>(byte & 0x7f)) << shift;
  92. shift += 7;
  93. } while (byte & 0x80);
  94. *len = num_read;
  95. return result;
  96. }
  97. // Read a signed LEB128 number. These are like regular LEB128
  98. // numbers, except the last byte may have a sign bit set.
  99. inline int64 ByteReader::ReadSignedLEB128(const char* buffer,
  100. size_t* len) const {
  101. int64 result = 0;
  102. unsigned int shift = 0;
  103. size_t num_read = 0;
  104. unsigned char byte;
  105. do {
  106. byte = *buffer++;
  107. num_read++;
  108. result |= (static_cast<uint64>(byte & 0x7f) << shift);
  109. shift += 7;
  110. } while (byte & 0x80);
  111. if ((shift < 8 * sizeof (result)) && (byte & 0x40))
  112. result |= -((static_cast<int64>(1)) << shift);
  113. *len = num_read;
  114. return result;
  115. }
  116. inline uint64 ByteReader::ReadOffset(const char* buffer) const {
  117. assert(this->offset_reader_);
  118. return (this->*offset_reader_)(buffer);
  119. }
  120. inline uint64 ByteReader::ReadAddress(const char* buffer) const {
  121. assert(this->address_reader_);
  122. return (this->*address_reader_)(buffer);
  123. }
  124. inline void ByteReader::SetCFIDataBase(uint64 section_base,
  125. const char *buffer_base) {
  126. section_base_ = section_base;
  127. buffer_base_ = buffer_base;
  128. have_section_base_ = true;
  129. }
  130. inline void ByteReader::SetTextBase(uint64 text_base) {
  131. text_base_ = text_base;
  132. have_text_base_ = true;
  133. }
  134. inline void ByteReader::SetDataBase(uint64 data_base) {
  135. data_base_ = data_base;
  136. have_data_base_ = true;
  137. }
  138. inline void ByteReader::SetFunctionBase(uint64 function_base) {
  139. function_base_ = function_base;
  140. have_function_base_ = true;
  141. }
  142. inline void ByteReader::ClearFunctionBase() {
  143. have_function_base_ = false;
  144. }
  145. } // namespace dwarf2reader
  146. #endif // UTIL_DEBUGINFO_BYTEREADER_INL_H__