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 3.9KB

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