2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-06-13 17:44:50 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
//! Snappy compression bindings.
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use libc::{c_char, c_int, size_t};
|
|
|
|
|
|
|
|
const SNAPPY_OK: c_int = 0;
|
|
|
|
const SNAPPY_INVALID_INPUT: c_int = 1;
|
|
|
|
const SNAPPY_BUFFER_TOO_SMALL: c_int = 2;
|
|
|
|
|
|
|
|
#[link(name = "snappy")]
|
|
|
|
extern {
|
|
|
|
fn snappy_compress(
|
|
|
|
input: *const c_char,
|
|
|
|
input_len: size_t,
|
|
|
|
compressed: *mut c_char,
|
|
|
|
compressed_len: *mut size_t
|
|
|
|
) -> c_int;
|
|
|
|
|
|
|
|
fn snappy_max_compressed_length(source_len: size_t) -> size_t;
|
|
|
|
|
|
|
|
fn snappy_uncompress(
|
|
|
|
compressed: *const c_char,
|
|
|
|
compressed_len: size_t,
|
|
|
|
uncompressed: *mut c_char,
|
|
|
|
uncompressed_len: *mut size_t,
|
|
|
|
) -> c_int;
|
|
|
|
|
|
|
|
fn snappy_uncompressed_length(
|
|
|
|
compressed: *const c_char,
|
|
|
|
compressed_len: size_t,
|
|
|
|
result: *mut size_t,
|
|
|
|
) -> c_int;
|
2016-06-15 19:14:46 +02:00
|
|
|
|
|
|
|
fn snappy_validate_compressed_buffer(
|
|
|
|
compressed: *const c_char,
|
|
|
|
compressed_len: size_t,
|
|
|
|
) -> c_int;
|
2016-06-13 17:44:50 +02:00
|
|
|
}
|
|
|
|
|
2016-06-16 13:29:24 +02:00
|
|
|
/// Attempted to decompress an uncompressed buffer.
|
2016-06-13 17:44:50 +02:00
|
|
|
#[derive(Debug)]
|
2016-06-16 13:29:24 +02:00
|
|
|
pub struct InvalidInput;
|
2016-06-13 17:44:50 +02:00
|
|
|
|
2016-06-16 13:29:24 +02:00
|
|
|
impl fmt::Display for InvalidInput {
|
2016-06-13 17:44:50 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-06-16 13:29:24 +02:00
|
|
|
write!(f, "Attempted snappy decompression with invalid input")
|
2016-06-13 17:44:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 13:22:15 +02:00
|
|
|
/// The maximum compressed length given a size.
|
|
|
|
pub fn max_compressed_len(len: usize) -> usize {
|
|
|
|
unsafe { snappy_max_compressed_length(len as size_t) as usize }
|
|
|
|
}
|
|
|
|
|
2016-06-15 17:46:40 +02:00
|
|
|
/// How large the given data will be when decompressed.
|
2016-06-16 13:29:24 +02:00
|
|
|
pub fn decompressed_len(compressed: &[u8]) -> Result<usize, InvalidInput> {
|
2016-06-15 17:46:40 +02:00
|
|
|
let mut size: size_t = 0;
|
|
|
|
let len = compressed.len() as size_t;
|
|
|
|
|
|
|
|
let status = unsafe { snappy_uncompressed_length(compressed.as_ptr() as *const c_char, len, &mut size) };
|
|
|
|
|
|
|
|
if status == SNAPPY_INVALID_INPUT {
|
2016-06-16 13:29:24 +02:00
|
|
|
Err(InvalidInput)
|
2016-06-15 17:46:40 +02:00
|
|
|
} else {
|
2016-06-16 13:29:24 +02:00
|
|
|
Ok(size)
|
2016-06-15 17:46:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 17:44:50 +02:00
|
|
|
/// Compress a buffer using snappy.
|
|
|
|
pub fn compress(input: &[u8]) -> Vec<u8> {
|
2016-06-16 12:57:57 +02:00
|
|
|
let mut buf = Vec::new();
|
2016-06-16 13:29:24 +02:00
|
|
|
compress_into(input, &mut buf);
|
2016-06-16 12:57:57 +02:00
|
|
|
buf
|
2016-06-13 17:44:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Compress a buffer using snappy, writing the result into
|
2016-06-16 12:57:57 +02:00
|
|
|
/// the given output buffer, growing it if necessary.
|
2016-06-13 17:44:50 +02:00
|
|
|
/// Otherwise, returns the length of the compressed data.
|
2016-06-16 13:29:24 +02:00
|
|
|
pub fn compress_into(input: &[u8], output: &mut Vec<u8>) -> usize {
|
2016-06-16 12:57:57 +02:00
|
|
|
let mut len = max_compressed_len(input.len());
|
|
|
|
|
|
|
|
if output.len() < len {
|
|
|
|
output.resize(len, 0);
|
|
|
|
}
|
|
|
|
|
2016-06-13 17:44:50 +02:00
|
|
|
let status = unsafe {
|
|
|
|
snappy_compress(
|
|
|
|
input.as_ptr() as *const c_char,
|
|
|
|
input.len() as size_t,
|
|
|
|
output.as_mut_ptr() as *mut c_char,
|
2016-06-16 12:57:57 +02:00
|
|
|
&mut len as &mut size_t,
|
2016-06-13 17:44:50 +02:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
match status {
|
2016-06-16 13:29:24 +02:00
|
|
|
SNAPPY_OK => len,
|
|
|
|
SNAPPY_INVALID_INPUT => panic!("snappy compression has no concept of invalid input"),
|
2016-06-16 12:57:57 +02:00
|
|
|
SNAPPY_BUFFER_TOO_SMALL => panic!("buffer cannot be too small, the capacity was just ensured."),
|
2016-06-13 17:44:50 +02:00
|
|
|
_ => panic!("snappy returned unspecified status"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Decompress a buffer using snappy. Will return an error if the buffer is not snappy-compressed.
|
2016-06-16 13:29:24 +02:00
|
|
|
pub fn decompress(input: &[u8]) -> Result<Vec<u8>, InvalidInput> {
|
2016-06-16 12:57:57 +02:00
|
|
|
let mut v = Vec::new();
|
|
|
|
decompress_into(input, &mut v).map(|_| v)
|
2016-06-13 17:44:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Decompress a buffer using snappy, writing the result into
|
2016-06-16 12:57:57 +02:00
|
|
|
/// the given output buffer, growing it if necessary.
|
|
|
|
/// Will error if the input buffer is not snappy-compressed.
|
2016-06-13 17:44:50 +02:00
|
|
|
/// Otherwise, returns the length of the decompressed data.
|
2016-06-16 13:29:24 +02:00
|
|
|
pub fn decompress_into(input: &[u8], output: &mut Vec<u8>) -> Result<usize, InvalidInput> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let mut len = decompressed_len(input)?;
|
2016-06-16 12:57:57 +02:00
|
|
|
|
|
|
|
if output.len() < len {
|
|
|
|
output.resize(len, 0);
|
|
|
|
}
|
|
|
|
|
2016-06-13 17:44:50 +02:00
|
|
|
let status = unsafe {
|
|
|
|
snappy_uncompress(
|
|
|
|
input.as_ptr() as *const c_char,
|
|
|
|
input.len() as size_t,
|
|
|
|
output.as_mut_ptr() as *mut c_char,
|
2016-06-16 12:57:57 +02:00
|
|
|
&mut len as &mut size_t,
|
2016-06-13 17:44:50 +02:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
match status {
|
|
|
|
SNAPPY_OK => Ok(len as usize),
|
2016-06-16 13:29:24 +02:00
|
|
|
SNAPPY_INVALID_INPUT => Err(InvalidInput),
|
2016-06-16 12:57:57 +02:00
|
|
|
SNAPPY_BUFFER_TOO_SMALL => panic!("buffer cannot be too small, size was just set to large enough."),
|
2016-06-13 17:44:50 +02:00
|
|
|
_ => panic!("snappy returned unspecified status"),
|
|
|
|
}
|
2016-06-15 19:14:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Validate a compressed buffer. True if valid, false if not.
|
|
|
|
pub fn validate_compressed_buffer(input: &[u8]) -> bool {
|
|
|
|
let status = unsafe { snappy_validate_compressed_buffer(input.as_ptr() as *const c_char, input.len() as size_t )};
|
|
|
|
status == SNAPPY_OK
|
2016-06-13 17:44:50 +02:00
|
|
|
}
|