]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_decoder.c
Some API changes, bug fixes, cleanups etc.
[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 encoding has been finished.
37         lzma_block *options;
38
39         /// Compressed Size calculated while encoding
40         lzma_vli compressed_size;
41
42         /// Uncompressed Size calculated while encoding
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                 coder->sequence = SEQ_PADDING;
114         }
115
116         // Fall through
117
118         case SEQ_PADDING:
119                 // Compressed Data is padded to a multiple of four bytes.
120                 while (coder->compressed_size & 3) {
121                         if (*in_pos >= in_size)
122                                 return LZMA_OK;
123
124                         if (in[(*in_pos)++] != 0x00)
125                                 return LZMA_DATA_ERROR;
126
127                         if (update_size(&coder->compressed_size, 1,
128                                         coder->compressed_limit))
129                                 return LZMA_DATA_ERROR;
130                 }
131
132                 // Compressed and Uncompressed Sizes are now at their final
133                 // values. Verify that they match the values given to us.
134                 if (!is_size_valid(coder->compressed_size,
135                                         coder->options->compressed_size)
136                                 || !is_size_valid(coder->uncompressed_size,
137                                         coder->options->uncompressed_size))
138                         return LZMA_DATA_ERROR;
139
140                 // Copy the values into coder->options. The caller
141                 // may use this information to construct Index.
142                 coder->options->compressed_size = coder->compressed_size;
143                 coder->options->uncompressed_size = coder->uncompressed_size;
144
145                 if (coder->options->check == LZMA_CHECK_NONE)
146                         return LZMA_STREAM_END;
147
148                 lzma_check_finish(&coder->check, coder->options->check);
149                 coder->sequence = SEQ_CHECK;
150
151         // Fall through
152
153         case SEQ_CHECK: {
154                 const bool chksup = lzma_check_is_supported(
155                                 coder->options->check);
156
157                 while (*in_pos < in_size) {
158                         // coder->check.buffer[] may be uninitialized when
159                         // the Check ID is not supported.
160                         if (chksup && coder->check.buffer.u8[coder->check_pos]
161                                         != in[*in_pos]) {
162                                 ++*in_pos;
163                                 return LZMA_DATA_ERROR;
164                         }
165
166                         ++*in_pos;
167
168                         if (++coder->check_pos == lzma_check_size(
169                                         coder->options->check))
170                                 return LZMA_STREAM_END;
171                 }
172
173                 return LZMA_OK;
174         }
175         }
176
177         return LZMA_PROG_ERROR;
178 }
179
180
181 static void
182 block_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
183 {
184         lzma_next_end(&coder->next, allocator);
185         lzma_free(coder, allocator);
186         return;
187 }
188
189
190 extern lzma_ret
191 lzma_block_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
192                 lzma_block *options)
193 {
194         lzma_next_coder_init(lzma_block_decoder_init, next, allocator);
195
196         // While lzma_block_total_size_get() is meant to calculate the Total
197         // Size, it also validates the options excluding the filters.
198         if (lzma_block_total_size_get(options) == 0)
199                 return LZMA_PROG_ERROR;
200
201         // options->check is used for array indexing so we need to know that
202         // it is in the valid range.
203         if ((unsigned)(options->check) > LZMA_CHECK_ID_MAX)
204                 return LZMA_PROG_ERROR;
205
206         // Allocate and initialize *next->coder if needed.
207         if (next->coder == NULL) {
208                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
209                 if (next->coder == NULL)
210                         return LZMA_MEM_ERROR;
211
212                 next->code = &block_decode;
213                 next->end = &block_decoder_end;
214                 next->coder->next = LZMA_NEXT_CODER_INIT;
215         }
216
217         // Basic initializations
218         next->coder->sequence = SEQ_CODE;
219         next->coder->options = options;
220         next->coder->compressed_size = 0;
221         next->coder->uncompressed_size = 0;
222
223         // If Compressed Size is not known, we calculate the maximum allowed
224         // value so that Total Size of the Block still is a valid VLI and
225         // a multiple of four.
226         next->coder->compressed_limit
227                         = options->compressed_size == LZMA_VLI_UNKNOWN
228                                 ? (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
229                                         - options->header_size
230                                         - lzma_check_size(options->check)
231                                 : options->compressed_size;
232
233         // Initialize the check. It's caller's problem if the Check ID is not
234         // supported, and the Block decoder cannot verify the Check field.
235         // Caller can test lzma_checks[options->check].
236         next->coder->check_pos = 0;
237         lzma_check_init(&next->coder->check, options->check);
238
239         // Initialize the filter chain.
240         return lzma_raw_decoder_init(&next->coder->next, allocator,
241                         options->filters);
242 }
243
244
245 extern LZMA_API lzma_ret
246 lzma_block_decoder(lzma_stream *strm, lzma_block *options)
247 {
248         lzma_next_strm_init(lzma_block_decoder_init, strm, options);
249
250         strm->internal->supported_actions[LZMA_RUN] = true;
251         strm->internal->supported_actions[LZMA_FINISH] = true;
252
253         return LZMA_OK;
254 }