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