1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file alone_decoder.c
6 /// \brief Decoder for LZMA_Alone files
8 // Author: Lasse Collin
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "alone_decoder.h"
13 #include "lzma_decoder.h"
14 #include "lz_decoder.h"
23 SEQ_UNCOMPRESSED_SIZE
,
28 /// If true, reject files that are unlikely to be .lzma files.
29 /// If false, more non-.lzma files get accepted and will give
30 /// LZMA_DATA_ERROR either immediately or after a few output bytes.
33 /// Position in the header fields
36 /// Uncompressed size decoded from the header
37 lzma_vli uncompressed_size
;
39 /// Memory usage limit
42 /// Amount of memory actually needed (only an estimate)
45 /// Options decoded from the header needed to initialize
47 lzma_options_lzma options
;
52 alone_decode(void *coder_ptr
, const lzma_allocator
*allocator
,
53 const uint8_t *restrict in
, size_t *restrict in_pos
,
54 size_t in_size
, uint8_t *restrict out
,
55 size_t *restrict out_pos
, size_t out_size
,
58 lzma_alone_coder
*coder
= coder_ptr
;
60 while (*out_pos
< out_size
61 && (coder
->sequence
== SEQ_CODE
|| *in_pos
< in_size
))
62 switch (coder
->sequence
) {
64 if (lzma_lzma_lclppb_decode(&coder
->options
, in
[*in_pos
]))
65 return LZMA_FORMAT_ERROR
;
67 coder
->sequence
= SEQ_DICTIONARY_SIZE
;
71 case SEQ_DICTIONARY_SIZE
:
72 coder
->options
.dict_size
73 |= (size_t)(in
[*in_pos
]) << (coder
->pos
* 8);
75 if (++coder
->pos
== 4) {
76 if (coder
->picky
&& coder
->options
.dict_size
78 // A hack to ditch tons of false positives:
79 // We allow only dictionary sizes that are
80 // 2^n or 2^n + 2^(n-1). LZMA_Alone created
81 // only files with 2^n, but accepts any
83 uint32_t d
= coder
->options
.dict_size
- 1;
91 if (d
!= coder
->options
.dict_size
)
92 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)
109 // Another hack to ditch false positives: Assume that
110 // if the uncompressed size is known, it must be less
113 // FIXME? Without picky we allow > LZMA_VLI_MAX which doesn't
114 // really matter in this specific situation (> LZMA_VLI_MAX is
115 // safe in the LZMA decoder) but it's somewhat weird still.
117 && coder
->uncompressed_size
!= LZMA_VLI_UNKNOWN
118 && coder
->uncompressed_size
119 >= (LZMA_VLI_C(1) << 38))
120 return LZMA_FORMAT_ERROR
;
122 // Use LZMA_FILTER_LZMA1EXT features to specify the
123 // uncompressed size and that the end marker is allowed
124 // even when the uncompressed size is known. Both .lzma
125 // header and LZMA1EXT use UINT64_MAX indicate that size
127 coder
->options
.ext_flags
= LZMA_LZMA1EXT_ALLOW_EOPM
;
128 lzma_set_ext_size(coder
->options
, coder
->uncompressed_size
);
130 // Calculate the memory usage so that it is ready
131 // for SEQ_CODER_INIT.
132 coder
->memusage
= lzma_lzma_decoder_memusage(&coder
->options
)
133 + LZMA_MEMUSAGE_BASE
;
136 coder
->sequence
= SEQ_CODER_INIT
;
140 case SEQ_CODER_INIT
: {
141 if (coder
->memusage
> coder
->memlimit
)
142 return LZMA_MEMLIMIT_ERROR
;
144 lzma_filter_info filters
[2] = {
146 .id
= LZMA_FILTER_LZMA1EXT
,
147 .init
= &lzma_lzma_decoder_init
,
148 .options
= &coder
->options
,
154 return_if_error(lzma_next_filter_init(&coder
->next
,
155 allocator
, filters
));
157 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(void *coder_ptr
, const lzma_allocator
*allocator
)
178 lzma_alone_coder
*coder
= coder_ptr
;
179 lzma_next_end(&coder
->next
, allocator
);
180 lzma_free(coder
, allocator
);
186 alone_decoder_memconfig(void *coder_ptr
, uint64_t *memusage
,
187 uint64_t *old_memlimit
, uint64_t new_memlimit
)
189 lzma_alone_coder
*coder
= coder_ptr
;
191 *memusage
= coder
->memusage
;
192 *old_memlimit
= coder
->memlimit
;
194 if (new_memlimit
!= 0) {
195 if (new_memlimit
< coder
->memusage
)
196 return LZMA_MEMLIMIT_ERROR
;
198 coder
->memlimit
= new_memlimit
;
206 lzma_alone_decoder_init(lzma_next_coder
*next
, const lzma_allocator
*allocator
,
207 uint64_t memlimit
, bool picky
)
209 lzma_next_coder_init(&lzma_alone_decoder_init
, next
, allocator
);
211 lzma_alone_coder
*coder
= next
->coder
;
214 coder
= lzma_alloc(sizeof(lzma_alone_coder
), allocator
);
216 return LZMA_MEM_ERROR
;
219 next
->code
= &alone_decode
;
220 next
->end
= &alone_decoder_end
;
221 next
->memconfig
= &alone_decoder_memconfig
;
222 coder
->next
= LZMA_NEXT_CODER_INIT
;
225 coder
->sequence
= SEQ_PROPERTIES
;
226 coder
->picky
= picky
;
228 coder
->options
.dict_size
= 0;
229 coder
->options
.preset_dict
= NULL
;
230 coder
->options
.preset_dict_size
= 0;
231 coder
->uncompressed_size
= 0;
232 coder
->memlimit
= my_max(1, memlimit
);
233 coder
->memusage
= LZMA_MEMUSAGE_BASE
;
239 extern LZMA_API(lzma_ret
)
240 lzma_alone_decoder(lzma_stream
*strm
, uint64_t memlimit
)
242 lzma_next_strm_init(lzma_alone_decoder_init
, strm
, memlimit
, false);
244 strm
->internal
->supported_actions
[LZMA_RUN
] = true;
245 strm
->internal
->supported_actions
[LZMA_FINISH
] = true;