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.dictionary_size
72 |= (size_t)(in[*in_pos]) << (coder->pos * 8);
74 if (++coder->pos == 4) {
75 if (coder->options.dictionary_size
76 > LZMA_DICTIONARY_SIZE_MAX)
77 return LZMA_FORMAT_ERROR;
79 // A hack to ditch tons of false positives: We allow
80 // only dictionary sizes that are 2^n or 2^n + 2^(n-1).
81 // LZMA_Alone created only files with 2^n, but accepts
82 // any dictionary size. If someone complains, this
83 // will be reconsidered.
84 uint32_t d = coder->options.dictionary_size - 1;
92 if (d != coder->options.dictionary_size)
93 return LZMA_FORMAT_ERROR;
96 coder->sequence = SEQ_UNCOMPRESSED_SIZE;
102 case SEQ_UNCOMPRESSED_SIZE:
103 coder->uncompressed_size
104 |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
106 if (++coder->pos == 8) {
107 // Another hack to ditch false positives: Assume that
108 // if the uncompressed size is known, it must be less
109 // than 256 GiB. Again, if someone complains, this
110 // will be reconsidered.
111 if (coder->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN
112 && coder->uncompressed_size
113 >= (LZMA_VLI_C(1) << 38))
114 return LZMA_FORMAT_ERROR;
117 coder->sequence = SEQ_CODER_INIT;
123 case SEQ_CODER_INIT: {
124 // FIXME It is unfair that this doesn't add a fixed amount
125 // like lzma_memusage_common() does.
126 const uint64_t memusage
127 = lzma_lzma_decoder_memusage(&coder->options);
129 // Use LZMA_PROG_ERROR since LZMA_Alone decoder cannot be
130 // built without LZMA support.
131 // FIXME TODO Make the above comment true.
132 if (memusage == UINT64_MAX)
133 return LZMA_PROG_ERROR;
135 if (memusage > coder->memlimit)
136 return LZMA_MEMLIMIT_ERROR;
138 lzma_filter_info filters[2] = {
140 .init = &lzma_lzma_decoder_init,
141 .options = &coder->options,
147 const lzma_ret ret = lzma_next_filter_init(&coder->next,
152 // Use a hack to set the uncompressed size.
153 lzma_lz_decoder_uncompressed(coder->next.coder,
154 coder->uncompressed_size);
156 coder->sequence = SEQ_CODE;
162 return coder->next.code(coder->next.coder,
163 allocator, in, in_pos, in_size,
164 out, out_pos, out_size, action);
168 return LZMA_PROG_ERROR;
176 alone_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
178 lzma_next_end(&coder->next, allocator);
179 lzma_free(coder, allocator);
185 lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
188 lzma_next_coder_init(lzma_alone_decoder_init, next, allocator);
190 if (next->coder == NULL) {
191 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
192 if (next->coder == NULL)
193 return LZMA_MEM_ERROR;
195 next->code = &alone_decode;
196 next->end = &alone_decoder_end;
197 next->coder->next = LZMA_NEXT_CODER_INIT;
200 next->coder->sequence = SEQ_PROPERTIES;
201 next->coder->pos = 0;
202 next->coder->options.dictionary_size = 0;
203 next->coder->uncompressed_size = 0;
204 next->coder->memlimit = memlimit;
210 extern LZMA_API lzma_ret
211 lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
213 lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit);
215 strm->internal->supported_actions[LZMA_RUN] = true;
216 strm->internal->supported_actions[LZMA_FINISH] = true;