chore: format code

This commit is contained in:
Michel de Graaf
2025-07-31 22:00:06 +02:00
parent 6431630510
commit 843fb79d59
21 changed files with 421 additions and 238 deletions
+32 -16
View File
@@ -19,7 +19,7 @@ pub fn init() {
.or_else(|_| env::var("RUST_LOG"))
.unwrap_or_else(|_| "VERBOSE".to_string())
.to_uppercase();
unsafe {
LOG_LEVEL = match level.as_str() {
"ERROR" => LogLevel::Error,
@@ -28,7 +28,7 @@ pub fn init() {
"DEBUG" => LogLevel::Debug,
"TRACE" => LogLevel::Trace,
"VERBOSE" => LogLevel::Debug, // Map VERBOSE to DEBUG
_ => LogLevel::Debug, // Default to VERBOSE
_ => LogLevel::Debug, // Default to VERBOSE
};
}
});
@@ -44,22 +44,23 @@ pub fn log(level: LogLevel, message: &str) {
LogLevel::Debug => "\x1b[34mDEBUG\x1b[0m", // Blue
LogLevel::Trace => "\x1b[35mTRACE\x1b[0m", // Magenta
};
let now = std::time::SystemTime::now();
let datetime = now.duration_since(std::time::UNIX_EPOCH).unwrap();
let secs = datetime.as_secs();
let subsec_millis = datetime.subsec_millis();
// Format as YYYY-MM-DD HH:MM:SS.mmm
let days_since_epoch = secs / 86400;
let days_since_1970 = days_since_epoch as i32;
// Calculate year (approximately)
let mut year = 1970;
let mut remaining_days = days_since_1970;
while remaining_days >= 365 {
let is_leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
let is_leap =
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
let days_in_year = if is_leap { 366 } else { 365 };
if remaining_days >= days_in_year {
remaining_days -= days_in_year;
@@ -68,15 +69,20 @@ pub fn log(level: LogLevel, message: &str) {
break;
}
}
// Calculate month and day (simplified)
let month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
let is_leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
let is_leap =
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
let mut month = 1;
let mut day = remaining_days + 1;
for &days_in_month in &month_days {
let actual_days = if month == 2 && is_leap { 29 } else { days_in_month };
let actual_days = if month == 2 && is_leap {
29
} else {
days_in_month
};
if day > actual_days {
day -= actual_days;
month += 1;
@@ -84,15 +90,25 @@ pub fn log(level: LogLevel, message: &str) {
break;
}
}
// Calculate time of day
let seconds_in_day = secs % 86400;
let hours = seconds_in_day / 3600;
let minutes = (seconds_in_day % 3600) / 60;
let seconds = seconds_in_day % 60;
eprintln!("[{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}] [{}] {}",
year, month, day, hours, minutes, seconds, subsec_millis, level_str, message);
eprintln!(
"[{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03}] [{}] {}",
year,
month,
day,
hours,
minutes,
seconds,
subsec_millis,
level_str,
message
);
}
}
}
@@ -130,4 +146,4 @@ macro_rules! trace {
($($arg:tt)*) => {
$crate::utils::logger::log($crate::utils::logger::LogLevel::Trace, &format!($($arg)*))
};
}
}
+13 -8
View File
@@ -54,8 +54,9 @@ fn i(x: u32, y: u32, z: u32) -> u32 {
// utility function to convert a vector of type T with size N into an array of type T with size N.
fn vec_to_array<T, const N: usize>(v: Vec<T>) -> [T; N] {
v.try_into()
.unwrap_or_else(|_v: Vec<T>| panic!("error converting vector to array - sizes don't match"))
v.try_into().unwrap_or_else(|_v: Vec<T>| {
panic!("error converting vector to array - sizes don't match")
})
}
/**
@@ -297,14 +298,16 @@ fn compute_md5_digest(mut v: Vec<u8>) -> String {
let word_dd = word_d;
// execute round 1
let result = round_one_operations(word_a, word_b, word_c, word_d, &table, &x);
let result =
round_one_operations(word_a, word_b, word_c, word_d, &table, &x);
word_a = result[0];
word_b = result[1];
word_c = result[2];
word_d = result[3];
// execute round 2
let result = round_two_operations(word_a, word_b, word_c, word_d, &table, &x);
let result =
round_two_operations(word_a, word_b, word_c, word_d, &table, &x);
word_a = result[0];
word_b = result[1];
@@ -312,14 +315,16 @@ fn compute_md5_digest(mut v: Vec<u8>) -> String {
word_d = result[3];
// execute round 3
let result = round_three_operations(word_a, word_b, word_c, word_d, &table, &x);
let result =
round_three_operations(word_a, word_b, word_c, word_d, &table, &x);
word_a = result[0];
word_b = result[1];
word_c = result[2];
word_d = result[3];
// execute round 4
let result = round_four_operations(word_a, word_b, word_c, word_d, &table, &x);
let result =
round_four_operations(word_a, word_b, word_c, word_d, &table, &x);
word_a = result[0];
word_b = result[1];
word_c = result[2];
@@ -374,7 +379,6 @@ fn bit_padding(input: &str) -> Vec<u8> {
}
fn split_u64_to_u8_array(s: u64) -> [u8; 8] {
[
s as u8,
(s >> 8) as u8,
@@ -455,7 +459,8 @@ fn lowercase_alphabet_correct_hash() {
#[test]
fn all_alphanumeric_correct_hash() {
let alpha_num_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let alpha_num_str =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
assert_eq!("d174ab98d277d9f5a5611c2c9f419d9f", md5(alpha_num_str));
}
+1 -1
View File
@@ -66,4 +66,4 @@ impl Worker {
thread: Some(thread),
}
}
}
}
+71 -45
View File
@@ -98,7 +98,10 @@ fn inflate(r: &mut BitReader) -> Result<Vec<u8>, String> {
Ok(out)
}
fn inflate_block_no_compression(r: &mut BitReader, o: &mut Vec<u8>) -> Result<(), String> {
fn inflate_block_no_compression(
r: &mut BitReader,
o: &mut Vec<u8>,
) -> Result<(), String> {
let len = r.read_bytes(2)?;
let _nlen = r.read_bytes(2)?;
for _ in 0..len {
@@ -168,19 +171,20 @@ fn decode_symbol(r: &mut BitReader, t: &HuffmanTree) -> Result<u32, String> {
}
const LENGTH_EXTRA_BITS: [usize; 29] = [
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5,
5, 5, 5, 0,
];
const LENGTH_BASE: [u32; 29] = [
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131,
163, 195, 227, 258,
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
67, 83, 99, 115, 131, 163, 195, 227, 258,
];
const DISTANCE_EXTRA_BITS: [usize; 30] = [
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13,
13,
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10,
11, 11, 12, 12, 13, 13,
];
const DISTANCE_BASE: [u32; 30] = [
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537,
2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577,
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513,
769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577,
];
fn inflate_block_data(
@@ -203,7 +207,8 @@ fn inflate_block_data(
if sym_idx >= LENGTH_EXTRA_BITS.len() {
return Err("Invalid length symbol".to_string());
}
let length = r.read_bits(LENGTH_EXTRA_BITS[sym_idx])? + LENGTH_BASE[sym_idx];
let length =
r.read_bits(LENGTH_EXTRA_BITS[sym_idx])? + LENGTH_BASE[sym_idx];
let dist_sym = decode_symbol(r, distance_tree)?;
if dist_sym as usize >= DISTANCE_EXTRA_BITS.len() {
return Err("Invalid distance symbol".to_string());
@@ -250,7 +255,9 @@ const CODE_LENGTH_CODES_ORDER: [usize; 19] = [
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
];
fn decode_trees(r: &mut BitReader) -> Result<(HuffmanTree, HuffmanTree), String> {
fn decode_trees(
r: &mut BitReader,
) -> Result<(HuffmanTree, HuffmanTree), String> {
// The number of literal/length codes
let hlit = r.read_bits(5)? + 257;
@@ -263,12 +270,14 @@ fn decode_trees(r: &mut BitReader) -> Result<(HuffmanTree, HuffmanTree), String>
// Read code lengths for the code length alphabet
let mut code_length_tree_bl = vec![0; 19];
for i in 0..hclen as usize {
code_length_tree_bl[CODE_LENGTH_CODES_ORDER[i]] = r.read_bits(3)? as usize;
code_length_tree_bl[CODE_LENGTH_CODES_ORDER[i]] =
r.read_bits(3)? as usize;
}
// Construct code length tree
let code_length_alphabet: Vec<u32> = (0..19).collect();
let code_length_tree = bl_list_to_tree(&code_length_tree_bl, &code_length_alphabet);
let code_length_tree =
bl_list_to_tree(&code_length_tree_bl, &code_length_alphabet);
// Read literal/length + distance code length list
let mut bl = Vec::new();
@@ -303,20 +312,28 @@ fn decode_trees(r: &mut BitReader) -> Result<(HuffmanTree, HuffmanTree), String>
// Construct trees
let literal_length_alphabet: Vec<u32> = (0..286).collect();
let literal_length_tree = bl_list_to_tree(&bl[..hlit as usize], &literal_length_alphabet);
let literal_length_tree =
bl_list_to_tree(&bl[..hlit as usize], &literal_length_alphabet);
let distance_alphabet: Vec<u32> = (0..30).collect();
let distance_tree = bl_list_to_tree(&bl[hlit as usize..], &distance_alphabet);
let distance_tree =
bl_list_to_tree(&bl[hlit as usize..], &distance_alphabet);
Ok((literal_length_tree, distance_tree))
}
fn inflate_block_dynamic(r: &mut BitReader, o: &mut Vec<u8>) -> Result<(), String> {
fn inflate_block_dynamic(
r: &mut BitReader,
o: &mut Vec<u8>,
) -> Result<(), String> {
let (literal_length_tree, distance_tree) = decode_trees(r)?;
inflate_block_data(r, &literal_length_tree, &distance_tree, o)
}
fn inflate_block_fixed(r: &mut BitReader, o: &mut Vec<u8>) -> Result<(), String> {
fn inflate_block_fixed(
r: &mut BitReader,
o: &mut Vec<u8>,
) -> Result<(), String> {
let mut bl = Vec::new();
bl.extend(vec![8; 144]); // 0-143: 8 bits
bl.extend(vec![9; 112]); // 144-255: 9 bits
@@ -377,7 +394,8 @@ mod tests {
fn test_deflate() {
// Test data from the original test - this should work with our new implementation
let data = vec![
120, 156, 203, 72, 205, 201, 201, 87, 8, 207, 47, 202, 73, 1, 0, 24, 11, 4, 93,
120, 156, 203, 72, 205, 201, 201, 87, 8, 207, 47, 202, 73, 1, 0,
24, 11, 4, 93,
];
let result = deflate(&data);
assert!(result.is_ok());
@@ -388,35 +406,44 @@ mod tests {
#[test]
fn test_deflate2() {
let data = vec![
120, 156, 99, 103, 96, 96, 72, 201, 79, 201, 76, 79, 204, 203, 213, 158, 98, 194, 4,
228, 50, 250, 3, 9, 7, 135, 162, 156, 148, 194, 188, 152, 228, 252, 220, 130, 156, 212,
146, 212, 24, 231, 196, 188, 228, 204, 252, 188, 212, 226, 152, 144, 162, 210, 226,
226, 212, 28, 93, 67, 75, 115, 75, 93, 119, 160, 144, 130, 91, 126, 145, 66, 72, 70,
170, 66, 120, 106, 106, 118, 106, 94, 138, 174, 161, 89, 82, 102, 137, 174, 137, 137,
142, 161, 119, 70, 149, 94, 90, 78, 98, 114, 203, 175, 243, 32, 163, 193, 128, 25, 100,
7, 16, 23, 0, 9, 22, 32, 237, 178, 134, 129, 129, 21, 72, 11, 128, 196, 243, 176, 217,
29, 156, 153, 151, 158, 147, 90, 12, 54, 95, 193, 216, 84, 193, 200, 192, 200, 36, 198,
45, 181, 168, 40, 53, 57, 91, 193, 37, 177, 60, 79, 71, 193, 55, 177, 44, 181, 40, 19,
200, 13, 78, 76, 42, 74, 85, 80, 83, 240, 75, 45, 7, 10, 38, 103, 100, 2, 221, 167,
139, 238, 66, 5, 13, 144, 17, 154, 96, 167, 173, 228, 215, 98, 68, 119, 218, 74, 6, 76,
167, 49, 60, 153, 202, 200, 160, 199, 128, 0, 0, 161, 99, 76, 142,
120, 156, 99, 103, 96, 96, 72, 201, 79, 201, 76, 79, 204, 203, 213,
158, 98, 194, 4, 228, 50, 250, 3, 9, 7, 135, 162, 156, 148, 194,
188, 152, 228, 252, 220, 130, 156, 212, 146, 212, 24, 231, 196,
188, 228, 204, 252, 188, 212, 226, 152, 144, 162, 210, 226, 226,
212, 28, 93, 67, 75, 115, 75, 93, 119, 160, 144, 130, 91, 126, 145,
66, 72, 70, 170, 66, 120, 106, 106, 118, 106, 94, 138, 174, 161,
89, 82, 102, 137, 174, 137, 137, 142, 161, 119, 70, 149, 94, 90,
78, 98, 114, 203, 175, 243, 32, 163, 193, 128, 25, 100, 7, 16, 23,
0, 9, 22, 32, 237, 178, 134, 129, 129, 21, 72, 11, 128, 196, 243,
176, 217, 29, 156, 153, 151, 158, 147, 90, 12, 54, 95, 193, 216,
84, 193, 200, 192, 200, 36, 198, 45, 181, 168, 40, 53, 57, 91, 193,
37, 177, 60, 79, 71, 193, 55, 177, 44, 181, 40, 19, 200, 13, 78,
76, 42, 74, 85, 80, 83, 240, 75, 45, 7, 10, 38, 103, 100, 2, 221,
167, 139, 238, 66, 5, 13, 144, 17, 154, 96, 167, 173, 228, 215, 98,
68, 119, 218, 74, 6, 76, 167, 49, 60, 153, 202, 200, 160, 199, 128,
0, 0, 161, 99, 76, 142,
];
let expect = vec![
7, 0, 0, 0, 100, 111, 100, 105, 103, 97, 110, 109, 43, 148, 52, 2, 0, 0, 0, 1, 79, 0,
0, 0, 64, 64, 114, 108, 100, 113, 110, 92, 99, 111, 109, 112, 108, 101, 116, 101, 92,
67, 97, 110, 99, 105, 111, 110, 101, 115, 92, 84, 114, 117, 115, 115, 101, 108, 45, 49,
57, 55, 57, 45, 71, 111, 110, 101, 32, 70, 111, 114, 32, 84, 104, 101, 32, 87, 101,
101, 107, 101, 110, 100, 45, 49, 54, 98, 105, 116, 45, 52, 52, 44, 49, 75, 104, 122,
46, 102, 108, 97, 99, 132, 250, 207, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0,
112, 1, 0, 0, 4, 0, 0, 0, 68, 172, 0, 0, 5, 0, 0, 0, 16, 0, 0, 0, 1, 110, 0, 0, 0, 64,
64, 114, 108, 100, 113, 110, 92, 99, 111, 109, 112, 108, 101, 116, 101, 92, 83, 105,
110, 103, 108, 101, 115, 32, 87, 101, 101, 107, 32, 51, 53, 32, 50, 48, 50, 52, 92, 70,
101, 114, 114, 101, 99, 107, 32, 68, 97, 119, 110, 44, 32, 77, 97, 118, 101, 114, 105,
99, 107, 32, 83, 97, 98, 114, 101, 32, 38, 32, 78, 101, 119, 32, 77, 97, 99, 104, 105,
110, 101, 32, 45, 32, 70, 111, 114, 32, 84, 104, 101, 32, 87, 101, 101, 107, 101, 110,
100, 32, 40, 50, 48, 50, 52, 41, 46, 102, 108, 97, 99, 169, 15, 42, 1, 0, 0, 0, 0, 0,
0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 169, 0, 0, 0, 4, 0, 0, 0, 68, 172, 0, 0, 5, 0, 0, 0,
16, 0, 0, 0, 0, 228, 149, 1, 0, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7, 0, 0, 0, 100, 111, 100, 105, 103, 97, 110, 109, 43, 148, 52, 2,
0, 0, 0, 1, 79, 0, 0, 0, 64, 64, 114, 108, 100, 113, 110, 92, 99,
111, 109, 112, 108, 101, 116, 101, 92, 67, 97, 110, 99, 105, 111,
110, 101, 115, 92, 84, 114, 117, 115, 115, 101, 108, 45, 49, 57,
55, 57, 45, 71, 111, 110, 101, 32, 70, 111, 114, 32, 84, 104, 101,
32, 87, 101, 101, 107, 101, 110, 100, 45, 49, 54, 98, 105, 116, 45,
52, 52, 44, 49, 75, 104, 122, 46, 102, 108, 97, 99, 132, 250, 207,
2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 112, 1, 0, 0, 4,
0, 0, 0, 68, 172, 0, 0, 5, 0, 0, 0, 16, 0, 0, 0, 1, 110, 0, 0, 0,
64, 64, 114, 108, 100, 113, 110, 92, 99, 111, 109, 112, 108, 101,
116, 101, 92, 83, 105, 110, 103, 108, 101, 115, 32, 87, 101, 101,
107, 32, 51, 53, 32, 50, 48, 50, 52, 92, 70, 101, 114, 114, 101,
99, 107, 32, 68, 97, 119, 110, 44, 32, 77, 97, 118, 101, 114, 105,
99, 107, 32, 83, 97, 98, 114, 101, 32, 38, 32, 78, 101, 119, 32,
77, 97, 99, 104, 105, 110, 101, 32, 45, 32, 70, 111, 114, 32, 84,
104, 101, 32, 87, 101, 101, 107, 101, 110, 100, 32, 40, 50, 48, 50,
52, 41, 46, 102, 108, 97, 99, 169, 15, 42, 1, 0, 0, 0, 0, 0, 0, 0,
0, 3, 0, 0, 0, 1, 0, 0, 0, 169, 0, 0, 0, 4, 0, 0, 0, 68, 172, 0, 0,
5, 0, 0, 0, 16, 0, 0, 0, 0, 228, 149, 1, 0, 46, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
]
.to_vec();
let result = deflate(&data);
@@ -427,4 +454,3 @@ mod tests {
assert_eq!(decompressed, expect);
}
}