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"
30 SEQ_UNCOMPRESSED_SIZE,
35 /// Position in the header fields
38 /// Uncompressed size decoded from the header
39 lzma_vli uncompressed_size;
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_decode_properties(&coder->options, in[*in_pos]))
60 return LZMA_FORMAT_ERROR;
62 coder->sequence = SEQ_DICTIONARY_SIZE;
66 case SEQ_DICTIONARY_SIZE:
67 coder->options.dictionary_size
68 |= (size_t)(in[*in_pos]) << (coder->pos * 8);
70 if (++coder->pos == 4) {
71 if (coder->options.dictionary_size
72 < LZMA_DICTIONARY_SIZE_MIN
73 || coder->options.dictionary_size
74 > LZMA_DICTIONARY_SIZE_MAX)
75 return LZMA_FORMAT_ERROR;
77 // A hack to ditch tons of false positives: We allow
78 // only dictionary sizes that are 2^n or 2^n + 2^(n-1).
79 // LZMA_Alone created only files with 2^n, but accepts
80 // any dictionary size. If someone complains, this
81 // will be reconsidered.
82 uint32_t d = coder->options.dictionary_size - 1;
90 if (d != coder->options.dictionary_size)
91 return LZMA_FORMAT_ERROR;
94 coder->sequence = SEQ_UNCOMPRESSED_SIZE;
100 case SEQ_UNCOMPRESSED_SIZE:
101 coder->uncompressed_size
102 |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
104 if (++coder->pos == 8) {
105 // Another hack to ditch false positives: Assume that
106 // if the uncompressed size is known, it must be less
107 // than 256 GiB. Again, if someone complains, this
108 // will be reconsidered.
109 if (coder->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN
110 && coder->uncompressed_size
111 >= (LZMA_VLI_C(1) << 38))
112 return LZMA_FORMAT_ERROR;
115 coder->sequence = SEQ_CODER_INIT;
121 case SEQ_CODER_INIT: {
122 // Two is enough because there won't be implicit filters.
123 lzma_filter_info filters[2] = {
125 .init = &lzma_lzma_decoder_init,
126 .options = &coder->options,
132 const lzma_ret ret = lzma_next_filter_init(&coder->next,
137 // Use a hack to set the uncompressed size.
138 lzma_lzma_decoder_uncompressed_size(&coder->next,
139 coder->uncompressed_size);
141 coder->sequence = SEQ_CODE;
147 return coder->next.code(coder->next.coder,
148 allocator, in, in_pos, in_size,
149 out, out_pos, out_size, action);
153 return LZMA_PROG_ERROR;
161 alone_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
163 lzma_next_coder_end(&coder->next, allocator);
164 lzma_free(coder, allocator);
170 alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator)
172 if (next->coder == NULL) {
173 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
174 if (next->coder == NULL)
175 return LZMA_MEM_ERROR;
177 next->code = &alone_decode;
178 next->end = &alone_decoder_end;
179 next->coder->next = LZMA_NEXT_CODER_INIT;
182 next->coder->sequence = SEQ_PROPERTIES;
183 next->coder->pos = 0;
184 next->coder->options.dictionary_size = 0;
185 next->coder->uncompressed_size = 0;
192 lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator)
194 lzma_next_coder_init0(alone_decoder_init, next, allocator);
198 extern LZMA_API lzma_ret
199 lzma_alone_decoder(lzma_stream *strm)
201 lzma_next_strm_init0(strm, alone_decoder_init);
203 strm->internal->supported_actions[LZMA_RUN] = true;
204 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;