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
|
use std::io;
use std::cmp;
use super::*;
/// A `BufferedReaderLimitor` limits the amount of data that can be
/// read from a `BufferedReader`.
pub struct BufferedReaderLimitor<T: BufferedReader<C>, C> {
reader: T,
limit: u64,
cookie: C,
}
impl<T: BufferedReader<C>, C> fmt::Debug for BufferedReaderLimitor<T, C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("BufferedReaderLimitor")
.field("limit", &self.limit)
.field("reader", &self.reader)
.finish()
}
}
impl<T: BufferedReader<()>> BufferedReaderLimitor<T, ()> {
/// Instantiate a new limitor. `reader` is the source to wrap.
/// `limit` is the maximum number of bytes that will be returned
/// from the source.
pub fn new(reader: T, limit: u64) -> Self {
Self::with_cookie(reader, limit, ())
}
}
impl<T: BufferedReader<C>, C> BufferedReaderLimitor<T, C> {
/// Like `new()`, but sets a cookie, which can be retrieved using
/// the `cookie_ref` and `cookie_mut` methods, and set using
/// the `cookie_set` method.
pub fn with_cookie(reader: T, limit: u64, cookie: C)
-> BufferedReaderLimitor<T, C> {
BufferedReaderLimitor {
reader: reader,
limit: limit,
cookie: cookie,
}
}
}
impl<T: BufferedReader<C>, C> io::Read for BufferedReaderLimitor<T, C> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
let len = cmp::min(self.limit, buf.len() as u64) as usize;
return self.reader.read(&mut buf[0..len]);
}
}
impl<T: BufferedReader<C>, C> BufferedReader<C> for BufferedReaderLimitor<T, C> {
fn buffer(&self) -> &[u8] {
let buf = self.reader.buffer();
&buf[..cmp::min(buf.len(),
cmp::min(std::usize::MAX as u64,
self.limit) as usize)]
}
/// Return the buffer. Ensure that it contains at least `amount`
/// bytes.
fn data(&mut self, amount: usize) -> Result<&[u8], io::Error> {
let amount = cmp::min(amount as u64, self.limit) as usize;
let result = self.reader.data(amount);
match result {
Ok(ref buffer) =>
if buffer.len() as u64 > self.limit {
return Ok(&buffer[0..self.limit as usize]);
} else {
return Ok(buffer);
},
Err(err) => return Err(err),
}
}
fn consume(&mut self, amount: usize) -> &[u8] {
assert!(amount as u64 <= self.limit);
self.limit -= amount as u64;
let data = self.reader.consume(amount);
return &data[..cmp::min(self.limit + amount as u64, data.len() as u64) as usize];
}
fn data_consume(&mut self, amount: usize) -> Result<&[u8], io::Error> {
let amount = cmp::min(amount as u64, self.limit) as usize;
let result = self.reader.data_consume(amount);
if let Ok(ref buffer) = result {
self.limit -= amount as u64;
return Ok(&buffer[
..cmp::min(buffer.len() as u64, self.limit + amount as u64) as usize]);
}
return result;
}
fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8], io::Error> {
if amount as u64 > self.limit {
return Err(Error::new(ErrorKind::UnexpectedEof, "EOF"));
}
let result = self.reader.data_consume_hard(amount);
if let Ok(ref buffer) = result {
self.limit -= amount as u64;
return Ok(&buffer[
..cmp::min(buffer.len() as u64, self.limit + amount as u64) as usize]);
}
return result;
}
fn get_mut(&mut self) -> Option<&mut BufferedReader<C>> {
Some(&mut self.reader)
}
fn get_ref(&self) -> Option<&BufferedReader<C>> {
Some(&self.reader)
}
fn into_inner<'b>(self: Box<Self>) -> Option<Box<BufferedReader<C> + 'b>>
where Self: 'b {
Some(Box::new(self.reader))
}
fn cookie_set(&mut self, cookie: C) -> C {
use std::mem;
mem::replace(&mut self.cookie, cookie)
}
fn cookie_ref(&self) -> &C {
&self.cookie
}
fn cookie_mut(&mut self) -> &mut C {
&mut self.cookie
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn buffered_reader_limitor_test() {
let data : &[u8] = b"01234567890123456789";
/* Add a single limitor. */
{
let mut bio : Box<BufferedReader<()>>
= Box::new(BufferedReaderMemory::new(data));
bio = {
let mut bio2 = Box::new(BufferedReaderLimitor::new(bio, 5));
{
let result =
|