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
pub use crate::codec_err::DecodeError;

/// Trait that allows reading of data into a slice.
pub trait NestedDecodeInput {
	/// The remaining length of the input data.
	fn remaining_len(&mut self) -> usize;

	fn empty(&mut self) -> bool {
		self.remaining_len() == 0
	}

	/// Read the exact number of bytes required to fill the given buffer.
	fn read_into(&mut self, into: &mut [u8]) -> Result<(), DecodeError>;

	/// Read the exact number of bytes required to fill the given buffer.
	/// Exit early if there are not enough bytes to fill the result.
	fn read_into_or_exit<ExitCtx: Clone>(
		&mut self,
		into: &mut [u8],
		c: ExitCtx,
		exit: fn(ExitCtx, DecodeError) -> !,
	);

	/// Read a single byte from the input.
	fn read_byte(&mut self) -> Result<u8, DecodeError> {
		let mut buf = [0u8];
		self.read_into(&mut buf[..])?;
		Ok(buf[0])
	}

	/// Read a single byte from the input.
	fn read_byte_or_exit<ExitCtx: Clone>(
		&mut self,
		c: ExitCtx,
		exit: fn(ExitCtx, DecodeError) -> !,
	) -> u8 {
		let mut buf = [0u8];
		self.read_into_or_exit(&mut buf[..], c, exit);
		buf[0]
	}

	/// Read the exact number of bytes required to fill the given buffer.
	fn read_slice(&mut self, length: usize) -> Result<&[u8], DecodeError>;

	/// Read the exact number of bytes required to fill the given buffer.
	/// Exit directly if the input contains too few bytes.
	fn read_slice_or_exit<ExitCtx: Clone>(
		&mut self,
		length: usize,
		c: ExitCtx,
		exit: fn(ExitCtx, DecodeError) -> !,
	) -> &[u8];

	/// Clears the input buffer and returns all remaining bytes.
	fn flush(&mut self) -> &[u8];
}

impl<'a> NestedDecodeInput for &'a [u8] {
	fn remaining_len(&mut self) -> usize {
		self.len()
	}

	fn read_into(&mut self, into: &mut [u8]) -> Result<(), DecodeError> {
		if into.len() > self.len() {
			return Err(DecodeError::INPUT_TOO_SHORT);
		}
		let len = into.len();
		into.copy_from_slice(&self[..len]);
		*self = &self[len..];
		Ok(())
	}

	fn read_into_or_exit<ExitCtx: Clone>(
		&mut self,
		into: &mut [u8],
		c: ExitCtx,
		exit: fn(ExitCtx, DecodeError) -> !,
	) {
		if into.len() > self.len() {
			exit(c, DecodeError::INPUT_TOO_SHORT);
		}
		let len = into.len();
		into.copy_from_slice(&self[..len]);
		*self = &self[len..];
	}

	fn read_slice(&mut self, length: usize) -> Result<&[u8], DecodeError> {
		if length > self.len() {
			return Err(DecodeError::INPUT_TOO_SHORT);
		}

		let (result, rest) = self.split_at(length);
		*self = rest;
		Ok(result)
	}

	fn read_slice_or_exit<ExitCtx: Clone>(
		&mut self,
		length: usize,
		c: ExitCtx,
		exit: fn(ExitCtx, DecodeError) -> !,
	) -> &[u8] {
		if length > self.len() {
			exit(c, DecodeError::INPUT_TOO_SHORT);
		}

		let (result, rest) = self.split_at(length);
		*self = rest;
		result
	}

	fn flush(&mut self) -> &[u8] {
		let result = &self[..];
		*self = &[];
		result
	}
}