2
0

CInputStream.h 1.5 KB

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