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
/*!

Provides byte stream parsing utilities.

```rust,ignore

let reader = File::open("..."); // reader is anything that implements io::Read
let alloc_size = 1000;  // Set custom allocation size
let muncher = ReadMuncher::<DataItem, _>::new(&reader, alloc_size, |bytes, is_eof| {
    // parse function here
});

for packet in &muncher {
     // packet is DataItem
}

```
*/

use anyhow::Error;
use std::io::Read;

/// Output type for `ReadMuncher` parse function
/// 
/// `T` is the iterator output type (e.g. `Packet` or `Frame` structs).
/// 
/// `usize` is the number of bytes consumed to created the output type.
pub type MunchOutput<T> = Option<(T, usize)>;

/** Continuously reads bytes from a `Read` implementor,
 parses that byte stream with the provided parser function,
 and provides an iterator over the parsed slices/packets/frames.

```rust
# use noodle::ReadMuncher;
# fn main() {
use std::io::Cursor;

let mut read = Cursor::new(vec![0xff, 1, 0xff, 2, 2, 0xff, 3, 3, 3]);

let munched: Vec<Vec<u8>> = ReadMuncher::new(&mut read, 5, |b, _| {
    if b.len() >= 2 {
        let skip = b[1] as usize + 1;
        if b.len() > skip {
            let blob = &b[..skip];
            Ok(Some((blob.to_owned(), skip)))
        } else {
            Ok(None)
        }
    } else {
        Ok(None)
    }
})
.collect();

assert_eq!(munched.len(), 3);
assert_eq!(munched[0], vec![0xff, 1]);
assert_eq!(munched[1], vec![0xff, 2, 2]);
assert_eq!(munched[2], vec![0xff, 3, 3, 3]);

 # }
 ```
*/
pub struct ReadMuncher<'a, T, F>
where
    F: Fn(&[u8], bool) -> Result<MunchOutput<T>, Error>,
{
    reader: &'a mut dyn Read,
    buffer: Vec<u8>,
    parse_location: usize,
    alloc_size: usize,
    // parse_fn: &'b ParseFn<T>,
    parse_fn: F,
    complete: bool,
    read_end_location: usize,
}

impl<'a, T, F> ReadMuncher<'a, T, F>
where
    F: Fn(&[u8], bool) -> Result<MunchOutput<T>, Error>,
{
    /// Starts a new `ReadMuncher` instance.
    ///
    /// ### reader
    ///
    /// The `Read` implementor to be read for bytes.
    ///
    /// ### alloc_size
    ///
    /// Internal buffer allocation increment size.
    ///
    /// This sets the initial buffer size, and size increase increments when necessary.
    ///
    /// ### parse_fn
    ///
    /// The parse function called against the read buffer. This can be a static function or a closure, with signature `Fn(&[u8], bool) -> Result<MunchOutput<T>, Error>`.
    ///
    /// The first parameter is a reference to the unconsumed slice of the read buffer.
    /// The second parameter is a boolean, which signals EOF (no more bytes available to read).
    ///
    /// The return type is used internally to consume/step forward. Below is some example return values and what they do:
    ///
    /// ```ignore
    ///
    /// Ok(Some((item, 12)))  // 'item' is returned, buffer drains 12 bytes
    ///
    /// Ok(None)  // Not enough bytes, keep trying
    ///
    /// Err(...)  // Error occurred. Iterator panics and prints to stderr
    ///
    /// ```
    pub fn new(reader: &'a mut dyn Read, alloc_size: usize, parse_fn: F) -> Self {
        Self {
            reader,
            alloc_size,
            buffer: Vec::with_capacity(alloc_size),
            parse_location: 0,
            parse_fn,
            complete: false,
            read_end_location: 0,
        }
    }

    /// Removes used bytes from buffer, and shifts everything to start.
    /// Returns remaining buffer bytes when at EOF.
    fn resize_no_alloc(&mut self) -> Result<Option<Vec<u8>>, Error> {
        let full_len = self.buffer.len();
        self.buffer.drain(0..self.parse_location);
        self.buffer.resize_with(full_len, || 0);
        self.read_end_location -= self.parse_location;
        let num_new_bytes = self
            .reader
            .read(&mut self.buffer[(full_len - self.parse_location)..])?;
        self.read_end_location += num_new_bytes;
        if num_new_bytes == 0 {
            let blob = &self.buffer[..self.read_end_location];
            self.complete = true;
            Ok(Some(blob.to_owned()))
        } else {
            self.parse_location = 0;
            Ok(None)
        }
    }

    /// Extends the buffer to allow storing more bytes.
    /// Returns remaining buffer bytes when at EOF.
    fn resize_alloc(&mut self) -> Result<Option<Vec<u8>>, Error> {
        let old_len = self.buffer.len();
        self.buffer.resize_with(old_len + self.alloc_size, || 0);
        let num_new_bytes = self.reader.read(&mut self.buffer[old_len..])?;
        self.read_end_location += num_new_bytes;
        if num_new_bytes == 0 {
            let blob = &self.buffer[..self.read_end_location];
            self.complete = true;
            Ok(Some(blob.to_owned()))
        } else {
            Ok(None)
        }
    }
}

