1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       lz_decoder.c
4207753Smm/// \brief      LZ out window
5207753Smm///
6207753Smm//  Authors:    Igor Pavlov
7207753Smm//              Lasse Collin
8207753Smm//
9207753Smm//  This file has been put into the public domain.
10207753Smm//  You can do whatever you want with this file.
11207753Smm//
12207753Smm///////////////////////////////////////////////////////////////////////////////
13207753Smm
14207753Smm// liblzma supports multiple LZ77-based filters. The LZ part is shared
15207753Smm// between these filters. The LZ code takes care of dictionary handling
16207753Smm// and passing the data between filters in the chain. The filter-specific
17207753Smm// part decodes from the input buffer to the dictionary.
18207753Smm
19207753Smm
20207753Smm#include "lz_decoder.h"
21207753Smm
22207753Smm
23312518Sdelphijtypedef struct {
24207753Smm	/// Dictionary (history buffer)
25207753Smm	lzma_dict dict;
26207753Smm
27207753Smm	/// The actual LZ-based decoder e.g. LZMA
28207753Smm	lzma_lz_decoder lz;
29207753Smm
30207753Smm	/// Next filter in the chain, if any. Note that LZMA and LZMA2 are
31207753Smm	/// only allowed as the last filter, but the long-range filter in
32207753Smm	/// future can be in the middle of the chain.
33207753Smm	lzma_next_coder next;
34207753Smm
35207753Smm	/// True if the next filter in the chain has returned LZMA_STREAM_END.
36207753Smm	bool next_finished;
37207753Smm
38207753Smm	/// True if the LZ decoder (e.g. LZMA) has detected end of payload
39207753Smm	/// marker. This may become true before next_finished becomes true.
40207753Smm	bool this_finished;
41207753Smm
42207753Smm	/// Temporary buffer needed when the LZ-based filter is not the last
43207753Smm	/// filter in the chain. The output of the next filter is first
44207753Smm	/// decoded into buffer[], which is then used as input for the actual
45207753Smm	/// LZ-based decoder.
46207753Smm	struct {
47207753Smm		size_t pos;
48207753Smm		size_t size;
49207753Smm		uint8_t buffer[LZMA_BUFFER_SIZE];
50207753Smm	} temp;
51312518Sdelphij} lzma_coder;
52207753Smm
53207753Smm
54207753Smmstatic void
55207753Smmlz_decoder_reset(lzma_coder *coder)
56207753Smm{
57207753Smm	coder->dict.pos = 0;
58207753Smm	coder->dict.full = 0;
59207753Smm	coder->dict.buf[coder->dict.size - 1] = '\0';
60207753Smm	coder->dict.need_reset = false;
61207753Smm	return;
62207753Smm}
63207753Smm
64207753Smm
65207753Smmstatic lzma_ret
66207753Smmdecode_buffer(lzma_coder *coder,
67207753Smm		const uint8_t *restrict in, size_t *restrict in_pos,
68207753Smm		size_t in_size, uint8_t *restrict out,
69207753Smm		size_t *restrict out_pos, size_t out_size)
70207753Smm{
71207753Smm	while (true) {
72207753Smm		// Wrap the dictionary if needed.
73207753Smm		if (coder->dict.pos == coder->dict.size)
74207753Smm			coder->dict.pos = 0;
75207753Smm
76207753Smm		// Store the current dictionary position. It is needed to know
77207753Smm		// where to start copying to the out[] buffer.
78207753Smm		const size_t dict_start = coder->dict.pos;
79207753Smm
80207753Smm		// Calculate how much we allow coder->lz.code() to decode.
81207753Smm		// It must not decode past the end of the dictionary
82207753Smm		// buffer, and we don't want it to decode more than is
83207753Smm		// actually needed to fill the out[] buffer.
84213700Smm		coder->dict.limit = coder->dict.pos
85213700Smm				+ my_min(out_size - *out_pos,
86213700Smm					coder->dict.size - coder->dict.pos);
87207753Smm
88207753Smm		// Call the coder->lz.code() to do the actual decoding.
89207753Smm		const lzma_ret ret = coder->lz.code(
90207753Smm				coder->lz.coder, &coder->dict,
91207753Smm				in, in_pos, in_size);
92207753Smm
93207753Smm		// Copy the decoded data from the dictionary to the out[]
94207753Smm		// buffer.
95207753Smm		const size_t copy_size = coder->dict.pos - dict_start;
96207753Smm		assert(copy_size <= out_size - *out_pos);
97207753Smm		memcpy(out + *out_pos, coder->dict.buf + dict_start,
98207753Smm				copy_size);
99207753Smm		*out_pos += copy_size;
100207753Smm
101207753Smm		// Reset the dictionary if so requested by coder->lz.code().
102207753Smm		if (coder->dict.need_reset) {
103207753Smm			lz_decoder_reset(coder);
104207753Smm
105207753Smm			// Since we reset dictionary, we don't check if
106207753Smm			// dictionary became full.
107207753Smm			if (ret != LZMA_OK || *out_pos == out_size)
108207753Smm				return ret;
109207753Smm		} else {
110207753Smm			// Return if everything got decoded or an error
111207753Smm			// occurred, or if there's no more data to decode.
112207753Smm			//
113207753Smm			// Note that detecting if there's something to decode
114207753Smm			// is done by looking if dictionary become full
115207753Smm			// instead of looking if *in_pos == in_size. This
116207753Smm			// is because it is possible that all the input was
117207753Smm			// consumed already but some data is pending to be
118207753Smm			// written to the dictionary.
119207753Smm			if (ret != LZMA_OK || *out_pos == out_size
120207753Smm					|| coder->dict.pos < coder->dict.size)
121207753Smm				return ret;
122207753Smm		}
123207753Smm	}
124207753Smm}
125207753Smm
126207753Smm
127207753Smmstatic lzma_ret
128312518Sdelphijlz_decode(void *coder_ptr,
129292588Sdelphij		const lzma_allocator *allocator lzma_attribute((__unused__)),
130207753Smm		const uint8_t *restrict in, size_t *restrict in_pos,
131207753Smm		size_t in_size, uint8_t *restrict out,
132207753Smm		size_t *restrict out_pos, size_t out_size,
133207753Smm		lzma_action action)
134207753Smm{
135312518Sdelphij	lzma_coder *coder = coder_ptr;
136312518Sdelphij
137207753Smm	if (coder->next.code == NULL)
138207753Smm		return decode_buffer(coder, in, in_pos, in_size,
139207753Smm				out, out_pos, out_size);
140207753Smm
141207753Smm	// We aren't the last coder in the chain, we need to decode
142207753Smm	// our input to a temporary buffer.
143207753Smm	while (*out_pos < out_size) {
144207753Smm		// Fill the temporary buffer if it is empty.
145207753Smm		if (!coder->next_finished
146207753Smm				&& coder->temp.pos == coder->temp.size) {
147207753Smm			coder->temp.pos = 0;
148207753Smm			coder->temp.size = 0;
149207753Smm
150207753Smm			const lzma_ret ret = coder->next.code(
151207753Smm					coder->next.coder,
152207753Smm					allocator, in, in_pos, in_size,
153207753Smm					coder->temp.buffer, &coder->temp.size,
154207753Smm					LZMA_BUFFER_SIZE, action);
155207753Smm
156207753Smm			if (ret == LZMA_STREAM_END)
157207753Smm				coder->next_finished = true;
158207753Smm			else if (ret != LZMA_OK || coder->temp.size == 0)
159207753Smm				return ret;
160207753Smm		}
161207753Smm
162207753Smm		if (coder->this_finished) {
163207753Smm			if (coder->temp.size != 0)
164207753Smm				return LZMA_DATA_ERROR;
165207753Smm
166207753Smm			if (coder->next_finished)
167207753Smm				return LZMA_STREAM_END;
168207753Smm
169207753Smm			return LZMA_OK;
170207753Smm		}
171207753Smm
172207753Smm		const lzma_ret ret = decode_buffer(coder, coder->temp.buffer,
173207753Smm				&coder->temp.pos, coder->temp.size,
174207753Smm				out, out_pos, out_size);
175207753Smm
176207753Smm		if (ret == LZMA_STREAM_END)
177207753Smm			coder->this_finished = true;
178207753Smm		else if (ret != LZMA_OK)
179207753Smm			return ret;
180207753Smm		else if (coder->next_finished && *out_pos < out_size)
181207753Smm			return LZMA_DATA_ERROR;
182207753Smm	}
183207753Smm
184207753Smm	return LZMA_OK;
185207753Smm}
186207753Smm
187207753Smm
188207753Smmstatic void
189312518Sdelphijlz_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
190207753Smm{
191312518Sdelphij	lzma_coder *coder = coder_ptr;
192312518Sdelphij
193207753Smm	lzma_next_end(&coder->next, allocator);
194207753Smm	lzma_free(coder->dict.buf, allocator);
195207753Smm
196207753Smm	if (coder->lz.end != NULL)
197207753Smm		coder->lz.end(coder->lz.coder, allocator);
198207753Smm	else
199207753Smm		lzma_free(coder->lz.coder, allocator);
200207753Smm
201207753Smm	lzma_free(coder, allocator);
202207753Smm	return;
203207753Smm}
204207753Smm
205207753Smm
206207753Smmextern lzma_ret
207292588Sdelphijlzma_lz_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
208207753Smm		const lzma_filter_info *filters,
209207753Smm		lzma_ret (*lz_init)(lzma_lz_decoder *lz,
210292588Sdelphij			const lzma_allocator *allocator, const void *options,
211207753Smm			lzma_lz_options *lz_options))
212207753Smm{
213207753Smm	// Allocate the base structure if it isn't already allocated.
214312518Sdelphij	lzma_coder *coder = next->coder;
215312518Sdelphij	if (coder == NULL) {
216312518Sdelphij		coder = lzma_alloc(sizeof(lzma_coder), allocator);
217312518Sdelphij		if (coder == NULL)
218207753Smm			return LZMA_MEM_ERROR;
219207753Smm
220312518Sdelphij		next->coder = coder;
221207753Smm		next->code = &lz_decode;
222207753Smm		next->end = &lz_decoder_end;
223207753Smm
224312518Sdelphij		coder->dict.buf = NULL;
225312518Sdelphij		coder->dict.size = 0;
226312518Sdelphij		coder->lz = LZMA_LZ_DECODER_INIT;
227312518Sdelphij		coder->next = LZMA_NEXT_CODER_INIT;
228207753Smm	}
229207753Smm
230207753Smm	// Allocate and initialize the LZ-based decoder. It will also give
231207753Smm	// us the dictionary size.
232207753Smm	lzma_lz_options lz_options;
233312518Sdelphij	return_if_error(lz_init(&coder->lz, allocator,
234207753Smm			filters[0].options, &lz_options));
235207753Smm
236207753Smm	// If the dictionary size is very small, increase it to 4096 bytes.
237207753Smm	// This is to prevent constant wrapping of the dictionary, which
238207753Smm	// would slow things down. The downside is that since we don't check
239207753Smm	// separately for the real dictionary size, we may happily accept
240207753Smm	// corrupt files.
241207753Smm	if (lz_options.dict_size < 4096)
242207753Smm		lz_options.dict_size = 4096;
243207753Smm
244207753Smm	// Make dictionary size a multipe of 16. Some LZ-based decoders like
245207753Smm	// LZMA use the lowest bits lzma_dict.pos to know the alignment of the
246207753Smm	// data. Aligned buffer is also good when memcpying from the
247207753Smm	// dictionary to the output buffer, since applications are
248207753Smm	// recommended to give aligned buffers to liblzma.
249207753Smm	//
250207753Smm	// Avoid integer overflow.
251207753Smm	if (lz_options.dict_size > SIZE_MAX - 15)
252207753Smm		return LZMA_MEM_ERROR;
253207753Smm
254207753Smm	lz_options.dict_size = (lz_options.dict_size + 15) & ~((size_t)(15));
255207753Smm
256207753Smm	// Allocate and initialize the dictionary.
257312518Sdelphij	if (coder->dict.size != lz_options.dict_size) {
258312518Sdelphij		lzma_free(coder->dict.buf, allocator);
259312518Sdelphij		coder->dict.buf
260207753Smm				= lzma_alloc(lz_options.dict_size, allocator);
261312518Sdelphij		if (coder->dict.buf == NULL)
262207753Smm			return LZMA_MEM_ERROR;
263207753Smm
264312518Sdelphij		coder->dict.size = lz_options.dict_size;
265207753Smm	}
266207753Smm
267207753Smm	lz_decoder_reset(next->coder);
268207753Smm
269207753Smm	// Use the preset dictionary if it was given to us.
270207753Smm	if (lz_options.preset_dict != NULL
271207753Smm			&& lz_options.preset_dict_size > 0) {
272207753Smm		// If the preset dictionary is bigger than the actual
273207753Smm		// dictionary, copy only the tail.
274213700Smm		const size_t copy_size = my_min(lz_options.preset_dict_size,
275207753Smm				lz_options.dict_size);
276207753Smm		const size_t offset = lz_options.preset_dict_size - copy_size;
277312518Sdelphij		memcpy(coder->dict.buf, lz_options.preset_dict + offset,
278207753Smm				copy_size);
279312518Sdelphij		coder->dict.pos = copy_size;
280312518Sdelphij		coder->dict.full = copy_size;
281207753Smm	}
282207753Smm
283207753Smm	// Miscellaneous initializations
284312518Sdelphij	coder->next_finished = false;
285312518Sdelphij	coder->this_finished = false;
286312518Sdelphij	coder->temp.pos = 0;
287312518Sdelphij	coder->temp.size = 0;
288207753Smm
289207753Smm	// Initialize the next filter in the chain, if any.
290312518Sdelphij	return lzma_next_filter_init(&coder->next, allocator, filters + 1);
291207753Smm}
292207753Smm
293207753Smm
294207753Smmextern uint64_t
295207753Smmlzma_lz_decoder_memusage(size_t dictionary_size)
296207753Smm{
297207753Smm	return sizeof(lzma_coder) + (uint64_t)(dictionary_size);
298207753Smm}
299207753Smm
300207753Smm
301207753Smmextern void
302312518Sdelphijlzma_lz_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size)
303207753Smm{
304312518Sdelphij	lzma_coder *coder = coder_ptr;
305207753Smm	coder->lz.set_uncompressed(coder->lz.coder, uncompressed_size);
306207753Smm}
307