fix localstorage error

This commit is contained in:
Simon Gardling 2025-12-03 00:25:25 -05:00
parent cef372377b
commit d5bc1bed11
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
4 changed files with 22 additions and 10 deletions

7
Cargo.lock generated
View File

@ -136,6 +136,12 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "base64"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bincode"
version = "1.3.3"
@ -3694,6 +3700,7 @@ dependencies = [
name = "ytbn_graphing_software"
version = "0.1.0"
dependencies = [
"base64",
"bincode",
"cfg-if",
"const_format",

View File

@ -57,6 +57,7 @@ static_assertions = "1.1"
bincode = "1.3"
serde = "1"
log = "0.4"
base64 = "0.22"
# Note: benchmarks are in a separate crate - run with:
# cd benchmarks && cargo bench

View File

@ -176,6 +176,7 @@ impl MathApp {
// TODO: stabilize FunctionManager serialize so it can persist across builds
let (commit, func_data) = crate::misc::hashed_storage_read(&data)?;
let func_data: &[u8] = &func_data;
if commit == unsafe { std::mem::transmute::<&str, &[u8]>(build::SHORT_COMMIT) } {

View File

@ -179,24 +179,27 @@ pub type HashBytes = [u8; HASH_LENGTH];
#[allow(dead_code)]
pub fn hashed_storage_create(hashbytes: HashBytes, data: &[u8]) -> String {
unsafe { std::mem::transmute::<Vec<u8>, String>([hashbytes.to_vec(), data.to_vec()].concat()) }
// Use base64 encoding to safely store binary data in localStorage
let combined: Vec<u8> = [hashbytes.to_vec(), data.to_vec()].concat();
base64::encode(combined)
}
#[allow(dead_code)]
pub fn hashed_storage_read(data: &str) -> Option<(HashBytes, &[u8])> {
pub fn hashed_storage_read(data: &str) -> Option<(HashBytes, Vec<u8>)> {
// Decode base64 data
let decoded = base64::decode(data).ok()?;
// Make sure data is long enough to decode
if HASH_LENGTH >= data.len() {
if HASH_LENGTH >= decoded.len() {
return None;
}
// Transmute data into slice
let decoded_1: &[u8] = unsafe { std::mem::transmute::<&str, &[u8]>(data) };
// Extract hash and data
let mut hash: HashBytes = [0u8; HASH_LENGTH];
hash.copy_from_slice(&decoded[..HASH_LENGTH]);
let data_part = decoded[HASH_LENGTH..].to_vec();
// Return hash and decoded data
Some((
unsafe { *(decoded_1[..HASH_LENGTH].as_ptr() as *const HashBytes) },
&decoded_1[HASH_LENGTH..],
))
Some((hash, data_part))
}
/// Creates and returns random u64