2012-05-28 19:42:11 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file test_bcj_exact_size.c
|
|
|
|
/// \brief Tests BCJ decoding when the output size is known
|
|
|
|
///
|
|
|
|
/// These tests fail with XZ Utils 5.0.3 and earlier.
|
|
|
|
//
|
|
|
|
// Author: Lasse Collin
|
|
|
|
//
|
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "tests.h"
|
|
|
|
|
|
|
|
|
2022-06-02 19:28:23 +02:00
|
|
|
static void
|
|
|
|
test_exact_size(void)
|
|
|
|
{
|
2022-10-05 17:54:12 +02:00
|
|
|
#if !defined(HAVE_ENCODERS) || !defined(HAVE_DECODERS)
|
|
|
|
assert_skip("Encoder or decoder support disabled");
|
|
|
|
#else
|
|
|
|
if (!lzma_filter_encoder_is_supported(LZMA_FILTER_POWERPC)
|
|
|
|
|| !lzma_filter_decoder_is_supported(
|
|
|
|
LZMA_FILTER_POWERPC))
|
|
|
|
assert_skip("PowerPC BCJ encoder and/or decoder "
|
|
|
|
"is disabled");
|
|
|
|
|
2022-06-02 19:28:23 +02:00
|
|
|
// Something to be compressed
|
|
|
|
const uint8_t in[16] = "0123456789ABCDEF";
|
2012-05-28 19:42:11 +02:00
|
|
|
|
2022-06-02 19:28:23 +02:00
|
|
|
// in[] after compression
|
|
|
|
uint8_t compressed[1024];
|
|
|
|
size_t compressed_size = 0;
|
2012-05-28 19:42:11 +02:00
|
|
|
|
2022-06-02 19:28:23 +02:00
|
|
|
// Output buffer for decompressing compressed[]
|
|
|
|
uint8_t out[sizeof(in)];
|
2012-05-28 19:42:11 +02:00
|
|
|
|
|
|
|
// Compress with PowerPC BCJ and LZMA2. PowerPC BCJ is used because
|
|
|
|
// it has fixed 4-byte alignment which makes triggering the potential
|
|
|
|
// bug easy.
|
|
|
|
lzma_options_lzma opt_lzma2;
|
2022-06-02 19:28:23 +02:00
|
|
|
assert_false(lzma_lzma_preset(&opt_lzma2, 0));
|
2012-05-28 19:42:11 +02:00
|
|
|
|
|
|
|
lzma_filter filters[3] = {
|
|
|
|
{ .id = LZMA_FILTER_POWERPC, .options = NULL },
|
|
|
|
{ .id = LZMA_FILTER_LZMA2, .options = &opt_lzma2 },
|
|
|
|
{ .id = LZMA_VLI_UNKNOWN, .options = NULL },
|
|
|
|
};
|
|
|
|
|
2022-06-02 19:28:23 +02:00
|
|
|
assert_lzma_ret(lzma_stream_buffer_encode(
|
|
|
|
filters, LZMA_CHECK_CRC32, NULL,
|
2012-05-28 19:42:11 +02:00
|
|
|
in, sizeof(in),
|
2022-06-02 19:28:23 +02:00
|
|
|
compressed, &compressed_size, sizeof(compressed)),
|
|
|
|
LZMA_OK);
|
2012-05-28 19:42:11 +02:00
|
|
|
|
2022-06-02 19:28:23 +02:00
|
|
|
// Decompress so that we won't give more output space than
|
|
|
|
// the Stream will need.
|
2012-05-28 19:42:11 +02:00
|
|
|
lzma_stream strm = LZMA_STREAM_INIT;
|
2022-06-02 19:28:23 +02:00
|
|
|
assert_lzma_ret(lzma_stream_decoder(&strm, 10 << 20, 0), LZMA_OK);
|
2012-05-28 19:42:11 +02:00
|
|
|
|
|
|
|
strm.next_in = compressed;
|
|
|
|
strm.next_out = out;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (strm.total_in < compressed_size)
|
|
|
|
strm.avail_in = 1;
|
|
|
|
|
|
|
|
const lzma_ret ret = lzma_code(&strm, LZMA_RUN);
|
|
|
|
if (ret == LZMA_STREAM_END) {
|
2022-06-02 19:28:23 +02:00
|
|
|
assert_uint_eq(strm.total_in, compressed_size);
|
|
|
|
assert_uint_eq(strm.total_out, sizeof(in));
|
2015-05-13 19:57:55 +02:00
|
|
|
lzma_end(&strm);
|
2012-05-28 19:42:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-02 19:28:23 +02:00
|
|
|
assert_lzma_ret(ret, LZMA_OK);
|
2012-05-28 19:42:11 +02:00
|
|
|
|
|
|
|
if (strm.total_out < sizeof(in))
|
|
|
|
strm.avail_out = 1;
|
|
|
|
}
|
2022-10-05 17:54:12 +02:00
|
|
|
#endif
|
2012-05-28 19:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2022-06-02 19:28:23 +02:00
|
|
|
test_empty_block(void)
|
2012-05-28 19:42:11 +02:00
|
|
|
{
|
2022-10-05 17:54:12 +02:00
|
|
|
#ifndef HAVE_DECODERS
|
|
|
|
assert_skip("Decoder support disabled");
|
|
|
|
#else
|
2012-05-28 19:42:11 +02:00
|
|
|
// An empty file with one Block using PowerPC BCJ and LZMA2.
|
2022-06-14 20:29:21 +02:00
|
|
|
size_t in_size;
|
2022-06-16 12:29:59 +02:00
|
|
|
uint8_t *empty_bcj_lzma2 = tuktest_file_from_srcdir(
|
2022-06-14 20:29:21 +02:00
|
|
|
"files/good-1-empty-bcj-lzma2.xz", &in_size);
|
2012-05-28 19:42:11 +02:00
|
|
|
|
|
|
|
// Decompress without giving any output space.
|
|
|
|
uint64_t memlimit = 1 << 20;
|
2022-06-02 19:28:23 +02:00
|
|
|
uint8_t out[1];
|
2012-05-28 19:42:11 +02:00
|
|
|
size_t in_pos = 0;
|
|
|
|
size_t out_pos = 0;
|
2022-06-02 19:28:23 +02:00
|
|
|
assert_lzma_ret(lzma_stream_buffer_decode(&memlimit, 0, NULL,
|
2022-06-14 20:29:21 +02:00
|
|
|
empty_bcj_lzma2, &in_pos, in_size, out, &out_pos, 0),
|
2022-06-02 19:28:23 +02:00
|
|
|
LZMA_OK);
|
2022-06-14 20:29:21 +02:00
|
|
|
assert_uint_eq(in_pos, in_size);
|
2022-06-02 19:28:23 +02:00
|
|
|
assert_uint_eq(out_pos, 0);
|
2022-10-05 17:54:12 +02:00
|
|
|
#endif
|
2012-05-28 19:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern int
|
2022-06-02 19:28:23 +02:00
|
|
|
main(int argc, char **argv)
|
2012-05-28 19:42:11 +02:00
|
|
|
{
|
2022-06-02 19:28:23 +02:00
|
|
|
tuktest_start(argc, argv);
|
|
|
|
|
|
|
|
tuktest_run(test_exact_size);
|
|
|
|
tuktest_run(test_empty_block);
|
|
|
|
|
|
|
|
return tuktest_end();
|
2012-05-28 19:42:11 +02:00
|
|
|
}
|