Lines Matching +full:custom +full:- +full:temp

1 // SPDX-License-Identifier: 0BSD
34 /* Position in variable-length integers and Check fields */
37 /* Variable-length integer decoded by dec_vli() */
128 } temp; member
151 * Fill s->temp by copying data starting from b->in[b->in_pos]. Caller
152 * must have set s->temp.pos to indicate how much data we are supposed
153 * to copy into s->temp.buf. Return true once s->temp.pos has reached
154 * s->temp.size.
159 b->in_size - b->in_pos, s->temp.size - s->temp.pos); in fill_temp()
161 memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size); in fill_temp()
162 b->in_pos += copy_size; in fill_temp()
163 s->temp.pos += copy_size; in fill_temp()
165 if (s->temp.pos == s->temp.size) { in fill_temp()
166 s->temp.pos = 0; in fill_temp()
173 /* Decode a variable-length integer (little-endian base-128 encoding) */
179 if (s->pos == 0) in dec_vli()
180 s->vli = 0; in dec_vli()
186 s->vli |= (vli_type)(byte & 0x7F) << s->pos; in dec_vli()
189 /* Don't allow non-minimal encodings. */ in dec_vli()
190 if (byte == 0 && s->pos != 0) in dec_vli()
193 s->pos = 0; in dec_vli()
197 s->pos += 7; in dec_vli()
198 if (s->pos == 7 * VLI_BYTES_MAX) in dec_vli()
221 s->in_start = b->in_pos; in dec_block()
222 s->out_start = b->out_pos; in dec_block()
225 if (s->bcj_active) in dec_block()
226 ret = xz_dec_bcj_run(s->bcj, s->lzma2, b); in dec_block()
229 ret = xz_dec_lzma2_run(s->lzma2, b); in dec_block()
231 s->block.compressed += b->in_pos - s->in_start; in dec_block()
232 s->block.uncompressed += b->out_pos - s->out_start; in dec_block()
238 if (s->block.compressed > s->block_header.compressed in dec_block()
239 || s->block.uncompressed in dec_block()
240 > s->block_header.uncompressed) in dec_block()
243 if (s->check_type == XZ_CHECK_CRC32) in dec_block()
244 s->crc32 = xz_crc32(b->out + s->out_start, in dec_block()
245 b->out_pos - s->out_start, s->crc32); in dec_block()
248 if (s->block_header.compressed != VLI_UNKNOWN in dec_block()
249 && s->block_header.compressed in dec_block()
250 != s->block.compressed) in dec_block()
253 if (s->block_header.uncompressed != VLI_UNKNOWN in dec_block()
254 && s->block_header.uncompressed in dec_block()
255 != s->block.uncompressed) in dec_block()
258 s->block.hash.unpadded += s->block_header.size in dec_block()
259 + s->block.compressed; in dec_block()
262 s->block.hash.unpadded += check_sizes[s->check_type]; in dec_block()
264 if (s->check_type == XZ_CHECK_CRC32) in dec_block()
265 s->block.hash.unpadded += 4; in dec_block()
268 s->block.hash.uncompressed += s->block.uncompressed; in dec_block()
269 s->block.hash.crc32 = xz_crc32( in dec_block()
270 (const uint8_t *)&s->block.hash, in dec_block()
271 sizeof(s->block.hash), s->block.hash.crc32); in dec_block()
273 ++s->block.count; in dec_block()
282 size_t in_used = b->in_pos - s->in_start; in index_update()
283 s->index.size += in_used; in index_update()
284 s->crc32 = xz_crc32(b->in + s->in_start, in_used, s->crc32); in index_update()
300 ret = dec_vli(s, b->in, &b->in_pos, b->in_size); in dec_index()
306 switch (s->index.sequence) { in dec_index()
308 s->index.count = s->vli; in dec_index()
315 if (s->index.count != s->block.count) in dec_index()
318 s->index.sequence = SEQ_INDEX_UNPADDED; in dec_index()
322 s->index.hash.unpadded += s->vli; in dec_index()
323 s->index.sequence = SEQ_INDEX_UNCOMPRESSED; in dec_index()
327 s->index.hash.uncompressed += s->vli; in dec_index()
328 s->index.hash.crc32 = xz_crc32( in dec_index()
329 (const uint8_t *)&s->index.hash, in dec_index()
330 sizeof(s->index.hash), in dec_index()
331 s->index.hash.crc32); in dec_index()
332 --s->index.count; in dec_index()
333 s->index.sequence = SEQ_INDEX_UNPADDED; in dec_index()
336 } while (s->index.count > 0); in dec_index()
342 * Validate that the next four input bytes match the value of s->crc32.
343 * s->pos must be zero when starting to validate the first byte.
348 if (b->in_pos == b->in_size) in crc32_validate()
351 if (((s->crc32 >> s->pos) & 0xFF) != b->in[b->in_pos++]) in crc32_validate()
354 s->pos += 8; in crc32_validate()
356 } while (s->pos < 32); in crc32_validate()
358 s->crc32 = 0; in crc32_validate()
359 s->pos = 0; in crc32_validate()
371 while (s->pos < check_sizes[s->check_type]) { in check_skip()
372 if (b->in_pos == b->in_size) in check_skip()
375 ++b->in_pos; in check_skip()
376 ++s->pos; in check_skip()
379 s->pos = 0; in check_skip()
388 if (!memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE)) in dec_stream_header()
391 if (xz_crc32(s->temp.buf + HEADER_MAGIC_SIZE, 2, 0) in dec_stream_header()
392 != get_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2)) in dec_stream_header()
395 if (s->temp.buf[HEADER_MAGIC_SIZE] != 0) in dec_stream_header()
404 if (s->temp.buf[HEADER_MAGIC_SIZE + 1] > XZ_CHECK_MAX) in dec_stream_header()
407 s->check_type = s->temp.buf[HEADER_MAGIC_SIZE + 1]; in dec_stream_header()
410 if (s->check_type > XZ_CHECK_CRC32) in dec_stream_header()
413 if (s->check_type > XZ_CHECK_CRC32) in dec_stream_header()
423 if (!memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE)) in dec_stream_footer()
426 if (xz_crc32(s->temp.buf + 4, 6, 0) != get_le32(s->temp.buf)) in dec_stream_footer()
431 * Index CRC32 field to s->index.size, thus we use s->index.size / 4 in dec_stream_footer()
432 * instead of s->index.size / 4 - 1. in dec_stream_footer()
434 if ((s->index.size >> 2) != get_le32(s->temp.buf + 4)) in dec_stream_footer()
437 if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->check_type) in dec_stream_footer()
453 * Validate the CRC32. We know that the temp buffer is at least in dec_block_header()
456 s->temp.size -= 4; in dec_block_header()
457 if (xz_crc32(s->temp.buf, s->temp.size, 0) in dec_block_header()
458 != get_le32(s->temp.buf + s->temp.size)) in dec_block_header()
461 s->temp.pos = 2; in dec_block_header()
468 if (s->temp.buf[1] & 0x3E) in dec_block_header()
470 if (s->temp.buf[1] & 0x3F) in dec_block_header()
475 if (s->temp.buf[1] & 0x40) { in dec_block_header()
476 if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size) in dec_block_header()
480 s->block_header.compressed = s->vli; in dec_block_header()
482 s->block_header.compressed = VLI_UNKNOWN; in dec_block_header()
486 if (s->temp.buf[1] & 0x80) { in dec_block_header()
487 if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size) in dec_block_header()
491 s->block_header.uncompressed = s->vli; in dec_block_header()
493 s->block_header.uncompressed = VLI_UNKNOWN; in dec_block_header()
498 s->bcj_active = s->temp.buf[1] & 0x01; in dec_block_header()
499 if (s->bcj_active) { in dec_block_header()
500 if (s->temp.size - s->temp.pos < 2) in dec_block_header()
503 ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]); in dec_block_header()
508 * We don't support custom start offset, in dec_block_header()
511 if (s->temp.buf[s->temp.pos++] != 0x00) in dec_block_header()
517 if (s->temp.size - s->temp.pos < 2) in dec_block_header()
521 if (s->temp.buf[s->temp.pos++] != 0x21) in dec_block_header()
524 /* Size of Properties = 1-byte Filter Properties */ in dec_block_header()
525 if (s->temp.buf[s->temp.pos++] != 0x01) in dec_block_header()
529 if (s->temp.size - s->temp.pos < 1) in dec_block_header()
532 ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]); in dec_block_header()
537 while (s->temp.pos < s->temp.size) in dec_block_header()
538 if (s->temp.buf[s->temp.pos++] != 0x00) in dec_block_header()
541 s->temp.pos = 0; in dec_block_header()
542 s->block.compressed = 0; in dec_block_header()
543 s->block.uncompressed = 0; in dec_block_header()
556 s->in_start = b->in_pos; in dec_main()
559 switch (s->sequence) { in dec_main()
562 * Stream Header is copied to s->temp, and then in dec_main()
575 * to continue decoding if working in multi-call in dec_main()
576 * mode. Thus, update s->sequence before calling in dec_main()
579 s->sequence = SEQ_BLOCK_START; in dec_main()
589 if (b->in_pos == b->in_size) in dec_main()
593 if (b->in[b->in_pos] == 0) { in dec_main()
594 s->in_start = b->in_pos++; in dec_main()
595 s->sequence = SEQ_INDEX; in dec_main()
603 s->block_header.size in dec_main()
604 = ((uint32_t)b->in[b->in_pos] + 1) * 4; in dec_main()
606 s->temp.size = s->block_header.size; in dec_main()
607 s->temp.pos = 0; in dec_main()
608 s->sequence = SEQ_BLOCK_HEADER; in dec_main()
620 s->sequence = SEQ_BLOCK_UNCOMPRESS; in dec_main()
629 s->sequence = SEQ_BLOCK_PADDING; in dec_main()
637 * s->block.compressed for anything else in dec_main()
641 while (s->block.compressed & 3) { in dec_main()
642 if (b->in_pos == b->in_size) in dec_main()
645 if (b->in[b->in_pos++] != 0) in dec_main()
648 ++s->block.compressed; in dec_main()
651 s->sequence = SEQ_BLOCK_CHECK; in dec_main()
656 if (s->check_type == XZ_CHECK_CRC32) { in dec_main()
667 s->sequence = SEQ_BLOCK_START; in dec_main()
675 s->sequence = SEQ_INDEX_PADDING; in dec_main()
680 while ((s->index.size + (b->in_pos - s->in_start)) in dec_main()
682 if (b->in_pos == b->in_size) { in dec_main()
687 if (b->in[b->in_pos++] != 0) in dec_main()
695 if (!memeq(&s->block.hash, &s->index.hash, in dec_main()
696 sizeof(s->block.hash))) in dec_main()
699 s->sequence = SEQ_INDEX_CRC32; in dec_main()
708 s->temp.size = STREAM_HEADER_SIZE; in dec_main()
709 s->sequence = SEQ_STREAM_FOOTER; in dec_main()
726 * multi-call and single-call decoding.
728 * In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we
731 * corrupt. Since zlib-style API allows that the caller fills the input buffer
736 * In single-call mode, if we couldn't decode everything and no error
743 * If single-call decoding fails, we reset b->in_pos and b->out_pos back to
755 if (DEC_IS_SINGLE(s->mode)) in xz_dec_run()
758 in_start = b->in_pos; in xz_dec_run()
759 out_start = b->out_pos; in xz_dec_run()
762 if (DEC_IS_SINGLE(s->mode)) { in xz_dec_run()
764 ret = b->in_pos == b->in_size in xz_dec_run()
768 b->in_pos = in_start; in xz_dec_run()
769 b->out_pos = out_start; in xz_dec_run()
772 } else if (ret == XZ_OK && in_start == b->in_pos in xz_dec_run()
773 && out_start == b->out_pos) { in xz_dec_run()
774 if (s->allow_buf_error) in xz_dec_run()
777 s->allow_buf_error = true; in xz_dec_run()
779 s->allow_buf_error = false; in xz_dec_run()
791 s->mode = mode; in xz_dec_init()
794 s->bcj = xz_dec_bcj_create(DEC_IS_SINGLE(mode)); in xz_dec_init()
795 if (s->bcj == NULL) in xz_dec_init()
799 s->lzma2 = xz_dec_lzma2_create(mode, dict_max); in xz_dec_init()
800 if (s->lzma2 == NULL) in xz_dec_init()
808 xz_dec_bcj_end(s->bcj); in xz_dec_init()
817 s->sequence = SEQ_STREAM_HEADER; in xz_dec_reset()
818 s->allow_buf_error = false; in xz_dec_reset()
819 s->pos = 0; in xz_dec_reset()
820 s->crc32 = 0; in xz_dec_reset()
821 memzero(&s->block, sizeof(s->block)); in xz_dec_reset()
822 memzero(&s->index, sizeof(s->index)); in xz_dec_reset()
823 s->temp.pos = 0; in xz_dec_reset()
824 s->temp.size = STREAM_HEADER_SIZE; in xz_dec_reset()
830 xz_dec_lzma2_end(s->lzma2); in xz_dec_end()
832 xz_dec_bcj_end(s->bcj); in xz_dec_end()