Denys Vlasenko | 602ce69 | 2010-05-30 03:35:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * .xz Stream decoder |
| 3 | * |
| 4 | * Author: Lasse Collin <lasse.collin@tukaani.org> |
| 5 | * |
| 6 | * This file has been put into the public domain. |
| 7 | * You can do whatever you want with this file. |
| 8 | */ |
| 9 | |
| 10 | #include "xz_private.h" |
| 11 | #include "xz_stream.h" |
| 12 | |
| 13 | /* Hash used to validate the Index field */ |
| 14 | struct xz_dec_hash { |
| 15 | vli_type unpadded; |
| 16 | vli_type uncompressed; |
| 17 | uint32_t crc32; |
| 18 | }; |
| 19 | |
| 20 | struct xz_dec { |
| 21 | /* Position in dec_main() */ |
| 22 | enum { |
| 23 | SEQ_STREAM_HEADER, |
| 24 | SEQ_BLOCK_START, |
| 25 | SEQ_BLOCK_HEADER, |
| 26 | SEQ_BLOCK_UNCOMPRESS, |
| 27 | SEQ_BLOCK_PADDING, |
| 28 | SEQ_BLOCK_CHECK, |
| 29 | SEQ_INDEX, |
| 30 | SEQ_INDEX_PADDING, |
| 31 | SEQ_INDEX_CRC32, |
| 32 | SEQ_STREAM_FOOTER |
| 33 | } sequence; |
| 34 | |
| 35 | /* Position in variable-length integers and Check fields */ |
| 36 | uint32_t pos; |
| 37 | |
| 38 | /* Variable-length integer decoded by dec_vli() */ |
| 39 | vli_type vli; |
| 40 | |
| 41 | /* Saved in_pos and out_pos */ |
| 42 | size_t in_start; |
| 43 | size_t out_start; |
| 44 | |
| 45 | /* CRC32 value in Block or Index */ |
| 46 | uint32_t crc32; |
| 47 | |
| 48 | /* True if CRC32 is calculated from uncompressed data */ |
| 49 | uint8_t crc_type; |
| 50 | |
| 51 | /* True if we are operating in single-call mode. */ |
| 52 | bool single_call; |
| 53 | |
| 54 | /* |
| 55 | * True if the next call to xz_dec_run() is allowed to return |
| 56 | * XZ_BUF_ERROR. |
| 57 | */ |
| 58 | bool allow_buf_error; |
| 59 | |
| 60 | /* Information stored in Block Header */ |
| 61 | struct { |
| 62 | /* |
| 63 | * Value stored in the Compressed Size field, or |
| 64 | * VLI_UNKNOWN if Compressed Size is not present. |
| 65 | */ |
| 66 | vli_type compressed; |
| 67 | |
| 68 | /* |
| 69 | * Value stored in the Uncompressed Size field, or |
| 70 | * VLI_UNKNOWN if Uncompressed Size is not present. |
| 71 | */ |
| 72 | vli_type uncompressed; |
| 73 | |
| 74 | /* Size of the Block Header field */ |
| 75 | uint32_t size; |
| 76 | } block_header; |
| 77 | |
| 78 | /* Information collected when decoding Blocks */ |
| 79 | struct { |
| 80 | /* Observed compressed size of the current Block */ |
| 81 | vli_type compressed; |
| 82 | |
| 83 | /* Observed uncompressed size of the current Block */ |
| 84 | vli_type uncompressed; |
| 85 | |
| 86 | /* Number of Blocks decoded so far */ |
| 87 | vli_type count; |
| 88 | |
| 89 | /* |
| 90 | * Hash calculated from the Block sizes. This is used to |
| 91 | * validate the Index field. |
| 92 | */ |
| 93 | struct xz_dec_hash hash; |
| 94 | } block; |
| 95 | |
| 96 | /* Variables needed when verifying the Index field */ |
| 97 | struct { |
| 98 | /* Position in dec_index() */ |
| 99 | enum { |
| 100 | SEQ_INDEX_COUNT, |
| 101 | SEQ_INDEX_UNPADDED, |
| 102 | SEQ_INDEX_UNCOMPRESSED |
| 103 | } sequence; |
| 104 | |
| 105 | /* Size of the Index in bytes */ |
| 106 | vli_type size; |
| 107 | |
| 108 | /* Number of Records (matches block.count in valid files) */ |
| 109 | vli_type count; |
| 110 | |
| 111 | /* |
| 112 | * Hash calculated from the Records (matches block.hash in |
| 113 | * valid files). |
| 114 | */ |
| 115 | struct xz_dec_hash hash; |
| 116 | } index; |
| 117 | |
| 118 | /* |
| 119 | * Temporary buffer needed to hold Stream Header, Block Header, |
| 120 | * and Stream Footer. The Block Header is the biggest (1 KiB) |
| 121 | * so we reserve space according to that. buf[] has to be aligned |
| 122 | * to a multiple of four bytes; the size_t variables before it |
| 123 | * should guarantee this. |
| 124 | */ |
| 125 | struct { |
| 126 | size_t pos; |
| 127 | size_t size; |
| 128 | uint8_t buf[1024]; |
| 129 | } temp; |
| 130 | |
| 131 | struct xz_dec_lzma2 *lzma2; |
| 132 | |
| 133 | #ifdef XZ_DEC_BCJ |
| 134 | struct xz_dec_bcj *bcj; |
| 135 | bool bcj_active; |
| 136 | #endif |
| 137 | |
| 138 | uint32_t crc32_table[256]; |
| 139 | }; |
| 140 | |
| 141 | /* |
| 142 | * Fill s->temp by copying data starting from b->in[b->in_pos]. Caller |
| 143 | * must have set s->temp.pos to indicate how much data we are supposed |
| 144 | * to copy into s->temp.buf. Return true once s->temp.pos has reached |
| 145 | * s->temp.size. |
| 146 | */ |
| 147 | static bool XZ_FUNC fill_temp(struct xz_dec *s, struct xz_buf *b) |
| 148 | { |
| 149 | size_t copy_size = min_t(size_t, |
| 150 | b->in_size - b->in_pos, s->temp.size - s->temp.pos); |
| 151 | |
| 152 | memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size); |
| 153 | b->in_pos += copy_size; |
| 154 | s->temp.pos += copy_size; |
| 155 | |
| 156 | if (s->temp.pos == s->temp.size) { |
| 157 | s->temp.pos = 0; |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | /* Decode a variable-length integer (little-endian base-128 encoding) */ |
| 165 | static enum xz_ret XZ_FUNC dec_vli(struct xz_dec *s, |
| 166 | const uint8_t *in, size_t *in_pos, size_t in_size) |
| 167 | { |
| 168 | uint8_t byte; |
| 169 | |
| 170 | if (s->pos == 0) |
| 171 | s->vli = 0; |
| 172 | |
| 173 | while (*in_pos < in_size) { |
| 174 | byte = in[*in_pos]; |
| 175 | ++*in_pos; |
| 176 | |
| 177 | s->vli |= (vli_type)(byte & 0x7F) << s->pos; |
| 178 | |
| 179 | if ((byte & 0x80) == 0) { |
| 180 | /* Don't allow non-minimal encodings. */ |
| 181 | if (byte == 0 && s->pos != 0) |
| 182 | return XZ_DATA_ERROR; |
| 183 | |
| 184 | s->pos = 0; |
| 185 | return XZ_STREAM_END; |
| 186 | } |
| 187 | |
| 188 | s->pos += 7; |
| 189 | if (s->pos == 7 * VLI_BYTES_MAX) |
| 190 | return XZ_DATA_ERROR; |
| 191 | } |
| 192 | |
| 193 | return XZ_OK; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Decode the Compressed Data field from a Block. Update and validate |
| 198 | * the observed compressed and uncompressed sizes of the Block so that |
| 199 | * they don't exceed the values possibly stored in the Block Header |
| 200 | * (validation assumes that no integer overflow occurs, since vli_type |
| 201 | * is normally uint64_t). Update the CRC32 if presence of the CRC32 |
| 202 | * field was indicated in Stream Header. |
| 203 | * |
| 204 | * Once the decoding is finished, validate that the observed sizes match |
| 205 | * the sizes possibly stored in the Block Header. Update the hash and |
| 206 | * Block count, which are later used to validate the Index field. |
| 207 | */ |
| 208 | static enum xz_ret XZ_FUNC dec_block(struct xz_dec *s, struct xz_buf *b) |
| 209 | { |
| 210 | enum xz_ret ret; |
| 211 | |
| 212 | s->in_start = b->in_pos; |
| 213 | s->out_start = b->out_pos; |
| 214 | |
| 215 | #ifdef XZ_DEC_BCJ |
| 216 | if (s->bcj_active) |
| 217 | ret = xz_dec_bcj_run(s->bcj, s->lzma2, b); |
| 218 | else |
| 219 | #endif |
| 220 | ret = xz_dec_lzma2_run(s->lzma2, b); |
| 221 | |
| 222 | s->block.compressed += b->in_pos - s->in_start; |
| 223 | s->block.uncompressed += b->out_pos - s->out_start; |
| 224 | |
| 225 | /* |
| 226 | * There is no need to separately check for VLI_UNKNOWN, since |
| 227 | * the observed sizes are always smaller than VLI_UNKNOWN. |
| 228 | */ |
| 229 | if (s->block.compressed > s->block_header.compressed |
| 230 | || s->block.uncompressed |
| 231 | > s->block_header.uncompressed) |
| 232 | return XZ_DATA_ERROR; |
| 233 | |
| 234 | if (s->crc_type == 0x01) |
| 235 | s->crc32 = xz_crc32(s->crc32_table, |
| 236 | b->out + s->out_start, |
| 237 | b->out_pos - s->out_start, s->crc32); |
| 238 | |
| 239 | if (ret == XZ_STREAM_END) { |
| 240 | if (s->block_header.compressed != VLI_UNKNOWN |
| 241 | && s->block_header.compressed |
| 242 | != s->block.compressed) |
| 243 | return XZ_DATA_ERROR; |
| 244 | |
| 245 | if (s->block_header.uncompressed != VLI_UNKNOWN |
| 246 | && s->block_header.uncompressed |
| 247 | != s->block.uncompressed) |
| 248 | return XZ_DATA_ERROR; |
| 249 | |
| 250 | s->block.hash.unpadded += s->block_header.size |
| 251 | + s->block.compressed; |
| 252 | if (s->crc_type == 0x01) |
| 253 | s->block.hash.unpadded += 4; |
| 254 | if (s->crc_type == 0x04) /* CRC64 */ |
| 255 | s->block.hash.unpadded += 8; |
| 256 | if (s->crc_type == 0x0A) /* SHA-256 */ |
| 257 | s->block.hash.unpadded += 32; |
| 258 | |
| 259 | s->block.hash.uncompressed += s->block.uncompressed; |
| 260 | s->block.hash.crc32 = xz_crc32(s->crc32_table, |
| 261 | (const uint8_t *)&s->block.hash, |
| 262 | sizeof(s->block.hash), s->block.hash.crc32); |
| 263 | |
| 264 | ++s->block.count; |
| 265 | } |
| 266 | |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | /* Update the Index size and the CRC32 value. */ |
| 271 | static void XZ_FUNC index_update(struct xz_dec *s, const struct xz_buf *b) |
| 272 | { |
| 273 | size_t in_used = b->in_pos - s->in_start; |
| 274 | s->index.size += in_used; |
| 275 | s->crc32 = xz_crc32(s->crc32_table, b->in + s->in_start, in_used, s->crc32); |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Decode the Number of Records, Unpadded Size, and Uncompressed Size |
| 280 | * fields from the Index field. That is, Index Padding and CRC32 are not |
| 281 | * decoded by this function. |
| 282 | * |
| 283 | * This can return XZ_OK (more input needed), XZ_STREAM_END (everything |
| 284 | * successfully decoded), or XZ_DATA_ERROR (input is corrupt). |
| 285 | */ |
| 286 | static enum xz_ret XZ_FUNC dec_index(struct xz_dec *s, struct xz_buf *b) |
| 287 | { |
| 288 | enum xz_ret ret; |
| 289 | |
| 290 | do { |
| 291 | ret = dec_vli(s, b->in, &b->in_pos, b->in_size); |
| 292 | if (ret != XZ_STREAM_END) { |
| 293 | index_update(s, b); |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | switch (s->index.sequence) { |
| 298 | case SEQ_INDEX_COUNT: |
| 299 | s->index.count = s->vli; |
| 300 | |
| 301 | /* |
| 302 | * Validate that the Number of Records field |
| 303 | * indicates the same number of Records as |
| 304 | * there were Blocks in the Stream. |
| 305 | */ |
| 306 | if (s->index.count != s->block.count) |
| 307 | return XZ_DATA_ERROR; |
| 308 | |
| 309 | s->index.sequence = SEQ_INDEX_UNPADDED; |
| 310 | break; |
| 311 | |
| 312 | case SEQ_INDEX_UNPADDED: |
| 313 | s->index.hash.unpadded += s->vli; |
| 314 | s->index.sequence = SEQ_INDEX_UNCOMPRESSED; |
| 315 | break; |
| 316 | |
| 317 | case SEQ_INDEX_UNCOMPRESSED: |
| 318 | s->index.hash.uncompressed += s->vli; |
| 319 | s->index.hash.crc32 = xz_crc32(s->crc32_table, |
| 320 | (const uint8_t *)&s->index.hash, |
| 321 | sizeof(s->index.hash), |
| 322 | s->index.hash.crc32); |
| 323 | --s->index.count; |
| 324 | s->index.sequence = SEQ_INDEX_UNPADDED; |
| 325 | break; |
| 326 | } |
| 327 | } while (s->index.count > 0); |
| 328 | |
| 329 | return XZ_STREAM_END; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Validate that the next four input bytes match the value of s->crc32. |
| 334 | * s->pos must be zero when starting to validate the first byte. |
| 335 | */ |
| 336 | static enum xz_ret XZ_FUNC crc32_validate(struct xz_dec *s, struct xz_buf *b) |
| 337 | { |
| 338 | do { |
| 339 | if (b->in_pos == b->in_size) |
| 340 | return XZ_OK; |
| 341 | |
| 342 | if (((s->crc32 >> s->pos) & 0xFF) != b->in[b->in_pos++]) |
| 343 | return XZ_DATA_ERROR; |
| 344 | |
| 345 | s->pos += 8; |
| 346 | |
| 347 | } while (s->pos < 32); |
| 348 | |
| 349 | s->crc32 = 0; |
| 350 | s->pos = 0; |
| 351 | |
| 352 | return XZ_STREAM_END; |
| 353 | } |
| 354 | |
| 355 | /* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */ |
| 356 | static enum xz_ret XZ_FUNC dec_stream_header(struct xz_dec *s) |
| 357 | { |
| 358 | if (!memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE)) |
| 359 | return XZ_FORMAT_ERROR; |
| 360 | |
| 361 | if (xz_crc32(s->crc32_table, s->temp.buf + HEADER_MAGIC_SIZE, 2, 0) |
| 362 | != get_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2)) |
| 363 | return XZ_DATA_ERROR; |
| 364 | |
| 365 | /* |
| 366 | * Decode the Stream Flags field. Of integrity checks, we support |
| 367 | * only none (Check ID = 0) and CRC32 (Check ID = 1). |
| 368 | */ |
| 369 | if (s->temp.buf[HEADER_MAGIC_SIZE] != 0 |
| 370 | || (s->temp.buf[HEADER_MAGIC_SIZE + 1] > 1 |
| 371 | && s->temp.buf[HEADER_MAGIC_SIZE + 1] != 0x04 |
| 372 | && s->temp.buf[HEADER_MAGIC_SIZE + 1] != 0x0A |
| 373 | ) |
| 374 | ) { |
| 375 | XZ_DEBUG_MSG("unsupported stream flags %x:%x", |
| 376 | s->temp.buf[HEADER_MAGIC_SIZE], |
| 377 | s->temp.buf[HEADER_MAGIC_SIZE+1]); |
| 378 | return XZ_OPTIONS_ERROR; |
| 379 | } |
| 380 | |
| 381 | s->crc_type = s->temp.buf[HEADER_MAGIC_SIZE + 1]; |
| 382 | |
| 383 | return XZ_OK; |
| 384 | } |
| 385 | |
| 386 | /* Decode the Stream Footer field (the last 12 bytes of the .xz Stream) */ |
| 387 | static enum xz_ret XZ_FUNC dec_stream_footer(struct xz_dec *s) |
| 388 | { |
| 389 | if (!memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE)) |
| 390 | return XZ_DATA_ERROR; |
| 391 | |
| 392 | if (xz_crc32(s->crc32_table, s->temp.buf + 4, 6, 0) != get_le32(s->temp.buf)) |
| 393 | return XZ_DATA_ERROR; |
| 394 | |
| 395 | /* |
| 396 | * Validate Backward Size. Note that we never added the size of the |
| 397 | * Index CRC32 field to s->index.size, thus we use s->index.size / 4 |
| 398 | * instead of s->index.size / 4 - 1. |
| 399 | */ |
| 400 | if ((s->index.size >> 2) != get_le32(s->temp.buf + 4)) |
| 401 | return XZ_DATA_ERROR; |
| 402 | |
| 403 | if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->crc_type) |
| 404 | return XZ_DATA_ERROR; |
| 405 | |
| 406 | /* |
| 407 | * Use XZ_STREAM_END instead of XZ_OK to be more convenient |
| 408 | * for the caller. |
| 409 | */ |
| 410 | return XZ_STREAM_END; |
| 411 | } |
| 412 | |
| 413 | /* Decode the Block Header and initialize the filter chain. */ |
| 414 | static enum xz_ret XZ_FUNC dec_block_header(struct xz_dec *s) |
| 415 | { |
| 416 | enum xz_ret ret; |
| 417 | |
| 418 | /* |
| 419 | * Validate the CRC32. We know that the temp buffer is at least |
| 420 | * eight bytes so this is safe. |
| 421 | */ |
| 422 | s->temp.size -= 4; |
| 423 | if (xz_crc32(s->crc32_table, s->temp.buf, s->temp.size, 0) |
| 424 | != get_le32(s->temp.buf + s->temp.size)) |
| 425 | return XZ_DATA_ERROR; |
| 426 | |
| 427 | s->temp.pos = 2; |
| 428 | |
| 429 | /* |
| 430 | * Catch unsupported Block Flags. We support only one or two filters |
| 431 | * in the chain, so we catch that with the same test. |
| 432 | */ |
| 433 | #ifdef XZ_DEC_BCJ |
| 434 | if (s->temp.buf[1] & 0x3E) |
| 435 | #else |
| 436 | if (s->temp.buf[1] & 0x3F) |
| 437 | #endif |
| 438 | { |
| 439 | XZ_DEBUG_MSG("s->temp.buf[1] & 0x3E/3F != 0"); |
| 440 | return XZ_OPTIONS_ERROR; |
| 441 | } |
| 442 | |
| 443 | /* Compressed Size */ |
| 444 | if (s->temp.buf[1] & 0x40) { |
| 445 | if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size) |
| 446 | != XZ_STREAM_END) |
| 447 | return XZ_DATA_ERROR; |
| 448 | |
| 449 | s->block_header.compressed = s->vli; |
| 450 | } else { |
| 451 | s->block_header.compressed = VLI_UNKNOWN; |
| 452 | } |
| 453 | |
| 454 | /* Uncompressed Size */ |
| 455 | if (s->temp.buf[1] & 0x80) { |
| 456 | if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size) |
| 457 | != XZ_STREAM_END) |
| 458 | return XZ_DATA_ERROR; |
| 459 | |
| 460 | s->block_header.uncompressed = s->vli; |
| 461 | } else { |
| 462 | s->block_header.uncompressed = VLI_UNKNOWN; |
| 463 | } |
| 464 | |
| 465 | #ifdef XZ_DEC_BCJ |
| 466 | /* If there are two filters, the first one must be a BCJ filter. */ |
| 467 | s->bcj_active = s->temp.buf[1] & 0x01; |
| 468 | if (s->bcj_active) { |
| 469 | if (s->temp.size - s->temp.pos < 2) { |
| 470 | XZ_DEBUG_MSG("temp.size - temp.pos < 2"); |
| 471 | return XZ_OPTIONS_ERROR; |
| 472 | } |
| 473 | |
| 474 | ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]); |
| 475 | if (ret != XZ_OK) |
| 476 | return ret; |
| 477 | |
| 478 | /* |
| 479 | * We don't support custom start offset, |
| 480 | * so Size of Properties must be zero. |
| 481 | */ |
| 482 | if (s->temp.buf[s->temp.pos++] != 0x00) { |
| 483 | XZ_DEBUG_MSG("size of properties != 0"); |
| 484 | return XZ_OPTIONS_ERROR; |
| 485 | } |
| 486 | } |
| 487 | #endif |
| 488 | |
| 489 | /* Valid Filter Flags always take at least two bytes. */ |
| 490 | if (s->temp.size - s->temp.pos < 2) |
| 491 | return XZ_DATA_ERROR; |
| 492 | |
| 493 | /* Filter ID = LZMA2 */ |
| 494 | if (s->temp.buf[s->temp.pos++] != 0x21) { |
| 495 | XZ_DEBUG_MSG("filter ID != 0x21"); |
| 496 | return XZ_OPTIONS_ERROR; |
| 497 | } |
| 498 | |
| 499 | /* Size of Properties = 1-byte Filter Properties */ |
| 500 | if (s->temp.buf[s->temp.pos++] != 0x01) { |
| 501 | XZ_DEBUG_MSG("size of properties != 1"); |
| 502 | return XZ_OPTIONS_ERROR; |
| 503 | } |
| 504 | |
| 505 | /* Filter Properties contains LZMA2 dictionary size. */ |
| 506 | if (s->temp.size - s->temp.pos < 1) |
| 507 | return XZ_DATA_ERROR; |
| 508 | |
| 509 | ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]); |
| 510 | if (ret != XZ_OK) |
| 511 | return ret; |
| 512 | |
| 513 | /* The rest must be Header Padding. */ |
| 514 | while (s->temp.pos < s->temp.size) |
| 515 | if (s->temp.buf[s->temp.pos++] != 0x00) { |
| 516 | XZ_DEBUG_MSG("padding is not zero-filled"); |
| 517 | return XZ_OPTIONS_ERROR; |
| 518 | } |
| 519 | |
| 520 | s->temp.pos = 0; |
| 521 | s->block.compressed = 0; |
| 522 | s->block.uncompressed = 0; |
| 523 | |
| 524 | return XZ_OK; |
| 525 | } |
| 526 | |
| 527 | static enum xz_ret XZ_FUNC dec_main(struct xz_dec *s, struct xz_buf *b) |
| 528 | { |
| 529 | enum xz_ret ret; |
| 530 | |
| 531 | /* |
| 532 | * Store the start position for the case when we are in the middle |
| 533 | * of the Index field. |
| 534 | */ |
| 535 | s->in_start = b->in_pos; |
| 536 | |
| 537 | while (true) { |
| 538 | switch (s->sequence) { |
| 539 | case SEQ_STREAM_HEADER: |
| 540 | /* |
| 541 | * Stream Header is copied to s->temp, and then |
| 542 | * decoded from there. This way if the caller |
| 543 | * gives us only little input at a time, we can |
| 544 | * still keep the Stream Header decoding code |
| 545 | * simple. Similar approach is used in many places |
| 546 | * in this file. |
| 547 | */ |
| 548 | if (!fill_temp(s, b)) |
| 549 | return XZ_OK; |
| 550 | |
| 551 | ret = dec_stream_header(s); |
| 552 | if (ret != XZ_OK) |
| 553 | return ret; |
| 554 | |
| 555 | s->sequence = SEQ_BLOCK_START; |
| 556 | |
| 557 | case SEQ_BLOCK_START: |
| 558 | /* We need one byte of input to continue. */ |
| 559 | if (b->in_pos == b->in_size) |
| 560 | return XZ_OK; |
| 561 | |
| 562 | /* See if this is the beginning of the Index field. */ |
| 563 | if (b->in[b->in_pos] == 0) { |
| 564 | s->in_start = b->in_pos++; |
| 565 | s->sequence = SEQ_INDEX; |
| 566 | break; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Calculate the size of the Block Header and |
| 571 | * prepare to decode it. |
| 572 | */ |
| 573 | s->block_header.size |
| 574 | = ((uint32_t)b->in[b->in_pos] + 1) * 4; |
| 575 | |
| 576 | s->temp.size = s->block_header.size; |
| 577 | s->temp.pos = 0; |
| 578 | s->sequence = SEQ_BLOCK_HEADER; |
| 579 | |
| 580 | case SEQ_BLOCK_HEADER: |
| 581 | if (!fill_temp(s, b)) |
| 582 | return XZ_OK; |
| 583 | |
| 584 | ret = dec_block_header(s); |
| 585 | if (ret != XZ_OK) |
| 586 | return ret; |
| 587 | |
| 588 | s->sequence = SEQ_BLOCK_UNCOMPRESS; |
| 589 | |
| 590 | case SEQ_BLOCK_UNCOMPRESS: |
| 591 | ret = dec_block(s, b); |
| 592 | if (ret != XZ_STREAM_END) |
| 593 | return ret; |
| 594 | |
| 595 | s->sequence = SEQ_BLOCK_PADDING; |
| 596 | |
| 597 | case SEQ_BLOCK_PADDING: |
| 598 | /* |
| 599 | * Size of Compressed Data + Block Padding |
| 600 | * must be a multiple of four. We don't need |
| 601 | * s->block.compressed for anything else |
| 602 | * anymore, so we use it here to test the size |
| 603 | * of the Block Padding field. |
| 604 | */ |
| 605 | while (s->block.compressed & 3) { |
| 606 | if (b->in_pos == b->in_size) |
| 607 | return XZ_OK; |
| 608 | |
| 609 | if (b->in[b->in_pos++] != 0) |
| 610 | return XZ_DATA_ERROR; |
| 611 | |
| 612 | ++s->block.compressed; |
| 613 | } |
| 614 | |
| 615 | s->sequence = SEQ_BLOCK_CHECK; |
| 616 | |
| 617 | case SEQ_BLOCK_CHECK: |
| 618 | if (s->crc_type == 0x01) { |
| 619 | ret = crc32_validate(s, b); |
| 620 | if (ret != XZ_STREAM_END) |
| 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | s->sequence = SEQ_BLOCK_START; |
| 625 | break; |
| 626 | |
| 627 | case SEQ_INDEX: |
| 628 | ret = dec_index(s, b); |
| 629 | if (ret != XZ_STREAM_END) |
| 630 | return ret; |
| 631 | |
| 632 | s->sequence = SEQ_INDEX_PADDING; |
| 633 | |
| 634 | case SEQ_INDEX_PADDING: |
| 635 | while ((s->index.size + (b->in_pos - s->in_start)) |
| 636 | & 3) { |
| 637 | if (b->in_pos == b->in_size) { |
| 638 | index_update(s, b); |
| 639 | return XZ_OK; |
| 640 | } |
| 641 | |
| 642 | if (b->in[b->in_pos++] != 0) |
| 643 | return XZ_DATA_ERROR; |
| 644 | } |
| 645 | |
| 646 | /* Finish the CRC32 value and Index size. */ |
| 647 | index_update(s, b); |
| 648 | |
| 649 | /* Compare the hashes to validate the Index field. */ |
| 650 | if (!memeq(&s->block.hash, &s->index.hash, |
| 651 | sizeof(s->block.hash))) |
| 652 | return XZ_DATA_ERROR; |
| 653 | |
| 654 | s->sequence = SEQ_INDEX_CRC32; |
| 655 | |
| 656 | case SEQ_INDEX_CRC32: |
| 657 | ret = crc32_validate(s, b); |
| 658 | if (ret != XZ_STREAM_END) |
| 659 | return ret; |
| 660 | |
| 661 | s->temp.size = STREAM_HEADER_SIZE; |
| 662 | s->sequence = SEQ_STREAM_FOOTER; |
| 663 | |
| 664 | case SEQ_STREAM_FOOTER: |
| 665 | if (!fill_temp(s, b)) |
| 666 | return XZ_OK; |
| 667 | |
| 668 | return dec_stream_footer(s); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /* Never reached */ |
| 673 | } |
| 674 | |
| 675 | /* |
| 676 | * xz_dec_run() is a wrapper for dec_main() to handle some special cases in |
| 677 | * multi-call and single-call decoding. |
| 678 | * |
| 679 | * In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we |
| 680 | * are not going to make any progress anymore. This is to prevent the caller |
| 681 | * from calling us infinitely when the input file is truncated or otherwise |
| 682 | * corrupt. Since zlib-style API allows that the caller fills the input buffer |
| 683 | * only when the decoder doesn't produce any new output, we have to be careful |
| 684 | * to avoid returning XZ_BUF_ERROR too easily: XZ_BUF_ERROR is returned only |
| 685 | * after the second consecutive call to xz_dec_run() that makes no progress. |
| 686 | * |
| 687 | * In single-call mode, if we couldn't decode everything and no error |
| 688 | * occurred, either the input is truncated or the output buffer is too small. |
| 689 | * Since we know that the last input byte never produces any output, we know |
| 690 | * that if all the input was consumed and decoding wasn't finished, the file |
| 691 | * must be corrupt. Otherwise the output buffer has to be too small or the |
| 692 | * file is corrupt in a way that decoding it produces too big output. |
| 693 | * |
| 694 | * If single-call decoding fails, we reset b->in_pos and b->out_pos back to |
| 695 | * their original values. This is because with some filter chains there won't |
| 696 | * be any valid uncompressed data in the output buffer unless the decoding |
| 697 | * actually succeeds (that's the price to pay of using the output buffer as |
| 698 | * the workspace). |
| 699 | */ |
| 700 | XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_run(struct xz_dec *s, struct xz_buf *b) |
| 701 | { |
| 702 | size_t in_start; |
| 703 | size_t out_start; |
| 704 | enum xz_ret ret; |
| 705 | |
| 706 | if (s->single_call) |
| 707 | xz_dec_reset(s); |
| 708 | |
| 709 | in_start = b->in_pos; |
| 710 | out_start = b->out_pos; |
| 711 | ret = dec_main(s, b); |
| 712 | |
| 713 | if (s->single_call) { |
| 714 | if (ret == XZ_OK) |
| 715 | ret = b->in_pos == b->in_size |
| 716 | ? XZ_DATA_ERROR : XZ_BUF_ERROR; |
| 717 | |
| 718 | if (ret != XZ_STREAM_END) { |
| 719 | b->in_pos = in_start; |
| 720 | b->out_pos = out_start; |
| 721 | } |
| 722 | |
| 723 | } else if (ret == XZ_OK && in_start == b->in_pos |
| 724 | && out_start == b->out_pos) { |
| 725 | if (s->allow_buf_error) |
| 726 | ret = XZ_BUF_ERROR; |
| 727 | |
| 728 | s->allow_buf_error = true; |
| 729 | } else { |
| 730 | s->allow_buf_error = false; |
| 731 | } |
| 732 | |
| 733 | return ret; |
| 734 | } |
| 735 | |
| 736 | XZ_EXTERN struct xz_dec * XZ_FUNC xz_dec_init(uint32_t dict_max) |
| 737 | { |
| 738 | struct xz_dec *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 739 | if (s == NULL) |
| 740 | return NULL; |
| 741 | |
| 742 | s->single_call = dict_max == 0; |
| 743 | |
| 744 | #ifdef XZ_DEC_BCJ |
| 745 | s->bcj = xz_dec_bcj_create(s->single_call); |
| 746 | if (s->bcj == NULL) |
| 747 | goto error_bcj; |
| 748 | #endif |
| 749 | |
| 750 | s->lzma2 = xz_dec_lzma2_create(dict_max); |
| 751 | if (s->lzma2 == NULL) |
| 752 | goto error_lzma2; |
| 753 | |
| 754 | xz_dec_reset(s); |
| 755 | return s; |
| 756 | |
| 757 | error_lzma2: |
| 758 | #ifdef XZ_DEC_BCJ |
| 759 | xz_dec_bcj_end(s->bcj); |
| 760 | error_bcj: |
| 761 | #endif |
| 762 | kfree(s); |
| 763 | return NULL; |
| 764 | } |
| 765 | |
| 766 | XZ_EXTERN void XZ_FUNC xz_dec_reset(struct xz_dec *s) |
| 767 | { |
| 768 | s->sequence = SEQ_STREAM_HEADER; |
| 769 | s->allow_buf_error = false; |
| 770 | s->pos = 0; |
| 771 | s->crc32 = 0; |
| 772 | memzero(&s->block, sizeof(s->block)); |
| 773 | memzero(&s->index, sizeof(s->index)); |
| 774 | s->temp.pos = 0; |
| 775 | s->temp.size = STREAM_HEADER_SIZE; |
| 776 | } |
| 777 | |
| 778 | XZ_EXTERN void XZ_FUNC xz_dec_end(struct xz_dec *s) |
| 779 | { |
| 780 | if (s != NULL) { |
| 781 | xz_dec_lzma2_end(s->lzma2); |
| 782 | #ifdef XZ_DEC_BCJ |
| 783 | xz_dec_bcj_end(s->bcj); |
| 784 | #endif |
| 785 | kfree(s); |
| 786 | } |
| 787 | } |