/home/noah/src/trueno/src/tuner/helpers.rs
Line | Count | Source |
1 | | //! Helper Functions for Tuner |
2 | | //! |
3 | | //! Utility functions: CRC32, timestamp, string formatting. |
4 | | |
5 | | // ============================================================================ |
6 | | // CRC32 Implementation |
7 | | // ============================================================================ |
8 | | |
9 | | /// Generate CRC32 lookup table at compile time. |
10 | 0 | const fn crc32_table() -> [u32; 256] { |
11 | 0 | let mut table = [0u32; 256]; |
12 | 0 | let mut i = 0; |
13 | 0 | while i < 256 { |
14 | 0 | let mut crc = i as u32; |
15 | 0 | let mut j = 0; |
16 | 0 | while j < 8 { |
17 | 0 | if crc & 1 != 0 { |
18 | 0 | crc = 0xEDB8_8320 ^ (crc >> 1); |
19 | 0 | } else { |
20 | 0 | crc >>= 1; |
21 | 0 | } |
22 | 0 | j += 1; |
23 | | } |
24 | 0 | table[i] = crc; |
25 | 0 | i += 1; |
26 | | } |
27 | 0 | table |
28 | 0 | } |
29 | | |
30 | | /// Simple CRC32 implementation (IEEE polynomial). |
31 | | /// Used for .apr file checksum verification. |
32 | 0 | pub fn crc32_update(crc: u32, data: &[u8]) -> u32 { |
33 | | const CRC32_TABLE: [u32; 256] = crc32_table(); |
34 | 0 | let mut crc = !crc; |
35 | 0 | for &byte in data { |
36 | 0 | crc = CRC32_TABLE[((crc ^ u32::from(byte)) & 0xFF) as usize] ^ (crc >> 8); |
37 | 0 | } |
38 | 0 | !crc |
39 | 0 | } |
40 | | |
41 | | /// Compute CRC32 hash for given data (convenience wrapper) |
42 | 0 | pub fn crc32_hash(data: &[u8]) -> u32 { |
43 | 0 | crc32_update(0, data) |
44 | 0 | } |
45 | | |
46 | | // ============================================================================ |
47 | | // Timestamp |
48 | | // ============================================================================ |
49 | | |
50 | | /// Simple timestamp (avoids chrono dependency) |
51 | 0 | pub fn chrono_lite_now() -> String { |
52 | | use std::time::{SystemTime, UNIX_EPOCH}; |
53 | 0 | let duration = SystemTime::now() |
54 | 0 | .duration_since(UNIX_EPOCH) |
55 | 0 | .unwrap_or_default(); |
56 | 0 | format!("{}", duration.as_secs()) |
57 | 0 | } |
58 | | |
59 | | // ============================================================================ |
60 | | // String Formatting |
61 | | // ============================================================================ |
62 | | |
63 | | /// Pad string to fixed width |
64 | 0 | pub fn pad_right(s: &str, width: usize) -> String { |
65 | 0 | if s.len() >= width { |
66 | 0 | s[..width].to_string() |
67 | | } else { |
68 | 0 | format!("{}{}", s, " ".repeat(width - s.len())) |
69 | | } |
70 | 0 | } |