1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file index_decoder.c
4 /// \brief Decodes the Index field
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
29 /// Memory usage limit
35 /// Number of Records left to decode.
38 /// The most recent Unpadded Size field
39 lzma_vli unpadded_size;
41 /// The most recent Uncompressed Size field
42 lzma_vli uncompressed_size;
44 /// Position in integers
47 /// CRC32 of the List of Records field
53 index_decode(lzma_coder *coder, lzma_allocator *allocator,
54 const uint8_t *restrict in, size_t *restrict in_pos,
55 size_t in_size, uint8_t *restrict out lzma_attribute((unused)),
56 size_t *restrict out_pos lzma_attribute((unused)),
57 size_t out_size lzma_attribute((unused)),
58 lzma_action action lzma_attribute((unused)))
60 // Similar optimization as in index_encoder.c
61 const size_t in_start = *in_pos;
62 lzma_ret ret = LZMA_OK;
64 while (*in_pos < in_size)
65 switch (coder->sequence) {
67 // Return LZMA_DATA_ERROR instead of e.g. LZMA_PROG_ERROR or
68 // LZMA_FORMAT_ERROR, because a typical usage case for Index
69 // decoder is when parsing the Stream backwards. If seeking
70 // backward from the Stream Footer gives us something that
71 // doesn't begin with Index Indicator, the file is considered
72 // corrupt, not "programming error" or "unrecognized file
73 // format". One could argue that the application should
74 // verify the Index Indicator before trying to decode the
75 // Index, but well, I suppose it is simpler this way.
76 if (in[(*in_pos)++] != 0x00)
77 return LZMA_DATA_ERROR;
79 coder->sequence = SEQ_COUNT;
83 ret = lzma_vli_decode(&coder->count, &coder->pos,
85 if (ret != LZMA_STREAM_END)
89 coder->sequence = SEQ_MEMUSAGE;
94 if (lzma_index_memusage(coder->count) > coder->memlimit) {
95 ret = LZMA_MEMLIMIT_ERROR;
100 coder->sequence = coder->count == 0
101 ? SEQ_PADDING_INIT : SEQ_UNPADDED;
105 case SEQ_UNCOMPRESSED: {
106 lzma_vli *size = coder->sequence == SEQ_UNPADDED
107 ? &coder->unpadded_size
108 : &coder->uncompressed_size;
110 ret = lzma_vli_decode(size, &coder->pos,
111 in, in_pos, in_size);
112 if (ret != LZMA_STREAM_END)
118 if (coder->sequence == SEQ_UNPADDED) {
119 // Validate that encoded Unpadded Size isn't too small
121 if (coder->unpadded_size < UNPADDED_SIZE_MIN
122 || coder->unpadded_size
124 return LZMA_DATA_ERROR;
126 coder->sequence = SEQ_UNCOMPRESSED;
128 // Add the decoded Record to the Index.
129 return_if_error(lzma_index_append(
130 coder->index, allocator,
131 coder->unpadded_size,
132 coder->uncompressed_size));
134 // Check if this was the last Record.
135 coder->sequence = --coder->count == 0
143 case SEQ_PADDING_INIT:
144 coder->pos = lzma_index_padding_size(coder->index);
145 coder->sequence = SEQ_PADDING;
150 if (coder->pos > 0) {
152 if (in[(*in_pos)++] != 0x00)
153 return LZMA_DATA_ERROR;
158 // Finish the CRC32 calculation.
159 coder->crc32 = lzma_crc32(in + in_start,
160 *in_pos - in_start, coder->crc32);
162 coder->sequence = SEQ_CRC32;
168 if (*in_pos == in_size)
171 if (((coder->crc32 >> (coder->pos * 8)) & 0xFF)
173 return LZMA_DATA_ERROR;
175 } while (++coder->pos < 4);
177 // Make index NULL so we don't free it unintentionally.
180 return LZMA_STREAM_END;
184 return LZMA_PROG_ERROR;
189 coder->crc32 = lzma_crc32(in + in_start,
190 *in_pos - in_start, coder->crc32);
197 index_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
199 lzma_index_end(coder->index, allocator);
200 lzma_free(coder, allocator);
206 index_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
207 uint64_t *old_memlimit, uint64_t new_memlimit)
209 *memusage = lzma_index_memusage(coder->count);
211 if (new_memlimit != 0 && new_memlimit < *memusage)
212 return LZMA_MEMLIMIT_ERROR;
214 *old_memlimit = coder->memlimit;
215 coder->memlimit = new_memlimit;
222 index_decoder_reset(lzma_coder *coder, lzma_allocator *allocator,
223 lzma_index **i, uint64_t memlimit)
225 // We always allocate a new lzma_index.
226 *i = lzma_index_init(NULL, allocator);
228 return LZMA_MEM_ERROR;
230 // Initialize the rest.
231 coder->sequence = SEQ_INDICATOR;
232 coder->memlimit = memlimit;
234 coder->count = 0; // Needs to be initialized due to _memconfig().
243 index_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
244 lzma_index **i, uint64_t memlimit)
246 lzma_next_coder_init(index_decoder_init, next, allocator);
248 if (i == NULL || memlimit == 0)
249 return LZMA_PROG_ERROR;
251 if (next->coder == NULL) {
252 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
253 if (next->coder == NULL)
254 return LZMA_MEM_ERROR;
256 next->code = &index_decode;
257 next->end = &index_decoder_end;
258 next->memconfig = &index_decoder_memconfig;
259 next->coder->index = NULL;
261 lzma_index_end(next->coder->index, allocator);
264 return index_decoder_reset(next->coder, allocator, i, memlimit);
268 extern LZMA_API(lzma_ret)
269 lzma_index_decoder(lzma_stream *strm, lzma_index **i, uint64_t memlimit)
271 lzma_next_strm_init(index_decoder_init, strm, i, memlimit);
273 strm->internal->supported_actions[LZMA_RUN] = true;
279 extern LZMA_API(lzma_ret)
280 lzma_index_buffer_decode(
281 lzma_index **i, uint64_t *memlimit, lzma_allocator *allocator,
282 const uint8_t *in, size_t *in_pos, size_t in_size)
285 if (i == NULL || in == NULL || in_pos == NULL || *in_pos > in_size)
286 return LZMA_PROG_ERROR;
288 // Initialize the decoder.
290 return_if_error(index_decoder_reset(&coder, allocator, i, *memlimit));
292 // Store the input start position so that we can restore it in case
294 const size_t in_start = *in_pos;
296 // Do the actual decoding.
297 lzma_ret ret = index_decode(&coder, allocator, in, in_pos, in_size,
298 NULL, NULL, 0, LZMA_RUN);
300 if (ret == LZMA_STREAM_END) {
303 // Something went wrong, free the Index structure and restore
304 // the input position.
305 lzma_index_end(*i, allocator);
309 if (ret == LZMA_OK) {
310 // The input is truncated or otherwise corrupt.
311 // Use LZMA_DATA_ERROR instead of LZMA_BUF_ERROR
312 // like lzma_vli_decode() does in single-call mode.
313 ret = LZMA_DATA_ERROR;
315 } else if (ret == LZMA_MEMLIMIT_ERROR) {
316 // Tell the caller how much memory would have
318 *memlimit = lzma_index_memusage(coder.count);