Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

BinReader.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "BinReader.h"
  19. #include "Core.h"
  20. #include "Log.h"
  21. BinReader::BinReader(const void* data, size_t size) : data_{data}, size_{size}, position_{0}
  22. {}
  23. const uint8_t* BinReader::readRaw(size_t size)
  24. {
  25. assert(size <= remaining());
  26. const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data_) + position_;
  27. position_ += size;
  28. return ptr;
  29. }
  30. uint8_t BinReader::readByte()
  31. {
  32. return *readPointer<uint8_t>();
  33. }
  34. uint16_t BinReader::readShort()
  35. {
  36. return *readPointer<uint16_t>();
  37. }
  38. uint32_t BinReader::readInt()
  39. {
  40. return *readPointer<uint32_t>();
  41. }
  42. uint64_t BinReader::readLong()
  43. {
  44. return *readPointer<uint64_t>();
  45. }
  46. float BinReader::readFloat()
  47. {
  48. return *readPointer<float>();
  49. }
  50. double BinReader::readDouble()
  51. {
  52. return *readPointer<double>();
  53. }
  54. uint16_t BinReader::readPackedShort()
  55. {
  56. uint16_t val = readByte();
  57. if(val & 0x80)
  58. {
  59. val = (val & 0x7F) << 8 | readByte();
  60. }
  61. return val;
  62. }
  63. uint32_t BinReader::readPackedInt()
  64. {
  65. uint32_t val = readShort();
  66. if(val & 0x8000)
  67. {
  68. val = (val & 0x7FFF) << 16 | readShort();
  69. }
  70. return val;
  71. }
  72. string BinReader::readString()
  73. {
  74. uint32_t count = readShort();
  75. if(count == 0xFFFF)
  76. {
  77. count = readInt();
  78. }
  79. const uint8_t* data = readRaw(count);
  80. align();
  81. return string(data, data + count);
  82. }
  83. void BinReader::align()
  84. {
  85. position_ = (position_ + 3) & ~3;
  86. }
  87. void BinReader::dump(size_t maxLen) const
  88. {
  89. const size_t kBytesPerLine = 32;
  90. string hexPart;
  91. string asciiPart;
  92. for(size_t i = 0; i < maxLen && position_ + i < size_; i++)
  93. {
  94. if(i != 0 && i % kBytesPerLine == 0)
  95. {
  96. LOG(Misc, Debug) << hexPart << " " << asciiPart << "\n";
  97. hexPart.clear();
  98. asciiPart.clear();
  99. }
  100. uint8_t val = reinterpret_cast<const uint8_t*>(data_)[position_ + i];
  101. char buf[4];
  102. sprintf(buf, "%02x ", val);
  103. hexPart += buf;
  104. asciiPart += (isprint(val) ? val : '.');
  105. }
  106. if(!asciiPart.empty())
  107. {
  108. while(asciiPart.size() < kBytesPerLine)
  109. {
  110. hexPart += " ";
  111. asciiPart += ' ';
  112. }
  113. LOG(Misc, Debug) << hexPart << " " << asciiPart << "\n";
  114. }
  115. }
  116. size_t BinReader::position() const
  117. {
  118. return position_;
  119. }
  120. size_t BinReader::remaining() const
  121. {
  122. return size_ - position_;
  123. }