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