GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / tools / misc / xz / src / liblzma / common / alone_decoder.c
blob678c79e6543339219a19aafaf387c4af463f727d
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file alone_decoder.c
4 /// \brief Decoder for LZMA_Alone files
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "alone_decoder.h"
14 #include "lzma_decoder.h"
15 #include "lz_decoder.h"
18 struct lzma_coder_s {
19 lzma_next_coder next;
21 enum {
22 SEQ_PROPERTIES,
23 SEQ_DICTIONARY_SIZE,
24 SEQ_UNCOMPRESSED_SIZE,
25 SEQ_CODER_INIT,
26 SEQ_CODE,
27 } sequence;
29 /// Position in the header fields
30 size_t pos;
32 /// Uncompressed size decoded from the header
33 lzma_vli uncompressed_size;
35 /// Memory usage limit
36 uint64_t memlimit;
38 /// Amount of memory actually needed (only an estimate)
39 uint64_t memusage;
41 /// Options decoded from the header needed to initialize
42 /// the LZMA decoder
43 lzma_options_lzma options;
47 static lzma_ret
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,
53 lzma_action action)
55 while (*out_pos < out_size
56 && (coder->sequence == SEQ_CODE || *in_pos < in_size))
57 switch (coder->sequence) {
58 case SEQ_PROPERTIES:
59 if (lzma_lzma_lclppb_decode(&coder->options, in[*in_pos]))
60 return LZMA_FORMAT_ERROR;
62 coder->sequence = SEQ_DICTIONARY_SIZE;
63 ++*in_pos;
64 break;
66 case SEQ_DICTIONARY_SIZE:
67 coder->options.dict_size
68 |= (size_t)(in[*in_pos]) << (coder->pos * 8);
70 if (++coder->pos == 4) {
71 if (coder->options.dict_size != UINT32_MAX) {
72 // A hack to ditch tons of false positives:
73 // We allow only dictionary sizes that are
74 // 2^n or 2^n + 2^(n-1). LZMA_Alone created
75 // only files with 2^n, but accepts any
76 // dictionary size. If someone complains, this
77 // will be reconsidered.
78 uint32_t d = coder->options.dict_size - 1;
79 d |= d >> 2;
80 d |= d >> 3;
81 d |= d >> 4;
82 d |= d >> 8;
83 d |= d >> 16;
84 ++d;
86 if (d != coder->options.dict_size)
87 return LZMA_FORMAT_ERROR;
90 coder->pos = 0;
91 coder->sequence = SEQ_UNCOMPRESSED_SIZE;
94 ++*in_pos;
95 break;
97 case SEQ_UNCOMPRESSED_SIZE:
98 coder->uncompressed_size
99 |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
100 ++*in_pos;
101 if (++coder->pos < 8)
102 break;
104 // Another hack to ditch false positives: Assume that
105 // if the uncompressed size is known, it must be less
106 // than 256 GiB. Again, if someone complains, this
107 // will be reconsidered.
108 if (coder->uncompressed_size != LZMA_VLI_UNKNOWN
109 && coder->uncompressed_size
110 >= (LZMA_VLI_C(1) << 38))
111 return LZMA_FORMAT_ERROR;
113 // Calculate the memory usage so that it is ready
114 // for SEQ_CODER_INIT.
115 coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
116 + LZMA_MEMUSAGE_BASE;
118 coder->pos = 0;
119 coder->sequence = SEQ_CODER_INIT;
121 // Fall through
123 case SEQ_CODER_INIT: {
124 if (coder->memusage > coder->memlimit)
125 return LZMA_MEMLIMIT_ERROR;
127 lzma_filter_info filters[2] = {
129 .init = &lzma_lzma_decoder_init,
130 .options = &coder->options,
131 }, {
132 .init = NULL,
136 const lzma_ret ret = lzma_next_filter_init(&coder->next,
137 allocator, filters);
138 if (ret != LZMA_OK)
139 return ret;
141 // Use a hack to set the uncompressed size.
142 lzma_lz_decoder_uncompressed(coder->next.coder,
143 coder->uncompressed_size);
145 coder->sequence = SEQ_CODE;
146 break;
149 case SEQ_CODE: {
150 return coder->next.code(coder->next.coder,
151 allocator, in, in_pos, in_size,
152 out, out_pos, out_size, action);
155 default:
156 return LZMA_PROG_ERROR;
159 return LZMA_OK;
163 static void
164 alone_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
166 lzma_next_end(&coder->next, allocator);
167 lzma_free(coder, allocator);
168 return;
172 static lzma_ret
173 alone_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
174 uint64_t *old_memlimit, uint64_t new_memlimit)
176 *memusage = coder->memusage;
177 *old_memlimit = coder->memlimit;
179 if (new_memlimit != 0) {
180 if (new_memlimit < coder->memusage)
181 return LZMA_MEMLIMIT_ERROR;
183 coder->memlimit = new_memlimit;
186 return LZMA_OK;
190 extern lzma_ret
191 lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
192 uint64_t memlimit)
194 lzma_next_coder_init(&lzma_alone_decoder_init, next, allocator);
196 if (memlimit == 0)
197 return LZMA_PROG_ERROR;
199 if (next->coder == NULL) {
200 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
201 if (next->coder == NULL)
202 return LZMA_MEM_ERROR;
204 next->code = &alone_decode;
205 next->end = &alone_decoder_end;
206 next->memconfig = &alone_decoder_memconfig;
207 next->coder->next = LZMA_NEXT_CODER_INIT;
210 next->coder->sequence = SEQ_PROPERTIES;
211 next->coder->pos = 0;
212 next->coder->options.dict_size = 0;
213 next->coder->options.preset_dict = NULL;
214 next->coder->options.preset_dict_size = 0;
215 next->coder->uncompressed_size = 0;
216 next->coder->memlimit = memlimit;
217 next->coder->memusage = LZMA_MEMUSAGE_BASE;
219 return LZMA_OK;
223 extern LZMA_API(lzma_ret)
224 lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
226 lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit);
228 strm->internal->supported_actions[LZMA_RUN] = true;
229 strm->internal->supported_actions[LZMA_FINISH] = true;
231 return LZMA_OK;