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 /// Options decoded from the header needed to initialize
47 lzma_options_lzma options;
52 alone_decode(lzma_coder *coder,
53 lzma_allocator *allocator lzma_attribute((unused)),
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 while (*out_pos < out_size
60 && (coder->sequence == SEQ_CODE || *in_pos < in_size))
61 switch (coder->sequence) {
63 if (lzma_lzma_lclppb_decode(&coder->options, in[*in_pos]))
64 return LZMA_FORMAT_ERROR;
66 coder->sequence = SEQ_DICTIONARY_SIZE;
70 case SEQ_DICTIONARY_SIZE:
71 coder->options.dict_size
72 |= (size_t)(in[*in_pos]) << (coder->pos * 8);
74 if (++coder->pos == 4) {
75 if (coder->options.dict_size > (UINT32_C(1) << 30))
76 return LZMA_FORMAT_ERROR;
78 // A hack to ditch tons of false positives: We allow
79 // only dictionary sizes that are 2^n or 2^n + 2^(n-1).
80 // LZMA_Alone created only files with 2^n, but accepts
81 // any dictionary size. If someone complains, this
82 // will be reconsidered.
83 uint32_t d = coder->options.dict_size - 1;
91 if (d != coder->options.dict_size)
92 return LZMA_FORMAT_ERROR;
95 coder->sequence = SEQ_UNCOMPRESSED_SIZE;
101 case SEQ_UNCOMPRESSED_SIZE:
102 coder->uncompressed_size
103 |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
105 if (++coder->pos == 8) {
106 // Another hack to ditch false positives: Assume that
107 // if the uncompressed size is known, it must be less
108 // than 256 GiB. Again, if someone complains, this
109 // will be reconsidered.
110 if (coder->uncompressed_size != LZMA_VLI_UNKNOWN
111 && coder->uncompressed_size
112 >= (LZMA_VLI_C(1) << 38))
113 return LZMA_FORMAT_ERROR;
116 coder->sequence = SEQ_CODER_INIT;
122 case SEQ_CODER_INIT: {
123 // FIXME It is unfair that this doesn't add a fixed amount
124 // like lzma_memusage_common() does.
125 const uint64_t memusage
126 = lzma_lzma_decoder_memusage(&coder->options);
128 // Use LZMA_PROG_ERROR since LZMA_Alone decoder cannot be
129 // built without LZMA support.
130 // FIXME TODO Make the above comment true.
131 if (memusage == UINT64_MAX)
132 return LZMA_PROG_ERROR;
134 if (memusage > coder->memlimit)
135 return LZMA_MEMLIMIT_ERROR;
137 lzma_filter_info filters[2] = {
139 .init = &lzma_lzma_decoder_init,
140 .options = &coder->options,
146 const lzma_ret ret = lzma_next_filter_init(&coder->next,
151 // Use a hack to set the uncompressed size.
152 lzma_lz_decoder_uncompressed(coder->next.coder,
153 coder->uncompressed_size);
155 coder->sequence = SEQ_CODE;
161 return coder->next.code(coder->next.coder,
162 allocator, in, in_pos, in_size,
163 out, out_pos, out_size, action);
167 return LZMA_PROG_ERROR;
175 alone_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
177 lzma_next_end(&coder->next, allocator);
178 lzma_free(coder, allocator);
184 lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
187 lzma_next_coder_init(lzma_alone_decoder_init, next, allocator);
189 if (next->coder == NULL) {
190 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
191 if (next->coder == NULL)
192 return LZMA_MEM_ERROR;
194 next->code = &alone_decode;
195 next->end = &alone_decoder_end;
196 next->coder->next = LZMA_NEXT_CODER_INIT;
199 next->coder->sequence = SEQ_PROPERTIES;
200 next->coder->pos = 0;
201 next->coder->options.dict_size = 0;
202 next->coder->uncompressed_size = 0;
203 next->coder->memlimit = memlimit;
209 extern LZMA_API lzma_ret
210 lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
212 lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit);
214 strm->internal->supported_actions[LZMA_RUN] = true;
215 strm->internal->supported_actions[LZMA_FINISH] = true;