Skip to content

Commit

Permalink
BUGFIX: compress()
Browse files Browse the repository at this point in the history
  • Loading branch information
msuiche committed Mar 17, 2021
1 parent c469611 commit c742f70
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
64 changes: 32 additions & 32 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn decompress(
indicator_bit = 32;
}

indicator_bit = indicator_bit - 1;
indicator_bit -= 1;

// Check whether the bit specified by indicator_bit is set or not
// set in indicator. For example, if indicator_bit has value 4
Expand All @@ -53,7 +53,7 @@ pub fn decompress(
length = usize::from(in_buf[in_idx + 1]) << 8
| usize::from(in_buf[in_idx]);

in_idx = in_idx + mem::size_of::<u16>();
in_idx += mem::size_of::<u16>();

offset = length / 8;
length = length % 8;
Expand All @@ -68,7 +68,7 @@ pub fn decompress(

length = (in_buf[in_idx] % 16).into();

in_idx = in_idx + mem::size_of::<u8>();
in_idx += mem::size_of::<u8>();
} else {
if nibble_idx >= in_buf.len() {
return Err(Error::MemLimit);
Expand All @@ -85,7 +85,7 @@ pub fn decompress(

length = in_buf[in_idx].into();

in_idx = in_idx + mem::size_of::<u8>();
in_idx += mem::size_of::<u8>();

if length == 255 {
if (in_idx + 1) >= in_buf.len() {
Expand All @@ -95,16 +95,16 @@ pub fn decompress(
length = usize::from(in_buf[in_idx + 1]) << 8
| usize::from(in_buf[in_idx]);

in_idx = in_idx + mem::size_of::<u16>();
in_idx += mem::size_of::<u16>();

length = length - (15 + 7);
length -= 15 + 7;
}
length = length + 15;
length -= 15;
}
length = length + 7;
length += 7;
}

length = length + 3;
length += 3;

while length != 0 {
if (offset + 1) > out_idx {
Expand All @@ -113,8 +113,8 @@ pub fn decompress(

out_buf.push(out_buf[out_idx - offset - 1]);

out_idx = out_idx + mem::size_of::<u8>();
length = length - mem::size_of::<u8>();
out_idx += mem::size_of::<u8>();
length -= mem::size_of::<u8>();
}
}
}
Expand Down Expand Up @@ -206,18 +206,18 @@ pub fn compress(
metadata = ((best_off - 1) << 3) | (best_len - 3);
out_buf[dest_off + metadata_size] = metadata as u8;
out_buf[dest_off + metadata_size + 1] = (metadata >> 8) as u8;
metadata_size = metadata_size + mem::size_of::<u16>();
metadata_size += mem::size_of::<u16>();
} else {
metadata = ((best_off - 1) << 3) | 7;
out_buf[dest_off + metadata_size] = metadata as u8;
out_buf[dest_off + metadata_size + 1] = (metadata >> 8) as u8;
metadata_size = metadata_size + mem::size_of::<u16>();
metadata_size += mem::size_of::<u16>();

if best_len < (15 + 7 + 3) {
// Shared byte
if nibble_index == 0 {
out_buf[out_idx + metadata_size] = ((best_len - (3 + 7)) & 0xF) as u8;
metadata_size = metadata_size + mem::size_of::<u8>();
metadata_size += mem::size_of::<u8>();
} else {
out_buf[nibble_index] = out_buf[nibble_index] & 0xF;
out_buf[nibble_index] = out_buf[nibble_index] | ((best_len - (3 + 7)) * 16) as u8;
Expand All @@ -226,35 +226,35 @@ pub fn compress(
// Shared byte
if nibble_index == 0 {
out_buf[out_idx + metadata_size] = 15;
metadata_size = metadata_size + mem::size_of::<u8>();
metadata_size += mem::size_of::<u8>();
} else {
out_buf[nibble_index] = out_buf[nibble_index] & 0xF;
out_buf[nibble_index] = out_buf[nibble_index] | (15 * 16);
}

// Additional best_len
out_buf[out_idx + metadata_size] = (best_len - (3 + 7 + 15)) as u8;
metadata_size = metadata_size + mem::size_of::<u8>();
metadata_size += mem::size_of::<u8>();
} else {
// Shared byte
if nibble_index == 0 {
out_buf[out_idx + metadata_size] = out_buf[out_idx + metadata_size] | 15;
metadata_size = metadata_size + mem::size_of::<u8>();
metadata_size += mem::size_of::<u8>();
} else {
out_buf[nibble_index] = out_buf[nibble_index] | (15 << 4);
}

// Additional best_len
out_buf[out_idx + metadata_size] = 255;
metadata_size = metadata_size + mem::size_of::<u8>();
metadata_size += mem::size_of::<u8>();

out_buf[out_idx + metadata_size] = (best_len - 3) as u8;
out_buf[out_idx + metadata_size + 1] = ((best_len - 3) >> 8) as u8;
metadata_size = metadata_size + mem::size_of::<u16>();
metadata_size += mem::size_of::<u16>();
}
}

indic = indic | (1 << (32 - ((indic_bit % 32) + 1)));
indic |= 1 << (32 - ((indic_bit % 32) + 1));

if best_len > 9 {
if nibble_index == 0 {
Expand All @@ -264,18 +264,18 @@ pub fn compress(
}
}

out_idx = out_idx + metadata_size;
in_idx = in_idx + best_len;
byte_left = byte_left - best_len;
out_idx += metadata_size;
in_idx += best_len;
byte_left -= best_len;
} else {
out_buf.push(in_buf[in_idx]);
out_idx = out_idx + 1;
in_idx = in_idx + 1;
out_idx += 1;
in_idx += 1;

byte_left = byte_left - 1;
byte_left -= 1;
}

indic_bit = indic_bit + 1;
indic_bit += 1;

if ((indic_bit - 1) % 32) > (indic_bit % 32) {
out_buf[indic_pos + 0] = indic as u8;
Expand All @@ -296,10 +296,10 @@ pub fn compress(

while in_idx < in_buf.len() {
out_buf.push(in_buf[in_idx]);
indic_bit = indic_bit + 1;
indic_bit += 1;

in_idx = in_idx + 1;
out_idx = out_idx + 1;
in_idx += 1;
out_idx += 1;
if ((indic_bit - 1) % 32) > (indic_bit % 32) {
out_buf[indic_pos + 0] = indic as u8;
out_buf[indic_pos + 1] = (indic >> 8) as u8;
Expand All @@ -314,8 +314,8 @@ pub fn compress(

if (indic_bit % 32) > 0 {
while (indic_bit % 32) != 0 {
indic |= 0 << (32 - ((indic_bit % 32) + 1));
indic_bit = indic_bit + 1;
indic |= 1 << (32 - ((indic_bit % 32) + 1));
indic_bit += 1;
}

out_buf[indic_pos + 0] = indic as u8;
Expand Down
2 changes: 1 addition & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const TEST_OSSFUZZ_20083_DATA: &'static [u8] = include_bytes!("clusterfuzz-testc

const TEST_STRING2: &'static str = "abcdefghijklmnopqrstuvwxyz";
const TEST_DATA2: &'static [u8] = &[
0x00, 0x00, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64,
0x3f, 0x00, 0x00, 0x00, 0x61, 0x62, 0x63, 0x64,
0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
0x75, 0x76, 0x77, 0x78, 0x79, 0x7a ];
Expand Down

0 comments on commit c742f70

Please sign in to comment.