3
0

decompress_unxz.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_simple_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. /* There is more data, but is it XZ data?
  88. * Example: dpkg-deb -f busybox_1.30.1-4_amd64.deb
  89. * reads control.tar.xz "control" file
  90. * inside the ar archive, but tar.xz
  91. * extraction code reaches end of xz data,
  92. * reached this code and reads the beginning
  93. * of data.tar.xz's ar header, which isn't xz data,
  94. * and prints "corrupted data".
  95. * The correct solution is to not read
  96. * past nested archive (to simulate EOF).
  97. * This is a workaround:
  98. */
  99. if (membuf[iobuf.in_pos] != 0xfd) {
  100. /* It's definitely not a xz signature
  101. * (which is 0xfd,"7zXZ",0x00).
  102. */
  103. goto end;
  104. }
  105. xz_dec_reset(state);
  106. goto do_run;
  107. }
  108. iobuf.in_pos++;
  109. } while (iobuf.in_pos < iobuf.in_size);
  110. }
  111. do_run:
  112. // bb_error_msg(">in pos:%d size:%d out pos:%d size:%d",
  113. // iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size);
  114. xz_result = xz_dec_run(state, &iobuf);
  115. // bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
  116. // iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, xz_result);
  117. if (iobuf.out_pos) {
  118. xtransformer_write(xstate, iobuf.out, iobuf.out_pos);
  119. IF_DESKTOP(total += iobuf.out_pos;)
  120. iobuf.out_pos = 0;
  121. }
  122. if (xz_result == XZ_STREAM_END) {
  123. /*
  124. * Can just "break;" here, if not for concatenated
  125. * .xz streams.
  126. * Checking for padding may require buffer
  127. * replenishment. Can't do it here.
  128. */
  129. continue;
  130. }
  131. if (xz_result != XZ_OK && xz_result != XZ_UNSUPPORTED_CHECK) {
  132. bb_simple_error_msg("corrupted data");
  133. total = -1;
  134. break;
  135. }
  136. }
  137. end:
  138. xz_dec_end(state);
  139. free(membuf);
  140. return total;
  141. }