Deleted Added
full compact
alone_decoder.c (256281) alone_decoder.c (263285)
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.

--- 12 unchanged lines hidden (view full) ---

21 enum {
22 SEQ_PROPERTIES,
23 SEQ_DICTIONARY_SIZE,
24 SEQ_UNCOMPRESSED_SIZE,
25 SEQ_CODER_INIT,
26 SEQ_CODE,
27 } sequence;
28
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.

--- 12 unchanged lines hidden (view full) ---

21 enum {
22 SEQ_PROPERTIES,
23 SEQ_DICTIONARY_SIZE,
24 SEQ_UNCOMPRESSED_SIZE,
25 SEQ_CODER_INIT,
26 SEQ_CODE,
27 } sequence;
28
29 /// If true, reject files that are unlikely to be .lzma files.
30 /// If false, more non-.lzma files get accepted and will give
31 /// LZMA_DATA_ERROR either immediately or after a few output bytes.
32 bool picky;
33
29 /// Position in the header fields
30 size_t pos;
31
32 /// Uncompressed size decoded from the header
33 lzma_vli uncompressed_size;
34
35 /// Memory usage limit
36 uint64_t memlimit;

--- 26 unchanged lines hidden (view full) ---

63 ++*in_pos;
64 break;
65
66 case SEQ_DICTIONARY_SIZE:
67 coder->options.dict_size
68 |= (size_t)(in[*in_pos]) << (coder->pos * 8);
69
70 if (++coder->pos == 4) {
34 /// Position in the header fields
35 size_t pos;
36
37 /// Uncompressed size decoded from the header
38 lzma_vli uncompressed_size;
39
40 /// Memory usage limit
41 uint64_t memlimit;

--- 26 unchanged lines hidden (view full) ---

68 ++*in_pos;
69 break;
70
71 case SEQ_DICTIONARY_SIZE:
72 coder->options.dict_size
73 |= (size_t)(in[*in_pos]) << (coder->pos * 8);
74
75 if (++coder->pos == 4) {
71 if (coder->options.dict_size != UINT32_MAX) {
76 if (coder->picky && coder->options.dict_size
77 != 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
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
76 // dictionary size. If someone complains, this
77 // will be reconsidered.
82 // dictionary size.
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;
85

--- 12 unchanged lines hidden (view full) ---

98 coder->uncompressed_size
99 |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
100 ++*in_pos;
101 if (++coder->pos < 8)
102 break;
103
104 // Another hack to ditch false positives: Assume that
105 // if the uncompressed size is known, it must be less
83 uint32_t d = coder->options.dict_size - 1;
84 d |= d >> 2;
85 d |= d >> 3;
86 d |= d >> 4;
87 d |= d >> 8;
88 d |= d >> 16;
89 ++d;
90

--- 12 unchanged lines hidden (view full) ---

103 coder->uncompressed_size
104 |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
105 ++*in_pos;
106 if (++coder->pos < 8)
107 break;
108
109 // Another hack to ditch false positives: Assume that
110 // 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
111 // than 256 GiB.
112 if (coder->picky
113 && coder->uncompressed_size != LZMA_VLI_UNKNOWN
109 && coder->uncompressed_size
110 >= (LZMA_VLI_C(1) << 38))
111 return LZMA_FORMAT_ERROR;
112
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;

--- 67 unchanged lines hidden (view full) ---

184 }
185
186 return LZMA_OK;
187}
188
189
190extern lzma_ret
191lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
114 && coder->uncompressed_size
115 >= (LZMA_VLI_C(1) << 38))
116 return LZMA_FORMAT_ERROR;
117
118 // Calculate the memory usage so that it is ready
119 // for SEQ_CODER_INIT.
120 coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
121 + LZMA_MEMUSAGE_BASE;

--- 67 unchanged lines hidden (view full) ---

189 }
190
191 return LZMA_OK;
192}
193
194
195extern lzma_ret
196lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
192 uint64_t memlimit)
197 uint64_t memlimit, bool picky)
193{
194 lzma_next_coder_init(&lzma_alone_decoder_init, next, allocator);
195
196 if (memlimit == 0)
197 return LZMA_PROG_ERROR;
198
199 if (next->coder == NULL) {
200 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
201 if (next->coder == NULL)
202 return LZMA_MEM_ERROR;
203
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;
208 }
209
210 next->coder->sequence = SEQ_PROPERTIES;
198{
199 lzma_next_coder_init(&lzma_alone_decoder_init, next, allocator);
200
201 if (memlimit == 0)
202 return LZMA_PROG_ERROR;
203
204 if (next->coder == NULL) {
205 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
206 if (next->coder == NULL)
207 return LZMA_MEM_ERROR;
208
209 next->code = &alone_decode;
210 next->end = &alone_decoder_end;
211 next->memconfig = &alone_decoder_memconfig;
212 next->coder->next = LZMA_NEXT_CODER_INIT;
213 }
214
215 next->coder->sequence = SEQ_PROPERTIES;
216 next->coder->picky = picky;
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;
218
219 return LZMA_OK;
220}
221
222
223extern LZMA_API(lzma_ret)
224lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
225{
217 next->coder->pos = 0;
218 next->coder->options.dict_size = 0;
219 next->coder->options.preset_dict = NULL;
220 next->coder->options.preset_dict_size = 0;
221 next->coder->uncompressed_size = 0;
222 next->coder->memlimit = memlimit;
223 next->coder->memusage = LZMA_MEMUSAGE_BASE;
224
225 return LZMA_OK;
226}
227
228
229extern LZMA_API(lzma_ret)
230lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
231{
226 lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit);
232 lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit, false);
227
228 strm->internal->supported_actions[LZMA_RUN] = true;
229 strm->internal->supported_actions[LZMA_FINISH] = true;
230
231 return LZMA_OK;
232}
233
234 strm->internal->supported_actions[LZMA_RUN] = true;
235 strm->internal->supported_actions[LZMA_FINISH] = true;
236
237 return LZMA_OK;
238}