-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiso_2022_jp.rs
More file actions
519 lines (451 loc) · 16 KB
/
iso_2022_jp.rs
File metadata and controls
519 lines (451 loc) · 16 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! ISO-2022-JP encoding.
//!
//! ISO-2022-JP is a stateful encoding that uses escape sequences to switch between
//! different character sets. It was commonly used for Japanese email.
//!
//! # Structure
//!
//! ISO-2022-JP uses escape sequences to switch between states:
//! - ESC ( B - ASCII (default state)
//! - ESC ( J - JIS X 0201 Roman (almost identical to ASCII)
//! - ESC $ @ - JIS X 0208-1978
//! - ESC $ B - JIS X 0208-1983
//!
//! # Stateful Decoding
//!
//! Because this is a stateful encoding, decoding at an arbitrary offset requires
//! scanning from the beginning of the string to track escape sequences. This makes
//! random access O(n) instead of O(1).
use alloc::vec::Vec;
use crate::encoding::Encoding;
use crate::error::EncodingError;
// Include generated tables (JIS X 0208 only - shared with EUC-JP)
include!(concat!(env!("OUT_DIR"), "/jis0208_tables.rs"));
/// ISO-2022-JP encoding.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Iso2022Jp;
/// State of the ISO-2022-JP decoder.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
/// ASCII mode (ESC ( B) - default
Ascii,
/// JIS X 0201 Roman mode (ESC ( J)
Roman,
/// JIS X 0208 mode (ESC $ @ or ESC $ B)
Jis0208,
}
impl Encoding for Iso2022Jp {
const NAME: &'static str = "ISO-2022-JP";
const IS_FIXED_WIDTH: bool = false;
const BYTES_PER_CHAR: Option<usize> = None;
const MAX_CHAR_LEN: usize = 2; // Plus escape sequences
fn validate(bytes: &[u8]) -> Result<(), EncodingError> {
let mut state = State::Ascii;
let mut i = 0;
while i < bytes.len() {
if bytes[i] == 0x1B {
// Escape sequence
match parse_escape(bytes, i) {
Some((new_state, len)) => {
state = new_state;
i += len;
}
None => {
return Err(EncodingError::new(i, Some(1)));
}
}
} else {
match state {
State::Ascii | State::Roman => {
let b = bytes[i];
// ASCII/Roman: 0x21-0x7E (printable) or 0x0A, 0x0D (newlines)
if b == 0x0A || b == 0x0D || (0x20..=0x7E).contains(&b) {
i += 1;
} else {
return Err(EncodingError::new(i, Some(1)));
}
}
State::Jis0208 => {
if i + 1 >= bytes.len() {
return Err(EncodingError::new(i, Some(1)));
}
let b1 = bytes[i];
let b2 = bytes[i + 1];
// JIS X 0208: 0x21-0x7E for both bytes
if !(0x21..=0x7E).contains(&b1) || !(0x21..=0x7E).contains(&b2) {
return Err(EncodingError::new(i, Some(2)));
}
// Validate the sequence maps to a valid character
let pointer = ((b1 - 0x21) as u16) * 94 + (b2 - 0x21) as u16;
if jis0208_lookup(pointer).is_none() {
return Err(EncodingError::new(i, Some(2)));
}
i += 2;
}
}
}
}
Ok(())
}
fn decode_char_at(bytes: &[u8], offset: usize) -> Option<(char, usize)> {
if offset >= bytes.len() {
return None;
}
// Scan from the beginning to determine the state at offset
let state = state_at_offset(bytes, offset)?;
// Check if we're at an escape sequence
if bytes[offset] == 0x1B {
return None; // Can't decode an escape sequence as a character
}
match state {
State::Ascii | State::Roman => {
let b = bytes[offset];
if b == 0x0A || b == 0x0D || (0x20..=0x7E).contains(&b) {
// JIS X 0201 Roman has slight differences (yen sign, overline)
// but for simplicity we treat them as ASCII
Some((b as char, offset + 1))
} else {
None
}
}
State::Jis0208 => {
let b1 = *bytes.get(offset)?;
let b2 = *bytes.get(offset + 1)?;
if (0x21..=0x7E).contains(&b1) && (0x21..=0x7E).contains(&b2) {
let pointer = ((b1 - 0x21) as u16) * 94 + (b2 - 0x21) as u16;
let c = jis0208_lookup(pointer)?;
Some((c, offset + 2))
} else {
None
}
}
}
}
fn encoded_len(c: char) -> usize {
let cp = c as u32;
// ASCII (excluding ESC)
if cp < 0x80 && cp != 0x1B {
return 1;
}
// Check if it's in JIS X 0208
if jis0208_encode_lookup(c).is_some() {
return 2;
}
// Can't encode
1
}
fn encode_char(c: char, buf: &mut [u8]) -> usize {
let cp = c as u32;
// ASCII (excluding ESC)
if cp < 0x80 && cp != 0x1B {
buf[0] = cp as u8;
return 1;
}
// JIS X 0208
if let Some(pointer) = jis0208_encode_lookup(c) {
let row = pointer / 94;
let col = pointer % 94;
buf[0] = (row + 0x21) as u8;
buf[1] = (col + 0x21) as u8;
return 2;
}
panic!(
"character '{}' (U+{:04X}) cannot be encoded in ISO-2022-JP",
c, cp
);
}
fn try_encode_char(c: char, buf: &mut [u8]) -> Option<usize> {
let cp = c as u32;
// ASCII (excluding ESC)
if cp < 0x80 && cp != 0x1B {
buf[0] = cp as u8;
return Some(1);
}
// JIS X 0208
if let Some(pointer) = jis0208_encode_lookup(c) {
let row = pointer / 94;
let col = pointer % 94;
buf[0] = (row + 0x21) as u8;
buf[1] = (col + 0x21) as u8;
return Some(2);
}
None
}
fn can_encode(c: char) -> bool {
let cp = c as u32;
// ASCII (excluding ESC)
if cp < 0x80 && cp != 0x1B {
return true;
}
// JIS X 0208
jis0208_encode_lookup(c).is_some()
}
fn is_char_boundary(bytes: &[u8], index: usize) -> bool {
if index == 0 || index >= bytes.len() {
return index <= bytes.len();
}
// Check if we're in the middle of an escape sequence
// ESC sequences are 3 bytes: 1B xx xx
if index >= 1 && bytes[index - 1] == 0x1B {
return false; // After ESC
}
if index >= 2 && bytes[index - 2] == 0x1B {
return false; // Second byte after ESC
}
// Determine state at this position
if let Some(state) = state_at_offset(bytes, index) {
match state {
State::Ascii | State::Roman => true,
State::Jis0208 => {
// In JIS X 0208 mode, check if we're at the second byte
// We need to count characters from the last escape sequence
// to see if we're on an even or odd byte
let mut scan = index;
while scan > 0 && bytes[scan - 1] != 0x1B {
scan -= 1;
}
// Skip past the escape sequence
if scan >= 3 && bytes[scan - 1] == 0x1B {
scan += 2; // Skip the two bytes after ESC
}
// Count how many bytes since the escape sequence
let offset_from_escape = index - scan;
offset_from_escape % 2 == 0
}
}
} else {
false
}
}
fn decode_char_before(bytes: &[u8], offset: usize) -> Option<(char, usize)> {
if offset == 0 || offset > bytes.len() {
return None;
}
// We need to find the previous character, which requires knowing the state
// at that position. This is tricky because we don't know where the previous
// character started.
// First, check if we're right after an escape sequence
if offset >= 3 && bytes[offset - 3] == 0x1B {
// We're right after an escape sequence, go back before it
return Self::decode_char_before(bytes, offset - 3);
}
// Determine state at offset - 1 (where the character might end)
// We scan from the beginning to find all escape sequences up to offset
let mut state = State::Ascii;
let mut i = 0;
let mut last_char_start = 0;
let mut last_char_state = State::Ascii;
while i < offset {
if bytes[i] == 0x1B {
if let Some((new_state, len)) = parse_escape(bytes, i) {
state = new_state;
i += len;
continue;
}
}
last_char_start = i;
last_char_state = state;
match state {
State::Ascii | State::Roman => {
i += 1;
}
State::Jis0208 => {
i += 2;
}
}
}
// If we overshot, we were in the middle of a character
if i > offset {
// Recalculate - go back one character
// This shouldn't happen if offset is a valid boundary
return None;
}
// last_char_start should be the start of the character ending at offset
if last_char_start >= offset {
return None;
}
// Decode at last_char_start
match last_char_state {
State::Ascii | State::Roman => {
let b = bytes[last_char_start];
if b == 0x0A || b == 0x0D || (0x20..=0x7E).contains(&b) {
Some((b as char, last_char_start))
} else {
None
}
}
State::Jis0208 => {
if last_char_start + 1 >= offset {
return None;
}
let b1 = bytes[last_char_start];
let b2 = bytes[last_char_start + 1];
if (0x21..=0x7E).contains(&b1) && (0x21..=0x7E).contains(&b2) {
let pointer = ((b1 - 0x21) as u16) * 94 + (b2 - 0x21) as u16;
let c = jis0208_lookup(pointer)?;
Some((c, last_char_start))
} else {
None
}
}
}
}
}
/// Parse an escape sequence at the given position.
/// Returns the new state and the length of the escape sequence.
fn parse_escape(bytes: &[u8], pos: usize) -> Option<(State, usize)> {
if bytes.get(pos)? != &0x1B {
return None;
}
let b1 = *bytes.get(pos + 1)?;
let b2 = *bytes.get(pos + 2)?;
match (b1, b2) {
// ESC ( B - ASCII
(0x28, 0x42) => Some((State::Ascii, 3)),
// ESC ( J - JIS X 0201 Roman
(0x28, 0x4A) => Some((State::Roman, 3)),
// ESC $ @ - JIS X 0208-1978
(0x24, 0x40) => Some((State::Jis0208, 3)),
// ESC $ B - JIS X 0208-1983
(0x24, 0x42) => Some((State::Jis0208, 3)),
_ => None,
}
}
/// Determine the decoder state at a given offset by scanning from the beginning.
fn state_at_offset(bytes: &[u8], offset: usize) -> Option<State> {
let mut state = State::Ascii;
let mut i = 0;
while i < offset {
if i >= bytes.len() {
break;
}
if bytes[i] == 0x1B {
if let Some((new_state, len)) = parse_escape(bytes, i) {
state = new_state;
i += len;
continue;
}
}
// Skip the character
match state {
State::Ascii | State::Roman => {
i += 1;
}
State::Jis0208 => {
i += 2;
}
}
}
Some(state)
}
/// Look up a character in the JIS X 0208 decode table.
fn jis0208_lookup(pointer: u16) -> Option<char> {
JIS0208_DECODE
.binary_search_by_key(&pointer, |&(p, _)| p)
.ok()
.map(|i| JIS0208_DECODE[i].1)
}
/// Look up the pointer for a character in JIS X 0208.
fn jis0208_encode_lookup(c: char) -> Option<u16> {
JIS0208_ENCODE
.binary_search_by_key(&c, |&(ch, _)| ch)
.ok()
.map(|i| JIS0208_ENCODE[i].1)
}
impl crate::encoding::LimitedEncoding for Iso2022Jp {}
#[cfg(feature = "registry")]
inventory::submit! {
crate::registry::EncodingEntry {
name: "ISO-2022-JP",
aliases: &["iso-2022-jp", "iso2022jp", "ISO2022JP", "csISO2022JP"],
is_unicode: false,
decode: |bytes| {
Iso2022Jp::validate(bytes)?;
let mut chars = Vec::new();
let mut offset = 0;
while let Some((c, next)) = Iso2022Jp::decode_char_at(bytes, offset) {
chars.push(c);
offset = next;
}
Ok(chars)
},
try_encode_char: |c| {
let mut buf = [0u8; 2];
Iso2022Jp::try_encode_char(c, &mut buf).map(|len| buf[..len].to_vec())
},
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ascii() {
let bytes = b"Hello";
assert_eq!(Iso2022Jp::decode_char_at(bytes, 0), Some(('H', 1)));
assert_eq!(Iso2022Jp::decode_char_at(bytes, 1), Some(('e', 2)));
}
#[test]
fn test_escape_sequence() {
// ESC $ B (switch to JIS X 0208)
let bytes = &[0x1B, 0x24, 0x42];
assert!(Iso2022Jp::validate(bytes).is_ok());
// Can't decode at escape position
assert_eq!(Iso2022Jp::decode_char_at(bytes, 0), None);
}
#[test]
fn test_jis0208() {
// "あ" in JIS X 0208 is row 4, col 2 (0-indexed from 0x21)
// ESC $ B + character + ESC ( B
let bytes = &[
0x1B, 0x24, 0x42, // ESC $ B (switch to JIS X 0208)
0x24, 0x22, // "あ" (hiragana A)
0x1B, 0x28, 0x42, // ESC ( B (switch back to ASCII)
];
assert!(Iso2022Jp::validate(bytes).is_ok());
// Decode at position 3 (after escape sequence)
let decoded = Iso2022Jp::decode_char_at(bytes, 3);
assert_eq!(decoded, Some(('あ', 5)));
}
#[test]
fn test_validate() {
// Valid ASCII
assert!(Iso2022Jp::validate(b"Hello").is_ok());
// Valid escape sequence
assert!(Iso2022Jp::validate(&[0x1B, 0x24, 0x42]).is_ok());
// Invalid: truncated escape
assert!(Iso2022Jp::validate(&[0x1B, 0x24]).is_err());
// Invalid: unknown escape
assert!(Iso2022Jp::validate(&[0x1B, 0x99, 0x99]).is_err());
}
#[test]
fn test_encode_ascii() {
let mut buf = [0u8; 2];
assert_eq!(Iso2022Jp::encode_char('A', &mut buf), 1);
assert_eq!(buf[0], b'A');
}
#[test]
fn test_encode_japanese() {
// Note: encode_char doesn't include escape sequences,
// just the raw character bytes
let mut buf = [0u8; 2];
let len = Iso2022Jp::encode_char('あ', &mut buf);
assert_eq!(len, 2);
assert_eq!(buf[0], 0x24);
assert_eq!(buf[1], 0x22);
}
#[test]
fn test_mixed_content() {
// "Aあ" - ASCII 'A' + JIS X 0208 'あ'
let bytes = &[
b'A', // ASCII 'A'
0x1B, 0x24, 0x42, // ESC $ B
0x24, 0x22, // "あ"
0x1B, 0x28, 0x42, // ESC ( B
];
assert!(Iso2022Jp::validate(bytes).is_ok());
// Decode 'A'
assert_eq!(Iso2022Jp::decode_char_at(bytes, 0), Some(('A', 1)));
// Decode 'あ' at position 4 (after escape)
assert_eq!(Iso2022Jp::decode_char_at(bytes, 4), Some(('あ', 6)));
}
}