Open Source Tomb Raider Engine
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

binary.cpp 5.5KB

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