From b08ac436beed955053b672d65715811535a46096 Mon Sep 17 00:00:00 2001 From: gennyble Date: Fri, 9 Jun 2023 01:38:51 -0500 Subject: unpacker --- unpacker/Cargo.lock | 7 +++++++ unpacker/Cargo.toml | 8 ++++++++ unpacker/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ unpacker/src/main.rs | 24 ++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 unpacker/Cargo.lock create mode 100644 unpacker/Cargo.toml create mode 100644 unpacker/src/lib.rs create mode 100644 unpacker/src/main.rs (limited to 'unpacker') diff --git a/unpacker/Cargo.lock b/unpacker/Cargo.lock new file mode 100644 index 0000000..44d0a0f --- /dev/null +++ b/unpacker/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "unpacker" +version = "0.1.0" diff --git a/unpacker/Cargo.toml b/unpacker/Cargo.toml new file mode 100644 index 0000000..147529a --- /dev/null +++ b/unpacker/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "unpacker" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/unpacker/src/lib.rs b/unpacker/src/lib.rs new file mode 100644 index 0000000..f649581 --- /dev/null +++ b/unpacker/src/lib.rs @@ -0,0 +1,44 @@ +#[derive(Debug)] +pub struct Unpacker { + pub out: Vec, + pub work: u16, + pub work_idx: usize, +} + +impl Unpacker { + pub fn new() -> Self { + Self { + out: vec![], + work: 0, + work_idx: 0, + } + } + + pub fn push(&mut self, byte: u8) { + self.work = self.work << 8; + self.work |= byte as u16; + self.work_idx += 8; + + //println!("[{work_idx}]"); + + if self.work_idx >= 10 { + let to_front = self.work_idx - 10; + let fronted = self.work >> to_front; + let masked = fronted & 0b000_000_111_11_111_11; + + let fixwork = fronted << to_front; + + self.out.extend(masked.to_le_bytes()); + self.work_idx -= 10; + self.work ^= fixwork; + } + } + + pub fn finish(&mut self) { + if self.work_idx > 0 { + let remain = 10 - self.work_idx; + let out = self.work << remain; + self.out.extend(out.to_le_bytes()) + } + } +} diff --git a/unpacker/src/main.rs b/unpacker/src/main.rs new file mode 100644 index 0000000..047b4c5 --- /dev/null +++ b/unpacker/src/main.rs @@ -0,0 +1,24 @@ +use unpacker::Unpacker; + +fn main() { + // Four bits padding at the end. + let testdata = vec![ + 0b10000000, 0b00010000, 0b00000010, 0b00000000, 0b01000000, 0b00001000, 0b00000001, + 0b00000000, 0b00100000, 0b00000100, 0b00000000, 0b10000000, 0b00010000, + ]; + + let mut up = Unpacker { + out: vec![], + work: 0, + work_idx: 0, + }; + + for byte in testdata { + up.push(byte); + } + up.finish(); + + for chnk in up.out.chunks(2) { + println!("{:08b} {:08b}", chnk[0], chnk[1]); + } +} -- cgit 1.4.1-3-g733a5