Skip to content

Commit

Permalink
Allow for running without allocator (inflate-only) (#117)
Browse files Browse the repository at this point in the history
feat(miniz_oxide) Add `with-alloc` feature (enabled by default)
  • Loading branch information
mkeeter committed Jun 17, 2022
1 parent d0000c5 commit 96ad0b8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ name = "miniz_oxide_c_api"
[dependencies]
libc = "0.2.22"
crc32fast = "1.2.0"
miniz_oxide = { path = "miniz_oxide", version = "0.5.0" }
miniz_oxide = { path = "miniz_oxide", version = "0.6.0" }

[build-dependencies]
cc = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pure rust Rust replacement for the [miniz](https://github.com/richgel999/miniz)
This project is organized into a C API shell and a rust crate.
The Rust crate is found in the [miniz_oxide subdirectory](https://github.com/Frommi/miniz_oxide/tree/master/miniz_oxide).

miniz_oxide 0.5.x Requires at least rust 1.40.0, 0.3.x requires at least rust 0.36.0.
miniz_oxide 0.5.x and 0.6.x Requires at least rust 1.40.0, 0.3.x requires at least rust 0.36.0.

For a friendlier streaming API using readers and writers, [flate2](https://crates.io/crates/flate2) can be used, which can use miniz_oxide as a rust-only back-end.

Expand Down
5 changes: 3 additions & 2 deletions miniz_oxide/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "miniz_oxide"
authors = ["Frommi <daniil.liferenko@gmail.com>", "oyvindln <oyvindln@users.noreply.github.com>"]
version = "0.5.3"
version = "0.6.0"
license = "MIT OR Zlib OR Apache-2.0"
readme = "Readme.md"
keywords = ["zlib", "miniz", "deflate", "encoding"]
Expand All @@ -27,7 +27,8 @@ alloc = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-all
compiler_builtins = { version = '0.1.2', optional = true }

[features]
default = []
default = ["with-alloc"]
with-alloc = []

# Internal feature, only used when building as part of libstd, not part of the
# stable interface of this crate.
Expand Down
11 changes: 9 additions & 2 deletions miniz_oxide/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
A fully safe, pure rust replacement for the [miniz](https://github.com/richgel999/miniz) DEFLATE/zlib encoder/decoder.
The main intention of this crate is to be used as a back-end for the [flate2](https://github.com/alexcrichton/flate2-rs), but it can also be used on it's own. Using flate2 with the ```rust_backend``` feature provides an easy to use streaming API for miniz_oxide.

The library is fully [no_std](https://docs.rust-embedded.org/book/intro/no-std.html), though it requires the use of the `alloc` and `collection` crates as it allocates memory.
The library is fully [no_std](https://docs.rust-embedded.org/book/intro/no-std.html). By default, the `with-alloc` feature is enabled, which requires the use of the `alloc` and `collection` crates as it allocates memory.

miniz_oxide 0.5.x Requires at least rust 1.40.0 0.3.x requires at least rust 0.36.0.
Using the library with `default-features = false` removes the dependency on `alloc`
and `collection` crates, making it suitable for systems without an allocator.
Running without allocation reduces crate functionality:

- The `deflate` module is removed complete
- Some `inflate` functions which return a `Vec` are removed

miniz_oxide 0.5.x and 0.6.x Requires at least rust 1.40.0 0.3.x requires at least rust 0.36.0.

miniz_oxide features no use of unsafe code.

Expand Down
13 changes: 8 additions & 5 deletions miniz_oxide/src/inflate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! This module contains functionality for decompression.

use crate::alloc::boxed::Box;
use crate::alloc::vec;
use crate::alloc::vec::Vec;
use ::core::cmp::min;
#[cfg(feature = "with-alloc")]
use crate::alloc::{boxed::Box, vec, vec::Vec};
use ::core::usize;

pub mod core;
Expand Down Expand Up @@ -82,6 +80,7 @@ impl TINFLStatus {
///
/// Returns a tuple of the [`Vec`] of decompressed data and the [status result][TINFLStatus].
#[inline]
#[cfg(feature = "with-alloc")]
pub fn decompress_to_vec(input: &[u8]) -> Result<Vec<u8>, TINFLStatus> {
decompress_to_vec_inner(input, 0, usize::max_value())
}
Expand All @@ -90,6 +89,7 @@ pub fn decompress_to_vec(input: &[u8]) -> Result<Vec<u8>, TINFLStatus> {
///
/// Returns a tuple of the [`Vec`] of decompressed data and the [status result][TINFLStatus].
#[inline]
#[cfg(feature = "with-alloc")]
pub fn decompress_to_vec_zlib(input: &[u8]) -> Result<Vec<u8>, TINFLStatus> {
decompress_to_vec_inner(
input,
Expand All @@ -104,6 +104,7 @@ pub fn decompress_to_vec_zlib(input: &[u8]) -> Result<Vec<u8>, TINFLStatus> {
///
/// Returns a tuple of the [`Vec`] of decompressed data and the [status result][TINFLStatus].
#[inline]
#[cfg(feature = "with-alloc")]
pub fn decompress_to_vec_with_limit(input: &[u8], max_size: usize) -> Result<Vec<u8>, TINFLStatus> {
decompress_to_vec_inner(input, 0, max_size)
}
Expand All @@ -114,6 +115,7 @@ pub fn decompress_to_vec_with_limit(input: &[u8], max_size: usize) -> Result<Vec
///
/// Returns a tuple of the [`Vec`] of decompressed data and the [status result][TINFLStatus].
#[inline]
#[cfg(feature = "with-alloc")]
pub fn decompress_to_vec_zlib_with_limit(
input: &[u8],
max_size: usize,
Expand All @@ -124,13 +126,14 @@ pub fn decompress_to_vec_zlib_with_limit(
/// Backend of various to-[`Vec`] decompressions.
///
/// Returns a tuple of the [`Vec`] of decompressed data and the [status result][TINFLStatus].
#[cfg(feature = "with-alloc")]
fn decompress_to_vec_inner(
input: &[u8],
flags: u32,
max_output_size: usize,
) -> Result<Vec<u8>, TINFLStatus> {
let flags = flags | inflate_flags::TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
let mut ret: Vec<u8> = vec![0; min(input.len().saturating_mul(2), max_output_size)];
let mut ret: Vec<u8> = vec![0; input.len().saturating_mul(2).min(max_output_size)];

let mut decomp = Box::<DecompressorOxide>::default();

Expand Down
3 changes: 3 additions & 0 deletions miniz_oxide/src/inflate/stream.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Extra streaming decompression functionality.
//!
//! As of now this is mainly intended for use to build a higher-level wrapper.
#[cfg(feature = "with-alloc")]
use crate::alloc::boxed::Box;
use core::{cmp, mem};

Expand Down Expand Up @@ -115,6 +116,7 @@ impl InflateState {
/// # Parameters
/// `data_format`: Determines whether the compressed data is assumed to wrapped with zlib
/// metadata.
#[cfg(feature = "with-alloc")]
pub fn new_boxed(data_format: DataFormat) -> Box<InflateState> {
let mut b: Box<InflateState> = Box::default();
b.data_format = data_format;
Expand All @@ -136,6 +138,7 @@ impl InflateState {
/// The decompressor does not support different window sizes. As such,
/// any positive (>0) value will set the zlib header flag, while a negative one
/// will not.
#[cfg(feature = "with-alloc")]
pub fn new_boxed_with_window_bits(window_bits: i32) -> Box<InflateState> {
let mut b: Box<InflateState> = Box::default();
b.data_format = DataFormat::from_window_bits(window_bits);
Expand Down
8 changes: 5 additions & 3 deletions miniz_oxide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
#![forbid(unsafe_code)]
#![no_std]

#[cfg(feature = "with-alloc")]
extern crate alloc;

#[cfg(feature = "with-alloc")]
pub mod deflate;
pub mod inflate;
mod shared;
Expand Down Expand Up @@ -154,15 +156,15 @@ pub enum DataFormat {
}

impl DataFormat {
pub(crate) fn from_window_bits(window_bits: i32) -> DataFormat {
pub fn from_window_bits(window_bits: i32) -> DataFormat {
if window_bits > 0 {
DataFormat::Zlib
} else {
DataFormat::Raw
}
}

pub(crate) fn to_window_bits(self) -> i32 {
pub fn to_window_bits(self) -> i32 {
match self {
DataFormat::Zlib | DataFormat::ZLibIgnoreChecksum => shared::MZ_DEFAULT_WINDOW_BITS,
DataFormat::Raw => -shared::MZ_DEFAULT_WINDOW_BITS,
Expand All @@ -186,7 +188,7 @@ pub struct StreamResult {

impl StreamResult {
#[inline]
pub(crate) const fn error(error: MZError) -> StreamResult {
pub const fn error(error: MZError) -> StreamResult {
StreamResult {
bytes_consumed: 0,
bytes_written: 0,
Expand Down

0 comments on commit 96ad0b8

Please sign in to comment.