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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

use std::{io, error, result, str};
use std::io::Seek;
use std::io::SeekFrom;
use std::convert::From;
use std::fmt;

/// Custom error type specific to the [`SliceReader`] struct.
#[derive(Debug)]
pub enum Error {
    /// Not enough bytes left in slice to read requested value.
    NotEnoughLeft(usize),
    /// I/O error happened while reading the slice.
    IoError(io::Error),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::NotEnoughLeft(n) => write!(f, "NotEoughLeft({})", n),
            Error::IoError(ref e) => write!(f, "IoError({})", e),
        }
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::IoError(err)
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::NotEnoughLeft(_) => "not enough bytes left in slice",
            Error::IoError(_) => "I/O error",
        }
    }

    fn cause(&self) -> Option<&dyn error::Error> {
        match *self {
            Error::IoError(ref err) => Some(err as &dyn error::Error),
            _ => None,
        }
    }
}


/// A specialized [`Result`](std::result::Result) type for slice reading operations.
/// 
/// This typedef is used to shorten `std::result::Result<T, slread::Error>` to `slread::Result<T>`.
pub type Result<T> = result::Result<T, Error>;


/// Tool to decode protocols from a byte slice.
///
/// The slice can be consumed as different types of integers (`u16`, `u32`, ...) for binary
/// protocols or read line by line for text protocols.
///
/// All numbers read from the slice are assumed to be stored in big endian (network order).
pub struct SliceReader<'a> {
    slice: &'a [u8],
    pos: usize,
    size: usize,
}

impl<'a> Seek for SliceReader<'a> {
    #[inline]
    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
        match pos {
            SeekFrom::Start(n) => self.pos = n as usize,
            SeekFrom::End(n) => {
                let pos = self.size as i64 + n;
                if pos < 0 {
                    return Err(io::Error::new(io::ErrorKind::InvalidInput, "Seeking before byte 0."));
                }
                self.pos = pos as usize;
            },
            SeekFrom::Current(n) => {
                let pos = self.pos as i64 + n;
                if pos < 0 {
                    return Err(io::Error::new(io::ErrorKind::InvalidInput, "Seeking before byte 0."));
                }
                self.pos = pos as usize;
            }
        }
        Ok(self.pos as u64)
    }
}

impl<'a> SliceReader<'a> {
    /// Creates a new `SliceReader` from a slice of `u8`.
    #[inline]
    pub fn new(slice: &[u8]) -> SliceReader {
        SliceReader {
            slice: slice,
            pos: 0,
            size: slice.len(),
        }
    }

    /// Reads and consumes a `u8` from the slice.
    #[inline]
    pub fn read_u8(&mut self) -> Result<u8> {
        if self.left() < 1 {
            Err(Error::NotEnoughLeft(0))
        } else {
            let result = self.slice[self.pos];
            self.pos += 1;
            Ok(result)
        }
    }

    /// Reads and consumes a `u16` from the slice.
    #[inline]
    pub fn read_u16(&mut self) -> Result<u16> {
        let left = self.left();
        if left < 2 {
            self.pos += 2;
            Err(Error::NotEnoughLeft(left))
        } else {
            let result = (self.slice[self.pos] as u16) << 8 |
                self.slice[self.pos + 1] as u16;
            self.pos += 2;
            Ok(result)
        }
    }

    /// Reads and consumes a `u24` from the slice.
    ///
    /// `u24` is actually a `u32` but only consumes 3 bytes from the slice.
    #[inline]
    pub fn read_u24(&mut self) -> Result<u32> {
        let left = self.left();
        if left < 3 {
            self.pos += 3;
            Err(Error::NotEnoughLeft(left))
        } else {
            let result = (self.slice[self.pos] as u32) << 16 |
                (self.slice[self.pos + 1] as u32) << 8 |
                self.slice[self.pos + 2] as u32;
            self.pos += 3;
            Ok(result)
        }
    }

