aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/common/block_header_decoder.c
blob: 7676c795aa560c8a4f7d2e34a443380c12aab699 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
///////////////////////////////////////////////////////////////////////////////
//
/// \file       block_header_decoder.c
/// \brief      Decodes Block Header from .lzma files
//
//  Copyright (C) 2007 Lasse Collin
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#include "common.h"
#include "check.h"


struct lzma_coder_s {
	lzma_options_block *options;

	enum {
		SEQ_FLAGS_1,
		SEQ_FLAGS_2,
		SEQ_COMPRESSED_SIZE,
		SEQ_UNCOMPRESSED_SIZE,
		SEQ_FILTER_FLAGS_INIT,
		SEQ_FILTER_FLAGS_DECODE,
		SEQ_CRC32,
		SEQ_PADDING
	} sequence;

	/// Position in variable-length integers
	size_t pos;

	/// CRC32 of the Block Header
	uint32_t crc32;

	lzma_next_coder filter_flags_decoder;
};


static bool
update_sequence(lzma_coder *coder)
{
	switch (coder->sequence) {
	case SEQ_FLAGS_2:
		if (coder->options->compressed_size
				!= LZMA_VLI_VALUE_UNKNOWN) {
			coder->pos = 0;
			coder->sequence = SEQ_COMPRESSED_SIZE;
			break;
		}

	// Fall through

	case SEQ_COMPRESSED_SIZE:
		if (coder->options->uncompressed_size
				!= LZMA_VLI_VALUE_UNKNOWN) {
			coder->pos = 0;
			coder->sequence = SEQ_UNCOMPRESSED_SIZE;
			break;
		}

	// Fall through

	case SEQ_UNCOMPRESSED_SIZE:
		coder->pos = 0;

	// Fall through

	case SEQ_FILTER_FLAGS_DECODE:
		if (coder->options->filters[coder->pos].id
				!= LZMA_VLI_VALUE_UNKNOWN) {
			coder->sequence = SEQ_FILTER_FLAGS_INIT;
			break;
		}

		if (coder->options->has_crc32) {
			coder->pos = 0;
			coder->sequence = SEQ_CRC32;
			break;
		}

	case SEQ_CRC32:
		if (coder->options->padding != 0) {
			coder->pos = 0;
			coder->sequence = SEQ_PADDING;
			break;
		}

		return true;

	default:
		assert(0);
		return true;
	}

	return false;
}


