K6L5F26WJMIYPP3Y3CBBUYMAK7C5SVR7NYFNU45NGU5NMDCSJJIQC
HA3ODU4GEQ4X55UDTY3QZUTD557DHTIHH2B5CSMY7YNHOBYQARBQC
SZHL3TSDHAQ3C4JKQRNYFDWOSXURPIN72HKTXQRY7YCABQDJE5FQC
HEFGMMUMJ2GGJGF2BEKF7EUL24AO53WJTXKBJSEGT64AV3Y4TBZQC
IH334Q5ACWE4TNQYYOOF6GWV6CRXOEM6542NVNPA6HRIZ3CBFKEAC
HELRYMZKHZW4S2IEYXA4IE7VOYYL7FH6TVHGFHJ4J3KRBBLHPQPAC
IB4L4N5M765IYALNZENJRKOGUPBRK2AMAB6IRNWPDCKNKKRMOB5AC
Error::Null => write!(fmt, "Null"),
Error::Io(ref e) => e.fmt(fmt),
Error::CouldNotOpenFile(ref f) => write!(fmt, "Could not open {}", f),
Error::CouldNotOpenFile(ref f) => write!(fmt, "Could not open file {}.", f),
Error::Other(ref s) => write!(fmt, "{}", s),
Error::Io(ref e) => e.fmt(fmt),
Error::Null => write!(fmt, "Null."),
Error::FIndexTooLarge(a, b) => write!(
fmt,
"Frame index too large. Expected at most {}, found {}.",
a, b
),
Error::Generic => write!(fmt, "Generic error from ZSTD."),
Error::InvalidConversion => write!(fmt, "Invalid slice to convert from bytes to u32."),
Error::FParamUnsupported(a, b) => write!(
fmt,
"Frame parameter not supported. Expected at most {}, found {}.",
a, b
),
Error::PrefixUnknown(p) => write!(fmt, "Prefix unknown {}", p),
Error::Corruption(ref s) => write!(fmt, "Corruption detected {}.", s),
Error::DSizeTooSmall(a, b) => write!(
fmt,
"Destination size too small. Expected at least {}, found {}.",
b, a
),
let result = unsafe { ZSTD_initCStream(cstream, level as c_int) };
if unsafe { ZSTD_isError(result) } != 0 {
return Err(Error::ZSTD(result));
}
unsafe {
let result = ZSTD_initCStream(cstream, level as c_int);
if ZSTD_isError(result) != 0 {
return Err(Error::ZSTD(result));
}
};
pub fn end_stream(&mut self, output: &mut ZSTD_outBuffer) -> Result<usize, Error> {
pub fn end_stream(&mut self, output: &mut [u8]) -> Result<usize, Error> {
let mut output_ = ZSTD_outBuffer {
dst: output.as_mut_ptr() as *mut c_void,
size: output.len() as size_t,
pos: 0,
};
let result = unsafe { ZSTD_endStream(self.p, &mut output) };
if unsafe { ZSTD_isError(result) } != 0 {
return Err(Error::ZSTD(result));
unsafe {
let result = ZSTD_endStream(self.p, &mut output);
if ZSTD_isError(result) != 0 {
return Err(Error::ZSTD(result));
}
if slice_to_num(&self.in_buff[..4])? as usize != (MAGIC_SKIPPABLE_START | 0xE) {
return Err(Error::Other("PrefixUnknown"));
let mut prefix = slice_to_num(&self.in_buff[..4])?;
if prefix != (MAGIC_SKIPPABLE_START | 0xE) {
return Err(Error::PrefixUnknown(prefix));
if slice_to_num(&self.in_buff[4..8])? as usize + SKIPPABLE_HEADER_SIZE != frame_size {
return Err(Error::Other("PrefixUnknown"));
prefix = slice_to_num(&self.in_buff[4..8])?;
if prefix as usize + SKIPPABLE_HEADER_SIZE != frame_size {
return Err(Error::PrefixUnknown(prefix));
// FIXME: if pointer arithmetic replace, this needs to change
handle.read_exact(unsafe {
slice::from_raw_parts_mut(buffer_ptr.add(offset), to_read)
})?;
// FIXME: if pointer arithmetic were to be replaced, this needs to change
handle
.read(unsafe { slice::from_raw_parts_mut(buffer_ptr.add(offset), to_read) })?;
entries[idx].c_offset = c_offset;
entries[idx].d_offset = d_offset;
c_offset += slice_to_num(&self.in_buff[pos..pos + 4])? as usize;
entries.push(SeekEntry {
c_offset,
d_offset,
checksum: 0,
});
let a = slice_to_num(&self.in_buff[pos..pos + 4])? as usize;
c_offset += a;
let dstream_init = unsafe { ZSTD_initDStream(self.dstream) };
if unsafe { ZSTD_isError(dstream_init) } != 0 {
Err(Error::ZSTD(dstream_init))
} else {
Ok(())
unsafe {
let dstream_init = ZSTD_initDStream(self.dstream);
if ZSTD_isError(dstream_init) != 0 {
return Err(Error::ZSTD(dstream_init));
} else {
Ok(())
}
let mut to_read =
unsafe { ZSTD_decompressStream(self.dstream, &mut out_tmp, &mut self.inn) };
if unsafe { ZSTD_isError(to_read) } != 0 {
return Err(Error::ZSTD(to_read));
}
let mut to_read;
unsafe {
to_read = ZSTD_decompressStream(self.dstream, &mut out_tmp, &mut self.inn);
if ZSTD_isError(to_read) != 0 {
return Err(Error::ZSTD(to_read));
}
};
let mut seekable = Seekable::make_seekable(source, ptr::null_mut());
seekable.init_advanced()?;
Ok(seekable)
}
}
impl<R: io::Read + io::Seek> Seekable<R> {
/// Initialise a decompressor with a file. This method opens the file, and dropping the resulting `Seekable` closes the file.
pub fn init(source: R) -> Result<Self, Error> {
pub fn stwrite32(&self, output: &mut ZSTD_outBuffer, value: usize, offset: usize) -> usize {
if (self.seek_table_pos as usize) < offset + 4 {
let tmp = value.to_ne_bytes();
pub fn stwrite32(&mut self, output: &mut ZSTD_outBuffer, value: u32, offset: u32) -> usize {
if self.seek_table_pos < offset + 4 {
ptr::copy_nonoverlapping(
tmp.as_ptr().add(self.seek_table_pos as usize - offset) as *const usize,
output.dst.add(output.pos) as *mut usize,
len_write,
)
};
let base = output.dst.add(output.pos) as *mut u8;
ptr::copy(value.to_ne_bytes().as_ptr(),base,len_write);
}
#[test]
fn test() {
let mut cstream = SeekableCStream::new(10, 256).unwrap();
let input = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut diam ante, sollicitudin a dolor et, volutpat elementum nulla. Etiam nec efficitur nibh, quis rutrum risus. Maecenas quis lorem malesuada, aliquet mi vel, viverra nunc. Donec et nulla sed velit sagittis varius. Suspendisse vestibulum, neque lobortis ornare vestibulum, orci turpis vulputate nisi, ut sodales neque purus eget magna. Nunc condimentum, diam eu consequat venenatis, est nisl semper lorem, et lobortis velit justo sed nulla. Nunc sit amet tempor nunc, vel posuere ipsum. Cras erat tortor, pulvinar ac pretium eu, auctor ac nibh. Duis iaculis porta magna, eu lobortis elit. Duis vitae massa eros. Nulla non magna accumsan, egestas quam sit amet, laoreet lectus.";
let mut input_pos = 0;
let mut output = vec![0; input.len()];
let mut output_pos = 0;
while input_pos < input.len() {
let (a, b) = cstream
.compress(&mut output[output_pos..], &input[input_pos..])
.unwrap();
output_pos += a;
input_pos += b;
}
// #[test]
// fn test() {
// let mut cstream = SeekableCStream::new(10, 256).unwrap();
// let input = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut diam ante, sollicitudin a dolor et, volutpat elementum nulla. Etiam nec efficitur nibh, quis rutrum risus. Maecenas quis lorem malesuada, aliquet mi vel, viverra nunc. Donec et nulla sed velit sagittis varius. Suspendisse vestibulum, neque lobortis ornare vestibulum, orci turpis vulputate nisi, ut sodales neque purus eget magna. Nunc condimentum, diam eu consequat venenatis, est nisl semper lorem, et lobortis velit justo sed nulla. Nunc sit amet tempor nunc, vel posuere ipsum. Cras erat tortor, pulvinar ac pretium eu, auctor ac nibh. Duis iaculis porta magna, eu lobortis elit. Duis vitae massa eros. Nulla non magna accumsan, egestas quam sit amet, laoreet lectus.";
// let mut input_pos = 0;
// let mut output = vec![0; input.len()];
// let mut output_pos = 0;
// while input_pos < input.len() {
// let (a, b) = cstream
// .compress(&mut output[output_pos..], &input[input_pos..])
// .unwrap();
// output_pos += a;
// input_pos += b;
// }
// while let Ok(n) = cstream.end_stream(&mut output[output_pos..]) {
// if n == 0 {
// break;
// }
// output_pos += n;
// }
// output.truncate(output_pos);
// {
// use std::io::Write;
// let mut file = std::fs::File::create("test").unwrap();
// file.write_all(&output).unwrap();
// }
// println!("input len = {:?}, pos = {:?}", input.len(), output_pos);
let mut output_stream = ZSTD_outBuffer {
dst: output[output_pos..].as_mut_ptr() as *mut c_void,
size: output.len() - output_pos as size_t,
pos: 0,
};
while let Ok(n) = cstream.end_stream(&mut output_stream) {
if n == 0 {
break;
}
output_pos += n;
}
output.truncate(output_pos);
{
use io::Write;
let mut file = fs::File::create("test").unwrap();
file.write_all(&output).unwrap();
}
println!("input len = {:?}, pos = {:?}", input.len(), output_pos);
let mut decomp = Vec::new();
let mut s = { Seekable::init_buf(&mut output).unwrap() };
for frame in 0..s.get_num_frames() {
let size = s.get_frame_decompressed_size(frame).unwrap();
println!("{:?} {:?}", size, s.get_frame_decompressed_offset(frame));
let n = decomp.len();
decomp.extend(std::iter::repeat(0).take(size));
s.decompress_frame(&mut decomp[n..], frame).unwrap();
}
println!("{:?}", std::str::from_utf8(&decomp).unwrap());
assert_eq!(&input[..], &decomp[..])
}
// let mut decomp = Vec::new();
// let mut s = { Seekable::init_buf(&mut output).unwrap() };
// for frame in 0..s.get_num_frames() {
// let size = s.get_frame_decompressed_size(frame).unwrap();
// println!("{:?} {:?}", size, s.get_frame_decompressed_offset(frame).unwrap());
// let n = decomp.len();
// decomp.extend(std::iter::repeat(0).take(size));
// s.decompress_frame(&mut decomp[n..], frame).unwrap();
// }
// println!("{:?}", std::str::from_utf8(&decomp).unwrap());
// assert_eq!(&input[..], &decomp[..])
// }