Skip to content

Commit

Permalink
Move bindings to zstd-sys crate
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Mar 24, 2017
1 parent fbff57a commit e2e7533
Show file tree
Hide file tree
Showing 19 changed files with 1,038 additions and 472 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "zstd"]
path = zstd
path = zstd-sys/zstd
url = https://github.com/facebook/zstd
10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
[package]
authors = ["Alexandre Bury <[email protected]>"]
build = "src/build.rs"
description = "Binding for the zstd compression library."
documentation = "https://docs.rs/zstd"
keywords = ["zstd", "zstandard", "compression"]
categories = ["compression", "api-bindings"]
license = "MIT"
name = "zstd"
repository = "https://github.com/gyscos/zstd-rs"
version = "0.4.4"
version = "0.4.5"
exclude = ["assets"]

[badges]
travis-ci = { repository = "gyscos/zstd-rs" }

[build-dependencies]
gcc = "0.3.28"
glob = "0.2.11"

[dependencies]
libc = "0.2"
zstd-sys = { version = "1.1.4", default-features = false }

[dev-dependencies]
clap = "2.6.0"

[features]
default = ["legacy"]
legacy = []
legacy = ["zstd-sys/legacy"]
6 changes: 3 additions & 3 deletions examples/stream.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
extern crate zstd;

use std::env;
use std::str::FromStr;
use std::io::{self, Write};
use std::str::FromStr;

