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 Total Size field
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_TOTAL;
99 case SEQ_UNCOMPRESSED: {
100 lzma_vli *size = coder->sequence == SEQ_TOTAL
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_TOTAL) {
113 // Validate that encoded Total Size isn't too big.
114 if (coder->total_size > TOTAL_SIZE_ENCODED_MAX)
115 return LZMA_DATA_ERROR;
117 // Convert the encoded Total Size to the real
119 coder->total_size = total_size_decode(
121 coder->sequence = SEQ_UNCOMPRESSED;
123 // Add the decoded Record to the Index.
124 return_if_error(lzma_index_append(
125 coder->index, allocator,
127 coder->uncompressed_size));
129 // Check if this was the last Record.
130 coder->sequence = --coder->count == 0
138 case SEQ_PADDING_INIT:
139 coder->pos = lzma_index_padding_size(coder->index);
140 coder->sequence = SEQ_PADDING;
145 if (coder->pos > 0) {
147 if (in[(*in_pos)++] != 0x00)
148 return LZMA_DATA_ERROR;
153 // Finish the CRC32 calculation.
154 coder->crc32 = lzma_crc32(in + in_start,
155 *in_pos - in_start, coder->crc32);
157 coder->sequence = SEQ_CRC32;
163 if (*in_pos == in_size)
166 if (((coder->crc32 >> (coder->pos * 8)) & 0xFF)
168 return LZMA_DATA_ERROR;
170 } while (++coder->pos < 4);
172 // Make index NULL so we don't free it unintentionally.
175 return LZMA_STREAM_END;
179 return LZMA_PROG_ERROR;
184 coder->crc32 = lzma_crc32(in + in_start,
185 *in_pos - in_start, coder->crc32);
192 index_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
194 lzma_index_end(coder->index, allocator);
195 lzma_free(coder, allocator);
201 index_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
205 return LZMA_PROG_ERROR;
207 if (next->coder == NULL) {
208 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
209 if (next->coder == NULL)
210 return LZMA_MEM_ERROR;
212 next->code = &index_decode;
213 next->end = &index_decoder_end;
214 next->coder->index = NULL;
216 lzma_index_end(next->coder->index, allocator);
219 // We always allocate a new lzma_index.
220 *i = lzma_index_init(NULL, allocator);
222 return LZMA_MEM_ERROR;
224 // Initialize the rest.
225 next->coder->sequence = SEQ_INDICATOR;
226 next->coder->index = *i;
227 next->coder->pos = 0;
228 next->coder->crc32 = 0;
236 lzma_index_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
239 lzma_next_coder_init(index_decoder_init, next, allocator, i);
244 extern LZMA_API lzma_ret
245 lzma_index_decoder(lzma_stream *strm, lzma_index **i)
247 lzma_next_strm_init(strm, index_decoder_init, i);
249 strm->internal->supported_actions[LZMA_RUN] = true;