-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin_file.rs
More file actions
171 lines (149 loc) · 4.32 KB
/
Copy pathbin_file.rs
File metadata and controls
171 lines (149 loc) · 4.32 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
use crate::error::{PatchError, Result};
pub struct BinReader<'a> {
buf: &'a [u8],
pos: usize,
}
impl<'a> BinReader<'a> {
#[must_use]
pub fn new(buf: &'a [u8]) -> Self {
Self { buf, pos: 0 }
}
#[must_use]
pub fn pos(&self) -> usize {
self.pos
}
#[must_use]
pub fn len(&self) -> usize {
self.buf.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
#[must_use]
pub fn remaining(&self) -> usize {
self.buf.len() - self.pos
}
pub fn seek(&mut self, pos: usize) -> Result<()> {
if pos > self.buf.len() {
return Err(PatchError::Truncated);
}
self.pos = pos;
Ok(())
}
pub fn peek(&self, n: usize) -> Result<&'a [u8]> {
let end = self.pos.checked_add(n).ok_or(PatchError::Truncated)?;
if end > self.buf.len() {
return Err(PatchError::Truncated);
}
Ok(&self.buf[self.pos..end])
}
pub fn read_u8(&mut self) -> Result<u8> {
let b = *self.buf.get(self.pos).ok_or(PatchError::Truncated)?;
self.pos += 1;
Ok(b)
}
pub fn read_u16_be(&mut self) -> Result<u16> {
let b = self.read_bytes(2)?;
Ok(u16::from_be_bytes([b[0], b[1]]))
}
pub fn read_u24_be(&mut self) -> Result<u32> {
let b = self.read_bytes(3)?;
Ok(u32::from_be_bytes([0, b[0], b[1], b[2]]))
}
pub fn read_u32_le(&mut self) -> Result<u32> {
let b = self.read_bytes(4)?;
Ok(u32::from_le_bytes([b[0], b[1], b[2], b[3]]))
}
pub fn read_u32_be(&mut self) -> Result<u32> {
let b = self.read_bytes(4)?;
Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]]))
}
pub fn read_u16_le(&mut self) -> Result<u16> {
let b = self.read_bytes(2)?;
Ok(u16::from_le_bytes([b[0], b[1]]))
}
pub fn read_u64_le(&mut self) -> Result<u64> {
let b = self.read_bytes(8)?;
Ok(u64::from_le_bytes([
b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
]))
}
pub fn read_bytes(&mut self, n: usize) -> Result<&'a [u8]> {
let end = self.pos.checked_add(n).ok_or(PatchError::Truncated)?;
if end > self.buf.len() {
return Err(PatchError::Truncated);
}
let slice = &self.buf[self.pos..end];
self.pos = end;
Ok(slice)
}
/// byuu's variable-length value encoding (UPS/BPS/RUP).
pub fn read_vlv(&mut self) -> Result<u64> {
let mut value: u64 = 0;
let mut shift: u64 = 1;
loop {
let byte = self.read_u8()?;
let chunk = u64::from(byte & 0x7f)
.checked_mul(shift)
.ok_or(PatchError::InvalidEncoding)?;
value = value
.checked_add(chunk)
.ok_or(PatchError::InvalidEncoding)?;
if byte & 0x80 != 0 {
break;
}
shift = shift.checked_shl(7).ok_or(PatchError::InvalidEncoding)?;
value = value
.checked_add(shift)
.ok_or(PatchError::InvalidEncoding)?;
}
Ok(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn write_vlv(out: &mut Vec<u8>, mut value: u64) {
loop {
let byte = (value & 0x7f) as u8;
value >>= 7;
if value == 0 {
out.push(byte | 0x80);
return;
}
out.push(byte);
value -= 1;
}
}
#[test]
fn vlv_roundtrips_known_values() {
let cases: &[u64] = &[
0,
1,
0x7f,
0x80,
0x100,
0x1234,
0xdead_beef,
u64::from(u32::MAX),
];
for &v in cases {
let mut buf = Vec::new();
write_vlv(&mut buf, v);
let mut r = BinReader::new(&buf);
assert_eq!(r.read_vlv().unwrap(), v, "value {v} did not roundtrip");
}
}
#[test]
fn read_past_end_returns_truncated() {
let mut r = BinReader::new(&[1, 2]);
assert_eq!(r.read_u32_le(), Err(PatchError::Truncated));
}
#[test]
fn peek_does_not_advance() {
let r = BinReader::new(&[1, 2, 3, 4]);
assert_eq!(r.peek(2).unwrap(), &[1, 2]);
assert_eq!(r.pos(), 0);
}
}