Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

binary.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*!
  2. * \file src/utils/binary.cpp
  3. * \brief Binary file reading utilities
  4. *
  5. * \author xythobuz
  6. */
  7. #include <sstream>
  8. #include "global.h"
  9. #include "Exception.h"
  10. #include "utils/binary.h"
  11. BinaryReader::~BinaryReader() {
  12. }
  13. uint8_t BinaryReader::readU8() {
  14. uint8_t ret;
  15. char* c = reinterpret_cast<char*>(&ret);
  16. read(c, 1);
  17. return ret;
  18. }
  19. uint16_t BinaryReader::readU16() {
  20. uint16_t a = readU8();
  21. uint16_t b = readU8();
  22. return (a | (b << 8));
  23. }
  24. uint32_t BinaryReader::readU32() {
  25. uint32_t a = readU16();
  26. uint32_t b = readU16();
  27. return (a | (b << 16));
  28. }
  29. uint64_t BinaryReader::readU64() {
  30. uint64_t a = readU32();
  31. uint64_t b = readU32();
  32. return (a | (b << 32));
  33. }
  34. float BinaryReader::readFloat() {
  35. uint32_t val = readU32();
  36. char* a = reinterpret_cast<char*>(&val);
  37. float ret;
  38. char* b = reinterpret_cast<char*>(&ret);
  39. for (int i = 0; i < 4; i++)
  40. b[i] = a[i];
  41. return ret;
  42. }
  43. namespace {
  44. /*! \fixme Left-Shift with signed integer is undefined!
  45. * So we can't use the same method as for unsigned integers.
  46. * Is there a portable way to read multi-byte signed integers,
  47. * without having to detect the endianness at run-time?
  48. */
  49. const int bigendiandetection = 1;
  50. #define ISBIGENDIAN() ((*reinterpret_cast<const char *>(&bigendiandetection)) == 0)
  51. void swapByteOrder(char* d, unsigned int n) {
  52. if (ISBIGENDIAN()) {
  53. for (unsigned int i = 0; i < (n / 2); i++) {
  54. char tmp = d[i];
  55. d[i] = d[n - i - 1];
  56. d[n - i - 1] = tmp;
  57. }
  58. }
  59. }
  60. }
  61. int8_t BinaryReader::read8() {
  62. int8_t ret;
  63. char* p = reinterpret_cast<char*>(&ret);
  64. read(p, sizeof(ret));
  65. return ret;
  66. }
  67. int16_t BinaryReader::read16() {
  68. int16_t ret;
  69. char* p = reinterpret_cast<char*>(&ret);
  70. read(p, sizeof(ret));
  71. swapByteOrder(p, 2);
  72. return ret;
  73. }
  74. int32_t BinaryReader::read32() {
  75. int32_t ret;
  76. char* p = reinterpret_cast<char*>(&ret);
  77. read(p, sizeof(ret));
  78. swapByteOrder(p, 4);
  79. return ret;
  80. }
  81. int64_t BinaryReader::read64() {
  82. int64_t ret;
  83. char* p = reinterpret_cast<char*>(&ret);
  84. read(p, sizeof(ret));
  85. swapByteOrder(p, 8);
  86. return ret;
  87. }
  88. // ----------------------------------------------------------------------------
  89. BinaryFile::BinaryFile(std::string f) {
  90. int r = open(f);
  91. if (r != 0)
  92. throw r;
  93. }
  94. BinaryFile::~BinaryFile() {
  95. if (file.is_open())
  96. file.close();
  97. }
  98. int BinaryFile::open(std::string f) {
  99. if (file.is_open()) {
  100. return 1;
  101. } else {
  102. if (f == "")
  103. return 0;
  104. file.open(f, std::ios_base::in | std::ios_base::binary);
  105. return (file ? 0 : 1);
  106. }
  107. }
  108. long long BinaryFile::tell() {
  109. assert(file.is_open());
  110. return file.tellg();
  111. }
  112. void BinaryFile::seek(long long pos) {
  113. assert(file.is_open());
  114. file.seekg(pos);
  115. }
  116. bool BinaryFile::eof() {
  117. file.peek();
  118. return file.eof();
  119. }
  120. void BinaryFile::read(char* d, int c) {
  121. assert(file.is_open());
  122. assert(file.good());
  123. file.read(d, c);
  124. }
  125. // ----------------------------------------------------------------------------
  126. BinaryMemory::BinaryMemory(const char* d, long long m) : data(nullptr), offset(0), max(-1) {
  127. int r = open(d, m);
  128. if (r != 0)
  129. throw r;
  130. }
  131. BinaryMemory::~BinaryMemory() {
  132. }
  133. int BinaryMemory::open(const char* d, long long m) {
  134. if (data != nullptr)
  135. return 1;
  136. if (d != nullptr) {
  137. data = d;
  138. offset = 0;
  139. max = m;
  140. }
  141. return 0;
  142. }
  143. long long BinaryMemory::tell() {
  144. assert(offset >= 0);
  145. return offset;
  146. }
  147. void BinaryMemory::seek(long long pos) {
  148. assert(pos >= 0);
  149. offset = pos;
  150. }
  151. bool BinaryMemory::eof() {
  152. return (offset > max);
  153. }
  154. void BinaryMemory::read(char* d, int c) {
  155. assert(offset >= 0);
  156. assert(c > 0);
  157. if ((offset + c) > max) {
  158. std::ostringstream ss;
  159. ss << "BinaryMemory read out of bounds ("
  160. << offset << " + " << c << " > " << max;
  161. throw new Exception(ss.str());
  162. }
  163. for (int i = 0; i < c; i++) {
  164. d[i] = data[offset + i];
  165. }
  166. offset += c;
  167. }