CInputStream.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. auto readSize = read(data.get(), getSize());
  37. assert(readSize == getSize());
  38. MAYBE_UNUSED(readSize);
  39. return std::make_pair(std::move(data), getSize());
  40. }
  41. /**
  42. * @brief calculateCRC32 calculates CRC32 checksum for the whole file
  43. * @return calculated checksum
  44. */
  45. virtual ui32 calculateCRC32()
  46. {
  47. si64 originalPos = tell();
  48. boost::crc_32_type checksum;
  49. auto data = readAll();
  50. checksum.process_bytes(reinterpret_cast<const void *>(data.first.get()), data.second);
  51. seek(originalPos);
  52. return checksum.checksum();
  53. }
  54. };
  55. VCMI_LIB_NAMESPACE_END