CBinaryReader.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "StdInc.h"
  2. #include "CBinaryReader.h"
  3. #include <SDL_endian.h>
  4. #include "CInputStream.h"
  5. CBinaryReader::CBinaryReader() : stream(nullptr)
  6. {
  7. }
  8. CBinaryReader::CBinaryReader(CInputStream & stream) : stream(&stream)
  9. {
  10. }
  11. CInputStream * CBinaryReader::getStream()
  12. {
  13. return stream;
  14. }
  15. void CBinaryReader::setStream(CInputStream & stream)
  16. {
  17. this->stream = &stream;
  18. }
  19. si64 CBinaryReader::read(ui8 * data, si64 size)
  20. {
  21. return stream->read(data, size);
  22. }
  23. ui8 CBinaryReader::readUInt8()
  24. {
  25. ui8 val;
  26. si64 b = stream->read(&val, 1);
  27. if(b < 1)
  28. {
  29. throw std::runtime_error(getEndOfStreamExceptionMsg(1));
  30. }
  31. return val;
  32. }
  33. si8 CBinaryReader::readInt8()
  34. {
  35. si8 val;
  36. si64 b = stream->read(reinterpret_cast<ui8 *>(&val), 1);
  37. if(b < 1)
  38. {
  39. throw std::runtime_error(getEndOfStreamExceptionMsg(1));
  40. }
  41. return val;
  42. }
  43. ui16 CBinaryReader::readUInt16()
  44. {
  45. ui16 val;
  46. si64 b = stream->read(reinterpret_cast<ui8 *>(&val), 2);
  47. if(b < 2)
  48. {
  49. throw std::runtime_error(getEndOfStreamExceptionMsg(2));
  50. }
  51. return SDL_SwapLE16(val);
  52. }
  53. si16 CBinaryReader::readInt16()
  54. {
  55. si16 val;
  56. si64 b = stream->read(reinterpret_cast<ui8 *>(&val), 2);
  57. if(b < 2)
  58. {
  59. throw std::runtime_error(getEndOfStreamExceptionMsg(2));
  60. }
  61. return SDL_SwapLE16(val);
  62. }
  63. ui32 CBinaryReader::readUInt32()
  64. {
  65. ui32 val;
  66. si64 b = stream->read(reinterpret_cast<ui8 *>(&val), 4);
  67. if(b < 4)
  68. {
  69. throw std::runtime_error(getEndOfStreamExceptionMsg(4));
  70. }
  71. return SDL_SwapLE32(val);
  72. }
  73. si32 CBinaryReader::readInt32()
  74. {
  75. si32 val;
  76. si64 b = stream->read(reinterpret_cast<ui8 *>(&val), 4);
  77. if(b < 4)
  78. {
  79. throw std::runtime_error(getEndOfStreamExceptionMsg(4));
  80. }
  81. return SDL_SwapLE32(val);
  82. }
  83. ui64 CBinaryReader::readUInt64()
  84. {
  85. ui64 val;
  86. si64 b = stream->read(reinterpret_cast<ui8 *>(&val), 8);
  87. if(b < 8)
  88. {
  89. throw std::runtime_error(getEndOfStreamExceptionMsg(8));
  90. }
  91. return SDL_SwapLE64(val);
  92. }
  93. si64 CBinaryReader::readInt64()
  94. {
  95. si64 val;
  96. si64 b = stream->read(reinterpret_cast<ui8 *>(&val), 8);
  97. if(b < 8)
  98. {
  99. throw std::runtime_error(getEndOfStreamExceptionMsg(8));
  100. }
  101. return SDL_SwapLE64(val);
  102. }
  103. std::string CBinaryReader::getEndOfStreamExceptionMsg(long bytesToRead) const
  104. {
  105. std::stringstream ss;
  106. ss << "The end of the stream was reached unexpectedly. The stream has a length of " << stream->getSize() << " and the current reading position is "
  107. << stream->tell() << ". The client wanted to read " << bytesToRead << " bytes.";
  108. return ss.str();
  109. }