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
|
use std::io;
use std::cmp;
use std::mem;
use std::fmt;
use buffered_reader::BufferedReader;
use buffered_reader::buffered_reader_generic_read_impl;
use crate::HashAlgorithm;
use crate::parse::{Cookie, HashesFor, Hashing};
const TRACE : bool = false;
pub(crate) struct HashedReader<R: BufferedReader<Cookie>> {
reader: R,
cookie: Cookie,
}
impl<R: BufferedReader<Cookie>> fmt::Display for HashedReader<R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "HashedReader")
}
}
impl<R: BufferedReader<Cookie>> fmt::Debug for HashedReader<R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("HashedReader")
.field("cookie", &self.cookie)
.field("reader", &self.reader)
.finish()
}
}
impl<R: BufferedReader<Cookie>> HashedReader<R> {
/// Instantiates a new hashed reader. `hashes_for` is the hash's
/// purpose. `algos` is a list of algorithms for which we should
/// compute the hash.
pub fn new(reader: R, hashes_for: HashesFor, algos: Vec<HashAlgorithm>)
-> Self {
let mut cookie = Cookie::default();
for &algo in &algos {
cookie.sig_group_mut().hashes
.push(algo.context().unwrap());
}
cookie.hashes_for = hashes_for;
HashedReader {
reader: reader,
cookie: cookie,
}
}
}
impl Cookie {
fn hash_update(&mut self, data: &[u8]) {
let level = self.level.unwrap_or(0);
let hashes_for = self.hashes_for;
let ngroups = self.sig_groups.len();
tracer!(TRACE, "Cookie::hash_update", level);
// Hash stashed data first.
if let Some(stashed_data) = self.hash_stash.take() {
// The stashed data was supposed to be hashed into the
// then-topmost signature-group's hash, but wasn't,
// because framing isn't hashed into the topmost signature
// group. By the time the parser encountered a new
// signature group, the data has already been consumed.
// We fix that here by hashing the stashed data into the
// former topmost signature-group's hash.
assert!(ngroups > 1);
for h in self.sig_groups[ngroups-2].hashes.iter_mut()
{
t!("({:?}): group {} {:?} hashing {} stashed bytes.",
hashes_for, ngroups-2, h.algo(), data.len());
h.update(&stashed_data);
}
}
if data.len() == 0 {
return;
}
t!("({} bytes, {} hashes, enabled: {:?})",
data.len(), self.sig_group().hashes.len(), self.hashing);
if self.hashing == Hashing::Disabled {
t!(" hash_update: NOT hashing {} bytes: {}.",
data.len(), crate::fmt::to_hex(data, true));
return;
}
let topmost_group = |i| i == ngroups - 1;
for (i, sig_group) in self.sig_groups.iter_mut().enumerate() {
if topmost_group(i) && self.hashing != Hashing::Enabled {
t!("topmost group {} NOT hashing {} bytes: {}.",
i, data.len(), crate::fmt::to_hex(data, true));
return;
}
for h in sig_group.hashes.iter_mut() {
t!("{:?}): group {} {:?} hashing {} bytes.",
hashes_for, i, h.algo(), data.len());
h.update(data);
}
}
}
}
impl<T: BufferedReader<Cookie>> io::Read for HashedReader<T> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
return buffered_reader_generic_read_impl(self, buf);
}
}
// Wrap a BufferedReader so that any data that is consumed is added to
// the hash.
impl<R: BufferedReader<Cookie>>
BufferedReader<Cookie> for HashedReader<R> {
fn buffer(&self) -> &[u8] {
self.reader.buffer()
}
fn data(&mut self, amount: usize) -> io::Result<&[u8]> {
self.reader.data(amount)
}
fn data_hard(&mut self, amount: usize) -> io::Result<&[u8]> {
self.reader.data_hard(amount)
}
fn consume(&mut self, amount: usize) -> &[u8] {
// We need to take the state rather than get a mutable
// reference to it, because self.reader.buffer() requires a
// reference as well.
let mut state = self.cookie_set(Cookie::default());
{
// The inner buffered reader must return at least `amount`
// bytes, because the caller can't `consume(amount)` if
// the internal buffer doesn't have at least that many
// bytes.
let data = self.reader.buffer();
assert!(data.len() >= amount);
state.hash_update(&data[..amount]);
}
self.cookie_set(state);
self.reader.consume(amount)
}
fn data_consume(&mut self, amount: usize) -> io::Result<&[u8]> {
// See consume() for an explanation of the following
// acrobatics.
let mut state = self.cookie_set(Cookie::default());
let got = {
let data = self.reader.data(amount)?;
let data = &data[..cmp::min(data.len(), amount)];
state.hash_update(data);
data.len()
};
self.cookie_set(state);
if let Ok(data) = self.reader.data_consume(amount) {
assert!(data.len() >= got);
Ok(data)
} else {
panic!("reader.data_consume() returned less than reader.data()!");
}
}
fn data_consume_hard(&mut self, amount: usize) -> io::Result<&[u8]> {
// See consume() for an explanation of the following
// acrobatics.
let mut state = self.cookie_set
|