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
//! Patch opcodes and their v1 wire format.
//!
//! ```text
//! Copy: 0x00 offset:u32_le len:u32_le
//! Add : 0x01 len:u32_le bytes...
//! ```
use crate::error::PatchError;
use crate::{vbyte, wire};
use std::fmt;
use wire::{TAG_ADD, TAG_COPY};
pub const ADD_HEADER_LEN: usize = 5;
/// A single patch opcode.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum Op<'a> {
/// Copy `len` bytes from `old` starting at `offset`.
Copy {
/// Offset within `old`.
offset: u32,
/// Number of bytes to copy.
len: u32,
},
/// Append a literal byte run.
Add(&'a [u8]),
}
impl fmt::Debug for Op<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
enum Content<'a> {
Text(&'a str),
Bytes(&'a [u8]),
}
impl<'a> From<&'a [u8]> for Content<'a> {
fn from(value: &'a [u8]) -> Self {
match std::str::from_utf8(value) {
Ok(s) => Content::Text(s),
Err(_) => Content::Bytes(value),
}
}
}
impl fmt::Debug for Content<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Content::Text(s) => write!(f, "Text({s:?})"),
Content::Bytes(b) => {
write!(f, "Bytes(")?;
for (i, byte) in b.iter().enumerate() {
if i > 0 {
write!(f, " ")?;
}
write!(f, "{byte:02X}")?;
}
write!(f, ")")
}
}
}
}
match *self {
Self::Copy { offset, len } => f.debug_tuple("Copy").field(&offset).field(&len).finish(),
Self::Add(content) => f.debug_tuple("Add").field(&Content::from(content)).finish(),
}
}
}
impl<'a> Op<'a> {
/// Serialize this op into `out`.
///
/// # Errors
///
/// Returns [`PatchError::InputTooLarge`] if an `Add` payload exceeds
/// `u32::MAX` bytes.
pub fn serialize_to(&self, out: &mut Vec<u8>) -> Result<(), PatchError> {
match *self {
Op::Copy { offset, len } => {
out.push(TAG_COPY);
out.extend_from_slice(&offset.to_le_bytes());
out.extend_from_slice(&len.to_le_bytes());
Ok(())
}
Op::Add(bytes) => {
let len = u32::try_from(bytes.len())
.map_err(|_| PatchError::InputTooLarge { len: bytes.len() })?;
out.push(TAG_ADD);
out.extend_from_slice(&len.to_le_bytes());
out.extend_from_slice(bytes);
Ok(())
}
}
}
/// Parse the next opcode from `input`, returning the op and the remaining
/// bytes.
///
/// # Errors
///
/// See [`PatchError`] variants.
pub fn deserialize(input: &'a [u8]) -> Result<(Self, &'a [u8]), PatchError> {
let (&tag, rest) = input.split_first().ok_or(PatchError::UnexpectedEof)?;
match tag {
TAG_COPY => {
let (offset_bytes, rest) = rest
.split_first_chunk::<4>()
.ok_or(PatchError::UnexpectedEof)?;
let (len_bytes, rest) = rest
.split_first_chunk::<4>()
.ok_or(PatchError::UnexpectedEof)?;
Ok((
Op::Copy {
offset: u32::from_le_bytes(*offset_bytes),
len: u32::from_le_bytes(*len_bytes),
},
rest,
))
}
TAG_ADD => {
let (len_bytes, rest) = rest
.split_first_chunk::<4>()
.ok_or(PatchError::UnexpectedEof)?;
let declared = u32::from_le_bytes(*len_bytes);
let len = usize::try_from(declared).map_err(|_| PatchError::Overflow)?;
let (payload, rest) = rest
.split_at_checked(len)
.ok_or(PatchError::UnexpectedEof)?;
Ok((Op::Add(payload), rest))
}
other => Err(PatchError::InvalidOpcode(other)),
}
}
/// Iterate the opcodes in `patch` lazily, without allocating a `Vec`.
///
/// Both container formats are accepted; the sectioned v2 layout is
/// reassembled into the same opcode sequence a v1 patch would have
/// carried. The base fingerprint in a v2 header is *not* checked here —
/// [`crate::apply_patch`] does that.
#[must_use]
pub fn iter(patch: &'a [u8]) -> OpIter<'a> {
match wire::parse(patch) {
Ok(wire::Layout::V1(input)) => OpIter {
state: State::V1 { input },
},
Ok(wire::Layout::V2(patch)) => OpIter {
state: Sections::open(&patch).map_or_else(
|| State::Failed(PatchError::UnexpectedEof),
|sections| State::V2(Box::new(sections)),
),
},
Err(error) => OpIter {
state: State::Failed(error),
},
}
}
}
/// Iterator over the opcodes of a patch.
///
/// Yields `Err` once and then stops on the first malformed opcode.
#[derive(Debug, Clone)]
pub struct OpIter<'a> {
state: State<'a>,
}
#[derive(Debug, Clone)]
enum State<'a> {
V1 { input: &'a [u8] },
V2(Box<Sections<'a>>),
Failed(PatchError),
Done,
}
/// Cursor over the four sections of a v2 patch, reading one opcode per step.
#[derive(Debug, Clone)]
struct Sections<'a> {
tags: &'a [u8],
deltas: vbyte::Reader<'a>,
lengths: vbyte::Reader<'a>,
literals: &'a [u8],
index: usize,
/// End of the previous copy, which the next delta is measured against.
cursor: u32,
consumed_literals: usize,
}
impl<'a> Sections<'a> {
fn open(patch: &wire::PatchV2<'a>) -> Option<Self> {
// Section readers borrow the patch buffer, not the header struct.
Some(Self {
tags: patch.tags,
deltas: vbyte::Reader::new(patch.deltas, patch.copies())?,
lengths: vbyte::Reader::new(patch.lengths, patch.ops)?,
literals: patch.literals,
index: 0,
cursor: 0,
consumed_literals: 0,
})
}
fn next_op(&mut self) -> Option<Result<Op<'a>, PatchError>> {
let &tag = self.tags.get(self.index)?;
self.index = self.index.saturating_add(1);
let Some(len) = self.lengths.next() else {
return Some(Err(PatchError::UnexpectedEof));
};
match tag {
wire::TAG_COPY => {
let Some(delta) = self.deltas.next() else {
return Some(Err(PatchError::UnexpectedEof));
};
let step = vbyte::unzigzag(delta);
let offset = self
.cursor
.wrapping_add(u32::from_ne_bytes(step.to_ne_bytes()));
self.cursor = offset.wrapping_add(len);
Some(Ok(Op::Copy { offset, len }))
}
wire::TAG_ADD => {
let Ok(len) = usize::try_from(len) else {
return Some(Err(PatchError::Overflow));
};
let Some(end) = self.consumed_literals.checked_add(len) else {
return Some(Err(PatchError::Overflow));
};
let Some(payload) = self.literals.get(self.consumed_literals..end) else {
return Some(Err(PatchError::UnexpectedEof));
};
self.consumed_literals = end;
Some(Ok(Op::Add(payload)))
}
other => Some(Err(PatchError::InvalidOpcode(other))),
}
}
}
impl<'a> Iterator for OpIter<'a> {
type Item = Result<Op<'a>, PatchError>;
fn next(&mut self) -> Option<Self::Item> {
// Any error ends iteration, so a caller that stops at the first `Err`
// and one that drains the iterator see the same opcode sequence.
match &mut self.state {
State::Done => None,
State::Failed(error) => {
let error = error.clone();
self.state = State::Done;
Some(Err(error))
}
State::V1 { input } => {
if input.is_empty() {
self.state = State::Done;
return None;
}
match Op::deserialize(input) {
Ok((op, rest)) => {
*input = rest;
Some(Ok(op))
}
Err(error) => {
self.state = State::Done;
Some(Err(error))
}
}
}
State::V2(sections) => match sections.next_op() {
None => {
self.state = State::Done;
None
}
Some(Ok(op)) => Some(Ok(op)),
Some(Err(error)) => {
self.state = State::Done;
Some(Err(error))
}
},
}
}
}