-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.rs
More file actions
145 lines (115 loc) · 3.62 KB
/
reader.rs
File metadata and controls
145 lines (115 loc) · 3.62 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
use super::{*, leb128_num_traits::*};
struct Reader<I> {
inner: byte_storage::Reader<I>,
}
impl<B: AsRef<[u8]> + ByteStorage, I: Input<Storage = B>> Reader<I> {
// begin wrapper impls
#[inline(always)]
pub fn new(bytes: B) -> Self {
Self { inner: byte_storage::Reader::new(bytes) }
}
#[inline(always)]
pub fn byte(&mut self) -> Result<u8> {
Ok(self.inner.read_byte()?)
}
#[inline(always)]
pub fn finish(self) -> core::result::Result<(), (ReadError, byte_storage::Reader<I>)> {
self.inner.finish()
}
#[inline(always)]
pub fn into_rest(self) -> I {
self.inner.into_rest()
}
#[inline(always)]
pub fn into_parts(self) -> (I, usize) {
self.inner.into_parts()
}
#[inline(always)]
pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
Ok(self.inner.read_exact(buf)?)
}
#[inline(always)]
pub fn bytes(&mut self, sz: usize) -> Result<B> {
Ok(self.inner.bytes(sz)?)
}
#[inline(always)]
pub fn bytes_sized<const N: usize>(&mut self) -> Result<[u8; N]> {
Ok(self.inner.bytes_sized()?)
}
// end wrapper impls
fn finish_with<T>(self, res: Result<T>) -> FullResult<T, B> {
match res {
Ok(val) => {
match self.finish() {
Ok(()) => Ok(val),
Err((err, reader)) => {
let (input, pos) = reader.into_parts();
let buf = input.leak();
Err(FullError { err: err.into(), buf, pos })
}
}
},
Err(err) => {
let (input, pos) = self.into_parts();
let buf = input.leak();
Err(FullError { err, buf, pos })
}
}
}
// https://github.com/BillGoldenWater/playground/blob/2ad09e4/rust/leb128/src/lib.rs
// TODO: byte-storage extension?
fn uleb128<N: NumUnsigned>(&mut self) -> Result<N> {
let mut res = N::from_u8(0);
let mut shift = 0;
let mut byte = self.byte()?;
loop {
res.shifted_or_assign(byte & 0x7F, shift);
shift += 7;
if byte & 0x80 == 0 {
break;
}
if shift >= 128 {
return Err(Error::LEB128TooLong);
}
byte = self.byte()?;
}
Ok(res)
}
fn sleb128<N: NumSigned>(&mut self) -> Result<N> {
let mut res = N::UnsignedVariant::from_u8(0);
let mut shift = 0;
let mut byte = self.byte()?;
loop {
res.shifted_or_assign(byte & 0x7F, shift);
shift += 7;
if byte & 0x80 == 0 {
break;
}
if shift >= 128 {
return Err(Error::LEB128TooLong);
}
byte = self.byte()?;
}
let mut res = N::from_unsigned(res);
if shift < N::UnsignedVariant::BITS && byte & 0x40 != 0 {
res.one_fill_left(shift);
}
Ok(res)
}
fn val(&mut self) -> Result<Value<B>> {
unimplemented!()
}
}
impl<B: AsRef<[u8]> + ByteStorage> Value<B> {
pub fn decode<I: Input<Storage = B>>(buf: B) -> FullResult<Value<B>, B> {
let mut reader = Reader::<I>::new(buf);
let val = reader.val();
reader.finish_with(val)
}
// cannot return FullResult
pub fn decode_first_value<I: Input<Storage = B>>(buf: B) -> (Result<Value<B>>, B) {
let mut reader = Reader::<I>::new(buf);
let res = reader.val();
(res, reader.into_rest().leak())
}
}