fix localstorage error
This commit is contained in:
parent
cef372377b
commit
d5bc1bed11
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -136,6 +136,12 @@ version = "1.5.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "base64"
|
||||||
|
version = "0.22.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bincode"
|
name = "bincode"
|
||||||
version = "1.3.3"
|
version = "1.3.3"
|
||||||
@ -3694,6 +3700,7 @@ dependencies = [
|
|||||||
name = "ytbn_graphing_software"
|
name = "ytbn_graphing_software"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"base64",
|
||||||
"bincode",
|
"bincode",
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"const_format",
|
"const_format",
|
||||||
|
|||||||
@ -57,6 +57,7 @@ static_assertions = "1.1"
|
|||||||
bincode = "1.3"
|
bincode = "1.3"
|
||||||
serde = "1"
|
serde = "1"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
base64 = "0.22"
|
||||||
|
|
||||||
# Note: benchmarks are in a separate crate - run with:
|
# Note: benchmarks are in a separate crate - run with:
|
||||||
# cd benchmarks && cargo bench
|
# cd benchmarks && cargo bench
|
||||||
|
|||||||
@ -176,6 +176,7 @@ impl MathApp {
|
|||||||
|
|
||||||
// TODO: stabilize FunctionManager serialize so it can persist across builds
|
// TODO: stabilize FunctionManager serialize so it can persist across builds
|
||||||
let (commit, func_data) = crate::misc::hashed_storage_read(&data)?;
|
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) } {
|
if commit == unsafe { std::mem::transmute::<&str, &[u8]>(build::SHORT_COMMIT) } {
|
||||||
|
|||||||
23
src/misc.rs
23
src/misc.rs
@ -179,24 +179,27 @@ pub type HashBytes = [u8; HASH_LENGTH];
|
|||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn hashed_storage_create(hashbytes: HashBytes, data: &[u8]) -> String {
|
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)]
|
#[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
|
// Make sure data is long enough to decode
|
||||||
if HASH_LENGTH >= data.len() {
|
if HASH_LENGTH >= decoded.len() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transmute data into slice
|
// Extract hash and data
|
||||||
let decoded_1: &[u8] = unsafe { std::mem::transmute::<&str, &[u8]>(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((hash, data_part))
|
||||||
Some((
|
|
||||||
unsafe { *(decoded_1[..HASH_LENGTH].as_ptr() as *const HashBytes) },
|
|
||||||
&decoded_1[HASH_LENGTH..],
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates and returns random u64
|
/// Creates and returns random u64
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user