]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/auto_decoder.c
Added bsr.h.
[icculus/xz.git] / src / liblzma / common / auto_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       auto_decoder.c
4 /// \brief      Autodetect between .lzma Stream and LZMA_Alone formats
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 "common.h"
21 #include "alone_decoder.h"
22
23
24 struct lzma_coder_s {
25         lzma_next_coder next;
26
27         lzma_extra **header;
28         lzma_extra **footer;
29         bool initialized;
30 };
31
32
33 static lzma_ret
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)
38 {
39         if (!coder->initialized) {
40                 if (*in_pos >= in_size)
41                         return LZMA_OK;
42
43                 lzma_ret ret;
44
45                 if (in[*in_pos] == 0xFF)
46                         ret = lzma_stream_decoder_init(&coder->next, allocator,
47                                         coder->header, coder->footer);
48                 else
49                         ret = lzma_alone_decoder_init(&coder->next, allocator);
50
51                 if (ret != LZMA_OK)
52                         return ret;
53
54                 coder->initialized = true;
55         }
56
57         return coder->next.code(coder->next.coder, allocator,
58                         in, in_pos, in_size, out, out_pos, out_size, action);
59 }
60
61
62 static void
63 auto_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
64 {
65         lzma_next_coder_end(&coder->next, allocator);
66         lzma_free(coder, allocator);
67         return;
68 }
69
70
71 static lzma_ret
72 auto_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
73                 lzma_extra **header, lzma_extra **footer)
74 {
75         if (next->coder == NULL) {
76                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
77                 if (next->coder == NULL)
78                         return LZMA_MEM_ERROR;
79
80                 next->code = &auto_decode;
81                 next->end = &auto_decoder_end;
82                 next->coder->next = LZMA_NEXT_CODER_INIT;
83         }
84
85         next->coder->header = header;
86         next->coder->footer = footer;
87         next->coder->initialized = false;
88
89         return LZMA_OK;
90 }
91
92
93 /*
94 extern lzma_ret
95 lzma_auto_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
96                 lzma_extra **header, lzma_extra **footer)
97 {
98         lzma_next_coder_init(
99                         auto_decoder_init, next, allocator, header, footer);
100 }
101 */
102
103
104 extern LZMA_API lzma_ret
105 lzma_auto_decoder(lzma_stream *strm, lzma_extra **header, lzma_extra **footer)
106 {
107         lzma_next_strm_init(strm, auto_decoder_init, header, footer);
108
109         strm->internal->supported_actions[LZMA_RUN] = true;
110         strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
111
112         return LZMA_OK;
113 }