impl<'a, T, F> Iterator for ReadMuncher<'a, T, F>
where
    F: Fn(&[u8], bool) -> Result<MunchOutput<T>, Error>,
{
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.complete == true {
            return None;
        }
        loop {
            // let buf: &'b [u8] = &self.buffer[self.parse_location..];
            let parse_result = (self.parse_fn)(&self.buffer[self.parse_location..], false);
            match parse_result {
                // Complete parse
                Ok(Some((item, n))) => {
                    self.parse_location += n;
                    return Some(item);
                }
                // Handle incomplete parse
                Ok(None) => {
                    // Partial buffer not enough, shift existing and fill
                    if self.parse_location != 0 {
                        match self.resize_no_alloc() {

                            Ok(r) => {
                                // EOF
                                if let Some(last) = r {
                                    match (self.parse_fn)(&last, true) {
                                        Ok(Some((item, _))) => return Some(item),
                                        _ => return None,
                                    }
                                }
                            }
                            Err(e) => eprintln!("Error while resizing: {:?}", e),
                        }
                    // Entire buffer not enough, increase buffer size and refill
                    } else {
                        match self.resize_alloc() {
                            Ok(r) => {
                                // EOF
                                if let Some(last) = r {
                                    match (self.parse_fn)(&last, true) {
                                        Ok(Some((item, _))) => return Some(item),
                                        _ => return None,
                                    }
                                }
                            }
                            Err(e) => eprintln!("Error while resizing: {:?}", e),
                        }
                    }
                }
                Err(e) => {
                    eprintln!("Error while parsing: {:?}", e);
                    return None;
                }
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    /// Tests passing static function as parser.
    #[test]
    pub fn static_fn() {
        use std::io::Cursor;
        use std::result::Result;

        fn my_parse_function(bytes: &[u8], _: bool) -> Result<MunchOutput<Vec<u8>>, Error> {
            if bytes.len() >= 2 {
                let skip = bytes[1] as usize + 1;
                if bytes.len() > skip {
                    let blob = &bytes[..skip];
                    Ok(Some((blob.to_owned(), skip)))
                } else {
                    Ok(None)
                }
            } else {
                Ok(None)
            }
        }

        let mut read = Cursor::new(vec![0xff, 1, 0xff, 2, 2, 0xff, 3, 3, 3]);

        let munched: Vec<Vec<u8>> = ReadMuncher::new(&mut read, 5, my_parse_function).collect();

        assert_eq!(munched.len(), 3);
        assert_eq!(munched[0], vec![0xff, 1]);
        assert_eq!(munched[1], vec![0xff, 2, 2]);
        assert_eq!(munched[2], vec![0xff, 3, 3, 3]);
    }

    /// Tests passing closure as parser function.
    #[test]
    pub fn closure() {
        use std::io::Cursor;

        let mut read = Cursor::new(vec![0xff, 1, 0xff, 2, 2, 0xff, 3, 3, 3]);

        let munched: Vec<Vec<u8>> = ReadMuncher::new(&mut read, 5, |b, _| {
            if b.len() >= 2 {
                let skip = b[1] as usize + 1;
                if b.len() > skip {
                    let blob = &b[..skip];
                    Ok(Some((blob.to_owned(), skip)))
                } else {
                    Ok(None)
                }
            } else {
                Ok(None)
            }
        })
        .collect();

        assert_eq!(munched.len(), 3);
        assert_eq!(munched[0], vec![0xff, 1]);
        assert_eq!(munched[1], vec![0xff, 2, 2]);
        assert_eq!(munched[2], vec![0xff, 3, 3, 3]);
    }
}