fn main() {
match env::args().skip(1).next() {
None => {
writeln!(&mut io::stderr(),
"Invalid option. Usage: `stream [-d|-1..-21]`")
.unwrap();
.unwrap();
}
Some(ref option) if option == "-d" => decompress(),
Some(ref option) => {
Expand All @@ -22,7 +22,7 @@ fn main() {
} else {
writeln!(&mut io::stderr(),
"Invalid option. Usage: `stream [-d|-1..-21]`")
.unwrap();
.unwrap();
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions examples/train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ extern crate zstd;
#[macro_use]
extern crate clap;

use std::io;

use clap::{App, Arg};
use std::io;

// This program trains a dictionary from one or more files,
// to make future compression of similar small files more efficient.
Expand All @@ -17,14 +17,14 @@ fn main() {
.author("Alexandre Bury <[email protected]>")
.about("A zstd dict trainer")
.arg(Arg::with_name("MAX_SIZE")
.help("Maximum dictionary size in bytes")
.short("s")
.long("max_size")
.takes_value(true))
.help("Maximum dictionary size in bytes")
.short("s")
.long("max_size")
.takes_value(true))
.arg(Arg::with_name("FILE")
.help("Files to use as input")
.required(true)
.multiple(true))
.help("Files to use as input")
.required(true)
.multiple(true))
.get_matches();

let size = value_t!(matches, "MAX_SIZE", usize).unwrap_or(110 * 1024);
Expand Down
2 changes: 1 addition & 1 deletion examples/zstdcat.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extern crate zstd;
extern crate clap;

use clap::{App, Arg};
use std::fs;
use std::io;
use clap::{App, Arg};

fn main() {
// This will be a simple application:
Expand Down
31 changes: 17 additions & 14 deletions src/block/compressor.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use ll;
use ::parse_code;
use libc::c_void;
use parse_code;

use std::io;
use zstd_sys;

struct EncoderContext {
c: *mut ll::ZSTD_CCtx,
c: *mut zstd_sys::ZSTD_CCtx,
}

impl Default for EncoderContext {
fn default() -> Self {
EncoderContext { c: unsafe { ll::ZSTD_createCCtx() } }
EncoderContext { c: unsafe { zstd_sys::ZSTD_createCCtx() } }
}
}

impl Drop for EncoderContext {
fn drop(&mut self) {
let code = unsafe { ll::ZSTD_freeCCtx(self.c) };
let code = unsafe { zstd_sys::ZSTD_freeCCtx(self.c) };
parse_code(code).unwrap();
}
}
Expand Down Expand Up @@ -52,22 +53,24 @@ impl Compressor {
destination: &mut [u8], level: i32)
-> io::Result<usize> {
let code = unsafe {
ll::ZSTD_compress_usingDict(self.context.c,
destination.as_mut_ptr(),
destination.len(),
source.as_ptr(),
source.len(),
self.dict.as_ptr(),
self.dict.len(),
level)
zstd_sys::ZSTD_compress_usingDict(self.context.c,
destination.as_mut_ptr() as
*mut c_void,
destination.len(),
source.as_ptr() as *mut c_void,
source.len(),
self.dict.as_ptr() as
*mut c_void,
self.dict.len(),
level)
};
parse_code(code)
}

/// Compresses a block of data and returns the compressed result.
pub fn compress(&mut self, data: &[u8], lvl: i32) -> io::Result<Vec<u8>> {
// We allocate a big buffer, slightly larger than the input data.
let buffer_len = unsafe { ll::ZSTD_compressBound(data.len()) };
let buffer_len = unsafe { zstd_sys::ZSTD_compressBound(data.len()) };
let mut buffer = Vec::with_capacity(buffer_len);
unsafe {
// Use all capacity.
Expand Down
28 changes: 16 additions & 12 deletions src/block/decompressor.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use ll;
use ::parse_code;
use libc::c_void;
use parse_code;

use std::io;
use zstd_sys;

struct DecoderContext {
c: *mut ll::ZSTD_DCtx,
c: *mut zstd_sys::ZSTD_DCtx,
}

impl Default for DecoderContext {
fn default() -> Self {
DecoderContext { c: unsafe { ll::ZSTD_createDCtx() } }
DecoderContext { c: unsafe { zstd_sys::ZSTD_createDCtx() } }
}
}

impl Drop for DecoderContext {
fn drop(&mut self) {
let code = unsafe { ll::ZSTD_freeDCtx(self.c) };
let code = unsafe { zstd_sys::ZSTD_freeDCtx(self.c) };
parse_code(code).unwrap();
}
}
Expand Down Expand Up @@ -51,13 +52,16 @@ impl Decompressor {
destination: &mut [u8])
-> io::Result<usize> {
let code = unsafe {
ll::ZSTD_decompress_usingDict(self.context.c,
destination.as_mut_ptr(),
destination.len(),
source.as_ptr(),
source.len(),
self.dict.as_ptr(),
self.dict.len())
let destination_ptr = destination.as_mut_ptr() as *mut c_void;
let source_ptr = source.as_ptr() as *const c_void;
let dict_ptr = self.dict.as_ptr() as *const c_void;
zstd_sys::ZSTD_decompress_usingDict(self.context.c,
destination_ptr,
destination.len(),
source_ptr,
source.len(),
dict_ptr,
self.dict.len())
};
parse_code(code)
}
Expand Down
30 changes: 17 additions & 13 deletions src/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
//! [`Encoder::with_dictionary`]: ../struct.Encoder.html#method.with_dictionary
//! [`Decoder::with_dictionary`]: ../struct.Decoder.html#method.with_dictionary
use ll;
use ::parse_code;
use libc::{c_uint, c_void};
use parse_code;
use std::fs;

use std::io::{self, Read};
use std::path;
use std::fs;
use zstd_sys;

/// Train a dictionary from a big continuous chunk of data.
///
Expand All @@ -36,11 +37,14 @@ pub fn from_continuous(sample_data: &[u8], sample_sizes: &[usize],

let mut result = Vec::with_capacity(max_size);
unsafe {
let code = ll::ZDICT_trainFromBuffer(result.as_mut_ptr(),
result.capacity(),
sample_data.as_ptr(),
sample_sizes.as_ptr(),
sample_sizes.len());
let result_ptr = result.as_mut_ptr() as *mut c_void;
let sample_ptr = sample_data.as_ptr() as *const c_void;
let code = zstd_sys::ZDICT_trainFromBuffer(result_ptr,
result.capacity(),
sample_ptr,
sample_sizes.as_ptr(),
sample_sizes.len() as
c_uint);
let written = try!(parse_code(code));
result.set_len(written);
}
Expand Down Expand Up @@ -112,16 +116,16 @@ mod tests {
&mut ::stream::Encoder::with_dictionary(&mut buffer,
1,
&dict)
.unwrap()
.auto_finish())
.unwrap();
.unwrap()
.auto_finish())
.unwrap();

let mut result = Vec::new();
io::copy(&mut ::stream::Decoder::with_dictionary(&buffer[..],
&dict[..])
.unwrap(),
.unwrap(),
&mut result)
.unwrap();
.unwrap();

assert_eq!(&content, &result);
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@
#![deny(missing_docs)]
extern crate libc;

mod ll;
extern crate zstd_sys;

pub mod stream;
pub mod block;
pub mod dict;

#[doc(no_inline)]
pub use stream::{Decoder, Encoder, decode_all, encode_all};
use std::ffi::CStr;

use std::io;
use std::ffi::CStr;
#[doc(no_inline)]
pub use stream::{Decoder, Encoder, decode_all, encode_all};

/// Parse the result code
///
/// Returns the number of bytes written if the code represents success,
/// or the error message otherwise.
fn parse_code(code: libc::size_t) -> Result<usize, io::Error> {
unsafe {
if ll::ZSTD_isError(code) == 0 {
if zstd_sys::ZSTD_isError(code) == 0 {
Ok(code as usize)
} else {
let msg = CStr::from_ptr(ll::ZSTD_getErrorName(code));
let msg = CStr::from_ptr(zstd_sys::ZSTD_getErrorName(code));
let error = io::Error::new(io::ErrorKind::Other,
msg.to_str().unwrap().to_string());
Err(error)
Expand Down
Loading

0 comments on commit e2e7533

Please sign in to comment.