Skip to main content

azathoth_utils/
lib.rs

1//! # azathoth_utils
2//!
3//! A small collection of utilities shared across the AzathothC2 ecosystem.
4//!
5//! ## Crate goals
6//! - `no_std` by default (uses `alloc` where needed).
7//! - Opt-in modules via Cargo features to keep binary size and surface area small.
8//!
9//! ## Cargo features
10//! - **`hasher`** – Identifier/symbol hashing helpers.
11//! - **`formatter`** – Lightweight formatting helpers for constrained environments.
12//! - **`psearch`** – Extendable pattern search utilities over byte slices.
13//! - **`codec`** – Minimal data encoding/decoding helpers.
14//!
15//! Each feature gates its corresponding module. Modules are excluded from the
16//! build unless their feature is enabled.
17//!
18//! ## Examples
19//! Computing a CRC32 checksum:
20//! ```no_run
21//! use azathoth_utils::crc32;
22//!
23//! let c = crc32(b"deadbeef");
24//! assert_eq!(c, 0x52_8f_6f_ca); // value will remain stable given the same table
25//! ```
26#![no_std]
27
28extern crate alloc;
29
30/// Error types used by azathoth utilities.
31///
32/// This module is always available and provides error enums and aliases shared
33/// by other feature-gated modules in this crate.
34pub mod errors;
35
36#[cfg(feature = "hasher")]
37/// Identifier and symbol hashing helpers.
38///
39/// Typical use-cases include obfuscated or stable hash-based lookups where
40/// string literals are undesirable at runtime.
41pub mod hasher;
42
43#[cfg(feature = "formatter")]
44/// Lightweight formatting helpers.
45///
46/// Useful in `no_std` contexts where calling the `alloc` crate formatter and related functions can cause crashes
47pub mod formatter;
48
49#[cfg(feature = "psearch")]
50/// Extendable pattern search utilities over byte slices.
51///
52/// Provides building blocks for scanning memory regions with optional wildcard support
53pub mod psearch;
54
55#[cfg(feature = "codec")]
56/// Minimal data encoding/decoding helpers.
57pub mod codec;
58
59/// Compute a CRC32 checksum over `data`.
60///
61/// This implementation uses the precomputed `azathoth_core::CRC32_TABLE`.
62///
63/// # Examples
64/// ```no_run
65/// use azathoth_utils::crc32;
66///
67/// assert_eq!(crc32(b""), 0xFFFF_FFFFu32 ^ 0xFFFF_FFFFu32); // CRC32 of empty input
68/// assert_eq!(crc32(b"123456789"), 0xCBF43926);
69/// ```
70#[inline(always)]
71pub fn crc32(data: impl AsRef<[u8]>) -> u32 {
72    let mut crc: u32 = 0xFFFF_FFFF;
73    let table = azathoth_core::CRC32_TABLE;
74
75    for &byte in data.as_ref() {
76        let index = ((crc ^ byte as u32) & 0xFF) as usize;
77        crc = (crc >> 8) ^ table[index];
78    }
79    !crc
80}
81
82#[cfg(feature = "formatter")]
83#[macro_export]
84macro_rules! format_str {
85     ($fmt:literal $(, $arg:expr)* $(,)?) => {{
86            $crate::format_str_inner($fmt, &($($arg,)*))
87     }};
88}
89
90#[cfg(feature = "formatter")]
91pub use formatter::format_str_inner;