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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use alloc::{
    string::String,
    vec::{IntoIter, Vec},
};
use core::{
    cmp, fmt,
    iter::FromIterator,
    mem,
    ops::{Deref, Index, Range, RangeFrom, RangeFull, RangeTo},
    slice,
};

use datasize::DataSize;
use rand::{
    distributions::{Distribution, Standard},
    Rng,
};
use serde::{
    de::{Error as SerdeError, SeqAccess, Visitor},
    Deserialize, Deserializer, Serialize, Serializer,
};

use super::{Error, FromBytes, ToBytes};
use crate::{CLType, CLTyped};

/// A newtype wrapper for bytes that has efficient serialization routines.
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Default, Hash)]
pub struct Bytes(Vec<u8>);

impl Bytes {
    /// Constructs a new, empty vector of bytes.
    pub fn new() -> Bytes {
        Bytes::default()
    }

    /// Returns reference to inner container.
    #[inline]
    pub fn inner_bytes(&self) -> &Vec<u8> {
        &self.0
    }

    /// Extracts a slice containing the entire vector.
    pub fn as_slice(&self) -> &[u8] {
        self
    }
}

impl Deref for Bytes {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.0.deref()
    }
}

impl From<Vec<u8>> for Bytes {
    fn from(vec: Vec<u8>) -> Self {
        Self(vec)
    }
}

impl From<Bytes> for Vec<u8> {
    fn from(bytes: Bytes) -> Self {
        bytes.0
    }
}

impl From<&[u8]> for Bytes {
    fn from(bytes: &[u8]) -> Self {
        Self(bytes.to_vec())
    }
}

impl CLTyped for Bytes {
    fn cl_type() -> CLType {
        <Vec<u8>>::cl_type()
    }
}

impl AsRef<[u8]> for Bytes {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl ToBytes for Bytes {
    #[inline(always)]
    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
        super::vec_u8_to_bytes(&self.0)
    }

    #[inline(always)]
    fn into_bytes(self) -> Result<Vec<u8>, Error> {
        super::vec_u8_to_bytes(&self.0)
    }

    #[inline(always)]
    fn serialized_length(&self) -> usize {
        super::vec_u8_serialized_length(&self.0)
    }
}

impl FromBytes for Bytes {
    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), super::Error> {
        let (size, remainder) = u32::from_bytes(bytes)?;
        let (result, remainder) = super::safe_split_at(remainder, size as usize)?;
        Ok((Bytes(result.to_vec()), remainder))
    }

    fn from_vec(stream: Vec<u8>) -> Result<(Self, Vec<u8>), Error> {
        let (size, mut stream) = u32::from_vec(stream)?;

        if size as usize > stream.len() {
            Err(Error::EarlyEndOfStream)
        } else {
            let remainder = stream.split_off(size as usize);
            Ok((Bytes(stream), remainder))
        }
    }
}

impl Index<usize> for Bytes {
    type Output = u8;

    fn index(&self, index: usize) -> &u8 {
        let Bytes(ref dat) = self;
        &dat[index]
    }
}

impl Index<Range<usize>> for Bytes {
    type Output = [u8];

    fn index(&self, index: Range<usize>) -> &[u8] {
        let &Bytes(ref dat) = self;
        &dat[index]
    }
}

impl Index<RangeTo<usize>> for Bytes {
    type Output = [u8];

    fn index(&self, index: RangeTo<usize>) -> &[u8] {
        let &Bytes(ref dat) = self;
        &dat[index]
    }
}

impl Index<RangeFrom<usize>> for Bytes {
    type Output = [u8];

    fn index(&self, index: RangeFrom<usize>) -> &[u8] {
        let &Bytes(ref dat) = self;
        &dat[index]
    }
}

impl Index<RangeFull> for Bytes {
    type Output = [u8];

    fn index(&self, _: RangeFull) -> &[u8] {
        let &Bytes(ref dat) = self;
        &dat[..]
    }
}

impl FromIterator<u8> for Bytes {
    #[inline]
    fn from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Bytes {
        let vec = Vec::from_iter(iter);
        Bytes(vec)
    }
}

impl<'a> IntoIterator for &'a Bytes {
    type Item = &'a u8;

    type IntoIter = slice::Iter<'a, u8>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl IntoIterator for Bytes {
    type Item = u8;

    type IntoIter = IntoIter<u8>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl DataSize for Bytes {
    const IS_DYNAMIC: bool = true;

    const STATIC_HEAP_SIZE: usize = 0;

    fn estimate_heap_size(&self) -> usize {
        self.0.capacity() * mem::size_of::<u8>()
    }
}

const RANDOM_BYTES_MAX_LENGTH: usize = 100;

impl Distribution<Bytes> for Standard {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Bytes {
        let len = rng.gen_range(0..RANDOM_BYTES_MAX_LENGTH);
        let mut result = Vec::with_capacity(len);
        for _ in 0..len {
            result.push(rng.gen());
        }
        result.into()
    }
}

struct BytesVisitor;

impl<'de> Visitor<'de> for BytesVisitor {
    type Value = Bytes;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("byte array")
    }

