1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file alone_decoder.c
4 /// \brief Decoder for LZMA_Alone files
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "alone_decoder.h"
14 #include "lzma_decoder.h"
15 #include "lz_decoder.h"
24 SEQ_UNCOMPRESSED_SIZE,
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.
34 /// Position in the header fields
37 /// Uncompressed size decoded from the header
38 lzma_vli uncompressed_size;
40 /// Memory usage limit
43 /// Amount of memory actually needed (only an estimate)
46 /// Options decoded from the header needed to initialize
48 lzma_options_lzma options;
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,
59 lzma_alone_coder *coder = coder_ptr;
61 while (*out_pos < out_size
62 && (coder->sequence == SEQ_CODE || *in_pos < in_size))
63 switch (coder->sequence) {
65 if (lzma_lzma_lclppb_decode(&coder->options, in[*in_pos]))
66 return LZMA_FORMAT_ERROR;
68 coder->sequence = SEQ_DICTIONARY_SIZE;
72 case SEQ_DICTIONARY_SIZE:
73 coder->options.dict_size
74 |= (size_t)(in[*in_pos]) << (coder->pos * 8);
76 if (++coder->pos == 4) {
77 if (coder->picky && coder->options.dict_size
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
84 uint32_t d = coder->options.dict_size - 1;
92 if (d != coder->options.dict_size)
93 return LZMA_FORMAT_ERROR;
97 coder->sequence = SEQ_UNCOMPRESSED_SIZE;
103 case SEQ_UNCOMPRESSED_SIZE:
104 coder->uncompressed_size
105 |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
107 if (++coder->pos < 8)
110 // Another hack to ditch false positives: Assume that
111 // if the uncompressed size is known, it must be less
114 && coder->uncompressed_size != LZMA_VLI_UNKNOWN
115 && coder->uncompressed_size
116 >= (LZMA_VLI_C(1) << 38))
117 return LZMA_FORMAT_ERROR;
119 // Calculate the memory usage so that it is ready
120 // for SEQ_CODER_INIT.
121 coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
122 + LZMA_MEMUSAGE_BASE;
125 coder->sequence = SEQ_CODER_INIT;
129 case SEQ_CODER_INIT: {
130 if (coder->memusage > coder->memlimit)
131 return LZMA_MEMLIMIT_ERROR;
133 lzma_filter_info filters[2] = {
135 .init = &lzma_lzma_decoder_init,
136 .options = &coder->options,
142 const lzma_ret ret = lzma_next_filter_init(&coder->next,
147 // Use a hack to set the uncompressed size.
148 lzma_lz_decoder_uncompressed(coder->next.coder,
149 coder->uncompressed_size);
151 coder->sequence = SEQ_CODE;
156 return coder->next.code(coder->next.coder,
157 allocator, in, in_pos, in_size,
158 out, out_pos, out_size, action);
162 return LZMA_PROG_ERROR;
170 alone_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
172 lzma_alone_coder *coder = coder_ptr;
173 lzma_next_end(&coder->next, allocator);
174 lzma_free(coder, allocator);
180 alone_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
181 uint64_t *old_memlimit, uint64_t new_memlimit)
183 lzma_alone_coder *coder = coder_ptr;
185 *memusage = coder->memusage;
186 *old_memlimit = coder->memlimit;
188 if (new_memlimit != 0) {
189 if (new_memlimit < coder->memusage)
190 return LZMA_MEMLIMIT_ERROR;
192 coder->memlimit = new_memlimit;
200 lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
201 uint64_t memlimit, bool picky)
203 lzma_next_coder_init(&lzma_alone_decoder_init, next, allocator);
205 lzma_alone_coder *coder = next->coder;
208 coder = lzma_alloc(sizeof(lzma_alone_coder), allocator);
210 return LZMA_MEM_ERROR;
213 next->code = &alone_decode;
214 next->end = &alone_decoder_end;
215 next->memconfig = &alone_decoder_memconfig;
216 coder->next = LZMA_NEXT_CODER_INIT;
219 coder->sequence = SEQ_PROPERTIES;
220 coder->picky = picky;
222 coder->options.dict_size = 0;
223 coder->options.preset_dict = NULL;
224 coder->options.preset_dict_size = 0;
225 coder->uncompressed_size = 0;
226 coder->memlimit = my_max(1, memlimit);
227 coder->memusage = LZMA_MEMUSAGE_BASE;
233 extern LZMA_API(lzma_ret)
234 lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
236 lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit, false);
238 strm->internal->supported_actions[LZMA_RUN] = true;
239 strm->internal->supported_actions[LZMA_FINISH] = true;