aws_smithy_eventstream/buf/
crc.rs

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
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! Utilities for calculating CRC-32 while reading from a [`Buf`] or writing to a [`BufMut`].

use bytes::buf::UninitSlice;
use bytes::{Buf, BufMut};
use crc32fast::Hasher;

/// Implementation of [`Buf`] that calculates a CRC-32 checksum of the data
/// being read from an underlying `Buf` instance.
pub(crate) struct CrcBuf<'a, B>
where
    B: Buf,
{
    buffer: &'a mut B,
    crc: Hasher,
}

impl<'a, B> CrcBuf<'a, B>
where
    B: Buf,
{
    /// Creates a new `CrcBuf` by wrapping the given `buffer`.
    pub(crate) fn new(buffer: &'a mut B) -> Self {
        CrcBuf {
            buffer,
            crc: Hasher::new(),
        }
    }

    /// Consumes the `CrcBuf` and returns the calculated checksum.
    pub(crate) fn into_crc(self) -> u32 {
        self.crc.finalize()
    }
}

impl<'a, B> Buf for CrcBuf<'a, B>
where
    B: Buf,
{
    fn remaining(&self) -> usize {
        self.buffer.remaining()
    }

    fn chunk(&self) -> &[u8] {
        self.buffer.chunk()
    }

    fn advance(&mut self, cnt: usize) {
        let chunk = self.buffer.chunk();
        self.crc.update(&chunk[0..cnt]);
        self.buffer.advance(cnt);
    }
}

#[cfg(test)]
mod crc_buf_tests {
    use super::CrcBuf;
    use bytes::Buf;

    #[test]
    fn crc_no_data_read() {
        let mut data: &[u8] = &[];
        let buf = CrcBuf::new(&mut data);
        assert_eq!(0, buf.into_crc());
    }

    #[test]
    fn crc_data_read() {
        let mut data: &[u8] = &[0, 0, 0, 5, 0, 10u8];
        let mut buf = CrcBuf::new(&mut data);
        assert_eq!(5, buf.get_i32());
        assert_eq!(0x512E2B93, buf.into_crc());

        let mut data: &[u8] = &[0, 0, 0, 5, 0, 10u8];
        let mut buf = CrcBuf::new(&mut data);
        assert_eq!(5, buf.get_i32());
        assert_eq!(10, buf.get_i16());
        assert_eq!(0x57DC8A56, buf.into_crc());
    }

    #[test]
    fn chunk_called_multiple_times_before_advance() {
        let mut data: &[u8] = &[0, 0, 0, 5, 0, 10u8];
        let mut buf = CrcBuf::new(&mut data);
        for _ in 0..3 {
            buf.chunk();
        }
        buf.advance(4);
        assert_eq!(10, buf.get_i16());
        assert_eq!(0x57DC8A56, buf.into_crc());
    }
}

/// Implementation of [`BufMut`] that calculates a CRC-32 checksum of the data
/// being written to an underlying `Buf` instance, with a function to then write
/// the calculated CRC-32 to the buffer.
pub(crate) struct CrcBufMut<'a> {
    buffer: &'a mut dyn BufMut,
    crc: Hasher,
}

impl<'a> CrcBufMut<'a> {
    /// Creates a new `CrcBufMut` by wrapping the given `buffer`.
    pub(crate) fn new(buffer: &'a mut dyn BufMut) -> Self {
        CrcBufMut {
            buffer,
            crc: Hasher::new(),
        }
    }

    /// Puts the calculated CRC-32 to the buffer as a Big Endian 32-bit integer.
    /// This can be called multiple times, and each successive call will include
    /// the previously written checksum in its new checksum.
    pub(crate) fn put_crc(&mut self) {
        self.put_u32(self.crc.clone().finalize());
    }
}

unsafe impl<'a> BufMut for CrcBufMut<'a> {
    fn remaining_mut(&self) -> usize {
        self.buffer.remaining_mut()
    }

    unsafe fn advance_mut(&mut self, cnt: usize) {
        // Safety: There is no guarantee the bytes being advanced are initialized, which is why
        // this trait method is unsafe. The best we can do is assume they have been initialized
        // by the caller before `advance_mut` was called.
        let written = std::slice::from_raw_parts_mut(self.chunk_mut().as_mut_ptr(), cnt);
        self.crc.update(written);
        self.buffer.advance_mut(cnt);
    }

    fn chunk_mut(&mut self) -> &mut UninitSlice {
        self.buffer.chunk_mut()
    }
}

#[cfg(test)]
mod crc_buf_mut_tests {
    use super::CrcBufMut;
    use bytes::BufMut;

    #[test]
    fn crc_no_bytes_written() {
        let mut buffer: Vec<u8> = Vec::new();
        let mut crc_buf = CrcBufMut::new(&mut buffer);
        crc_buf.put_crc();
        crc_buf.put_crc();
        assert_eq!(vec![0, 0, 0, 0u8, 0x21, 0x44, 0xDF, 0x1C], buffer);
    }

    #[test]
    fn crc_bytes_written() {
        let mut buffer: Vec<u8> = Vec::new();
        let mut crc_buf = CrcBufMut::new(&mut buffer);
        crc_buf.put_u32(5);
        crc_buf.put_crc();
        assert_eq!(vec![0, 0, 0, 5, 0x51, 0x2E, 0x2B, 0x93], buffer);
    }
}