Skip to content
This repository was archived by the owner on Apr 19, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hard_tabs = true
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
[package]
name = "snappy"
name = "parity-snappy"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
license = "GPL-3.0"
repository = "https://github.com/paritytech/rust-snappy.git"
description = "Rust bindings for the snappy compression library"
keywords = ["snappy"]

[dependencies]
libc = "0.2"
snappy-sys = { path = "snappy-sys" }
parity-snappy-sys = { path = "snappy-sys", version = "0.1" }

[dev-dependencies]
rand = "0.5"
675 changes: 675 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
# snappy

Rust bindings for the snappy compression library.
# parity-snappy

[![Build Status](https://travis-ci.org/paritytech/rust-snappy.svg?branch=master)](https://travis-ci.org/paritytech/rust-snappy)
[![Build status](https://ci.appveyor.com/api/projects/status/rr3ipesm4qqwv7y1?svg=true)](https://ci.appveyor.com/project/andresilva/rust-snappy)

Rust bindings for the snappy compression library.

Currently this library uses snappy v1.1.7. The source for snappy is included in the `parity-snappy-sys` crate, so
there's no need to pre-install snappy, and the library will be statically linked.

## Example

```rust
use parity_snappy as snappy;

let input: Vec<u8> = ...;
let compressed = snappy::compress(&input);
let decompressed = snappy::decompress(&compressed);

assert_eq!(decompressed == input);
```

```rust
use parity_snappy as snappy;

let input: Vec<u8> = ...;
let mut compressed = Vec::with_capacity(snappy::max_compressed_len(input.len()));
let mut decompressed = Vec::with_capacity(input.len());

let len = snappy::compress_into(&input, &mut compressed);
let _ = snappy::decompress_into(&compressed[..len], &mut decompressed);

assert_eq!(decompressed == input);
```
20 changes: 18 additions & 2 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

#![feature(test)]
extern crate test;

extern crate parity_snappy as snappy;
extern crate rand;
extern crate snappy;

#[cfg(test)]
mod tests {
use test::Bencher;
use rand::prelude::*;
use snappy;
use test::Bencher;

const INPUT_SIZE: usize = 1 << 19;

Expand Down
8 changes: 4 additions & 4 deletions snappy-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "snappy-sys"
name = "parity-snappy-sys"
version = "0.1.0"
authors = ["Arkadiy Paronyan arkadiy@ethcore.io>"]
license = "MIT/Apache-2.0/BSD"
authors = ["Parity Technologies <admin@parity.io>"]
license = "GPL-3.0"
repository = "https://github.com/paritytech/rust-snappy.git"
description = "Native bindings to libsnappy"
readme = "README.md"
keywords = ["ffi", "snappy"]

build = "build.rs"
Expand Down
3 changes: 1 addition & 2 deletions snappy-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -51,4 +51,3 @@ extern {
compressed_len: size_t,
) -> c_int;
}

29 changes: 21 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
Expand All @@ -15,13 +15,13 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Snappy compression bindings.
extern crate snappy_sys;
extern crate libc;
extern crate parity_snappy_sys;
#[cfg(test)]
extern crate rand;

use snappy_sys as snappy;
use libc::{c_char, size_t};
use parity_snappy_sys as snappy;
use std::fmt;

/// Attempted to decompress an uncompressed buffer.
Expand Down Expand Up @@ -50,7 +50,9 @@ pub fn decompressed_len(compressed: &[u8]) -> Result<usize, InvalidInput> {
let mut size: size_t = 0;
let len = compressed.len() as size_t;

let status = unsafe { snappy::snappy_uncompressed_length(compressed.as_ptr() as *const c_char, len, &mut size) };
let status = unsafe {
snappy::snappy_uncompressed_length(compressed.as_ptr() as *const c_char, len, &mut size)
};

if status == snappy::SNAPPY_INVALID_INPUT {
Err(InvalidInput)
Expand Down Expand Up @@ -88,8 +90,12 @@ pub fn compress_into(input: &[u8], output: &mut Vec<u8>) -> usize {

match status {
snappy::SNAPPY_OK => len,
snappy::SNAPPY_INVALID_INPUT => panic!("snappy compression has no concept of invalid input"),
snappy::SNAPPY_BUFFER_TOO_SMALL => panic!("buffer cannot be too small, the capacity was just ensured."),
snappy::SNAPPY_INVALID_INPUT => {
panic!("snappy compression has no concept of invalid input")
}
snappy::SNAPPY_BUFFER_TOO_SMALL => {
panic!("buffer cannot be too small, the capacity was just ensured.")
}
_ => panic!("snappy returned unspecified status"),
}
}
Expand Down Expand Up @@ -123,14 +129,21 @@ pub fn decompress_into(input: &[u8], output: &mut Vec<u8>) -> Result<usize, Inva
match status {
snappy::SNAPPY_OK => Ok(len as usize),
snappy::SNAPPY_INVALID_INPUT => Err(InvalidInput),
snappy::SNAPPY_BUFFER_TOO_SMALL => panic!("buffer cannot be too small, size was just set to large enough."),
snappy::SNAPPY_BUFFER_TOO_SMALL => {
panic!("buffer cannot be too small, size was just set to large enough.")
}
_ => panic!("snappy returned unspecified status"),
}
}

/// Validate a compressed buffer. True if valid, false if not.
pub fn validate_compressed_buffer(input: &[u8]) -> bool {
let status = unsafe { snappy::snappy_validate_compressed_buffer(input.as_ptr() as *const c_char, input.len() as size_t )};
let status = unsafe {
snappy::snappy_validate_compressed_buffer(
input.as_ptr() as *const c_char,
input.len() as size_t,
)
};
status == snappy::SNAPPY_OK
}

Expand Down