Skip to content

Commit

Permalink
Try feeding libpng as many bytes as remain, rather than panicking if …
Browse files Browse the repository at this point in the history
…there aren't as many as it requested.
  • Loading branch information
lilith committed Oct 30, 2024
1 parent c0746d4 commit 24ce9d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 4 additions & 3 deletions imageflow_core/src/codecs/libpng_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ impl PngDec{

let buffer_slice = unsafe{ std::slice::from_raw_parts_mut(buffer, bytes_requested) };

return match decoder.io.read_exact(buffer_slice) {
Ok(()) => {
*out_bytes_read = buffer_slice.len();
return match decoder.io.read_maximally(buffer_slice) {
Ok(read_total) => {
assert!(read_total <= bytes_requested);
*out_bytes_read = read_total;
true
},
Err(err) => {
Expand Down
16 changes: 16 additions & 0 deletions imageflow_core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ impl IoProxy {
self.backend.get_read().expect("cannot read from writer").read_exact(buf)
}

pub fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize>{
self.backend.get_read().expect("cannot read from writer").read(buf)
}

pub fn read_maximally(&mut self, buf: &mut [u8]) -> std::io::Result<usize>{
let mut total_read = 0;
while total_read < buf.len(){
let read = self.read(&mut buf[total_read..])?;
if read == 0{
break;
}
total_read += read;
}
Ok(total_read)
}

pub fn read_file(context: &Context, filename: PathBuf, io_id: i32) -> Result<IoProxy> {
IoProxy::file_with_mode(context, io_id, filename,IoDirection::In)
}
Expand Down

0 comments on commit 24ce9d9

Please sign in to comment.