]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_decoder.c
Oh well, big messy commit again. Some highlights:
[icculus/xz.git] / src / liblzma / common / block_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       block_decoder.c
4 /// \brief      Decodes .lzma Blocks
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "block_decoder.h"
21 #include "filter_decoder.h"
22 #include "check.h"
23
24
25 struct lzma_coder_s {
26         enum {
27                 SEQ_CODE,
28                 SEQ_PADDING,
29                 SEQ_CHECK,
30         } sequence;
31
32         /// The filters in the chain; initialized with lzma_raw_decoder_init().
33         lzma_next_coder next;
34
35         /// Decoding options; we also write Compressed Size and Uncompressed
36         /// Size back to this structure when the decoding has been finished.
37         lzma_block *options;
38
39         /// Compressed Size calculated while decoding
40         lzma_vli compressed_size;
41
42         /// Uncompressed Size calculated while decoding
43         lzma_vli uncompressed_size;
44
45         /// Maximum allowed Compressed Size; this takes into account the
46         /// size of the Block Header and Check fields when Compressed Size
47         /// is unknown.
48         lzma_vli compressed_limit;
49
50         /// Position when reading the Check field
51         size_t check_pos;
52
53         /// Check of the uncompressed data
54         lzma_check_state check;
55 };
56
57
58 static inline bool
59 update_size(lzma_vli *size, lzma_vli add, lzma_vli limit)
60 {
61         if (limit > LZMA_VLI_MAX)
62                 limit = LZMA_VLI_MAX;
63
64         if (limit < *size || limit - *size < add)
65                 return true;
66
67         *size += add;
68
69         return false;
70 }
71
72
73 static inline bool
74 is_size_valid(lzma_vli size, lzma_vli reference)
75 {
76         return reference == LZMA_VLI_UNKNOWN || reference == size;
77 }
78
79
80 static lzma_ret
81 block_decode(lzma_coder *coder, lzma_allocator *allocator,
82                 const uint8_t *restrict in, size_t *restrict in_pos,
83                 size_t in_size, uint8_t *restrict out,
84                 size_t *restrict out_pos, size_t out_size, lzma_action action)
85 {
86         switch (coder->sequence) {
87         case SEQ_CODE: {
88                 const size_t in_start = *in_pos;
89                 const size_t out_start = *out_pos;
90
91                 const lzma_ret ret = coder->next.code(coder->next.coder,
92                                 allocator, in, in_pos, in_size,
93                                 out, out_pos, out_size, action);
94
95                 const size_t in_used = *in_pos - in_start;
96                 const size_t out_used = *out_pos - out_start;
97
98                 // NOTE: We compare to compressed_limit here, which prevents
99                 // the total size of the Block growing past LZMA_VLI_MAX.
100                 if (update_size(&coder->compressed_size, in_used,
101                                         coder->compressed_limit)
102                                 || update_size(&coder->uncompressed_size,
103                                         out_used,
104                                         coder->options->uncompressed_size))
105                         return LZMA_DATA_ERROR;
106
107                 lzma_check_update(&coder->check, coder->options->check,
108                                 out + out_start, out_used);
109
110                 if (ret != LZMA_STREAM_END)
111                         return ret;
112
113                 // Compressed and Uncompressed Sizes are now at their final
114                 // values. Verify that they match the values given to us.
115                 if (!is_size_valid(coder->compressed_size,
116                                         coder->options->compressed_size)
117                                 || !is_size_valid(coder->uncompressed_size,
118                                         coder->options->uncompressed_size))
119                         return LZMA_DATA_ERROR;
120
121                 // Copy the values into coder->options. The caller
122                 // may use this information to construct Index.
123                 coder->options->compressed_size = coder->compressed_size;
124                 coder->options->uncompressed_size = coder->uncompressed_size;
125
126                 coder->sequence = SEQ_PADDING;
127         }
128
129         // Fall through
130
131         case SEQ_PADDING:
132                 // Compressed Data is padded to a multiple of four bytes.
133                 while (coder->compressed_size & 3) {
134                         // We use compressed_size here just get the Padding
135                         // right. The actual Compressed Size was stored to
136                         // coder->options already, and won't be modified by
137                         // us anymore.
138                         ++coder->compressed_size;
139
140                         if (*in_pos >= in_size)
141                                 return LZMA_OK;
142
143                         if (in[(*in_pos)++] != 0x00)
144                                 return LZMA_DATA_ERROR;
145                 }
146
147                 if (coder->options->check == LZMA_CHECK_NONE)
148                         return LZMA_STREAM_END;
149
150                 lzma_check_finish(&coder->check, coder->options->check);
151                 coder->sequence = SEQ_CHECK;
152
153         // Fall through
154
155         case SEQ_CHECK: {
156                 const bool chksup = lzma_check_is_supported(
157                                 coder->options->check);
158
159                 while (*in_pos < in_size) {
160                         // coder->check.buffer[] may be uninitialized when
161                         // the Check ID is not supported.
162                         if (chksup && coder->check.buffer.u8[coder->check_pos]
163                                         != in[*in_pos]) {
164                                 ++*in_pos;
165                                 return LZMA_DATA_ERROR;
166                         }
167
168                         ++*in_pos;
169
170                         if (++coder->check_pos == lzma_check_size(
171                                         coder->options->check))
172                                 return LZMA_STREAM_END;
173                 }
174
175                 return LZMA_OK;
176         }
177         }
178
179         return LZMA_PROG_ERROR;
180 }
181
182
183 static void
184 block_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
185 {
186         lzma_next_end(&coder->next, allocator);
187         lzma_free(coder, allocator);
188         return;
189 }
190
191
192 extern lzma_ret
193 lzma_block_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
194                 lzma_block *options)
195 {
196         lzma_next_coder_init(lzma_block_decoder_init, next, allocator);
197
198         // Validate the options. lzma_block_unpadded_size() does that for us
199         // except for Uncompressed Size and filters. Filters are validated
200         // by the raw decoder.
201         if (lzma_block_unpadded_size(options) == 0
202                         || !lzma_vli_is_valid(options->uncompressed_size))
203                 return LZMA_PROG_ERROR;
204
205         // Allocate and initialize *next->coder if needed.
206         if (next->coder == NULL) {
207                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
208                 if (next->coder == NULL)
209                         return LZMA_MEM_ERROR;
210
211                 next->code = &block_decode;
212                 next->end = &block_decoder_end;
213                 next->coder->next = LZMA_NEXT_CODER_INIT;
214         }
215
216         // Basic initializations
217         next->coder->sequence = SEQ_CODE;
218         next->coder->options = options;
219         next->coder->compressed_size = 0;
220         next->coder->uncompressed_size = 0;
221
222         // If Compressed Size is not known, we calculate the maximum allowed
223         // value so that encoded size of the Block (including Block Padding)
224         // is still a valid VLI and a multiple of four.
225         next->coder->compressed_limit
226                         = options->compressed_size == LZMA_VLI_UNKNOWN
227                                 ? (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
228                                         - options->header_size
229                                         - lzma_check_size(options->check)
230                                 : options->compressed_size;
231
232         // Initialize the check. It's caller's problem if the Check ID is not
233         // supported, and the Block decoder cannot verify the Check field.
234         // Caller can test lzma_checks[options->check].
235         next->coder->check_pos = 0;
236         lzma_check_init(&next->coder->check, options->check);
237
238         // Initialize the filter chain.
239         return lzma_raw_decoder_init(&next->coder->next, allocator,
240                         options->filters);
241 }
242
243
244 extern LZMA_API lzma_ret
245 lzma_block_decoder(lzma_stream *strm, lzma_block *options)
246 {
247         lzma_next_strm_init(lzma_block_decoder_init, strm, options);
248
249         strm->internal->supported_actions[LZMA_RUN] = true;
250         strm->internal->supported_actions[LZMA_FINISH] = true;
251
252         return LZMA_OK;
253 }