static lzma_ret
block_header_decode(lzma_coder *coder, lzma_allocator *allocator,
		const uint8_t *restrict in, size_t *restrict in_pos,
		size_t in_size, uint8_t *restrict out lzma_attribute((unused)),
		size_t *restrict out_pos lzma_attribute((unused)),
		size_t out_size lzma_attribute((unused)),
		lzma_action action lzma_attribute((unused)))
{
	while (*in_pos < in_size)
	switch (coder->sequence) {
	case SEQ_FLAGS_1:
		// Check that the reserved bit is unset. Use HEADER_ERROR
		// because newer version of liblzma may support the reserved
		// bit, although it is likely that this is just a broken file.
		if (in[*in_pos] & 0x40)
			return LZMA_HEADER_ERROR;

		// Number of filters: we prepare appropriate amount of
		// variables for variable-length integer parsing. The
		// initialization function has already reset the rest
		// of the values to LZMA_VLI_VALUE_UNKNOWN, which allows
		// us to later know how many filters there are.
		for (int i = (int)(in[*in_pos] & 0x07) - 1; i >= 0; --i)
			coder->options->filters[i].id = 0;

		// End of Payload Marker flag
		coder->options->has_eopm = (in[*in_pos] & 0x08) != 0;

		// Compressed Size: Prepare for variable-length integer
		// parsing if it is known.
		if (in[*in_pos] & 0x10)
			coder->options->compressed_size = 0;

		// Uncompressed Size: the same.
		if (in[*in_pos] & 0x20)
			coder->options->uncompressed_size = 0;

		// Is Metadata Block flag
		coder->options->is_metadata = (in[*in_pos] & 0x80) != 0;

		// We need at least one: Uncompressed Size or EOPM.
		if (coder->options->uncompressed_size == LZMA_VLI_VALUE_UNKNOWN
				&& !coder->options->has_eopm)
			return LZMA_DATA_ERROR;

		// Update header CRC32.
		coder->crc32 = lzma_crc32(in + *in_pos, 1, coder->crc32);

		++*in_pos;
		coder->sequence = SEQ_FLAGS_2;
		break;

	case SEQ_FLAGS_2:
		// Check that the reserved bits are unset.
		if (in[*in_pos] & 0xE0)
			return LZMA_DATA_ERROR;

		// Get the size of Header Padding.
		coder->options->padding = in[*in_pos] & 0x1F;

		coder->crc32 = lzma_crc32(in + *in_pos, 1, coder->crc32);

		++*in_pos;

		if (update_sequence(coder))
			return LZMA_STREAM_END;

		break;

	case SEQ_COMPRESSED_SIZE: {
		// Store the old input position to be used when
		// updating coder->header_crc32.
		const size_t in_start = *in_pos;

		const lzma_ret ret = lzma_vli_decode(
				&coder->options->compressed_size,
				&coder->pos, in, in_pos, in_size);

		const size_t in_used = *in_pos - in_start;

		coder->options->compressed_reserve += in_used;
		assert(coder->options->compressed_reserve
				<= LZMA_VLI_BYTES_MAX);

		coder->options->header_size += in_used;

		coder->crc32 = lzma_crc32(in + in_start, in_used,
				coder->crc32);

		if (ret != LZMA_STREAM_END)
			return ret;

		if (update_sequence(coder))
			return LZMA_STREAM_END;

		break;
	}

	case SEQ_UNCOMPRESSED_SIZE: {
		const size_t in_start = *in_pos;

		const lzma_ret ret = lzma_vli_decode(
				&coder->options->uncompressed_size,
				&coder->pos, in, in_pos, in_size);

		const size_t in_used = *in_pos - in_start;

		coder->options->uncompressed_reserve += in_used;
		assert(coder->options->uncompressed_reserve
				<= LZMA_VLI_BYTES_MAX);

		coder->options->header_size += in_used;

		coder->crc32 = lzma_crc32(in + in_start, in_used,
				coder->crc32);

		if (ret != LZMA_STREAM_END)
			return ret;

		if (update_sequence(coder))
			return LZMA_STREAM_END;

		break;
	}

	case SEQ_FILTER_FLAGS_INIT: {
		assert(coder->options->filters[coder->pos].id
				!= LZMA_VLI_VALUE_UNKNOWN);

		const lzma_ret ret = lzma_filter_flags_decoder_init(
				&coder->filter_flags_decoder, allocator,
				&coder->options->filters[coder->pos]);
		if (ret != LZMA_OK)
			return ret;

		coder->sequence = SEQ_FILTER_FLAGS_DECODE;
	}

	// Fall through

	case SEQ_FILTER_FLAGS_DECODE: {
		const size_t in_start = *in_pos;

		const lzma_ret ret = coder->filter_flags_decoder.code(
				coder->filter_flags_decoder.coder,
				allocator, in, in_pos, in_size,
				NULL, NULL, 0, LZMA_RUN);

		const size_t in_used = *in_pos - in_start;
		coder->options->header_size += in_used;
		coder->crc32 = lzma_crc32(in + in_start,
				in_used, coder->crc32);

		if (ret != LZMA_STREAM_END)
			return ret;

		++coder->pos;

		if (update_sequence(coder))
			return LZMA_STREAM_END;

		break;
	}

	case SEQ_CRC32:
		assert(coder->options->has_crc32);

		if (in[*in_pos] != ((coder->crc32 >> (coder->pos * 8)) & 0xFF))
			return LZMA_DATA_ERROR;

		++*in_pos;
		++coder->pos;

		// Check if we reached end of the CRC32 field.
		if (coder->pos == 4) {
			coder->options->header_size += 4;

			if (update_sequence(coder))
				return LZMA_STREAM_END;
		}

		break;

	case SEQ_PADDING:
		if (in[*in_pos] != 0x00)
			return LZMA_DATA_ERROR;

		++*in_pos;
		++coder->options->header_size;
		++coder->pos;

		if (coder->pos < (size_t)(coder->options->padding))
			break;

		return LZMA_STREAM_END;

	default:
		return LZMA_PROG_ERROR;
	}

	return LZMA_OK;
}


static void
block_header_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
{
	lzma_next_coder_end(&coder->filter_flags_decoder, allocator);
	lzma_free(coder, allocator);
	return;
}


extern lzma_ret
lzma_block_header_decoder_init(lzma_next_coder *next,
		lzma_allocator *allocator, lzma_options_block *options)
{
	if (next->coder == NULL) {
		next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
		if (next->coder == NULL)
			return LZMA_MEM_ERROR;

		next->code = &block_header_decode;
		next->end = &block_header_decoder_end;
		next->coder->filter_flags_decoder = LZMA_NEXT_CODER_INIT;
	}

	// Assume that Compressed Size and Uncompressed Size are unknown.
	options->compressed_size = LZMA_VLI_VALUE_UNKNOWN;
	options->uncompressed_size = LZMA_VLI_VALUE_UNKNOWN;

	// We will calculate the sizes of these fields too so that the
	// application may rewrite the header if it wishes so.
	options->compressed_reserve = 0;
	options->uncompressed_reserve = 0;

	// The Block Flags field is always present, so include its size here
	// and we don't need to worry about it in block_header_decode().
	options->header_size = 2;

	// Reset filters[] to indicate empty list of filters.
	// See SEQ_FLAGS_1 in block_header_decode() for reasoning of this.
	for (size_t i = 0; i < 8; ++i) {
		options->filters[i].id = LZMA_VLI_VALUE_UNKNOWN;
		options->filters[i].options = NULL;
	}

	next->coder->options = options;
	next->coder->sequence = SEQ_FLAGS_1;
	next->coder->pos = 0;
	next->coder->crc32 = 0;

	return LZMA_OK;
}


extern LZMA_API lzma_ret
lzma_block_header_decoder(lzma_stream *strm,
		lzma_options_block *options)
{
	lzma_next_strm_init(strm, lzma_block_header_decoder_init, options);

	strm->internal->supported_actions[LZMA_RUN] = true;

	return LZMA_OK;
}