lzip_decoder.c 11 KB

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