/rust/registry/src/github.com-1ecc6299db9ec823/flate2-1.0.22/src/crc.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Simple CRC bindings backed by miniz.c |
2 | | |
3 | | use std::io; |
4 | | use std::io::prelude::*; |
5 | | |
6 | | use crc32fast::Hasher; |
7 | | |
8 | | /// The CRC calculated by a [`CrcReader`]. |
9 | | /// |
10 | | /// [`CrcReader`]: struct.CrcReader.html |
11 | 0 | #[derive(Debug)] |
12 | | pub struct Crc { |
13 | | amt: u32, |
14 | | hasher: Hasher, |
15 | | } |
16 | | |
17 | | /// A wrapper around a [`Read`] that calculates the CRC. |
18 | | /// |
19 | | /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html |
20 | 0 | #[derive(Debug)] |
21 | | pub struct CrcReader<R> { |
22 | | inner: R, |
23 | | crc: Crc, |
24 | | } |
25 | | |
26 | | impl Crc { |
27 | | /// Create a new CRC. |
28 | 5.94k | pub fn new() -> Crc { |
29 | 5.94k | Crc { |
30 | 5.94k | amt: 0, |
31 | 5.94k | hasher: Hasher::new(), |
32 | 5.94k | } |
33 | 5.94k | } |
34 | | |
35 | | /// Returns the current crc32 checksum. |
36 | 221 | pub fn sum(&self) -> u32 { |
37 | 221 | self.hasher.clone().finalize() |
38 | 221 | } |
39 | | |
40 | | /// The number of bytes that have been used to calculate the CRC. |
41 | | /// This value is only accurate if the amount is lower than 2<sup>32</sup>. |
42 | 31 | pub fn amount(&self) -> u32 { |
43 | 31 | self.amt |
44 | 31 | } |
45 | | |
46 | | /// Update the CRC with the bytes in `data`. |
47 | 22.9M | pub fn update(&mut self, data: &[u8]) { |
48 | 22.9M | self.amt = self.amt.wrapping_add(data.len() as u32); |
49 | 22.9M | self.hasher.update(data); |
50 | 22.9M | } |
51 | | |
52 | | /// Reset the CRC. |
53 | 0 | pub fn reset(&mut self) { |
54 | 0 | self.amt = 0; |
55 | 0 | self.hasher.reset(); |
56 | 0 | } |
57 | | |
58 | | /// Combine the CRC with the CRC for the subsequent block of bytes. |
59 | 0 | pub fn combine(&mut self, additional_crc: &Crc) { |
60 | 0 | self.amt += additional_crc.amt; |
61 | 0 | self.hasher.combine(&additional_crc.hasher); |
62 | 0 | } |
63 | | } |
64 | | |
65 | | impl<R: Read> CrcReader<R> { |
66 | | /// Create a new CrcReader. |
67 | 2.97k | pub fn new(r: R) -> CrcReader<R> { |
68 | 2.97k | CrcReader { |
69 | 2.97k | inner: r, |
70 | 2.97k | crc: Crc::new(), |
71 | 2.97k | } |
72 | 2.97k | } <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata_rust::http2::decompression::HTTP2cursor>>>>::new Line | Count | Source | 67 | 2.97k | pub fn new(r: R) -> CrcReader<R> { | 68 | 2.97k | CrcReader { | 69 | 2.97k | inner: r, | 70 | 2.97k | crc: Crc::new(), | 71 | 2.97k | } | 72 | 2.97k | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_>>::new |
73 | | } |
74 | | |
75 | | impl<R> CrcReader<R> { |
76 | | /// Get the Crc for this CrcReader. |
77 | 211 | pub fn crc(&self) -> &Crc { |
78 | 211 | &self.crc |
79 | 211 | } <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata_rust::http2::decompression::HTTP2cursor>>>>::crc Line | Count | Source | 77 | 211 | pub fn crc(&self) -> &Crc { | 78 | 211 | &self.crc | 79 | 211 | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_>>::crc |
80 | | |
81 | | /// Get the reader that is wrapped by this CrcReader. |
82 | 0 | pub fn into_inner(self) -> R { |
83 | 0 | self.inner |
84 | 0 | } |
85 | | |
86 | | /// Get the reader that is wrapped by this CrcReader by reference. |
87 | 0 | pub fn get_ref(&self) -> &R { |
88 | 0 | &self.inner |
89 | 0 | } |
90 | | |
91 | | /// Get a mutable reference to the reader that is wrapped by this CrcReader. |
92 | 130k | pub fn get_mut(&mut self) -> &mut R { |
93 | 130k | &mut self.inner |
94 | 130k | } <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata_rust::http2::decompression::HTTP2cursor>>>>::get_mut Line | Count | Source | 92 | 130k | pub fn get_mut(&mut self) -> &mut R { | 93 | 130k | &mut self.inner | 94 | 130k | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_>>::get_mut |
95 | | |
96 | | /// Reset the Crc in this CrcReader. |
97 | 0 | pub fn reset(&mut self) { |
98 | 0 | self.crc.reset(); |
99 | 0 | } Unexecuted instantiation: <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata_rust::http2::decompression::HTTP2cursor>>>>::reset Unexecuted instantiation: <flate2::crc::CrcReader<_>>::reset |
100 | | } |
101 | | |
102 | | impl<R: Read> Read for CrcReader<R> { |
103 | 390k | fn read(&mut self, into: &mut [u8]) -> io::Result<usize> { |
104 | 390k | let amt = self.inner.read(into)?; |
105 | 361k | self.crc.update(&into[..amt]); |
106 | 361k | Ok(amt) |
107 | 390k | } <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata_rust::http2::decompression::HTTP2cursor>>> as std::io::Read>::read Line | Count | Source | 103 | 390k | fn read(&mut self, into: &mut [u8]) -> io::Result<usize> { | 104 | 390k | let amt = self.inner.read(into)?; | 105 | 361k | self.crc.update(&into[..amt]); | 106 | 361k | Ok(amt) | 107 | 390k | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_> as std::io::Read>::read |
108 | | } |
109 | | |
110 | | impl<R: BufRead> BufRead for CrcReader<R> { |
111 | 0 | fn fill_buf(&mut self) -> io::Result<&[u8]> { |
112 | 0 | self.inner.fill_buf() |
113 | 0 | } |
114 | 0 | fn consume(&mut self, amt: usize) { |
115 | 0 | if let Ok(data) = self.inner.fill_buf() { |
116 | 0 | self.crc.update(&data[..amt]); |
117 | 0 | } |
118 | 0 | self.inner.consume(amt); |
119 | 0 | } |
120 | | } |
121 | | |
122 | | /// A wrapper around a [`Write`] that calculates the CRC. |
123 | | /// |
124 | | /// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html |
125 | 0 | #[derive(Debug)] |
126 | | pub struct CrcWriter<W> { |
127 | | inner: W, |
128 | | crc: Crc, |
129 | | } |
130 | | |
131 | | impl<W> CrcWriter<W> { |
132 | | /// Get the Crc for this CrcWriter. |
133 | 0 | pub fn crc(&self) -> &Crc { |
134 | 0 | &self.crc |
135 | 0 | } |
136 | | |
137 | | /// Get the writer that is wrapped by this CrcWriter. |
138 | 0 | pub fn into_inner(self) -> W { |
139 | 0 | self.inner |
140 | 0 | } |
141 | | |
142 | | /// Get the writer that is wrapped by this CrcWriter by reference. |
143 | 0 | pub fn get_ref(&self) -> &W { |
144 | 0 | &self.inner |
145 | 0 | } |
146 | | |
147 | | /// Get a mutable reference to the writer that is wrapped by this CrcWriter. |
148 | 0 | pub fn get_mut(&mut self) -> &mut W { |
149 | 0 | &mut self.inner |
150 | 0 | } |
151 | | |
152 | | /// Reset the Crc in this CrcWriter. |
153 | 0 | pub fn reset(&mut self) { |
154 | 0 | self.crc.reset(); |
155 | 0 | } |
156 | | } |
157 | | |
158 | | impl<W: Write> CrcWriter<W> { |
159 | | /// Create a new CrcWriter. |
160 | 0 | pub fn new(w: W) -> CrcWriter<W> { |
161 | 0 | CrcWriter { |
162 | 0 | inner: w, |
163 | 0 | crc: Crc::new(), |
164 | 0 | } |
165 | 0 | } |
166 | | } |
167 | | |
168 | | impl<W: Write> Write for CrcWriter<W> { |
169 | 0 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
170 | 0 | let amt = self.inner.write(buf)?; |
171 | 0 | self.crc.update(&buf[..amt]); |
172 | 0 | Ok(amt) |
173 | 0 | } |
174 | | |
175 | 0 | fn flush(&mut self) -> io::Result<()> { |
176 | 0 | self.inner.flush() |
177 | 0 | } |
178 | | } |