Deleted Added
full compact
auto_decoder.c (292588) auto_decoder.c (312518)
1///////////////////////////////////////////////////////////////////////////////
2//
3/// \file auto_decoder.c
4/// \brief Autodetect between .xz Stream and .lzma (LZMA_Alone) formats
5//
6// Author: Lasse Collin
7//
8// This file has been put into the public domain.
9// You can do whatever you want with this file.
10//
11///////////////////////////////////////////////////////////////////////////////
12
13#include "stream_decoder.h"
14#include "alone_decoder.h"
15
16
1///////////////////////////////////////////////////////////////////////////////
2//
3/// \file auto_decoder.c
4/// \brief Autodetect between .xz Stream and .lzma (LZMA_Alone) formats
5//
6// Author: Lasse Collin
7//
8// This file has been put into the public domain.
9// You can do whatever you want with this file.
10//
11///////////////////////////////////////////////////////////////////////////////
12
13#include "stream_decoder.h"
14#include "alone_decoder.h"
15
16
17struct lzma_coder_s {
17typedef struct {
18 /// Stream decoder or LZMA_Alone decoder
19 lzma_next_coder next;
20
21 uint64_t memlimit;
22 uint32_t flags;
23
24 enum {
25 SEQ_INIT,
26 SEQ_CODE,
27 SEQ_FINISH,
28 } sequence;
18 /// Stream decoder or LZMA_Alone decoder
19 lzma_next_coder next;
20
21 uint64_t memlimit;
22 uint32_t flags;
23
24 enum {
25 SEQ_INIT,
26 SEQ_CODE,
27 SEQ_FINISH,
28 } sequence;
29};
29} lzma_auto_coder;
30
31
32static lzma_ret
30
31
32static lzma_ret
33auto_decode(lzma_coder *coder, const lzma_allocator *allocator,
33auto_decode(void *coder_ptr, const lzma_allocator *allocator,
34 const uint8_t *restrict in, size_t *restrict in_pos,
35 size_t in_size, uint8_t *restrict out,
36 size_t *restrict out_pos, size_t out_size, lzma_action action)
37{
34 const uint8_t *restrict in, size_t *restrict in_pos,
35 size_t in_size, uint8_t *restrict out,
36 size_t *restrict out_pos, size_t out_size, lzma_action action)
37{
38 lzma_auto_coder *coder = coder_ptr;
39
38 switch (coder->sequence) {
39 case SEQ_INIT:
40 if (*in_pos >= in_size)
41 return LZMA_OK;
42
43 // Update the sequence now, because we want to continue from
44 // SEQ_CODE even if we return some LZMA_*_CHECK.
45 coder->sequence = SEQ_CODE;

--- 49 unchanged lines hidden (view full) ---

95 default:
96 assert(0);
97 return LZMA_PROG_ERROR;
98 }
99}
100
101
102static void
40 switch (coder->sequence) {
41 case SEQ_INIT:
42 if (*in_pos >= in_size)
43 return LZMA_OK;
44
45 // Update the sequence now, because we want to continue from
46 // SEQ_CODE even if we return some LZMA_*_CHECK.
47 coder->sequence = SEQ_CODE;

--- 49 unchanged lines hidden (view full) ---

97 default:
98 assert(0);
99 return LZMA_PROG_ERROR;
100 }
101}
102
103
104static void
103auto_decoder_end(lzma_coder *coder, const lzma_allocator *allocator)
105auto_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
104{
106{
107 lzma_auto_coder *coder = coder_ptr;
105 lzma_next_end(&coder->next, allocator);
106 lzma_free(coder, allocator);
107 return;
108}
109
110
111static lzma_check
108 lzma_next_end(&coder->next, allocator);
109 lzma_free(coder, allocator);
110 return;
111}
112
113
114static lzma_check
112auto_decoder_get_check(const lzma_coder *coder)
115auto_decoder_get_check(const void *coder_ptr)
113{
116{
117 const lzma_auto_coder *coder = coder_ptr;
118
114 // It is LZMA_Alone if get_check is NULL.
115 return coder->next.get_check == NULL ? LZMA_CHECK_NONE
116 : coder->next.get_check(coder->next.coder);
117}
118
119
120static lzma_ret
119 // It is LZMA_Alone if get_check is NULL.
120 return coder->next.get_check == NULL ? LZMA_CHECK_NONE
121 : coder->next.get_check(coder->next.coder);
122}
123
124
125static lzma_ret
121auto_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
126auto_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
122 uint64_t *old_memlimit, uint64_t new_memlimit)
123{
127 uint64_t *old_memlimit, uint64_t new_memlimit)
128{
129 lzma_auto_coder *coder = coder_ptr;
130
124 lzma_ret ret;
125
126 if (coder->next.memconfig != NULL) {
127 ret = coder->next.memconfig(coder->next.coder,
128 memusage, old_memlimit, new_memlimit);
129 assert(*old_memlimit == coder->memlimit);
130 } else {
131 // No coder is configured yet. Use the base value as

--- 17 unchanged lines hidden (view full) ---

149 lzma_next_coder_init(&auto_decoder_init, next, allocator);
150
151 if (memlimit == 0)
152 return LZMA_PROG_ERROR;
153
154 if (flags & ~LZMA_SUPPORTED_FLAGS)
155 return LZMA_OPTIONS_ERROR;
156
131 lzma_ret ret;
132
133 if (coder->next.memconfig != NULL) {
134 ret = coder->next.memconfig(coder->next.coder,
135 memusage, old_memlimit, new_memlimit);
136 assert(*old_memlimit == coder->memlimit);
137 } else {
138 // No coder is configured yet. Use the base value as

--- 17 unchanged lines hidden (view full) ---

156 lzma_next_coder_init(&auto_decoder_init, next, allocator);
157
158 if (memlimit == 0)
159 return LZMA_PROG_ERROR;
160
161 if (flags & ~LZMA_SUPPORTED_FLAGS)
162 return LZMA_OPTIONS_ERROR;
163
157 if (next->coder == NULL) {
158 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
159 if (next->coder == NULL)
164 lzma_auto_coder *coder = next->coder;
165 if (coder == NULL) {
166 coder = lzma_alloc(sizeof(lzma_auto_coder), allocator);
167 if (coder == NULL)
160 return LZMA_MEM_ERROR;
161
168 return LZMA_MEM_ERROR;
169
170 next->coder = coder;
162 next->code = &auto_decode;
163 next->end = &auto_decoder_end;
164 next->get_check = &auto_decoder_get_check;
165 next->memconfig = &auto_decoder_memconfig;
171 next->code = &auto_decode;
172 next->end = &auto_decoder_end;
173 next->get_check = &auto_decoder_get_check;
174 next->memconfig = &auto_decoder_memconfig;
166 next->coder->next = LZMA_NEXT_CODER_INIT;
175 coder->next = LZMA_NEXT_CODER_INIT;
167 }
168
176 }
177
169 next->coder->memlimit = memlimit;
170 next->coder->flags = flags;
171 next->coder->sequence = SEQ_INIT;
178 coder->memlimit = memlimit;
179 coder->flags = flags;
180 coder->sequence = SEQ_INIT;
172
173 return LZMA_OK;
174}
175
176
177extern LZMA_API(lzma_ret)
178lzma_auto_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
179{
180 lzma_next_strm_init(auto_decoder_init, strm, memlimit, flags);
181
182 strm->internal->supported_actions[LZMA_RUN] = true;
183 strm->internal->supported_actions[LZMA_FINISH] = true;
184
185 return LZMA_OK;
186}
181
182 return LZMA_OK;
183}
184
185
186extern LZMA_API(lzma_ret)
187lzma_auto_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
188{
189 lzma_next_strm_init(auto_decoder_init, strm, memlimit, flags);
190
191 strm->internal->supported_actions[LZMA_RUN] = true;
192 strm->internal->supported_actions[LZMA_FINISH] = true;
193
194 return LZMA_OK;
195}