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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! ISO-2022-JP as RFC 1468 defines it: ASCII, JIS X 0201's Roman set, and
//! JIS X 0208.
//!
//! Both halves are escape-driven state machines: the decoder tracks the escape it
//! is in the middle of parsing separately from the mode that escape selected, and
//! the encoder emits an escape whenever the character at hand needs a different
//! mode than the one currently active.
//!
//! The WHATWG Encoding Standard's encoding of the same name adds a fourth mode
//! behind `ESC ( I`, for half-width katakana that RFC 1468 has no set for; it
//! folds the NEC and IBM extension rows into the JIS X 0208 plane and remaps six
//! of its pointers; and its encoder turns half-width katakana into the fullwidth
//! forms rather than refusing them. That one is [`X_WHATWG_ISO_2022_JP`].
//!
//! [`X_WHATWG_ISO_2022_JP`]: crate::X_WHATWG_ISO_2022_JP
use crate::ascii::ascii_prefix_len_capped;
use crate::jis0208_1997::{code_point, pointer};
use crate::result::{DecoderResult, EncoderResult};
use crate::sink::{ByteSink, DECODER_HEADROOM, ENCODER_HEADROOM, Pushback};
const ESCAPE_ASCII: [u8; 3] = [0x1B, 0x28, 0x42];
const ESCAPE_ROMAN: [u8; 3] = [0x1B, 0x28, 0x4A];
const ESCAPE_JIS0208: [u8; 3] = [0x1B, 0x24, 0x42];
/// The mode an escape sequence selects, which the decoder returns to after a
/// malformed escape.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Mode {
Ascii,
Roman,
LeadingByte,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DecoderState {
Mode(Mode),
TrailingByte,
EscapeStart,
Escape,
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Iso2022Jp1468Decoder {
state: DecoderState,
output_mode: Mode,
leading: u8,
/// Whether an escape has been seen with no output since; two escapes in a row
/// are an error, which is what keeps escape sequences from being smuggled
/// through a filter that only inspects the decoded text.
output: bool,
pending: Pushback,
}
impl Default for Iso2022Jp1468Decoder {
fn default() -> Self {
Iso2022Jp1468Decoder {
state: DecoderState::Mode(Mode::Ascii),
output_mode: Mode::Ascii,
leading: 0,
output: false,
pending: Pushback::default(),
}
}
}
impl Iso2022Jp1468Decoder {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn decode(
&mut self,
src: &[u8],
sink: &mut ByteSink,
last: bool,
) -> (DecoderResult, usize) {
let mut read = 0usize;
loop {
if !sink.has_room(DECODER_HEADROOM) {
return (DecoderResult::OutputFull, read);
}
let (byte, from_pending) = match self.pending.next() {
Some(byte) => (Some(byte), true),
None => {
let byte = src.get(read).copied();
if byte.is_some() {
read += 1;
}
(byte, false)
}
};
if byte.is_none() && !last {
return (DecoderResult::InputEmpty, read);
}
match self.state {
DecoderState::Mode(Mode::Ascii) => match byte {
Some(0x1B) => self.state = DecoderState::EscapeStart,
Some(b) if b < 0x80 && b != 0x0E && b != 0x0F => {
self.output = false;
sink.write_byte(b);
}
None => return (DecoderResult::InputEmpty, read),
Some(_) => {
self.output = false;
return (DecoderResult::Malformed(1), read);
}
},
DecoderState::Mode(Mode::Roman) => match byte {
Some(0x1B) => self.state = DecoderState::EscapeStart,
Some(0x5C) => {
self.output = false;
sink.write_code_point(0x00A5);
}
Some(0x7E) => {
self.output = false;
sink.write_code_point(0x203E);
}
Some(b) if b < 0x80 && b != 0x0E && b != 0x0F => {
self.output = false;
sink.write_byte(b);
}
None => return (DecoderResult::InputEmpty, read),
Some(_) => {
self.output = false;
return (DecoderResult::Malformed(1), read);
}
},
DecoderState::Mode(Mode::LeadingByte) => match byte {
Some(0x1B) => self.state = DecoderState::EscapeStart,
Some(b @ 0x21..=0x7E) => {
self.output = false;
self.leading = b;
self.state = DecoderState::TrailingByte;
}
None => return (DecoderResult::InputEmpty, read),
Some(_) => {
self.output = false;
return (DecoderResult::Malformed(1), read);
}
},
DecoderState::TrailingByte => match byte {
Some(0x1B) => {
self.state = DecoderState::EscapeStart;
return (DecoderResult::Malformed(1), read);
}
Some(b @ 0x21..=0x7E) => {
self.state = DecoderState::Mode(Mode::LeadingByte);
let pointer =
(usize::from(self.leading) - 0x21) * 94 + (usize::from(b) - 0x21);
match code_point(pointer) {
Some(code_point) => sink.write_code_point(code_point),
None => return (DecoderResult::Malformed(2), read),
}
}
None => {
self.state = DecoderState::Mode(Mode::LeadingByte);
return (DecoderResult::Malformed(1), read);
}
Some(_) => {
self.state = DecoderState::Mode(Mode::LeadingByte);
return (DecoderResult::Malformed(2), read);
}
},
DecoderState::EscapeStart => match byte {
Some(b @ (0x24 | 0x28)) => {
self.leading = b;
self.state = DecoderState::Escape;
}
other => {
if other.is_some() {
if from_pending {
self.pending.unread();
} else {
read -= 1;
}
}
self.output = false;
self.state = DecoderState::Mode(self.output_mode);
return (DecoderResult::Malformed(1), read);
}
},
DecoderState::Escape => {
let leading = self.leading;
self.leading = 0;
let mode = match (leading, byte) {
(0x28, Some(0x42)) => Some(Mode::Ascii),
(0x28, Some(0x4A)) => Some(Mode::Roman),
(0x24, Some(0x40 | 0x42)) => Some(Mode::LeadingByte),
_ => None,
};
if let Some(mode) = mode {
self.state = DecoderState::Mode(mode);
self.output_mode = mode;
let had_output = self.output;
self.output = true;
if had_output {
return (DecoderResult::Malformed(1), read);
}
continue;
}
debug_assert!(!from_pending, "escape bytes never come from the pushback");
if byte.is_some() {
read -= 1;
}
self.pending.set(&[leading]);
self.output = false;
self.state = DecoderState::Mode(self.output_mode);
return (DecoderResult::Malformed(1), read);
}
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum EncoderState {
Ascii,
Roman,
Jis0208,
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Iso2022Jp1468Encoder {
state: EncoderState,
}
impl Default for Iso2022Jp1468Encoder {
fn default() -> Self {
Iso2022Jp1468Encoder {
state: EncoderState::Ascii,
}
}
}
impl Iso2022Jp1468Encoder {
pub(crate) fn encode(
&mut self,
src: &str,
sink: &mut ByteSink,
last: bool,
) -> (EncoderResult, usize) {
let mut read = 0usize;
loop {
if !sink.has_room(ENCODER_HEADROOM) {
return (EncoderResult::OutputFull, read);
}
let rest = &src[read..];
let Some(c) = rest.chars().next() else {
if last && self.state != EncoderState::Ascii {
self.state = EncoderState::Ascii;
sink.write_slice(&ESCAPE_ASCII);
}
return (EncoderResult::InputEmpty, read);
};
if self.state == EncoderState::Ascii {
let run = ascii_prefix_len_capped(rest.as_bytes(), sink.room());
let run = rest.as_bytes()[..run]
.iter()
.position(|&b| b == 0x0E || b == 0x0F || b == 0x1B)
.unwrap_or(run);
if run > 0 {
sink.write_slice(&rest.as_bytes()[..run]);
read += run;
continue;
}
}
let scalar = u32::from(c);
let width = c.len_utf8();
if matches!(self.state, EncoderState::Ascii | EncoderState::Roman)
&& matches!(scalar, 0x000E | 0x000F | 0x001B)
{
// Reported as U+FFFD rather than the character itself so that a
// caller substituting errors cannot be tricked into emitting an
// escape sequence.
read += width;
return (EncoderResult::Unmappable(char::REPLACEMENT_CHARACTER), read);
}
if self.state == EncoderState::Ascii && c.is_ascii() {
sink.write_byte(scalar as u8);
read += width;
continue;
}
if self.state == EncoderState::Roman {
match scalar {
0x005C | 0x007E => {}
0x00A5 => {
sink.write_byte(0x5C);
read += width;
continue;
}
0x203E => {
sink.write_byte(0x7E);
read += width;
continue;
}
_ if c.is_ascii() => {
sink.write_byte(scalar as u8);
read += width;
continue;
}
_ => {}
}
}
// The remaining arms switch mode and re-examine the same character.
if c.is_ascii() && self.state != EncoderState::Ascii {
self.state = EncoderState::Ascii;
sink.write_slice(&ESCAPE_ASCII);
continue;
}
if matches!(scalar, 0x00A5 | 0x203E) && self.state != EncoderState::Roman {
self.state = EncoderState::Roman;
sink.write_slice(&ESCAPE_ROMAN);
continue;
}
// RFC 1468 has no set for the half-width katakana, and no reason
// to reach for the fullwidth forms instead: they are a different
// character, and this refuses rather than substitutes.
let Some(pointer) = pointer(scalar) else {
if self.state == EncoderState::Jis0208 {
self.state = EncoderState::Ascii;
sink.write_slice(&ESCAPE_ASCII);
continue;
}
read += width;
return (EncoderResult::Unmappable(c), read);
};
if self.state != EncoderState::Jis0208 {
self.state = EncoderState::Jis0208;
sink.write_slice(&ESCAPE_JIS0208);
continue;
}
let pointer = u32::from(pointer);
sink.write_slice(&[(pointer / 94 + 0x21) as u8, (pointer % 94 + 0x21) as u8]);
read += width;
}
}
}