Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs

This commit is contained in:
2026-09-15 10:41:00 -06:00
parent 6dc77e8eb0
commit a2b65bb47f
16427 changed files with 6972976 additions and 3 deletions
+1
View File
@@ -0,0 +1 @@
{"$comment":"This file only protects against accidental modifications. It is not a security mechanism and does not protect against malicious changes.","files":{".cargo_vcs_info.json":"13715a0156fd95846555c2d9cedc4e4d903cb9f350db07a3b0235eec0454ef3c","CHANGELOG.md":"9fe52bc559c1ece0ee3d296aef691950350a957e12c5ed3fda204148fb5f02df","Cargo.lock":"91ed76b52491271fe4f18b2784cc455deeb1593d89dcc7fb031b67f2a9d4fcc8","Cargo.toml":"b92ba437a0d6b0060fdca0b1dd02f255707c9a7b468fcfe5685083e2799cca90","Cargo.toml.orig":"a51eaf2c1ac9d5667f19126e9bcced762f013e8a600d3830b0a7ef07c5500a66","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"88d1e3160df48926ad3310a8ec5699b502889565908f1be7e77cd21282c7a709","README.md":"5a7f048730d9cadc4d3ba7d4e635a28b4681e8e7bee276d914cc088855fdc4e0","src/brotli/decoder.rs":"9197e72171cdfaed8eea42eb54e19bd179ee69a6fffff0fd941e743343d34264","src/brotli/encoder.rs":"ec3c9c0f60f76d16dd75c462d52d81a211f5a8f722f7d173b9111339b4fb5d5c","src/brotli/mod.rs":"4e9e9130ed7452510545f16300e0796d3891fb92529cc2bc5bd91a5e334ebf1e","src/brotli/params.rs":"4793ac39e4894494992515ebdc8d5fa53962c11c001710f9532e7ac3b684a5c2","src/bzip2/decoder.rs":"8c8619b3d3e4251eb009b3e80e8cc277443c9c52c12721b9966963948e7c9280","src/bzip2/encoder.rs":"8e55acf8d1cef2182d2f6920fa5183b70a8b6f25d6bf46750f8266a08722d875","src/bzip2/mod.rs":"497c77afdfa966f6730d46f944814ef4f09e28c5b994ffbb78ac431cd944c4a4","src/bzip2/params.rs":"0f8c0613f9144ccc2bd8ca57484c7689b8167b2f33c9969110e9e6f66dde4297","src/deflate/decoder.rs":"f9fd860dd8875690dea67e884a3a74fe6073bd854d98111452bea40ee0930bc1","src/deflate/encoder.rs":"1f3ad3ead3d3db90d86a3434b9ce341f287084684791ae7f1280d88ad90cf79a","src/deflate/mod.rs":"14fa2925a0bd651bc6f528b4436e7eeb22187ba1c89409dc506e86ce5ce319ce","src/deflate64/decoder.rs":"6e79e6a7ab9a54763c1d92961e5b4636802d69e1d10923ac6f40a9ddec90d7a7","src/deflate64/mod.rs":"64e39b4c5a148caa36f5dd7dd25126ef0aec393239594985969c21c903b82fd7","src/flate/decoder.rs":"05798713cb1740b5eb65047abb972269b1f66b3c67bdc3aba65384a5dfcda0e0","src/flate/encoder.rs":"d7fdebad5641be9899dd84f75b24092ceac51449e0e6d1f6a3087955de0673a7","src/flate/mod.rs":"fc918bba525e6dcf0ab0d56dbb7077f6366d48f1e6451bca2dd322364df865c4","src/flate/params.rs":"752237105822513583cee3bc475fc877edee4e35a9874bc6af3c41b804bbe553","src/gzip/decoder.rs":"f8b50ec4d1afe5df7367a99f9de4c5b16e083841c8f7c40994362d93f1cd2803","src/gzip/encoder.rs":"8b2ade61fc80500323557f3275134fb5bee3d6ceb04c1c27ea8abe8d754140f9","src/gzip/header.rs":"a19c72576a3504aa12cddd2c80f1995dcf453893a00f25b094a438c443d9a9d8","src/gzip/mod.rs":"7f1f5c24393f8d5b1af9856c021936b545026f741423d29c9728f7eb873cf26c","src/lib.rs":"6c6d5879faf523e0e76472f6306e0e7261e6f0ddb652c617d214043944efa976","src/lz4/decoder.rs":"91c7a25de582956c3283ffc0a8f36a15aa33274610c56f64ccc7d5c9db63676d","src/lz4/encoder.rs":"9557660293bcda76cf8095b587f9f108dc42ceec861845828cf01c393d211019","src/lz4/mod.rs":"b7ddd0698abb4632bbf7f8268ba278549e444e14d5efaea232874a263d9e838e","src/lz4/params.rs":"536cd767f54e42e4a47cfffa5e78ac5641d4aa495d909d13c73e3b06505cb26d","src/lzma/decoder.rs":"a2fe9166b51c473ed97a062f24880c001f71b44179e216f4bb03bd12329563e7","src/lzma/encoder.rs":"eb1261fa35020d524e441de7aefcf9ce2c59edb7ddc1532b2812ac2513cb0342","src/lzma/mod.rs":"c80f3e171c1a0689f9cfbf070599a0fbba70f69566df9809e604c77a6358ff5d","src/lzma/params.rs":"70fa16133a482de36399e521922b60f9d981ff6466eb8daab24b6a8b545844dc","src/xz/decoder.rs":"402d2d271d2a1f16a5369e7e0e7d63c6768559672c2017398060bf7631f7d89f","src/xz/encoder.rs":"46d5de4a2df9491f7efdc07731b68f53d012fed28e0ce7844d3f40cc88c2eb93","src/xz/mod.rs":"a0175eb8f44903cece8b8f6c40946fbaf380bd5b83738f472b63d1e04e0de349","src/xz2/decoder.rs":"1387eebd6faaa669c50006d0202352101fb6dcc39351c512fbe13caef490b65f","src/xz2/encoder.rs":"f1e63a3a37c7c7ab1790df445b9d4030930648eb394339548ab6819648d90269","src/xz2/mod.rs":"ee2b1c8490f9ba2bb0f84f1e8f1becfbef032c6157605a9363dd85528e41eb8a","src/zlib/decoder.rs":"5d301c5c3426868449985695a5f3b417e7911406f1f76c5b30f92c77e135b3ac","src/zlib/encoder.rs":"5a24c2dae804ac5daafa779d5978a52e4a9ea73e5ad57d60b82269edf1b090fa","src/zlib/mod.rs":"433dd472c8c1f718c469fca4550610b9d5214e579c3edc6a6db7b6700efb3cf8","src/zstd/decoder.rs":"3bf69b56f59ccf97fbbf5de4afd1327e11d633521da292c45d4665040a2893d1","src/zstd/encoder.rs":"d2c0cdf5dc8eb24949e0f454529d59fb3c67c7df89089a8afca1f7963368e9b2","src/zstd/mod.rs":"ea487af2a0e26b79bcee118c2c166f35a37272b60ee8bc56ae5cd9ecf761ee2e","src/zstd/params.rs":"30cd7f3db0ecd80d5d3fdd8bc3e2090d2244de9bfaab22b6a827c5ba174a86e7"},"package":"257c7085cbb71be72d8fb97edff08b03d86d5d9f2222b9cc34a6bec87093bc10"}
+6
View File
@@ -0,0 +1,6 @@
{
"git": {
"sha1": "d34d64e4762d69e394c255f16aea71ed6e08f439"
},
"path_in_vcs": "crates/compression-codecs"
}
+121
View File
@@ -0,0 +1,121 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
## [0.4.42](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.41...compression-codecs-v0.4.42) - 2026-09-14
### Fixed
- *(zstd)* avoid no-progress errors while awaiting input ([#486](https://github.com/Nullus157/async-compression/pull/486))
## [0.4.41](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.40...compression-codecs-v0.4.41) - 2026-09-09
### Other
- Return UnexpectedEof when deflate64 does not progress ([#483](https://github.com/Nullus157/async-compression/pull/483))
## [0.4.40](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.39...compression-codecs-v0.4.40) - 2026-09-07
### Other
- *(deps)* update zstd requirement from 0.13 to 0.14 ([#481](https://github.com/Nullus157/async-compression/pull/481))
## [0.4.39](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.38...compression-codecs-v0.4.39) - 2026-09-02
### Other
- updated the following local packages: compression-core
## [0.4.38](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.37...compression-codecs-v0.4.38) - 2026-04-21
### Other
- Include license files in published crates ([#467](https://github.com/Nullus157/async-compression/pull/467))
## [0.4.37](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.36...compression-codecs-v0.4.37) - 2026-02-18
### Other
- Stop consuming input on errors in codecs. ([#451](https://github.com/Nullus157/async-compression/pull/451))
## [0.4.36](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.35...compression-codecs-v0.4.36) - 2026-01-10
### Other
- Set async-compression msrv ([#440](https://github.com/Nullus157/async-compression/pull/440))
## [0.4.35](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.34...compression-codecs-v0.4.35) - 2025-12-11
### Other
- Add Crc checksum validation for gzip::header::Parser ([#432](https://github.com/Nullus157/async-compression/pull/432))
## [0.4.34](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.33...compression-codecs-v0.4.34) - 2025-12-07
### Other
- Optimize GzipEncoder to not allocate for header and footer ([#431](https://github.com/Nullus157/async-compression/pull/431))
- Optimize GzipDecoder ([#430](https://github.com/Nullus157/async-compression/pull/430))
- Optimize gzip::header remove heap allocation ([#428](https://github.com/Nullus157/async-compression/pull/428))
## [0.4.33](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.32...compression-codecs-v0.4.33) - 2025-11-21
### Added
- Add uninitialized output buffer support ([#414](https://github.com/Nullus157/async-compression/pull/414))
### Other
- Set msrv for codecs to 1.83 ([#422](https://github.com/Nullus157/async-compression/pull/422))
## [0.4.32](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.31...compression-codecs-v0.4.32) - 2025-11-06
### Added
- allow reading uncompressed size ([#396](https://github.com/Nullus157/async-compression/pull/396))
### Fixed
- `UnexpectedEof` on truncated input ([#412](https://github.com/Nullus157/async-compression/pull/412))
### Other
- Simplify `WriteBuffer`: Rm `unwritten_initialized_mut` ([#413](https://github.com/Nullus157/async-compression/pull/413))
- Implement new traits `DecodeV2`/`EncodeV2` ([#398](https://github.com/Nullus157/async-compression/pull/398))
- Re-export core in codecs ([#395](https://github.com/Nullus157/async-compression/pull/395))
- Disable nightly feature `doc_auto_cfg` on docsrs ([#392](https://github.com/Nullus157/async-compression/pull/392))
## [0.4.31](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.30...compression-codecs-v0.4.31) - 2025-09-25
### Other
- Use io::ErrorKind::OutOfMemory for xz2/bzip2 codecs ([#387](https://github.com/Nullus157/async-compression/pull/387))
## [0.4.30](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.29...compression-codecs-v0.4.30) - 2025-08-31
### Other
- rm unused dep from async-compression and compression-codecs ([#381](https://github.com/Nullus157/async-compression/pull/381))
## [0.4.29](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.28...compression-codecs-v0.4.29) - 2025-08-28
### Other
- Update Deps.rs badge ([#380](https://github.com/Nullus157/async-compression/pull/380))
- move async-compression to crates/ ([#379](https://github.com/Nullus157/async-compression/pull/379))
## [0.4.28](https://github.com/Nullus157/async-compression/compare/compression-codecs-v0.4.27...compression-codecs-v0.4.28) - 2025-08-23
### Fixed
- fix wasi ci testing and update doc in README ([#367](https://github.com/Nullus157/async-compression/pull/367))
### Other
- Have separate package.version field for compression-* ([#369](https://github.com/Nullus157/async-compression/pull/369))
- Fix docs.rs build for compression-codecs ([#365](https://github.com/Nullus157/async-compression/pull/365))
+289
View File
@@ -0,0 +1,289 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "alloc-no-stdlib"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc7bb162ec39d46ab1ca8c77bf72e890535becd1751bb45f64c597edb4c8c6b3"
[[package]]
name = "alloc-stdlib"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e76a019e91224d279006ff972f1e984179a6e9feb050adba6ce8274aef23195"
dependencies = [
"alloc-no-stdlib",
]
[[package]]
name = "brotli"
version = "8.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cc91aac060a7a1e25823bdccbfb6af1875b88f17c6daac97894eed8207166b3"
dependencies = [
"alloc-no-stdlib",
"alloc-stdlib",
"brotli-decompressor",
]
[[package]]
name = "brotli-decompressor"
version = "5.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a32acac15fe1967bc3986b2a6347dffc965602354ea6f450ad07e8bfd253583"
dependencies = [
"alloc-no-stdlib",
"alloc-stdlib",
]
[[package]]
name = "bzip2"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3a53fac24f34a81bc9954b5d6cfce0c21e18ec6959f44f56e8e90e4bb7c346c"
dependencies = [
"libbz2-rs-sys",
]
[[package]]
name = "cc"
version = "1.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3eb0f42d6c360dc3f8a821f6bf2fdea7f72bfd36b3076eb0e6d1e9e0752fff4"
dependencies = [
"find-msvc-tools",
"jobserver",
"libc",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "compression-codecs"
version = "0.4.42"
dependencies = [
"brotli",
"bzip2",
"compression-core",
"deflate64",
"flate2",
"liblzma",
"lz4",
"memchr",
"zstd",
"zstd-safe",
]
[[package]]
name = "compression-core"
version = "0.4.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e8ccc4ea9f6acc32d102c0f6d471d11d913ad15f20c04de743374861fa1d414"
[[package]]
name = "crc32fast"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01a7799fd6b852db0e61728dde9a204c423b44d689dbd432522543614b490e78"
dependencies = [
"cfg-if",
]
[[package]]
name = "deflate64"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac6b926516df9c60bfa16e107b21086399f8285a44ca9711344b9e553c5146e2"
[[package]]
name = "find-msvc-tools"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e0f1c7c3a72c66fd80abe965175f7523475c0489a87d3ff9d6e8c87d87a9d2d"
[[package]]
name = "flate2"
version = "1.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb"
dependencies = [
"crc32fast",
"miniz_oxide",
"zlib-rs",
]
[[package]]
name = "getrandom"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
dependencies = [
"cfg-if",
"libc",
"r-efi",
]
[[package]]
name = "hermit-abi"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284"
[[package]]
name = "jobserver"
version = "0.1.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3"
dependencies = [
"getrandom",
"libc",
]
[[package]]
name = "libbz2-rs-sys"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34b357333733e8260735ba5894eb928c02ecc69c78715f01a8019e7fa7f2db4c"
[[package]]
name = "libc"
version = "0.2.189"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"
[[package]]
name = "liblzma"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fe0a34ca854fd4f20c07f696fc8675aec78f87d88d29f5e10257a7490a1b2e1"
dependencies = [
"liblzma-sys",
"num_cpus",
]
[[package]]
name = "liblzma-sys"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c7e3581f367a78d7b7e7ae948d023310556f0cfc13156c2e4e00e25616492b9"
dependencies = [
"cc",
"libc",
"pkg-config",
]
[[package]]
name = "lz4"
version = "1.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a20b523e860d03443e98350ceaac5e71c6ba89aea7d960769ec3ce37f4de5af4"
dependencies = [
"lz4-sys",
]
[[package]]
name = "lz4-sys"
version = "1.11.1+lz4-1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bd8c0d6c6ed0cd30b3652886bb8711dc4bb01d637a68105a3d5158039b418e6"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "memchr"
version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
[[package]]
name = "miniz_oxide"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c"
dependencies = [
"adler2",
"simd-adler32",
]
[[package]]
name = "num_cpus"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "pkg-config"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548"
[[package]]
name = "r-efi"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "shlex"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
[[package]]
name = "simd-adler32"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"
[[package]]
name = "zlib-rs"
version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34b31d188d9d685a4f9c7b46d6e36631b07058d2cfe190267adce54dc230bf12"
[[package]]
name = "zstd"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf06bd8162af0734b344780deb55b42a2429ae430870d13fcc12f238e880fe6e"
dependencies = [
"zstd-safe",
]
[[package]]
name = "zstd-safe"
version = "8.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae42c0555055784c70058d19ba8e275528e8a99a706684868ace5da4e716a4ab"
dependencies = [
"zstd-sys",
]
[[package]]
name = "zstd-sys"
version = "2.1.0+zstd.1.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef0a8027ec3ee71300ab3bcbcd0393f434aa72b91ca6d635a39941deae8eea0"
dependencies = [
"cc",
"pkg-config",
]
+131
View File
@@ -0,0 +1,131 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2018"
rust-version = "1.83"
name = "compression-codecs"
version = "0.4.42"
authors = [
"Wim Looman <wim@nemo157.com>",
"Allen Bui <fairingrey@gmail.com>",
]
build = false
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = """
Adaptors for various compression algorithms.
"""
readme = "README.md"
categories = [
"compression",
"asynchronous",
]
license = "MIT OR Apache-2.0"
repository = "https://github.com/Nullus157/async-compression"
resolver = "2"
[package.metadata.docs.rs]
all-features = true
[features]
all-algorithms = [
"brotli",
"bzip2",
"deflate",
"gzip",
"lz4",
"lzma",
"xz-parallel",
"xz",
"zlib",
"zstd",
"deflate64",
]
deflate = ["flate2"]
deflate64 = ["dep:deflate64"]
gzip = [
"flate2",
"memchr",
]
lz4 = ["dep:lz4"]
lzma = ["dep:liblzma"]
xz = ["lzma"]
xz-parallel = [
"xz",
"liblzma/parallel",
]
xz2 = ["xz"]
zlib = ["flate2"]
zstd = [
"libzstd",
"zstd-safe",
]
zstdmt = [
"zstd",
"zstd-safe/zstdmt",
]
[lib]
name = "compression_codecs"
path = "src/lib.rs"
[dependencies.brotli]
version = "8"
optional = true
[dependencies.bzip2]
version = "0.6.1"
optional = true
[dependencies.compression-core]
version = "0.4.33"
[dependencies.deflate64]
version = "0.1.10"
optional = true
[dependencies.flate2]
version = "1.1.4"
optional = true
[dependencies.liblzma]
version = "0.4.5"
optional = true
[dependencies.libzstd]
version = "0.14.0"
optional = true
default-features = false
package = "zstd"
[dependencies.lz4]
version = "1.28.1"
optional = true
[dependencies.memchr]
version = "2"
optional = true
[dependencies.zstd-safe]
version = "8"
optional = true
default-features = false
[lints.rust]
rust_2018_idioms = "deny"
[lints.rust.missing_debug_implementations]
level = "deny"
priority = -1
+61
View File
@@ -0,0 +1,61 @@
[package]
name = "compression-codecs"
version = "0.4.42"
description = """
Adaptors for various compression algorithms.
"""
authors.workspace = true
categories.workspace = true
edition.workspace = true
license.workspace = true
readme = "../../README.md"
repository.workspace = true
rust-version.workspace = true
[package.metadata.docs.rs]
all-features = true
[features]
all-algorithms = [
"brotli",
"bzip2",
"deflate",
"gzip",
"lz4",
"lzma",
"xz-parallel",
"xz",
"zlib",
"zstd",
"deflate64",
]
# algorithms
deflate = ["flate2"]
gzip = ["flate2", "memchr"]
lz4 = ["dep:lz4"]
lzma = ["dep:liblzma"]
xz = ["lzma"]
xz-parallel = ["xz", "liblzma/parallel"]
xz2 = ["xz"]
zlib = ["flate2"]
zstd = ["libzstd", "zstd-safe"]
zstdmt = ["zstd", "zstd-safe/zstdmt"]
deflate64 = ["dep:deflate64"]
[dependencies]
# Workspace dependencies.
compression-core.workspace = true
# features
brotli = { version = "8", optional = true }
bzip2 = { version = "0.6.1", optional = true }
deflate64 = { version = "0.1.10", optional = true }
flate2 = { version = "1.1.4", optional = true }
libzstd = { package = "zstd", version = "0.14.0", optional = true, default-features = false }
lz4 = { version = "1.28.1", optional = true }
liblzma = { version = "0.4.5", optional = true }
memchr = { version = "2", optional = true }
zstd-safe = { version = "8", optional = true, default-features = false }
[lints]
workspace = true
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2018 the rustasync developers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+88
View File
@@ -0,0 +1,88 @@
# async-compression
[![crates.io version][1]][2] ![build status][3]
[![downloads][5]][6] [![docs.rs docs][7]][8]
![MIT or Apache 2.0 licensed][9]
[![dependency status][10]][11]
This crate provides adaptors between compression crates and Rust's modern
asynchronous IO types.
- [Documentation][8]
- [Crates.io][2]
- [Releases][releases]
## Development
When developing you will need to enable appropriate features for the different
test cases to run, the simplest is `cargo test --all-features`, but you can
enable different subsets of features as appropriate for the code you are
testing to avoid compiling all dependencies, e.g. `cargo test --features
tokio,gzip`.
To prepare for a pull request, you can run several other checks:
1. `fmt`
```bash
cargo fmt --all
cargo clippy --no-deps
```
2. `build`
```bash
cargo build --lib --all-features
```
3. `nextest`
```bash
cargo --locked nextest run --workspace --all-features
```
4. `hack check`
```bash
cargo hack check --workspace --feature-powerset --all-targets --skip 'all,all-algorithms,all-implementations'
```
5. `wasm32` - Linux only
```bash
gh release download --repo WebAssembly/wasi-sdk --pattern 'wasi-sysroot-*.tar.gz'
mkdir -p wasi-sysroot
tar xf wasi-sysroot-*.tar.gz --strip-components=1 -C wasi-sysroot
rustup target add wasm32-wasip1-threads
export "CFLAGS_wasm32_wasip1_threads=--sysroot=\"${PWD}/wasi-sysroot\" -I\"${PWD}/wasi-sysroot/include/wasm32-wasip1-threads\" -L-I\"${PWD}/wasi-sysroot/lib/wasm32-wasip1-threads\""
cargo build --lib --features all-implementations,brotli,bzip2,deflate,gzip,lz4,lzma,xz,zlib,zstd,deflate64 --target wasm32-wasip1-threads
```
## License
Licensed under either of
- [Apache License, Version 2.0](LICENSE-APACHE)
- [MIT license](LICENSE-MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you shall be dual licensed as above, without any
additional terms or conditions.
[1]: https://img.shields.io/crates/v/async-compression.svg?style=flat-square
[2]: https://crates.io/crates/async-compression
[3]: https://img.shields.io/github/actions/workflow/status/Nullus157/async-compression/base.yml?style=flat-square
[5]: https://img.shields.io/crates/d/async-compression.svg?style=flat-square
[6]: https://crates.io/crates/async-compression
[7]: https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square
[8]: https://docs.rs/async-compression
[9]: https://img.shields.io/crates/l/async-compression.svg?style=flat-square
[10]: https://deps.rs/crate/async-compression/latest/status.svg?style=flat-square
[11]: https://deps.rs/crate/async-compression
[releases]: https://github.com/Nullus157/async-compression/releases
+111
View File
@@ -0,0 +1,111 @@
use crate::DecodeV2;
use brotli::{enc::StandardAlloc, BrotliDecompressStream, BrotliResult};
use compression_core::util::{PartialBuffer, WriteBuffer};
use std::{fmt, io};
type BrotliState = brotli::BrotliState<StandardAlloc, StandardAlloc, StandardAlloc>;
pub struct BrotliDecoder {
// `BrotliState` is very large (over 2kb) which is why we're boxing it.
state: Box<BrotliState>,
}
impl Default for BrotliDecoder {
fn default() -> Self {
Self {
state: Box::new(Self::new_brotli_state()),
}
}
}
impl BrotliDecoder {
fn new_brotli_state() -> BrotliState {
BrotliState::new(
StandardAlloc::default(),
StandardAlloc::default(),
StandardAlloc::default(),
)
}
pub fn new() -> Self {
Self::default()
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<BrotliResult> {
let in_buf = input.unwritten();
let out_buf = output.initialize_unwritten();
let mut input_len = 0;
let mut output_len = 0;
let status = match BrotliDecompressStream(
&mut in_buf.len(),
&mut input_len,
in_buf,
&mut out_buf.len(),
&mut output_len,
out_buf,
&mut 0,
&mut self.state,
) {
BrotliResult::ResultFailure => return Err(io::Error::other("brotli error")),
status => status,
};
input.advance(input_len);
output.advance(output_len);
Ok(status)
}
}
impl DecodeV2 for BrotliDecoder {
fn reinit(&mut self) -> io::Result<()> {
*self.state = Self::new_brotli_state();
Ok(())
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<bool> {
match self.decode(input, output)? {
BrotliResult::ResultSuccess => Ok(true),
BrotliResult::NeedsMoreOutput | BrotliResult::NeedsMoreInput => Ok(false),
BrotliResult::ResultFailure => unreachable!(),
}
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
match self.decode(&mut PartialBuffer::new(&[][..]), output)? {
BrotliResult::ResultSuccess | BrotliResult::NeedsMoreInput => Ok(true),
BrotliResult::NeedsMoreOutput => Ok(false),
BrotliResult::ResultFailure => unreachable!(),
}
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
match self.decode(&mut PartialBuffer::new(&[][..]), output)? {
BrotliResult::ResultSuccess => Ok(true),
BrotliResult::NeedsMoreOutput => Ok(false),
BrotliResult::NeedsMoreInput => Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"reached unexpected EOF",
)),
BrotliResult::ResultFailure => unreachable!(),
}
}
}
impl fmt::Debug for BrotliDecoder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BrotliDecoder")
.field("decompress", &"<no debug>")
.finish()
}
}
+95
View File
@@ -0,0 +1,95 @@
use crate::{brotli::params::EncoderParams, EncodeV2};
use brotli::enc::{
backward_references::BrotliEncoderParams,
encode::{BrotliEncoderOperation, BrotliEncoderStateStruct},
StandardAlloc,
};
use compression_core::util::{PartialBuffer, WriteBuffer};
use std::{fmt, io};
pub struct BrotliEncoder {
state: BrotliEncoderStateStruct<StandardAlloc>,
}
impl BrotliEncoder {
pub fn new(params: EncoderParams) -> Self {
let params = BrotliEncoderParams::from(params);
let mut state = BrotliEncoderStateStruct::new(StandardAlloc::default());
state.params = params;
Self { state }
}
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
op: BrotliEncoderOperation,
) -> io::Result<()> {
let in_buf = input.unwritten();
let out_buf = output.initialize_unwritten();
let mut input_len = 0;
let mut output_len = 0;
if !self.state.compress_stream(
op,
&mut in_buf.len(),
in_buf,
&mut input_len,
&mut out_buf.len(),
out_buf,
&mut output_len,
&mut None,
&mut |_, _, _, _| (),
) {
return Err(io::Error::other("brotli error"));
}
input.advance(input_len);
output.advance(output_len);
Ok(())
}
}
impl EncodeV2 for BrotliEncoder {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<()> {
self.encode(
input,
output,
BrotliEncoderOperation::BROTLI_OPERATION_PROCESS,
)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
self.encode(
&mut PartialBuffer::new(&[][..]),
output,
BrotliEncoderOperation::BROTLI_OPERATION_FLUSH,
)?;
Ok(!self.state.has_more_output())
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
self.encode(
&mut PartialBuffer::new(&[][..]),
output,
BrotliEncoderOperation::BROTLI_OPERATION_FINISH,
)?;
Ok(self.state.is_finished())
}
}
impl fmt::Debug for BrotliEncoder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BrotliEncoder")
.field("compress", &"<no debug>")
.finish()
}
}
+5
View File
@@ -0,0 +1,5 @@
mod decoder;
mod encoder;
pub mod params;
pub use self::{decoder::BrotliDecoder, encoder::BrotliEncoder};
+92
View File
@@ -0,0 +1,92 @@
//! This module contains Brotli-specific types for async-compression.
use brotli::enc::backward_references::{BrotliEncoderMode, BrotliEncoderParams};
use compression_core::Level;
/// Brotli compression parameters builder. This is a stable wrapper around Brotli's own encoder
/// params type, to abstract over different versions of the Brotli library.
///
/// See the [Brotli documentation](https://www.brotli.org/encode.html#a9a8) for more information on
/// these parameters.
///
/// # Examples
///
/// ```
/// use compression_codecs::brotli;
///
/// let params = brotli::params::EncoderParams::default()
/// .window_size(12)
/// .text_mode();
/// ```
#[derive(Debug, Clone, Default)]
pub struct EncoderParams {
inner: BrotliEncoderParams,
}
impl From<EncoderParams> for BrotliEncoderParams {
fn from(value: EncoderParams) -> Self {
value.inner
}
}
impl EncoderParams {
pub fn quality(mut self, level: Level) -> Self {
let quality = match level {
Level::Fastest => Some(0),
Level::Best => Some(11),
Level::Precise(quality) => Some(quality.clamp(0, 11)),
_ => None,
};
match quality {
Some(quality) => self.inner.quality = quality,
None => {
let default_params = BrotliEncoderParams::default();
self.inner.quality = default_params.quality;
}
}
self
}
/// Sets window size in bytes (as a power of two).
///
/// Used as Brotli's `lgwin` parameter.
///
/// `window_size` is clamped to `0 <= window_size <= 24`.
pub fn window_size(mut self, window_size: i32) -> Self {
self.inner.lgwin = window_size.clamp(0, 24);
self
}
/// Sets input block size in bytes (as a power of two).
///
/// Used as Brotli's `lgblock` parameter.
///
/// `block_size` is clamped to `16 <= block_size <= 24`.
pub fn block_size(mut self, block_size: i32) -> Self {
self.inner.lgblock = block_size.clamp(16, 24);
self
}
/// Sets hint for size of data to be compressed.
pub fn size_hint(mut self, size_hint: usize) -> Self {
self.inner.size_hint = size_hint;
self
}
/// Sets encoder to text mode.
///
/// If input data is known to be UTF-8 text, this allows the compressor to make assumptions and
/// optimizations.
///
/// Used as Brotli's `mode` parameter.
pub fn text_mode(mut self) -> Self {
self.inner.mode = BrotliEncoderMode::BROTLI_MODE_TEXT;
self
}
pub fn mode(mut self, mode: BrotliEncoderMode) -> Self {
self.inner.mode = mode;
self
}
}
+123
View File
@@ -0,0 +1,123 @@
use crate::DecodeV2;
use bzip2::{Decompress, Status};
use compression_core::util::{PartialBuffer, WriteBuffer};
use std::{fmt, io};
pub struct BzDecoder {
decompress: Decompress,
stream_ended: bool,
}
impl fmt::Debug for BzDecoder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"BzDecoder {{total_in: {}, total_out: {}}}",
self.decompress.total_in(),
self.decompress.total_out()
)
}
}
impl Default for BzDecoder {
fn default() -> Self {
Self {
decompress: Decompress::new(false),
stream_ended: false,
}
}
}
impl BzDecoder {
pub fn new() -> Self {
Self::default()
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<Status> {
let prior_in = self.decompress.total_in();
let prior_out = self.decompress.total_out();
let status = self
.decompress
// Safety: We **trust** bzip2 to only write initialized data to it
.decompress_uninit(input.unwritten(), unsafe { output.unwritten_mut() })
.map_err(io::Error::other)?;
input.advance((self.decompress.total_in() - prior_in) as usize);
// Safety: We **trust** bzip2 to write bytes properly
unsafe {
output.assume_init_and_advance((self.decompress.total_out() - prior_out) as usize)
};
// Track when stream has properly ended
if status == Status::StreamEnd {
self.stream_ended = true;
}
Ok(status)
}
}
impl DecodeV2 for BzDecoder {
fn reinit(&mut self) -> io::Result<()> {
self.decompress = Decompress::new(false);
self.stream_ended = false;
Ok(())
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<bool> {
match self.decode(input, output)? {
// Decompression went fine, nothing much to report.
Status::Ok => Ok(false),
// The Flush action on a compression went ok.
Status::FlushOk => unreachable!(),
// THe Run action on compression went ok.
Status::RunOk => unreachable!(),
// The Finish action on compression went ok.
Status::FinishOk => unreachable!(),
// The stream's end has been met, meaning that no more data can be input.
Status::StreamEnd => Ok(true),
// There was insufficient memory in the input or output buffer to complete
// the request, but otherwise everything went normally.
Status::MemNeeded => Err(io::ErrorKind::OutOfMemory.into()),
}
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
self.decode(&mut PartialBuffer::new(&[][..]), output)?;
loop {
let old_len = output.written_len();
self.decode(&mut PartialBuffer::new(&[][..]), output)?;
if output.written_len() == old_len {
break;
}
}
Ok(!output.has_no_spare_space())
}
fn finish(&mut self, _output: &mut WriteBuffer<'_>) -> io::Result<bool> {
if self.stream_ended {
Ok(true)
} else {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"bzip2 stream did not finish",
))
}
}
}
+145
View File
@@ -0,0 +1,145 @@
use crate::{bzip2::params::Bzip2EncoderParams, EncodeV2};
use bzip2::{Action, Compress, Compression, Status};
use compression_core::util::{PartialBuffer, WriteBuffer};
use std::{fmt, io};
pub struct BzEncoder {
compress: Compress,
}
impl fmt::Debug for BzEncoder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"BzEncoder {{total_in: {}, total_out: {}}}",
self.compress.total_in(),
self.compress.total_out()
)
}
}
impl BzEncoder {
/// Creates a new stream prepared for compression.
///
/// The `work_factor` parameter controls how the compression phase behaves
/// when presented with worst case, highly repetitive, input data. If
/// compression runs into difficulties caused by repetitive data, the
/// library switches from the standard sorting algorithm to a fallback
/// algorithm. The fallback is slower than the standard algorithm by perhaps
/// a factor of three, but always behaves reasonably, no matter how bad the
/// input.
///
/// Lower values of `work_factor` reduce the amount of effort the standard
/// algorithm will expend before resorting to the fallback. You should set
/// this parameter carefully; too low, and many inputs will be handled by
/// the fallback algorithm and so compress rather slowly, too high, and your
/// average-to-worst case compression times can become very large. The
/// default value of 30 gives reasonable behaviour over a wide range of
/// circumstances.
///
/// Allowable values range from 0 to 250 inclusive. 0 is a special case,
/// equivalent to using the default value of 30.
pub fn new(params: Bzip2EncoderParams, work_factor: u32) -> Self {
let params = Compression::from(params);
Self {
compress: Compress::new(params, work_factor),
}
}
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
action: Action,
) -> io::Result<Status> {
let prior_in = self.compress.total_in();
let prior_out = self.compress.total_out();
let status = self
.compress
// Safety: We **trust** bzip2 to only write initialized bytes into it
.compress_uninit(input.unwritten(), unsafe { output.unwritten_mut() }, action)
.map_err(io::Error::other)?;
input.advance((self.compress.total_in() - prior_in) as usize);
// Safety: We **trust** bzip2 to properly write bytes into it
unsafe { output.assume_init_and_advance((self.compress.total_out() - prior_out) as usize) };
Ok(status)
}
}
impl EncodeV2 for BzEncoder {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<()> {
match self.encode(input, output, Action::Run)? {
// Decompression went fine, nothing much to report.
Status::Ok => Ok(()),
// The Flush action on a compression went ok.
Status::FlushOk => unreachable!(),
// The Run action on compression went ok.
Status::RunOk => Ok(()),
// The Finish action on compression went ok.
Status::FinishOk => unreachable!(),
// The stream's end has been met, meaning that no more data can be input.
Status::StreamEnd => unreachable!(),
// There was insufficient memory in the input or output buffer to complete
// the request, but otherwise everything went normally.
Status::MemNeeded => Err(io::ErrorKind::OutOfMemory.into()),
}
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
match self.encode(&mut PartialBuffer::new(&[][..]), output, Action::Flush)? {
// Decompression went fine, nothing much to report.
Status::Ok => unreachable!(),
// The Flush action on a compression went ok.
Status::FlushOk => Ok(false),
// The Run action on compression went ok.
Status::RunOk => Ok(true),
// The Finish action on compression went ok.
Status::FinishOk => unreachable!(),
// The stream's end has been met, meaning that no more data can be input.
Status::StreamEnd => unreachable!(),
// There was insufficient memory in the input or output buffer to complete
// the request, but otherwise everything went normally.
Status::MemNeeded => Err(io::ErrorKind::OutOfMemory.into()),
}
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
match self.encode(&mut PartialBuffer::new(&[][..]), output, Action::Finish)? {
// Decompression went fine, nothing much to report.
Status::Ok => Ok(false),
// The Flush action on a compression went ok.
Status::FlushOk => unreachable!(),
// The Run action on compression went ok.
Status::RunOk => unreachable!(),
// The Finish action on compression went ok.
Status::FinishOk => Ok(false),
// The stream's end has been met, meaning that no more data can be input.
Status::StreamEnd => Ok(true),
// There was insufficient memory in the input or output buffer to complete
// the request, but otherwise everything went normally.
Status::MemNeeded => Err(io::ErrorKind::OutOfMemory.into()),
}
}
}
+5
View File
@@ -0,0 +1,5 @@
mod decoder;
mod encoder;
pub mod params;
pub use self::{decoder::BzDecoder, encoder::BzEncoder};
+34
View File
@@ -0,0 +1,34 @@
use std::convert::TryInto;
use bzip2::Compression;
use compression_core::Level;
#[derive(Debug)]
pub struct Bzip2EncoderParams {
inner: Compression,
}
impl From<Bzip2EncoderParams> for Compression {
fn from(value: Bzip2EncoderParams) -> Self {
value.inner
}
}
impl From<Level> for Bzip2EncoderParams {
fn from(value: Level) -> Self {
let fastest = bzip2::Compression::fast();
let best = bzip2::Compression::best();
let inner = match value {
Level::Fastest => fastest,
Level::Best => best,
Level::Precise(quality) => bzip2::Compression::new(
quality
.try_into()
.unwrap_or(0)
.clamp(fastest.level(), best.level()),
),
_ => bzip2::Compression::default(),
};
Self { inner }
}
}
+45
View File
@@ -0,0 +1,45 @@
use crate::{DecodeV2, FlateDecoder};
use compression_core::util::{PartialBuffer, WriteBuffer};
use std::io::Result;
#[derive(Debug)]
pub struct DeflateDecoder {
inner: FlateDecoder,
}
impl Default for DeflateDecoder {
fn default() -> Self {
Self {
inner: FlateDecoder::new(false),
}
}
}
impl DeflateDecoder {
pub fn new() -> Self {
Self::default()
}
}
impl DecodeV2 for DeflateDecoder {
fn reinit(&mut self) -> Result<()> {
self.inner.reinit()?;
Ok(())
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<bool> {
self.inner.decode(input, output)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.flush(output)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.finish(output)
}
}
+38
View File
@@ -0,0 +1,38 @@
use crate::{flate::params::FlateEncoderParams, EncodeV2, FlateEncoder};
use compression_core::util::{PartialBuffer, WriteBuffer};
use std::io::Result;
#[derive(Debug)]
pub struct DeflateEncoder {
inner: FlateEncoder,
}
impl DeflateEncoder {
pub fn new(level: FlateEncoderParams) -> Self {
Self {
inner: FlateEncoder::new(level, false),
}
}
pub fn get_ref(&self) -> &FlateEncoder {
&self.inner
}
}
impl EncodeV2 for DeflateEncoder {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<()> {
self.inner.encode(input, output)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.flush(output)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.finish(output)
}
}
+4
View File
@@ -0,0 +1,4 @@
mod decoder;
mod encoder;
pub use self::{decoder::DeflateDecoder, encoder::DeflateEncoder};
+83
View File
@@ -0,0 +1,83 @@
use crate::DecodeV2;
use compression_core::util::{PartialBuffer, WriteBuffer};
use deflate64::InflaterManaged;
use std::io::{Error, ErrorKind, Result};
#[derive(Debug)]
pub struct Deflate64Decoder {
inflater: Box<InflaterManaged>,
}
impl Default for Deflate64Decoder {
fn default() -> Self {
Self {
inflater: Box::new(InflaterManaged::new()),
}
}
}
impl Deflate64Decoder {
pub fn new() -> Self {
Self::default()
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<bool> {
let result = self
.inflater
// Safety: We **trust** deflate64 to not write uninitialized bytes
.inflate_uninit(input.unwritten(), unsafe { output.unwritten_mut() });
if result.data_error {
Err(Error::new(ErrorKind::InvalidData, "invalid data"))
} else {
input.advance(result.bytes_consumed);
// Safety: We **trust** deflate64 to properly write bytes into buffer
unsafe { output.assume_init_and_advance(result.bytes_written) };
Ok(self.inflater.finished() && self.inflater.available_output() == 0)
}
}
}
impl DecodeV2 for Deflate64Decoder {
fn reinit(&mut self) -> Result<()> {
*self.inflater = InflaterManaged::new();
Ok(())
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<bool> {
self.decode(input, output)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.decode(&mut PartialBuffer::new(&[]), output)?;
loop {
let old_len = output.written_len();
self.decode(&mut PartialBuffer::new(&[]), output)?;
if output.written_len() == old_len {
break;
}
}
Ok(!output.has_no_spare_space())
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
let before = output.written_len();
let finished = self.decode(&mut PartialBuffer::new(&[]), output)?;
if !finished && output.written_len() == before && !output.has_no_spare_space() {
Err(ErrorKind::UnexpectedEof.into())
} else {
Ok(finished)
}
}
}
+3
View File
@@ -0,0 +1,3 @@
mod decoder;
pub use self::decoder::Deflate64Decoder;
+95
View File
@@ -0,0 +1,95 @@
use crate::DecodeV2;
use compression_core::util::{PartialBuffer, WriteBuffer};
use flate2::{Decompress, FlushDecompress, Status};
use std::io;
#[derive(Debug)]
pub struct FlateDecoder {
zlib_header: bool,
decompress: Decompress,
}
impl FlateDecoder {
pub(crate) fn new(zlib_header: bool) -> Self {
Self {
zlib_header,
decompress: Decompress::new(zlib_header),
}
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
flush: FlushDecompress,
) -> io::Result<Status> {
let prior_in = self.decompress.total_in();
let prior_out = self.decompress.total_out();
let status = self
.decompress
// Safety: We **trust** flate2 to not write uninitialized bytes into buffer
.decompress_uninit(input.unwritten(), unsafe { output.unwritten_mut() }, flush)?;
input.advance((self.decompress.total_in() - prior_in) as usize);
// Safety: We **trust** flate2 to write bytes into buffer properly
unsafe {
output.assume_init_and_advance((self.decompress.total_out() - prior_out) as usize)
};
Ok(status)
}
}
impl DecodeV2 for FlateDecoder {
fn reinit(&mut self) -> io::Result<()> {
self.decompress.reset(self.zlib_header);
Ok(())
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<bool> {
match self.decode(input, output, FlushDecompress::None)? {
Status::Ok => Ok(false),
Status::StreamEnd => Ok(true),
Status::BufError => Err(io::Error::other("unexpected BufError")),
}
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
self.decode(
&mut PartialBuffer::new(&[][..]),
output,
FlushDecompress::Sync,
)?;
loop {
let old_len = output.written_len();
self.decode(
&mut PartialBuffer::new(&[][..]),
output,
FlushDecompress::None,
)?;
if output.written_len() == old_len {
break;
}
}
Ok(!output.has_no_spare_space())
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
match self.decode(
&mut PartialBuffer::new(&[][..]),
output,
FlushDecompress::Finish,
)? {
Status::Ok => Ok(false),
Status::StreamEnd => Ok(true),
Status::BufError => Err(io::Error::other("unexpected BufError")),
}
}
}
+103
View File
@@ -0,0 +1,103 @@
use crate::{flate::params::FlateEncoderParams, EncodeV2};
use compression_core::util::{PartialBuffer, WriteBuffer};
use flate2::{Compress, FlushCompress, Status};
use std::io;
#[derive(Debug)]
pub struct FlateEncoder {
compress: Compress,
flushed: bool,
}
impl FlateEncoder {
pub fn new(level: FlateEncoderParams, zlib_header: bool) -> Self {
let level = flate2::Compression::from(level);
Self {
compress: Compress::new(level, zlib_header),
flushed: true,
}
}
pub fn get_ref(&self) -> &Compress {
&self.compress
}
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
flush: FlushCompress,
) -> io::Result<Status> {
let prior_in = self.compress.total_in();
let prior_out = self.compress.total_out();
let status = self
.compress
// Safety: We **trust** flate2 to not write uninitialized bytes into buffer
.compress_uninit(input.unwritten(), unsafe { output.unwritten_mut() }, flush)?;
input.advance((self.compress.total_in() - prior_in) as usize);
// Safety: We **trust** flate2 to write bytes properly into buffer
unsafe { output.assume_init_and_advance((self.compress.total_out() - prior_out) as usize) };
Ok(status)
}
}
impl EncodeV2 for FlateEncoder {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<()> {
self.flushed = false;
match self.encode(input, output, FlushCompress::None)? {
Status::Ok => Ok(()),
Status::StreamEnd => unreachable!(),
Status::BufError => Err(io::Error::other("unexpected BufError")),
}
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
// We need to keep track of whether we've already flushed otherwise we'll just keep writing
// out sync blocks continuously and probably never complete flushing.
if self.flushed {
return Ok(true);
}
self.encode(
&mut PartialBuffer::new(&[][..]),
output,
FlushCompress::Sync,
)?;
loop {
let old_len = output.written_len();
self.encode(
&mut PartialBuffer::new(&[][..]),
output,
FlushCompress::None,
)?;
if output.written_len() == old_len {
break;
}
}
let internal_flushed = !output.has_no_spare_space();
self.flushed = internal_flushed;
Ok(internal_flushed)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
self.flushed = false;
match self.encode(
&mut PartialBuffer::new(&[][..]),
output,
FlushCompress::Finish,
)? {
Status::Ok => Ok(false),
Status::StreamEnd => Ok(true),
Status::BufError => Err(io::Error::other("unexpected BufError")),
}
}
}
+5
View File
@@ -0,0 +1,5 @@
mod decoder;
mod encoder;
pub mod params;
pub use self::{decoder::FlateDecoder, encoder::FlateEncoder};
+40
View File
@@ -0,0 +1,40 @@
use std::convert::TryInto;
use compression_core::Level;
#[derive(Debug, Clone)]
pub struct FlateEncoderParams {
inner: flate2::Compression,
}
impl From<flate2::Compression> for FlateEncoderParams {
fn from(inner: flate2::Compression) -> Self {
Self { inner }
}
}
impl From<FlateEncoderParams> for flate2::Compression {
fn from(value: FlateEncoderParams) -> Self {
value.inner
}
}
impl From<Level> for FlateEncoderParams {
fn from(value: Level) -> Self {
let fastest = flate2::Compression::fast();
let best = flate2::Compression::best();
let none = flate2::Compression::none();
let inner = match value {
Level::Fastest => fastest,
Level::Best => best,
Level::Precise(quality) => flate2::Compression::new(
quality
.try_into()
.unwrap_or(0)
.clamp(none.level(), best.level()),
),
_ => flate2::Compression::default(),
};
Self { inner }
}
}
+160
View File
@@ -0,0 +1,160 @@
use super::header;
use crate::{DecodeV2, FlateDecoder};
use compression_core::util::{PartialBuffer, WriteBuffer};
use flate2::Crc;
use std::io::{Error, ErrorKind, Result};
#[derive(Debug)]
enum State {
Header(header::Parser),
Decoding,
Footer(PartialBuffer<[u8; 8]>),
Done,
}
#[derive(Debug)]
pub struct GzipDecoder {
inner: FlateDecoder,
crc: Crc,
state: State,
}
fn check_footer(crc: &Crc, input: &[u8; 8]) -> Result<()> {
let crc_sum = crc.sum().to_le_bytes();
let bytes_read = crc.amount().to_le_bytes();
if crc_sum != input[0..4] {
return Err(Error::new(
ErrorKind::InvalidData,
"CRC computed does not match",
));
}
if bytes_read != input[4..8] {
return Err(Error::new(
ErrorKind::InvalidData,
"amount of bytes read does not match",
));
}
Ok(())
}
impl Default for GzipDecoder {
fn default() -> Self {
Self {
inner: FlateDecoder::new(false),
crc: Crc::new(),
state: State::Header(header::Parser::default()),
}
}
}
impl GzipDecoder {
pub fn new() -> Self {
Self::default()
}
fn process(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
inner: impl Fn(&mut Self, &mut PartialBuffer<&[u8]>, &mut WriteBuffer<'_>) -> Result<bool>,
) -> Result<bool> {
loop {
match &mut self.state {
State::Header(parser) => {
if parser.input(&mut self.crc, input)?.is_some() {
self.crc.reset();
self.state = State::Decoding;
}
}
State::Decoding => {
let prior = output.written_len();
let res = inner(self, input, output);
if output.written_len() > prior {
// update CRC even if there was an error
self.crc.update(&output.written()[prior..]);
}
let done = res?;
if done {
self.state = State::Footer([0; 8].into());
}
}
State::Footer(footer) => {
footer.copy_unwritten_from(input);
if footer.unwritten().is_empty() {
check_footer(&self.crc, footer.get_mut())?;
self.state = State::Done;
}
}
State::Done => {}
};
if let State::Done = self.state {
return Ok(true);
}
if input.unwritten().is_empty() || output.has_no_spare_space() {
return Ok(false);
}
}
}
}
impl DecodeV2 for GzipDecoder {
fn reinit(&mut self) -> Result<()> {
self.inner.reinit()?;
self.crc.reset();
self.state = State::Header(header::Parser::default());
Ok(())
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<bool> {
self.process(input, output, |this, input, output| {
this.inner.decode(input, output)
})
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
loop {
match self.state {
State::Header(_) | State::Footer(_) | State::Done => return Ok(true),
State::Decoding => {
let prior = output.written_len();
let done = self.inner.flush(output)?;
self.crc.update(&output.written()[prior..]);
if done {
return Ok(true);
}
}
};
if output.has_no_spare_space() {
return Ok(false);
}
}
}
fn finish(&mut self, _output: &mut WriteBuffer<'_>) -> Result<bool> {
// Because of the footer we have to have already flushed all the data out before we get here
if let State::Done = self.state {
Ok(true)
} else {
Err(Error::from(ErrorKind::UnexpectedEof))
}
}
}
+160
View File
@@ -0,0 +1,160 @@
use crate::{flate::params::FlateEncoderParams, EncodeV2, FlateEncoder};
use compression_core::util::{PartialBuffer, WriteBuffer};
use flate2::{Compression, Crc};
use std::io;
#[derive(Debug)]
enum State {
Header(PartialBuffer<[u8; 10]>),
Encoding,
Footer(PartialBuffer<[u8; 8]>),
Done,
}
#[derive(Debug)]
pub struct GzipEncoder {
inner: FlateEncoder,
crc: Crc,
state: State,
}
fn header(level: Compression) -> [u8; 10] {
let level_byte = if level.level() >= Compression::best().level() {
0x02
} else if level.level() <= Compression::fast().level() {
0x04
} else {
0x00
};
[0x1f, 0x8b, 0x08, 0, 0, 0, 0, 0, level_byte, 0xff]
}
impl GzipEncoder {
pub fn new(level: FlateEncoderParams) -> Self {
Self {
inner: FlateEncoder::new(level.clone(), false),
crc: Crc::new(),
state: State::Header(header(Compression::from(level)).into()),
}
}
fn footer(&mut self) -> [u8; 8] {
let mut output = [0; 8];
output[..4].copy_from_slice(&self.crc.sum().to_le_bytes());
output[4..].copy_from_slice(&self.crc.amount().to_le_bytes());
output
}
}
impl EncodeV2 for GzipEncoder {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<()> {
loop {
match &mut self.state {
State::Header(header) => {
output.copy_unwritten_from(&mut *header);
if header.unwritten().is_empty() {
self.state = State::Encoding;
}
}
State::Encoding => {
let prior_written = input.written().len();
self.inner.encode(input, output)?;
self.crc.update(&input.written()[prior_written..]);
}
State::Footer(_) | State::Done => {
return Err(io::Error::other("encode after complete"));
}
};
if input.unwritten().is_empty() || output.has_no_spare_space() {
return Ok(());
}
}
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
loop {
let done = match &mut self.state {
State::Header(header) => {
output.copy_unwritten_from(&mut *header);
if header.unwritten().is_empty() {
self.state = State::Encoding;
}
false
}
State::Encoding => self.inner.flush(output)?,
State::Footer(footer) => {
output.copy_unwritten_from(&mut *footer);
if footer.unwritten().is_empty() {
self.state = State::Done;
true
} else {
false
}
}
State::Done => true,
};
if done {
return Ok(true);
}
if output.has_no_spare_space() {
return Ok(false);
}
}
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
loop {
match &mut self.state {
State::Header(header) => {
output.copy_unwritten_from(&mut *header);
if header.unwritten().is_empty() {
self.state = State::Encoding;
}
}
State::Encoding => {
if self.inner.finish(output)? {
self.state = State::Footer(self.footer().into());
}
}
State::Footer(footer) => {
output.copy_unwritten_from(&mut *footer);
if footer.unwritten().is_empty() {
self.state = State::Done;
}
}
State::Done => {}
};
if let State::Done = self.state {
return Ok(true);
}
if output.has_no_spare_space() {
return Ok(false);
}
}
}
}
+189
View File
@@ -0,0 +1,189 @@
use compression_core::util::PartialBuffer;
use flate2::Crc;
use std::io;
#[derive(Debug, Default)]
struct Flags {
_ascii: bool,
crc: bool,
extra: bool,
filename: bool,
comment: bool,
}
#[derive(Debug, Default)]
pub(super) struct Header {
flags: Flags,
}
#[derive(Debug)]
enum State {
Fixed(PartialBuffer<[u8; 10]>),
ExtraLen(PartialBuffer<[u8; 2]>),
Extra(usize),
Filename,
Comment,
Crc(PartialBuffer<[u8; 2]>),
Done,
}
impl Default for State {
fn default() -> Self {
State::Fixed(<_>::default())
}
}
#[derive(Debug, Default)]
pub(super) struct Parser {
state: State,
header: Header,
}
impl Header {
fn parse(input: &[u8; 10]) -> io::Result<Self> {
if input[0..3] != [0x1f, 0x8b, 0x08] {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid gzip header",
));
}
let flag = input[3];
let flags = Flags {
_ascii: (flag & 0b0000_0001) != 0,
crc: (flag & 0b0000_0010) != 0,
extra: (flag & 0b0000_0100) != 0,
filename: (flag & 0b0000_1000) != 0,
comment: (flag & 0b0001_0000) != 0,
};
Ok(Header { flags })
}
}
fn consume_input(crc: &mut Crc, n: usize, input: &mut PartialBuffer<&[u8]>) {
crc.update(&input.unwritten()[..n]);
input.advance(n);
}
fn consume_cstr(crc: &mut Crc, input: &mut PartialBuffer<&[u8]>) -> Option<()> {
if let Some(len) = memchr::memchr(0, input.unwritten()) {
consume_input(crc, len + 1, input);
Some(())
} else {
consume_input(crc, input.unwritten().len(), input);
None
}
}
impl Parser {
pub(super) fn input(
&mut self,
crc: &mut Crc,
input: &mut PartialBuffer<&[u8]>,
) -> io::Result<Option<Header>> {
loop {
match &mut self.state {
State::Fixed(data) => {
data.copy_unwritten_from(input);
if data.unwritten().is_empty() {
let data = data.get_mut();
crc.update(data);
self.header = Header::parse(data)?;
self.state = State::ExtraLen(<_>::default());
} else {
break Ok(None);
}
}
State::ExtraLen(data) => {
if !self.header.flags.extra {
self.state = State::Filename;
continue;
}
data.copy_unwritten_from(input);
if data.unwritten().is_empty() {
let data = data.get_mut();
crc.update(data);
let len = u16::from_le_bytes(*data);
self.state = State::Extra(len.into());
} else {
break Ok(None);
}
}
State::Extra(bytes_to_consume) => {
let n = input.unwritten().len().min(*bytes_to_consume);
*bytes_to_consume -= n;
consume_input(crc, n, input);
if *bytes_to_consume == 0 {
self.state = State::Filename;
} else {
break Ok(None);
}
}
State::Filename => {
if !self.header.flags.filename {
self.state = State::Comment;
continue;
}
if consume_cstr(crc, input).is_none() {
break Ok(None);
}
self.state = State::Comment;
}
State::Comment => {
if !self.header.flags.comment {
self.state = State::Crc(<_>::default());
continue;
}
if consume_cstr(crc, input).is_none() {
break Ok(None);
}
self.state = State::Crc(<_>::default());
}
State::Crc(data) => {
let header = std::mem::take(&mut self.header);
if !self.header.flags.crc {
self.state = State::Done;
break Ok(Some(header));
}
data.copy_unwritten_from(input);
break if data.unwritten().is_empty() {
let data = data.take().into_inner();
self.state = State::Done;
let checksum = crc.sum().to_le_bytes();
if data == checksum[..2] {
Ok(Some(header))
} else {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"CRC computed for header does not match",
))
}
} else {
Ok(None)
};
}
State::Done => break Err(io::Error::other("parser used after done")),
}
}
}
}
+5
View File
@@ -0,0 +1,5 @@
mod decoder;
mod encoder;
mod header;
pub use self::{decoder::GzipDecoder, encoder::GzipEncoder};
+234
View File
@@ -0,0 +1,234 @@
//! Adaptors for various compression algorithms.
#![cfg_attr(docsrs, feature(doc_cfg))]
use std::io::Result;
pub use compression_core as core;
#[cfg(feature = "brotli")]
pub mod brotli;
#[cfg(feature = "bzip2")]
pub mod bzip2;
#[cfg(feature = "deflate")]
pub mod deflate;
#[cfg(feature = "deflate64")]
pub mod deflate64;
#[cfg(feature = "flate2")]
pub mod flate;
#[cfg(feature = "gzip")]
pub mod gzip;
#[cfg(feature = "lz4")]
pub mod lz4;
#[cfg(feature = "lzma")]
pub mod lzma;
#[cfg(feature = "xz")]
pub mod xz;
#[cfg(feature = "lzma")]
pub mod xz2;
#[cfg(feature = "zlib")]
pub mod zlib;
#[cfg(feature = "zstd")]
pub mod zstd;
use compression_core::util::{PartialBuffer, WriteBuffer};
#[cfg(feature = "brotli")]
pub use self::brotli::{BrotliDecoder, BrotliEncoder};
#[cfg(feature = "bzip2")]
pub use self::bzip2::{BzDecoder, BzEncoder};
#[cfg(feature = "deflate")]
pub use self::deflate::{DeflateDecoder, DeflateEncoder};
#[cfg(feature = "deflate64")]
pub use self::deflate64::Deflate64Decoder;
#[cfg(feature = "flate2")]
pub use self::flate::{FlateDecoder, FlateEncoder};
#[cfg(feature = "gzip")]
pub use self::gzip::{GzipDecoder, GzipEncoder};
#[cfg(feature = "lz4")]
pub use self::lz4::{Lz4Decoder, Lz4Encoder};
#[cfg(feature = "lzma")]
pub use self::lzma::{LzmaDecoder, LzmaEncoder};
#[cfg(feature = "xz")]
pub use self::xz::{XzDecoder, XzEncoder};
#[cfg(feature = "lzma")]
pub use self::xz2::{Xz2Decoder, Xz2Encoder, Xz2FileFormat};
#[cfg(feature = "zlib")]
pub use self::zlib::{ZlibDecoder, ZlibEncoder};
#[cfg(feature = "zstd")]
pub use self::zstd::{ZstdDecoder, ZstdEncoder};
fn forward_output<R>(
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
f: impl FnOnce(&mut WriteBuffer<'_>) -> R,
) -> R {
let written_len = output.written_len();
let output_buffer = output.get_mut();
let mut write_buffer = WriteBuffer::new_initialized(output_buffer.as_mut());
write_buffer.advance(written_len);
let result = f(&mut write_buffer);
let new_written_len = write_buffer.written_len();
output.advance(new_written_len - written_len);
result
}
fn forward_input_output<R>(
input: &mut PartialBuffer<impl AsRef<[u8]>>,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
f: impl FnOnce(&mut PartialBuffer<&[u8]>, &mut WriteBuffer<'_>) -> R,
) -> R {
let written_len = input.written_len();
let input_buffer = input.get_mut();
let mut partial_buffer = PartialBuffer::new(input_buffer.as_ref());
partial_buffer.advance(written_len);
let result = forward_output(output, |output| f(&mut partial_buffer, output));
let new_written_len = partial_buffer.written_len();
input.advance(new_written_len - written_len);
result
}
pub trait Encode {
fn encode(
&mut self,
input: &mut PartialBuffer<impl AsRef<[u8]>>,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<()>;
/// Returns whether the internal buffers are flushed
fn flush(&mut self, output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>)
-> Result<bool>;
/// Returns whether the internal buffers are flushed and the end of the stream is written
fn finish(
&mut self,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool>;
}
impl<T: EncodeV2 + ?Sized> Encode for T {
fn encode(
&mut self,
input: &mut PartialBuffer<impl AsRef<[u8]>>,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<()> {
forward_input_output(input, output, |input, output| {
EncodeV2::encode(self, input, output)
})
}
fn flush(
&mut self,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool> {
forward_output(output, |output| EncodeV2::flush(self, output))
}
fn finish(
&mut self,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool> {
forward_output(output, |output| EncodeV2::finish(self, output))
}
}
/// version 2 of [`Encode`] that is trait object safe.
///
/// The different from [`Encode`] is that:
/// - It doesn't have any generic in it, so it is trait object safe
/// - It uses [`WriteBuffer`] for output, which will support uninitialized buffer.
pub trait EncodeV2 {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<()>;
/// Returns whether the internal buffers are flushed
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;
/// Returns whether the internal buffers are flushed and the end of the stream is written
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;
}
pub trait Decode {
/// Reinitializes this decoder ready to decode a new member/frame of data.
fn reinit(&mut self) -> Result<()>;
/// Returns whether the end of the stream has been read
fn decode(
&mut self,
input: &mut PartialBuffer<impl AsRef<[u8]>>,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool>;
/// Returns whether the internal buffers are flushed
fn flush(&mut self, output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>)
-> Result<bool>;
/// Returns whether the internal buffers are flushed
fn finish(
&mut self,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool>;
}
impl<T: DecodeV2 + ?Sized> Decode for T {
fn reinit(&mut self) -> Result<()> {
DecodeV2::reinit(self)
}
fn decode(
&mut self,
input: &mut PartialBuffer<impl AsRef<[u8]>>,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool> {
forward_input_output(input, output, |input, output| {
DecodeV2::decode(self, input, output)
})
}
fn flush(
&mut self,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool> {
forward_output(output, |output| DecodeV2::flush(self, output))
}
fn finish(
&mut self,
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> Result<bool> {
forward_output(output, |output| DecodeV2::finish(self, output))
}
}
/// version 2 [`Decode`] that is trait object safe.
///
/// The different from [`Decode`] is that:
/// - It doesn't have any generic in it, so it is trait object safe
/// - It uses [`WriteBuffer`] for output, which will support uninitialized buffer.
pub trait DecodeV2 {
/// Reinitializes this decoder ready to decode a new member/frame of data.
fn reinit(&mut self) -> Result<()>;
/// Returns whether the end of the stream has been read
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<bool>;
/// Returns whether the internal buffers are flushed
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;
/// Returns whether the internal buffers are flushed
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool>;
}
pub trait DecodedSize {
/// Returns the size of the input when uncompressed.
fn decoded_size(input: &[u8]) -> Result<u64>;
}
+120
View File
@@ -0,0 +1,120 @@
use crate::DecodeV2;
use compression_core::{
unshared::Unshared,
util::{PartialBuffer, WriteBuffer},
};
use lz4::liblz4::{
check_error, LZ4FDecompressionContext, LZ4F_createDecompressionContext, LZ4F_decompress,
LZ4F_freeDecompressionContext, LZ4F_resetDecompressionContext, LZ4F_VERSION,
};
use std::io::Result;
#[derive(Debug)]
struct DecoderContext {
ctx: LZ4FDecompressionContext,
}
#[derive(Debug)]
pub struct Lz4Decoder {
ctx: Unshared<DecoderContext>,
stream_ended: bool,
}
impl DecoderContext {
fn new() -> Result<Self> {
let mut context = LZ4FDecompressionContext(core::ptr::null_mut());
check_error(unsafe { LZ4F_createDecompressionContext(&mut context, LZ4F_VERSION) })?;
Ok(Self { ctx: context })
}
}
impl Drop for DecoderContext {
fn drop(&mut self) {
unsafe { LZ4F_freeDecompressionContext(self.ctx) };
}
}
impl Default for Lz4Decoder {
fn default() -> Self {
Self {
ctx: Unshared::new(DecoderContext::new().unwrap()),
stream_ended: false,
}
}
}
impl Lz4Decoder {
pub fn new() -> Self {
Self::default()
}
}
impl DecodeV2 for Lz4Decoder {
fn reinit(&mut self) -> Result<()> {
unsafe { LZ4F_resetDecompressionContext(self.ctx.get_mut().ctx) };
self.stream_ended = false;
Ok(())
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<bool> {
let mut input_size = input.unwritten().len();
// Safety: We **trust** lz4 bytes to properly function as expected,
// only write decompressed, initialized data into the buffer properly.
let result = unsafe {
let out_buf = output.unwritten_mut();
let mut output_size = out_buf.len();
let result = check_error(LZ4F_decompress(
self.ctx.get_mut().ctx,
out_buf.as_mut_ptr() as *mut _,
&mut output_size,
input.unwritten().as_ptr(),
&mut input_size,
core::ptr::null(),
))?;
output.assume_init_and_advance(output_size);
result
};
input.advance(input_size);
let finished = result == 0;
if finished {
self.stream_ended = true;
}
Ok(finished)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.decode(&mut PartialBuffer::new(&[][..]), output)?;
loop {
let old_len = output.written_len();
self.decode(&mut PartialBuffer::new(&[][..]), output)?;
if output.written_len() == old_len {
break;
}
}
Ok(!output.has_no_spare_space())
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.flush(output)?;
if self.stream_ended {
Ok(true)
} else {
Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"lz4 stream did not finish",
))
}
}
}
+300
View File
@@ -0,0 +1,300 @@
use crate::{lz4::params::EncoderParams, EncodeV2};
use compression_core::{
unshared::Unshared,
util::{PartialBuffer, WriteBuffer},
};
use lz4::liblz4::{
check_error, LZ4FCompressionContext, LZ4FPreferences, LZ4F_compressBegin, LZ4F_compressBound,
LZ4F_compressEnd, LZ4F_compressUpdate, LZ4F_createCompressionContext, LZ4F_flush,
LZ4F_freeCompressionContext, LZ4F_VERSION,
};
use std::io::{self, Result};
// https://github.com/lz4/lz4/blob/9d53d8bb6c4120345a0966e5d8b16d7def1f32c5/lib/lz4frame.h#L281
const LZ4F_HEADER_SIZE_MAX: usize = 19;
#[derive(Debug)]
struct EncoderContext {
ctx: LZ4FCompressionContext,
}
#[derive(Clone, Copy, Debug)]
enum State {
Header,
Encoding,
Footer,
Done,
}
enum Lz4Fn<'a, 'b> {
Begin,
Update {
input: &'a mut PartialBuffer<&'b [u8]>,
},
Flush,
End,
}
#[derive(Debug)]
pub struct Lz4Encoder {
ctx: Unshared<EncoderContext>,
state: State,
preferences: LZ4FPreferences,
limit: usize,
maybe_buffer: Option<PartialBuffer<Vec<u8>>>,
/// Minimum dst buffer size for a block
block_buffer_size: usize,
/// Minimum dst buffer size for flush/end
flush_buffer_size: usize,
}
// minimum size of destination buffer for compressing `src_size` bytes
fn min_dst_size(src_size: usize, preferences: &LZ4FPreferences) -> usize {
unsafe { LZ4F_compressBound(src_size, preferences) }
}
impl EncoderContext {
fn new() -> Result<Self> {
let mut context = LZ4FCompressionContext(core::ptr::null_mut());
check_error(unsafe { LZ4F_createCompressionContext(&mut context, LZ4F_VERSION) })?;
Ok(Self { ctx: context })
}
}
impl Drop for EncoderContext {
fn drop(&mut self) {
unsafe { LZ4F_freeCompressionContext(self.ctx) };
}
}
impl Lz4Encoder {
pub fn new(params: EncoderParams) -> Self {
let preferences = LZ4FPreferences::from(params);
let block_size = preferences.frame_info.block_size_id.get_size();
let block_buffer_size = min_dst_size(block_size, &preferences);
let flush_buffer_size = min_dst_size(0, &preferences);
Self {
ctx: Unshared::new(EncoderContext::new().unwrap()),
state: State::Header,
preferences,
limit: block_size,
maybe_buffer: None,
block_buffer_size,
flush_buffer_size,
}
}
pub fn buffer_size(&self) -> usize {
self.block_buffer_size
}
fn drain_buffer(&mut self, output: &mut WriteBuffer<'_>) -> (usize, usize) {
match self.maybe_buffer.as_mut() {
Some(buffer) => {
let drained_bytes = output.copy_unwritten_from(buffer);
(drained_bytes, buffer.unwritten().len())
}
None => (0, 0),
}
}
fn write(&mut self, lz4_fn: Lz4Fn<'_, '_>, output: &mut WriteBuffer<'_>) -> Result<usize> {
let (drained_before, undrained) = self.drain_buffer(output);
if undrained > 0 || output.has_no_spare_space() {
return Ok(drained_before);
}
let mut src_size = 0;
let min_dst_size = match &lz4_fn {
Lz4Fn::Begin => LZ4F_HEADER_SIZE_MAX,
Lz4Fn::Update { input } => {
src_size = input.unwritten().len().min(self.limit);
min_dst_size(src_size, &self.preferences)
}
Lz4Fn::Flush | Lz4Fn::End => self.flush_buffer_size,
};
// Safety: We **trust** lz4 to not write uninitialized bytes
let out_buf = unsafe { output.unwritten_mut() };
let output_len = out_buf.len();
let (dst_buffer, dst_size, maybe_internal_buffer) = if min_dst_size > output_len {
let buffer_size = self.block_buffer_size;
let buffer = self
.maybe_buffer
.get_or_insert_with(|| PartialBuffer::new(Vec::with_capacity(buffer_size)));
buffer.reset();
buffer.get_mut().clear();
(
buffer.get_mut().spare_capacity_mut().as_mut_ptr(),
buffer_size,
Some(buffer),
)
} else {
(out_buf.as_mut_ptr(), output_len, None)
};
let dst_buffer = dst_buffer as *mut u8;
let len = match lz4_fn {
Lz4Fn::Begin => {
let len = check_error(unsafe {
LZ4F_compressBegin(
self.ctx.get_mut().ctx,
dst_buffer,
dst_size,
&self.preferences,
)
})?;
self.state = State::Encoding;
len
}
Lz4Fn::Update { input } => {
let len = check_error(unsafe {
LZ4F_compressUpdate(
self.ctx.get_mut().ctx,
dst_buffer,
dst_size,
input.unwritten().as_ptr(),
src_size,
core::ptr::null(),
)
})?;
input.advance(src_size);
len
}
Lz4Fn::Flush => check_error(unsafe {
LZ4F_flush(
self.ctx.get_mut().ctx,
dst_buffer,
dst_size,
core::ptr::null(),
)
})?,
Lz4Fn::End => {
let len = check_error(unsafe {
LZ4F_compressEnd(
self.ctx.get_mut().ctx,
dst_buffer,
dst_size,
core::ptr::null(),
)
})?;
self.state = State::Footer;
len
}
};
let drained_after = if let Some(internal_buffer) = maybe_internal_buffer {
// Safety: We **trust** lz4 to properly write data into the buffer
unsafe {
internal_buffer.get_mut().set_len(len);
}
let (d, _) = self.drain_buffer(output);
d
} else {
// Safety: We **trust** lz4 to properly write data into the buffer
unsafe { output.assume_init_and_advance(len) };
len
};
Ok(drained_before + drained_after)
}
}
impl EncodeV2 for Lz4Encoder {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<()> {
loop {
match self.state {
State::Header => {
self.write(Lz4Fn::Begin, output)?;
}
State::Encoding => {
self.write(Lz4Fn::Update { input }, output)?;
}
State::Footer | State::Done => {
return Err(io::Error::other("encode after complete"));
}
}
if input.unwritten().is_empty() || output.has_no_spare_space() {
return Ok(());
}
}
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
loop {
let done = match self.state {
State::Header => {
self.write(Lz4Fn::Begin, output)?;
false
}
State::Encoding => {
let len = self.write(Lz4Fn::Flush, output)?;
len == 0
}
State::Footer => {
let (_, undrained) = self.drain_buffer(output);
if undrained == 0 {
self.state = State::Done;
true
} else {
false
}
}
State::Done => true,
};
if done {
return Ok(true);
}
if output.has_no_spare_space() {
return Ok(false);
}
}
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
loop {
match self.state {
State::Header => {
self.write(Lz4Fn::Begin, output)?;
}
State::Encoding => {
self.write(Lz4Fn::End, output)?;
}
State::Footer => {
let (_, undrained) = self.drain_buffer(output);
if undrained == 0 {
self.state = State::Done;
}
}
State::Done => {}
}
if let State::Done = self.state {
return Ok(true);
}
if output.has_no_spare_space() {
return Ok(false);
}
}
}
}
+5
View File
@@ -0,0 +1,5 @@
mod decoder;
mod encoder;
pub mod params;
pub use self::{decoder::Lz4Decoder, encoder::Lz4Encoder};
+102
View File
@@ -0,0 +1,102 @@
//! This module contains lz4-specific types for async-compression.
use std::convert::TryInto;
use compression_core::Level;
pub use lz4::liblz4::BlockSize;
use lz4::{
liblz4::{BlockChecksum, FrameType, LZ4FFrameInfo, LZ4FPreferences},
BlockMode, ContentChecksum,
};
/// lz4 compression parameters builder. This is a stable wrapper around lz4's own encoder
/// params type, to abstract over different versions of the lz4 library.
///
/// See the [lz4 documentation](https://github.com/lz4/lz4/blob/dev/doc/lz4frame_manual.html)
/// for more information on these parameters.
///
/// # Examples
///
/// ```
/// use compression_codecs::lz4;
///
/// let params = lz4::params::EncoderParams::default()
/// .block_size(lz4::params::BlockSize::Max1MB)
/// .content_checksum(true);
/// ```
#[derive(Clone, Debug, Default)]
pub struct EncoderParams {
block_size: Option<BlockSize>,
block_checksum: Option<BlockChecksum>,
content_checksum: Option<ContentChecksum>,
level: Level,
}
impl EncoderParams {
pub fn level(mut self, level: Level) -> Self {
self.level = level;
self
}
/// Sets input block size.
pub fn block_size(mut self, block_size: BlockSize) -> Self {
self.block_size = Some(block_size);
self
}
/// Add a 32-bit checksum of frame's decompressed data.
pub fn content_checksum(mut self, enable: bool) -> Self {
self.content_checksum = Some(if enable {
ContentChecksum::ChecksumEnabled
} else {
ContentChecksum::NoChecksum
});
self
}
/// Each block followed by a checksum of block's compressed data.
pub fn block_checksum(mut self, enable: bool) -> Self {
self.block_checksum = Some(if enable {
BlockChecksum::BlockChecksumEnabled
} else {
BlockChecksum::NoBlockChecksum
});
self
}
}
impl From<EncoderParams> for LZ4FPreferences {
fn from(value: EncoderParams) -> Self {
let block_size_id = value.block_size.clone().unwrap_or(BlockSize::Default);
let content_checksum_flag = value
.content_checksum
.clone()
.unwrap_or(ContentChecksum::NoChecksum);
let block_checksum_flag = value
.block_checksum
.clone()
.unwrap_or(BlockChecksum::NoBlockChecksum);
let compression_level = match value.level {
Level::Fastest => 0,
Level::Best => 12,
Level::Precise(quality) => quality.try_into().unwrap_or(0).clamp(0, 12),
_ => 0,
};
LZ4FPreferences {
frame_info: LZ4FFrameInfo {
block_size_id,
block_mode: BlockMode::Linked,
content_checksum_flag,
frame_type: FrameType::Frame,
content_size: 0,
dict_id: 0,
block_checksum_flag,
},
compression_level,
auto_flush: 0,
favor_dec_speed: 0,
reserved: [0; 3],
}
}
}
+63
View File
@@ -0,0 +1,63 @@
use crate::{DecodeV2, DecodedSize, Xz2Decoder};
use compression_core::util::{PartialBuffer, WriteBuffer};
use std::{convert::TryInto, io::Result};
/// Lzma decoding stream
#[derive(Debug)]
pub struct LzmaDecoder {
inner: Xz2Decoder,
}
impl From<Xz2Decoder> for LzmaDecoder {
fn from(inner: Xz2Decoder) -> Self {
Self { inner }
}
}
impl Default for LzmaDecoder {
fn default() -> Self {
Self {
inner: Xz2Decoder::new(usize::MAX.try_into().unwrap()),
}
}
}
impl LzmaDecoder {
pub fn new() -> Self {
Self::default()
}
pub fn with_memlimit(memlimit: u64) -> Self {
Self {
inner: Xz2Decoder::new(memlimit),
}
}
}
impl DecodeV2 for LzmaDecoder {
fn reinit(&mut self) -> Result<()> {
self.inner.reinit()
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<bool> {
self.inner.decode(input, output)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.flush(output)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.finish(output)
}
}
impl DecodedSize for LzmaDecoder {
fn decoded_size(input: &[u8]) -> Result<u64> {
Xz2Decoder::decoded_size(input)
}
}
+45
View File
@@ -0,0 +1,45 @@
use crate::{EncodeV2, Xz2Encoder, Xz2FileFormat};
use compression_core::{
util::{PartialBuffer, WriteBuffer},
Level,
};
use std::io::Result;
/// Lzma encoding stream
#[derive(Debug)]
pub struct LzmaEncoder {
inner: Xz2Encoder,
}
impl LzmaEncoder {
pub fn new(level: Level) -> Self {
Self {
inner: Xz2Encoder::new(Xz2FileFormat::Lzma, level),
}
}
}
impl From<Xz2Encoder> for LzmaEncoder {
fn from(inner: Xz2Encoder) -> Self {
Self { inner }
}
}
impl EncodeV2 for LzmaEncoder {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<()> {
self.inner.encode(input, output)
}
fn flush(&mut self, _output: &mut WriteBuffer<'_>) -> Result<bool> {
// Flush on LZMA 1 is not supported
Ok(true)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.finish(output)
}
}
+6
View File
@@ -0,0 +1,6 @@
mod decoder;
mod encoder;
pub mod params;
pub use self::{decoder::LzmaDecoder, encoder::LzmaEncoder};
+416
View File
@@ -0,0 +1,416 @@
//! Clone-able structs to build the non-clone-ables in liblzma
use std::convert::TryFrom;
#[cfg(feature = "xz-parallel")]
use std::num::NonZeroU32;
/// Used to control how the LZMA stream is created.
#[derive(Debug, Clone)]
pub enum LzmaEncoderParams {
Easy {
preset: u32,
check: liblzma::stream::Check,
},
Lzma {
options: LzmaOptions,
},
Raw {
filters: LzmaFilters,
},
Stream {
filters: LzmaFilters,
check: liblzma::stream::Check,
},
#[cfg(feature = "xz-parallel")]
MultiThread {
builder: MtStreamBuilder,
},
}
impl TryFrom<&LzmaEncoderParams> for liblzma::stream::Stream {
type Error = liblzma::stream::Error;
fn try_from(value: &LzmaEncoderParams) -> Result<Self, Self::Error> {
let stream = match value {
LzmaEncoderParams::Easy { preset, check } => Self::new_easy_encoder(*preset, *check)?,
LzmaEncoderParams::Lzma { options } => {
let options = liblzma::stream::LzmaOptions::try_from(options)?;
Self::new_lzma_encoder(&options)?
}
LzmaEncoderParams::Raw { filters } => {
let filters = liblzma::stream::Filters::try_from(filters)?;
Self::new_raw_encoder(&filters)?
}
LzmaEncoderParams::Stream { filters, check } => {
let filters = liblzma::stream::Filters::try_from(filters)?;
Self::new_stream_encoder(&filters, *check)?
}
#[cfg(feature = "xz-parallel")]
LzmaEncoderParams::MultiThread { builder } => {
let builder = liblzma::stream::MtStreamBuilder::try_from(builder)?;
builder.encoder()?
}
};
Ok(stream)
}
}
/// Directly translate to how the stream is constructed
#[derive(Clone, Debug)]
pub enum LzmaDecoderParams {
Auto {
mem_limit: u64,
flags: u32,
},
Lzip {
mem_limit: u64,
flags: u32,
},
Lzma {
mem_limit: u64,
},
Raw {
filters: LzmaFilters,
},
Stream {
mem_limit: u64,
flags: u32,
},
#[cfg(feature = "xz-parallel")]
MultiThread {
builder: MtStreamBuilder,
},
}
impl TryFrom<&LzmaDecoderParams> for liblzma::stream::Stream {
type Error = liblzma::stream::Error;
fn try_from(value: &LzmaDecoderParams) -> Result<Self, Self::Error> {
let stream = match value {
LzmaDecoderParams::Auto { mem_limit, flags } => {
Self::new_auto_decoder(*mem_limit, *flags)?
}
LzmaDecoderParams::Lzip { mem_limit, flags } => {
Self::new_lzip_decoder(*mem_limit, *flags)?
}
LzmaDecoderParams::Lzma { mem_limit } => Self::new_lzma_decoder(*mem_limit)?,
LzmaDecoderParams::Stream { mem_limit, flags } => {
Self::new_stream_decoder(*mem_limit, *flags)?
}
LzmaDecoderParams::Raw { filters } => {
let filters = liblzma::stream::Filters::try_from(filters)?;
Self::new_raw_decoder(&filters)?
}
#[cfg(feature = "xz-parallel")]
LzmaDecoderParams::MultiThread { builder } => {
let builder = liblzma::stream::MtStreamBuilder::try_from(builder)?;
builder.decoder()?
}
};
Ok(stream)
}
}
/// Clone-able `liblzma::Filters`.
#[derive(Default, Clone, Debug)]
pub struct LzmaFilters {
filters: Vec<LzmaFilter>,
}
impl LzmaFilters {
/// Add `LzmaFilter` to the collection
pub fn add_filter(mut self, filter: LzmaFilter) -> Self {
self.filters.push(filter);
self
}
}
/// An individual filter directly corresponding to liblzma Filters method calls
#[derive(Debug, Clone)]
pub enum LzmaFilter {
Arm(Option<Vec<u8>>),
Arm64(Option<Vec<u8>>),
ArmThumb(Option<Vec<u8>>),
Delta(Option<Vec<u8>>),
Ia64(Option<Vec<u8>>),
Lzma1(LzmaOptions),
Lzma1Properties(Vec<u8>),
Lzma2(LzmaOptions),
Lzma2Properties(Vec<u8>),
PowerPc(Option<Vec<u8>>),
Sparc(Option<Vec<u8>>),
X86(Option<Vec<u8>>),
}
impl TryFrom<&LzmaFilters> for liblzma::stream::Filters {
type Error = liblzma::stream::Error;
fn try_from(value: &LzmaFilters) -> Result<Self, Self::Error> {
let mut filters = liblzma::stream::Filters::new();
for f in value.filters.iter() {
match f {
LzmaFilter::Arm(Some(p)) => filters.arm_properties(p)?,
LzmaFilter::Arm(None) => filters.arm(),
LzmaFilter::Arm64(Some(p)) => filters.arm64_properties(p)?,
LzmaFilter::Arm64(None) => filters.arm64(),
LzmaFilter::ArmThumb(Some(p)) => filters.arm_thumb_properties(p)?,
LzmaFilter::ArmThumb(None) => filters.arm_thumb(),
LzmaFilter::Delta(Some(p)) => filters.delta_properties(p)?,
LzmaFilter::Delta(None) => filters.delta(),
LzmaFilter::Ia64(Some(p)) => filters.ia64_properties(p)?,
LzmaFilter::Ia64(None) => filters.ia64(),
LzmaFilter::Lzma1(opts) => {
let opts = liblzma::stream::LzmaOptions::try_from(opts)?;
filters.lzma1(&opts)
}
LzmaFilter::Lzma1Properties(p) => filters.lzma1_properties(p)?,
LzmaFilter::Lzma2(opts) => {
let opts = liblzma::stream::LzmaOptions::try_from(opts)?;
filters.lzma2(&opts)
}
LzmaFilter::Lzma2Properties(p) => filters.lzma2_properties(p)?,
LzmaFilter::PowerPc(Some(p)) => filters.powerpc_properties(p)?,
LzmaFilter::PowerPc(None) => filters.powerpc(),
LzmaFilter::Sparc(Some(p)) => filters.sparc_properties(p)?,
LzmaFilter::Sparc(None) => filters.sparc(),
LzmaFilter::X86(Some(p)) => filters.x86_properties(p)?,
LzmaFilter::X86(None) => filters.x86(),
};
}
Ok(filters)
}
}
/// A builder for liblzma::LzmaOptions, so that it can be cloned
#[derive(Default, Clone)]
pub struct LzmaOptions {
preset: Option<u32>,
depth: Option<u32>,
dict_size: Option<u32>,
literal_context_bits: Option<u32>,
literal_position_bits: Option<u32>,
match_finder: Option<liblzma::stream::MatchFinder>,
mode: Option<liblzma::stream::Mode>,
nice_len: Option<u32>,
position_bits: Option<u32>,
}
impl std::fmt::Debug for LzmaOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let match_finder = self.match_finder.map(|m| match m {
liblzma::stream::MatchFinder::HashChain3 => "HashChain3",
liblzma::stream::MatchFinder::HashChain4 => "HashChain4",
liblzma::stream::MatchFinder::BinaryTree2 => "BTree2",
liblzma::stream::MatchFinder::BinaryTree3 => "BTree3",
liblzma::stream::MatchFinder::BinaryTree4 => "BTree4",
});
let mode = self.mode.map(|m| match m {
liblzma::stream::Mode::Fast => "Fast",
liblzma::stream::Mode::Normal => "Normal",
});
f.debug_struct("LzmaOptions")
.field("preset", &self.preset)
.field("depth", &self.depth)
.field("dict_size", &self.dict_size)
.field("literal_context_bits", &self.literal_context_bits)
.field("literal_position_bits", &self.literal_position_bits)
.field("match_finder", &match_finder)
.field("mode", &mode)
.field("nice_len", &self.nice_len)
.field("position_bits", &self.position_bits)
.finish()
}
}
impl LzmaOptions {
pub fn preset(mut self, value: u32) -> Self {
self.preset = Some(value);
self
}
pub fn depth(mut self, value: u32) -> Self {
self.depth = Some(value);
self
}
pub fn dict_size(mut self, value: u32) -> Self {
self.dict_size = Some(value);
self
}
pub fn literal_context_bits(mut self, value: u32) -> Self {
self.literal_context_bits = Some(value);
self
}
pub fn literal_position_bits(mut self, value: u32) -> Self {
self.literal_position_bits = Some(value);
self
}
pub fn match_finder(mut self, value: liblzma::stream::MatchFinder) -> Self {
self.match_finder = Some(value);
self
}
pub fn mode(mut self, value: liblzma::stream::Mode) -> Self {
self.mode = Some(value);
self
}
pub fn nice_len(mut self, value: u32) -> Self {
self.nice_len = Some(value);
self
}
pub fn position_bits(mut self, value: u32) -> Self {
self.position_bits = Some(value);
self
}
}
impl TryFrom<&LzmaOptions> for liblzma::stream::LzmaOptions {
type Error = liblzma::stream::Error;
fn try_from(value: &LzmaOptions) -> Result<Self, Self::Error> {
let mut s = match value.preset {
Some(preset) => liblzma::stream::LzmaOptions::new_preset(preset)?,
None => liblzma::stream::LzmaOptions::new(),
};
if let Some(depth) = value.depth {
s.depth(depth);
}
if let Some(dict_size) = value.dict_size {
s.dict_size(dict_size);
}
if let Some(bits) = value.literal_context_bits {
s.literal_context_bits(bits);
}
if let Some(bits) = value.literal_position_bits {
s.literal_position_bits(bits);
}
if let Some(mf) = value.match_finder {
s.match_finder(mf);
}
if let Some(mode) = value.mode {
s.mode(mode);
}
if let Some(len) = value.nice_len {
s.nice_len(len);
}
if let Some(bits) = value.position_bits {
s.position_bits(bits);
}
Ok(s)
}
}
#[cfg(feature = "xz-parallel")]
#[derive(Default, Clone)]
/// Used to build a clonable mt stream builder
pub struct MtStreamBuilder {
block_size: Option<u64>,
preset: Option<u32>,
check: Option<liblzma::stream::Check>,
filters: Option<LzmaFilters>,
mem_limit_stop: Option<u64>,
mem_limit_threading: Option<u64>,
threads: Option<NonZeroU32>,
timeout_ms: Option<u32>,
}
#[cfg(feature = "xz-parallel")]
impl std::fmt::Debug for MtStreamBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let check = self.check.map(|s| match s {
liblzma::stream::Check::None => "None",
liblzma::stream::Check::Crc32 => "Crc32",
liblzma::stream::Check::Crc64 => "Crc64",
liblzma::stream::Check::Sha256 => "Sha256",
});
f.debug_struct("MtStreamBuilder")
.field("block_size", &self.block_size)
.field("preset", &self.preset)
.field("check", &check)
.field("filters", &self.filters)
.field("mem_limit_stop", &self.mem_limit_stop)
.field("mem_limit_threading", &self.mem_limit_threading)
.field("threads", &self.threads)
.field("timeout_ms", &self.timeout_ms)
.finish()
}
}
#[cfg(feature = "xz-parallel")]
impl MtStreamBuilder {
pub fn block_size(&mut self, block_size: u64) -> &mut Self {
self.block_size = Some(block_size);
self
}
pub fn preset(&mut self, preset: u32) -> &mut Self {
self.preset = Some(preset);
self
}
pub fn check(&mut self, check: liblzma::stream::Check) -> &mut Self {
self.check = Some(check);
self
}
pub fn filters(&mut self, filters: LzmaFilters) -> &mut Self {
self.filters = Some(filters);
self
}
pub fn mem_limit_stop(&mut self, mem_limit_stop: u64) -> &mut Self {
self.mem_limit_stop = Some(mem_limit_stop);
self
}
pub fn mem_limit_threading(&mut self, mem_limit_threading: u64) -> &mut Self {
self.mem_limit_threading = Some(mem_limit_threading);
self
}
pub fn threads(&mut self, threads: NonZeroU32) -> &mut Self {
self.threads = Some(threads);
self
}
pub fn timeout_ms(&mut self, timeout_ms: u32) -> &mut Self {
self.timeout_ms = Some(timeout_ms);
self
}
}
#[cfg(feature = "xz-parallel")]
impl TryFrom<&MtStreamBuilder> for liblzma::stream::MtStreamBuilder {
type Error = liblzma::stream::Error;
fn try_from(value: &MtStreamBuilder) -> Result<Self, Self::Error> {
let mut mt = liblzma::stream::MtStreamBuilder::new();
if let Some(block_size) = value.block_size {
mt.block_size(block_size);
}
if let Some(preset) = value.preset {
mt.preset(preset);
}
if let Some(check) = value.check {
mt.check(check);
}
if let Some(filters) = &value.filters {
let filters = liblzma::stream::Filters::try_from(filters)?;
mt.filters(filters);
}
if let Some(memlimit) = value.mem_limit_stop {
mt.memlimit_stop(memlimit);
}
if let Some(memlimit) = value.mem_limit_threading {
mt.memlimit_threading(memlimit);
}
if let Some(threads) = value.threads {
mt.threads(threads.get());
}
if let Some(timeout) = value.timeout_ms {
mt.timeout_ms(timeout);
}
Ok(mt)
}
}
+99
View File
@@ -0,0 +1,99 @@
use crate::{DecodeV2, DecodedSize, Xz2Decoder};
use compression_core::util::{PartialBuffer, WriteBuffer};
use std::{
convert::TryInto,
io::{Error, ErrorKind, Result},
};
/// Xz decoding stream
#[derive(Debug)]
pub struct XzDecoder {
inner: Xz2Decoder,
skip_padding: Option<u8>,
}
impl Default for XzDecoder {
fn default() -> Self {
Self {
inner: Xz2Decoder::new(usize::MAX.try_into().unwrap()),
skip_padding: None,
}
}
}
impl XzDecoder {
pub fn new() -> Self {
Self::default()
}
pub fn with_memlimit(memlimit: u64) -> Self {
Self {
inner: Xz2Decoder::new(memlimit),
skip_padding: None,
}
}
#[cfg(feature = "xz-parallel")]
pub fn parallel(threads: std::num::NonZeroU32, memlimit: u64) -> Self {
Self {
inner: Xz2Decoder::parallel(threads, memlimit),
skip_padding: None,
}
}
}
impl DecodeV2 for XzDecoder {
fn reinit(&mut self) -> Result<()> {
self.skip_padding = Some(4);
self.inner.reinit()
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<bool> {
if let Some(ref mut count) = self.skip_padding {
while input.unwritten().first() == Some(&0) {
input.advance(1);
*count -= 1;
if *count == 0 {
*count = 4;
}
}
if input.unwritten().is_empty() {
return Ok(true);
}
// If this is non-padding then it cannot start with null bytes, so it must be invalid
// padding
if *count != 4 {
return Err(Error::new(
ErrorKind::InvalidData,
"stream padding was not a multiple of 4 bytes",
));
}
self.skip_padding = None;
}
self.inner.decode(input, output)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
if self.skip_padding.is_some() {
return Ok(true);
}
self.inner.flush(output)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
if self.skip_padding.is_some() {
return Ok(true);
}
self.inner.finish(output)
}
}
impl DecodedSize for XzDecoder {
fn decoded_size(input: &[u8]) -> Result<u64> {
Xz2Decoder::decoded_size(input)
}
}
+45
View File
@@ -0,0 +1,45 @@
use crate::{EncodeV2, Xz2Encoder, Xz2FileFormat};
use compression_core::{
util::{PartialBuffer, WriteBuffer},
Level,
};
use std::io::Result;
/// Xz encoding stream
#[derive(Debug)]
pub struct XzEncoder {
inner: Xz2Encoder,
}
impl XzEncoder {
pub fn new(level: Level) -> Self {
Self {
inner: Xz2Encoder::new(Xz2FileFormat::Xz, level),
}
}
#[cfg(feature = "xz-parallel")]
pub fn parallel(threads: std::num::NonZeroU32, level: Level) -> Self {
Self {
inner: Xz2Encoder::xz_parallel(level, threads),
}
}
}
impl EncodeV2 for XzEncoder {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<()> {
self.inner.encode(input, output)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.flush(output)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.finish(output)
}
}
+4
View File
@@ -0,0 +1,4 @@
mod decoder;
mod encoder;
pub use self::{decoder::XzDecoder, encoder::XzEncoder};
+108
View File
@@ -0,0 +1,108 @@
use crate::{lzma::params::LzmaDecoderParams, xz2::process_stream, DecodeV2, DecodedSize};
use compression_core::util::{PartialBuffer, WriteBuffer};
use liblzma::stream::{Action, Stream};
use std::{
convert::TryFrom,
fmt,
io::{self, Cursor},
};
/// Xz2 decoding stream
pub struct Xz2Decoder {
stream: Stream,
params: LzmaDecoderParams,
}
impl fmt::Debug for Xz2Decoder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Xz2Decoder").finish_non_exhaustive()
}
}
impl TryFrom<LzmaDecoderParams> for Xz2Decoder {
type Error = liblzma::stream::Error;
fn try_from(params: LzmaDecoderParams) -> Result<Self, Self::Error> {
let stream = Stream::try_from(&params)?;
Ok(Self { stream, params })
}
}
impl Xz2Decoder {
pub fn new(mem_limit: u64) -> Self {
let params = LzmaDecoderParams::Auto {
mem_limit,
flags: 0,
};
Self::try_from(params).unwrap()
}
#[cfg(feature = "xz-parallel")]
pub fn parallel(threads: std::num::NonZeroU32, mem_limit: u64) -> Self {
use crate::lzma::params::MtStreamBuilder;
let mut builder = MtStreamBuilder::default();
builder
.threads(threads)
.timeout_ms(300)
.mem_limit_stop(mem_limit);
let params = LzmaDecoderParams::MultiThread { builder };
Self::try_from(params).unwrap()
}
}
impl DecodeV2 for Xz2Decoder {
fn reinit(&mut self) -> io::Result<()> {
*self = Self::try_from(self.params.clone())?;
Ok(())
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<bool> {
process_stream(&mut self.stream, input, output, Action::Run)
}
fn flush(&mut self, _output: &mut WriteBuffer<'_>) -> io::Result<bool> {
// While decoding flush is a noop
Ok(true)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
process_stream(
&mut self.stream,
&mut PartialBuffer::new(&[]),
output,
Action::Finish,
)
}
}
impl DecodedSize for Xz2Decoder {
fn decoded_size(input: &[u8]) -> io::Result<u64> {
let cursor = Cursor::new(input);
liblzma::uncompressed_size(cursor)
}
}
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use crate::{
lzma::params::{LzmaDecoderParams, LzmaFilter, LzmaFilters, LzmaOptions},
Xz2Decoder,
};
#[test]
fn test_lzma_decoder_from_params() {
let filters = LzmaFilters::default().add_filter(LzmaFilter::Lzma2(LzmaOptions::default()));
let params = LzmaDecoderParams::Raw { filters };
Xz2Decoder::try_from(params).unwrap();
}
}
+116
View File
@@ -0,0 +1,116 @@
use compression_core::{
util::{PartialBuffer, WriteBuffer},
Level,
};
use liblzma::stream::{Action, Check, Stream};
use std::{
convert::{TryFrom, TryInto},
fmt, io,
};
use crate::{
lzma::params::{LzmaEncoderParams, LzmaOptions},
xz2::process_stream,
EncodeV2, Xz2FileFormat,
};
/// Xz2 encoding stream
pub struct Xz2Encoder {
stream: Stream,
params: LzmaEncoderParams,
}
impl fmt::Debug for Xz2Encoder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Xz2Encoder").finish_non_exhaustive()
}
}
impl TryFrom<LzmaEncoderParams> for Xz2Encoder {
type Error = liblzma::stream::Error;
fn try_from(params: LzmaEncoderParams) -> Result<Self, Self::Error> {
let stream = Stream::try_from(&params)?;
Ok(Self {
stream,
params: params.clone(),
})
}
}
fn xz2_level(level: Level) -> u32 {
match level {
Level::Fastest => 0,
Level::Best => 9,
Level::Precise(quality) => quality.try_into().unwrap_or(0).clamp(0, 9),
_ => 5,
}
}
impl Xz2Encoder {
pub fn new(format: Xz2FileFormat, level: Level) -> Self {
let preset = xz2_level(level);
let params = match format {
Xz2FileFormat::Xz => LzmaEncoderParams::Easy {
preset,
check: Check::Crc64,
},
Xz2FileFormat::Lzma => {
let options = LzmaOptions::default().preset(preset);
LzmaEncoderParams::Lzma { options }
}
};
Self::try_from(params).unwrap()
}
#[cfg(feature = "xz-parallel")]
pub fn xz_parallel(level: Level, threads: std::num::NonZeroU32) -> Self {
use crate::lzma::params::MtStreamBuilder;
let preset = xz2_level(level);
let mut builder = MtStreamBuilder::default();
builder
.threads(threads)
.timeout_ms(300)
.preset(preset)
.check(Check::Crc64);
let params = LzmaEncoderParams::MultiThread { builder };
Self::try_from(params).unwrap()
}
}
impl EncodeV2 for Xz2Encoder {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<()> {
process_stream(&mut self.stream, input, output, Action::Run).map(|_| ())
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
let action = match &self.params {
// Multi-threaded streams don't support SyncFlush, use FullFlush instead
#[cfg(feature = "xz-parallel")]
LzmaEncoderParams::MultiThread { builder: _ } => Action::FullFlush,
_ => Action::SyncFlush,
};
process_stream(
&mut self.stream,
&mut PartialBuffer::new(&[]),
output,
action,
)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
process_stream(
&mut self.stream,
&mut PartialBuffer::new(&[]),
output,
Action::Finish,
)
}
}
+40
View File
@@ -0,0 +1,40 @@
mod decoder;
mod encoder;
#[derive(Debug)]
pub enum Xz2FileFormat {
Xz,
Lzma,
}
pub use self::{decoder::Xz2Decoder, encoder::Xz2Encoder};
use compression_core::util::{PartialBuffer, WriteBuffer};
use liblzma::stream::{Action, Status, Stream};
use std::io;
/// Return `Ok(true)` if stream ends.
fn process_stream(
stream: &mut Stream,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
action: Action,
) -> io::Result<bool> {
let previous_in = stream.total_in() as usize;
let previous_out = stream.total_out() as usize;
// Safety: We **trust** liblzma to not write uninitialized bytes into the buffer
let status =
stream.process_uninit(input.unwritten(), unsafe { output.unwritten_mut() }, action)?;
input.advance(stream.total_in() as usize - previous_in);
// Safety: We **trust** liblzma to write bytes into the buffer properly
unsafe { output.assume_init_and_advance(stream.total_out() as usize - previous_out) };
match status {
Status::Ok => Ok(false),
Status::StreamEnd => Ok(true),
Status::GetCheck => Err(io::Error::other("Unexpected lzma integrity check")),
Status::MemNeeded => Err(io::ErrorKind::OutOfMemory.into()),
}
}
+43
View File
@@ -0,0 +1,43 @@
use crate::{DecodeV2, FlateDecoder};
use compression_core::util::{PartialBuffer, WriteBuffer};
use std::io::Result;
#[derive(Debug)]
pub struct ZlibDecoder {
inner: FlateDecoder,
}
impl Default for ZlibDecoder {
fn default() -> Self {
Self {
inner: FlateDecoder::new(true),
}
}
}
impl ZlibDecoder {
pub fn new() -> Self {
Self::default()
}
}
impl DecodeV2 for ZlibDecoder {
fn reinit(&mut self) -> Result<()> {
self.inner.reinit()?;
Ok(())
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<bool> {
self.inner.decode(input, output)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.flush(output)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.finish(output)
}
}
+38
View File
@@ -0,0 +1,38 @@
use crate::{flate::params::FlateEncoderParams, EncodeV2, FlateEncoder};
use compression_core::util::{PartialBuffer, WriteBuffer};
use std::io::Result;
#[derive(Debug)]
pub struct ZlibEncoder {
inner: FlateEncoder,
}
impl ZlibEncoder {
pub fn new(level: FlateEncoderParams) -> Self {
Self {
inner: FlateEncoder::new(level, true),
}
}
pub fn get_ref(&self) -> &FlateEncoder {
&self.inner
}
}
impl EncodeV2 for ZlibEncoder {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<()> {
self.inner.encode(input, output)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.flush(output)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.inner.finish(output)
}
}
+4
View File
@@ -0,0 +1,4 @@
mod decoder;
mod encoder;
pub use self::{decoder::ZlibDecoder, encoder::ZlibEncoder};
+120
View File
@@ -0,0 +1,120 @@
use crate::{
zstd::{params::DParameter, OperationExt},
{DecodeV2, DecodedSize},
};
use compression_core::{
unshared::Unshared,
util::{PartialBuffer, WriteBuffer},
};
use libzstd::stream::raw::Decoder;
use std::{
convert::TryInto,
io::{self, Result},
};
use zstd_safe::get_error_name;
#[derive(Debug)]
pub struct ZstdDecoder {
decoder: Unshared<Decoder<'static>>,
stream_ended: bool,
needs_input: bool,
}
impl Default for ZstdDecoder {
fn default() -> Self {
Self {
decoder: Unshared::new(Decoder::new().unwrap()),
stream_ended: false,
needs_input: false,
}
}
}
impl ZstdDecoder {
pub fn new() -> Self {
Self::default()
}
pub fn new_with_params(params: &[DParameter]) -> Self {
let mut decoder = Decoder::new().unwrap();
for param in params {
decoder.set_parameter(param.as_zstd()).unwrap();
}
Self {
decoder: Unshared::new(decoder),
stream_ended: false,
needs_input: false,
}
}
pub fn new_with_dict(dictionary: &[u8]) -> io::Result<Self> {
let decoder = Decoder::with_dictionary(dictionary)?;
Ok(Self {
decoder: Unshared::new(decoder),
stream_ended: false,
needs_input: false,
})
}
}
impl DecodeV2 for ZstdDecoder {
fn reinit(&mut self) -> Result<()> {
self.decoder.reinit()?;
self.stream_ended = false;
self.needs_input = false;
Ok(())
}
fn decode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<bool> {
let empty_input = input.unwritten().is_empty();
// Once buffered output is drained, wait for input before calling zstd
// again. Repeated empty-input calls (e.g. while a reader is Pending)
// otherwise trigger its no-forward-progress error and poison the context.
if empty_input && self.needs_input {
return Ok(false);
}
let before = output.written_len();
let finished = self.decoder.run(input, output)?;
self.needs_input =
empty_input && output.written_len() == before && !output.has_no_spare_space();
if finished {
self.stream_ended = true;
}
Ok(finished)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
// Note: stream_ended is not updated here because zstd's flush only flushes
// buffered output and doesn't indicate stream completion. Stream completion
// is detected in decode() when status.remaining == 0.
self.decoder.flush(output)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.decoder.finish(output)?;
if self.stream_ended {
Ok(true)
} else {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"zstd stream did not finish",
))
}
}
}
impl DecodedSize for ZstdDecoder {
fn decoded_size(input: &[u8]) -> Result<u64> {
zstd_safe::find_frame_compressed_size(input)
.map_err(|error_code| io::Error::other(get_error_name(error_code)))
.and_then(|size| {
size.try_into()
.map_err(|_| io::Error::from(io::ErrorKind::FileTooLarge))
})
}
}
+59
View File
@@ -0,0 +1,59 @@
use crate::{
zstd::{params::CParameter, OperationExt},
EncodeV2,
};
use compression_core::{
unshared::Unshared,
util::{PartialBuffer, WriteBuffer},
};
use libzstd::stream::raw::Encoder;
use std::io::{self, Result};
#[derive(Debug)]
pub struct ZstdEncoder {
encoder: Unshared<Encoder<'static>>,
}
impl ZstdEncoder {
pub fn new(level: i32) -> Self {
Self {
encoder: Unshared::new(Encoder::new(level).unwrap()),
}
}
pub fn new_with_params(level: i32, params: &[CParameter]) -> Self {
let mut encoder = Encoder::new(level).unwrap();
for param in params {
encoder.set_parameter(param.as_zstd()).unwrap();
}
Self {
encoder: Unshared::new(encoder),
}
}
pub fn new_with_dict(level: i32, dictionary: &[u8]) -> io::Result<Self> {
let encoder = Encoder::with_dictionary(level, dictionary)?;
Ok(Self {
encoder: Unshared::new(encoder),
})
}
}
impl EncodeV2 for ZstdEncoder {
fn encode(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> Result<()> {
self.encoder.run(input, output)?;
Ok(())
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.encoder.flush(output)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> Result<bool> {
self.encoder.finish(output)
}
}
+97
View File
@@ -0,0 +1,97 @@
mod decoder;
mod encoder;
pub mod params;
pub use self::{decoder::ZstdDecoder, encoder::ZstdEncoder};
use compression_core::{
unshared::Unshared,
util::{PartialBuffer, WriteBuffer},
};
use libzstd::stream::raw::{InBuffer, Operation, OutBuffer, WriteBuf};
use std::io;
#[repr(transparent)]
struct WriteBufferWrapper<'a>(WriteBuffer<'a>);
unsafe impl WriteBuf for WriteBufferWrapper<'_> {
fn as_slice(&self) -> &[u8] {
self.0.written()
}
fn capacity(&self) -> usize {
self.0.capacity()
}
fn as_mut_ptr(&mut self) -> *mut u8 {
self.0.as_mut_ptr()
}
unsafe fn filled_until(&mut self, n: usize) {
self.0.set_written_and_initialized_len(n);
}
}
trait WriteBufExt {
fn get_out_buf(&mut self) -> OutBuffer<'_, WriteBufferWrapper<'_>>;
}
impl WriteBufExt for WriteBuffer<'_> {
fn get_out_buf(&mut self) -> OutBuffer<'_, WriteBufferWrapper<'_>> {
{
use std::mem::{align_of, size_of};
assert_eq!(
size_of::<WriteBuffer<'static>>(),
size_of::<WriteBufferWrapper<'static>>()
);
assert_eq!(
align_of::<WriteBuffer<'static>>(),
align_of::<WriteBufferWrapper<'static>>()
);
}
// Pass written_len to avoid overwriting existing data in buffer.
let written_len = self.written_len();
OutBuffer::around_pos(
unsafe { &mut *(self as *mut _ as *mut WriteBufferWrapper<'_>) },
written_len,
)
}
}
trait OperationExt {
fn reinit(&mut self) -> io::Result<()>;
/// Return `true` if finished.
fn run(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<bool>;
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool>;
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool>;
}
impl<C: Operation> OperationExt for Unshared<C> {
fn reinit(&mut self) -> io::Result<()> {
self.get_mut().reinit()
}
fn run(
&mut self,
input: &mut PartialBuffer<&[u8]>,
output: &mut WriteBuffer<'_>,
) -> io::Result<bool> {
let mut in_buf = InBuffer::around(input.unwritten());
let result = self.get_mut().run(&mut in_buf, &mut output.get_out_buf());
input.advance(in_buf.pos());
Ok(result? == 0)
}
fn flush(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
Ok(self.get_mut().flush(&mut output.get_out_buf())? == 0)
}
fn finish(&mut self, output: &mut WriteBuffer<'_>) -> io::Result<bool> {
Ok(self.get_mut().finish(&mut output.get_out_buf(), true)? == 0)
}
}
+154
View File
@@ -0,0 +1,154 @@
//! This module contains zstd-specific types for async-compression.
use compression_core::Level;
/// A compression parameter for zstd. This is a stable wrapper around zstd's own `CParameter`
/// type, to abstract over different versions of the zstd library.
///
/// See the [zstd documentation](https://facebook.github.io/zstd/zstd_manual.html) for more
/// information on these parameters.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct CParameter(libzstd::stream::raw::CParameter);
impl From<CParameter> for libzstd::stream::raw::CParameter {
fn from(value: CParameter) -> Self {
value.0
}
}
impl CParameter {
pub fn quality(level: Level) -> i32 {
let (fastest, best) = libzstd::compression_level_range().into_inner();
// NOTE: zstd's "fastest" level is -131072 which can create outputs larger than inputs.
// This library chooses a "fastest" level which has a more-or-less equivalent compression
// ratio to gzip's fastest mode. We still allow precise levels to go negative.
// See discussion in https://github.com/Nullus157/async-compression/issues/352
const OUR_FASTEST: i32 = 1;
match level {
Level::Fastest => OUR_FASTEST,
Level::Best => best,
Level::Precise(quality) => quality.clamp(fastest, best),
_ => libzstd::DEFAULT_COMPRESSION_LEVEL,
}
}
/// Window size in bytes (as a power of two)
pub fn window_log(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::WindowLog(value))
}
/// Size of the initial probe table in 4-byte entries (as a power of two)
pub fn hash_log(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::HashLog(value))
}
/// Size of the multi-probe table in 4-byte entries (as a power of two)
pub fn chain_log(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::ChainLog(value))
}
/// Number of search attempts (as a power of two)
pub fn search_log(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::SearchLog(value))
}
/// Minimum size of matches searched for
pub fn min_match(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::MinMatch(value))
}
/// Strategy-dependent length modifier
pub fn target_length(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::TargetLength(value))
}
/// Enable long-distance matching mode to look for and emit long-distance references.
///
/// This increases the default window size.
pub fn enable_long_distance_matching(value: bool) -> Self {
Self(libzstd::stream::raw::CParameter::EnableLongDistanceMatching(value))
}
/// Size of the long-distance matching table (as a power of two)
pub fn ldm_hash_log(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::LdmHashLog(value))
}
/// Minimum size of long-distance matches searched for
pub fn ldm_min_match(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::LdmMinMatch(value))
}
/// Size of each bucket in the LDM hash table for collision resolution (as a power of two)
pub fn ldm_bucket_size_log(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::LdmBucketSizeLog(value))
}
/// Frequency of using the LDM hash table (as a power of two)
pub fn ldm_hash_rate_log(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::LdmHashRateLog(value))
}
/// Emit the size of the content (default: true).
pub fn content_size_flag(value: bool) -> Self {
Self(libzstd::stream::raw::CParameter::ContentSizeFlag(value))
}
/// Emit a checksum (default: false).
pub fn checksum_flag(value: bool) -> Self {
Self(libzstd::stream::raw::CParameter::ChecksumFlag(value))
}
/// Emit a dictionary ID when using a custom dictionary (default: true).
pub fn dict_id_flag(value: bool) -> Self {
Self(libzstd::stream::raw::CParameter::DictIdFlag(value))
}
/// Number of threads to spawn.
///
/// If set to 0, compression functions will block; if set to 1 or more, compression will
/// run in background threads and `flush` pushes bytes through the compressor.
///
/// # Panics
///
/// This parameter requires feature `zstdmt` to be enabled, otherwise it will cause a panic
/// when used in `ZstdEncoder::with_quality_and_params()` calls.
//
// TODO: make this a normal feature guarded fn on next breaking release
#[cfg_attr(docsrs, doc(cfg(feature = "zstdmt")))]
pub fn nb_workers(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::NbWorkers(value))
}
/// Number of bytes given to each worker.
///
/// If set to 0, zstd selects a job size based on compression parameters.
pub fn job_size(value: u32) -> Self {
Self(libzstd::stream::raw::CParameter::JobSize(value))
}
pub(crate) fn as_zstd(&self) -> libzstd::stream::raw::CParameter {
self.0
}
}
/// A decompression parameter for zstd. This is a stable wrapper around zstd's own `DParameter`
/// type, to abstract over different versions of the zstd library.
///
/// See the [zstd documentation](https://facebook.github.io/zstd/zstd_manual.html) for more
/// information on these parameters.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct DParameter(libzstd::stream::raw::DParameter);
impl DParameter {
/// Maximum window size in bytes (as a power of two)
pub fn window_log_max(value: u32) -> Self {
Self(libzstd::stream::raw::DParameter::WindowLogMax(value))
}
pub(crate) fn as_zstd(&self) -> libzstd::stream::raw::DParameter {
self.0
}
}