2
0

CCompressedStream.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * CCompressedStream.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 "CInputStream.h"
  12. struct z_stream_s;
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. /// Abstract class that provides buffer for one-directional input streams (e.g. compressed data)
  15. /// Used for zip archives support and in .lod deflate compression
  16. class CBufferedStream : public CInputStream
  17. {
  18. public:
  19. CBufferedStream();
  20. /**
  21. * Reads n bytes from the stream into the data buffer.
  22. *
  23. * @param data A pointer to the destination data array.
  24. * @param size The number of bytes to read.
  25. * @return the number of bytes read actually.
  26. *
  27. * @throws std::runtime_error if the file decompression was not successful
  28. */
  29. si64 read(ui8 * data, si64 size) override;
  30. /**
  31. * Seeks the internal read pointer to the specified position.
  32. * This will cause decompressing data till this position is found
  33. *
  34. * @param position The read position from the beginning.
  35. * @return the position actually moved to, -1 on error.
  36. */
  37. si64 seek(si64 position) override;
  38. /**
  39. * Gets the current read position in the stream.
  40. *
  41. * @return the read position.
  42. */
  43. si64 tell() override;
  44. /**
  45. * Skips delta numbers of bytes.
  46. *
  47. * @param delta The count of bytes to skip.
  48. * @return the count of bytes skipped actually.
  49. */
  50. si64 skip(si64 delta) override;
  51. /**
  52. * Gets the length in bytes of the stream.
  53. * Causes complete data decompression
  54. *
  55. * @return the length in bytes of the stream.
  56. */
  57. si64 getSize() override;
  58. protected:
  59. /**
  60. * @brief virtual method to get more data into the buffer
  61. * @return amount of bytes actually read, non-positive values are considered to be end-of-file mark
  62. */
  63. virtual si64 readMore(ui8 * data, si64 size) = 0;
  64. /// resets all internal state
  65. void reset();
  66. private:
  67. /// ensures that buffer contains at lest size of bytes. Calls readMore() to fill remaining part
  68. void ensureSize(si64 size);
  69. /** buffer with already decompressed data */
  70. std::vector<ui8> buffer;
  71. /** Current read position */
  72. si64 position;
  73. bool endOfFileReached;
  74. };
  75. /**
  76. * A class which provides method definitions for reading a gzip-compressed file
  77. * This class implements lazy loading - data will be decompressed (and cached) only by request
  78. */
  79. class DLL_LINKAGE CCompressedStream : public CBufferedStream
  80. {
  81. public:
  82. /**
  83. * C-tor.
  84. *
  85. * @param stream - stream with compresed data
  86. * @param gzip - this is gzipp'ed file e.g. campaign or maps, false for files in lod
  87. * @param decompressedSize - optional parameter to hint size of decompressed data
  88. */
  89. CCompressedStream(std::unique_ptr<CInputStream> stream, bool gzip, size_t decompressedSize=0);
  90. ~CCompressedStream();
  91. /**
  92. * Prepare stream for decompression of next block (e.g. next part of h3c)
  93. * Applicable only for streams that contain multiple concatenated compressed data
  94. *
  95. * @return false if next block was not found, true othervice
  96. */
  97. bool getNextBlock();
  98. private:
  99. /**
  100. * Decompresses data to ensure that buffer has newSize bytes or end of stream was reached
  101. */
  102. si64 readMore(ui8 * data, si64 size) override;
  103. /** The file stream with compressed data. */
  104. std::unique_ptr<CInputStream> gzipStream;
  105. /** buffer with not yet decompressed data*/
  106. std::vector<ui8> compressedBuffer;
  107. /** struct with current zlib inflate state */
  108. z_stream_s * inflateState;
  109. enum EState
  110. {
  111. ERROR_OCCURED,
  112. INITIALIZED,
  113. IN_PROGRESS,
  114. STREAM_END,
  115. FINISHED
  116. };
  117. };
  118. VCMI_LIB_NAMESPACE_END