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
//! One PDF byte-value grammar for resident and suspended original-range input.
use crate::bytes::{Form, is_space};
#[derive(Clone, Copy)]
enum State {
Ready,
NameHigh,
NameLow(u8),
Escape,
SkipLf,
Octal(u8, u8),
HexLow(u8),
End,
Failed,
}
pub(crate) enum Step {
/// Exactly one input byte was consumed, with no output.
More,
/// Output may precede consumption of the next byte (octal lookahead).
Byte {
value: u8,
consumed: bool,
},
End,
Malformed,
}
pub(crate) struct Decoder {
form: Form,
state: State,
}
impl Decoder {
pub(crate) fn new(form: Form) -> Self {
Self {
form,
state: State::Ready,
}
}
pub(crate) fn step(&mut self, input: Option<u8>) -> Step {
match self.state {
State::End => return Step::End,
State::Failed => return Step::Malformed,
State::NameHigh => {
let Some(value) = input.and_then(hex) else {
return self.fail();
};
self.state = State::NameLow(value);
return Step::More;
}
State::NameLow(high) => {
let Some(low) = input.and_then(hex) else {
return self.fail();
};
let value = high * 16 + low;
if value == 0 {
return self.fail();
}
self.state = State::Ready;
return Step::Byte {
value,
consumed: true,
};
}
State::Escape => return self.escaped(input),
State::SkipLf => {
self.state = State::Ready;
if input == Some(b'\n') {
return Step::More;
}
// A non-LF byte belongs to the next value. Process it here
// without a second source read, work charge or async request.
}
State::Octal(value, count) => {
if let Some(byte @ b'0'..=b'7') = input {
// ISO32000-1§7.3.4.2 explicitly ignores high-order overflow.
let value = value.wrapping_mul(8).wrapping_add(byte - b'0');
if count == 2 {
self.state = State::Ready;
return Step::Byte {
value,
consumed: true,
};
}
self.state = State::Octal(value, count + 1);
return Step::More;
}
self.state = State::Ready;
return Step::Byte {
value,
consumed: false,
};
}
State::HexLow(high) => {
if let Some(byte) = input {
if is_space(byte) {
return Step::More;
}
let Some(low) = hex(byte) else {
return self.fail();
};
self.state = State::Ready;
return Step::Byte {
value: high * 16 + low,
consumed: true,
};
}
// ISO32000-1§7.3.4.3 pads an odd final nibble with zero.
self.state = State::End;
return Step::Byte {
value: high * 16,
consumed: false,
};
}
State::Ready => {}
}
self.ready(input)
}
fn ready(&mut self, input: Option<u8>) -> Step {
let Some(byte) = input else {
self.state = State::End;
return Step::End;
};
match self.form {
Form::Name => {
// ISO32000-1§7.3.5: # introduces exactly two hexadecimal
// digits; a name cannot contain an escaped or literal zero.
// https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf
if byte == 0 {
return self.fail();
}
if byte == b'#' {
self.state = State::NameHigh;
return Step::More;
}
Step::Byte {
value: byte,
consumed: true,
}
}
Form::Literal => match byte {
b'\\' => {
self.state = State::Escape;
Step::More
}
b'\r' => {
self.state = State::SkipLf;
Step::Byte {
value: b'\n',
consumed: true,
}
}
_ => Step::Byte {
value: byte,
consumed: true,
},
},
Form::Hexadecimal => {
if is_space(byte) {
return Step::More;
}
let Some(high) = hex(byte) else {
return self.fail();
};
self.state = State::HexLow(high);
Step::More
}
}
}
fn escaped(&mut self, input: Option<u8>) -> Step {
let Some(byte) = input else {
return self.fail();
};
self.state = State::Ready;
let value = match byte {
b'\n' => return Step::More,
b'\r' => {
self.state = State::SkipLf;
return Step::More;
}
b'n' => b'\n',
b'r' => b'\r',
b't' => b'\t',
b'b' => 8,
b'f' => 12,
b'0'..=b'7' => {
self.state = State::Octal(byte - b'0', 1);
return Step::More;
}
// §7.3.4.2: unlisted escapes ignore only the backslash.
other => other,
};
Step::Byte {
value,
consumed: true,
}
}
fn fail(&mut self) -> Step {
self.state = State::Failed;
Step::Malformed
}
}
const fn hex(byte: u8) -> Option<u8> {
match byte {
b'0'..=b'9' => Some(byte - b'0'),
b'a'..=b'f' => Some(byte - b'a' + 10),
b'A'..=b'F' => Some(byte - b'A' + 10),
_ => None,
}
}
#[cfg(test)]
mod tests;