1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file auto_decoder.c
4 /// \brief Autodetect between .lzma Stream and 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"
31 auto_decode(lzma_coder *coder, lzma_allocator *allocator,
32 const uint8_t *restrict in, size_t *restrict in_pos,
33 size_t in_size, uint8_t *restrict out,
34 size_t *restrict out_pos, size_t out_size, lzma_action action)
36 if (!coder->initialized) {
37 if (*in_pos >= in_size)
42 if (in[*in_pos] == 0xFF)
43 ret = lzma_stream_decoder_init(
44 &coder->next, allocator);
46 ret = lzma_alone_decoder_init(&coder->next, allocator);
51 coder->initialized = true;
54 return coder->next.code(coder->next.coder, allocator,
55 in, in_pos, in_size, out, out_pos, out_size, action);
60 auto_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
62 lzma_next_coder_end(&coder->next, allocator);
63 lzma_free(coder, allocator);
69 auto_decoder_init(lzma_next_coder *next, lzma_allocator *allocator)
71 if (next->coder == NULL) {
72 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
73 if (next->coder == NULL)
74 return LZMA_MEM_ERROR;
76 next->code = &auto_decode;
77 next->end = &auto_decoder_end;
78 next->coder->next = LZMA_NEXT_CODER_INIT;
81 next->coder->initialized = false;
89 lzma_auto_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
90 lzma_extra **header, lzma_extra **footer)
93 auto_decoder_init, next, allocator, header, footer);
98 extern LZMA_API lzma_ret
99 lzma_auto_decoder(lzma_stream *strm)
101 lzma_next_strm_init0(strm, auto_decoder_init);
103 strm->internal->supported_actions[LZMA_RUN] = true;
104 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;