Skip to content

Commit

Permalink
Fix bug with id_pat and read_bytes_const
Browse files Browse the repository at this point in the history
* Fix setting last read length, intsead of incorrectly adding
  • Loading branch information
wcampbell0x2a committed Sep 7, 2024
1 parent 8fad609 commit 0440037
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl<'a, R: Read + Seek> Reader<'a, R> {
}

let bits_read = amt * 8;
self.last_bits_read_amt += bits_read;
self.last_bits_read_amt = bits_read;
self.bits_read += bits_read;

#[cfg(feature = "logging")]
Expand Down Expand Up @@ -403,7 +403,7 @@ impl<'a, R: Read + Seek> Reader<'a, R> {
return Err(DekuError::Io(e.kind()));
}

self.last_bits_read_amt += N * 8;
self.last_bits_read_amt = N * 8;
self.bits_read += N * 8;

#[cfg(feature = "logging")]
Expand Down Expand Up @@ -518,4 +518,52 @@ mod tests {
let _ = reader.read_bytes(1, &mut buf);
assert_eq!([0xaa], buf);
}

#[test]
fn test_seek_last_read_bytes() {
// bytes
let input = hex!("aa");
let mut cursor = Cursor::new(input);
let mut reader = Reader::new(&mut cursor);
let mut buf = [0; 1];
let _ = reader.read_bytes(1, &mut buf);
assert_eq!([0xaa], buf);
reader.seek_last_read().unwrap();
let _ = reader.read_bytes(1, &mut buf);
assert_eq!([0xaa], buf);

// 2 bytes (and const)
let input = hex!("aabb");
let mut cursor = Cursor::new(input);
let mut reader = Reader::new(&mut cursor);
let mut buf = [0; 2];
let _ = reader.read_bytes_const::<2>(&mut buf);
assert_eq!([0xaa, 0xbb], buf);
reader.seek_last_read().unwrap();
let _ = reader.read_bytes_const::<2>(&mut buf);
assert_eq!([0xaa, 0xbb], buf);
}

#[cfg(feature = "bits")]
#[test]
fn test_seek_last_read_bits() {
let input = hex!("ab");
let mut cursor = Cursor::new(input);
let mut reader = Reader::new(&mut cursor);
let bits = reader.read_bits(4).unwrap();
assert_eq!(bits, Some(bitvec![u8, Msb0; 1, 0, 1, 0]));
reader.seek_last_read().unwrap();
let bits = reader.read_bits(4).unwrap();
assert_eq!(bits, Some(bitvec![u8, Msb0; 1, 0, 1, 0]));

// more than byte
let input = hex!("abd0");
let mut cursor = Cursor::new(input);
let mut reader = Reader::new(&mut cursor);
let bits = reader.read_bits(9).unwrap();
assert_eq!(bits, Some(bitvec![u8, Msb0; 1, 0, 1, 0, 1, 0, 1, 1, 1]));
reader.seek_last_read().unwrap();
let bits = reader.read_bits(9).unwrap();
assert_eq!(bits, Some(bitvec![u8, Msb0; 1, 0, 1, 0, 1, 0, 1, 1, 1]));
}
}

0 comments on commit 0440037

Please sign in to comment.