BinaryReader.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <bit>
  2. #include <iostream>
  3. #include "BinaryReader.h"
  4. #include <cstring>
  5. using namespace WallpaperEngine::Data::Utils;
  6. BinaryReader::BinaryReader (ReadStreamSharedPtr file) : m_input (std::move (file)) { }
  7. // a read that comes up short leaves zeros for the missing bytes (and the stream's failbit for callers that look)
  8. uint32_t BinaryReader::nextUInt32 () const {
  9. char buffer[4] = {};
  10. this->m_input->read (buffer, 4);
  11. if constexpr (std::endian::native == std::endian::little) {
  12. return (buffer[3] & 0xFF) << 24 | (buffer[2] & 0xFF) << 16 | (buffer[1] & 0xFF) << 8 | (buffer[0] & 0xFF);
  13. } else {
  14. return (buffer[0] & 0xFF) << 24 | (buffer[1] & 0xFF) << 16 | (buffer[2] & 0xFF) << 8 | (buffer[3] & 0xFF);
  15. }
  16. }
  17. int BinaryReader::nextInt () const {
  18. char buffer[4] = {};
  19. this->m_input->read (buffer, 4);
  20. if constexpr (std::endian::native == std::endian::little) {
  21. return (buffer[3] & 0xFF) << 24 | (buffer[2] & 0xFF) << 16 | (buffer[1] & 0xFF) << 8 | (buffer[0] & 0xFF);
  22. } else {
  23. return (buffer[0] & 0xFF) << 24 | (buffer[1] & 0xFF) << 16 | (buffer[2] & 0xFF) << 8 | (buffer[3] & 0xFF);
  24. }
  25. }
  26. float BinaryReader::nextFloat () const {
  27. float result = 0.0f;
  28. static_assert (std::endian::native == std::endian::little, "Only little endian is supported for floats");
  29. this->m_input->read (reinterpret_cast<char*> (&result), sizeof (result));
  30. return result;
  31. }
  32. std::string BinaryReader::nextNullTerminatedString () const {
  33. std::string output;
  34. for (char c = this->next (); this->m_input->good () && c != '\0'; c = this->next ()) {
  35. output += c;
  36. }
  37. return output;
  38. }
  39. std::string BinaryReader::nextSizedString () const {
  40. const uint32_t length = this->nextUInt32 ();
  41. // a length the file can't hold would allocate it (up to 4 GB) for nothing
  42. if (length > this->remaining ()) {
  43. this->m_input->setstate (std::ios::failbit);
  44. return {};
  45. }
  46. std::string output (length, '\0');
  47. this->m_input->read (output.data (), length);
  48. return output;
  49. }
  50. void BinaryReader::next (char* out, size_t size) const {
  51. this->m_input->read (out, static_cast<std::streamsize> (size));
  52. if (const auto got = static_cast<size_t> (this->m_input->gcount ()); got < size) {
  53. memset (out + got, 0, size - got);
  54. }
  55. }
  56. char BinaryReader::next () const {
  57. char buffer = 0;
  58. this->m_input->read (&buffer, 1);
  59. return buffer;
  60. }
  61. std::istream& BinaryReader::base () const { return *this->m_input; }
  62. uint64_t BinaryReader::size () const {
  63. if (this->m_size < 0) {
  64. const auto here = this->m_input->tellg ();
  65. if (here < 0) {
  66. return 0;
  67. }
  68. // relative moves only, MemoryStream has no absolute seek
  69. this->m_input->seekg (0, std::ios::end);
  70. const auto end = this->m_input->tellg ();
  71. this->m_input->seekg (here - end, std::ios::cur);
  72. this->m_size = static_cast<int64_t> (end);
  73. }
  74. return static_cast<uint64_t> (this->m_size);
  75. }
  76. uint64_t BinaryReader::remaining () const {
  77. const auto here = this->m_input->tellg ();
  78. if (here < 0) {
  79. return 0;
  80. }
  81. const uint64_t total = this->size ();
  82. return total > static_cast<uint64_t> (here) ? total - static_cast<uint64_t> (here) : 0;
  83. }
  84. bool BinaryReader::fits (const uint64_t count, const uint64_t recordBytes) const {
  85. if (count == 0 || recordBytes == 0) {
  86. return true;
  87. }
  88. // count * recordBytes without wrapping
  89. return count <= this->remaining () / recordBytes;
  90. }