From abea01cf9bbac41e973e0bc0e2e8b99576475ef3 Mon Sep 17 00:00:00 2001 From: femsci Date: Sun, 8 Jan 2023 22:52:38 +0100 Subject: [PATCH] Refactor search algo. Set enum from(u8) functions to implement the From trait. --- .vscode/launch.json | 9 ++++++ src/store/local.rs | 72 +++++++++++++++++++++++++++++++++------------ src/store/mod.rs | 40 ++++++++++++------------- 3 files changed, 82 insertions(+), 39 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 1934554..4777ea6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,6 +9,15 @@ "args": ["build"] }, "args": [] + }, + { + "type": "lldb", + "request": "launch", + "name": "Cargo test", + "cargo": { + "args": ["test"] + }, + "args": [] } ] } diff --git a/src/store/local.rs b/src/store/local.rs index 7a39167..209582f 100644 --- a/src/store/local.rs +++ b/src/store/local.rs @@ -98,7 +98,7 @@ struct LocalStoreMeta { specifier_count: u32, index_node_arity: u16, data_block_size: u32, - data_offset: u64 + data_offset: u64, } struct EncryptionContext { @@ -231,7 +231,7 @@ fn try_create_store(path: &String, passphrase: &Vec) -> bool { specifier_count: 0, index_node_arity: 64, data_block_size: 8092, - data_offset: 0 + data_offset: 0, }; superblock_buf.append(&mut serialize_store_meta(&meta)); @@ -315,14 +315,12 @@ fn deserialize_store_meta(data: &[u8]) -> LocalStoreMeta { let data_block_size = u32::from_be_bytes(data[10..14].try_into().unwrap()); let data_offset = u64::from_be_bytes(data[14..22].try_into().unwrap()); - - LocalStoreMeta { record_count: records, specifier_count: specifiers, index_node_arity, data_block_size, - data_offset + data_offset, } } @@ -519,8 +517,30 @@ impl Store for LocalStore { } } +enum IndexNodeType { + Internal, + Leaf, + Invalid, +} + +impl From for IndexNodeType { + fn from(value: u8) -> Self { + match value { + 0 => IndexNodeType::Invalid, + 1 => IndexNodeType::Internal, + 2 => IndexNodeType::Leaf, + _ => panic!("Invalid value {}", value), + } + } +} + +struct IndexNode { + node_type: IndexNodeType, + children: [u64], +} + impl LocalStore { - fn btree_find_key(&self, key: &String) { + fn btree_find_key(&self, key: &String) -> Option { let mut file: BufReader = BufReader::new(File::open(&self.path).expect("Cannot open file")); @@ -531,21 +551,35 @@ impl LocalStore { }; let pos = file.seek(SeekFrom::Start(0x90)).unwrap(); - - let mut buf: [u8; 16] = [0; 16]; - file.read_exact(&mut buf).expect("Read error"); - let key = u64::from_be_bytes(buf[0..8].try_into().unwrap()); + let mut read_node_type = || { + let mut buf: [u8; 2] = [0; 2]; + file.read_exact(&mut buf).expect("Cannot read node type."); + IndexNodeType::from(buf[0]) + }; - if hash > key { - //advance - todo!(); + let mut read_key_pos = || { + let mut buf: [u8; 16] = [0; 16]; + file.read_exact(&mut buf).expect("Read error"); + + ( + u64::from_be_bytes(buf[0..8].try_into().unwrap()), + u64::from_be_bytes(buf[8..16].try_into().unwrap()), + ) + }; + + let mut subnode_pos = 0; + loop { + let (key, offset) = read_key_pos(); + + if hash > key { + if subnode_pos > self.meta.index_node_arity { + return None; + } + subnode_pos += 1; + continue; + } } - - let pointer = u64::from_be_bytes(buf[8..16].try_into().unwrap()); - let pos2 = file.seek(SeekFrom::Start(pointer)).unwrap(); - - println!("Pos: {:x} 2: {:x}", pos, pos2); } } @@ -672,7 +706,7 @@ mod tests { key: "meow".to_owned(), meta: None, specifier: "test".to_owned(), - r#type: crate::store::RecordType::LOGIN_PASSWORD, + r#type: crate::store::RecordType::LoginPassword, value: vec![1, 2, 3, 4], }, }; diff --git a/src/store/mod.rs b/src/store/mod.rs index a577174..cd8a34e 100644 --- a/src/store/mod.rs +++ b/src/store/mod.rs @@ -12,25 +12,25 @@ pub struct StoreRecord { #[derive(Clone, Copy, PartialEq, PartialOrd, Debug)] pub enum RecordType { - LOGIN_PASSWORD = 0, - PASSWORD = 1, - SSH_KEY = 2, - PGP_KEY = 3, - SYM_KEY = 4, - ASYM_KEY_PAIR = 5, - ASYM_PRIV_KEY = 6, + LoginPassword = 0, + Password = 1, + SSHKey = 2, + PGPKey = 3, + SymKey = 4, + AsymKeyPair = 5, + AsymPrivKey = 6, } -impl RecordType { - pub fn from(n: u8) -> Self { +impl From for RecordType { + fn from(n: u8) -> Self { match n { - 0 => RecordType::LOGIN_PASSWORD, - 1 => RecordType::PASSWORD, - 2 => RecordType::SSH_KEY, - 3 => RecordType::PGP_KEY, - 4 => RecordType::SYM_KEY, - 5 => RecordType::ASYM_KEY_PAIR, - 6 => RecordType::ASYM_PRIV_KEY, + 0 => RecordType::LoginPassword, + 1 => RecordType::Password, + 2 => RecordType::SSHKey, + 3 => RecordType::PGPKey, + 4 => RecordType::SymKey, + 5 => RecordType::AsymKeyPair, + 6 => RecordType::AsymPrivKey, _ => panic!("Invalid value '{}'.", n), } } @@ -56,8 +56,8 @@ pub enum EncryptionType { Chacha20Poly1305 = 2, } -impl EncryptionType { - pub fn from(n: u8) -> Self { +impl From for EncryptionType { + fn from(n: u8) -> Self { match n { 0 => EncryptionType::Invalid, 1 => EncryptionType::AesGcm, @@ -75,8 +75,8 @@ pub enum HashType { Pbkdf2 = 4, } -impl HashType { - pub fn from(n: u8) -> Self { +impl From for HashType { + fn from(n: u8) -> Self { match n { 0 => HashType::Invalid, 1 => HashType::Argon2,