-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgb18030.rs
More file actions
436 lines (372 loc) · 13 KB
/
gb18030.rs
File metadata and controls
436 lines (372 loc) · 13 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
//! GB18030 encoding.
//!
//! GB18030 is China's mandatory national standard encoding that covers all Unicode code points.
//! It is a superset of GBK/GB2312 and is backwards compatible with ASCII.
//!
//! # Structure
//!
//! GB18030 uses variable-width encoding:
//! - 1 byte: ASCII (0x00-0x7F)
//! - 2 bytes: GBK characters (lead: 0x81-0xFE, trail: 0x40-0x7E or 0x80-0xFE)
//! - 4 bytes: All other Unicode (B1: 0x81-0xFE, B2: 0x30-0x39, B3: 0x81-0xFE, B4: 0x30-0x39)
use alloc::vec::Vec;
use crate::encoding::Encoding;
use crate::error::EncodingError;
// Include generated tables
include!(concat!(env!("OUT_DIR"), "/gb18030_tables.rs"));
/// GB18030 encoding - China's mandatory national standard.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Gb18030;
impl Encoding for Gb18030 {
const NAME: &'static str = "GB18030";
const IS_FIXED_WIDTH: bool = false;
const BYTES_PER_CHAR: Option<usize> = None;
const MAX_CHAR_LEN: usize = 4;
fn validate(bytes: &[u8]) -> Result<(), EncodingError> {
let mut i = 0;
while i < bytes.len() {
let b1 = bytes[i];
if b1 < 0x80 {
// ASCII
i += 1;
} else if (0x81..=0xFE).contains(&b1) {
if i + 1 >= bytes.len() {
return Err(EncodingError::new(i, Some(1)));
}
let b2 = bytes[i + 1];
if (0x30..=0x39).contains(&b2) {
// 4-byte sequence
if i + 3 >= bytes.len() {
return Err(EncodingError::new(i, Some(bytes.len() - i)));
}
let b3 = bytes[i + 2];
let b4 = bytes[i + 3];
if !(0x81..=0xFE).contains(&b3) || !(0x30..=0x39).contains(&b4) {
return Err(EncodingError::new(i, Some(4)));
}
// Validate the 4-byte sequence maps to a valid codepoint
if decode_4byte(b1, b2, b3, b4).is_none() {
return Err(EncodingError::new(i, Some(4)));
}
i += 4;
} else if (0x40..=0x7E).contains(&b2) || (0x80..=0xFE).contains(&b2) {
// 2-byte GBK sequence
if decode_2byte(b1, b2).is_none() {
return Err(EncodingError::new(i, Some(2)));
}
i += 2;
} else {
return Err(EncodingError::new(i, Some(2)));
}
} else {
return Err(EncodingError::new(i, Some(1)));
}
}
Ok(())
}
fn decode_char_at(bytes: &[u8], offset: usize) -> Option<(char, usize)> {
let b1 = *bytes.get(offset)?;
// 1-byte: ASCII
if b1 < 0x80 {
return Some((b1 as char, offset + 1));
}
// Must be a lead byte
if !(0x81..=0xFE).contains(&b1) {
return None;
}
let b2 = *bytes.get(offset + 1)?;
if (0x30..=0x39).contains(&b2) {
// 4-byte sequence
let b3 = *bytes.get(offset + 2)?;
let b4 = *bytes.get(offset + 3)?;
let c = decode_4byte(b1, b2, b3, b4)?;
Some((c, offset + 4))
} else if (0x40..=0x7E).contains(&b2) || (0x80..=0xFE).contains(&b2) {
// 2-byte GBK sequence
let c = decode_2byte(b1, b2)?;
Some((c, offset + 2))
} else {
None
}
}
fn encoded_len(c: char) -> usize {
let cp = c as u32;
// ASCII
if cp < 0x80 {
return 1;
}
// Check if it's in the GBK 2-byte table
if gbk_encode_lookup(c).is_some() {
return 2;
}
// Everything else is 4 bytes
4
}
fn encode_char(c: char, buf: &mut [u8]) -> usize {
let cp = c as u32;
// ASCII
if cp < 0x80 {
buf[0] = cp as u8;
return 1;
}
// Try 2-byte GBK
if let Some(bytes) = gbk_encode_lookup(c) {
buf[0] = (bytes >> 8) as u8;
buf[1] = (bytes & 0xFF) as u8;
return 2;
}
// 4-byte algorithmic encoding
let (b1, b2, b3, b4) = encode_4byte(c);
buf[0] = b1;
buf[1] = b2;
buf[2] = b3;
buf[3] = b4;
4
}
fn is_char_boundary(bytes: &[u8], index: usize) -> bool {
if index == 0 || index >= bytes.len() {
return index <= bytes.len();
}
let b = bytes[index];
// ASCII is always a boundary
if b < 0x80 {
return true;
}
// Lead byte (0x81-0xFE) is a boundary
if (0x81..=0xFE).contains(&b) {
// But we need to check if the previous byte was a lead byte
// that makes this a trail byte
if index > 0 {
let prev = bytes[index - 1];
if (0x81..=0xFE).contains(&prev) {
// Previous is a lead byte, check if this could be a valid trail
if (0x40..=0x7E).contains(&b) || (0x80..=0xFE).contains(&b) {
return false; // This is a trail byte of a 2-byte sequence
}
if (0x30..=0x39).contains(&b) {
// Could be byte 2 of a 4-byte sequence, not a boundary
return false;
}
}
}
return true;
}
// Trail bytes (0x30-0x39, 0x40-0x7E, 0x80) are not boundaries
false
}
fn decode_char_before(bytes: &[u8], offset: usize) -> Option<(char, usize)> {
if offset == 0 || offset > bytes.len() {
return None;
}
// Try 1-byte (ASCII)
let b1 = bytes[offset - 1];
if b1 < 0x80 {
return Some((b1 as char, offset - 1));
}
// Try 2-byte
if offset >= 2 {
let lead = bytes[offset - 2];
let trail = bytes[offset - 1];
if (0x81..=0xFE).contains(&lead) {
if (0x40..=0x7E).contains(&trail) || (0x80..=0xFE).contains(&trail) {
if let Some(c) = decode_2byte(lead, trail) {
return Some((c, offset - 2));
}
}
}
}
// Try 4-byte
if offset >= 4 {
let b1 = bytes[offset - 4];
let b2 = bytes[offset - 3];
let b3 = bytes[offset - 2];
let b4 = bytes[offset - 1];
if (0x81..=0xFE).contains(&b1)
&& (0x30..=0x39).contains(&b2)
&& (0x81..=0xFE).contains(&b3)
&& (0x30..=0x39).contains(&b4)
{
if let Some(c) = decode_4byte(b1, b2, b3, b4) {
return Some((c, offset - 4));
}
}
}
None
}
}
/// Decode a 2-byte GBK sequence.
fn decode_2byte(b1: u8, b2: u8) -> Option<char> {
let key = ((b1 as u16) << 8) | (b2 as u16);
// Binary search in the decode table
GBK_DECODE_2BYTE
.binary_search_by_key(&key, |&(k, _)| k)
.ok()
.map(|i| GBK_DECODE_2BYTE[i].1)
}
/// Look up 2-byte encoding for a character.
fn gbk_encode_lookup(c: char) -> Option<u16> {
// Binary search in the encode table
GBK_ENCODE
.binary_search_by_key(&c, |&(ch, _)| ch)
.ok()
.map(|i| GBK_ENCODE[i].1)
}
/// Decode a 4-byte GB18030 sequence using the algorithmic mapping.
fn decode_4byte(b1: u8, b2: u8, b3: u8, b4: u8) -> Option<char> {
// Validate byte ranges
if !(0x81..=0xFE).contains(&b1) {
return None;
}
if !(0x30..=0x39).contains(&b2) {
return None;
}
if !(0x81..=0xFE).contains(&b3) {
return None;
}
if !(0x30..=0x39).contains(&b4) {
return None;
}
// Calculate linear index (pointer)
let pointer = ((b1 as u32 - 0x81) * 12600)
+ ((b2 as u32 - 0x30) * 1260)
+ ((b3 as u32 - 0x81) * 10)
+ (b4 as u32 - 0x30);
// Use the ranges table to find the codepoint
pointer_to_codepoint(pointer)
}
/// Encode a character as 4-byte GB18030.
fn encode_4byte(c: char) -> (u8, u8, u8, u8) {
let codepoint = c as u32;
let pointer = codepoint_to_pointer(codepoint);
let b4 = (pointer % 10) as u8 + 0x30;
let pointer = pointer / 10;
let b3 = (pointer % 126) as u8 + 0x81;
let pointer = pointer / 126;
let b2 = (pointer % 10) as u8 + 0x30;
let b1 = (pointer / 10) as u8 + 0x81;
(b1, b2, b3, b4)
}
/// Convert a 4-byte pointer to a Unicode codepoint using the ranges table.
fn pointer_to_codepoint(pointer: u32) -> Option<char> {
// Binary search for the range containing this pointer
let idx = match GB18030_RANGES.binary_search_by_key(&pointer, |&(p, _)| p) {
Ok(i) => i, // Exact match
Err(0) => return None, // Before first range
Err(i) => i - 1, // In a range
};
let (range_pointer, range_codepoint) = GB18030_RANGES[idx];
let offset = pointer - range_pointer;
let codepoint = range_codepoint + offset;
// Check we haven't gone past the next range
if idx + 1 < GB18030_RANGES.len() {
let (next_pointer, _) = GB18030_RANGES[idx + 1];
if pointer >= next_pointer {
return None;
}
}
char::from_u32(codepoint)
}
/// Convert a Unicode codepoint to a 4-byte pointer using the ranges table.
fn codepoint_to_pointer(codepoint: u32) -> u32 {
// Binary search for the range containing this codepoint
let idx = match GB18030_RANGES.binary_search_by_key(&codepoint, |&(_, cp)| cp) {
Ok(i) => i,
Err(0) => 0,
Err(i) => i - 1,
};
let (range_pointer, range_codepoint) = GB18030_RANGES[idx];
let offset = codepoint - range_codepoint;
range_pointer + offset
}
impl crate::encoding::UniversalEncoding for Gb18030 {}
// === Registry registration ===
#[cfg(feature = "registry")]
inventory::submit! {
crate::registry::EncodingEntry {
name: "GB18030",
aliases: &["gb18030", "GB-18030", "gb-18030", "GBK", "gbk", "GB2312", "gb2312"],
is_unicode: true,
decode: |bytes| {
Gb18030::validate(bytes)?;
let mut chars = Vec::new();
let mut offset = 0;
while let Some((c, next)) = Gb18030::decode_char_at(bytes, offset) {
chars.push(c);
offset = next;
}
Ok(chars)
},
try_encode_char: |c| {
let mut buf = [0u8; 4];
let len = Gb18030::encode_char(c, &mut buf);
Some(buf[..len].to_vec())
},
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ascii() {
let mut buf = [0u8; 4];
assert_eq!(Gb18030::encode_char('A', &mut buf), 1);
assert_eq!(buf[0], b'A');
let bytes = b"Hello";
assert_eq!(Gb18030::decode_char_at(bytes, 0), Some(('H', 1)));
}
#[test]
fn test_2byte_gbk() {
// Test a common Chinese character: 中 (U+4E2D)
// In GBK, 中 is encoded as 0xD6D0
let mut buf = [0u8; 4];
let len = Gb18030::encode_char('中', &mut buf);
assert_eq!(len, 2);
assert_eq!(buf[0], 0xD6);
assert_eq!(buf[1], 0xD0);
// Decode it back
assert_eq!(Gb18030::decode_char_at(&[0xD6, 0xD0], 0), Some(('中', 2)));
}
#[test]
fn test_4byte() {
// Test a character not in GBK that requires 4-byte encoding
// U+20000 (𠀀) - first CJK Extension B character
let mut buf = [0u8; 4];
let len = Gb18030::encode_char('\u{20000}', &mut buf);
assert_eq!(len, 4);
// Decode it back
let decoded = Gb18030::decode_char_at(&buf, 0);
assert_eq!(decoded, Some(('\u{20000}', 4)));
}
#[test]
fn test_euro_sign() {
// Euro sign € (U+20AC) is in the 4-byte range for GB18030
// (it's not in GBK)
let mut buf = [0u8; 4];
let len = Gb18030::encode_char('€', &mut buf);
// Decode it back
let decoded = Gb18030::decode_char_at(&buf[..len], 0);
assert_eq!(decoded.map(|(c, _)| c), Some('€'));
}
#[test]
fn test_validate() {
// Valid ASCII
assert!(Gb18030::validate(b"Hello").is_ok());
// Valid 2-byte
assert!(Gb18030::validate(&[0xD6, 0xD0]).is_ok());
// Invalid: truncated 2-byte
assert!(Gb18030::validate(&[0xD6]).is_err());
// Invalid: bad trail byte
assert!(Gb18030::validate(&[0xD6, 0x20]).is_err());
}
#[test]
fn test_roundtrip() {
// Test roundtrip for various characters
let chars = ['A', '中', '国', '€', '\u{20000}', '日', '本'];
for c in chars {
let mut buf = [0u8; 4];
let len = Gb18030::encode_char(c, &mut buf);
let decoded = Gb18030::decode_char_at(&buf[..len], 0);
assert_eq!(decoded, Some((c, len)), "Failed roundtrip for {:?}", c);
}
}
}