| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "stream_decoder.h" |
| 13 |
#include "alone_decoder.h" |
| 14 |
#ifdef HAVE_LZIP_DECODER |
| 15 |
# include "lzip_decoder.h" |
| 16 |
#endif |
| 17 |
|
| 18 |
|
| 19 |
typedef struct { |
| 20 |
|
| 21 |
lzma_next_coder next; |
| 22 |
|
| 23 |
uint64_t memlimit; |
| 24 |
uint32_t flags; |
| 25 |
|
| 26 |
enum { |
| 27 |
SEQ_INIT, |
| 28 |
SEQ_CODE, |
| 29 |
SEQ_FINISH, |
| 30 |
} sequence; |
| 31 |
} lzma_auto_coder; |
| 32 |
|
| 33 |
|
| 34 |
static lzma_ret |
| 35 |
auto_decode(void *coder_ptr, const lzma_allocator *allocator, |
| 36 |
const uint8_t *restrict in, size_t *restrict in_pos, |
| 37 |
size_t in_size, uint8_t *restrict out, |
| 38 |
size_t *restrict out_pos, size_t out_size, lzma_action action) |
| 39 |
{ |
| 40 |
lzma_auto_coder *coder = coder_ptr; |
| 41 |
|
| 42 |
switch (coder->sequence) { |
| 43 |
case SEQ_INIT: |
| 44 |
if (*in_pos >= in_size) |
| 45 |
return LZMA_OK; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
coder->sequence = SEQ_CODE; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
if (in[*in_pos] == 0xFD) { |
| 58 |
return_if_error(lzma_stream_decoder_init( |
| 59 |
&coder->next, allocator, |
| 60 |
coder->memlimit, coder->flags)); |
| 61 |
#ifdef HAVE_LZIP_DECODER |
| 62 |
} else if (in[*in_pos] == 0x4C) { |
| 63 |
return_if_error(lzma_lzip_decoder_init( |
| 64 |
&coder->next, allocator, |
| 65 |
coder->memlimit, coder->flags)); |
| 66 |
#endif |
| 67 |
} else { |
| 68 |
return_if_error(lzma_alone_decoder_init(&coder->next, |
| 69 |
allocator, coder->memlimit, true)); |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
if (coder->flags & LZMA_TELL_NO_CHECK) |
| 76 |
return LZMA_NO_CHECK; |
| 77 |
|
| 78 |
if (coder->flags & LZMA_TELL_ANY_CHECK) |
| 79 |
return LZMA_GET_CHECK; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
case SEQ_CODE: { |
| 85 |
const lzma_ret ret = coder->next.code( |
| 86 |
coder->next.coder, allocator, |
| 87 |
in, in_pos, in_size, |
| 88 |
out, out_pos, out_size, action); |
| 89 |
if (ret != LZMA_STREAM_END |
| 90 |
|| (coder->flags & LZMA_CONCATENATED) == 0) |
| 91 |
return ret; |
| 92 |
|
| 93 |
coder->sequence = SEQ_FINISH; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
case SEQ_FINISH: |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
if (*in_pos < in_size) |
| 103 |
return LZMA_DATA_ERROR; |
| 104 |
|
| 105 |
return action == LZMA_FINISH ? LZMA_STREAM_END : LZMA_OK; |
| 106 |
|
| 107 |
default: |
| 108 |
assert(0); |
| 109 |
return LZMA_PROG_ERROR; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
static void |
| 115 |
auto_decoder_end(void *coder_ptr, const lzma_allocator *allocator) |
| 116 |
{ |
| 117 |
lzma_auto_coder *coder = coder_ptr; |
| 118 |
lzma_next_end(&coder->next, allocator); |
| 119 |
lzma_free(coder, allocator); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
static lzma_check |
| 125 |
auto_decoder_get_check(const void *coder_ptr) |
| 126 |
{ |
| 127 |
const lzma_auto_coder *coder = coder_ptr; |
| 128 |
|
| 129 |
|
| 130 |
return coder->next.get_check == NULL ? LZMA_CHECK_NONE |
| 131 |
: coder->next.get_check(coder->next.coder); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
static lzma_ret |
| 136 |
auto_decoder_memconfig(void *coder_ptr, uint64_t *memusage, |
| 137 |
uint64_t *old_memlimit, uint64_t new_memlimit) |
| 138 |
{ |
| 139 |
lzma_auto_coder *coder = coder_ptr; |
| 140 |
|
| 141 |
lzma_ret ret; |
| 142 |
|
| 143 |
if (coder->next.memconfig != NULL) { |
| 144 |
ret = coder->next.memconfig(coder->next.coder, |
| 145 |
memusage, old_memlimit, new_memlimit); |
| 146 |
assert(*old_memlimit == coder->memlimit); |
| 147 |
} else { |
| 148 |
|
| 149 |
|
| 150 |
*memusage = LZMA_MEMUSAGE_BASE; |
| 151 |
*old_memlimit = coder->memlimit; |
| 152 |
|
| 153 |
ret = LZMA_OK; |
| 154 |
if (new_memlimit != 0 && new_memlimit < *memusage) |
| 155 |
ret = LZMA_MEMLIMIT_ERROR; |
| 156 |
} |
| 157 |
|
| 158 |
if (ret == LZMA_OK && new_memlimit != 0) |
| 159 |
coder->memlimit = new_memlimit; |
| 160 |
|
| 161 |
return ret; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
static lzma_ret |
| 166 |
auto_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, |
| 167 |
uint64_t memlimit, uint32_t flags) |
| 168 |
{ |
| 169 |
lzma_next_coder_init(&auto_decoder_init, next, allocator); |
| 170 |
|
| 171 |
if (flags & ~LZMA_SUPPORTED_FLAGS) |
| 172 |
return LZMA_OPTIONS_ERROR; |
| 173 |
|
| 174 |
lzma_auto_coder *coder = next->coder; |
| 175 |
if (coder == NULL) { |
| 176 |
coder = lzma_alloc(sizeof(lzma_auto_coder), allocator); |
| 177 |
if (coder == NULL) |
| 178 |
return LZMA_MEM_ERROR; |
| 179 |
|
| 180 |
next->coder = coder; |
| 181 |
next->code = &auto_decode; |
| 182 |
next->end = &auto_decoder_end; |
| 183 |
next->get_check = &auto_decoder_get_check; |
| 184 |
next->memconfig = &auto_decoder_memconfig; |
| 185 |
coder->next = LZMA_NEXT_CODER_INIT; |
| 186 |
} |
| 187 |
|
| 188 |
coder->memlimit = my_max(1, memlimit); |
| 189 |
coder->flags = flags; |
| 190 |
coder->sequence = SEQ_INIT; |
| 191 |
|
| 192 |
return LZMA_OK; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
extern LZMA_API(lzma_ret) |
| 197 |
lzma_auto_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags) |
| 198 |
{ |
| 199 |
lzma_next_strm_init(auto_decoder_init, strm, memlimit, flags); |
| 200 |
|
| 201 |
strm->internal->supported_actions[LZMA_RUN] = true; |
| 202 |
strm->internal->supported_actions[LZMA_FINISH] = true; |
| 203 |
|
| 204 |
return LZMA_OK; |
| 205 |
} |