decompress_unxz.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * This file uses XZ Embedded library code which is written
  3. * by Lasse Collin <lasse.collin@tukaani.org>
  4. * and Igor Pavlov <http://7-zip.org/>
  5. *
  6. * See README file in unxz/ directory for more information.
  7. *
  8. * This file is:
  9. * Copyright (C) 2010 Denys Vlasenko <vda.linux@googlemail.com>
  10. * Licensed under GPLv2, see file LICENSE in this source tree.
  11. */
  12. #include "libbb.h"
  13. #include "bb_archive.h"
  14. #define XZ_FUNC FAST_FUNC
  15. #define XZ_EXTERN static
  16. #define XZ_DEC_DYNALLOC
  17. /* Skip check (rather than fail) of unsupported hash functions */
  18. #define XZ_DEC_ANY_CHECK 1
  19. /* We use our own crc32 function */
  20. #define XZ_INTERNAL_CRC32 0
  21. static uint32_t xz_crc32(const uint8_t *buf, size_t size, uint32_t crc)
  22. {
  23. return ~crc32_block_endian0(~crc, buf, size, global_crc32_table);
  24. }
  25. /* We use arch-optimized unaligned fixed-endian accessors.
  26. * They have been moved to libbb (proved to be useful elsewhere as well),
  27. * just check that we have them defined:
  28. */
  29. #if !defined(get_unaligned_le32) \
  30. || !defined(get_unaligned_be32) \
  31. || !defined(put_unaligned_le32) \
  32. || !defined(put_unaligned_be32)
  33. # error get_unaligned_le32 accessors are not defined
  34. #endif
  35. #include "unxz/xz_dec_bcj.c"
  36. #include "unxz/xz_dec_lzma2.c"
  37. #include "unxz/xz_dec_stream.c"
  38. IF_DESKTOP(long long) int FAST_FUNC
  39. unpack_xz_stream(transformer_state_t *xstate)
  40. {
  41. enum xz_ret xz_result;
  42. struct xz_buf iobuf;
  43. struct xz_dec *state;
  44. unsigned char *membuf;
  45. IF_DESKTOP(long long) int total = 0;
  46. if (!global_crc32_table)
  47. global_crc32_new_table_le();
  48. memset(&iobuf, 0, sizeof(iobuf));
  49. membuf = xmalloc(2 * BUFSIZ);
  50. iobuf.in = membuf;
  51. iobuf.out = membuf + BUFSIZ;
  52. iobuf.out_size = BUFSIZ;
  53. if (!xstate || xstate->signature_skipped) {
  54. /* Preload XZ file signature */
  55. strcpy((char*)membuf, HEADER_MAGIC);
  56. iobuf.in_size = HEADER_MAGIC_SIZE;
  57. } /* else: let xz code read & check it */
  58. /* Limit memory usage to about 64 MiB. */
  59. state = xz_dec_init(XZ_DYNALLOC, 64*1024*1024);
  60. xz_result = X_OK;
  61. while (1) {
  62. if (iobuf.in_pos == iobuf.in_size) {
  63. int rd = safe_read(xstate->src_fd, membuf, BUFSIZ);
  64. if (rd < 0) {
  65. bb_error_msg(bb_msg_read_error);
  66. total = -1;
  67. break;
  68. }
  69. if (rd == 0 && xz_result == XZ_STREAM_END)
  70. break;
  71. iobuf.in_size = rd;
  72. iobuf.in_pos = 0;
  73. }
  74. if (xz_result == XZ_STREAM_END) {
  75. /*
  76. * Try to start decoding next concatenated stream.
  77. * Stream padding must always be a multiple of four
  78. * bytes to preserve four-byte alignment. To keep the
  79. * code slightly smaller, we aren't as strict here as
  80. * the .xz spec requires. We just skip all zero-bytes
  81. * without checking the alignment and thus can accept
  82. * files that aren't valid, e.g. the XZ utils test
  83. * files bad-0pad-empty.xz and bad-0catpad-empty.xz.
  84. */
  85. do {
  86. if (membuf[iobuf.in_pos] != 0) {
  87. xz_dec_reset(state);
  88. goto do_run;
  89. }
  90. iobuf.in_pos++;
  91. } while (iobuf.in_pos < iobuf.in_size);
  92. }
  93. do_run:
  94. // bb_error_msg(">in pos:%d size:%d out pos:%d size:%d",
  95. // iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size);
  96. xz_result = xz_dec_run(state, &iobuf);
  97. // bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
  98. // iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, xz_result);
  99. if (iobuf.out_pos) {
  100. xtransformer_write(xstate, iobuf.out, iobuf.out_pos);
  101. IF_DESKTOP(total += iobuf.out_pos;)
  102. iobuf.out_pos = 0;
  103. }
  104. if (xz_result == XZ_STREAM_END) {
  105. /*
  106. * Can just "break;" here, if not for concatenated
  107. * .xz streams.
  108. * Checking for padding may require buffer
  109. * replenishment. Can't do it here.
  110. */
  111. continue;
  112. }
  113. if (xz_result != XZ_OK && xz_result != XZ_UNSUPPORTED_CHECK) {
  114. bb_error_msg("corrupted data");
  115. total = -1;
  116. break;
  117. }
  118. }
  119. xz_dec_end(state);
  120. free(membuf);
  121. return total;
  122. }