85 lines
3.0 KiB
Rust
85 lines
3.0 KiB
Rust
use crate::{BigDirectoryNode, DirectoryEntry, DirectoryNode, MerkleNode, MerkleTree, NodeHash};
|
|
use sha2::{Digest, Sha256};
|
|
|
|
const CHUNK: u8 = 0;
|
|
const DIRECTORY: u8 = 1;
|
|
const BIG: u8 = 2;
|
|
const BIGDIRECTORY: u8 = 3;
|
|
|
|
pub fn parse_received_datum(
|
|
recevied_datum: Vec<u8>,
|
|
datum_length: usize,
|
|
) -> Option<([u8; 32], MerkleNode)> {
|
|
let hash_name: [u8; 32] = recevied_datum[..32].try_into().expect("error");
|
|
let value = &recevied_datum[32..datum_length];
|
|
let value_slice = value.to_vec();
|
|
println!("valueslice: {:?}, {}", value_slice, value_slice.len());
|
|
let datum_type = value_slice[0];
|
|
match datum_type {
|
|
CHUNK => Some((
|
|
hash_name,
|
|
MerkleNode::Chunk(crate::ChunkNode { data: value_slice }),
|
|
)),
|
|
DIRECTORY => {
|
|
let mut dir_entries = Vec::new();
|
|
let mut offset = 1 as usize;
|
|
for i in 0..((value_slice.len() - 1) / 64) as u8 {
|
|
offset = (1 + 64 * i as usize) as usize;
|
|
println!("offset:{}, i:{}", offset, i);
|
|
let name = &value_slice[offset..offset + 32];
|
|
let mut hash = [0u8; 32];
|
|
hash.copy_from_slice(&value_slice[offset + 32..offset + 64]);
|
|
let dp_name = String::from_utf8(name.to_vec()).expect("err");
|
|
println!("name:{}", dp_name);
|
|
|
|
// envoyer un datum request
|
|
dir_entries.push(DirectoryEntry {
|
|
filename: name.try_into().expect("incorrect size"),
|
|
content_hash: hash,
|
|
});
|
|
}
|
|
|
|
let current = DirectoryNode::new(dir_entries);
|
|
match current {
|
|
Ok(current_node) => Some((hash_name, MerkleNode::Directory(current_node))),
|
|
Err(e) => {
|
|
println!("{}", e);
|
|
None
|
|
}
|
|
}
|
|
}
|
|
BIG => {
|
|
println!("its a BIG bro");
|
|
let chlidren: Vec<NodeHash> = Vec::new();
|
|
Some((
|
|
hash_name,
|
|
MerkleNode::Big(crate::BigNode {
|
|
children_hashes: chlidren,
|
|
}),
|
|
))
|
|
}
|
|
BIGDIRECTORY => {
|
|
let mut bigdir_entries: Vec<NodeHash> = Vec::new();
|
|
let mut offset = 1 as usize;
|
|
for i in 0..((value_slice.len() - 1) / 32) as u8 {
|
|
offset = (1 + 32 * i as usize) as usize;
|
|
println!("offset:{}, i:{}", offset, i);
|
|
let hash = &value_slice[offset..offset + 32];
|
|
|
|
// envoyer un datum request
|
|
bigdir_entries.push(hash.try_into().expect("incorrect size"));
|
|
}
|
|
|
|
let current = BigDirectoryNode::new(bigdir_entries);
|
|
match current {
|
|
Ok(current_node) => Some((hash_name, MerkleNode::BigDirectory(current_node))),
|
|
Err(e) => {
|
|
println!("{}", e);
|
|
None
|
|
}
|
|
}
|
|
}
|
|
_ => None,
|
|
}
|
|
}
|