| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "lzma_decoder.h" |
| 13 |
#include "lz_decoder.h" |
| 14 |
|
| 15 |
|
| 16 |
typedef struct { |
| 17 |
|
| 18 |
lzma_next_coder lzma; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
uint64_t comp_size; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
lzma_vli uncomp_size; |
| 32 |
|
| 33 |
|
| 34 |
uint32_t dict_size; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
bool uncomp_size_is_exact; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
bool props_decoded; |
| 44 |
} lzma_microlzma_coder; |
| 45 |
|
| 46 |
|
| 47 |
static lzma_ret |
| 48 |
microlzma_decode(void *coder_ptr, const lzma_allocator *allocator, |
| 49 |
const uint8_t *restrict in, size_t *restrict in_pos, |
| 50 |
size_t in_size, uint8_t *restrict out, |
| 51 |
size_t *restrict out_pos, size_t out_size, lzma_action action) |
| 52 |
{ |
| 53 |
lzma_microlzma_coder *coder = coder_ptr; |
| 54 |
|
| 55 |
|
| 56 |
const size_t in_start = *in_pos; |
| 57 |
|
| 58 |
|
| 59 |
const size_t out_start = *out_pos; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
if (in_size - *in_pos > coder->comp_size) |
| 66 |
in_size = *in_pos + (size_t)(coder->comp_size); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
if (!coder->uncomp_size_is_exact |
| 72 |
&& out_size - *out_pos > coder->uncomp_size) |
| 73 |
out_size = *out_pos + (size_t)(coder->uncomp_size); |
| 74 |
|
| 75 |
if (!coder->props_decoded) { |
| 76 |
|
| 77 |
|
| 78 |
if (*in_pos >= in_size) |
| 79 |
return LZMA_OK; |
| 80 |
|
| 81 |
lzma_options_lzma options = { |
| 82 |
.dict_size = coder->dict_size, |
| 83 |
.preset_dict = NULL, |
| 84 |
.preset_dict_size = 0, |
| 85 |
.ext_flags = 0, |
| 86 |
.ext_size_low = UINT32_MAX, |
| 87 |
.ext_size_high = UINT32_MAX, |
| 88 |
}; |
| 89 |
|
| 90 |
if (coder->uncomp_size_is_exact) |
| 91 |
lzma_set_ext_size(options, coder->uncomp_size); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if (lzma_lzma_lclppb_decode(&options, ~in[*in_pos])) |
| 96 |
return LZMA_OPTIONS_ERROR; |
| 97 |
|
| 98 |
++*in_pos; |
| 99 |
|
| 100 |
|
| 101 |
lzma_filter_info filters[2] = { |
| 102 |
{ |
| 103 |
.id = LZMA_FILTER_LZMA1EXT, |
| 104 |
.init = &lzma_lzma_decoder_init, |
| 105 |
.options = &options, |
| 106 |
}, { |
| 107 |
.init = NULL, |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
return_if_error(lzma_next_filter_init(&coder->lzma, |
| 112 |
allocator, filters)); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
const uint8_t dummy_in = 0; |
| 117 |
size_t dummy_in_pos = 0; |
| 118 |
if (coder->lzma.code(coder->lzma.coder, allocator, |
| 119 |
&dummy_in, &dummy_in_pos, 1, |
| 120 |
out, out_pos, out_size, LZMA_RUN) != LZMA_OK) |
| 121 |
return LZMA_PROG_ERROR; |
| 122 |
|
| 123 |
assert(dummy_in_pos == 1); |
| 124 |
coder->props_decoded = true; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
lzma_ret ret = coder->lzma.code(coder->lzma.coder, allocator, |
| 129 |
in, in_pos, in_size, |
| 130 |
out, out_pos, out_size, action); |
| 131 |
|
| 132 |
|
| 133 |
assert(coder->comp_size >= *in_pos - in_start); |
| 134 |
coder->comp_size -= *in_pos - in_start; |
| 135 |
|
| 136 |
if (coder->uncomp_size_is_exact) { |
| 137 |
|
| 138 |
|
| 139 |
if (ret == LZMA_STREAM_END && coder->comp_size != 0) |
| 140 |
ret = LZMA_DATA_ERROR; |
| 141 |
} else { |
| 142 |
|
| 143 |
assert(coder->uncomp_size >= *out_pos - out_start); |
| 144 |
coder->uncomp_size -= *out_pos - out_start; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
if (ret == LZMA_STREAM_END) |
| 151 |
ret = LZMA_DATA_ERROR; |
| 152 |
else if (coder->uncomp_size == 0) |
| 153 |
ret = LZMA_STREAM_END; |
| 154 |
} |
| 155 |
|
| 156 |
return ret; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
static void |
| 161 |
microlzma_decoder_end(void *coder_ptr, const lzma_allocator *allocator) |
| 162 |
{ |
| 163 |
lzma_microlzma_coder *coder = coder_ptr; |
| 164 |
lzma_next_end(&coder->lzma, allocator); |
| 165 |
lzma_free(coder, allocator); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
static lzma_ret |
| 171 |
microlzma_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, |
| 172 |
uint64_t comp_size, |
| 173 |
uint64_t uncomp_size, bool uncomp_size_is_exact, |
| 174 |
uint32_t dict_size) |
| 175 |
{ |
| 176 |
lzma_next_coder_init(µlzma_decoder_init, next, allocator); |
| 177 |
|
| 178 |
lzma_microlzma_coder *coder = next->coder; |
| 179 |
|
| 180 |
if (coder == NULL) { |
| 181 |
coder = lzma_alloc(sizeof(lzma_microlzma_coder), allocator); |
| 182 |
if (coder == NULL) |
| 183 |
return LZMA_MEM_ERROR; |
| 184 |
|
| 185 |
next->coder = coder; |
| 186 |
next->code = µlzma_decode; |
| 187 |
next->end = µlzma_decoder_end; |
| 188 |
|
| 189 |
coder->lzma = LZMA_NEXT_CODER_INIT; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
if (uncomp_size > LZMA_VLI_MAX) |
| 195 |
return LZMA_OPTIONS_ERROR; |
| 196 |
|
| 197 |
coder->comp_size = comp_size; |
| 198 |
coder->uncomp_size = uncomp_size; |
| 199 |
coder->uncomp_size_is_exact = uncomp_size_is_exact; |
| 200 |
coder->dict_size = dict_size; |
| 201 |
|
| 202 |
coder->props_decoded = false; |
| 203 |
|
| 204 |
return LZMA_OK; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
extern LZMA_API(lzma_ret) |
| 209 |
lzma_microlzma_decoder(lzma_stream *strm, uint64_t comp_size, |
| 210 |
uint64_t uncomp_size, lzma_bool uncomp_size_is_exact, |
| 211 |
uint32_t dict_size) |
| 212 |
{ |
| 213 |
lzma_next_strm_init(microlzma_decoder_init, strm, comp_size, |
| 214 |
uncomp_size, uncomp_size_is_exact, dict_size); |
| 215 |
|
| 216 |
strm->internal->supported_actions[LZMA_RUN] = true; |
| 217 |
strm->internal->supported_actions[LZMA_FINISH] = true; |
| 218 |
|
| 219 |
return LZMA_OK; |
| 220 |
} |