pub mod deserializer;
pub mod serializer;
use crate::aligned::rup;
use crate::aligned::HSIZE;
use crate::Compressor;
use crate::DataChunk;
use crate::DataChunkTrait;
use crate::EncryptedDataChunk;
use crate::HashCow;
use crate::PsDataChunkError;
use ps_hash::Hash;
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct OwnedDataChunk {
hash: Arc<Hash>,
data: Vec<u8>,
}
impl OwnedDataChunk {
pub fn data_ref(&self) -> &[u8] {
&self.data
}
pub fn hash_ref(&self) -> &[u8] {
self.hash.as_bytes()
}
pub fn hash(&self) -> Arc<Hash> {
self.hash.clone()
}
pub fn from_parts(data: Vec<u8>, hash: Arc<Hash>) -> Self {
Self { data, hash }
}
pub fn from_data(data: Vec<u8>) -> Self {
let hash = ps_hash::hash(&data);
Self::from_parts(data, hash.into())
}
pub fn from_data_ref_and_hash(data: &[u8], hash: Arc<Hash>) -> Self {
let reserved_size = rup(data.len(), 6) + rup(HSIZE, 6);
let mut data_vec = Vec::with_capacity(reserved_size);
data_vec.extend_from_slice(data);
Self::from_parts(data_vec, hash)
}
pub fn from_data_ref(data: &[u8]) -> Self {
Self::from_data_ref_and_hash(data, ps_hash::hash(data).into())
}
#[inline(always)]
pub fn serialize_into(mut self) -> Vec<u8> {
serializer::serialize_vec_with_known_hash(&mut self.data, self.hash.as_bytes());
return self.data;
}
#[inline(always)]
pub fn serialize(&self) -> Vec<u8> {
serializer::serialize_bytes_with_known_hash(&self.data, self.hash_ref())
}
#[inline(always)]
pub fn deserialize_from(data: Vec<u8>) -> Result<Self, PsDataChunkError> {
let (data, hash) = deserializer::deserialize_vec_to_parts(data)?;
Ok(Self {
data,
hash: hash.into(),
})
}
#[inline(always)]
pub fn deserialize(data: &[u8]) -> Result<Self, PsDataChunkError> {
Self::deserialize_from(data.to_vec())
}
#[inline(always)]
pub fn decrypt_bytes(
encrypted: &[u8],
key: &[u8],
compressor: &Compressor,
) -> Result<Self, PsDataChunkError> {
let decrypted = ps_cypher::decrypt(encrypted, key, compressor)?;
Self::deserialize_from(decrypted)
}
#[inline(always)]
pub fn decrypt(&self, key: &[u8], compressor: &Compressor) -> Result<Self, PsDataChunkError> {
Self::decrypt_bytes(&self.data, key, compressor)
}
#[inline(always)]
pub fn encrypt_bytes(
bytes: &[u8],
compressor: &Compressor,
) -> Result<EncryptedDataChunk, PsDataChunkError> {
let encrypted = ps_cypher::encrypt(bytes, compressor)?;
Ok(EncryptedDataChunk {
chunk: OwnedDataChunk {
data: encrypted.bytes,
hash: encrypted.hash.into(),
},
key: encrypted.key.into(),
})
}
#[inline(always)]
pub fn encrypt(&self, compressor: &Compressor) -> Result<EncryptedDataChunk, PsDataChunkError> {
Self::encrypt_bytes(&self.serialize(), compressor)
}
#[inline(always)]
pub fn encrypt_mut(
&mut self,
compressor: &Compressor,
) -> Result<EncryptedDataChunk, PsDataChunkError> {
let data_length = self.data.len();
serializer::serialize_vec_with_known_hash(&mut self.data, self.hash.as_bytes());
let encrypted = Self::encrypt_bytes(&self.data, compressor);
self.data.truncate(data_length);
return encrypted;
}
}
impl DataChunkTrait for OwnedDataChunk {
fn data_ref(&self) -> &[u8] {
self.data_ref()
}
fn hash_ref(&self) -> &[u8] {
self.hash_ref()
}
fn hash(&self) -> HashCow {
HashCow::from_arc(self.hash())
}
}
impl<'lt> From<DataChunk<'lt>> for OwnedDataChunk {
fn from(value: DataChunk<'lt>) -> Self {
match value {
DataChunk::Owned(owned) => owned,
_ => OwnedDataChunk::from_data_ref_and_hash(value.data_ref(), value.hash().into()),
}
}
}