#![crate_name = "rpgcpf"]
#![warn(clippy::pedantic, missing_docs)]
#![cfg_attr(test, feature(coverage_attribute))]
#![cfg_attr(test, feature(assert_matches))]
pub mod compression_mode;
pub mod compressor;
pub mod decompressor;
pub mod error;
pub mod gcpf;
pub use crate::{error::*, gcpf::*};
#[cfg(test)]
mod test {
use std::assert_matches::assert_matches;
#[allow(unused_imports)]
use pretty_assertions::{assert_eq, assert_ne};
use rstest::rstest;
pub use crate::{
compression_mode::CompressionMode, compressor::Compressor, decompressor::Decompressor,
error::Error, gcpf::Gcpf,
};
#[rstest]
#[cfg_attr(not(feature = "brotli"), ignore)]
fn test_brotli_compression() {
let data = b"here's some test data";
let expected = [
0x47, 0x43, 0x50, 0x46, 0x4, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x15, 0x0, 0x0, 0x0,
0x19, 0x0, 0x0, 0x0, 0xb, 0xa, 0x80, 0x68, 0x65, 0x72, 0x65, 0x27, 0x73, 0x20, 0x73,
0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x64, 0x61, 0x74, 0x61, 0x3,
0x47, 0x43, 0x50, 0x46,
];
let actual = Gcpf::compress(data, CompressionMode::Brotli).unwrap();
let dec = Gcpf::read(&actual).unwrap().decompress().unwrap();
assert_eq!(dec, data);
assert_eq!(actual, expected);
}
#[rstest]
#[cfg_attr(not(feature = "brotli"), ignore)]
fn test_brotli_decompression() {
let data = [
0x47, 0x43, 0x50, 0x46, 0x4, 0x0, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x15, 0x0, 0x0, 0x0,
0x19, 0x0, 0x0, 0x0, 0xb, 0xa, 0x80, 0x68, 0x65, 0x72, 0x65, 0x27, 0x73, 0x20, 0x73,
0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x64, 0x61, 0x74, 0x61, 0x3,
0x47, 0x43, 0x50, 0x46,
];
let expected = b"here's some test data";
let gcpf = Gcpf::read(&data).unwrap();
let actual = gcpf.decompress().unwrap();
assert_eq!(actual, expected);
}
#[rstest]
#[cfg_attr(not(feature = "deflate"), ignore)]
fn test_deflate_compression() {
let data = b"here's some test data";
let expected = [
0x47, 0x43, 0x50, 0x46, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x15, 0x00,
0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0xcb, 0x48, 0x2d, 0x4a, 0x55, 0x2f, 0x56, 0x28,
0xce, 0xcf, 0x4d, 0x55, 0x28, 0x49, 0x2d, 0x2e, 0x51, 0x48, 0x49, 0x2c, 0x49, 0x04,
0x00, 0x47, 0x43, 0x50, 0x46,
];
let actual = Gcpf::compress(data, CompressionMode::Deflate).unwrap();
let dec = Gcpf::read(&actual).unwrap().decompress().unwrap();
assert_eq!(dec, data);
assert_eq!(actual, expected);
}
#[rstest]
#[cfg_attr(not(feature = "deflate"), ignore)]
fn test_deflate_decompression() {
let data = [
0x47, 0x43, 0x50, 0x46, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x15, 0x00,
0x00, 0x00, 0x17, 0x00, 0x00, 0x00, 0xcb, 0x48, 0x2d, 0x4a, 0x55, 0x2f, 0x56, 0x28,
0xce, 0xcf, 0x4d, 0x55, 0x28, 0x49, 0x2d, 0x2e, 0x51, 0x48, 0x49, 0x2c, 0x49, 0x04,
0x00, 0x47, 0x43, 0x50, 0x46,
];
let expected = b"here's some test data";
let gcpf = Gcpf::read(&data).unwrap();
let actual = gcpf.decompress().unwrap();
assert_eq!(actual, expected);
}
#[rstest]
#[cfg_attr(not(feature = "fastlz"), ignore)]
fn test_fastlz_compression() {
let data = b"here's some test data";
let expected = [
0x47, 0x43, 0x50, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x15, 0x00,
0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x14, 0x68, 0x65, 0x72, 0x65, 0x27, 0x73, 0x20,
0x73, 0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x64, 0x61, 0x74, 0x61,
0x47, 0x43, 0x50, 0x46,
];
let actual = Gcpf::compress(data, CompressionMode::FastLZ).unwrap();
let dec = Gcpf::read(&actual).unwrap().decompress().unwrap();
assert_eq!(dec, data);
assert_eq!(actual, expected);
}
#[rstest]
#[cfg_attr(not(feature = "fastlz"), ignore)]
fn test_fastlz_decompression() {
let data = [
0x47, 0x43, 0x50, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x15, 0x00,
0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x14, 0x68, 0x65, 0x72, 0x65, 0x27, 0x73, 0x20,
0x73, 0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x64, 0x61, 0x74, 0x61,
0x47, 0x43, 0x50, 0x46,
];
let expected = b"here's some test data";
let gcpf = Gcpf::read(&data).unwrap();
let actual = gcpf.decompress().unwrap();
assert_eq!(actual, expected);
}
#[rstest]
#[cfg_attr(not(feature = "gzip"), ignore)]
fn test_gzip_compression() {
let data = b"here's some test data";
let expected = [
0x47, 0x43, 0x50, 0x46, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x15, 0x00,
0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xff, 0xcb, 0x48, 0x2d, 0x4a, 0x55, 0x2f, 0x56, 0x28, 0xce, 0xcf, 0x4d, 0x55,
0x28, 0x49, 0x2d, 0x2e, 0x51, 0x48, 0x49, 0x2c, 0x49, 0x04, 0x00, 0xc7, 0x8a, 0xef,
0x2e, 0x15, 0x00, 0x00, 0x00, 0x47, 0x43, 0x50, 0x46,
];
let actual = Gcpf::compress(data, CompressionMode::Gzip).unwrap();
let dec = Gcpf::read(&actual).unwrap().decompress().unwrap();
assert_eq!(dec, data);
assert_eq!(actual, expected);
}
#[rstest]
#[cfg_attr(not(feature = "gzip"), ignore)]
fn test_gzip_decompression() {
let data = [
0x47, 0x43, 0x50, 0x46, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x15, 0x00,
0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x13, 0xcb, 0x48, 0x2d, 0x4a, 0x55, 0x2f, 0x56, 0x28, 0xce, 0xcf, 0x4d, 0x55,
0x28, 0x49, 0x2d, 0x2e, 0x51, 0x48, 0x49, 0x2c, 0x49, 0x04, 0x00, 0xc7, 0x8a, 0xef,
0x2e, 0x15, 0x00, 0x00, 0x00, 0x47, 0x43, 0x50, 0x46,
];
let expected = b"here's some test data";
let gcpf = Gcpf::read(&data).unwrap();
let actual = gcpf.decompress().unwrap();
assert_eq!(actual, expected);
}
#[rstest]
#[cfg_attr(not(feature = "zstd"), ignore)]
fn test_zstd_compression() {
let data = b"here's some test data";
let expected = [
0x47, 0x43, 0x50, 0x46, 0x02, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x15, 0x00,
0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x28, 0xb5, 0x2f, 0xfd, 0x00, 0x58, 0xa9, 0x00,
0x00, 0x68, 0x65, 0x72, 0x65, 0x27, 0x73, 0x20, 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x74,
0x65, 0x73, 0x74, 0x20, 0x64, 0x61, 0x74, 0x61, 0x47, 0x43, 0x50, 0x46,
];
let actual = Gcpf::compress(data, CompressionMode::Zstd).unwrap();
let dec = Gcpf::read(&actual).unwrap().decompress().unwrap();
assert_eq!(dec, data);
assert_eq!(actual, expected);
}
#[rstest]
#[cfg_attr(not(feature = "zstd"), ignore)]
fn test_zstd_decompression() {
let data = [
0x47, 0x43, 0x50, 0x46, 0x02, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x15, 0x00,
0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x28, 0xb5, 0x2f, 0xfd, 0x20, 0x15, 0xa9, 0x00,
0x00, 0x68, 0x65, 0x72, 0x65, 0x27, 0x73, 0x20, 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x74,
0x65, 0x73, 0x74, 0x20, 0x64, 0x61, 0x74, 0x61, 0x47, 0x43, 0x50, 0x46,
];
let expected = b"here's some test data";
let gcpf = Gcpf::read(&data).unwrap();
let actual = gcpf.decompress().unwrap();
assert_eq!(actual, expected);
}
#[rstest]
fn test_gcpf_truncated_min_length() {
let bad_data = b"GCPF";
let result = Gcpf::read(bad_data);
assert_matches!(result, Err(Error::GcpfTruncated));
}
#[rstest]
fn test_bad_magic() {
let bad_data = b"this is some very very bad data";
let result = Gcpf::read(bad_data);
assert_matches!(result, Err(Error::BadMagic([b't', b'h', b'i', b's'])));
}
#[rstest]
fn test_truncated_blocks() {
let bad_data = [
b'G', b'C', b'P', b'F', 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, b'G', b'C', b'P', b'F',
];
let result = Gcpf::read(&bad_data);
assert_matches!(result, Err(Error::GcpfTruncated));
}
#[rstest]
fn test_no_footer() {
let bad_data = [
b'G', b'C', b'P', b'F', 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
];
let result = Gcpf::read(&bad_data);
assert_matches!(result, Err(Error::GcpfTruncated));
}
#[rstest]
fn test_bad_footer() {
let bad_data = [
b'G', b'C', b'P', b'F', 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
let result = Gcpf::read(&bad_data);
assert_matches!(result, Err(Error::BadMagic([0, 0, 0, 0])));
}
#[rstest]
fn test_multiblock_compression_deflate() {
test_multiblock_compression(CompressionMode::Deflate);
}
#[rstest]
fn test_multiblock_compression_zstd() {
test_multiblock_compression(CompressionMode::Zstd);
}
#[rstest]
fn test_multiblock_compression_gzip() {
test_multiblock_compression(CompressionMode::Gzip);
}
#[rstest]
fn test_multiblock_compression_brotli() {
test_multiblock_compression(CompressionMode::Brotli);
}
#[rstest]
fn test_multiblock_compression_fastlz() {
test_multiblock_compression(CompressionMode::FastLZ);
}
fn test_multiblock_compression(mode: CompressionMode) {
let expected = b"this is test data";
let actual =
Gcpf::compress_settings(expected, mode, 10, mode.get_compressor().unwrap().as_ref())
.unwrap();
let dec = Gcpf::read(&actual).unwrap().decompress().unwrap();
assert_ne!(actual, expected);
assert_eq!(dec, expected);
}
}