/rust/registry/src/index.crates.io-1949cf8c6b5b557f/flate2-1.0.35/src/crc.rs
Line | Count | Source |
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 | | #[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 | | #[derive(Debug)] |
21 | | pub struct CrcReader<R> { |
22 | | inner: R, |
23 | | crc: Crc, |
24 | | } |
25 | | |
26 | | impl Default for Crc { |
27 | 0 | fn default() -> Self { |
28 | 0 | Self::new() |
29 | 0 | } Unexecuted instantiation: <flate2::crc::Crc as core::default::Default>::default Unexecuted instantiation: <flate2::crc::Crc as core::default::Default>::default |
30 | | } |
31 | | |
32 | | impl Crc { |
33 | | /// Create a new CRC. |
34 | 40.1k | pub fn new() -> Crc { |
35 | 40.1k | Crc { |
36 | 40.1k | amt: 0, |
37 | 40.1k | hasher: Hasher::new(), |
38 | 40.1k | } |
39 | 40.1k | } Line | Count | Source | 34 | 27.2k | pub fn new() -> Crc { | 35 | 27.2k | Crc { | 36 | 27.2k | amt: 0, | 37 | 27.2k | hasher: Hasher::new(), | 38 | 27.2k | } | 39 | 27.2k | } |
Line | Count | Source | 34 | 12.8k | pub fn new() -> Crc { | 35 | 12.8k | Crc { | 36 | 12.8k | amt: 0, | 37 | 12.8k | hasher: Hasher::new(), | 38 | 12.8k | } | 39 | 12.8k | } |
|
40 | | |
41 | | /// Returns the current crc32 checksum. |
42 | 8.85k | pub fn sum(&self) -> u32 { |
43 | 8.85k | self.hasher.clone().finalize() |
44 | 8.85k | } Line | Count | Source | 42 | 6.42k | pub fn sum(&self) -> u32 { | 43 | 6.42k | self.hasher.clone().finalize() | 44 | 6.42k | } |
Line | Count | Source | 42 | 2.42k | pub fn sum(&self) -> u32 { | 43 | 2.42k | self.hasher.clone().finalize() | 44 | 2.42k | } |
|
45 | | |
46 | | /// The number of bytes that have been used to calculate the CRC. |
47 | | /// This value is only accurate if the amount is lower than 2<sup>32</sup>. |
48 | 4.01k | pub fn amount(&self) -> u32 { |
49 | 4.01k | self.amt |
50 | 4.01k | } <flate2::crc::Crc>::amount Line | Count | Source | 48 | 2.87k | pub fn amount(&self) -> u32 { | 49 | 2.87k | self.amt | 50 | 2.87k | } |
<flate2::crc::Crc>::amount Line | Count | Source | 48 | 1.13k | pub fn amount(&self) -> u32 { | 49 | 1.13k | self.amt | 50 | 1.13k | } |
|
51 | | |
52 | | /// Update the CRC with the bytes in `data`. |
53 | 220k | pub fn update(&mut self, data: &[u8]) { |
54 | 220k | self.amt = self.amt.wrapping_add(data.len() as u32); |
55 | 220k | self.hasher.update(data); |
56 | 220k | } <flate2::crc::Crc>::update Line | Count | Source | 53 | 103k | pub fn update(&mut self, data: &[u8]) { | 54 | 103k | self.amt = self.amt.wrapping_add(data.len() as u32); | 55 | 103k | self.hasher.update(data); | 56 | 103k | } |
<flate2::crc::Crc>::update Line | Count | Source | 53 | 117k | pub fn update(&mut self, data: &[u8]) { | 54 | 117k | self.amt = self.amt.wrapping_add(data.len() as u32); | 55 | 117k | self.hasher.update(data); | 56 | 117k | } |
|
57 | | |
58 | | /// Reset the CRC. |
59 | 0 | pub fn reset(&mut self) { |
60 | 0 | self.amt = 0; |
61 | 0 | self.hasher.reset(); |
62 | 0 | } Unexecuted instantiation: <flate2::crc::Crc>::reset Unexecuted instantiation: <flate2::crc::Crc>::reset |
63 | | |
64 | | /// Combine the CRC with the CRC for the subsequent block of bytes. |
65 | 0 | pub fn combine(&mut self, additional_crc: &Crc) { |
66 | 0 | self.amt = self.amt.wrapping_add(additional_crc.amt); |
67 | 0 | self.hasher.combine(&additional_crc.hasher); |
68 | 0 | } Unexecuted instantiation: <flate2::crc::Crc>::combine Unexecuted instantiation: <flate2::crc::Crc>::combine |
69 | | } |
70 | | |
71 | | impl<R: Read> CrcReader<R> { |
72 | | /// Create a new `CrcReader`. |
73 | 32.9k | pub fn new(r: R) -> CrcReader<R> { |
74 | 32.9k | CrcReader { |
75 | 32.9k | inner: r, |
76 | 32.9k | crc: Crc::new(), |
77 | 32.9k | } |
78 | 32.9k | } <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>>>::new Line | Count | Source | 73 | 21.5k | pub fn new(r: R) -> CrcReader<R> { | 74 | 21.5k | CrcReader { | 75 | 21.5k | inner: r, | 76 | 21.5k | crc: Crc::new(), | 77 | 21.5k | } | 78 | 21.5k | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_>>::new <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>>>::new Line | Count | Source | 73 | 11.4k | pub fn new(r: R) -> CrcReader<R> { | 74 | 11.4k | CrcReader { | 75 | 11.4k | inner: r, | 76 | 11.4k | crc: Crc::new(), | 77 | 11.4k | } | 78 | 11.4k | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_>>::new |
79 | | } |
80 | | |
81 | | impl<R> CrcReader<R> { |
82 | | /// Get the Crc for this `CrcReader`. |
83 | 10.5k | pub fn crc(&self) -> &Crc { |
84 | 10.5k | &self.crc |
85 | 10.5k | } <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>>>::crc Line | Count | Source | 83 | 7.51k | pub fn crc(&self) -> &Crc { | 84 | 7.51k | &self.crc | 85 | 7.51k | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_>>::crc <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>>>::crc Line | Count | Source | 83 | 3.05k | pub fn crc(&self) -> &Crc { | 84 | 3.05k | &self.crc | 85 | 3.05k | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_>>::crc |
86 | | |
87 | | /// Get the reader that is wrapped by this `CrcReader`. |
88 | 0 | pub fn into_inner(self) -> R { |
89 | 0 | self.inner |
90 | 0 | } Unexecuted instantiation: <flate2::crc::CrcReader<_>>::into_inner Unexecuted instantiation: <flate2::crc::CrcReader<_>>::into_inner |
91 | | |
92 | | /// Get the reader that is wrapped by this `CrcReader` by reference. |
93 | 0 | pub fn get_ref(&self) -> &R { |
94 | 0 | &self.inner |
95 | 0 | } Unexecuted instantiation: <flate2::crc::CrcReader<_>>::get_ref Unexecuted instantiation: <flate2::crc::CrcReader<_>>::get_ref |
96 | | |
97 | | /// Get a mutable reference to the reader that is wrapped by this `CrcReader`. |
98 | 406k | pub fn get_mut(&mut self) -> &mut R { |
99 | 406k | &mut self.inner |
100 | 406k | } <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>>>::get_mut Line | Count | Source | 98 | 228k | pub fn get_mut(&mut self) -> &mut R { | 99 | 228k | &mut self.inner | 100 | 228k | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_>>::get_mut <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>>>::get_mut Line | Count | Source | 98 | 177k | pub fn get_mut(&mut self) -> &mut R { | 99 | 177k | &mut self.inner | 100 | 177k | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_>>::get_mut |
101 | | |
102 | | /// Reset the Crc in this `CrcReader`. |
103 | 0 | pub fn reset(&mut self) { |
104 | 0 | self.crc.reset(); |
105 | 0 | } Unexecuted instantiation: <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>>>::reset Unexecuted instantiation: <flate2::crc::CrcReader<_>>::reset Unexecuted instantiation: <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>>>::reset Unexecuted instantiation: <flate2::crc::CrcReader<_>>::reset |
106 | | } |
107 | | |
108 | | impl<R: Read> Read for CrcReader<R> { |
109 | 238k | fn read(&mut self, into: &mut [u8]) -> io::Result<usize> { |
110 | 238k | let amt = self.inner.read(into)?; |
111 | 193k | self.crc.update(&into[..amt]); |
112 | 193k | Ok(amt) |
113 | 238k | } <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>> as std::io::Read>::read Line | Count | Source | 109 | 97.3k | fn read(&mut self, into: &mut [u8]) -> io::Result<usize> { | 110 | 97.3k | let amt = self.inner.read(into)?; | 111 | 84.1k | self.crc.update(&into[..amt]); | 112 | 84.1k | Ok(amt) | 113 | 97.3k | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_> as std::io::Read>::read <flate2::crc::CrcReader<flate2::deflate::bufread::DeflateDecoder<flate2::bufreader::BufReader<suricata::http2::decompression::HTTP2cursor>>> as std::io::Read>::read Line | Count | Source | 109 | 141k | fn read(&mut self, into: &mut [u8]) -> io::Result<usize> { | 110 | 141k | let amt = self.inner.read(into)?; | 111 | 109k | self.crc.update(&into[..amt]); | 112 | 109k | Ok(amt) | 113 | 141k | } |
Unexecuted instantiation: <flate2::crc::CrcReader<_> as std::io::Read>::read |
114 | | } |
115 | | |
116 | | impl<R: BufRead> BufRead for CrcReader<R> { |
117 | 0 | fn fill_buf(&mut self) -> io::Result<&[u8]> { |
118 | 0 | self.inner.fill_buf() |
119 | 0 | } Unexecuted instantiation: <flate2::crc::CrcReader<_> as std::io::BufRead>::fill_buf Unexecuted instantiation: <flate2::crc::CrcReader<_> as std::io::BufRead>::fill_buf |
120 | 0 | fn consume(&mut self, amt: usize) { |
121 | 0 | if let Ok(data) = self.inner.fill_buf() { |
122 | 0 | self.crc.update(&data[..amt]); |
123 | 0 | } |
124 | 0 | self.inner.consume(amt); |
125 | 0 | } Unexecuted instantiation: <flate2::crc::CrcReader<_> as std::io::BufRead>::consume Unexecuted instantiation: <flate2::crc::CrcReader<_> as std::io::BufRead>::consume |
126 | | } |
127 | | |
128 | | /// A wrapper around a [`Write`] that calculates the CRC. |
129 | | /// |
130 | | /// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html |
131 | | #[derive(Debug)] |
132 | | pub struct CrcWriter<W> { |
133 | | inner: W, |
134 | | crc: Crc, |
135 | | } |
136 | | |
137 | | impl<W> CrcWriter<W> { |
138 | | /// Get the Crc for this `CrcWriter`. |
139 | 0 | pub fn crc(&self) -> &Crc { |
140 | 0 | &self.crc |
141 | 0 | } Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::crc Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::crc |
142 | | |
143 | | /// Get the writer that is wrapped by this `CrcWriter`. |
144 | 0 | pub fn into_inner(self) -> W { |
145 | 0 | self.inner |
146 | 0 | } Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::into_inner Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::into_inner |
147 | | |
148 | | /// Get the writer that is wrapped by this `CrcWriter` by reference. |
149 | 0 | pub fn get_ref(&self) -> &W { |
150 | 0 | &self.inner |
151 | 0 | } Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::get_ref Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::get_ref |
152 | | |
153 | | /// Get a mutable reference to the writer that is wrapped by this `CrcWriter`. |
154 | 0 | pub fn get_mut(&mut self) -> &mut W { |
155 | 0 | &mut self.inner |
156 | 0 | } Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::get_mut Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::get_mut |
157 | | |
158 | | /// Reset the Crc in this `CrcWriter`. |
159 | 0 | pub fn reset(&mut self) { |
160 | 0 | self.crc.reset(); |
161 | 0 | } Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::reset Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::reset |
162 | | } |
163 | | |
164 | | impl<W: Write> CrcWriter<W> { |
165 | | /// Create a new `CrcWriter`. |
166 | 0 | pub fn new(w: W) -> CrcWriter<W> { |
167 | 0 | CrcWriter { |
168 | 0 | inner: w, |
169 | 0 | crc: Crc::new(), |
170 | 0 | } |
171 | 0 | } Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::new Unexecuted instantiation: <flate2::crc::CrcWriter<_>>::new |
172 | | } |
173 | | |
174 | | impl<W: Write> Write for CrcWriter<W> { |
175 | 0 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
176 | 0 | let amt = self.inner.write(buf)?; |
177 | 0 | self.crc.update(&buf[..amt]); |
178 | 0 | Ok(amt) |
179 | 0 | } Unexecuted instantiation: <flate2::crc::CrcWriter<_> as std::io::Write>::write Unexecuted instantiation: <flate2::crc::CrcWriter<_> as std::io::Write>::write |
180 | | |
181 | 0 | fn flush(&mut self) -> io::Result<()> { |
182 | 0 | self.inner.flush() |
183 | 0 | } Unexecuted instantiation: <flate2::crc::CrcWriter<_> as std::io::Write>::flush Unexecuted instantiation: <flate2::crc::CrcWriter<_> as std::io::Write>::flush |
184 | | } |