]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/alone_decoder.c
Bunch of liblzma API cleanups and fixes.
[icculus/xz.git] / src / liblzma / common / alone_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       alone_decoder.c
4 /// \brief      Decoder for LZMA_Alone files
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "alone_decoder.h"
21 #include "lzma_decoder.h"
22 #include "lz_decoder.h"
23
24
25 struct lzma_coder_s {
26         lzma_next_coder next;
27
28         enum {
29                 SEQ_PROPERTIES,
30                 SEQ_DICTIONARY_SIZE,
31                 SEQ_UNCOMPRESSED_SIZE,
32                 SEQ_CODER_INIT,
33                 SEQ_CODE,
34         } sequence;
35
36         /// Position in the header fields
37         size_t pos;
38
39         /// Uncompressed size decoded from the header
40         lzma_vli uncompressed_size;
41
42         /// Memory usage limit
43         uint64_t memlimit;
44
45         /// Amount of memory actually needed (only an estimate)
46         uint64_t memusage;
47
48         /// Options decoded from the header needed to initialize
49         /// the LZMA decoder
50         lzma_options_lzma options;
51 };
52
53
54 static lzma_ret
55 alone_decode(lzma_coder *coder,
56                 lzma_allocator *allocator lzma_attribute((unused)),
57                 const uint8_t *restrict in, size_t *restrict in_pos,
58                 size_t in_size, uint8_t *restrict out,
59                 size_t *restrict out_pos, size_t out_size,
60                 lzma_action action)
61 {
62         while (*out_pos < out_size
63                         && (coder->sequence == SEQ_CODE || *in_pos < in_size))
64         switch (coder->sequence) {
65         case SEQ_PROPERTIES:
66                 if (lzma_lzma_lclppb_decode(&coder->options, in[*in_pos]))
67                         return LZMA_FORMAT_ERROR;
68
69                 coder->sequence = SEQ_DICTIONARY_SIZE;
70                 ++*in_pos;
71                 break;
72
73         case SEQ_DICTIONARY_SIZE:
74                 coder->options.dict_size
75                                 |= (size_t)(in[*in_pos]) << (coder->pos * 8);
76
77                 if (++coder->pos == 4) {
78                         if (coder->options.dict_size > (UINT32_C(1) << 30))
79                                 return LZMA_FORMAT_ERROR;
80
81                         // A hack to ditch tons of false positives: We allow
82                         // only dictionary sizes that are 2^n or 2^n + 2^(n-1).
83                         // LZMA_Alone created only files with 2^n, but accepts
84                         // any dictionary size. If someone complains, this
85                         // will be reconsidered.
86                         uint32_t d = coder->options.dict_size - 1;
87                         d |= d >> 2;
88                         d |= d >> 3;
89                         d |= d >> 4;
90                         d |= d >> 8;
91                         d |= d >> 16;
92                         ++d;
93
94                         if (d != coder->options.dict_size)
95                                 return LZMA_FORMAT_ERROR;
96
97                         coder->pos = 0;
98                         coder->sequence = SEQ_UNCOMPRESSED_SIZE;
99                 }
100
101                 ++*in_pos;
102                 break;
103
104         case SEQ_UNCOMPRESSED_SIZE:
105                 coder->uncompressed_size
106                                 |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
107
108                 if (++coder->pos == 8) {
109                         // Another hack to ditch false positives: Assume that
110                         // if the uncompressed size is known, it must be less
111                         // than 256 GiB. Again, if someone complains, this
112                         // will be reconsidered.
113                         if (coder->uncompressed_size != LZMA_VLI_UNKNOWN
114                                         && coder->uncompressed_size
115                                                 >= (LZMA_VLI_C(1) << 38))
116                                 return LZMA_FORMAT_ERROR;
117
118                         coder->pos = 0;
119                         coder->sequence = SEQ_CODER_INIT;
120                 }
121
122                 ++*in_pos;
123
124                 // Calculate the memory usage so that it is ready
125                 // for SEQ_CODER_INIT.
126                 coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
127                                 + LZMA_MEMUSAGE_BASE;
128
129         // Fall through
130
131         case SEQ_CODER_INIT: {
132                 if (coder->memusage > coder->memlimit)
133                         return LZMA_MEMLIMIT_ERROR;
134
135                 lzma_filter_info filters[2] = {
136                         {
137                                 .init = &lzma_lzma_decoder_init,
138                                 .options = &coder->options,
139                         }, {
140                                 .init = NULL,
141                         }
142                 };
143
144                 const lzma_ret ret = lzma_next_filter_init(&coder->next,
145                                 allocator, filters);
146                 if (ret != LZMA_OK)
147                         return ret;
148
149                 // Use a hack to set the uncompressed size.
150                 lzma_lz_decoder_uncompressed(coder->next.coder,
151                                 coder->uncompressed_size);
152
153                 coder->sequence = SEQ_CODE;
154                 break;
155         }
156
157         case SEQ_CODE: {
158                 return coder->next.code(coder->next.coder,
159                                 allocator, in, in_pos, in_size,
160                                 out, out_pos, out_size, action);
161         }
162
163         default:
164                 return LZMA_PROG_ERROR;
165         }
166
167         return LZMA_OK;
168 }
169
170
171 static void
172 alone_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
173 {
174         lzma_next_end(&coder->next, allocator);
175         lzma_free(coder, allocator);
176         return;
177 }
178
179
180 static lzma_ret
181 alone_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
182                 uint64_t *old_memlimit, uint64_t new_memlimit)
183 {
184         if (new_memlimit != 0 && new_memlimit < coder->memusage)
185                 return LZMA_MEMLIMIT_ERROR;
186
187         *memusage = coder->memusage;
188         *old_memlimit = coder->memlimit;
189         coder->memlimit = new_memlimit;
190
191         return LZMA_OK;
192 }
193
194
195 extern lzma_ret
196 lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
197                 uint64_t memlimit)
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->pos = 0;
217         next->coder->options.dict_size = 0;
218         next->coder->uncompressed_size = 0;
219         next->coder->memlimit = memlimit;
220         next->coder->memusage = LZMA_MEMUSAGE_BASE;
221
222         return LZMA_OK;
223 }
224
225
226 extern LZMA_API lzma_ret
227 lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
228 {
229         lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit);
230
231         strm->internal->supported_actions[LZMA_RUN] = true;
232         strm->internal->supported_actions[LZMA_FINISH] = true;
233
234         return LZMA_OK;
235 }