Skip to content

Commit

Permalink
Finish cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Dec 28, 2023
1 parent 562f0c8 commit de0a52c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- CHANGELOG.md file.

### Changed

- A few code cleanups.

## [0.2.0] - 2023-12-28

### Added
Expand Down
10 changes: 2 additions & 8 deletions lib/src/mio0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ pub fn compress(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
let mut cur_layout_bit: u32 = 0x80000000;

while input_pos < input_size {
let mut group_pos: i32;
let mut group_size: u32;

(group_pos, group_size) = utils::search(input_pos, bytes, 18);
let (mut group_pos, mut group_size) = utils::search(input_pos, bytes, 18);

// If the group isn't larger than 2 bytes, copying the input without compression is smaller
if group_size <= 2 {
Expand All @@ -112,11 +109,8 @@ pub fn compress(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
def.push(bytes[input_pos]);
input_pos += 1;
} else {
let new_size: u32;
let new_position: i32;

// Search for a new group after one position after the current one
(new_position, new_size) = utils::search(input_pos + 1, bytes, 18);
let (new_position, new_size) = utils::search(input_pos + 1, bytes, 18);

// If the new group is better than the current group by at least 2 bytes, use it instead
if new_size >= group_size + 2 {
Expand Down
12 changes: 6 additions & 6 deletions lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub(crate) fn set_pointer_array_from_u8_array(
Ok(())
}

pub(crate) fn search(input_pos: usize, data_in: &[u8], max_match_length: usize) -> (i32, u32) {
pub(crate) fn search(input_pos: usize, data_in: &[u8], max_match_length: usize) -> (u32, u32) {
let mut cur_size = 3;
let mut found_pos = 0;
let mut search_pos = cmp::max(input_pos as isize - 0x1000, 0) as usize;
Expand Down Expand Up @@ -108,15 +108,15 @@ pub(crate) fn search(input_pos: usize, data_in: &[u8], max_match_length: usize)
}

if search_size == cur_size {
return ((found_offset + search_pos) as i32, cur_size as u32);
return ((found_offset + search_pos) as u32, cur_size as u32);
}

found_pos = (search_pos + found_offset) as isize;
search_pos = (found_pos + 1) as usize;
cur_size += 1;
}

(found_pos as i32, cmp::max(cur_size as isize - 1, 0) as u32)
(found_pos as u32, cmp::max(cur_size as isize - 1, 0) as u32)
}

fn mischarsearch(pattern: &[u8], pattern_len: usize, data: &[u8], data_len: usize) -> usize {
Expand All @@ -127,7 +127,7 @@ fn mischarsearch(pattern: &[u8], pattern_len: usize, data: &[u8], data_len: usiz
let mut j: isize;

if pattern_len <= data_len {
initskip(pattern, pattern_len as i32, &mut skip_table);
initskip(pattern, pattern_len, &mut skip_table);

i = pattern_len as isize - 1;
loop {
Expand Down Expand Up @@ -160,10 +160,10 @@ fn mischarsearch(pattern: &[u8], pattern_len: usize, data: &[u8], data_len: usiz
data_len
}

fn initskip(pattern: &[u8], len: i32, skip: &mut [u16; 256]) {
fn initskip(pattern: &[u8], len: usize, skip: &mut [u16; 256]) {
skip.fill(len as u16);

for i in 0..len {
skip[pattern[i as usize] as usize] = (len - i - 1) as u16;
skip[pattern[i] as usize] = (len - i - 1) as u16;
}
}
10 changes: 2 additions & 8 deletions lib/src/yay0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ pub fn compress(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
let mut cur_layout_bit: u32 = 0x80000000;

while input_pos < input_size {
let mut group_pos: i32;
let mut group_size: u32;

(group_pos, group_size) = utils::search(input_pos, bytes, 0x111);
let (mut group_pos, mut group_size) = utils::search(input_pos, bytes, 0x111);

// If the group isn't larger than 2 bytes, copying the input without compression is smaller
if group_size <= 2 {
Expand All @@ -120,11 +117,8 @@ pub fn compress(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
def.push(bytes[input_pos]);
input_pos += 1;
} else {
let new_size: u32;
let new_position: i32;

// Search for a new group after one position after the current one
(new_position, new_size) = utils::search(input_pos + 1, bytes, 0x111);
let (new_position, new_size) = utils::search(input_pos + 1, bytes, 0x111);

// If the new group is better than the current group by at least 2 bytes, use it instead
if new_size >= group_size + 2 {
Expand Down
12 changes: 3 additions & 9 deletions lib/src/yaz0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ pub fn compress(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
let mut cur_layout_bit: u8 = 0x80;

while input_pos < input_size {
let mut group_pos: i32;
let mut group_size: u32;

(group_pos, group_size) = utils::search(input_pos, bytes, 0x111);
let (mut group_pos, mut group_size) = utils::search(input_pos, bytes, 0x111);

// If the group isn't larger than 2 bytes, copying the input without compression is smaller
if group_size <= 2 {
Expand All @@ -120,11 +117,8 @@ pub fn compress(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
input_pos += 1;
index_out_ptr += 1;
} else {
let new_size: u32;
let new_position: i32;

// Search for a new group after one position after the current one
(new_position, new_size) = utils::search(input_pos + 1, bytes, 0x111);
let (new_position, new_size) = utils::search(input_pos + 1, bytes, 0x111);

// If the new group is better than the current group by at least 2 bytes, use it instead
if new_size >= group_size + 2 {
Expand All @@ -150,7 +144,7 @@ pub fn compress(bytes: &[u8]) -> Result<Box<[u8]>, Crunch64Error> {
}

// Calculate the offset for the current group
let group_offset: u32 = (input_pos as i32 - group_pos - 1) as u32;
let group_offset = input_pos as u32 - group_pos - 1;

// Determine which encoding to use for the current group
if group_size >= 0x12 {
Expand Down

0 comments on commit de0a52c

Please sign in to comment.