numcodecs_zlib/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
//! [![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs]
//!
//! [CI Status]: https://img.shields.io/github/actions/workflow/status/juntyr/numcodecs-rs/ci.yml?branch=main
//! [workflow]: https://github.com/juntyr/numcodecs-rs/actions/workflows/ci.yml?query=branch%3Amain
//!
//! [MSRV]: https://img.shields.io/badge/MSRV-1.76.0-blue
//! [repo]: https://github.com/juntyr/numcodecs-rs
//!
//! [Latest Version]: https://img.shields.io/crates/v/numcodecs-zlib
//! [crates.io]: https://crates.io/crates/numcodecs-zlib
//!
//! [Rust Doc Crate]: https://img.shields.io/docsrs/numcodecs-zlib
//! [docs.rs]: https://docs.rs/numcodecs-zlib/
//!
//! [Rust Doc Main]: https://img.shields.io/badge/docs-main-blue
//! [docs]: https://juntyr.github.io/numcodecs-rs/numcodecs_zlib
//!
//! Zlib codec implementation for the [`numcodecs`] API.
use std::borrow::Cow;
use ndarray::Array1;
use numcodecs::{
AnyArray, AnyArrayAssignError, AnyArrayDType, AnyArrayView, AnyArrayViewMut, AnyCowArray,
Codec, StaticCodec, StaticCodecConfig,
};
use schemars::{JsonSchema, JsonSchema_repr};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use thiserror::Error;
#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
/// Codec providing compression using Zlib
pub struct ZlibCodec {
/// Zlib compression level.
///
/// The level ranges from 0, no compression, to 9, best compression.
pub level: ZlibLevel,
}
#[derive(Copy, Clone, Serialize_repr, Deserialize_repr, JsonSchema_repr)]
#[repr(u8)]
/// Zlib compression level.
///
/// The level ranges from 0, no compression, to 9, best compression.
#[allow(missing_docs)]
pub enum ZlibLevel {
ZNoCompression = 0,
ZBestSpeed = 1,
ZLevel2 = 2,
ZLevel3 = 3,
ZLevel4 = 4,
ZLevel5 = 5,
ZLevel6 = 6,
ZLevel7 = 7,
ZLevel8 = 8,
ZBestCompression = 9,
}
impl Codec for ZlibCodec {
type Error = ZlibCodecError;
fn encode(&self, data: AnyCowArray) -> Result<AnyArray, Self::Error> {
compress(data.view(), self.level)
.map(|bytes| AnyArray::U8(Array1::from_vec(bytes).into_dyn()))
}
fn decode(&self, encoded: AnyCowArray) -> Result<AnyArray, Self::Error> {
let AnyCowArray::U8(encoded) = encoded else {
return Err(ZlibCodecError::EncodedDataNotBytes {
dtype: encoded.dtype(),
});
};
if !matches!(encoded.shape(), [_]) {
return Err(ZlibCodecError::EncodedDataNotOneDimensional {
shape: encoded.shape().to_vec(),
});
}
decompress(&AnyCowArray::U8(encoded).as_bytes())
}
fn decode_into(
&self,
encoded: AnyArrayView,
decoded: AnyArrayViewMut,
) -> Result<(), Self::Error> {
let AnyArrayView::U8(encoded) = encoded else {
return Err(ZlibCodecError::EncodedDataNotBytes {
dtype: encoded.dtype(),
});
};
if !matches!(encoded.shape(), [_]) {
return Err(ZlibCodecError::EncodedDataNotOneDimensional {
shape: encoded.shape().to_vec(),
});
}
decompress_into(&AnyArrayView::U8(encoded).as_bytes(), decoded)
}
}
impl StaticCodec for ZlibCodec {
const CODEC_ID: &'static str = "zlib";
type Config<'de> = Self;
fn from_config(config: Self::Config<'_>) -> Self {
config
}
fn get_config(&self) -> StaticCodecConfig<Self> {
StaticCodecConfig::from(self)
}
}
#[derive(Debug, Error)]
/// Errors that may occur when applying the [`ZlibCodec`].
pub enum ZlibCodecError {
/// [`ZlibCodec`] failed to encode the header
#[error("Zlib failed to encode the header")]
HeaderEncodeFailed {
/// Opaque source error
source: ZlibHeaderError,
},
/// [`ZlibCodec`] can only decode one-dimensional byte arrays but received
/// an array of a different dtype
#[error(
"Zlib can only decode one-dimensional byte arrays but received an array of dtype {dtype}"
)]
EncodedDataNotBytes {
/// The unexpected dtype of the encoded array
dtype: AnyArrayDType,
},
/// [`ZlibCodec`] can only decode one-dimensional byte arrays but received
/// an array of a different shape
#[error("Zlib can only decode one-dimensional byte arrays but received a byte array of shape {shape:?}")]
EncodedDataNotOneDimensional {
/// The unexpected shape of the encoded array
shape: Vec<usize>,
},
/// [`ZlibCodec`] failed to encode the header
#[error("Zlib failed to decode the header")]
HeaderDecodeFailed {
/// Opaque source error
source: ZlibHeaderError,
},
/// [`ZlibCodec`] decode consumed less encoded data, which contains trailing
/// junk
#[error("Zlib decode consumed less encoded data, which contains trailing junk")]
DecodeExcessiveEncodedData,
/// [`ZlibCodec`] produced less decoded data than expected
#[error("Zlib produced less decoded data than expected")]
DecodeProducedLess,
/// [`ZlibCodec`] failed to decode the encoded data
#[error("Zlib failed to decode the encoded data")]
ZlibDecodeFailed {
/// Opaque source error
source: ZlibDecodeError,
},
/// [`ZlibCodec`] cannot decode into the provided array
#[error("Zlib cannot decode into the provided array")]
MismatchedDecodeIntoArray {
/// The source of the error
#[from]
source: AnyArrayAssignError,
},
}
#[derive(Debug, Error)]
#[error(transparent)]
/// Opaque error for when encoding or decoding the header fails
pub struct ZlibHeaderError(postcard::Error);
#[derive(Debug, Error)]
#[error(transparent)]
/// Opaque error for when decoding with Zlib fails
pub struct ZlibDecodeError(miniz_oxide::inflate::DecompressError);
#[allow(clippy::needless_pass_by_value)]
/// Compress the `array` using Zlib with the provided `level`.
///
/// # Errors
///
/// Errors with [`ZlibCodecError::HeaderEncodeFailed`] if encoding the header
/// to the output bytevec failed.
///
/// # Panics
///
/// Panics if the infallible encoding with Zlib fails.
pub fn compress(array: AnyArrayView, level: ZlibLevel) -> Result<Vec<u8>, ZlibCodecError> {
let data = array.as_bytes();
let mut encoded = postcard::to_extend(
&CompressionHeader {
dtype: array.dtype(),
shape: Cow::Borrowed(array.shape()),
},
Vec::new(),
)
.map_err(|err| ZlibCodecError::HeaderEncodeFailed {
source: ZlibHeaderError(err),
})?;
let mut in_pos = 0;
let mut out_pos = encoded.len();
// The comp flags function sets the zlib flag if the window_bits parameter
// is > 0.
let flags =
miniz_oxide::deflate::core::create_comp_flags_from_zip_params((level as u8).into(), 1, 0);
let mut compressor = miniz_oxide::deflate::core::CompressorOxide::new(flags);
encoded.resize(encoded.len() + (data.len() / 2).max(2), 0);
loop {
let (Some(data_left), Some(encoded_left)) =
(data.get(in_pos..), encoded.get_mut(out_pos..))
else {
#[allow(clippy::panic)] // this would be a bug and cannot be user-caused
{
panic!("Zlib encode bug: input or output is out of bounds")
}
};
let (status, bytes_in, bytes_out) = miniz_oxide::deflate::core::compress(
&mut compressor,
data_left,
encoded_left,
miniz_oxide::deflate::core::TDEFLFlush::Finish,
);
out_pos += bytes_out;
in_pos += bytes_in;
match status {
miniz_oxide::deflate::core::TDEFLStatus::Okay => {
// We need more space, so resize the vector.
if encoded.len().saturating_sub(out_pos) < 30 {
encoded.resize(encoded.len() * 2, 0);
}
}
miniz_oxide::deflate::core::TDEFLStatus::Done => {
encoded.truncate(out_pos);
assert!(
in_pos == data.len(),
"Zlib encode bug: consumed less input than expected"
);
return Ok(encoded);
}
#[allow(clippy::panic)] // this would be a bug and cannot be user-caused
err => panic!("Zlib encode bug: {err:?}"),
}
}
}
/// Decompress the `encoded` data into an array using Zlib.
///
/// # Errors
///
/// Errors with
/// - [`ZlibCodecError::HeaderDecodeFailed`] if decoding the header failed
/// - [`ZlibCodecError::DecodeExcessiveEncodedData`] if the encoded data
/// contains excessive trailing data junk
/// - [`ZlibCodecError::DecodeProducedLess`] if decoding produced less data than
/// expected
/// - [`ZlibCodecError::ZlibDecodeFailed`] if an opaque decoding error occurred
pub fn decompress(encoded: &[u8]) -> Result<AnyArray, ZlibCodecError> {
let (header, encoded) =
postcard::take_from_bytes::<CompressionHeader>(encoded).map_err(|err| {
ZlibCodecError::HeaderDecodeFailed {
source: ZlibHeaderError(err),
}
})?;
let (decoded, result) = AnyArray::with_zeros_bytes(header.dtype, &header.shape, |decoded| {
decompress_into_bytes(encoded, decoded)
});
result.map(|()| decoded)
}
/// Decompress the `encoded` data into a `decoded` array using Zlib.
///
/// # Errors
///
/// Errors with
/// - [`ZlibCodecError::HeaderDecodeFailed`] if decoding the header failed
/// - [`ZlibCodecError::MismatchedDecodeIntoArray`] if the `decoded` array is of
/// the wrong dtype or shape
/// - [`ZlibCodecError::HeaderDecodeFailed`] if decoding the header failed
/// - [`ZlibCodecError::DecodeExcessiveEncodedData`] if the encoded data
/// contains excessive trailing data junk
/// - [`ZlibCodecError::DecodeProducedLess`] if decoding produced less data than
/// expected
/// - [`ZlibCodecError::ZlibDecodeFailed`] if an opaque decoding error occurred
pub fn decompress_into(encoded: &[u8], mut decoded: AnyArrayViewMut) -> Result<(), ZlibCodecError> {
let (header, encoded) =
postcard::take_from_bytes::<CompressionHeader>(encoded).map_err(|err| {
ZlibCodecError::HeaderDecodeFailed {
source: ZlibHeaderError(err),
}
})?;
if header.dtype != decoded.dtype() {
return Err(ZlibCodecError::MismatchedDecodeIntoArray {
source: AnyArrayAssignError::DTypeMismatch {
src: header.dtype,
dst: decoded.dtype(),
},
});
}
if header.shape != decoded.shape() {
return Err(ZlibCodecError::MismatchedDecodeIntoArray {
source: AnyArrayAssignError::ShapeMismatch {
src: header.shape.into_owned(),
dst: decoded.shape().to_vec(),
},
});
}
decoded.with_bytes_mut(|decoded| decompress_into_bytes(encoded, decoded))
}
fn decompress_into_bytes(encoded: &[u8], decoded: &mut [u8]) -> Result<(), ZlibCodecError> {
let flags = miniz_oxide::inflate::core::inflate_flags::TINFL_FLAG_PARSE_ZLIB_HEADER
| miniz_oxide::inflate::core::inflate_flags::TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
let mut decomp = Box::<miniz_oxide::inflate::core::DecompressorOxide>::default();
let (status, in_consumed, out_consumed) =
miniz_oxide::inflate::core::decompress(&mut decomp, encoded, decoded, 0, flags);
match status {
miniz_oxide::inflate::TINFLStatus::Done => {
if in_consumed != encoded.len() {
Err(ZlibCodecError::DecodeExcessiveEncodedData)
} else if out_consumed == decoded.len() {
Ok(())
} else {
Err(ZlibCodecError::DecodeProducedLess)
}
}
status => Err(ZlibCodecError::ZlibDecodeFailed {
source: ZlibDecodeError(miniz_oxide::inflate::DecompressError {
status,
output: Vec::new(),
}),
}),
}
}
#[derive(Serialize, Deserialize)]
struct CompressionHeader<'a> {
dtype: AnyArrayDType,
#[serde(borrow)]
shape: Cow<'a, [usize]>,
}