lz.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /**
  25. * MiniLZO compressing stream.
  26. */
  27. class TLzoCompress: public IOutputStream {
  28. public:
  29. TLzoCompress(IOutputStream* slave, ui16 maxBlockSize = 1 << 15);
  30. ~TLzoCompress() override;
  31. private:
  32. void DoWrite(const void* buf, size_t len) override;
  33. void DoFlush() override;
  34. void DoFinish() override;
  35. private:
  36. class TImpl;
  37. THolder<TImpl> Impl_;
  38. };
  39. /**
  40. * MiniLZO decompressing stream.
  41. */
  42. class TLzoDecompress: public IInputStream {
  43. public:
  44. TLzoDecompress(IInputStream* slave);
  45. ~TLzoDecompress() override;
  46. private:
  47. size_t DoRead(void* buf, size_t len) override;
  48. private:
  49. class TImpl;
  50. THolder<TImpl> Impl_;
  51. };
  52. /**
  53. * FastLZ compressing stream.
  54. */
  55. class TLzfCompress: public IOutputStream {
  56. public:
  57. TLzfCompress(IOutputStream* slave, ui16 maxBlockSize = 1 << 15);
  58. ~TLzfCompress() override;
  59. private:
  60. void DoWrite(const void* buf, size_t len) override;
  61. void DoFlush() override;
  62. void DoFinish() override;
  63. private:
  64. class TImpl;
  65. THolder<TImpl> Impl_;
  66. };
  67. /**
  68. * FastLZ decompressing stream.
  69. */
  70. class TLzfDecompress: public IInputStream {
  71. public:
  72. TLzfDecompress(IInputStream* slave);
  73. ~TLzfDecompress() override;
  74. private:
  75. size_t DoRead(void* buf, size_t len) override;
  76. private:
  77. class TImpl;
  78. THolder<TImpl> Impl_;
  79. };
  80. /**
  81. * QuickLZ compressing stream.
  82. */
  83. class TLzqCompress: public IOutputStream {
  84. public:
  85. enum EVersion {
  86. V_1_31 = 0,
  87. V_1_40 = 1,
  88. V_1_51 = 2
  89. };
  90. /*
  91. * streaming mode - actually, backlog size
  92. */
  93. enum EMode {
  94. M_0 = 0,
  95. M_100000 = 1,
  96. M_1000000 = 2
  97. };
  98. TLzqCompress(IOutputStream* slave, ui16 maxBlockSize = 1 << 15,
  99. EVersion ver = V_1_31,
  100. unsigned level = 0,
  101. EMode mode = M_0);
  102. ~TLzqCompress() override;
  103. private:
  104. void DoWrite(const void* buf, size_t len) override;
  105. void DoFlush() override;
  106. void DoFinish() override;
  107. private:
  108. class TImpl;
  109. THolder<TImpl> Impl_;
  110. };
  111. /**
  112. * QuickLZ decompressing stream.
  113. */
  114. class TLzqDecompress: public IInputStream {
  115. public:
  116. TLzqDecompress(IInputStream* slave);
  117. ~TLzqDecompress() override;
  118. private:
  119. size_t DoRead(void* buf, size_t len) override;
  120. private:
  121. class TImpl;
  122. THolder<TImpl> Impl_;
  123. };
  124. /** @} */
  125. /**
  126. * Reads a compression signature from the provided input stream and returns a
  127. * corresponding decompressing stream.
  128. *
  129. * Note that returned stream doesn't own the provided input stream, thus it's
  130. * up to the user to free them both.
  131. *
  132. * @param input Stream to decompress.
  133. * @return Decompressing proxy input stream.
  134. */
  135. TAutoPtr<IInputStream> OpenLzDecompressor(IInputStream* input);
  136. TAutoPtr<IInputStream> TryOpenLzDecompressor(IInputStream* input);
  137. TAutoPtr<IInputStream> TryOpenLzDecompressor(const TStringBuf& signature, IInputStream* input);
  138. TAutoPtr<IInputStream> OpenOwnedLzDecompressor(TAutoPtr<IInputStream> input);
  139. TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(TAutoPtr<IInputStream> input);
  140. TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(const TStringBuf& signature, TAutoPtr<IInputStream> input);