BinaryReader.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "stdafx.h"
  2. #include "BinaryReader.h"
  3. BinaryReader::BinaryReader(FILE* file)
  4. : file(file)
  5. {
  6. }
  7. uint8_t BinaryReader::ReadByte()
  8. {
  9. int value = fgetc(file);
  10. if (value == EOF)
  11. {
  12. throw exception("Error reading file.");
  13. }
  14. return (uint8_t)value;
  15. }
  16. uint16_t BinaryReader::ReadUInt16()
  17. {
  18. uint8_t b1 = ReadByte();
  19. uint8_t b2 = ReadByte();
  20. return uint16_t(b1) |
  21. uint16_t(b2) << 8;
  22. }
  23. uint32_t BinaryReader::ReadUInt32()
  24. {
  25. uint8_t b1 = ReadByte();
  26. uint8_t b2 = ReadByte();
  27. uint8_t b3 = ReadByte();
  28. uint8_t b4 = ReadByte();
  29. return uint32_t(b1) |
  30. uint32_t(b2) << 8 |
  31. uint32_t(b3) << 16 |
  32. uint32_t(b4) << 24;
  33. }
  34. uint64_t BinaryReader::ReadUInt64()
  35. {
  36. uint8_t b1 = ReadByte();
  37. uint8_t b2 = ReadByte();
  38. uint8_t b3 = ReadByte();
  39. uint8_t b4 = ReadByte();
  40. uint8_t b5 = ReadByte();
  41. uint8_t b6 = ReadByte();
  42. uint8_t b7 = ReadByte();
  43. uint8_t b8 = ReadByte();
  44. return uint64_t(b1) |
  45. uint64_t(b2) << 8 |
  46. uint64_t(b3) << 16 |
  47. uint64_t(b4) << 24 |
  48. uint64_t(b5) << 32 |
  49. uint64_t(b6) << 40 |
  50. uint64_t(b7) << 48 |
  51. uint64_t(b8) << 56;
  52. }
  53. int8_t BinaryReader::ReadSByte()
  54. {
  55. return (int8_t)ReadByte();
  56. }
  57. int16_t BinaryReader::ReadInt16()
  58. {
  59. return (int16_t)ReadUInt16();
  60. }
  61. int32_t BinaryReader::ReadInt32()
  62. {
  63. return (int32_t)ReadUInt32();
  64. }
  65. int64_t BinaryReader::ReadInt64()
  66. {
  67. return (int64_t)ReadUInt64();
  68. }
  69. float BinaryReader::ReadSingle()
  70. {
  71. uint32_t value = ReadUInt32();
  72. return *(float*)&value;
  73. }
  74. double BinaryReader::ReadDouble()
  75. {
  76. uint64_t value = ReadUInt64();
  77. return *(double*)&value;
  78. }
  79. bool BinaryReader::ReadBoolean()
  80. {
  81. return ReadByte() ? true : false;
  82. }
  83. wchar_t BinaryReader::ReadChar()
  84. {
  85. wchar_t result = ReadByte();
  86. // Decode UTF-8.
  87. if (result & 0x80)
  88. {
  89. int byteCount = 1;
  90. while (result & (0x80 >> byteCount))
  91. {
  92. byteCount++;
  93. }
  94. result &= (1 << (8 - byteCount)) - 1;
  95. while (--byteCount)
  96. {
  97. result <<= 6;
  98. result |= ReadByte() & 0x3F;
  99. }
  100. }
  101. return result;
  102. }
  103. wstring BinaryReader::ReadString()
  104. {
  105. uint32_t stringLength = Read7BitEncodedInt();
  106. uint32_t endOfString = FilePosition() + stringLength;
  107. wstring result;
  108. while (FilePosition() < endOfString)
  109. {
  110. result += ReadChar();
  111. }
  112. return result;
  113. }
  114. uint32_t BinaryReader::Read7BitEncodedInt()
  115. {
  116. uint32_t result = 0;
  117. uint32_t bitsRead = 0;
  118. uint32_t value;
  119. do
  120. {
  121. value = ReadByte();
  122. result |= (value & 0x7f) << bitsRead;
  123. bitsRead += 7;
  124. }
  125. while (value & 0x80);
  126. return result;
  127. }
  128. vector<uint8_t> BinaryReader::ReadBytes(uint32_t count)
  129. {
  130. vector<uint8_t> result;
  131. result.reserve(count);
  132. while (count--)
  133. {
  134. result.push_back(ReadByte());
  135. }
  136. return result;
  137. }
  138. uint32_t BinaryReader::FilePosition()
  139. {
  140. return ftell(file);
  141. }
  142. uint32_t BinaryReader::FileSize()
  143. {
  144. uint32_t currentPosition = FilePosition();
  145. // Seek to the end of the file.
  146. if (fseek(file, 0, SEEK_END) != 0)
  147. {
  148. throw exception("Seek failed.");
  149. }
  150. // Query the file size.
  151. uint32_t size = FilePosition();
  152. // Restore the original position.
  153. if (fseek(file, currentPosition, SEEK_SET) != 0)
  154. {
  155. throw exception("Seek failed.");
  156. }
  157. return size;
  158. }