]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/index_decoder.c
Oh well, big messy commit again. Some highlights:
[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_UNPADDED,
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 Unpadded Size field
42         lzma_vli unpadded_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_UNPADDED;
95                 break;
96         }
97
98         case SEQ_UNPADDED:
99         case SEQ_UNCOMPRESSED: {
100                 lzma_vli *size = coder->sequence == SEQ_UNPADDED
101                                 ? &coder->unpadded_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_UNPADDED) {
113                         // Validate that encoded Unpadded Size isn't too small
114                         // or too big.
115                         if (coder->unpadded_size < UNPADDED_SIZE_MIN
116                                         || coder->unpadded_size
117                                                 > UNPADDED_SIZE_MAX)
118                                 return LZMA_DATA_ERROR;
119
120                         coder->sequence = SEQ_UNCOMPRESSED;
121                 } else {
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));
127
128                         // Check if this was the last Record.
129                         coder->sequence = --coder->count == 0
130                                         ? SEQ_PADDING_INIT
131                                         : SEQ_UNPADDED;
132                 }
133
134                 break;
135         }
136
137         case SEQ_PADDING_INIT:
138                 coder->pos = lzma_index_padding_size(coder->index);
139                 coder->sequence = SEQ_PADDING;
140
141         // Fall through
142
143         case SEQ_PADDING:
144                 if (coder->pos > 0) {
145                         --coder->pos;
146                         if (in[(*in_pos)++] != 0x00)
147                                 return LZMA_DATA_ERROR;
148
149                         break;
150                 }
151
152                 // Finish the CRC32 calculation.
153                 coder->crc32 = lzma_crc32(in + in_start,
154                                 *in_pos - in_start, coder->crc32);
155
156                 coder->sequence = SEQ_CRC32;
157
158         // Fall through
159
160         case SEQ_CRC32:
161                 do {
162                         if (*in_pos == in_size)
163                                 return LZMA_OK;
164
165                         if (((coder->crc32 >> (coder->pos * 8)) & 0xFF)
166                                         != in[(*in_pos)++])
167                                 return LZMA_DATA_ERROR;
168
169                 } while (++coder->pos < 4);
170
171                 // Make index NULL so we don't free it unintentionally.
172                 coder->index = NULL;
173
174                 return LZMA_STREAM_END;
175
176         default:
177                 assert(0);
178                 return LZMA_PROG_ERROR;
179         }
180
181 out:
182         // Update the CRC32,
183         coder->crc32 = lzma_crc32(in + in_start,
184                         *in_pos - in_start, coder->crc32);
185
186         return ret;
187 }
188
189
190 static void
191 index_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
192 {
193         lzma_index_end(coder->index, allocator);
194         lzma_free(coder, allocator);
195         return;
196 }
197
198
199 static lzma_ret
200 index_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
201                 lzma_index **i)
202 {
203         lzma_next_coder_init(index_decoder_init, next, allocator);
204
205         if (i == NULL)
206                 return LZMA_PROG_ERROR;
207
208         if (next->coder == NULL) {
209                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
210                 if (next->coder == NULL)
211                         return LZMA_MEM_ERROR;
212
213                 next->code = &index_decode;
214                 next->end = &index_decoder_end;
215                 next->coder->index = NULL;
216         } else {
217                 lzma_index_end(next->coder->index, allocator);
218         }
219
220         // We always allocate a new lzma_index.
221         *i = lzma_index_init(NULL, allocator);
222         if (*i == NULL)
223                 return LZMA_MEM_ERROR;
224
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;
230
231         return LZMA_OK;
232 }
233
234
235 extern LZMA_API lzma_ret
236 lzma_index_decoder(lzma_stream *strm, lzma_index **i)
237 {
238         lzma_next_strm_init(index_decoder_init, strm, i);
239
240         strm->internal->supported_actions[LZMA_RUN] = true;
241
242         return LZMA_OK;
243 }