    /// Reads and consumes a `u32` from the slice.
    ///
    /// # Example
    ///
    /// ```
    /// use t2plugin::slread::SliceReader;
    /// use t2plugin::packet::Packet;
    /// 
    /// ...
    /// let slr = SliceReader::new(packet.l7_header());
    /// // read the first 32 bits of the packet payload
    /// let id = try!(slr.read_u32());
    /// ```
    #[inline]
    pub fn read_u32(&mut self) -> Result<u32> {
        let left = self.left();
        if left < 4 {
            self.pos += 4;
            Err(Error::NotEnoughLeft(left))
        } else {
            let result = (self.slice[self.pos] as u32) << 24 |
                (self.slice[self.pos + 1] as u32) << 16 |
                (self.slice[self.pos + 2] as u32) << 8 |
                self.slice[self.pos + 3] as u32;
            self.pos += 4;
            Ok(result)
        }
    }

    /// Reads and consumes a `u48` from the slice.
    ///
    /// `u48` is actually a `u64` but only consumes 6 bytes from the slice.
    #[inline]
    pub fn read_u48(&mut self) -> Result<u64> {
        let left = self.left();
        if left < 6 {
            self.pos += 6;
            Err(Error::NotEnoughLeft(left))
        } else {
            let result = (self.slice[self.pos] as u64) << 40 |
                (self.slice[self.pos + 1] as u64) << 32 |
                (self.slice[self.pos + 2] as u64) << 24 |
                (self.slice[self.pos + 3] as u64) << 16 |
                (self.slice[self.pos + 4] as u64) << 8 |
                self.slice[self.pos + 5] as u64;
            self.pos += 6;
            Ok(result)
        }
    }

    /// Reads and consumes a `u64` from the slice.
    #[inline]
    pub fn read_u64(&mut self) -> Result<u64> {
        let left = self.left();
        if left < 8 {
            self.pos += 8;
            Err(Error::NotEnoughLeft(left))
        } else {
            let result = (self.slice[self.pos] as u64) << 56 |
                (self.slice[self.pos + 1] as u64) << 48 |
                (self.slice[self.pos + 2] as u64) << 40 |
                (self.slice[self.pos + 3] as u64) << 32 |
                (self.slice[self.pos + 4] as u64) << 24 |
                (self.slice[self.pos + 5] as u64) << 16 |
                (self.slice[self.pos + 6] as u64) << 8 |
                self.slice[self.pos + 7] as u64;
            self.pos += 8;
            Ok(result)
        }
    }

    /// Copies and consumes a sub-slice to a mutable buffer.
    ///
    /// # Example
    ///
    /// ```
    /// use t2plugin::slread::SliceReader;
    /// use t2plugin::packet::Packet;
    /// 
    /// ...
    /// let slr = SliceReader::new(packet.l7_header());
    /// let mut data = [0u8; 6];
    /// try!(slr.read_copy(&mut data));
    /// ```
    #[inline]
    pub fn read_copy(&mut self, buf: &mut [u8]) -> Result<()> {
        let len = buf.len();
        let left = self.left();
        if left < len {
            self.pos += len;
            Err(Error::NotEnoughLeft(left))
        } else {
            buf.copy_from_slice(&self.slice[self.pos .. self.pos + len]);
            self.pos += len;
            Ok(())
        }
    }

