1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file index_decoder.c
4 /// \brief Decodes the Index field
6 // Copyright (C) 2008 Lasse Collin
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.
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.
18 ///////////////////////////////////////////////////////////////////////////////
38 /// Number of Records left to decode.
41 /// The most recent Unpadded Size field
42 lzma_vli unpadded_size;
44 /// The most recent Uncompressed Size field
45 lzma_vli uncompressed_size;
47 /// Position in integers
50 /// CRC32 of the List of Records field
56 index_decode(lzma_coder *coder, lzma_allocator *allocator,
57 const uint8_t *restrict in, size_t *restrict in_pos,
58 size_t in_size, uint8_t *restrict out lzma_attribute((unused)),
59 size_t *restrict out_pos lzma_attribute((unused)),
60 size_t out_size lzma_attribute((unused)),
61 lzma_action action lzma_attribute((unused)))
63 // Similar optimization as in index_encoder.c
64 const size_t in_start = *in_pos;
65 lzma_ret ret = LZMA_OK;
67 while (*in_pos < in_size)
68 switch (coder->sequence) {
70 // Return LZMA_DATA_ERROR instead of e.g. LZMA_PROG_ERROR or
71 // LZMA_FORMAT_ERROR, because a typical usage case for Index
72 // decoder is when parsing the Stream backwards. If seeking
73 // backward from the Stream Footer gives us something that
74 // doesn't begin with Index Indicator, the file is considered
75 // corrupt, not "programming error" or "unrecognized file
76 // format". One could argue that the application should
77 // verify the Index Indicator before trying to decode the
78 // Index, but well, I suppose it is simpler this way.
79 if (in[(*in_pos)++] != 0x00)
80 return LZMA_DATA_ERROR;
82 coder->sequence = SEQ_COUNT;
86 ret = lzma_vli_decode(&coder->count, &coder->pos,
88 if (ret != LZMA_STREAM_END)
93 coder->sequence = coder->count == 0
94 ? SEQ_PADDING_INIT : SEQ_UNPADDED;
99 case SEQ_UNCOMPRESSED: {
100 lzma_vli *size = coder->sequence == SEQ_UNPADDED
101 ? &coder->unpadded_size
102 : &coder->uncompressed_size;
104 ret = lzma_vli_decode(size, &coder->pos,
105 in, in_pos, in_size);
106 if (ret != LZMA_STREAM_END)
112 if (coder->sequence == SEQ_UNPADDED) {
113 // Validate that encoded Unpadded Size isn't too small
115 if (coder->unpadded_size < UNPADDED_SIZE_MIN
116 || coder->unpadded_size
118 return LZMA_DATA_ERROR;
120 coder->sequence = SEQ_UNCOMPRESSED;
122 // Add the decoded Record to the Index.
123 return_if_error(lzma_index_append(
124 coder->index, allocator,
125 coder->unpadded_size,
126 coder->uncompressed_size));
128 // Check if this was the last Record.
129 coder->sequence = --coder->count == 0
137 case SEQ_PADDING_INIT:
138 coder->pos = lzma_index_padding_size(coder->index);
139 coder->sequence = SEQ_PADDING;
144 if (coder->pos > 0) {
146 if (in[(*in_pos)++] != 0x00)
147 return LZMA_DATA_ERROR;
152 // Finish the CRC32 calculation.
153 coder->crc32 = lzma_crc32(in + in_start,
154 *in_pos - in_start, coder->crc32);
156 coder->sequence = SEQ_CRC32;
162 if (*in_pos == in_size)
165 if (((coder->crc32 >> (coder->pos * 8)) & 0xFF)
167 return LZMA_DATA_ERROR;
169 } while (++coder->pos < 4);
171 // Make index NULL so we don't free it unintentionally.
174 return LZMA_STREAM_END;
178 return LZMA_PROG_ERROR;
183 coder->crc32 = lzma_crc32(in + in_start,
184 *in_pos - in_start, coder->crc32);
191 index_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
193 lzma_index_end(coder->index, allocator);
194 lzma_free(coder, allocator);
200 index_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
203 lzma_next_coder_init(index_decoder_init, next, allocator);
206 return LZMA_PROG_ERROR;
208 if (next->coder == NULL) {
209 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
210 if (next->coder == NULL)
211 return LZMA_MEM_ERROR;
213 next->code = &index_decode;
214 next->end = &index_decoder_end;
215 next->coder->index = NULL;
217 lzma_index_end(next->coder->index, allocator);
220 // We always allocate a new lzma_index.
221 *i = lzma_index_init(NULL, allocator);
223 return LZMA_MEM_ERROR;
225 // Initialize the rest.
226 next->coder->sequence = SEQ_INDICATOR;
227 next->coder->index = *i;
228 next->coder->pos = 0;
229 next->coder->crc32 = 0;
235 extern LZMA_API lzma_ret
236 lzma_index_decoder(lzma_stream *strm, lzma_index **i)
238 lzma_next_strm_init(index_decoder_init, strm, i);
240 strm->internal->supported_actions[LZMA_RUN] = true;