mirror of
https://github.com/exoticorn/upkr.git
synced 2026-01-20 11:36:42 +01:00
make packer usable as lib
This commit is contained in:
10
README.md
Normal file
10
README.md
Normal 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
8
src/lib.rs
Normal 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;
|
||||
14
src/lz.rs
14
src/lz.rs
@@ -53,7 +53,7 @@ impl LzCoder {
|
||||
assert!(value >= 1);
|
||||
let top_bit = usize::BITS - 1 - value.leading_zeros();
|
||||
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((value >> i) & 1 != 0, context_index + 1);
|
||||
context_index += 2;
|
||||
@@ -78,14 +78,16 @@ pub fn unpack(packed_data: &[u8]) -> Vec<u8> {
|
||||
contexts: &mut ContextState,
|
||||
mut context_index: 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)) {
|
||||
length = (length << 1)
|
||||
| decoder.decode_with_context(&mut contexts.context_mut(context_index + 1))
|
||||
as usize;
|
||||
length |= (decoder.decode_with_context(&mut contexts.context_mut(context_index + 1))
|
||||
as usize)
|
||||
<< bit_pos;
|
||||
bit_pos += 1;
|
||||
context_index += 2;
|
||||
}
|
||||
length
|
||||
length | (1 << bit_pos)
|
||||
}
|
||||
|
||||
loop {
|
||||
|
||||
12
src/main.rs
12
src/main.rs
@@ -1,16 +1,10 @@
|
||||
mod context_state;
|
||||
mod greedy_packer;
|
||||
mod lz;
|
||||
mod match_finder;
|
||||
mod range_coder;
|
||||
|
||||
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()));
|
||||
|
||||
let unpacked = lz::unpack(&packed);
|
||||
let unpacked = upkr::unpack(&packed);
|
||||
dbg!(unpacked.len());
|
||||
assert!(test_data == unpacked.as_slice());
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ impl<'a> Iterator for Matches<'a> {
|
||||
self.add_to_queue(self.finder.suffixes[self.left_index]);
|
||||
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.move_right();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user