]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_decoder.c
Sort of garbage collection commit. :-| Many things are still
[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 "block_private.h"
22 #include "filter_decoder.h"
23 #include "check.h"
24
25
26 struct lzma_coder_s {
27         enum {
28                 SEQ_CODE,
29                 SEQ_PADDING,
30                 SEQ_CHECK,
31         } sequence;
32
33         /// The filters in the chain; initialized with lzma_raw_decoder_init().
34         lzma_next_coder next;
35
36         /// Decoding options; we also write Compressed Size and Uncompressed
37         /// Size back to this structure when the encoding has been finished.
38         lzma_block *options;
39
40         /// Compressed Size calculated while encoding
41         lzma_vli compressed_size;
42
43         /// Uncompressed Size calculated while encoding
44         lzma_vli uncompressed_size;
45
46         /// Maximum allowed Compressed Size; this takes into account the
47         /// size of the Block Header and Check fields when Compressed Size
48         /// is unknown.
49         lzma_vli compressed_limit;
50
51         /// Position when reading the Check field
52         size_t check_pos;
53
54         /// Check of the uncompressed data
55         lzma_check_state check;
56 };
57
58
59 static lzma_ret
60 block_decode(lzma_coder *coder, lzma_allocator *allocator,
61                 const uint8_t *restrict in, size_t *restrict in_pos,
62                 size_t in_size, uint8_t *restrict out,
63                 size_t *restrict out_pos, size_t out_size, lzma_action action)
64 {
65         switch (coder->sequence) {
66         case SEQ_CODE: {
67                 const size_t in_start = *in_pos;
68                 const size_t out_start = *out_pos;
69
70                 const lzma_ret ret = coder->next.code(coder->next.coder,
71                                 allocator, in, in_pos, in_size,
72                                 out, out_pos, out_size, action);
73
74                 const size_t in_used = *in_pos - in_start;
75                 const size_t out_used = *out_pos - out_start;
76
77                 // NOTE: We compare to compressed_limit here, which prevents
78                 // the total size of the Block growing past LZMA_VLI_VALUE_MAX.
79                 if (update_size(&coder->compressed_size, in_used,
80                                         coder->compressed_limit)
81                                 || update_size(&coder->uncompressed_size,
82                                         out_used,
83                                         coder->options->uncompressed_size))
84                         return LZMA_DATA_ERROR;
85
86                 lzma_check_update(&coder->check, coder->options->check,
87                                 out + out_start, out_used);
88
89                 if (ret != LZMA_STREAM_END)
90                         return ret;
91
92                 coder->sequence = SEQ_PADDING;
93         }
94
95         // Fall through
96
97         case SEQ_PADDING:
98                 // Compressed Data is padded to a multiple of four bytes.
99                 while (coder->compressed_size & 3) {
100                         if (*in_pos >= in_size)
101                                 return LZMA_OK;
102
103                         if (in[(*in_pos)++] != 0x00)
104                                 return LZMA_DATA_ERROR;
105
106                         if (update_size(&coder->compressed_size, 1,
107                                         coder->compressed_limit))
108                                 return LZMA_DATA_ERROR;
109                 }
110
111                 // Compressed and Uncompressed Sizes are now at their final
112                 // values. Verify that they match the values given to us.
113                 if (!is_size_valid(coder->compressed_size,
114                                         coder->options->compressed_size)
115                                 || !is_size_valid(coder->uncompressed_size,
116                                         coder->options->uncompressed_size))
117                         return LZMA_DATA_ERROR;
118
119                 // Copy the values into coder->options. The caller
120                 // may use this information to construct Index.
121                 coder->options->compressed_size = coder->compressed_size;
122                 coder->options->uncompressed_size = coder->uncompressed_size;
123
124                 if (coder->options->check == LZMA_CHECK_NONE)
125                         return LZMA_STREAM_END;
126
127                 lzma_check_finish(&coder->check, coder->options->check);
128                 coder->sequence = SEQ_CHECK;
129
130         // Fall through
131
132         case SEQ_CHECK: {
133                 const bool chksup = lzma_check_is_supported(
134                                 coder->options->check);
135
136                 while (*in_pos < in_size) {
137                         // coder->check.buffer[] may be uninitialized when
138                         // the Check ID is not supported.
139                         if (chksup && coder->check.buffer.u8[coder->check_pos]
140                                         != in[*in_pos]) {
141                                 ++*in_pos;
142                                 return LZMA_DATA_ERROR;
143                         }
144
145                         ++*in_pos;
146
147                         if (++coder->check_pos == lzma_check_size(
148                                         coder->options->check))
149                                 return LZMA_STREAM_END;
150                 }
151
152                 return LZMA_OK;
153         }
154         }
155
156         return LZMA_PROG_ERROR;
157 }
158
159
160 static void
161 block_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
162 {
163         lzma_next_end(&coder->next, allocator);
164         lzma_free(coder, allocator);
165         return;
166 }
167
168
169 extern lzma_ret
170 lzma_block_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
171                 lzma_block *options)
172 {
173         lzma_next_coder_init(lzma_block_decoder_init, next, allocator);
174
175         // While lzma_block_total_size_get() is meant to calculate the Total
176         // Size, it also validates the options excluding the filters.
177         if (lzma_block_total_size_get(options) == 0)
178                 return LZMA_PROG_ERROR;
179
180         // options->check is used for array indexing so we need to know that
181         // it is in the valid range.
182         if ((unsigned)(options->check) > LZMA_CHECK_ID_MAX)
183                 return LZMA_PROG_ERROR;
184
185         // Allocate and initialize *next->coder if needed.
186         if (next->coder == NULL) {
187                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
188                 if (next->coder == NULL)
189                         return LZMA_MEM_ERROR;
190
191                 next->code = &block_decode;
192                 next->end = &block_decoder_end;
193                 next->coder->next = LZMA_NEXT_CODER_INIT;
194         }
195
196         // Basic initializations
197         next->coder->sequence = SEQ_CODE;
198         next->coder->options = options;
199         next->coder->compressed_size = 0;
200         next->coder->uncompressed_size = 0;
201
202         // If Compressed Size is not known, we calculate the maximum allowed
203         // value so that Total Size of the Block still is a valid VLI and
204         // a multiple of four.
205         next->coder->compressed_limit
206                         = options->compressed_size == LZMA_VLI_VALUE_UNKNOWN
207                                 ? (LZMA_VLI_VALUE_MAX & ~LZMA_VLI_C(3))
208                                         - options->header_size
209                                         - lzma_check_size(options->check)
210                                 : options->compressed_size;
211
212         // Initialize the check. It's caller's problem if the Check ID is not
213         // supported, and the Block decoder cannot verify the Check field.
214         // Caller can test lzma_checks[options->check].
215         next->coder->check_pos = 0;
216         lzma_check_init(&next->coder->check, options->check);
217
218         // Initialize the filter chain.
219         return lzma_raw_decoder_init(&next->coder->next, allocator,
220                         options->filters);
221 }
222
223
224 extern LZMA_API lzma_ret
225 lzma_block_decoder(lzma_stream *strm, lzma_block *options)
226 {
227         lzma_next_strm_init(lzma_block_decoder_init, strm, options);
228
229         strm->internal->supported_actions[LZMA_RUN] = true;
230         strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
231
232         return LZMA_OK;
233 }