decompress_unxz.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "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_unaligned16(buf, SWAP_LE32(val))
  29. #define put_unaligned_be32(val, buf) move_to_unaligned16(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(int src_fd, int dst_fd)
  35. {
  36. struct xz_buf iobuf;
  37. struct xz_dec *state;
  38. unsigned char *membuf;
  39. IF_DESKTOP(long long) int total = 0;
  40. if (!global_crc32_table)
  41. global_crc32_table = crc32_filltable(NULL, /*endian:*/ 0);
  42. memset(&iobuf, 0, sizeof(iobuf));
  43. /* Preload XZ file signature */
  44. membuf = (void*) strcpy(xmalloc(2 * BUFSIZ), HEADER_MAGIC);
  45. iobuf.in = membuf;
  46. iobuf.in_size = HEADER_MAGIC_SIZE;
  47. iobuf.out = membuf + BUFSIZ;
  48. iobuf.out_size = BUFSIZ;
  49. /* Limit memory usage to about 64 MiB. */
  50. state = xz_dec_init(XZ_DYNALLOC, 64*1024*1024);
  51. while (1) {
  52. enum xz_ret r;
  53. if (iobuf.in_pos == iobuf.in_size) {
  54. int rd = safe_read(src_fd, membuf, BUFSIZ);
  55. if (rd < 0) {
  56. bb_error_msg(bb_msg_read_error);
  57. total = -1;
  58. break;
  59. }
  60. iobuf.in_size = rd;
  61. iobuf.in_pos = 0;
  62. }
  63. // bb_error_msg(">in pos:%d size:%d out pos:%d size:%d",
  64. // iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size);
  65. r = xz_dec_run(state, &iobuf);
  66. // bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
  67. // iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, r);
  68. if (iobuf.out_pos) {
  69. xwrite(dst_fd, iobuf.out, iobuf.out_pos);
  70. IF_DESKTOP(total += iobuf.out_pos;)
  71. iobuf.out_pos = 0;
  72. }
  73. if (r == XZ_STREAM_END) {
  74. break;
  75. }
  76. if (r != XZ_OK && r != XZ_UNSUPPORTED_CHECK) {
  77. bb_error_msg("corrupted data");
  78. total = -1;
  79. break;
  80. }
  81. }
  82. xz_dec_end(state);
  83. free(membuf);
  84. return total;
  85. }