decompress.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "decompress.h"
  2. #include <contrib/libs/lzma/liblzma/api/lzma.h>
  3. #include <util/generic/yexception.h>
  4. #include <util/stream/output.h>
  5. #include <util/stream/str.h>
  6. #include <util/stream/zerocopy.h>
  7. // Based on https://fossies.org/linux/xz/doc/examples/02_decompress.c
  8. ///////////////////////////////////////////////////////////////////////////////
  9. //
  10. /// \file 02_decompress.c
  11. /// \brief Decompress .xz files to stdout
  12. ///
  13. /// Usage: ./02_decompress INPUT_FILES... > OUTFILE
  14. ///
  15. /// Example: ./02_decompress foo.xz bar.xz > foobar
  16. //
  17. // Author: Lasse Collin
  18. //
  19. // This file has been put into the public domain.
  20. // You can do whatever you want with this file.
  21. //
  22. ///////////////////////////////////////////////////////////////////////////////
  23. namespace {
  24. class IInput {
  25. public:
  26. virtual ~IInput() = default;
  27. virtual size_t Next(const ui8*& ptr) = 0;
  28. };
  29. class TCopyInput: public IInput {
  30. public:
  31. TCopyInput(IInputStream* slave)
  32. : Slave_(slave)
  33. {
  34. }
  35. size_t Next(const ui8*& ptr) override {
  36. ptr = Inbuf_;
  37. return Slave_->Read(Inbuf_, sizeof(Inbuf_));
  38. }
  39. private:
  40. IInputStream* Slave_;
  41. ui8 Inbuf_[4096];
  42. };
  43. class TZeroCopy: public IInput {
  44. public:
  45. TZeroCopy(IZeroCopyInput* slave)
  46. : Slave_(slave)
  47. {
  48. }
  49. size_t Next(const ui8*& ptr) override {
  50. return Slave_->Next(&ptr);
  51. }
  52. private:
  53. IZeroCopyInput* Slave_;
  54. };
  55. std::unique_ptr<IInput> createInput(IInputStream* slave) {
  56. return std::make_unique<TCopyInput>(slave);
  57. }
  58. std::unique_ptr<IInput> createInput(IZeroCopyInput* slave) {
  59. return std::make_unique<TZeroCopy>(slave);
  60. }
  61. }
  62. class TUnbufferedXzDecompress::TImpl {
  63. public:
  64. template <class T>
  65. TImpl(T* slave)
  66. : Input_(createInput(slave))
  67. , Strm_(LZMA_STREAM_INIT)
  68. {
  69. TString err;
  70. Y_ENSURE(initDecoder(&Strm_, err),
  71. "Error initializing the decoder: " << err);
  72. Strm_.next_in = NULL;
  73. Strm_.avail_in = 0;
  74. }
  75. ~TImpl() {
  76. // Free the memory allocated for the decoder
  77. lzma_end(&Strm_);
  78. }
  79. size_t DoRead(void* buf, size_t len) {
  80. if (IsOutFinished_) {
  81. return 0;
  82. }
  83. size_t res;
  84. TString err;
  85. Y_ENSURE(decompress(buf, len, res, err),
  86. "lzma decoder error: " << err);
  87. return res;
  88. }
  89. private:
  90. bool decompress(void* buf, size_t len, size_t& outLen, TString& err) {
  91. // When LZMA_CONCATENATED flag was used when initializing the decoder,
  92. // we need to tell lzma_code() when there will be no more input.
  93. // This is done by setting action to LZMA_FINISH instead of LZMA_RUN
  94. // in the same way as it is done when encoding.
  95. //
  96. // When LZMA_CONCATENATED isn't used, there is no need to use
  97. // LZMA_FINISH to tell when all the input has been read, but it
  98. // is still OK to use it if you want. When LZMA_CONCATENATED isn't
  99. // used, the decoder will stop after the first .xz stream. In that
  100. // case some unused data may be left in strm->next_in.
  101. lzma_action action = LZMA_RUN;
  102. Strm_.next_out = (ui8*)buf;
  103. Strm_.avail_out = len;
  104. while (true) {
  105. if (Strm_.avail_in == 0 && !IsInFinished_) {
  106. size_t size = Input_->Next(Strm_.next_in);
  107. if (size == 0) {
  108. IsInFinished_ = true;
  109. } else {
  110. Strm_.avail_in = size;
  111. }
  112. // Once the end of the input file has been reached,
  113. // we need to tell lzma_code() that no more input
  114. // will be coming. As said before, this isn't required
  115. // if the LZMA_CONCATENATED flag isn't used when
  116. // initializing the decoder.
  117. if (IsInFinished_)
  118. action = LZMA_FINISH;
  119. }
  120. lzma_ret ret = lzma_code(&Strm_, action);
  121. if (ret == LZMA_STREAM_END) {
  122. // Once everything has been decoded successfully, the
  123. // return value of lzma_code() will be LZMA_STREAM_END.
  124. //
  125. // It is important to check for LZMA_STREAM_END. Do not
  126. // assume that getting ret != LZMA_OK would mean that
  127. // everything has gone well or that when you aren't
  128. // getting more output it must have successfully
  129. // decoded everything.
  130. IsOutFinished_ = true;
  131. }
  132. if (Strm_.avail_out == 0 || ret == LZMA_STREAM_END) {
  133. outLen = len - Strm_.avail_out;
  134. return true;
  135. }
  136. if (ret != LZMA_OK) {
  137. // It's not LZMA_OK nor LZMA_STREAM_END,
  138. // so it must be an error code. See lzma/base.h
  139. // (src/liblzma/api/lzma/base.h in the source package
  140. // or e.g. /usr/include/lzma/base.h depending on the
  141. // install prefix) for the list and documentation of
  142. // possible values. Many values listen in lzma_ret
  143. // enumeration aren't possible in this example, but
  144. // can be made possible by enabling memory usage limit
  145. // or adding flags to the decoder initialization.
  146. switch (ret) {
  147. case LZMA_MEM_ERROR:
  148. err = "Memory allocation failed";
  149. break;
  150. case LZMA_FORMAT_ERROR:
  151. // .xz magic bytes weren't found.
  152. err = "The input is not in the .xz format";
  153. break;
  154. case LZMA_OPTIONS_ERROR:
  155. // For example, the headers specify a filter
  156. // that isn't supported by this liblzma
  157. // version (or it hasn't been enabled when
  158. // building liblzma, but no-one sane does
  159. // that unless building liblzma for an
  160. // embedded system). Upgrading to a newer
  161. // liblzma might help.
  162. //
  163. // Note that it is unlikely that the file has
  164. // accidentally became corrupt if you get this
  165. // error. The integrity of the .xz headers is
  166. // always verified with a CRC32, so
  167. // unintentionally corrupt files can be
  168. // distinguished from unsupported files.
  169. err = "Unsupported compression options";
  170. break;
  171. case LZMA_DATA_ERROR:
  172. err = "Compressed file is corrupt";
  173. break;
  174. case LZMA_BUF_ERROR:
  175. // Typically this error means that a valid
  176. // file has got truncated, but it might also
  177. // be a damaged part in the file that makes
  178. // the decoder think the file is truncated.
  179. // If you prefer, you can use the same error
  180. // message for this as for LZMA_DATA_ERROR.
  181. err = "Compressed file is truncated or "
  182. "otherwise corrupt";
  183. break;
  184. default:
  185. // This is most likely LZMA_PROG_ERROR.
  186. err = "Unknown error, possibly a bug";
  187. break;
  188. }
  189. TStringOutput out(err);
  190. out << "[" << (int)ret << "]";
  191. return false;
  192. }
  193. }
  194. }
  195. static bool initDecoder(lzma_stream* strm, TString& err) {
  196. // Initialize a .xz decoder. The decoder supports a memory usage limit
  197. // and a set of flags.
  198. //
  199. // The memory usage of the decompressor depends on the settings used
  200. // to compress a .xz file. It can vary from less than a megabyte to
  201. // a few gigabytes, but in practice (at least for now) it rarely
  202. // exceeds 65 MiB because that's how much memory is required to
  203. // decompress files created with "xz -9". Settings requiring more
  204. // memory take extra effort to use and don't (at least for now)
  205. // provide significantly better compression in most cases.
  206. //
  207. // Memory usage limit is useful if it is important that the
  208. // decompressor won't consume gigabytes of memory. The need
  209. // for limiting depends on the application. In this example,
  210. // no memory usage limiting is used. This is done by setting
  211. // the limit to UINT64_MAX.
  212. //
  213. // The .xz format allows concatenating compressed files as is:
  214. //
  215. // echo foo | xz > foobar.xz
  216. // echo bar | xz >> foobar.xz
  217. //
  218. // When decompressing normal standalone .xz files, LZMA_CONCATENATED
  219. // should always be used to support decompression of concatenated
  220. // .xz files. If LZMA_CONCATENATED isn't used, the decoder will stop
  221. // after the first .xz stream. This can be useful when .xz data has
  222. // been embedded inside another file format.
  223. //
  224. // Flags other than LZMA_CONCATENATED are supported too, and can
  225. // be combined with bitwise-or. See lzma/container.h
  226. // (src/liblzma/api/lzma/container.h in the source package or e.g.
  227. // /usr/include/lzma/container.h depending on the install prefix)
  228. // for details.
  229. lzma_ret ret = lzma_auto_decoder(
  230. strm, UINT64_MAX, LZMA_CONCATENATED);
  231. // Return successfully if the initialization went fine.
  232. if (ret == LZMA_OK)
  233. return true;
  234. // Something went wrong. The possible errors are documented in
  235. // lzma/container.h (src/liblzma/api/lzma/container.h in the source
  236. // package or e.g. /usr/include/lzma/container.h depending on the
  237. // install prefix).
  238. //
  239. // Note that LZMA_MEMLIMIT_ERROR is never possible here. If you
  240. // specify a very tiny limit, the error will be delayed until
  241. // the first headers have been parsed by a call to lzma_code().
  242. switch (ret) {
  243. case LZMA_MEM_ERROR:
  244. err = "Memory allocation failed";
  245. break;
  246. case LZMA_OPTIONS_ERROR:
  247. err = "Unsupported decompressor flags";
  248. break;
  249. default:
  250. // This is most likely LZMA_PROG_ERROR indicating a bug in
  251. // this program or in liblzma. It is inconvenient to have a
  252. // separate error message for errors that should be impossible
  253. // to occur, but knowing the error code is important for
  254. // debugging. That's why it is good to print the error code
  255. // at least when there is no good error message to show.
  256. err = "Unknown error, possibly a bug";
  257. break;
  258. }
  259. TStringOutput out(err);
  260. out << "[" << (int)ret << "]";
  261. return false;
  262. }
  263. private:
  264. std::unique_ptr<IInput> Input_;
  265. lzma_stream Strm_;
  266. bool IsInFinished_ = false;
  267. bool IsOutFinished_ = false;
  268. };
  269. TUnbufferedXzDecompress::TUnbufferedXzDecompress(IInputStream* slave)
  270. : Impl_(std::make_unique<TImpl>(slave))
  271. {
  272. }
  273. TUnbufferedXzDecompress::TUnbufferedXzDecompress(IZeroCopyInput* slave)
  274. : Impl_(std::make_unique<TImpl>(slave))
  275. {
  276. }
  277. TUnbufferedXzDecompress::~TUnbufferedXzDecompress() = default;
  278. size_t TUnbufferedXzDecompress::DoRead(void* buf, size_t len) {
  279. return Impl_->DoRead(buf, len);
  280. }