PKBuffer
PKBuffer is a simple but flexible buffer library for reading and writing data to byte buffers using safe byte-copy operations. It was born of multiple memory parsing scenarios such as executable header parsing and game ROM memory mapping. Its name comes from the magic that is casting arbitrary structure definitions at byte buffers and the fact that I was reverse engineering the game EarthBound at the time.
You can read the documentation here, and see various use examples in the test file. The changelog can be found here.
Quick Start
use ;
let mut buffer = new;
buffer.;
buffer.;
buffer.;
assert_eq!;
Aligned Reference Access (0.7.0+)
For well-aligned data, use get_aligned_ref and get_aligned_mut to get direct references:
use ;
let mut buffer = with_initial_size;
buffer..unwrap;
// Get a reference at an aligned offset
let val: &u32 = buffer.get_aligned_ref.unwrap;
assert_eq!;
// Get a mutable reference for in-place modification
let val: &mut u32 = buffer.get_aligned_mut.unwrap;
*val = 0xFEEDFACE; // writes directly to buffer
// Misaligned access returns an error
assert!;
// Error::AlignmentMismatch(offset, required_alignment)
Key Features
- Safe byte-copy reads/writes: Use
read_val<T>()andwrite_val<T>()to read/write anyCopytype without alignment requirements - Two buffer types:
PtrBufferfor unowned memory andVecBufferfor owned data - Rich search functionality: Search for byte patterns with
search()and dynamic patterns with wildcards viasearch_dynamic() - No unsafe pointer casting: All type conversions use safe byte-copying instead of pointer casting
- Aligned reference access:
get_aligned_refandget_aligned_mutprovide direct references with alignment verification
API Changes in 0.6.0
This version removes the Castable trait and all methods that relied on unsafe pointer casting for type conversions. The new API uses safe byte-copy operations exclusively:
read_val<T: Copy>(offset)replacesget_ref<T: Castable>(offset)write_val<T: Copy>(offset, &data)replaceswrite_ref<T: Castable>(offset, &data)search(bytes)replacessearch_ref(data)contains(bytes)replacescontains_ref(data)
All Copy types (primitives like u8, u16, u32, etc.) work directly with the new API. No trait derivations required.