Skip to content

Commit

Permalink
fix: correctly handle mid window offsets in forward mode
Browse files Browse the repository at this point in the history
  • Loading branch information
RisaI committed Oct 20, 2024
1 parent bc27ed0 commit 1435707
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/read/magic_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::result::ZipResult;
pub trait FinderDirection<'a> {
fn new(needle: &'a [u8]) -> Self;
fn reset_cursor(bounds: (u64, u64), window_size: usize) -> u64;
fn scope_window(window: &[u8], mid_window_offset: usize) -> &[u8];
fn scope_window(window: &[u8], mid_window_offset: usize) -> (&[u8], usize);

fn needle(&self) -> &[u8];
fn find(&self, haystack: &[u8]) -> Option<usize>;
Expand All @@ -25,8 +25,8 @@ impl<'a> FinderDirection<'a> for Forward<'a> {
start_inclusive
}

fn scope_window(window: &[u8], mid_window_offset: usize) -> &[u8] {
&window[mid_window_offset..]
fn scope_window(window: &[u8], mid_window_offset: usize) -> (&[u8], usize) {
(&window[mid_window_offset..], mid_window_offset)
}

fn find(&self, haystack: &[u8]) -> Option<usize> {
Expand Down Expand Up @@ -66,8 +66,8 @@ impl<'a> FinderDirection<'a> for Backwards<'a> {
.clamp(bounds.0, bounds.1)
}

fn scope_window(window: &[u8], mid_window_offset: usize) -> &[u8] {
&window[..mid_window_offset]
fn scope_window(window: &[u8], mid_window_offset: usize) -> (&[u8], usize) {
(&window[..mid_window_offset], 0)
}

fn find(&self, haystack: &[u8]) -> Option<usize> {
Expand Down Expand Up @@ -168,16 +168,16 @@ impl<'a, T: FinderDirection<'a>> MagicFinder<T> {
reader.read_exact(window)?;
}

let window = match self.mid_buffer_offset {
let (window, window_start_offset) = match self.mid_buffer_offset {
Some(mid_buffer_offset) => T::scope_window(window, mid_buffer_offset),
None => window,
None => (&*window, 0usize),
};

if let Some(offset) = self.finder.find(window) {
let magic_pos = window_start + offset as u64;
let magic_pos = window_start + window_start_offset as u64 + offset as u64;
reader.seek(SeekFrom::Start(magic_pos))?;

self.mid_buffer_offset = Some(self.finder.move_scope(offset));
self.mid_buffer_offset = Some(self.finder.move_scope(window_start_offset + offset));

return Ok(Some(magic_pos));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/prepended_garbage.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::io;
use std::io::Cursor;
use zip::ZipArchive;

#[test]
fn test_prepended_garbage() {
let mut v = vec![0, 1, 2, 3];
v.extend_from_slice(include_bytes!("../tests/data/extended_timestamp.zip"));

let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file");
let mut archive = ZipArchive::new(Cursor::new(v)).expect("couldn't open test zip file");

assert_eq!(2, archive.len());

Expand Down

0 comments on commit 1435707

Please sign in to comment.