From 5b738e48aa0e42b7bda3ca4facb1c949ade5194d Mon Sep 17 00:00:00 2001 From: Bavfalcon9 Date: Wed, 27 Oct 2021 21:48:18 -0500 Subject: [PATCH] bug-fix(#8): Adds hacks to fix this issue. LE streams should be properly implemented in the future. --- src/lib.rs | 12 ++++++++- tests/lstring.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/tests.rs | 3 ++- 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 tests/lstring.rs diff --git a/src/lib.rs b/src/lib.rs index cf5f4b5..21ab707 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,7 +109,17 @@ where reverse_vec(source[*position..].to_vec()) } }; - LE(T::compose(&stream[..], position)) + + // todo Properly implement LE streams + // todo Get rid of this NASTY hack! + // we need to get the stream releative to the current source, and "inject" into the current source. + // we can do this by getting the position and the length of the stream. + let mut hacked_stream = Vec::::new(); + hacked_stream.write_all(&source[..*position]).unwrap(); + // now we write the rest of our changed stream + hacked_stream.write_all(&stream).unwrap(); + + LE(T::compose(&hacked_stream[..], position)) } } diff --git a/tests/lstring.rs b/tests/lstring.rs new file mode 100644 index 0000000..91c9faf --- /dev/null +++ b/tests/lstring.rs @@ -0,0 +1,66 @@ +use binary_utils::*; +use std::{io::Write}; + + +// Extracted from protocol. +#[derive(Debug, Clone)] +pub struct LString32(pub String); + +impl Streamable for LString32 { + fn parse(&self) -> Vec { + // get the length + let mut buffer: Vec = Vec::new(); + buffer.write_all(&LE::(self.0.len() as u32).parse()[..]).unwrap(); + // now we write string buffer. + buffer.write_all(&self.0.clone().into_bytes()[..]).unwrap(); + buffer + } + + fn compose(source: &[u8], position: &mut usize) -> Self { + let length = LE::::compose(&source[..], position); + let bytes = &source[*position..(*position + length.0 as usize)]; + + *position += bytes.len(); + + Self(unsafe { String::from_utf8_unchecked(bytes.to_vec()) }) + } +} + + +pub const HW_TEST_DATA: &[u8] = &[ + // Length of the string in Little Endian Format + 12, 0, 0, 0, + // Contents of string + 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 +]; + +#[test] +fn write_l32string() { + let hello_world = "Hello World!".to_string(); + let data = LString32(hello_world).parse(); + + assert_eq!(HW_TEST_DATA, &data[..]); +} + +#[test] +fn read_l32string() { + let hello_world = "Hello World!".to_string(); + let data = LString32::compose(HW_TEST_DATA, &mut 0); + assert_eq!(data.0, hello_world); +} + +#[test] +fn read_twice() { + let hello_world = "Hello World!".to_string(); + let mut stream = Vec::::new(); + stream.write_all(&LString32(hello_world.clone()).parse()[..]).unwrap(); + stream.write_all(&LString32(hello_world).parse()[..]).unwrap(); + // ok read it. + let mut pos: usize = 0; + let one = LString32::compose(&stream[..], &mut pos).0; + dbg!(&one); + dbg!(&pos); + let two = LString32::compose(&stream[..], &mut pos).0; + + assert_eq!(one, two); +} \ No newline at end of file diff --git a/tests/tests.rs b/tests/tests.rs index ca911f4..dceee37 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -2,4 +2,5 @@ mod macro_tests; mod vec; mod var_int; -mod le_test; \ No newline at end of file +mod le_test; +mod lstring; \ No newline at end of file