    fn visit_seq<V>(self, mut visitor: V) -> Result<Bytes, V::Error>
    where
        V: SeqAccess<'de>,
    {
        let len = cmp::min(visitor.size_hint().unwrap_or(0), 4096);
        let mut bytes = Vec::with_capacity(len);

        while let Some(b) = visitor.next_element()? {
            bytes.push(b);
        }

        Ok(Bytes::from(bytes))
    }

    fn visit_bytes<E>(self, v: &[u8]) -> Result<Bytes, E>
    where
        E: SerdeError,
    {
        Ok(Bytes::from(v))
    }

    fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Bytes, E>
    where
        E: SerdeError,
    {
        Ok(Bytes::from(v))
    }

    fn visit_str<E>(self, v: &str) -> Result<Bytes, E>
    where
        E: SerdeError,
    {
        Ok(Bytes::from(v.as_bytes()))
    }

    fn visit_string<E>(self, v: String) -> Result<Bytes, E>
    where
        E: SerdeError,
    {
        Ok(Bytes::from(v.into_bytes()))
    }
}

impl<'de> Deserialize<'de> for Bytes {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        if deserializer.is_human_readable() {
            let hex_string = String::deserialize(deserializer)?;
            base16::decode(&hex_string)
                .map(Bytes)
                .map_err(SerdeError::custom)
        } else {
            let bytes = deserializer.deserialize_byte_buf(BytesVisitor)?;
            Ok(bytes)
        }
    }
}

impl Serialize for Bytes {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if serializer.is_human_readable() {
            base16::encode_lower(&self.0).serialize(serializer)
        } else {
            serializer.serialize_bytes(&self.0)
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::bytesrepr::{self, Error, FromBytes, ToBytes, U32_SERIALIZED_LENGTH};
    use alloc::vec::Vec;

    use serde_json::json;
    use serde_test::{assert_tokens, Configure, Token};

    use super::Bytes;

    const TRUTH: &[u8] = &[0xde, 0xad, 0xbe, 0xef];

    #[test]
    fn vec_u8_from_bytes() {
        let data: Bytes = vec![1, 2, 3, 4, 5].into();
        let data_bytes = data.to_bytes().unwrap();
        assert!(Bytes::from_bytes(&data_bytes[..U32_SERIALIZED_LENGTH / 2]).is_err());
        assert!(Bytes::from_bytes(&data_bytes[..U32_SERIALIZED_LENGTH]).is_err());
        assert!(Bytes::from_bytes(&data_bytes[..U32_SERIALIZED_LENGTH + 2]).is_err());
    }

    #[test]
    fn should_serialize_deserialize_bytes() {
        let data: Bytes = vec![1, 2, 3, 4, 5].into();
        bytesrepr::test_serialization_roundtrip(&data);
    }

    #[test]
    fn should_fail_to_serialize_deserialize_malicious_bytes() {
        let data: Bytes = vec![1, 2, 3, 4, 5].into();
        let mut serialized = data.to_bytes().expect("should serialize data");
        serialized = serialized[..serialized.len() - 1].to_vec();
        let res: Result<(_, &[u8]), Error> = Bytes::from_bytes(&serialized);
        assert_eq!(res.unwrap_err(), Error::EarlyEndOfStream);
    }

    #[test]
    fn should_serialize_deserialize_bytes_and_keep_rem() {
        let data: Bytes = vec![1, 2, 3, 4, 5].into();
        let expected_rem: Vec<u8> = vec![6, 7, 8, 9, 10];
        let mut serialized = data.to_bytes().expect("should serialize data");
        serialized.extend(&expected_rem);
        let (deserialized, rem): (Bytes, &[u8]) =
            FromBytes::from_bytes(&serialized).expect("should deserialize data");
        assert_eq!(data, deserialized);
        assert_eq!(&rem, &expected_rem);
    }

    #[test]
    fn should_ser_de_human_readable() {
        let truth = vec![0xde, 0xad, 0xbe, 0xef];

        let bytes_ser: Bytes = truth.clone().into();

        let json_object = serde_json::to_value(bytes_ser).unwrap();
        assert_eq!(json_object, json!("deadbeef"));

        let bytes_de: Bytes = serde_json::from_value(json_object).unwrap();
        assert_eq!(bytes_de, Bytes::from(truth));
    }

    #[test]
    fn should_ser_de_readable() {
        let truth: Bytes = TRUTH.into();
        assert_tokens(&truth.readable(), &[Token::Str("deadbeef")]);
    }

    #[test]
    fn should_ser_de_compact() {
        let truth: Bytes = TRUTH.into();
        assert_tokens(&truth.compact(), &[Token::Bytes(TRUTH)]);
    }
}

#[cfg(test)]
pub mod gens {
    use super::Bytes;
    use proptest::{
        collection::{vec, SizeRange},
        prelude::*,
    };

    pub fn bytes_arb(size: impl Into<SizeRange>) -> impl Strategy<Value = Bytes> {
        vec(any::<u8>(), size).prop_map(Bytes::from)
    }
}