]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_flags_decoder.c
Imported to git.
[icculus/xz.git] / src / liblzma / common / stream_flags_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       stream_flags_decoder.c
4 /// \brief      Decodes Stream Header and tail from .lzma 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 "stream_flags_decoder.h"
21 #include "stream_common.h"
22
23
24 ////////////
25 // Common //
26 ////////////
27
28 struct lzma_coder_s {
29         enum {
30                 SEQ_HEADER_MAGIC,
31                 SEQ_HEADER_FLAGS,
32                 SEQ_HEADER_CRC32,
33
34                 SEQ_FOOTER_FLAGS,
35                 SEQ_FOOTER_MAGIC,
36         } sequence;
37
38         size_t pos;
39         uint32_t crc32;
40
41         lzma_stream_flags *options;
42 };
43
44
45 static void
46 stream_header_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
47 {
48         lzma_free(coder, allocator);
49         return;
50 }
51
52
53 static bool
54 stream_flags_decode(const uint8_t *in, lzma_stream_flags *options)
55 {
56         // Reserved bits must be unset.
57         if (*in & 0xE0)
58                 return true;
59
60         options->check = *in & 0x07;
61         options->has_crc32 = (*in & 0x08) != 0;
62         options->is_multi = (*in & 0x10) != 0;
63
64         return false;
65 }
66
67
68 ////////////
69 // Header //
70 ////////////
71
72 static lzma_ret
73 stream_header_decode(lzma_coder *coder,
74                 lzma_allocator *allocator lzma_attribute((unused)),
75                 const uint8_t *restrict in, size_t *restrict in_pos,
76                 size_t in_size, uint8_t *restrict out lzma_attribute((unused)),
77                 size_t *restrict out_pos lzma_attribute((unused)),
78                 size_t out_size lzma_attribute((unused)),
79                 lzma_action action lzma_attribute((unused)))
80 {
81         while (*in_pos < in_size)
82         switch (coder->sequence) {
83         case SEQ_HEADER_MAGIC:
84                 if (in[*in_pos] != lzma_header_magic[coder->pos])
85                         return LZMA_DATA_ERROR;
86
87                 ++*in_pos;
88
89                 if (++coder->pos == sizeof(lzma_header_magic)) {
90                         coder->pos = 0;
91                         coder->sequence = SEQ_HEADER_FLAGS;
92                 }
93
94                 break;
95
96         case SEQ_HEADER_FLAGS:
97                 if (stream_flags_decode(in + *in_pos, coder->options))
98                         return LZMA_HEADER_ERROR;
99
100                 coder->crc32 = lzma_crc32(in + *in_pos, 1, 0);
101
102                 ++*in_pos;
103                 coder->sequence = SEQ_HEADER_CRC32;
104                 break;
105
106         case SEQ_HEADER_CRC32:
107                 if (in[*in_pos] != ((coder->crc32 >> (coder->pos * 8)) & 0xFF))
108                         return LZMA_DATA_ERROR;
109
110                 ++*in_pos;
111
112                 if (++coder->pos == 4)
113                         return LZMA_STREAM_END;
114
115                 break;
116
117         default:
118                 return LZMA_PROG_ERROR;
119         }
120
121         return LZMA_OK;
122 }
123
124
125 static lzma_ret
126 stream_header_decoder_init(lzma_next_coder *next,
127                 lzma_allocator *allocator, lzma_stream_flags *options)
128 {
129         if (options == NULL)
130                 return LZMA_PROG_ERROR;
131
132         if (next->coder == NULL) {
133                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
134                 if (next->coder == NULL)
135                         return LZMA_MEM_ERROR;
136         }
137
138         // Set the function pointers unconditionally, because they may
139         // have been pointing to footer decoder too.
140         next->code = &stream_header_decode;
141         next->end = &stream_header_decoder_end;
142
143         next->coder->sequence = SEQ_HEADER_MAGIC;
144         next->coder->pos = 0;
145         next->coder->crc32 = 0;
146         next->coder->options = options;
147
148         return LZMA_OK;
149 }
150
151
152 extern lzma_ret
153 lzma_stream_header_decoder_init(lzma_next_coder *next,
154                 lzma_allocator *allocator, lzma_stream_flags *options)
155 {
156         lzma_next_coder_init(
157                         stream_header_decoder_init, next, allocator, options);
158 }
159
160
161 extern LZMA_API lzma_ret
162 lzma_stream_header_decoder(lzma_stream *strm, lzma_stream_flags *options)
163 {
164         lzma_next_strm_init(strm, stream_header_decoder_init, options);
165
166         strm->internal->supported_actions[LZMA_RUN] = true;
167
168         return LZMA_OK;
169 }
170
171
172 //////////
173 // Tail //
174 //////////
175
176 static lzma_ret
177 stream_tail_decode(lzma_coder *coder,
178                 lzma_allocator *allocator lzma_attribute((unused)),
179                 const uint8_t *restrict in, size_t *restrict in_pos,
180                 size_t in_size, uint8_t *restrict out lzma_attribute((unused)),
181                 size_t *restrict out_pos lzma_attribute((unused)),
182                 size_t out_size lzma_attribute((unused)),
183                 lzma_action action lzma_attribute((unused)))
184 {
185         while (*in_pos < in_size)
186         switch (coder->sequence) {
187         case SEQ_FOOTER_FLAGS:
188                 if (stream_flags_decode(in + *in_pos, coder->options))
189                         return LZMA_HEADER_ERROR;
190
191                 ++*in_pos;
192                 coder->sequence = SEQ_FOOTER_MAGIC;
193                 break;
194
195         case SEQ_FOOTER_MAGIC:
196                 if (in[*in_pos] != lzma_footer_magic[coder->pos])
197                         return LZMA_DATA_ERROR;
198
199                 ++*in_pos;
200
201                 if (++coder->pos == sizeof(lzma_footer_magic))
202                         return LZMA_STREAM_END;
203
204                 break;
205
206         default:
207                 return LZMA_PROG_ERROR;
208         }
209
210         return LZMA_OK;
211 }
212
213
214 static lzma_ret
215 stream_tail_decoder_init(lzma_next_coder *next,
216                 lzma_allocator *allocator, lzma_stream_flags *options)
217 {
218         if (options == NULL)
219                 return LZMA_PROG_ERROR;
220
221         if (next->coder == NULL) {
222                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
223                 if (next->coder == NULL)
224                         return LZMA_MEM_ERROR;
225         }
226
227         // Set the function pointers unconditionally, because they may
228         // have been pointing to footer decoder too.
229         next->code = &stream_tail_decode;
230         next->end = &stream_header_decoder_end;
231
232         next->coder->sequence = SEQ_FOOTER_FLAGS;
233         next->coder->pos = 0;
234         next->coder->options = options;
235
236         return LZMA_OK;
237 }
238
239
240 extern lzma_ret
241 lzma_stream_tail_decoder_init(lzma_next_coder *next,
242                 lzma_allocator *allocator, lzma_stream_flags *options)
243 {
244         lzma_next_coder_init2(next, allocator, stream_header_decoder_init,
245                         stream_tail_decoder_init, allocator, options);
246 }
247
248
249 extern LZMA_API lzma_ret
250 lzma_stream_tail_decoder(lzma_stream *strm, lzma_stream_flags *options)
251 {
252         lzma_next_strm_init2(strm, stream_header_decoder_init,
253                         stream_tail_decoder_init, strm->allocator, options);
254
255         strm->internal->supported_actions[LZMA_RUN] = true;
256
257         return LZMA_OK;
258 }