1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_flags_decoder.c
4 /// \brief Decodes Stream Header and tail from .lzma files
6 // Copyright (C) 2007 Lasse Collin
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.
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.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "stream_flags_decoder.h"
21 #include "stream_common.h"
41 lzma_stream_flags *options;
46 stream_header_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
48 lzma_free(coder, allocator);
54 stream_flags_decode(const uint8_t *in, lzma_stream_flags *options)
56 // Reserved bits must be unset.
60 options->check = *in & 0x07;
61 options->has_crc32 = (*in & 0x08) != 0;
62 options->is_multi = (*in & 0x10) != 0;
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)))
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;
89 if (++coder->pos == sizeof(lzma_header_magic)) {
91 coder->sequence = SEQ_HEADER_FLAGS;
96 case SEQ_HEADER_FLAGS:
97 if (stream_flags_decode(in + *in_pos, coder->options))
98 return LZMA_HEADER_ERROR;
100 coder->crc32 = lzma_crc32(in + *in_pos, 1, 0);
103 coder->sequence = SEQ_HEADER_CRC32;
106 case SEQ_HEADER_CRC32:
107 if (in[*in_pos] != ((coder->crc32 >> (coder->pos * 8)) & 0xFF))
108 return LZMA_DATA_ERROR;
112 if (++coder->pos == 4)
113 return LZMA_STREAM_END;
118 return LZMA_PROG_ERROR;
126 stream_header_decoder_init(lzma_next_coder *next,
127 lzma_allocator *allocator, lzma_stream_flags *options)
130 return LZMA_PROG_ERROR;
132 if (next->coder == NULL) {
133 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
134 if (next->coder == NULL)
135 return LZMA_MEM_ERROR;
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;
143 next->coder->sequence = SEQ_HEADER_MAGIC;
144 next->coder->pos = 0;
145 next->coder->crc32 = 0;
146 next->coder->options = options;
153 lzma_stream_header_decoder_init(lzma_next_coder *next,
154 lzma_allocator *allocator, lzma_stream_flags *options)
156 lzma_next_coder_init(
157 stream_header_decoder_init, next, allocator, options);
161 extern LZMA_API lzma_ret
162 lzma_stream_header_decoder(lzma_stream *strm, lzma_stream_flags *options)
164 lzma_next_strm_init(strm, stream_header_decoder_init, options);
166 strm->internal->supported_actions[LZMA_RUN] = true;
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)))
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;
192 coder->sequence = SEQ_FOOTER_MAGIC;
195 case SEQ_FOOTER_MAGIC:
196 if (in[*in_pos] != lzma_footer_magic[coder->pos])
197 return LZMA_DATA_ERROR;
201 if (++coder->pos == sizeof(lzma_footer_magic))
202 return LZMA_STREAM_END;
207 return LZMA_PROG_ERROR;
215 stream_tail_decoder_init(lzma_next_coder *next,
216 lzma_allocator *allocator, lzma_stream_flags *options)
219 return LZMA_PROG_ERROR;
221 if (next->coder == NULL) {
222 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
223 if (next->coder == NULL)
224 return LZMA_MEM_ERROR;
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;
232 next->coder->sequence = SEQ_FOOTER_FLAGS;
233 next->coder->pos = 0;
234 next->coder->options = options;
241 lzma_stream_tail_decoder_init(lzma_next_coder *next,
242 lzma_allocator *allocator, lzma_stream_flags *options)
244 lzma_next_coder_init2(next, allocator, stream_header_decoder_init,
245 stream_tail_decoder_init, allocator, options);
249 extern LZMA_API lzma_ret
250 lzma_stream_tail_decoder(lzma_stream *strm, lzma_stream_flags *options)
252 lzma_next_strm_init2(strm, stream_header_decoder_init,
253 stream_tail_decoder_init, strm->allocator, options);
255 strm->internal->supported_actions[LZMA_RUN] = true;