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 ///////////////////////////////////////////////////////////////////////////////
21 #include "alone_decoder.h"
34 auto_decode(lzma_coder *coder, lzma_allocator *allocator,
35 const uint8_t *restrict in, size_t *restrict in_pos,
36 size_t in_size, uint8_t *restrict out,
37 size_t *restrict out_pos, size_t out_size, lzma_action action)
39 if (!coder->initialized) {
40 if (*in_pos >= in_size)
45 if (in[*in_pos] == 0xFF)
46 ret = lzma_stream_decoder_init(&coder->next, allocator,
47 coder->header, coder->footer);
49 ret = lzma_alone_decoder_init(&coder->next, allocator);
54 coder->initialized = true;
57 return coder->next.code(coder->next.coder, allocator,
58 in, in_pos, in_size, out, out_pos, out_size, action);
63 auto_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
65 lzma_next_coder_end(&coder->next, allocator);
66 lzma_free(coder, allocator);
72 auto_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
73 lzma_extra **header, lzma_extra **footer)
75 if (next->coder == NULL) {
76 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
77 if (next->coder == NULL)
78 return LZMA_MEM_ERROR;
80 next->code = &auto_decode;
81 next->end = &auto_decoder_end;
82 next->coder->next = LZMA_NEXT_CODER_INIT;
85 next->coder->header = header;
86 next->coder->footer = footer;
87 next->coder->initialized = false;
95 lzma_auto_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
96 lzma_extra **header, lzma_extra **footer)
99 auto_decoder_init, next, allocator, header, footer);
104 extern LZMA_API lzma_ret
105 lzma_auto_decoder(lzma_stream *strm, lzma_extra **header, lzma_extra **footer)
107 lzma_next_strm_init(strm, auto_decoder_init, header, footer);
109 strm->internal->supported_actions[LZMA_RUN] = true;
110 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;