Skip to content

Commit

Permalink
search in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ethteck committed Dec 5, 2023
1 parent 0b4360c commit 7f37e35
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 133 deletions.
72 changes: 62 additions & 10 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cmp;

pub fn read_u32(bytes: &[u8], offset: usize) -> u32 {
if offset % 4 != 0 {
panic!("Unaligned offset");
Expand All @@ -6,20 +8,62 @@ pub fn read_u32(bytes: &[u8], offset: usize) -> u32 {
u32::from_be_bytes(bytes[offset..offset + 4].try_into().unwrap())
}

fn initskip(pattern: &[u8], len: i32, skip: &mut [u16; 256]) {
skip.fill(len as u16);
pub(crate) fn search(
input_pos: usize,
input_size: usize,
pos_out: &mut i32,
size_out: &mut u32,
data_in: &[u8],
) {
let mut cur_size: usize = 3;
let mut found_pos: isize = 0;
let mut search_pos: usize = cmp::max(input_pos as isize - 0x1000, 0) as usize;
let search_size = cmp::min(input_size - input_pos, 0x111);

for i in 0..len {
skip[pattern[i as usize] as usize] = (len - i - 1) as u16;
if search_size >= 3 {
while search_pos < input_pos {
let found_offset = mischarsearch(
&data_in[input_pos..],
cur_size,
&data_in[search_pos..],
cur_size + input_pos - search_pos,
);

if found_offset >= input_pos - search_pos {
break;
}

while cur_size < search_size {
if data_in[cur_size + search_pos + found_offset] != data_in[cur_size + input_pos] {
break;
}
cur_size += 1;
}

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

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

*pos_out = found_pos as i32;
if cur_size > 3 {
cur_size -= 1;
*size_out = cur_size as u32;
return;
}
} else {
*pos_out = 0;
}
*size_out = 0;
}

pub(crate) fn mischarsearch(
pattern: &[u8],
pattern_len: usize,
data: &[u8],
data_len: usize,
) -> usize {
fn mischarsearch(pattern: &[u8], pattern_len: usize, data: &[u8], data_len: usize) -> usize {
let mut skip_table = [0u16; 256];
let mut i: isize;
//let mut k: usize;
Expand Down Expand Up @@ -60,3 +104,11 @@ pub(crate) fn mischarsearch(
}
data_len
}

fn initskip(pattern: &[u8], len: i32, 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;
}
}
68 changes: 2 additions & 66 deletions src/yay0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ pub fn compress_yay0(bytes: &[u8]) -> Box<[u8]> {
if v6 < v0 as u32 {
v6 += 1024;
}
search(v0, insize, &mut a3, &mut a4, bytes);
utils::search(v0, insize, &mut a3, &mut a4, bytes);

if a4 <= 2 {
cmd[cp] |= v1;
def.push(bytes[v0]);
v0 += 1;
} else {
search(v0 + 1, insize, &mut v8, &mut v7, bytes);
utils::search(v0 + 1, insize, &mut v8, &mut v7, bytes);
if v7 > a4 + 1 {
cmd[cp] |= v1;
def.push(bytes[v0]);
Expand Down Expand Up @@ -162,70 +162,6 @@ pub fn compress_yay0(bytes: &[u8]) -> Box<[u8]> {
ret.into_boxed_slice()
}

fn search(
input_pos: usize,
input_size: usize,
pos_out: &mut i32,
size_out: &mut u32,
data_in: &[u8],
) {
let mut cur_size: usize = 3;
let mut found_pos: isize = 0;
let mut search_pos: usize = 0;

if input_pos > 0x1000 {
search_pos = input_pos - 0x1000;
}

let mut search_size = 273;

if input_size - input_pos <= 273 {
search_size = input_size - input_pos;
}

if search_size >= 3 {
while search_pos < input_pos {
let found_offset = utils::mischarsearch(
&data_in[input_pos..],
cur_size,
&data_in[search_pos..],
cur_size + input_pos - search_pos,
);

if found_offset >= input_pos - search_pos {
break;
}

while cur_size < search_size {
if data_in[cur_size + search_pos + found_offset] != data_in[cur_size + input_pos] {
break;
}
cur_size += 1;
}

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

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

*pos_out = found_pos as i32;
if cur_size > 3 {
cur_size -= 1;
*size_out = cur_size as u32;
return;
}
} else {
*pos_out = 0;
}
*size_out = 0;
}

#[cfg(test)]
mod tests {
use core::panic;
Expand Down
59 changes: 2 additions & 57 deletions src/yaz0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub fn compress_yaz0(bytes: &[u8]) -> Box<[u8]> {
let mut group_pos: i32 = 0;
let mut group_size: u32 = 0;

search(
utils::search(
input_pos,
input_size,
&mut group_pos,
Expand All @@ -109,7 +109,7 @@ pub fn compress_yaz0(bytes: &[u8]) -> Box<[u8]> {
let mut new_position: i32 = 0;

// Search for a new group after one position after the current one
search(
utils::search(
input_pos + 1,
input_size,
&mut new_position,
Expand Down Expand Up @@ -179,61 +179,6 @@ pub fn compress_yaz0(bytes: &[u8]) -> Box<[u8]> {
output.into_boxed_slice()
}

fn search(
input_pos: usize,
input_size: usize,
pos_out: &mut i32,
size_out: &mut u32,
data_in: &[u8],
) {
let mut cur_size: usize = 3;
let mut found_pos: isize = 0;
let mut search_pos: usize = cmp::max(input_pos as isize - 0x1000, 0) as usize;
let search_size = cmp::min(input_size - input_pos, 0x111);

if search_size >= 3 {
while search_pos < input_pos {
let found_offset = utils::mischarsearch(
&data_in[input_pos..],
cur_size,
&data_in[search_pos..],
cur_size + input_pos - search_pos,
);

if found_offset >= input_pos - search_pos {
break;
}

while cur_size < search_size {
if data_in[cur_size + search_pos + found_offset] != data_in[cur_size + input_pos] {
break;
}
cur_size += 1;
}

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

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

*pos_out = found_pos as i32;
if cur_size > 3 {
cur_size -= 1;
*size_out = cur_size as u32;
return;
}
} else {
*pos_out = 0;
}
*size_out = 0;
}

#[cfg(test)]
mod tests {
#[test]
Expand Down

0 comments on commit 7f37e35

Please sign in to comment.