]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/alone_decoder.c
Some API changes, bug fixes, cleanups etc.
[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         /// Options decoded from the header needed to initialize
46         /// the LZMA decoder
47         lzma_options_lzma options;
48 };
49
50
51 static lzma_ret
52 alone_decode(lzma_coder *coder,
53                 lzma_allocator *allocator lzma_attribute((unused)),
54                 const uint8_t *restrict in, size_t *restrict in_pos,
55                 size_t in_size, uint8_t *restrict out,
56                 size_t *restrict out_pos, size_t out_size,
57                 lzma_action action)
58 {
59         while (*out_pos < out_size
60                         && (coder->sequence == SEQ_CODE || *in_pos < in_size))
61         switch (coder->sequence) {
62         case SEQ_PROPERTIES:
63                 if (lzma_lzma_lclppb_decode(&coder->options, in[*in_pos]))
64                         return LZMA_FORMAT_ERROR;
65
66                 coder->sequence = SEQ_DICTIONARY_SIZE;
67                 ++*in_pos;
68                 break;
69
70         case SEQ_DICTIONARY_SIZE:
71                 coder->options.dict_size
72                                 |= (size_t)(in[*in_pos]) << (coder->pos * 8);
73
74                 if (++coder->pos == 4) {
75                         if (coder->options.dict_size > (UINT32_C(1) << 30))
76                                 return LZMA_FORMAT_ERROR;
77
78                         // A hack to ditch tons of false positives: We allow
79                         // only dictionary sizes that are 2^n or 2^n + 2^(n-1).
80                         // LZMA_Alone created only files with 2^n, but accepts
81                         // any dictionary size. If someone complains, this
82                         // will be reconsidered.
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
91                         if (d != coder->options.dict_size)
92                                 return LZMA_FORMAT_ERROR;
93
94                         coder->pos = 0;
95                         coder->sequence = SEQ_UNCOMPRESSED_SIZE;
96                 }
97
98                 ++*in_pos;
99                 break;
100
101         case SEQ_UNCOMPRESSED_SIZE:
102                 coder->uncompressed_size
103                                 |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
104
105                 if (++coder->pos == 8) {
106                         // Another hack to ditch false positives: Assume that
107                         // if the uncompressed size is known, it must be less
108                         // than 256 GiB. Again, if someone complains, this
109                         // will be reconsidered.
110                         if (coder->uncompressed_size != LZMA_VLI_UNKNOWN
111                                         && coder->uncompressed_size
112                                                 >= (LZMA_VLI_C(1) << 38))
113                                 return LZMA_FORMAT_ERROR;
114
115                         coder->pos = 0;
116                         coder->sequence = SEQ_CODER_INIT;
117                 }
118
119                 ++*in_pos;
120                 break;
121
122         case SEQ_CODER_INIT: {
123                 // FIXME It is unfair that this doesn't add a fixed amount
124                 // like lzma_memusage_common() does.
125                 const uint64_t memusage
126                                 = lzma_lzma_decoder_memusage(&coder->options);
127
128                 // Use LZMA_PROG_ERROR since LZMA_Alone decoder cannot be
129                 // built without LZMA support.
130                 // FIXME TODO Make the above comment true.
131                 if (memusage == UINT64_MAX)
132                         return LZMA_PROG_ERROR;
133
134                 if (memusage > coder->memlimit)
135                         return LZMA_MEMLIMIT_ERROR;
136
137                 lzma_filter_info filters[2] = {
138                         {
139                                 .init = &lzma_lzma_decoder_init,
140                                 .options = &coder->options,
141                         }, {
142                                 .init = NULL,
143                         }
144                 };
145
146                 const lzma_ret ret = lzma_next_filter_init(&coder->next,
147                                 allocator, filters);
148                 if (ret != LZMA_OK)
149                         return ret;
150
151                 // Use a hack to set the uncompressed size.
152                 lzma_lz_decoder_uncompressed(coder->next.coder,
153                                 coder->uncompressed_size);
154
155                 coder->sequence = SEQ_CODE;
156         }
157
158         // Fall through
159
160         case SEQ_CODE: {
161                 return coder->next.code(coder->next.coder,
162                                 allocator, in, in_pos, in_size,
163                                 out, out_pos, out_size, action);
164         }
165
166         default:
167                 return LZMA_PROG_ERROR;
168         }
169
170         return LZMA_OK;
171 }
172
173
174 static void
175 alone_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
176 {
177         lzma_next_end(&coder->next, allocator);
178         lzma_free(coder, allocator);
179         return;
180 }
181
182
183 extern lzma_ret
184 lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
185                 uint64_t memlimit)
186 {
187         lzma_next_coder_init(lzma_alone_decoder_init, next, allocator);
188
189         if (next->coder == NULL) {
190                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
191                 if (next->coder == NULL)
192                         return LZMA_MEM_ERROR;
193
194                 next->code = &alone_decode;
195                 next->end = &alone_decoder_end;
196                 next->coder->next = LZMA_NEXT_CODER_INIT;
197         }
198
199         next->coder->sequence = SEQ_PROPERTIES;
200         next->coder->pos = 0;
201         next->coder->options.dict_size = 0;
202         next->coder->uncompressed_size = 0;
203         next->coder->memlimit = memlimit;
204
205         return LZMA_OK;
206 }
207
208
209 extern LZMA_API lzma_ret
210 lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
211 {
212         lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit);
213
214         strm->internal->supported_actions[LZMA_RUN] = true;
215         strm->internal->supported_actions[LZMA_FINISH] = true;
216
217         return LZMA_OK;
218 }