1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Validates Index by using a hash function
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 ///////////////////////////////////////////////////////////////////////////////
26 /// Sum of the Total Size fields
29 /// Sum of the Uncompressed Size fields
30 lzma_vli uncompressed_size;
35 /// Size of the List of Index Records as bytes
36 lzma_vli index_list_size;
38 /// Check calculated from Total Sizes and Uncompressed Sizes.
41 } lzma_index_hash_info;
44 struct lzma_index_hash_s {
55 /// Information collected while decoding the actual Blocks.
56 lzma_index_hash_info blocks;
58 /// Information collected from the Index field.
59 lzma_index_hash_info records;
61 /// Number of Records not fully decoded
64 /// Total Size currently being read from an Index Record.
67 /// Uncompressed Size currently being read from an Index Record.
68 lzma_vli uncompressed_size;
70 /// Position in variable-length integers when decoding them from
71 /// the List of Records.
74 /// CRC32 of the Index
79 extern LZMA_API lzma_index_hash *
80 lzma_index_hash_init(lzma_index_hash *index_hash, lzma_allocator *allocator)
82 if (index_hash == NULL) {
83 index_hash = lzma_alloc(sizeof(lzma_index_hash), allocator);
84 if (index_hash == NULL)
88 index_hash->sequence = SEQ_BLOCK;
89 index_hash->blocks.total_size = 0;
90 index_hash->blocks.uncompressed_size = 0;
91 index_hash->blocks.count = 0;
92 index_hash->blocks.index_list_size = 0;
93 index_hash->records.total_size = 0;
94 index_hash->records.uncompressed_size = 0;
95 index_hash->records.count = 0;
96 index_hash->records.index_list_size = 0;
97 index_hash->total_size = 0;
98 index_hash->uncompressed_size = 0;
100 index_hash->crc32 = 0;
102 // These cannot fail because LZMA_CHECK_BEST is known to be supported.
103 (void)lzma_check_init(&index_hash->blocks.check, LZMA_CHECK_BEST);
104 (void)lzma_check_init(&index_hash->records.check, LZMA_CHECK_BEST);
111 lzma_index_hash_end(lzma_index_hash *index_hash, lzma_allocator *allocator)
113 lzma_free(index_hash, allocator);
118 extern LZMA_API lzma_vli
119 lzma_index_hash_size(const lzma_index_hash *index_hash)
121 // Get the size of the Index from ->blocks instead of ->records for
122 // cases where application wants to know the Index Size before
123 // decoding the Index.
124 return index_size(index_hash->blocks.count,
125 index_hash->blocks.index_list_size);
129 /// Updates the sizes and the hash without any validation.
131 hash_append(lzma_index_hash_info *info, lzma_vli total_size,
132 lzma_vli uncompressed_size)
134 info->total_size += total_size;
135 info->uncompressed_size += uncompressed_size;
136 info->index_list_size += lzma_vli_size(total_size_encode(total_size))
137 + lzma_vli_size(uncompressed_size);
140 const lzma_vli sizes[2] = { total_size, uncompressed_size };
141 lzma_check_update(&info->check, LZMA_CHECK_BEST,
142 (const uint8_t *)(sizes), sizeof(sizes));
148 extern LZMA_API lzma_ret
149 lzma_index_hash_append(lzma_index_hash *index_hash, lzma_vli total_size,
150 lzma_vli uncompressed_size)
152 // Validate the arguments.
153 if (index_hash->sequence != SEQ_BLOCK || total_size == 0 ||
154 total_size > LZMA_VLI_VALUE_MAX || (total_size & 3)
155 || uncompressed_size > LZMA_VLI_VALUE_MAX)
156 return LZMA_PROG_ERROR;
159 return_if_error(hash_append(&index_hash->blocks,
160 total_size, uncompressed_size));
162 // Validate the properties of *info are still in allowed limits.
163 if (index_hash->blocks.total_size > LZMA_VLI_VALUE_MAX
164 || index_hash->blocks.uncompressed_size
166 || index_size(index_hash->blocks.count,
167 index_hash->blocks.index_list_size)
168 > LZMA_BACKWARD_SIZE_MAX
169 || index_stream_size(index_hash->blocks.total_size,
170 index_hash->blocks.count,
171 index_hash->blocks.index_list_size)
172 > LZMA_VLI_VALUE_MAX)
173 return LZMA_DATA_ERROR;
179 extern LZMA_API lzma_ret
180 lzma_index_hash_decode(lzma_index_hash *index_hash, const uint8_t *in,
181 size_t *in_pos, size_t in_size)
183 // Catch zero input buffer here, because in contrast to Index encoder
184 // and decoder functions, applications call this function directly
185 // instead of via lzma_code(), which does the buffer checking.
186 if (*in_pos >= in_size)
187 return LZMA_BUF_ERROR;
189 // NOTE: This function has many similarities to index_encode() and
190 // index_decode() functions found from index_encoder.c and
191 // index_decoder.c. See the comments especially in index_encoder.c.
192 const size_t in_start = *in_pos;
193 lzma_ret ret = LZMA_OK;
195 while (*in_pos < in_size)
196 switch (index_hash->sequence) {
198 // Check the Index Indicator is present.
199 if (in[(*in_pos)++] != 0x00)
200 return LZMA_DATA_ERROR;
202 index_hash->sequence = SEQ_COUNT;
206 ret = lzma_vli_decode(&index_hash->remaining,
207 &index_hash->pos, in, in_pos, in_size);
208 if (ret != LZMA_STREAM_END)
211 // The count must match the count of the Blocks decoded.
212 if (index_hash->remaining != index_hash->blocks.count)
213 return LZMA_DATA_ERROR;
218 // Handle the special case when there are no Blocks.
219 index_hash->sequence = index_hash->remaining == 0
220 ? SEQ_PADDING_INIT : SEQ_TOTAL;
225 case SEQ_UNCOMPRESSED: {
226 lzma_vli *size = index_hash->sequence == SEQ_TOTAL
227 ? &index_hash->total_size
228 : &index_hash->uncompressed_size;
230 ret = lzma_vli_decode(size, &index_hash->pos,
231 in, in_pos, in_size);
232 if (ret != LZMA_STREAM_END)
238 if (index_hash->sequence == SEQ_TOTAL) {
239 if (index_hash->total_size > TOTAL_SIZE_ENCODED_MAX)
240 return LZMA_DATA_ERROR;
242 index_hash->total_size = total_size_decode(
243 index_hash->total_size);
245 index_hash->sequence = SEQ_UNCOMPRESSED;
248 return_if_error(hash_append(&index_hash->records,
249 index_hash->total_size,
250 index_hash->uncompressed_size));
252 // Verify that we don't go over the known sizes. Note
253 // that this validation is simpler than the one used
254 // in lzma_index_hash_append(), because here we know
255 // that values in index_hash->blocks are already
256 // validated and we are fine as long as we don't
257 // exceed them in index_hash->records.
258 if (index_hash->blocks.total_size
259 < index_hash->records.total_size
260 || index_hash->blocks.uncompressed_size
261 < index_hash->records.uncompressed_size
262 || index_hash->blocks.index_list_size
263 < index_hash->records.index_list_size)
264 return LZMA_DATA_ERROR;
266 // Check if this was the last Record.
267 index_hash->sequence = --index_hash->remaining == 0
268 ? SEQ_PADDING_INIT : SEQ_TOTAL;
274 case SEQ_PADDING_INIT:
275 index_hash->pos = (LZMA_VLI_C(4) - index_size_unpadded(
276 index_hash->records.count,
277 index_hash->records.index_list_size)) & 3;
278 index_hash->sequence = SEQ_PADDING;
283 if (index_hash->pos > 0) {
285 if (in[(*in_pos)++] != 0x00)
286 return LZMA_DATA_ERROR;
291 // Compare the sizes.
292 if (index_hash->blocks.total_size
293 != index_hash->records.total_size
294 || index_hash->blocks.uncompressed_size
295 != index_hash->records.uncompressed_size
296 || index_hash->blocks.index_list_size
297 != index_hash->records.index_list_size)
298 return LZMA_DATA_ERROR;
300 // Finish the hashes and compare them.
301 lzma_check_finish(&index_hash->blocks.check, LZMA_CHECK_BEST);
302 lzma_check_finish(&index_hash->records.check, LZMA_CHECK_BEST);
303 if (memcmp(index_hash->blocks.check.buffer,
304 index_hash->records.check.buffer,
305 lzma_check_sizes[LZMA_CHECK_BEST]) != 0)
306 return LZMA_DATA_ERROR;
308 // Finish the CRC32 calculation.
309 index_hash->crc32 = lzma_crc32(in + in_start,
310 *in_pos - in_start, index_hash->crc32);
312 index_hash->sequence = SEQ_CRC32;
318 if (*in_pos == in_size)
321 if (((index_hash->crc32 >> (index_hash->pos * 8))
322 & 0xFF) != in[(*in_pos)++])
323 return LZMA_DATA_ERROR;
325 } while (++index_hash->pos < 4);
327 return LZMA_STREAM_END;
331 return LZMA_PROG_ERROR;
336 index_hash->crc32 = lzma_crc32(in + in_start,
337 *in_pos - in_start, index_hash->crc32);