From f4cd7abdd38aa3d14dcce98cb92dc1054fe97503 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 9 Mar 2021 12:58:34 +0200 Subject: Add support for compressing/decompressing fdstreams with LZ4 Importing LZ4 code from version 1.9.3. --- tests/lz4/buildfile | 6 +++ tests/lz4/driver.cxx | 47 +++++++++++++++++++++++ tests/lz4/testscript | 78 +++++++++++++++++++++++++++++++++++++++ tests/lz4/truncated-content.lz4 | Bin 0 -> 108 bytes tests/lz4/truncated-header12.lz4 | Bin 0 -> 12 bytes tests/lz4/truncated-header6.lz4 | 1 + 6 files changed, 132 insertions(+) create mode 100644 tests/lz4/buildfile create mode 100644 tests/lz4/driver.cxx create mode 100644 tests/lz4/testscript create mode 100644 tests/lz4/truncated-content.lz4 create mode 100644 tests/lz4/truncated-header12.lz4 create mode 100644 tests/lz4/truncated-header6.lz4 (limited to 'tests') diff --git a/tests/lz4/buildfile b/tests/lz4/buildfile new file mode 100644 index 0000000..1f9a244 --- /dev/null +++ b/tests/lz4/buildfile @@ -0,0 +1,6 @@ +# file : tests/lz4/buildfile +# license : MIT; see accompanying LICENSE file + +import libs = libbutl%lib{butl} + +exe{driver}: {hxx cxx}{*} $libs testscript file{*.lz4} diff --git a/tests/lz4/driver.cxx b/tests/lz4/driver.cxx new file mode 100644 index 0000000..e3da5e6 --- /dev/null +++ b/tests/lz4/driver.cxx @@ -0,0 +1,47 @@ +// file : tests/lz4/driver.cxx -*- C++ -*- +// license : MIT; see accompanying LICENSE file + +#include +#include + +#include +#include +#include // entry_stat, path_entry() + +using namespace std; +using namespace butl; + +// Usage: argv[0] [-c|-d] +// +int +main (int argc, const char* argv[]) +try +{ + assert (argc == 4); + + ifdstream ifs (argv[2], fdopen_mode::binary, ifdstream::badbit); + ofdstream ofs (argv[3], fdopen_mode::binary); + + if (argv[1][1] == 'c') + { + // @@ TODO: would be nice to get it from fd. + // + entry_stat st (path_entry (argv[2], true /* follow_symlinks */).second); + + lz4::compress (ofs, ifs, + 1 /* compression_level */, + 4 /* block_size_id (64KB) */, + st.size); + } + else + { + lz4::decompress (ofs, ifs); + } + + ofs.close (); +} +catch (const std::exception& e) +{ + cerr << e.what () << endl; + return 1; +} diff --git a/tests/lz4/testscript b/tests/lz4/testscript new file mode 100644 index 0000000..1806c94 --- /dev/null +++ b/tests/lz4/testscript @@ -0,0 +1,78 @@ +# file : tests/lz4/testscript +# license : MIT; see accompanying LICENSE file + ++touch zero ++echo 'The quick brown fox jumps over the lazy dog.' >=small ++cat <=1kb +The quick brown fox jumps over the lazy dog. The quick brown fox jumps over +the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox +jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The +quick brown fox jumps over the lazy dog. The quick brown fox jumps over the +lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox +jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The +quick brown fox jumps over the lazy dog. The quick brown fox jumps over the +lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox +jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The +quick brown fox jumps over the lazy dog. The quick brown fox jumps over the +lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox +jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The +quick brown fox jumps over the lazy dog. The quick brown fox jumps over the +lazy dog. The quick brown fox jumps over th +EOI ++cat 1kb 1kb 1kb 1kb 1kb 1kb 1kb 1kb >=8kb ++cat 8kb 8kb 8kb 8kb 8kb 8kb 8kb 8kb >=64kb ++cat 64kb 64kb 64kb 64kb 64kb 64kb 64kb 64kb >=512kb + +: rt-zero +: +$* -c ../zero zero.lz4 &zero.lz4; +$* -d zero.lz4 zero &zero; +diff ../zero zero + +: rt-small +: +$* -c ../small small.lz4 &small.lz4; +$* -d small.lz4 small &small; +diff ../small small + +: rt-1kb +: +$* -c ../1kb 1kb.lz4 &1kb.lz4; +$* -d 1kb.lz4 1kb &1kb; +diff ../1kb 1kb + +: rt-8kb +: +$* -c ../8kb 8kb.lz4 &8kb.lz4; +$* -d 8kb.lz4 8kb &8kb; +diff ../8kb 8kb + +: rt-64kb +: +$* -c ../64kb 64kb.lz4 &64kb.lz4; +$* -d 64kb.lz4 64kb &64kb; +diff ../64kb 64kb + +: rt-512kb +: +$* -c ../512kb 512kb.lz4 &512kb.lz4; +$* -d 512kb.lz4 512kb &512kb; +diff ../512kb 512kb + +: truncated-header6 +: +$* -d $src_base/truncated-header6.lz4 out &out 2>>EOE !=0 +incomplete frame header +EOE + +: truncated-header12 +: +$* -d $src_base/truncated-header12.lz4 out &out 2>>EOE !=0 +incomplete frame header +EOE + +: truncated-content +: +$* -d $src_base/truncated-content.lz4 out &out 2>>EOE !=0 +incomplete compressed content +EOE diff --git a/tests/lz4/truncated-content.lz4 b/tests/lz4/truncated-content.lz4 new file mode 100644 index 0000000..2a9a39d Binary files /dev/null and b/tests/lz4/truncated-content.lz4 differ diff --git a/tests/lz4/truncated-header12.lz4 b/tests/lz4/truncated-header12.lz4 new file mode 100644 index 0000000..101088e Binary files /dev/null and b/tests/lz4/truncated-header12.lz4 differ diff --git a/tests/lz4/truncated-header6.lz4 b/tests/lz4/truncated-header6.lz4 new file mode 100644 index 0000000..a00e998 --- /dev/null +++ b/tests/lz4/truncated-header6.lz4 @@ -0,0 +1 @@ +"Ml@ \ No newline at end of file -- cgit v1.1