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
23312517Sdelphijtypedef 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;
51312517Sdelphij} 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[]
94360523Sdelphij		// buffer. Do it conditionally because out can be NULL
95360523Sdelphij		// (in which case copy_size is always 0). Calling memcpy()
96360523Sdelphij		// with a null-pointer is undefined even if the third
97360523Sdelphij		// argument is 0.
98207753Smm		const size_t copy_size = coder->dict.pos - dict_start;
99207753Smm		assert(copy_size <= out_size - *out_pos);
100360523Sdelphij
101360523Sdelphij		if (copy_size > 0)
102360523Sdelphij			memcpy(out + *out_pos, coder->dict.buf + dict_start,
103360523Sdelphij					copy_size);
104360523Sdelphij
105207753Smm		*out_pos += copy_size;
106207753Smm
107207753Smm		// Reset the dictionary if so requested by coder->lz.code().
108207753Smm		if (coder->dict.need_reset) {
109207753Smm			lz_decoder_reset(coder);
110207753Smm
111207753Smm			// Since we reset dictionary, we don't check if
112207753Smm			// dictionary became full.
113207753Smm			if (ret != LZMA_OK || *out_pos == out_size)
114207753Smm				return ret;
115207753Smm		} else {
116207753Smm			// Return if everything got decoded or an error
117207753Smm			// occurred, or if there's no more data to decode.
118207753Smm			//
119207753Smm			// Note that detecting if there's something to decode
120207753Smm			// is done by looking if dictionary become full
121207753Smm			// instead of looking if *in_pos == in_size. This
122207753Smm			// is because it is possible that all the input was
123207753Smm			// consumed already but some data is pending to be
124207753Smm			// written to the dictionary.
125207753Smm			if (ret != LZMA_OK || *out_pos == out_size
126207753Smm					|| coder->dict.pos < coder->dict.size)
127207753Smm				return ret;
128207753Smm		}
129207753Smm	}
130207753Smm}
131207753Smm
132207753Smm
133207753Smmstatic lzma_ret
134360523Sdelphijlz_decode(void *coder_ptr, const lzma_allocator *allocator,
135207753Smm		const uint8_t *restrict in, size_t *restrict in_pos,
136207753Smm		size_t in_size, uint8_t *restrict out,
137207753Smm		size_t *restrict out_pos, size_t out_size,
138207753Smm		lzma_action action)
139207753Smm{
140312517Sdelphij	lzma_coder *coder = coder_ptr;
141312517Sdelphij
142207753Smm	if (coder->next.code == NULL)
143207753Smm		return decode_buffer(coder, in, in_pos, in_size,
144207753Smm				out, out_pos, out_size);
145207753Smm
146207753Smm	// We aren't the last coder in the chain, we need to decode
147207753Smm	// our input to a temporary buffer.
148207753Smm	while (*out_pos < out_size) {
149207753Smm		// Fill the temporary buffer if it is empty.
150207753Smm		if (!coder->next_finished
151207753Smm				&& coder->temp.pos == coder->temp.size) {
152207753Smm			coder->temp.pos = 0;
153207753Smm			coder->temp.size = 0;
154207753Smm
155207753Smm			const lzma_ret ret = coder->next.code(
156207753Smm					coder->next.coder,
157207753Smm					allocator, in, in_pos, in_size,
158207753Smm					coder->temp.buffer, &coder->temp.size,
159207753Smm					LZMA_BUFFER_SIZE, action);
160207753Smm
161207753Smm			if (ret == LZMA_STREAM_END)
162207753Smm				coder->next_finished = true;
163207753Smm			else if (ret != LZMA_OK || coder->temp.size == 0)
164207753Smm				return ret;
165207753Smm		}
166207753Smm
167207753Smm		if (coder->this_finished) {
168207753Smm			if (coder->temp.size != 0)
169207753Smm				return LZMA_DATA_ERROR;
170207753Smm
171207753Smm			if (coder->next_finished)
172207753Smm				return LZMA_STREAM_END;
173207753Smm
174207753Smm			return LZMA_OK;
175207753Smm		}
176207753Smm
177207753Smm		const lzma_ret ret = decode_buffer(coder, coder->temp.buffer,
178207753Smm				&coder->temp.pos, coder->temp.size,
179207753Smm				out, out_pos, out_size);
180207753Smm
181207753Smm		if (ret == LZMA_STREAM_END)
182207753Smm			coder->this_finished = true;
183207753Smm		else if (ret != LZMA_OK)
184207753Smm			return ret;
185207753Smm		else if (coder->next_finished && *out_pos < out_size)
186207753Smm			return LZMA_DATA_ERROR;
187207753Smm	}
188207753Smm
189207753Smm	return LZMA_OK;
190207753Smm}
191207753Smm
192207753Smm
193207753Smmstatic void
194312517Sdelphijlz_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
195207753Smm{
196312517Sdelphij	lzma_coder *coder = coder_ptr;
197312517Sdelphij
198207753Smm	lzma_next_end(&coder->next, allocator);
199207753Smm	lzma_free(coder->dict.buf, allocator);
200207753Smm
201207753Smm	if (coder->lz.end != NULL)
202207753Smm		coder->lz.end(coder->lz.coder, allocator);
203207753Smm	else
204207753Smm		lzma_free(coder->lz.coder, allocator);
205207753Smm
206207753Smm	lzma_free(coder, allocator);
207207753Smm	return;
208207753Smm}
209207753Smm
210207753Smm
211207753Smmextern lzma_ret
212278433Srpaulolzma_lz_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
213207753Smm		const lzma_filter_info *filters,
214207753Smm		lzma_ret (*lz_init)(lzma_lz_decoder *lz,
215278433Srpaulo			const lzma_allocator *allocator, const void *options,
216207753Smm			lzma_lz_options *lz_options))
217207753Smm{
218207753Smm	// Allocate the base structure if it isn't already allocated.
219312517Sdelphij	lzma_coder *coder = next->coder;
220312517Sdelphij	if (coder == NULL) {
221312517Sdelphij		coder = lzma_alloc(sizeof(lzma_coder), allocator);
222312517Sdelphij		if (coder == NULL)
223207753Smm			return LZMA_MEM_ERROR;
224207753Smm
225312517Sdelphij		next->coder = coder;
226207753Smm		next->code = &lz_decode;
227207753Smm		next->end = &lz_decoder_end;
228207753Smm
229312517Sdelphij		coder->dict.buf = NULL;
230312517Sdelphij		coder->dict.size = 0;
231312517Sdelphij		coder->lz = LZMA_LZ_DECODER_INIT;
232312517Sdelphij		coder->next = LZMA_NEXT_CODER_INIT;
233207753Smm	}
234207753Smm
235207753Smm	// Allocate and initialize the LZ-based decoder. It will also give
236207753Smm	// us the dictionary size.
237207753Smm	lzma_lz_options lz_options;
238312517Sdelphij	return_if_error(lz_init(&coder->lz, allocator,
239207753Smm			filters[0].options, &lz_options));
240207753Smm
241207753Smm	// If the dictionary size is very small, increase it to 4096 bytes.
242207753Smm	// This is to prevent constant wrapping of the dictionary, which
243207753Smm	// would slow things down. The downside is that since we don't check
244207753Smm	// separately for the real dictionary size, we may happily accept
245207753Smm	// corrupt files.
246207753Smm	if (lz_options.dict_size < 4096)
247207753Smm		lz_options.dict_size = 4096;
248207753Smm
249360523Sdelphij	// Make dictionary size a multiple of 16. Some LZ-based decoders like
250207753Smm	// LZMA use the lowest bits lzma_dict.pos to know the alignment of the
251207753Smm	// data. Aligned buffer is also good when memcpying from the
252207753Smm	// dictionary to the output buffer, since applications are
253207753Smm	// recommended to give aligned buffers to liblzma.
254207753Smm	//
255207753Smm	// Avoid integer overflow.
256207753Smm	if (lz_options.dict_size > SIZE_MAX - 15)
257207753Smm		return LZMA_MEM_ERROR;
258207753Smm
259207753Smm	lz_options.dict_size = (lz_options.dict_size + 15) & ~((size_t)(15));
260207753Smm
261207753Smm	// Allocate and initialize the dictionary.
262312517Sdelphij	if (coder->dict.size != lz_options.dict_size) {
263312517Sdelphij		lzma_free(coder->dict.buf, allocator);
264312517Sdelphij		coder->dict.buf
265207753Smm				= lzma_alloc(lz_options.dict_size, allocator);
266312517Sdelphij		if (coder->dict.buf == NULL)
267207753Smm			return LZMA_MEM_ERROR;
268207753Smm
269312517Sdelphij		coder->dict.size = lz_options.dict_size;
270207753Smm	}
271207753Smm
272207753Smm	lz_decoder_reset(next->coder);
273207753Smm
274207753Smm	// Use the preset dictionary if it was given to us.
275207753Smm	if (lz_options.preset_dict != NULL
276207753Smm			&& lz_options.preset_dict_size > 0) {
277207753Smm		// If the preset dictionary is bigger than the actual
278207753Smm		// dictionary, copy only the tail.
279213700Smm		const size_t copy_size = my_min(lz_options.preset_dict_size,
280207753Smm				lz_options.dict_size);
281207753Smm		const size_t offset = lz_options.preset_dict_size - copy_size;
282312517Sdelphij		memcpy(coder->dict.buf, lz_options.preset_dict + offset,
283207753Smm				copy_size);
284312517Sdelphij		coder->dict.pos = copy_size;
285312517Sdelphij		coder->dict.full = copy_size;
286207753Smm	}
287207753Smm
288207753Smm	// Miscellaneous initializations
289312517Sdelphij	coder->next_finished = false;
290312517Sdelphij	coder->this_finished = false;
291312517Sdelphij	coder->temp.pos = 0;
292312517Sdelphij	coder->temp.size = 0;
293207753Smm
294207753Smm	// Initialize the next filter in the chain, if any.
295312517Sdelphij	return lzma_next_filter_init(&coder->next, allocator, filters + 1);
296207753Smm}
297207753Smm
298207753Smm
299207753Smmextern uint64_t
300207753Smmlzma_lz_decoder_memusage(size_t dictionary_size)
301207753Smm{
302207753Smm	return sizeof(lzma_coder) + (uint64_t)(dictionary_size);
303207753Smm}
304207753Smm
305207753Smm
306207753Smmextern void
307312517Sdelphijlzma_lz_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size)
308207753Smm{
309312517Sdelphij	lzma_coder *coder = coder_ptr;
310207753Smm	coder->lz.set_uncompressed(coder->lz.coder, uncompressed_size);
311207753Smm}
312