basexx 0.2.0

basexx utilities. base32, base64, base58, base56...
Documentation
# Source Code Review Report for `basexx` (v0.1.1)

This document provides a technical code review of the `basexx` Rust library. The project demonstrates a strong commitment to performance through SIMD acceleration and supports a wide range of base encoding standards.

---

## 1. Executive Summary

`basexx` is a highly optimized encoding library with several commendable features:
- **High Performance:** Use of SSSE3 and AVX2 intrinsics for Base32 and Base64.
- **Flexibility:** Custom character map support via `AsciiGraphicSet`.
- **Comprehensive Coverage:** Supports Base32, Base32I, Base56, Base58, Base58B, Base58R, Base64, and Base64G.

However, the current version (v0.1.1) contains several critical issues, including a broken benchmark build and potential undefined behavior in SIMD code paths.

---

## 2. Critical Issues & Regressions

### 2.1. Broken Benchmark Build (Regression)
* **Location:** `src/lib_bench.rs:1`
* **Severity:** **Critical**
* **Description:** 
  The benchmark file `src/lib_bench.rs` contains a module declaration `mod aligned_data;`, but the corresponding file `src/aligned_data.rs` is missing from the repository. This causes `cargo check --bench lib_bench` to fail.
  ```
  error[E0583]: file not found for module `aligned_data`
   --> src/lib_bench.rs:1:1
    |
  1 | mod aligned_data;
    | ^^^^^^^^^^^^^^^^^
  ```
* **Recommendation:** Remove the `mod aligned_data;` declaration from `src/lib_bench.rs` if it is no longer used, or restore the missing file.

### 2.2. Potential Undefined Behavior: Alignment Violations in SIMD
* **Locations:**
  - `src/ags/ags_128_avx2.rs:32`
  - `src/ags/ags_64_avx2.rs:32`
* **Severity:** **High / Soundness Risk**
* **Description:** 
  In the `_chunks32` variants of SIMD conversion functions, the code performs the following cast and slice creation:
  ```rust
  buf2.copy_from_slice(from_raw_parts(buf_ptr as *const u64, 4));
  ```
  And later:
  ```rust
  *(buf_ptr as *mut u64) = buf2[0];
  ```
  The `buf_ptr` is derived from a `&mut [u8]` slice, which only guarantees 1-byte alignment. Casting it to `*const u64` and dereferencing it (or creating a slice from it) is **Undefined Behavior** in Rust if the pointer is not 8-byte aligned. While x86_64 hardware often handles unaligned loads/stores, the Rust compiler assumes alignment for these types, which can lead to miscompilation.
* **Recommendation:** Use `std::ptr::read_unaligned` and `std::ptr::write_unaligned`, or use `copy_nonoverlapping` to safely move data between the `u8` buffer and the aligned `u64` array.

---

## 3. Code Quality & Architectural Observations

### 3.1. Dispatch Logic Duplication
* **Locations:** `src/base32.rs`, `src/base64.rs`, `src/ags.rs`
* **Observation:** 
  The pattern for runtime CPU feature detection and dispatching to SIMD vs. scalar implementations is repeated across multiple modules. 
  ```rust
  if is_x86_feature_detected!("avx2") {
      unsafe { _encode_base32_avx2(...) }
  } else if is_x86_feature_detected!("ssse3") {
      ...
  ```
* **Suggestion:** Consider abstracting the dispatch logic into a macro or a internal trait to reduce boilerplate and ensure consistency in feature detection.

### 3.2. Use of `from_utf8_unchecked`
* **Observation:** 
  The library frequently uses `String::from_utf8_unchecked` after encoding. 
* **Risk:** While the internal logic ensures that only ASCII characters from the `AsciiGraphicSet` are used, any bug in the encoding logic or an incorrectly initialized character set could lead to invalid UTF-8 strings, which breaks Rust's safety guarantees.
* **Suggestion:** Ensure that `AsciiGraphicSet` validation is rigorous (which it appears to be in `with_slice`). Consider adding a debug-only check or a small comment justifying the safety.

### 3.3. Deprecated `black_box` in Benchmarks
* **Observation:** 
  The benchmarks generate numerous warnings: `use of deprecated function criterion::black_box: use std::hint::black_box() instead`.
* **Suggestion:** Update benchmarks to use `std::hint::black_box` for Rust 1.66+.

---

## 4. Summary of Recommendations

1. **Fix the benchmark build** by removing the reference to the non-existent `aligned_data` module.
2. **Resolve alignment UB** in SIMD modules by using unaligned read/write primitives.
3. **Refactor dispatch logic** to reduce duplication across codec modules.
4. **Update benchmark utilities** to eliminate deprecation warnings.

---
Review Date: 2026-05-26
Reviewer: Gemini CLI Agent