]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/auto_decoder.c
Renamed constants:
[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         /// Stream decoder or LZMA_Alone decoder
26         lzma_next_coder next;
27
28         uint64_t memlimit;
29         uint32_t flags;
30
31         enum {
32                 SEQ_INIT,
33                 SEQ_CODE,
34                 SEQ_FINISH,
35         } sequence;
36 };
37
38
39 static lzma_ret
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)
44 {
45         switch (coder->sequence) {
46         case SEQ_INIT:
47                 if (*in_pos >= in_size)
48                         return LZMA_OK;
49
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;
53
54                 // Detect the file format. For now this is simple, since if
55                 // it doesn't start with 0xFF (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] == 0xFF) {
59                         return_if_error(lzma_stream_decoder_init(
60                                         &coder->next, allocator,
61                                         coder->memlimit, coder->flags));
62                 } else {
63                         return_if_error(lzma_alone_decoder_init(&coder->next,
64                                         allocator, coder->memlimit));
65
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)
71                                 return LZMA_NO_CHECK;
72
73                         if (coder->flags & LZMA_TELL_ANY_CHECK)
74                                 return LZMA_GET_CHECK;
75                 }
76
77         // Fall through
78
79         case SEQ_CODE: {
80                 const lzma_ret ret = coder->next.code(
81                                 coder->next.coder, allocator,
82                                 in, in_pos, in_size,
83                                 out, out_pos, out_size, action);
84                 if (ret != LZMA_STREAM_END
85                                 || (coder->flags & LZMA_CONCATENATED) == 0)
86                         return ret;
87
88                 coder->sequence = SEQ_FINISH;
89         }
90
91         // Fall through
92
93         case 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;
99
100                 return action == LZMA_FINISH ? LZMA_STREAM_END : LZMA_OK;
101
102         default:
103                 assert(0);
104                 return LZMA_PROG_ERROR;
105         }
106 }
107
108
109 static void
110 auto_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
111 {
112         lzma_next_end(&coder->next, allocator);
113         lzma_free(coder, allocator);
114         return;
115 }
116
117
118 static lzma_check
119 auto_decoder_get_check(const lzma_coder *coder)
120 {
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);
124 }
125
126
127 static lzma_ret
128 auto_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
129                 uint64_t memlimit, uint32_t flags)
130 {
131         lzma_next_coder_init(auto_decoder_init, next, allocator);
132
133         if (flags & ~LZMA_SUPPORTED_FLAGS)
134                 return LZMA_OPTIONS_ERROR;
135
136         if (next->coder == NULL) {
137                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
138                 if (next->coder == NULL)
139                         return LZMA_MEM_ERROR;
140
141                 next->code = &auto_decode;
142                 next->end = &auto_decoder_end;
143                 next->get_check = &auto_decoder_get_check;
144                 next->coder->next = LZMA_NEXT_CODER_INIT;
145         }
146
147         next->coder->memlimit = memlimit;
148         next->coder->flags = flags;
149         next->coder->sequence = SEQ_INIT;
150
151         return LZMA_OK;
152 }
153
154
155 extern LZMA_API lzma_ret
156 lzma_auto_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
157 {
158         lzma_next_strm_init(auto_decoder_init, strm, memlimit, flags);
159
160         strm->internal->supported_actions[LZMA_RUN] = true;
161         strm->internal->supported_actions[LZMA_FINISH] = true;
162
163         return LZMA_OK;
164 }