1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file alone_decoder.c
4 /// \brief Decoder for LZMA_Alone files
6 // Copyright (C) 2007 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "alone_decoder.h"
21 #include "lzma_decoder.h"
22 #include "lz_decoder.h"
31 SEQ_UNCOMPRESSED_SIZE,
36 /// Position in the header fields
39 /// Uncompressed size decoded from the header
40 lzma_vli uncompressed_size;
42 /// Memory usage limit
45 /// Amount of memory actually needed (only an estimate)
48 /// Options decoded from the header needed to initialize
50 lzma_options_lzma options;
55 alone_decode(lzma_coder *coder,
56 lzma_allocator *allocator lzma_attribute((unused)),
57 const uint8_t *restrict in, size_t *restrict in_pos,
58 size_t in_size, uint8_t *restrict out,
59 size_t *restrict out_pos, size_t out_size,
62 while (*out_pos < out_size
63 && (coder->sequence == SEQ_CODE || *in_pos < in_size))
64 switch (coder->sequence) {
66 if (lzma_lzma_lclppb_decode(&coder->options, in[*in_pos]))
67 return LZMA_FORMAT_ERROR;
69 coder->sequence = SEQ_DICTIONARY_SIZE;
73 case SEQ_DICTIONARY_SIZE:
74 coder->options.dict_size
75 |= (size_t)(in[*in_pos]) << (coder->pos * 8);
77 if (++coder->pos == 4) {
78 if (coder->options.dict_size != 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. If someone complains, this
84 // will be reconsidered.
85 uint32_t d = coder->options.dict_size - 1;
93 if (d != coder->options.dict_size)
94 return LZMA_FORMAT_ERROR;
98 coder->sequence = SEQ_UNCOMPRESSED_SIZE;
104 case SEQ_UNCOMPRESSED_SIZE:
105 coder->uncompressed_size
106 |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
108 if (++coder->pos < 8)
111 // Another hack to ditch false positives: Assume that
112 // if the uncompressed size is known, it must be less
113 // than 256 GiB. Again, if someone complains, this
114 // will be reconsidered.
115 if (coder->uncompressed_size != LZMA_VLI_UNKNOWN
116 && coder->uncompressed_size
117 >= (LZMA_VLI_C(1) << 38))
118 return LZMA_FORMAT_ERROR;
120 // Calculate the memory usage so that it is ready
121 // for SEQ_CODER_INIT.
122 coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
123 + LZMA_MEMUSAGE_BASE;
126 coder->sequence = SEQ_CODER_INIT;
130 case SEQ_CODER_INIT: {
131 if (coder->memusage > coder->memlimit)
132 return LZMA_MEMLIMIT_ERROR;
134 lzma_filter_info filters[2] = {
136 .init = &lzma_lzma_decoder_init,
137 .options = &coder->options,
143 const lzma_ret ret = lzma_next_filter_init(&coder->next,
148 // Use a hack to set the uncompressed size.
149 lzma_lz_decoder_uncompressed(coder->next.coder,
150 coder->uncompressed_size);
152 coder->sequence = SEQ_CODE;
157 return coder->next.code(coder->next.coder,
158 allocator, in, in_pos, in_size,
159 out, out_pos, out_size, action);
163 return LZMA_PROG_ERROR;
171 alone_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
173 lzma_next_end(&coder->next, allocator);
174 lzma_free(coder, allocator);
180 alone_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
181 uint64_t *old_memlimit, uint64_t new_memlimit)
183 if (new_memlimit != 0 && new_memlimit < coder->memusage)
184 return LZMA_MEMLIMIT_ERROR;
186 *memusage = coder->memusage;
187 *old_memlimit = coder->memlimit;
188 coder->memlimit = new_memlimit;
195 lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
198 lzma_next_coder_init(lzma_alone_decoder_init, next, allocator);
201 return LZMA_PROG_ERROR;
203 if (next->coder == NULL) {
204 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
205 if (next->coder == NULL)
206 return LZMA_MEM_ERROR;
208 next->code = &alone_decode;
209 next->end = &alone_decoder_end;
210 next->memconfig = &alone_decoder_memconfig;
211 next->coder->next = LZMA_NEXT_CODER_INIT;
214 next->coder->sequence = SEQ_PROPERTIES;
215 next->coder->pos = 0;
216 next->coder->options.dict_size = 0;
217 next->coder->options.preset_dict = NULL;
218 next->coder->options.preset_dict_size = 0;
219 next->coder->uncompressed_size = 0;
220 next->coder->memlimit = memlimit;
221 next->coder->memusage = LZMA_MEMUSAGE_BASE;
227 extern LZMA_API(lzma_ret)
228 lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
230 lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit);
232 strm->internal->supported_actions[LZMA_RUN] = true;
233 strm->internal->supported_actions[LZMA_FINISH] = true;