lz.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "lz.h"
  2. #include <util/system/yassert.h>
  3. #include <util/system/byteorder.h>
  4. #include <util/memory/addstorage.h>
  5. #include <util/generic/buffer.h>
  6. #include <util/generic/utility.h>
  7. #include <util/generic/singleton.h>
  8. #include <util/generic/yexception.h>
  9. #include <util/stream/mem.h>
  10. #include <library/cpp/streams/lz/common/compressor.h>
  11. #include <library/cpp/streams/lz/lz4/block.h>
  12. #include <library/cpp/streams/lz/snappy/block.h>
  13. #include <contrib/libs/fastlz/fastlz.h>
  14. #ifndef OPENSOURCE
  15. #include "minilzo.h"
  16. #include "quicklz.h"
  17. #endif
  18. /*
  19. * FastLZ
  20. */
  21. class TFastLZ {
  22. public:
  23. static const char signature[];
  24. static inline size_t Hint(size_t len) noexcept {
  25. return Max<size_t>((size_t)(len * 1.06), 100);
  26. }
  27. inline size_t Compress(const char* data, size_t len, char* ptr, size_t /*dstMaxSize*/) {
  28. return fastlz_compress(data, len, ptr);
  29. }
  30. inline size_t Decompress(const char* data, size_t len, char* ptr, size_t max) {
  31. return fastlz_decompress(data, len, ptr, max);
  32. }
  33. inline void InitFromStream(IInputStream*) const noexcept {
  34. }
  35. static inline bool SaveIncompressibleChunks() noexcept {
  36. return false;
  37. }
  38. };
  39. const char TFastLZ::signature[] = "YLZF";
  40. DEF_COMPRESSOR(TLzfCompress, TFastLZ)
  41. DEF_DECOMPRESSOR(TLzfDecompress, TFastLZ)
  42. template <class T>
  43. static TAutoPtr<IInputStream> TryOpenLzDecompressorX(const TDecompressSignature& s, T input) {
  44. if (s.Check<TLZ4>())
  45. return new TLzDecompressInput<T, TLZ4>(input);
  46. if (s.Check<TSnappy>())
  47. return new TLzDecompressInput<T, TSnappy>(input);
  48. #ifndef OPENSOURCE
  49. if (auto result = TryOpenMiniLzoDecompressor(s, input))
  50. return result;
  51. #endif
  52. if (s.Check<TFastLZ>())
  53. return new TLzDecompressInput<T, TFastLZ>(input);
  54. #ifndef OPENSOURCE
  55. if (auto result = TryOpenQuickLzDecompressor(s, input))
  56. return result;
  57. #endif
  58. return nullptr;
  59. }
  60. template <class T>
  61. static inline TAutoPtr<IInputStream> TryOpenLzDecompressorImpl(const TStringBuf& signature, T input) {
  62. if (signature.size() == SIGNATURE_SIZE) {
  63. TMemoryInput mem(signature.data(), signature.size());
  64. TDecompressSignature s(&mem);
  65. return TryOpenLzDecompressorX(s, input);
  66. }
  67. return nullptr;
  68. }
  69. template <class T>
  70. static inline TAutoPtr<IInputStream> TryOpenLzDecompressorImpl(T input) {
  71. TDecompressSignature s(&*input);
  72. return TryOpenLzDecompressorX(s, input);
  73. }
  74. template <class T>
  75. static inline TAutoPtr<IInputStream> OpenLzDecompressorImpl(T input) {
  76. TAutoPtr<IInputStream> ret = TryOpenLzDecompressorImpl(input);
  77. if (!ret) {
  78. ythrow TDecompressorError() << "Unknown compression format";
  79. }
  80. return ret;
  81. }
  82. TAutoPtr<IInputStream> OpenLzDecompressor(IInputStream* input) {
  83. return OpenLzDecompressorImpl(input);
  84. }
  85. TAutoPtr<IInputStream> TryOpenLzDecompressor(IInputStream* input) {
  86. return TryOpenLzDecompressorImpl(input);
  87. }
  88. TAutoPtr<IInputStream> TryOpenLzDecompressor(const TStringBuf& signature, IInputStream* input) {
  89. return TryOpenLzDecompressorImpl(signature, input);
  90. }
  91. TAutoPtr<IInputStream> OpenOwnedLzDecompressor(TAutoPtr<IInputStream> input) {
  92. return OpenLzDecompressorImpl(input);
  93. }
  94. TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(TAutoPtr<IInputStream> input) {
  95. return TryOpenLzDecompressorImpl(input);
  96. }
  97. TAutoPtr<IInputStream> TryOpenOwnedLzDecompressor(const TStringBuf& signature, TAutoPtr<IInputStream> input) {
  98. return TryOpenLzDecompressorImpl(signature, input);
  99. }