CInputStream.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * D-tor.
  21. */
  22. virtual ~CInputStream() {}
  23. /**
  24. * Reads n bytes from the stream into the data buffer.
  25. *
  26. * @param data A pointer to the destination data array.
  27. * @param size The number of bytes to read.
  28. * @return the number of bytes read actually.
  29. */
  30. virtual si64 read(ui8 * data, si64 size) = 0;
  31. /**
  32. * @brief for convenience, reads whole stream at once
  33. *
  34. * @return pair, first = raw data, second = size of data
  35. */
  36. std::pair<std::unique_ptr<ui8[]>, si64> readAll()
  37. {
  38. std::unique_ptr<ui8[]> data(new ui8[getSize()]);
  39. seek(0);
  40. auto readSize = read(data.get(), getSize());
  41. assert(readSize == getSize());
  42. MAYBE_UNUSED(readSize);
  43. return std::make_pair(std::move(data), getSize());
  44. }
  45. /**
  46. * @brief calculateCRC32 calculates CRC32 checksum for the whole file
  47. * @return calculated checksum
  48. */
  49. virtual ui32 calculateCRC32()
  50. {
  51. si64 originalPos = tell();
  52. boost::crc_32_type checksum;
  53. auto data = readAll();
  54. checksum.process_bytes(reinterpret_cast<const void *>(data.first.get()), data.second);
  55. seek(originalPos);
  56. return checksum.checksum();
  57. }
  58. };
  59. VCMI_LIB_NAMESPACE_END