1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file auto_decoder.c
4 /// \brief Autodetect between .xz Stream and .lzma (LZMA_Alone) formats
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_decoder.h"
21 #include "alone_decoder.h"
25 /// Stream decoder or LZMA_Alone decoder
40 auto_decode(lzma_coder *coder, lzma_allocator *allocator,
41 const uint8_t *restrict in, size_t *restrict in_pos,
42 size_t in_size, uint8_t *restrict out,
43 size_t *restrict out_pos, size_t out_size, lzma_action action)
45 switch (coder->sequence) {
47 if (*in_pos >= in_size)
50 // Update the sequence now, because we want to continue from
51 // SEQ_CODE even if we return some LZMA_*_CHECK.
52 coder->sequence = SEQ_CODE;
54 // Detect the file format. For now this is simple, since if
55 // it doesn't start with 0xFD (the first magic byte of the
56 // new format), it has to be LZMA_Alone, or something that
57 // we don't support at all.
58 if (in[*in_pos] == 0xFD) {
59 return_if_error(lzma_stream_decoder_init(
60 &coder->next, allocator,
61 coder->memlimit, coder->flags));
63 return_if_error(lzma_alone_decoder_init(&coder->next,
64 allocator, coder->memlimit));
66 // If the application wants to know about missing
67 // integrity check or about the check in general, we
68 // need to handle it here, because LZMA_Alone decoder
69 // doesn't accept any flags.
70 if (coder->flags & LZMA_TELL_NO_CHECK)
73 if (coder->flags & LZMA_TELL_ANY_CHECK)
74 return LZMA_GET_CHECK;
80 const lzma_ret ret = coder->next.code(
81 coder->next.coder, allocator,
83 out, out_pos, out_size, action);
84 if (ret != LZMA_STREAM_END
85 || (coder->flags & LZMA_CONCATENATED) == 0)
88 coder->sequence = SEQ_FINISH;
94 // When LZMA_DECODE_CONCATENATED was used and we were decoding
95 // LZMA_Alone file, we need to check check that there is no
96 // trailing garbage and wait for LZMA_FINISH.
97 if (*in_pos < in_size)
98 return LZMA_DATA_ERROR;
100 return action == LZMA_FINISH ? LZMA_STREAM_END : LZMA_OK;
104 return LZMA_PROG_ERROR;
110 auto_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
112 lzma_next_end(&coder->next, allocator);
113 lzma_free(coder, allocator);
119 auto_decoder_get_check(const lzma_coder *coder)
121 // It is LZMA_Alone if get_check is NULL.
122 return coder->next.get_check == NULL ? LZMA_CHECK_NONE
123 : coder->next.get_check(coder->next.coder);
128 auto_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
129 uint64_t *old_memlimit, uint64_t new_memlimit)
133 if (coder->next.memconfig != NULL) {
134 ret = coder->next.memconfig(coder->next.coder,
135 memusage, old_memlimit, new_memlimit);
136 assert(*old_memlimit == coder->memlimit);
138 // No coder is configured yet. Use the base value as
139 // the current memory usage.
140 *memusage = LZMA_MEMUSAGE_BASE;
141 *old_memlimit = coder->memlimit;
145 if (ret == LZMA_OK && new_memlimit != 0)
146 coder->memlimit = new_memlimit;
153 auto_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
154 uint64_t memlimit, uint32_t flags)
156 lzma_next_coder_init(auto_decoder_init, next, allocator);
159 return LZMA_PROG_ERROR;
161 if (flags & ~LZMA_SUPPORTED_FLAGS)
162 return LZMA_OPTIONS_ERROR;
164 if (next->coder == NULL) {
165 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
166 if (next->coder == NULL)
167 return LZMA_MEM_ERROR;
169 next->code = &auto_decode;
170 next->end = &auto_decoder_end;
171 next->get_check = &auto_decoder_get_check;
172 next->memconfig = &auto_decoder_memconfig;
173 next->coder->next = LZMA_NEXT_CODER_INIT;
176 next->coder->memlimit = memlimit;
177 next->coder->flags = flags;
178 next->coder->sequence = SEQ_INIT;
184 extern LZMA_API(lzma_ret)
185 lzma_auto_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
187 lzma_next_strm_init(auto_decoder_init, strm, memlimit, flags);
189 strm->internal->supported_actions[LZMA_RUN] = true;
190 strm->internal->supported_actions[LZMA_FINISH] = true;