2
0

CInputStream.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * CInputStream.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "CStream.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. /**
  14. * Abstract class which provides method definitions for reading from a stream.
  15. */
  16. class DLL_LINKAGE CInputStream : public virtual CStream
  17. {
  18. public:
  19. /**
  20. * Reads n bytes from the stream into the data buffer.
  21. *
  22. * @param data A pointer to the destination data array.
  23. * @param size The number of bytes to read.
  24. * @return the number of bytes read actually.
  25. */
  26. virtual si64 read(ui8 * data, si64 size) = 0;
  27. /**
  28. * @brief for convenience, reads whole stream at once
  29. *
  30. * @return pair, first = raw data, second = size of data
  31. */
  32. std::pair<std::unique_ptr<ui8[]>, si64> readAll()
  33. {
  34. std::unique_ptr<ui8[]> data(new ui8[getSize()]);
  35. seek(0);
  36. [[maybe_unused]] auto readSize = read(data.get(), getSize());
  37. assert(readSize == getSize());
  38. return std::make_pair(std::move(data), getSize());
  39. }
  40. /**
  41. * @brief calculateCRC32 calculates CRC32 checksum for the whole file
  42. * @return calculated checksum
  43. */
  44. virtual ui32 calculateCRC32()
  45. {
  46. si64 originalPos = tell();
  47. boost::crc_32_type checksum;
  48. auto data = readAll();
  49. checksum.process_bytes(reinterpret_cast<const void *>(data.first.get()), data.second);
  50. seek(originalPos);
  51. return checksum.checksum();
  52. }
  53. };
  54. VCMI_LIB_NAMESPACE_END