about summary refs log tree commit diff
path: root/unpacker/src/lib.rs
diff options
context:
space:
mode:
authorgennyble <gen@nyble.dev>2023-06-09 01:38:51 -0500
committergennyble <gen@nyble.dev>2023-06-09 01:38:51 -0500
commitb08ac436beed955053b672d65715811535a46096 (patch)
tree3b7adedf8a0930b875ad9ea3f1c07c1c10b10389 /unpacker/src/lib.rs
parent1e2224b6273e2b57ebf7dc90e2439c1d6075ef39 (diff)
downloadlri-rs-b08ac436beed955053b672d65715811535a46096.tar.gz
lri-rs-b08ac436beed955053b672d65715811535a46096.zip
unpacker
Diffstat (limited to 'unpacker/src/lib.rs')
-rw-r--r--unpacker/src/lib.rs44
1 files changed, 44 insertions, 0 deletions
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<u8>,
+	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())
+		}
+	}
+}