| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "index_decoder.h" |
| 13 |
#include "check.h" |
| 14 |
|
| 15 |
|
| 16 |
typedef struct { |
| 17 |
enum { |
| 18 |
SEQ_INDICATOR, |
| 19 |
SEQ_COUNT, |
| 20 |
SEQ_MEMUSAGE, |
| 21 |
SEQ_UNPADDED, |
| 22 |
SEQ_UNCOMPRESSED, |
| 23 |
SEQ_PADDING_INIT, |
| 24 |
SEQ_PADDING, |
| 25 |
SEQ_CRC32, |
| 26 |
} sequence; |
| 27 |
|
| 28 |
|
| 29 |
uint64_t memlimit; |
| 30 |
|
| 31 |
|
| 32 |
lzma_index *index; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
lzma_index **index_ptr; |
| 37 |
|
| 38 |
|
| 39 |
lzma_vli count; |
| 40 |
|
| 41 |
|
| 42 |
lzma_vli unpadded_size; |
| 43 |
|
| 44 |
|
| 45 |
lzma_vli uncompressed_size; |
| 46 |
|
| 47 |
|
| 48 |
size_t pos; |
| 49 |
|
| 50 |
|
| 51 |
uint32_t crc32; |
| 52 |
} lzma_index_coder; |
| 53 |
|
| 54 |
|
| 55 |
static lzma_ret |
| 56 |
index_decode(void *coder_ptr, const lzma_allocator *allocator, |
| 57 |
const uint8_t *restrict in, size_t *restrict in_pos, |
| 58 |
size_t in_size, |
| 59 |
uint8_t *restrict out lzma_attribute((__unused__)), |
| 60 |
size_t *restrict out_pos lzma_attribute((__unused__)), |
| 61 |
size_t out_size lzma_attribute((__unused__)), |
| 62 |
lzma_action action lzma_attribute((__unused__))) |
| 63 |
{ |
| 64 |
lzma_index_coder *coder = coder_ptr; |
| 65 |
|
| 66 |
|
| 67 |
const size_t in_start = *in_pos; |
| 68 |
lzma_ret ret = LZMA_OK; |
| 69 |
|
| 70 |
while (*in_pos < in_size) |
| 71 |
switch (coder->sequence) { |
| 72 |
case SEQ_INDICATOR: |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
if (in[(*in_pos)++] != INDEX_INDICATOR) |
| 83 |
return LZMA_DATA_ERROR; |
| 84 |
|
| 85 |
coder->sequence = SEQ_COUNT; |
| 86 |
break; |
| 87 |
|
| 88 |
case SEQ_COUNT: |
| 89 |
ret = lzma_vli_decode(&coder->count, &coder->pos, |
| 90 |
in, in_pos, in_size); |
| 91 |
if (ret != LZMA_STREAM_END) |
| 92 |
goto out; |
| 93 |
|
| 94 |
coder->pos = 0; |
| 95 |
coder->sequence = SEQ_MEMUSAGE; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
case SEQ_MEMUSAGE: |
| 100 |
if (lzma_index_memusage(1, coder->count) > coder->memlimit) { |
| 101 |
ret = LZMA_MEMLIMIT_ERROR; |
| 102 |
goto out; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
lzma_index_prealloc(coder->index, coder->count); |
| 108 |
|
| 109 |
ret = LZMA_OK; |
| 110 |
coder->sequence = coder->count == 0 |
| 111 |
? SEQ_PADDING_INIT : SEQ_UNPADDED; |
| 112 |
break; |
| 113 |
|
| 114 |
case SEQ_UNPADDED: |
| 115 |
case SEQ_UNCOMPRESSED: { |
| 116 |
lzma_vli *size = coder->sequence == SEQ_UNPADDED |
| 117 |
? &coder->unpadded_size |
| 118 |
: &coder->uncompressed_size; |
| 119 |
|
| 120 |
ret = lzma_vli_decode(size, &coder->pos, |
| 121 |
in, in_pos, in_size); |
| 122 |
if (ret != LZMA_STREAM_END) |
| 123 |
goto out; |
| 124 |
|
| 125 |
ret = LZMA_OK; |
| 126 |
coder->pos = 0; |
| 127 |
|
| 128 |
if (coder->sequence == SEQ_UNPADDED) { |
| 129 |
|
| 130 |
|
| 131 |
if (coder->unpadded_size < UNPADDED_SIZE_MIN |
| 132 |
|| coder->unpadded_size |
| 133 |
> UNPADDED_SIZE_MAX) |
| 134 |
return LZMA_DATA_ERROR; |
| 135 |
|
| 136 |
coder->sequence = SEQ_UNCOMPRESSED; |
| 137 |
} else { |
| 138 |
|
| 139 |
return_if_error(lzma_index_append( |
| 140 |
coder->index, allocator, |
| 141 |
coder->unpadded_size, |
| 142 |
coder->uncompressed_size)); |
| 143 |
|
| 144 |
|
| 145 |
coder->sequence = --coder->count == 0 |
| 146 |
? SEQ_PADDING_INIT |
| 147 |
: SEQ_UNPADDED; |
| 148 |
} |
| 149 |
|
| 150 |
break; |
| 151 |
} |
| 152 |
|
| 153 |
case SEQ_PADDING_INIT: |
| 154 |
coder->pos = lzma_index_padding_size(coder->index); |
| 155 |
coder->sequence = SEQ_PADDING; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
case SEQ_PADDING: |
| 160 |
if (coder->pos > 0) { |
| 161 |
--coder->pos; |
| 162 |
if (in[(*in_pos)++] != 0x00) |
| 163 |
return LZMA_DATA_ERROR; |
| 164 |
|
| 165 |
break; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
coder->crc32 = lzma_crc32(in + in_start, |
| 170 |
*in_pos - in_start, coder->crc32); |
| 171 |
|
| 172 |
coder->sequence = SEQ_CRC32; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
case SEQ_CRC32: |
| 177 |
do { |
| 178 |
if (*in_pos == in_size) |
| 179 |
return LZMA_OK; |
| 180 |
|
| 181 |
if (((coder->crc32 >> (coder->pos * 8)) & 0xFF) |
| 182 |
!= in[(*in_pos)++]) { |
| 183 |
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 184 |
return LZMA_DATA_ERROR; |
| 185 |
#endif |
| 186 |
} |
| 187 |
|
| 188 |
} while (++coder->pos < 4); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
*coder->index_ptr = coder->index; |
| 193 |
|
| 194 |
|
| 195 |
coder->index = NULL; |
| 196 |
|
| 197 |
return LZMA_STREAM_END; |
| 198 |
|
| 199 |
default: |
| 200 |
assert(0); |
| 201 |
return LZMA_PROG_ERROR; |
| 202 |
} |
| 203 |
|
| 204 |
out: |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
{ |
| 210 |
const size_t in_used = *in_pos - in_start; |
| 211 |
if (in_used > 0) |
| 212 |
coder->crc32 = lzma_crc32(in + in_start, |
| 213 |
in_used, coder->crc32); |
| 214 |
} |
| 215 |
|
| 216 |
return ret; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
static void |
| 221 |
index_decoder_end(void *coder_ptr, const lzma_allocator *allocator) |
| 222 |
{ |
| 223 |
lzma_index_coder *coder = coder_ptr; |
| 224 |
lzma_index_end(coder->index, allocator); |
| 225 |
lzma_free(coder, allocator); |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
static lzma_ret |
| 231 |
index_decoder_memconfig(void *coder_ptr, uint64_t *memusage, |
| 232 |
uint64_t *old_memlimit, uint64_t new_memlimit) |
| 233 |
{ |
| 234 |
lzma_index_coder *coder = coder_ptr; |
| 235 |
|
| 236 |
*memusage = lzma_index_memusage(1, coder->count); |
| 237 |
*old_memlimit = coder->memlimit; |
| 238 |
|
| 239 |
if (new_memlimit != 0) { |
| 240 |
if (new_memlimit < *memusage) |
| 241 |
return LZMA_MEMLIMIT_ERROR; |
| 242 |
|
| 243 |
coder->memlimit = new_memlimit; |
| 244 |
} |
| 245 |
|
| 246 |
return LZMA_OK; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
static lzma_ret |
| 251 |
index_decoder_reset(lzma_index_coder *coder, const lzma_allocator *allocator, |
| 252 |
lzma_index **i, uint64_t memlimit) |
| 253 |
{ |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
coder->index_ptr = i; |
| 259 |
*i = NULL; |
| 260 |
|
| 261 |
|
| 262 |
coder->index = lzma_index_init(allocator); |
| 263 |
if (coder->index == NULL) |
| 264 |
return LZMA_MEM_ERROR; |
| 265 |
|
| 266 |
|
| 267 |
coder->sequence = SEQ_INDICATOR; |
| 268 |
coder->memlimit = my_max(1, memlimit); |
| 269 |
coder->count = 0; |
| 270 |
coder->pos = 0; |
| 271 |
coder->crc32 = 0; |
| 272 |
|
| 273 |
return LZMA_OK; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
extern lzma_ret |
| 278 |
lzma_index_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, |
| 279 |
lzma_index **i, uint64_t memlimit) |
| 280 |
{ |
| 281 |
lzma_next_coder_init(&lzma_index_decoder_init, next, allocator); |
| 282 |
|
| 283 |
if (i == NULL) |
| 284 |
return LZMA_PROG_ERROR; |
| 285 |
|
| 286 |
lzma_index_coder *coder = next->coder; |
| 287 |
if (coder == NULL) { |
| 288 |
coder = lzma_alloc(sizeof(lzma_index_coder), allocator); |
| 289 |
if (coder == NULL) |
| 290 |
return LZMA_MEM_ERROR; |
| 291 |
|
| 292 |
next->coder = coder; |
| 293 |
next->code = &index_decode; |
| 294 |
next->end = &index_decoder_end; |
| 295 |
next->memconfig = &index_decoder_memconfig; |
| 296 |
coder->index = NULL; |
| 297 |
} else { |
| 298 |
lzma_index_end(coder->index, allocator); |
| 299 |
} |
| 300 |
|
| 301 |
return index_decoder_reset(coder, allocator, i, memlimit); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
extern LZMA_API(lzma_ret) |
| 306 |
lzma_index_decoder(lzma_stream *strm, lzma_index **i, uint64_t memlimit) |
| 307 |
{ |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
if (i != NULL) |
| 312 |
*i = NULL; |
| 313 |
|
| 314 |
lzma_next_strm_init(lzma_index_decoder_init, strm, i, memlimit); |
| 315 |
|
| 316 |
strm->internal->supported_actions[LZMA_RUN] = true; |
| 317 |
strm->internal->supported_actions[LZMA_FINISH] = true; |
| 318 |
|
| 319 |
return LZMA_OK; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
extern LZMA_API(lzma_ret) |
| 324 |
lzma_index_buffer_decode(lzma_index **i, uint64_t *memlimit, |
| 325 |
const lzma_allocator *allocator, |
| 326 |
const uint8_t *in, size_t *in_pos, size_t in_size) |
| 327 |
{ |
| 328 |
|
| 329 |
|
| 330 |
if (i != NULL) |
| 331 |
*i = NULL; |
| 332 |
|
| 333 |
|
| 334 |
if (i == NULL || memlimit == NULL |
| 335 |
|| in == NULL || in_pos == NULL || *in_pos > in_size) |
| 336 |
return LZMA_PROG_ERROR; |
| 337 |
|
| 338 |
|
| 339 |
lzma_index_coder coder; |
| 340 |
return_if_error(index_decoder_reset(&coder, allocator, i, *memlimit)); |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
const size_t in_start = *in_pos; |
| 345 |
|
| 346 |
|
| 347 |
lzma_ret ret = index_decode(&coder, allocator, in, in_pos, in_size, |
| 348 |
NULL, NULL, 0, LZMA_RUN); |
| 349 |
|
| 350 |
if (ret == LZMA_STREAM_END) { |
| 351 |
ret = LZMA_OK; |
| 352 |
} else { |
| 353 |
|
| 354 |
|
| 355 |
lzma_index_end(coder.index, allocator); |
| 356 |
*in_pos = in_start; |
| 357 |
|
| 358 |
if (ret == LZMA_OK) { |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
ret = LZMA_DATA_ERROR; |
| 363 |
|
| 364 |
} else if (ret == LZMA_MEMLIMIT_ERROR) { |
| 365 |
|
| 366 |
|
| 367 |
*memlimit = lzma_index_memusage(1, coder.count); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
return ret; |
| 372 |
} |