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 /// Position in the header fields
32 /// Uncompressed size decoded from the header
33 lzma_vli uncompressed_size;
35 /// Memory usage limit
38 /// Amount of memory actually needed (only an estimate)
41 /// Options decoded from the header needed to initialize
43 lzma_options_lzma options;
48 alone_decode(lzma_coder *coder,
49 lzma_allocator *allocator lzma_attribute((__unused__)),
50 const uint8_t *restrict in, size_t *restrict in_pos,
51 size_t in_size, uint8_t *restrict out,
52 size_t *restrict out_pos, size_t out_size,
55 while (*out_pos < out_size
56 && (coder->sequence == SEQ_CODE || *in_pos < in_size))
57 switch (coder->sequence) {
59 if (lzma_lzma_lclppb_decode(&coder->options, in[*in_pos]))
60 return LZMA_FORMAT_ERROR;
62 coder->sequence = SEQ_DICTIONARY_SIZE;
66 case SEQ_DICTIONARY_SIZE:
67 coder->options.dict_size
68 |= (size_t)(in[*in_pos]) << (coder->pos * 8);
70 if (++coder->pos == 4) {
71 if (coder->options.dict_size != UINT32_MAX) {
72 // A hack to ditch tons of false positives:
73 // We allow only dictionary sizes that are
74 // 2^n or 2^n + 2^(n-1). LZMA_Alone created
75 // only files with 2^n, but accepts any
76 // dictionary size. If someone complains, this
77 // will be reconsidered.
78 uint32_t d = coder->options.dict_size - 1;
86 if (d != coder->options.dict_size)
87 return LZMA_FORMAT_ERROR;
91 coder->sequence = SEQ_UNCOMPRESSED_SIZE;
97 case SEQ_UNCOMPRESSED_SIZE:
98 coder->uncompressed_size
99 |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
101 if (++coder->pos < 8)
104 // Another hack to ditch false positives: Assume that
105 // if the uncompressed size is known, it must be less
106 // than 256 GiB. Again, if someone complains, this
107 // will be reconsidered.
108 if (coder->uncompressed_size != LZMA_VLI_UNKNOWN
109 && coder->uncompressed_size
110 >= (LZMA_VLI_C(1) << 38))
111 return LZMA_FORMAT_ERROR;
113 // Calculate the memory usage so that it is ready
114 // for SEQ_CODER_INIT.
115 coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
116 + LZMA_MEMUSAGE_BASE;
119 coder->sequence = SEQ_CODER_INIT;
123 case SEQ_CODER_INIT: {
124 if (coder->memusage > coder->memlimit)
125 return LZMA_MEMLIMIT_ERROR;
127 lzma_filter_info filters[2] = {
129 .init = &lzma_lzma_decoder_init,
130 .options = &coder->options,
136 const lzma_ret ret = lzma_next_filter_init(&coder->next,
141 // Use a hack to set the uncompressed size.
142 lzma_lz_decoder_uncompressed(coder->next.coder,
143 coder->uncompressed_size);
145 coder->sequence = SEQ_CODE;
150 return coder->next.code(coder->next.coder,
151 allocator, in, in_pos, in_size,
152 out, out_pos, out_size, action);
156 return LZMA_PROG_ERROR;
164 alone_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
166 lzma_next_end(&coder->next, allocator);
167 lzma_free(coder, allocator);
173 alone_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
174 uint64_t *old_memlimit, uint64_t new_memlimit)
176 *memusage = coder->memusage;
177 *old_memlimit = coder->memlimit;
179 if (new_memlimit != 0) {
180 if (new_memlimit < coder->memusage)
181 return LZMA_MEMLIMIT_ERROR;
183 coder->memlimit = new_memlimit;
191 lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
194 lzma_next_coder_init(&lzma_alone_decoder_init, next, allocator);
197 return LZMA_PROG_ERROR;
199 if (next->coder == NULL) {
200 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
201 if (next->coder == NULL)
202 return LZMA_MEM_ERROR;
204 next->code = &alone_decode;
205 next->end = &alone_decoder_end;
206 next->memconfig = &alone_decoder_memconfig;
207 next->coder->next = LZMA_NEXT_CODER_INIT;
210 next->coder->sequence = SEQ_PROPERTIES;
211 next->coder->pos = 0;
212 next->coder->options.dict_size = 0;
213 next->coder->options.preset_dict = NULL;
214 next->coder->options.preset_dict_size = 0;
215 next->coder->uncompressed_size = 0;
216 next->coder->memlimit = memlimit;
217 next->coder->memusage = LZMA_MEMUSAGE_BASE;
223 extern LZMA_API(lzma_ret)
224 lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
226 lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit);
228 strm->internal->supported_actions[LZMA_RUN] = true;
229 strm->internal->supported_actions[LZMA_FINISH] = true;