make packer usable as lib

This commit is contained in:
2021-11-20 16:27:35 +01:00
parent 8f23ec711f
commit 479a2e2c89
5 changed files with 30 additions and 16 deletions

10
README.md Normal file
View File

@@ -0,0 +1,10 @@
# Upkr
Upkr is a simple general purpose lz packer designed to be used in the [MicroW8](https://github.com/exoticorn/microw8) platform.
The compressed format is base on [Shrinkler](https://github.com/askeksa/Shrinkler) with the main difference being that
Upkr doesn't differnetiate between literals at odd or even addresses.
At this point, Upkr should be considered unstable - the exact format isn't finalized yet and still subject to change
and only a very simple (but also very fast) greedy compressor is implemented. The compression ratio will be improved
with a more thourough lz parse in the future, although even in the current state is is already similar to the
DEFLATE compression algorithm.

8
src/lib.rs Normal file
View File

@@ -0,0 +1,8 @@
mod context_state;
mod greedy_packer;
mod lz;
mod match_finder;
mod range_coder;
pub use greedy_packer::pack;
pub use lz::unpack;

View File

@@ -53,7 +53,7 @@ impl LzCoder {
assert!(value >= 1); assert!(value >= 1);
let top_bit = usize::BITS - 1 - value.leading_zeros(); let top_bit = usize::BITS - 1 - value.leading_zeros();
let mut context_index = context_start; let mut context_index = context_start;
for i in (0..top_bit).rev() { for i in 0..top_bit {
self.bit(true, context_index); self.bit(true, context_index);
self.bit((value >> i) & 1 != 0, context_index + 1); self.bit((value >> i) & 1 != 0, context_index + 1);
context_index += 2; context_index += 2;
@@ -78,14 +78,16 @@ pub fn unpack(packed_data: &[u8]) -> Vec<u8> {
contexts: &mut ContextState, contexts: &mut ContextState,
mut context_index: usize, mut context_index: usize,
) -> usize { ) -> usize {
let mut length = 1; let mut length = 0;
let mut bit_pos = 0;
while decoder.decode_with_context(&mut contexts.context_mut(context_index)) { while decoder.decode_with_context(&mut contexts.context_mut(context_index)) {
length = (length << 1) length |= (decoder.decode_with_context(&mut contexts.context_mut(context_index + 1))
| decoder.decode_with_context(&mut contexts.context_mut(context_index + 1)) as usize)
as usize; << bit_pos;
bit_pos += 1;
context_index += 2; context_index += 2;
} }
length length | (1 << bit_pos)
} }
loop { loop {

View File

@@ -1,16 +1,10 @@
mod context_state;
mod greedy_packer;
mod lz;
mod match_finder;
mod range_coder;
fn main() { fn main() {
let test_data = include_bytes!("../testcases/skipahead.wasm"); let test_data = include_bytes!("../README.md");
let packed = greedy_packer::pack(test_data); let packed = upkr::pack(test_data);
dbg!((test_data.len(), packed.len())); dbg!((test_data.len(), packed.len()));
let unpacked = lz::unpack(&packed); let unpacked = upkr::unpack(&packed);
dbg!(unpacked.len()); dbg!(unpacked.len());
assert!(test_data == unpacked.as_slice()); assert!(test_data == unpacked.as_slice());
} }

View File

@@ -113,7 +113,7 @@ impl<'a> Iterator for Matches<'a> {
self.add_to_queue(self.finder.suffixes[self.left_index]); self.add_to_queue(self.finder.suffixes[self.left_index]);
self.move_left(); self.move_left();
} }
if self.right_length == self.current_length { if self.right_length == self.current_length && self.matches_left > 0 {
self.add_to_queue(self.finder.suffixes[self.right_index]); self.add_to_queue(self.finder.suffixes[self.right_index]);
self.move_right(); self.move_right();
} }