aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/subblock/subblock_decoder_helper.c
blob: 77d1f4bdcb7c5a4909e5080210d3fb4e20ce823b (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
///////////////////////////////////////////////////////////////////////////////
//
/// \file       subblock_decoder_helper.c
/// \brief      Helper filter for the Subblock decoder
///
/// This filter is used to indicate End of Input for subfilters needing it.
//
//  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 "subblock_decoder_helper.h"


struct lzma_coder_s {
	const lzma_options_subblock_helper *options;
};


static lzma_ret
helper_decode(lzma_coder *coder,
		lzma_allocator *allocator lzma_attribute((unused)),
		const uint8_t *restrict in, size_t *restrict in_pos,
		size_t in_size, uint8_t *restrict out,
		size_t *restrict out_pos, size_t out_size,
		lzma_action action lzma_attribute((unused)))
{
	// If end_was_reached is true, we cannot have any input.
	assert(!coder->options->end_was_reached || *in_pos == in_size);

	// We can safely copy as much as possible, because we are never
	// given more data than a single Subblock Data field.
	bufcpy(in, in_pos, in_size, out, out_pos, out_size);

	// Return LZMA_STREAM_END when instructed so by the Subblock decoder.
	return coder->options->end_was_reached ? LZMA_STREAM_END : LZMA_OK;
}


static void
helper_end(lzma_coder *coder, lzma_allocator *allocator)
{
	lzma_free(coder, allocator);
	return;
}


extern lzma_ret
lzma_subblock_decoder_helper_init(lzma_next_coder *next,
		lzma_allocator *allocator, const lzma_filter_info *filters)
{
	// This is always the last filter in the chain.
	assert(filters[1].init == NULL);

	// We never know uncompressed size.
	assert(filters[0].uncompressed_size == LZMA_VLI_VALUE_UNKNOWN);

	if (next->coder == NULL) {
		next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
		if (next->coder == NULL)
			return LZMA_MEM_ERROR;
		
		next->code = &helper_decode;
		next->end = &helper_end;
	}

	next->coder->options = filters[0].options;

	return LZMA_OK;
}