decompress_unxz.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 accessors */
  26. #define get_unaligned_le32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_LE32(v); })
  27. #define get_unaligned_be32(buf) ({ uint32_t v; move_from_unaligned32(v, buf); SWAP_BE32(v); })
  28. #define put_unaligned_le32(val, buf) move_to_unaligned32(buf, SWAP_LE32(val))
  29. #define put_unaligned_be32(val, buf) move_to_unaligned32(buf, SWAP_BE32(val))
  30. #include "unxz/xz_dec_bcj.c"
  31. #include "unxz/xz_dec_lzma2.c"
  32. #include "unxz/xz_dec_stream.c"
  33. IF_DESKTOP(long long) int FAST_FUNC
  34. unpack_xz_stream(transformer_aux_data_t *aux, int src_fd, int dst_fd)
  35. {
  36. enum xz_ret xz_result;
  37. struct xz_buf iobuf;
  38. struct xz_dec *state;
  39. unsigned char *membuf;
  40. IF_DESKTOP(long long) int total = 0;
  41. if (!global_crc32_table)
  42. global_crc32_table = crc32_filltable(NULL, /*endian:*/ 0);
  43. memset(&iobuf, 0, sizeof(iobuf));
  44. membuf = xmalloc(2 * BUFSIZ);
  45. iobuf.in = membuf;
  46. iobuf.out = membuf + BUFSIZ;
  47. iobuf.out_size = BUFSIZ;
  48. if (!aux || aux->check_signature == 0) {
  49. /* Preload XZ file signature */
  50. strcpy((char*)membuf, HEADER_MAGIC);
  51. iobuf.in_size = HEADER_MAGIC_SIZE;
  52. } /* else: let xz code read & check it */
  53. /* Limit memory usage to about 64 MiB. */
  54. state = xz_dec_init(XZ_DYNALLOC, 64*1024*1024);
  55. xz_result = X_OK;
  56. while (1) {
  57. if (iobuf.in_pos == iobuf.in_size) {
  58. int rd = safe_read(src_fd, membuf, BUFSIZ);
  59. if (rd < 0) {
  60. bb_error_msg(bb_msg_read_error);
  61. total = -1;
  62. break;
  63. }
  64. if (rd == 0 && xz_result == XZ_STREAM_END)
  65. break;
  66. iobuf.in_size = rd;
  67. iobuf.in_pos = 0;
  68. }
  69. if (xz_result == XZ_STREAM_END) {
  70. /*
  71. * Try to start decoding next concatenated stream.
  72. * Stream padding must always be a multiple of four
  73. * bytes to preserve four-byte alignment. To keep the
  74. * code slightly smaller, we aren't as strict here as
  75. * the .xz spec requires. We just skip all zero-bytes
  76. * without checking the alignment and thus can accept
  77. * files that aren't valid, e.g. the XZ utils test
  78. * files bad-0pad-empty.xz and bad-0catpad-empty.xz.
  79. */
  80. do {
  81. if (membuf[iobuf.in_pos] != 0) {
  82. xz_dec_reset(state);
  83. goto do_run;
  84. }
  85. iobuf.in_pos++;
  86. } while (iobuf.in_pos < iobuf.in_size);
  87. }
  88. do_run:
  89. // bb_error_msg(">in pos:%d size:%d out pos:%d size:%d",
  90. // iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size);
  91. xz_result = xz_dec_run(state, &iobuf);
  92. // bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
  93. // iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, xz_result);
  94. if (iobuf.out_pos) {
  95. xwrite(dst_fd, iobuf.out, iobuf.out_pos);
  96. IF_DESKTOP(total += iobuf.out_pos;)
  97. iobuf.out_pos = 0;
  98. }
  99. if (xz_result == XZ_STREAM_END) {
  100. /*
  101. * Can just "break;" here, if not for concatenated
  102. * .xz streams.
  103. * Checking for padding may require buffer
  104. * replenishment. Can't do it here.
  105. */
  106. continue;
  107. }
  108. if (xz_result != XZ_OK && xz_result != XZ_UNSUPPORTED_CHECK) {
  109. bb_error_msg("corrupted data");
  110. total = -1;
  111. break;
  112. }
  113. }
  114. xz_dec_end(state);
  115. free(membuf);
  116. return total;
  117. }