lz.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #pragma once
  2. #include <util/stream/output.h>
  3. #include <util/stream/input.h>
  4. #include <util/generic/ptr.h>
  5. #include <util/generic/yexception.h>
  6. #include <library/cpp/streams/lz/common/error.h>
  7. #include <library/cpp/streams/lz/lz4/lz4.h>
  8. #include <library/cpp/streams/lz/snappy/snappy.h>
  9. /**
  10. * @file
  11. *
  12. * All lz compressors compress blocks. `Write` method splits input data into
  13. * blocks, compresses each block and then writes each compressed block to the
  14. * underlying output stream. Thus compression classes are not buffered.
  15. * MaxBlockSize parameter specified max allowed block size.
  16. *
  17. * See http://altdevblogaday.com/2011/04/22/survey-of-fast-compression-algorithms-part-1/
  18. * for some comparisons.
  19. */
  20. /**
  21. * @addtogroup Streams_Archs
  22. * @{
  23. */
  24. #ifndef OPENSOURCE
  25. /**
  26. * MiniLZO compressing stream.
  27. */
  28. class TLzoCompress: public IOutputStream {
  29. public:
  30. TLzoCompress(IOutputStream* slave, ui16 maxBlockSize = 1 << 15);
  31. ~TLzoCompress() override;
  32. private:
  33. void DoWrite(const void* buf, size_t len) override;
  34. void DoFlush() override;
  35. void DoFinish() override;
  36. private:
  37. class TImpl;
  38. THolder<TImpl> Impl_;
  39. };
  40. /**
  41. * MiniLZO decompressing stream.
  42. */
  43. class TLzoDecompress: public IInputStream {
  44. public:
  45. TLzoDecompress(IInputStream* slave);
  46. ~TLzoDecompress() override;
  47. private:
  48. size_t DoRead(void* buf, size_t len) override;
  49. private:
  50. class TImpl;
  51. THolder<TImpl> Impl_;
  52. };
  53. #endif
  54. /**
  55. * FastLZ compressing stream.
  56. */
  57. class TLzfCompress: public IOutputStream {
  58. public:
  59. TLzfCompress(IOutputStream* slave, ui16 maxBlockSize = 1 << 15);
  60. ~TLzfCompress() override;
  61. private:
  62. void DoWrite(const void* buf, size_t len) override;
  63. void DoFlush() override;
  64. void DoFinish() override;
  65. private:
  66. class TImpl;
  67. THolder<TImpl> Impl_;
  68. };
  69. /**
  70. * FastLZ decompressing stream.
  71. */
  72. class TLzfDecompress: public IInputStream {
  73. public:
  74. TLzfDecompress(IInputStream* slave);
  75. ~TLzfDecompress() override;
  76. private:
  77. size_t DoRead(void* buf, size_t len) override;
  78. private:
  79. class TImpl;
  80. THolder<TImpl> Impl_;
  81. };
  82. #ifndef OPENSOURCE
  83. /**
  84. * QuickLZ compressing stream.
  85. */
  86. class TLzqCompress: public IOutputStream {
  87. public:
  88. enum EVersion {
  89. V_1_31 = 0,
  90. V_1_40 = 1,
  91. V_1_51 = 2
  92. };
  93. /*
  94. * streaming mode - actually, backlog size
  95. */
  96. enum EMode {
  97. M_0 = 0,
  98. M_100000 = 1,
  99. M_1000000 = 2
  100. };
  101. TLzqCompress(IOutputStream* slave, ui16 maxBlockSize = 1 << 15,
  102. EVersion ver = V_1_31,
  103. unsigned level = 0,
  104. EMode mode = M_0);
  105. ~TLzqCompress() override;
  106. private:
  107. void DoWrite(const void* buf, size_t len) override;
  108. void DoFlush() override;
  109. void DoFinish() override;
  110. private:
  111. class TImpl;
  112. THolder<TImpl> Impl_;
  113. };
  114. /**
  115. * QuickLZ decompressing stream.
  116. */
  117. class TLzqDecompress: public IInputStream {
  118. public:
  119. TLzqDecompress(IInputStream* slave);
  120. ~TLzqDecompress() override;
  121. private:
  122. size_t DoRead(void* buf, size_t len) override;
  123. private:
  124. class TImpl;
  125. THolder<TImpl> Impl_;
  126. };
  127. #endif
  128. /** @} */
  129. /**
  130. * Reads a compression signature from the provided input stream and returns a
  131. * corresponding decompressing stream.
  132. *
  133. * Note that returned stream doesn't own the provided input stream, thus it's
  134. * up to the user to free them both.
  135. *
  136. * @param input Stream to decompress.
  137. * @return Decompressing proxy input stream.
  138. */
  139. TAutoPtr<IInputStream> OpenLzDecompressor(IInputStream* input);
  140. TAutoPtr<IInputStream> TryOpenLzDecompressor(IInputStream* input);
  141. TAutoPtr<IInputStream> TryOpenLzDecompressor(const TStringBuf& signature, IInputStream* input);
  142. TAutoPtr<IInputStream> OpenOwnedLzDecompressor(TAutoPtr<IInputStream> input);
  143. TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(TAutoPtr<IInputStream> input);
  144. TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(const TStringBuf& signature, TAutoPtr<IInputStream> input);