lzip_decoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file lzip_decoder.c
  5. /// \brief Decodes .lz (lzip) files
  6. //
  7. // Author: Michał Górny
  8. // Lasse Collin
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "lzip_decoder.h"
  12. #include "lzma_decoder.h"
  13. #include "check.h"
  14. // .lz format version 0 lacks the 64-bit Member size field in the footer.
  15. #define LZIP_V0_FOOTER_SIZE 12
  16. #define LZIP_V1_FOOTER_SIZE 20
  17. #define LZIP_FOOTER_SIZE_MAX LZIP_V1_FOOTER_SIZE
  18. // lc/lp/pb are hardcoded in the .lz format.
  19. #define LZIP_LC 3
  20. #define LZIP_LP 0
  21. #define LZIP_PB 2
  22. typedef struct {
  23. enum {
  24. SEQ_ID_STRING,
  25. SEQ_VERSION,
  26. SEQ_DICT_SIZE,
  27. SEQ_CODER_INIT,
  28. SEQ_LZMA_STREAM,
  29. SEQ_MEMBER_FOOTER,
  30. } sequence;
  31. /// .lz member format version
  32. uint32_t version;
  33. /// CRC32 of the uncompressed data in the .lz member
  34. uint32_t crc32;
  35. /// Uncompressed size of the .lz member
  36. uint64_t uncompressed_size;
  37. /// Compressed size of the .lz member
  38. uint64_t member_size;
  39. /// Memory usage limit
  40. uint64_t memlimit;
  41. /// Amount of memory actually needed
  42. uint64_t memusage;
  43. /// If true, LZMA_GET_CHECK is returned after decoding the header
  44. /// fields. As all files use CRC32 this is redundant but it's
  45. /// implemented anyway since the initialization functions supports
  46. /// all other flags in addition to LZMA_TELL_ANY_CHECK.
  47. bool tell_any_check;
  48. /// If true, we won't calculate or verify the CRC32 of
  49. /// the uncompressed data.
  50. bool ignore_check;
  51. /// If true, we will decode concatenated .lz members and stop if
  52. /// non-.lz data is seen after at least one member has been
  53. /// successfully decoded.
  54. bool concatenated;
  55. /// When decoding concatenated .lz members, this is true as long as
  56. /// we are decoding the first .lz member. This is needed to avoid
  57. /// incorrect LZMA_FORMAT_ERROR in case there is non-.lz data at
  58. /// the end of the file.
  59. bool first_member;
  60. /// Reading position in the header and footer fields
  61. size_t pos;
  62. /// Buffer to hold the .lz footer fields
  63. uint8_t buffer[LZIP_FOOTER_SIZE_MAX];
  64. /// Options decoded from the .lz header that needed to initialize
  65. /// the LZMA1 decoder.
  66. lzma_options_lzma options;
  67. /// LZMA1 decoder
  68. lzma_next_coder lzma_decoder;
  69. } lzma_lzip_coder;
  70. static lzma_ret
  71. lzip_decode(void *coder_ptr, const lzma_allocator *allocator,
  72. const uint8_t *restrict in, size_t *restrict in_pos,
  73. size_t in_size, uint8_t *restrict out,
  74. size_t *restrict out_pos, size_t out_size, lzma_action action)
  75. {
  76. lzma_lzip_coder *coder = coder_ptr;
  77. while (true)
  78. switch (coder->sequence) {
  79. case SEQ_ID_STRING: {
  80. // The "ID string" or magic bytes are "LZIP" in US-ASCII.
  81. const uint8_t lzip_id_string[4] = { 0x4C, 0x5A, 0x49, 0x50 };
  82. while (coder->pos < sizeof(lzip_id_string)) {
  83. if (*in_pos >= in_size) {
  84. // If we are on the 2nd+ concatenated member
  85. // and the input ends before we can read
  86. // the magic bytes, we discard the bytes that
  87. // were already read (up to 3) and finish.
  88. // See the reasoning below.
  89. return !coder->first_member
  90. && action == LZMA_FINISH
  91. ? LZMA_STREAM_END : LZMA_OK;
  92. }
  93. if (in[*in_pos] != lzip_id_string[coder->pos]) {
  94. // The .lz format allows putting non-.lz data
  95. // at the end of the file. If we have seen
  96. // at least one valid .lz member already,
  97. // then we won't consume the byte at *in_pos
  98. // and will return LZMA_STREAM_END. This way
  99. // apps can easily locate and read the non-.lz
  100. // data after the .lz member(s).
  101. //
  102. // NOTE: If the first 1-3 bytes of the non-.lz
  103. // data match the .lz ID string then the first
  104. // 1-3 bytes of the junk will get ignored by
  105. // us. If apps want to properly locate the
  106. // trailing data they must ensure that the
  107. // first byte of their custom data isn't the
  108. // same as the first byte of .lz ID string.
  109. // With the liblzma API we cannot rewind the
  110. // input position across calls to lzma_code().
  111. return !coder->first_member
  112. ? LZMA_STREAM_END : LZMA_FORMAT_ERROR;
  113. }
  114. ++*in_pos;
  115. ++coder->pos;
  116. }
  117. coder->pos = 0;
  118. coder->crc32 = 0;
  119. coder->uncompressed_size = 0;
  120. coder->member_size = sizeof(lzip_id_string);
  121. coder->sequence = SEQ_VERSION;
  122. }
  123. // Fall through
  124. case SEQ_VERSION:
  125. if (*in_pos >= in_size)
  126. return LZMA_OK;
  127. coder->version = in[(*in_pos)++];
  128. // We support version 0 and unextended version 1.
  129. if (coder->version > 1)
  130. return LZMA_OPTIONS_ERROR;
  131. ++coder->member_size;
  132. coder->sequence = SEQ_DICT_SIZE;
  133. // .lz versions 0 and 1 use CRC32 as the integrity check
  134. // so if the application wanted to know that
  135. // (LZMA_TELL_ANY_CHECK) we can tell it now.
  136. if (coder->tell_any_check)
  137. return LZMA_GET_CHECK;
  138. // Fall through
  139. case SEQ_DICT_SIZE: {
  140. if (*in_pos >= in_size)
  141. return LZMA_OK;
  142. const uint32_t ds = in[(*in_pos)++];
  143. ++coder->member_size;
  144. // The five lowest bits are for the base-2 logarithm of
  145. // the dictionary size and the highest three bits are
  146. // the fractional part (0/16 to 7/16) that will be
  147. // subtracted to get the final value.
  148. //
  149. // For example, with 0xB5:
  150. // b2log = 21
  151. // fracnum = 5
  152. // dict_size = 2^21 - 2^21 * 5 / 16 = 1408 KiB
  153. const uint32_t b2log = ds & 0x1F;
  154. const uint32_t fracnum = ds >> 5;
  155. // The format versions 0 and 1 allow dictionary size in the
  156. // range [4 KiB, 512 MiB].
  157. if (b2log < 12 || b2log > 29 || (b2log == 12 && fracnum > 0))
  158. return LZMA_DATA_ERROR;
  159. // 2^[b2log] - 2^[b2log] * [fracnum] / 16
  160. // = 2^[b2log] - [fracnum] * 2^([b2log] - 4)
  161. coder->options.dict_size = (UINT32_C(1) << b2log)
  162. - (fracnum << (b2log - 4));
  163. assert(coder->options.dict_size >= 4096);
  164. assert(coder->options.dict_size <= (UINT32_C(512) << 20));
  165. coder->options.preset_dict = NULL;
  166. coder->options.lc = LZIP_LC;
  167. coder->options.lp = LZIP_LP;
  168. coder->options.pb = LZIP_PB;
  169. // Calculate the memory usage.
  170. coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
  171. + LZMA_MEMUSAGE_BASE;
  172. // Initialization is a separate step because if we return
  173. // LZMA_MEMLIMIT_ERROR we need to be able to restart after
  174. // the memlimit has been increased.
  175. coder->sequence = SEQ_CODER_INIT;
  176. }
  177. // Fall through
  178. case SEQ_CODER_INIT: {
  179. if (coder->memusage > coder->memlimit)
  180. return LZMA_MEMLIMIT_ERROR;
  181. const lzma_filter_info filters[2] = {
  182. {
  183. .id = LZMA_FILTER_LZMA1,
  184. .init = &lzma_lzma_decoder_init,
  185. .options = &coder->options,
  186. }, {
  187. .init = NULL,
  188. }
  189. };
  190. return_if_error(lzma_next_filter_init(&coder->lzma_decoder,
  191. allocator, filters));
  192. coder->crc32 = 0;
  193. coder->sequence = SEQ_LZMA_STREAM;
  194. }
  195. // Fall through
  196. case SEQ_LZMA_STREAM: {
  197. const size_t in_start = *in_pos;
  198. const size_t out_start = *out_pos;
  199. const lzma_ret ret = coder->lzma_decoder.code(
  200. coder->lzma_decoder.coder, allocator,
  201. in, in_pos, in_size, out, out_pos, out_size,
  202. action);
  203. const size_t out_used = *out_pos - out_start;
  204. coder->member_size += *in_pos - in_start;
  205. coder->uncompressed_size += out_used;
  206. // Don't update the CRC32 if the integrity check will be
  207. // ignored or if there was no new output. The latter is
  208. // important in case out == NULL to avoid null pointer + 0
  209. // which is undefined behavior.
  210. if (!coder->ignore_check && out_used > 0)
  211. coder->crc32 = lzma_crc32(out + out_start, out_used,
  212. coder->crc32);
  213. if (ret != LZMA_STREAM_END)
  214. return ret;
  215. coder->sequence = SEQ_MEMBER_FOOTER;
  216. }
  217. // Fall through
  218. case SEQ_MEMBER_FOOTER: {
  219. // The footer of .lz version 0 lacks the Member size field.
  220. // This is the only difference between version 0 and
  221. // unextended version 1 formats.
  222. const size_t footer_size = coder->version == 0
  223. ? LZIP_V0_FOOTER_SIZE
  224. : LZIP_V1_FOOTER_SIZE;
  225. // Copy the CRC32, Data size, and Member size fields to
  226. // the internal buffer.
  227. lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
  228. footer_size);
  229. // Return if we didn't get the whole footer yet.
  230. if (coder->pos < footer_size)
  231. return LZMA_OK;
  232. coder->pos = 0;
  233. coder->member_size += footer_size;
  234. // Check that the footer fields match the observed data.
  235. if (!coder->ignore_check
  236. && coder->crc32 != read32le(&coder->buffer[0]))
  237. return LZMA_DATA_ERROR;
  238. if (coder->uncompressed_size != read64le(&coder->buffer[4]))
  239. return LZMA_DATA_ERROR;
  240. if (coder->version > 0) {
  241. // .lz version 0 has no Member size field.
  242. if (coder->member_size != read64le(&coder->buffer[12]))
  243. return LZMA_DATA_ERROR;
  244. }
  245. // Decoding is finished if we weren't requested to decode
  246. // more than one .lz member.
  247. if (!coder->concatenated)
  248. return LZMA_STREAM_END;
  249. coder->first_member = false;
  250. coder->sequence = SEQ_ID_STRING;
  251. break;
  252. }
  253. default:
  254. assert(0);
  255. return LZMA_PROG_ERROR;
  256. }
  257. // Never reached
  258. }
  259. static void
  260. lzip_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
  261. {
  262. lzma_lzip_coder *coder = coder_ptr;
  263. lzma_next_end(&coder->lzma_decoder, allocator);
  264. lzma_free(coder, allocator);
  265. return;
  266. }
  267. static lzma_check
  268. lzip_decoder_get_check(const void *coder_ptr lzma_attribute((__unused__)))
  269. {
  270. return LZMA_CHECK_CRC32;
  271. }
  272. static lzma_ret
  273. lzip_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
  274. uint64_t *old_memlimit, uint64_t new_memlimit)
  275. {
  276. lzma_lzip_coder *coder = coder_ptr;
  277. *memusage = coder->memusage;
  278. *old_memlimit = coder->memlimit;
  279. if (new_memlimit != 0) {
  280. if (new_memlimit < coder->memusage)
  281. return LZMA_MEMLIMIT_ERROR;
  282. coder->memlimit = new_memlimit;
  283. }
  284. return LZMA_OK;
  285. }
  286. extern lzma_ret
  287. lzma_lzip_decoder_init(
  288. lzma_next_coder *next, const lzma_allocator *allocator,
  289. uint64_t memlimit, uint32_t flags)
  290. {
  291. lzma_next_coder_init(&lzma_lzip_decoder_init, next, allocator);
  292. if (flags & ~LZMA_SUPPORTED_FLAGS)
  293. return LZMA_OPTIONS_ERROR;
  294. lzma_lzip_coder *coder = next->coder;
  295. if (coder == NULL) {
  296. coder = lzma_alloc(sizeof(lzma_lzip_coder), allocator);
  297. if (coder == NULL)
  298. return LZMA_MEM_ERROR;
  299. next->coder = coder;
  300. next->code = &lzip_decode;
  301. next->end = &lzip_decoder_end;
  302. next->get_check = &lzip_decoder_get_check;
  303. next->memconfig = &lzip_decoder_memconfig;
  304. coder->lzma_decoder = LZMA_NEXT_CODER_INIT;
  305. }
  306. coder->sequence = SEQ_ID_STRING;
  307. coder->memlimit = my_max(1, memlimit);
  308. coder->memusage = LZMA_MEMUSAGE_BASE;
  309. coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
  310. coder->ignore_check = (flags & LZMA_IGNORE_CHECK) != 0;
  311. coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
  312. coder->first_member = true;
  313. coder->pos = 0;
  314. return LZMA_OK;
  315. }
  316. extern LZMA_API(lzma_ret)
  317. lzma_lzip_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
  318. {
  319. lzma_next_strm_init(lzma_lzip_decoder_init, strm, memlimit, flags);
  320. strm->internal->supported_actions[LZMA_RUN] = true;
  321. strm->internal->supported_actions[LZMA_FINISH] = true;
  322. return LZMA_OK;
  323. }