]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/index_decoder.c
Update the code to mostly match the new simpler file format
[icculus/xz.git] / src / liblzma / common / index_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       index_decoder.c
4 /// \brief      Decodes the Index field
5 //
6 //  Copyright (C) 2008 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 "index.h"
21 #include "check.h"
22
23
24 struct lzma_coder_s {
25         enum {
26                 SEQ_INDICATOR,
27                 SEQ_COUNT,
28                 SEQ_TOTAL,
29                 SEQ_UNCOMPRESSED,
30                 SEQ_PADDING_INIT,
31                 SEQ_PADDING,
32                 SEQ_CRC32,
33         } sequence;
34
35         /// Target Index
36         lzma_index *index;
37
38         /// Number of Records left to decode.
39         lzma_vli count;
40
41         /// The most recent Total Size field
42         lzma_vli total_size;
43
44         /// The most recent Uncompressed Size field
45         lzma_vli uncompressed_size;
46
47         /// Position in integers
48         size_t pos;
49
50         /// CRC32 of the List of Records field
51         uint32_t crc32;
52 };
53
54
55 static lzma_ret
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)))
62 {
63         // Similar optimization as in index_encoder.c
64         const size_t in_start = *in_pos;
65         lzma_ret ret = LZMA_OK;
66
67         while (*in_pos < in_size)
68         switch (coder->sequence) {
69         case SEQ_INDICATOR:
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;
81
82                 coder->sequence = SEQ_COUNT;
83                 break;
84
85         case SEQ_COUNT: {
86                 ret = lzma_vli_decode(&coder->count, &coder->pos,
87                                 in, in_pos, in_size);
88                 if (ret != LZMA_STREAM_END)
89                         goto out;
90
91                 ret = LZMA_OK;
92                 coder->pos = 0;
93                 coder->sequence = coder->count == 0
94                                 ? SEQ_PADDING_INIT : SEQ_TOTAL;
95                 break;
96         }
97
98         case SEQ_TOTAL:
99         case SEQ_UNCOMPRESSED: {
100                 lzma_vli *size = coder->sequence == SEQ_TOTAL
101                                 ? &coder->total_size
102                                 : &coder->uncompressed_size;
103
104                 ret = lzma_vli_decode(size, &coder->pos,
105                                 in, in_pos, in_size);
106                 if (ret != LZMA_STREAM_END)
107                         goto out;
108
109                 ret = LZMA_OK;
110                 coder->pos = 0;
111
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;
116
117                         // Convert the encoded Total Size to the real
118                         // Total Size.
119                         coder->total_size = total_size_decode(
120                                         coder->total_size);
121                         coder->sequence = SEQ_UNCOMPRESSED;
122                 } else {
123                         // Add the decoded Record to the Index.
124                         return_if_error(lzma_index_append(
125                                         coder->index, allocator,
126                                         coder->total_size,
127                                         coder->uncompressed_size));
128
129                         // Check if this was the last Record.
130                         coder->sequence = --coder->count == 0
131                                         ? SEQ_PADDING_INIT
132                                         : SEQ_TOTAL;
133                 }
134
135                 break;
136         }
137
138         case SEQ_PADDING_INIT:
139                 coder->pos = lzma_index_padding_size(coder->index);
140                 coder->sequence = SEQ_PADDING;
141
142         // Fall through
143
144         case SEQ_PADDING:
145                 if (coder->pos > 0) {
146                         --coder->pos;
147                         if (in[(*in_pos)++] != 0x00)
148                                 return LZMA_DATA_ERROR;
149
150                         break;
151                 }
152
153                 // Finish the CRC32 calculation.
154                 coder->crc32 = lzma_crc32(in + in_start,
155                                 *in_pos - in_start, coder->crc32);
156
157                 coder->sequence = SEQ_CRC32;
158
159         // Fall through
160
161         case SEQ_CRC32:
162                 do {
163                         if (*in_pos == in_size)
164                                 return LZMA_OK;
165
166                         if (((coder->crc32 >> (coder->pos * 8)) & 0xFF)
167                                         != in[(*in_pos)++])
168                                 return LZMA_DATA_ERROR;
169
170                 } while (++coder->pos < 4);
171
172                 // Make index NULL so we don't free it unintentionally.
173                 coder->index = NULL;
174
175                 return LZMA_STREAM_END;
176
177         default:
178                 assert(0);
179                 return LZMA_PROG_ERROR;
180         }
181
182 out:
183         // Update the CRC32,
184         coder->crc32 = lzma_crc32(in + in_start,
185                         *in_pos - in_start, coder->crc32);
186
187         return ret;
188 }
189
190
191 static void
192 index_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
193 {
194         lzma_index_end(coder->index, allocator);
195         lzma_free(coder, allocator);
196         return;
197 }
198
199
200 static lzma_ret
201 index_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
202                 lzma_index **i)
203 {
204         if (i == NULL)
205                 return LZMA_PROG_ERROR;
206
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 = &index_decode;
213                 next->end = &index_decoder_end;
214                 next->coder->index = NULL;
215         } else {
216                 lzma_index_end(next->coder->index, allocator);
217         }
218
219         // We always allocate a new lzma_index.
220         *i = lzma_index_init(NULL, allocator);
221         if (*i == NULL)
222                 return LZMA_MEM_ERROR;
223
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;
229
230         return LZMA_OK;
231 }
232
233
234 /*
235 extern lzma_ret
236 lzma_index_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
237                 lzma_index **i)
238 {
239         lzma_next_coder_init(index_decoder_init, next, allocator, i);
240 }
241 */
242
243
244 extern LZMA_API lzma_ret
245 lzma_index_decoder(lzma_stream *strm, lzma_index **i)
246 {
247         lzma_next_strm_init(strm, index_decoder_init, i);
248
249         strm->internal->supported_actions[LZMA_RUN] = true;
250
251         return LZMA_OK;
252 }