    /// Returns and consumes a sub-slice containing `count` bytes.
    ///
    /// The lifetime of the returned slice is the same as the one of the slice provided when
    /// creating this `SliceReader` with the [`new`](#method.new) method.
    #[inline]
    pub fn read_bytes(&mut self, count: usize) -> Result<&'a [u8]> {
        let left = self.left();
        if left < count {
            self.pos += count;
            Err(Error::NotEnoughLeft(left))
        } else {
            let slice = &self.slice[self.pos .. self.pos + count];
            self.pos += count;
            Ok(slice)
        }
    }

    /// Reads buffer until the first occurence of `byte`.
    ///
    /// The lifetime of the returned slice is the same as the one of the slice provided when
    /// creating this `SliceReader` with the [`new`](#method.new) method.
    #[inline]
    pub fn read_until(&mut self, byte: u8) -> Result<&'a [u8]> {
        if self.left() < 1 {
            return Err(Error::NotEnoughLeft(0));
        }
        // find next 'byte' position
        let start = self.pos;
        let end = if let Some(index) = self.slice[start ..].iter().position(|&e| e == byte) {
            start + index + 1
        } else {
            self.size
        };
        self.pos = end;
        Ok(&self.slice[start .. end])
    }

    /// Reads a line from the buffer.
    ///
    /// The line is returned as a slice of byte. This is necessary in order to also process lines
    /// which contain invalid UTF-8 characters.
    ///
    /// The lifetime of the returned line is the same as the one of the slice provided when
    /// creating this `SliceReader` with the [`new`](#method.new) method.
    ///
    /// # Example
    ///
    /// ```
    /// use t2plugin::slread::{SliceReader, TrimBytes};
    /// use t2plugin::packet::Packet;
    ///
    /// let slr = SliceReader::new(packet.l7_header());
    /// // read the packet payload line by line
    /// while let Ok(line) = slr.read_line() {
    ///     if line.starts_with(b"User-Agent: ") {
    ///         let ua = line[12 ..].trim();
    ///         // do something with HTTP user agent
    ///     }
    /// }
    /// ```
    #[inline]
    pub fn read_line(&mut self) -> Result<&'a [u8]> {
        self.read_until(b'\n')
    }

    /// Skips `count` bytes of the slice.
    #[inline]
    pub fn skip(&mut self, count: usize) {
        self.pos += count;
    }

    /// Seeks back `count` bytes in the slice.
    #[inline]
    pub fn rewind(&mut self, count: usize) -> Result<()> {
        if self.pos < count {
            Err(Error::IoError(io::Error::new(io::ErrorKind::InvalidInput, "Seeking before byte 0.")))
        } else {
            self.pos -= count;
            Ok(())
        }
    }

    /// Return the number of bytes left in the slice.
    #[inline]
    pub fn left(&self) -> usize {
        if self.pos >= self.size { 0 } else { self.size - self.pos }
    }

    /// Returns the current position in the buffer.
    #[inline]
    pub fn pos(&self) -> usize {
        self.pos
    }

    /// Shortens the slice by `count` bytes.
    ///
    /// On success, returns the slice new length.
    #[inline]
    pub fn cut_tail(&mut self, count: usize) -> Result<usize> {
        if self.size < count {
            Err(Error::NotEnoughLeft(self.size))
        } else {
            self.size -= count;
            Ok(self.size)
        }
    }
}


/// Trait to trim a byte slice `&[u8]` similarly to `str::trim()`.
pub trait TrimBytes {
    /// Returns a byte slice with leading and trailing whitespace removed.
    ///
    /// # Example
    ///
    /// ```
    /// use t2plugin::slread::{SliceReader, TrimBytes};
    /// use t2plugin::packet::Packet;
    ///
    /// let slr = SliceReader::new(packet.l7_header());
    /// // read the first line of the packet payload and trim it
    /// let line = try!(slr.read_line()).trim();
    /// ```
    fn trim(&self) -> &Self;
}

// helper functions for trim
fn is_not_whitespace(b: &u8) -> bool {
    fn is_whitespace(b: &u8) -> bool {
        *b == b' ' || *b == b'\t' || *b == b'\n' || *b == b'\r' || *b == 0x0b || *b == 0x0c
    }
    !is_whitespace(b)
}

impl TrimBytes for [u8] {
    #[inline]
    fn trim(&self) -> &Self {
        if let Some(start) = self.iter().position(is_not_whitespace) {
            if let Some(end) = self.iter().rposition(is_not_whitespace) {
                return &self[start .. end + 1]
            }
        }
        &[]
    }
}