-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.rs
More file actions
206 lines (174 loc) · 5.09 KB
/
Copy pathinput.rs
File metadata and controls
206 lines (174 loc) · 5.09 KB
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
use crate::*;
// primitives in this mod should provide the same no-panic guarantees as the crate `untrusted`
/// # Safety
/// types impl this should provide the same no-panic guarantees as the crate `untrusted`
pub unsafe trait Input: Sized + From<Self::Storage> + ByteStorage {
type Storage: AsRef<[u8]> + ByteStorage;
fn byte(&self, pos: usize) -> Option<&u8>;
fn bytes(&self, range: core::ops::Range<usize>) -> Option<Self>;
/* const */ fn len(&self) -> usize;
/* const */ fn is_empty(&self) -> bool {
self.len() == 0
}
fn leak(self) -> Self::Storage;
fn leak_as_array<const N: usize>(self) -> [u8; N];
}
pub struct SliceInput<'a> {
bytes: &'a [u8],
}
impl<'a> From<&'a [u8]> for SliceInput<'a> {
fn from(bytes: &'a [u8]) -> Self {
SliceInput { bytes }
}
}
unsafe impl<'a> Input for SliceInput<'a> {
type Storage = &'a [u8];
#[inline(always)]
fn byte(&self, pos: usize) -> Option<&u8> {
self.bytes.get(pos)
}
#[inline(always)]
fn bytes(&self, range: core::ops::Range<usize>) -> Option<Self> {
self.bytes.get(range).map(|bytes| SliceInput { bytes })
}
#[inline(always)]
/* const */ fn len(&self) -> usize {
self.bytes.len()
}
#[inline(always)]
/* const */ fn leak(self) -> &'a [u8] {
self.bytes
}
#[inline]
fn leak_as_array<const N: usize>(self) -> [u8; N] {
let r: &[u8; N] = self.bytes.try_into().unwrap(/* ? */);
*r
}
}
#[cfg(feature = "bytes")]
pub struct BytesInput {
bytes: Bytes,
}
#[cfg(feature = "bytes")]
impl From<Bytes> for BytesInput {
fn from(bytes: Bytes) -> Self {
BytesInput { bytes }
}
}
#[cfg(feature = "bytes")]
unsafe impl Input for BytesInput {
type Storage = Bytes;
#[inline(always)]
fn byte(&self, pos: usize) -> Option<&u8> {
self.bytes.get(pos)
}
#[inline(always)]
fn bytes(&self, range: core::ops::Range<usize>) -> Option<Self> {
// from <core::ops::Range<usize> as core::slice::SliceIndex<[T]>>::get
let core::ops::Range { start, end } = range;
if start > end || end > self.len() {
None
} else {
Some(BytesInput { bytes: self.bytes.slice(range) })
}
}
#[inline(always)]
/* const */ fn len(&self) -> usize {
self.bytes.len()
}
#[inline(always)]
fn leak(self) -> Bytes {
self.bytes
}
// copies
#[inline]
fn leak_as_array<const N: usize>(self) -> [u8; N] {
self.bytes.as_ref().try_into().unwrap(/* ? */)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ReadError {
TooShort { rest: usize, expected: usize },
TooLong { rest: usize },
// TODO temp solution
TooLongReadLen(usize),
}
type Result<T> = core::result::Result<T, ReadError>;
pub struct Reader<I> {
input: I,
pos: usize,
}
impl<B: AsRef<[u8]>, I: Input<Storage = B>> Reader<I> {
pub fn new(bytes: B) -> Self {
Reader {
input: bytes.into(),
pos: 0,
}
}
#[inline(always)]
pub /* const */ fn rest_len(&self) -> usize {
self.input.len() - self.pos
}
pub fn split_out(&mut self, len: usize) -> Result<I> {
let new_pos = self.pos.checked_add(len)
.ok_or(ReadError::TooLongReadLen(len))?;
let ret = self.input.bytes(self.pos..new_pos)
.ok_or(ReadError::TooShort { rest: self.rest_len(), expected: len })?;
self.pos = new_pos;
Ok(ret)
}
// copies
pub fn read_byte(&mut self) -> Result<u8> {
match self.input.byte(self.pos) {
Some(b) => {
// safe from overflow; see https://docs.rs/untrusted/0.9.0/src/untrusted/input.rs.html#39-43
self.pos += 1;
Ok(*b)
}
None => Err(ReadError::TooShort { rest: 0, expected: 1 }),
}
}
pub fn finish(self) -> core::result::Result<(), (ReadError, Self)> {
let rest = self.rest_len();
if rest == 0 {
Ok(())
} else {
Err((ReadError::TooLong { rest }, self))
}
}
pub fn into_rest(mut self) -> I {
self.split_out(self.rest_len()).unwrap()
}
pub fn into_parts(self) -> (I, usize) {
let Reader { input, pos } = self;
(input, pos)
}
}
// primitive derivatives
impl<B: AsRef<[u8]>, I: Input<Storage = B>> Reader<I> {
// copies
#[inline]
pub fn split_out_array<const N: usize>(&mut self) -> Result<[u8; N]> {
Ok(self.split_out(N)?.leak_as_array())
}
// copies
pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
let src = self.split_out(buf.len())?;
if buf.len() == 1 {
// checked below
buf[0] = *src.byte(0).unwrap();
} else {
buf.copy_from_slice(src.leak().as_ref());
}
Ok(())
}
#[inline]
pub fn bytes(&mut self, len: usize) -> Result<B> {
Ok(self.split_out(len)?.leak())
}
// copies
#[inline]
pub fn bytes_sized<const N: usize>(&mut self) -> Result<[u8; N]> {
self.split_out_array()
}
}