1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       alone_decoder.c
4 /// \brief      Decoder for LZMA_Alone files
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12 
13 #include "alone_decoder.h"
14 #include "lzma_decoder.h"
15 #include "lz_decoder.h"
16 
17 
18 typedef struct {
19 	lzma_next_coder next;
20 
21 	enum {
22 		SEQ_PROPERTIES,
23 		SEQ_DICTIONARY_SIZE,
24 		SEQ_UNCOMPRESSED_SIZE,
25 		SEQ_CODER_INIT,
26 		SEQ_CODE,
27 	} sequence;
28 
29 	/// If true, reject files that are unlikely to be .lzma files.
30 	/// If false, more non-.lzma files get accepted and will give
31 	/// LZMA_DATA_ERROR either immediately or after a few output bytes.
32 	bool picky;
33 
34 	/// Position in the header fields
35 	size_t pos;
36 
37 	/// Uncompressed size decoded from the header
38 	lzma_vli uncompressed_size;
39 
40 	/// Memory usage limit
41 	uint64_t memlimit;
42 
43 	/// Amount of memory actually needed (only an estimate)
44 	uint64_t memusage;
45 
46 	/// Options decoded from the header needed to initialize
47 	/// the LZMA decoder
48 	lzma_options_lzma options;
49 } lzma_alone_coder;
50 
51 
52 static lzma_ret
53 alone_decode(void *coder_ptr, const lzma_allocator *allocator,
54 		const uint8_t *restrict in, size_t *restrict in_pos,
55 		size_t in_size, uint8_t *restrict out,
56 		size_t *restrict out_pos, size_t out_size,
57 		lzma_action action)
58 {
59 	lzma_alone_coder *coder = coder_ptr;
60 
61 	while (*out_pos < out_size
62 			&& (coder->sequence == SEQ_CODE || *in_pos < in_size))
63 	switch (coder->sequence) {
64 	case SEQ_PROPERTIES:
65 		if (lzma_lzma_lclppb_decode(&coder->options, in[*in_pos]))
66 			return LZMA_FORMAT_ERROR;
67 
68 		coder->sequence = SEQ_DICTIONARY_SIZE;
69 		++*in_pos;
70 		break;
71 
72 	case SEQ_DICTIONARY_SIZE:
73 		coder->options.dict_size
74 				|= (size_t)(in[*in_pos]) << (coder->pos * 8);
75 
76 		if (++coder->pos == 4) {
77 			if (coder->picky && coder->options.dict_size
78 					!= UINT32_MAX) {
79 				// A hack to ditch tons of false positives:
80 				// We allow only dictionary sizes that are
81 				// 2^n or 2^n + 2^(n-1). LZMA_Alone created
82 				// only files with 2^n, but accepts any
83 				// dictionary size.
84 				uint32_t d = coder->options.dict_size - 1;
85 				d |= d >> 2;
86 				d |= d >> 3;
87 				d |= d >> 4;
88 				d |= d >> 8;
89 				d |= d >> 16;
90 				++d;
91 
92 				if (d != coder->options.dict_size)
93 					return LZMA_FORMAT_ERROR;
94 			}
95 
96 			coder->pos = 0;
97 			coder->sequence = SEQ_UNCOMPRESSED_SIZE;
98 		}
99 
100 		++*in_pos;
101 		break;
102 
103 	case SEQ_UNCOMPRESSED_SIZE:
104 		coder->uncompressed_size
105 				|= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
106 		++*in_pos;
107 		if (++coder->pos < 8)
108 			break;
109 
110 		// Another hack to ditch false positives: Assume that
111 		// if the uncompressed size is known, it must be less
112 		// than 256 GiB.
113 		//
114 		// FIXME? Without picky we allow > LZMA_VLI_MAX which doesn't
115 		// really matter in this specific situation (> LZMA_VLI_MAX is
116 		// safe in the LZMA decoder) but it's somewhat weird still.
117 		if (coder->picky
118 				&& coder->uncompressed_size != LZMA_VLI_UNKNOWN
119 				&& coder->uncompressed_size
120 					>= (LZMA_VLI_C(1) << 38))
121 			return LZMA_FORMAT_ERROR;
122 
123 		// Use LZMA_FILTER_LZMA1EXT features to specify the
124 		// uncompressed size and that the end marker is allowed
125 		// even when the uncompressed size is known. Both .lzma
126 		// header and LZMA1EXT use UINT64_MAX indicate that size
127 		// is unknown.
128 		coder->options.ext_flags = LZMA_LZMA1EXT_ALLOW_EOPM;
129 		lzma_set_ext_size(coder->options, coder->uncompressed_size);
130 
131 		// Calculate the memory usage so that it is ready
132 		// for SEQ_CODER_INIT.
133 		coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
134 				+ LZMA_MEMUSAGE_BASE;
135 
136 		coder->pos = 0;
137 		coder->sequence = SEQ_CODER_INIT;
138 
139 	// Fall through
140 
141 	case SEQ_CODER_INIT: {
142 		if (coder->memusage > coder->memlimit)
143 			return LZMA_MEMLIMIT_ERROR;
144 
145 		lzma_filter_info filters[2] = {
146 			{
147 				.id = LZMA_FILTER_LZMA1EXT,
148 				.init = &lzma_lzma_decoder_init,
149 				.options = &coder->options,
150 			}, {
151 				.init = NULL,
152 			}
153 		};
154 
155 		return_if_error(lzma_next_filter_init(&coder->next,
156 				allocator, filters));
157 
158 		coder->sequence = SEQ_CODE;
159 		break;
160 	}
161 
162 	case SEQ_CODE: {
163 		return coder->next.code(coder->next.coder,
164 				allocator, in, in_pos, in_size,
165 				out, out_pos, out_size, action);
166 	}
167 
168 	default:
169 		return LZMA_PROG_ERROR;
170 	}
171 
172 	return LZMA_OK;
173 }
174 
175 
176 static void
177 alone_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
178 {
179 	lzma_alone_coder *coder = coder_ptr;
180 	lzma_next_end(&coder->next, allocator);
181 	lzma_free(coder, allocator);
182 	return;
183 }
184 
185 
186 static lzma_ret
187 alone_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
188 		uint64_t *old_memlimit, uint64_t new_memlimit)
189 {
190 	lzma_alone_coder *coder = coder_ptr;
191 
192 	*memusage = coder->memusage;
193 	*old_memlimit = coder->memlimit;
194 
195 	if (new_memlimit != 0) {
196 		if (new_memlimit < coder->memusage)
197 			return LZMA_MEMLIMIT_ERROR;
198 
199 		coder->memlimit = new_memlimit;
200 	}
201 
202 	return LZMA_OK;
203 }
204 
205 
206 extern lzma_ret
207 lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
208 		uint64_t memlimit, bool picky)
209 {
210 	lzma_next_coder_init(&lzma_alone_decoder_init, next, allocator);
211 
212 	lzma_alone_coder *coder = next->coder;
213 
214 	if (coder == NULL) {
215 		coder = lzma_alloc(sizeof(lzma_alone_coder), allocator);
216 		if (coder == NULL)
217 			return LZMA_MEM_ERROR;
218 
219 		next->coder = coder;
220 		next->code = &alone_decode;
221 		next->end = &alone_decoder_end;
222 		next->memconfig = &alone_decoder_memconfig;
223 		coder->next = LZMA_NEXT_CODER_INIT;
224 	}
225 
226 	coder->sequence = SEQ_PROPERTIES;
227 	coder->picky = picky;
228 	coder->pos = 0;
229 	coder->options.dict_size = 0;
230 	coder->options.preset_dict = NULL;
231 	coder->options.preset_dict_size = 0;
232 	coder->uncompressed_size = 0;
233 	coder->memlimit = my_max(1, memlimit);
234 	coder->memusage = LZMA_MEMUSAGE_BASE;
235 
236 	return LZMA_OK;
237 }
238 
239 
240 extern LZMA_API(lzma_ret)
241 lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
242 {
243 	lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit, false);
244 
245 	strm->internal->supported_actions[LZMA_RUN] = true;
246 	strm->internal->supported_actions[LZMA_FINISH] = true;
247 
248 	return LZMA_OK;
249 }
250