use lzma_sys::*;
use error::{LzmaError, LzmaLibResult};
use std::ptr;
use std::ops::Drop;
pub struct LzmaStreamWrapper {
stream: lzma_stream,
}
pub struct LzmaCodeResult {
pub ret: LzmaLibResult,
pub bytes_read: usize,
pub bytes_written: usize,
}
unsafe impl Send for LzmaStreamWrapper {}
impl LzmaStreamWrapper {
pub fn new() -> LzmaStreamWrapper {
LzmaStreamWrapper {
stream: lzma_stream::new(),
}
}
pub fn easy_encoder(&mut self, preset: u32, check: lzma_check) -> Result<(), LzmaError> {
unsafe {
LzmaLibResult::from(lzma_easy_encoder(&mut self.stream, preset, check)).map(|_| ())
}
}
pub fn stream_decoder(&mut self, memlimit: u64, flags: u32) -> Result<(), LzmaError> {
unsafe {
LzmaLibResult::from(lzma_auto_decoder(&mut self.stream, memlimit, flags)).map(|_| ())
}
}
pub fn end(&mut self) {
unsafe {
lzma_end(&mut self.stream)
}
}
pub fn code(&mut self, input: &[u8], output: &mut [u8], action: lzma_action) -> LzmaCodeResult {
self.stream.next_in = input.as_ptr();
self.stream.avail_in = input.len();
self.stream.next_out = output.as_mut_ptr();
self.stream.avail_out = output.len();
let ret = unsafe {
LzmaLibResult::from(lzma_code(&mut self.stream, action))
};
let bytes_read = input.len() - self.stream.avail_in;
let bytes_written = output.len() - self.stream.avail_out;
self.stream.next_in = ptr::null();
self.stream.avail_in = 0;
self.stream.next_out = ptr::null_mut();
self.stream.avail_out = 0;
LzmaCodeResult {
ret,
bytes_read,
bytes_written,
}
}
}
impl Drop for LzmaStreamWrapper {
fn drop(&mut self) {
self.end();
}
}