1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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)]
/// represents an owned chunk of data
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()
    }

    /// Creates an OwnedDataChunk from its constituent parts
    /// # Safety
    /// - `hash` must be the hash of `data`
    /// - use `from_data()` if you cannot ensure this
    pub fn from_parts(data: Vec<u8>, hash: Arc<Hash>) -> Self {
        Self { data, hash }
    }

    /// calculates the hash of `data` and returns an `OwnedDataChunk`
    pub fn from_data(data: Vec<u8>) -> Self {
        let hash = ps_hash::hash(&data);

        Self::from_parts(data, hash.into())
    }

    /// creates an `OwnedDataChunk` with given `data` and `hash`
    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)
    }

    /// creates an `OwnedDataChunk` with given `data`
    pub fn from_data_ref(data: &[u8]) -> Self {
        Self::from_data_ref_and_hash(data, ps_hash::hash(data).into())
    }

    #[inline(always)]
    /// converts this `OwnedDataChunk` into a `Vec<u8>`
    /// - extends `self.hash`
    /// - returns `self.data`
    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)]
    /// serializes this `OwnedDataChunk` into a newly allocated `Vec<u8>`
    /// - allocated a new `Vec<u8>`
    /// - copies `self.data` into the new `Vec<u8>`
    /// - copies `self.hash` into the new `Vec<u8>`
    /// - returns the new `Vec<u8>`
    pub fn serialize(&self) -> Vec<u8> {
        serializer::serialize_bytes_with_known_hash(&self.data, self.hash_ref())
    }

    #[inline(always)]
    /// - converts a `Vec<u8>` into an `OwnedDataChunk`
    /// - performs hash validation
    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)]
    /// Copies `data` into a new `Vec<u8>` and deserializes it into an `OwnedDataChunk`.
    pub fn deserialize(data: &[u8]) -> Result<Self, PsDataChunkError> {
        Self::deserialize_from(data.to_vec())
    }

    #[inline(always)]
    /// Decrypts into an `OwnedDataChunk` with the given `key`
    /// - performs hash validation
    /// - fails if `key` not correct
    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)]
    /// Decrypts an `OwnedDataChunk` with the given `key`.
    /// - performs hash validation
    /// - fails if `key` not correct
    pub fn decrypt(&self, key: &[u8], compressor: &Compressor) -> Result<Self, PsDataChunkError> {
        Self::decrypt_bytes(&self.data, key, compressor)
    }

    #[inline(always)]
    /// Encrypts a serialized [DataChunk].
    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)]
    /// Encrypts this [DataChunk].
    pub fn encrypt(&self, compressor: &Compressor) -> Result<EncryptedDataChunk, PsDataChunkError> {
        Self::encrypt_bytes(&self.serialize(), compressor)
    }

    #[inline(always)]
    /// Encrypts this [DataChunk].
    /// - optimized by using `self.data` as the serialization buffer
    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()),
        }
    }
}