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