Move get_unaligned_le32() macros to platform.h
[oweals/busybox.git] / archival / libarchive / decompress_unxz.c
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
25 static 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 #define get_le32(p) (*(uint32_t*)(p))
41
42 #include "unxz/xz_dec_bcj.c"
43 #include "unxz/xz_dec_lzma2.c"
44 #include "unxz/xz_dec_stream.c"
45
46 IF_DESKTOP(long long) int FAST_FUNC
47 unpack_xz_stream(transformer_state_t *xstate)
48 {
49         enum xz_ret xz_result;
50         struct xz_buf iobuf;
51         struct xz_dec *state;
52         unsigned char *membuf;
53         IF_DESKTOP(long long) int total = 0;
54
55         if (!global_crc32_table)
56                 global_crc32_table = crc32_filltable(NULL, /*endian:*/ 0);
57
58         memset(&iobuf, 0, sizeof(iobuf));
59         membuf = xmalloc(2 * BUFSIZ);
60         iobuf.in = membuf;
61         iobuf.out = membuf + BUFSIZ;
62         iobuf.out_size = BUFSIZ;
63
64         if (!xstate || xstate->signature_skipped) {
65                 /* Preload XZ file signature */
66                 strcpy((char*)membuf, HEADER_MAGIC);
67                 iobuf.in_size = HEADER_MAGIC_SIZE;
68         } /* else: let xz code read & check it */
69
70         /* Limit memory usage to about 64 MiB. */
71         state = xz_dec_init(XZ_DYNALLOC, 64*1024*1024);
72
73         xz_result = X_OK;
74         while (1) {
75                 if (iobuf.in_pos == iobuf.in_size) {
76                         int rd = safe_read(xstate->src_fd, membuf, BUFSIZ);
77                         if (rd < 0) {
78                                 bb_error_msg(bb_msg_read_error);
79                                 total = -1;
80                                 break;
81                         }
82                         if (rd == 0 && xz_result == XZ_STREAM_END)
83                                 break;
84                         iobuf.in_size = rd;
85                         iobuf.in_pos = 0;
86                 }
87                 if (xz_result == XZ_STREAM_END) {
88                         /*
89                          * Try to start decoding next concatenated stream.
90                          * Stream padding must always be a multiple of four
91                          * bytes to preserve four-byte alignment. To keep the
92                          * code slightly smaller, we aren't as strict here as
93                          * the .xz spec requires. We just skip all zero-bytes
94                          * without checking the alignment and thus can accept
95                          * files that aren't valid, e.g. the XZ utils test
96                          * files bad-0pad-empty.xz and bad-0catpad-empty.xz.
97                          */
98                         do {
99                                 if (membuf[iobuf.in_pos] != 0) {
100                                         xz_dec_reset(state);
101                                         goto do_run;
102                                 }
103                                 iobuf.in_pos++;
104                         } while (iobuf.in_pos < iobuf.in_size);
105                 }
106  do_run:
107 //              bb_error_msg(">in pos:%d size:%d out pos:%d size:%d",
108 //                              iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size);
109                 xz_result = xz_dec_run(state, &iobuf);
110 //              bb_error_msg("<in pos:%d size:%d out pos:%d size:%d r:%d",
111 //                              iobuf.in_pos, iobuf.in_size, iobuf.out_pos, iobuf.out_size, xz_result);
112                 if (iobuf.out_pos) {
113                         xtransformer_write(xstate, iobuf.out, iobuf.out_pos);
114                         IF_DESKTOP(total += iobuf.out_pos;)
115                         iobuf.out_pos = 0;
116                 }
117                 if (xz_result == XZ_STREAM_END) {
118                         /*
119                          * Can just "break;" here, if not for concatenated
120                          * .xz streams.
121                          * Checking for padding may require buffer
122                          * replenishment. Can't do it here.
123                          */
124                         continue;
125                 }
126                 if (xz_result != XZ_OK && xz_result != XZ_UNSUPPORTED_CHECK) {
127                         bb_error_msg("corrupted data");
128                         total = -1;
129                         break;
130                 }
131         }
132
133         xz_dec_end(state);
134         free(membuf);
135
136         return total;
137 }