Something went wrong. Try again.
Converting between UTF-8 and legacy character encodings, as the WHATWG Encoding Standard and glibc's iconv define them.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! gb18030 and GBK, § 10.//!//! One decoder serves both: GBK's decoder *is* gb18030's. The encoders differ//! by a single flag. GBK's will not reach for the four-byte form, so a code//! point outside the two-byte index is an error rather than something a//! consumer expecting GBK would not understand -- a conservative move, and//! the reason the standard keeps two names for what is nearly one encoding.//!//! glibc 2.44's GB18030 is this with a flag set, and differs in three//! places, all both ways round, so that every scalar value reads back as//! itself://!//! - The single byte 0x80 is an error rather than the euro sign.//! - 0xA3A0 is U+E5E5, as GB 18030 itself has it, rather than U+3000.//! - The eighteen private use code points that GB18030-2022 moved out of//! the two-byte table are four-byte sequences, not the two-byte ones the//! standard's encoder keeps for them: see `glibc_four_byte`.
const std = @import("std");
const codec = @import("../codec.zig");const index = @import("../index.zig");const ranges = @import("../tables/gb18030_ranges.zig");const tables = @import("../tables/gb18030.zig");
const ByteQueue = codec.ByteQueue;const Decoded = codec.Decoded;const Encoded = codec.Encoded;
const gb18030_index: index.Index(u16) = .{ .code_points = &tables.encode_code_points, .pointers = &tables.encode_pointers,};
/// The four-byte form is a four-digit mixed-radix number: the first and third/// bytes carry 126 values each and the second and fourth carry ten.const third_place = 10;const second_place = 10 * 126;const first_place = 10 * 126 * 10;
/// The pointer of 0xA3A0, which glibc reads as U+E5E5.const a3a0 = (0xa3 - 0x81) * 190 + (0xa0 - 0x41);
/// The four-byte sequences glibc reads as private use code points where the/// standard's ranges give the characters GB18030-2022 moved into the/// two-byte table, which glibc reads from there as the standard does.const glibc_four_byte = [_]struct { pointer: u32, code_point: u21 }{ .{ .pointer = 19057, .code_point = 0xe81e }, .{ .pointer = 19058, .code_point = 0xe826 }, .{ .pointer = 19059, .code_point = 0xe82b }, .{ .pointer = 19060, .code_point = 0xe82c }, .{ .pointer = 19061, .code_point = 0xe832 }, .{ .pointer = 19062, .code_point = 0xe843 }, .{ .pointer = 19063, .code_point = 0xe854 }, .{ .pointer = 19064, .code_point = 0xe864 }, .{ .pointer = 39076, .code_point = 0xe78d }, .{ .pointer = 39077, .code_point = 0xe78f }, .{ .pointer = 39078, .code_point = 0xe78e }, .{ .pointer = 39079, .code_point = 0xe790 }, .{ .pointer = 39080, .code_point = 0xe791 }, .{ .pointer = 39081, .code_point = 0xe792 }, .{ .pointer = 39082, .code_point = 0xe793 }, .{ .pointer = 39083, .code_point = 0xe794 }, .{ .pointer = 39084, .code_point = 0xe795 }, .{ .pointer = 39085, .code_point = 0xe796 },};
pub const Decoder = struct { /// GB18030 as glibc has it. glibc: bool = false, first: u8 = 0, second: u8 = 0, third: u8 = 0,
pub fn handle(self: *Decoder, queue: *ByteQueue, byte: ?u8) Decoded { const b = byte orelse { if (self.first == 0 and self.second == 0 and self.third == 0) return .finished; self.* = .{ .glibc = self.glibc }; return .failure; };
if (self.third != 0) { if (b < 0x30 or b > 0x39) { queue.restore(&.{ self.second, self.third, b }); self.* = .{ .glibc = self.glibc }; return .failure; } const pointer = (@as(u32, self.first - 0x81) * first_place) + (@as(u32, self.second - 0x30) * second_place) + (@as(u32, self.third - 0x81) * third_place) + (b - 0x30); self.* = .{ .glibc = self.glibc }; if (self.glibc) for (glibc_four_byte) |entry| { if (entry.pointer == pointer) return .{ .code_point = entry.code_point }; }; return if (rangesCodePoint(pointer)) |c| .{ .code_point = c } else .failure; }
if (self.second != 0) { if (b >= 0x81 and b <= 0xfe) { self.third = b; return .cont; } queue.restore(&.{ self.second, b }); self.first = 0; self.second = 0; return .failure; }
if (self.first != 0) { if (b >= 0x30 and b <= 0x39) { self.second = b; return .cont; } const leading = self.first; self.first = 0;
if ((b >= 0x40 and b <= 0x7e) or (b >= 0x80 and b <= 0xfe)) { const offset: u8 = if (b < 0x7f) 0x40 else 0x41; const pointer = (@as(u32, leading - 0x81) * 190) + (b - offset); if (self.glibc and pointer == a3a0) return .{ .code_point = 0xe5e5 }; if (index.codePoint(&tables.decode, pointer)) |c| return .{ .code_point = c }; } if (b < 0x80) queue.restore(&.{b}); return .failure; }
if (b < 0x80) return .{ .code_point = b }; // The one byte that means something on its own: gb18030 puts the euro // sign where GBK's single-byte range ends. if (b == 0x80) return if (self.glibc) .failure else .{ .code_point = 0x20ac }; if (b >= 0x81 and b <= 0xfe) { self.first = b; return .cont; } return .failure; }};
/// The standard's "index gb18030 ranges code point for pointer".fn rangesCodePoint(pointer: u32) ?u21 { if ((pointer > 39419 and pointer < 189000) or pointer > 1237575) return null; // The one code point the GB18030-2005 revision moved, handled here rather // than by adding a range of length one. if (pointer == 7457) return 0xe7c7;
const i = lastAtOrBelow(&ranges.pointers, pointer) orelse return null; return @intCast(ranges.code_points[i] + (pointer - ranges.pointers[i]));}
/// The standard's "index gb18030 ranges pointer for codePoint".fn rangesPointer(code_point: u21) ?u32 { if (code_point == 0xe7c7) return 7457;
const i = lastAtOrBelow(&ranges.code_points, code_point) orelse return null; return ranges.pointers[i] + (@as(u32, code_point) - ranges.code_points[i]);}
/// The index of the last entry that is less than or equal to `value`, both/// arrays of `index gb18030 ranges` being sorted ascending.fn lastAtOrBelow(haystack: []const u32, value: u32) ?usize { const order = struct { fn order(context: u32, item: u32) std.math.Order { return std.math.order(context, item); } }.order; const after = std.sort.upperBound(u32, haystack, value, order); return if (after == 0) null else after - 1;}
/// The eighteen private use code points the gb18030 encoder spells out/// itself, rather than through the index.////// GB18030-2022 moved these, and following it would have changed the bytes/// that existing content decodes from. The standard keeps the old answers in/// the encoder and the new ones in the index, which is why this table is/// asymmetric: encoding one of these and decoding the result does not give it/// back.const encoder_side_table = [_]struct { code_point: u21, bytes: [2]u8 }{ .{ .code_point = 0xe78d, .bytes = .{ 0xa6, 0xd9 } }, .{ .code_point = 0xe78e, .bytes = .{ 0xa6, 0xda } }, .{ .code_point = 0xe78f, .bytes = .{ 0xa6, 0xdb } }, .{ .code_point = 0xe790, .bytes = .{ 0xa6, 0xdc } }, .{ .code_point = 0xe791, .bytes = .{ 0xa6, 0xdd } }, .{ .code_point = 0xe792, .bytes = .{ 0xa6, 0xde } }, .{ .code_point = 0xe793, .bytes = .{ 0xa6, 0xdf } }, .{ .code_point = 0xe794, .bytes = .{ 0xa6, 0xec } }, .{ .code_point = 0xe795, .bytes = .{ 0xa6, 0xed } }, .{ .code_point = 0xe796, .bytes = .{ 0xa6, 0xf3 } }, .{ .code_point = 0xe81e, .bytes = .{ 0xfe, 0x59 } }, .{ .code_point = 0xe826, .bytes = .{ 0xfe, 0x61 } }, .{ .code_point = 0xe82b, .bytes = .{ 0xfe, 0x66 } }, .{ .code_point = 0xe82c, .bytes = .{ 0xfe, 0x67 } }, .{ .code_point = 0xe832, .bytes = .{ 0xfe, 0x6d } }, .{ .code_point = 0xe843, .bytes = .{ 0xfe, 0x7e } }, .{ .code_point = 0xe854, .bytes = .{ 0xfe, 0x90 } }, .{ .code_point = 0xe864, .bytes = .{ 0xfe, 0xa0 } },};
pub const Encoder = struct { /// GBK's encoder is this one with the flag set. is_gbk: bool = false, /// GB18030 as glibc has it. glibc: bool = false,
pub fn handle(self: *Encoder, code_point: ?u21) Encoded { const c = code_point orelse return .finished; if (c < 0x80) return .{ .bytes = .one(@intCast(c)) };
if (self.glibc) { if (c == 0xe5e5) return .{ .bytes = .two(0xa3, 0xa0) }; for (glibc_four_byte) |entry| { if (entry.code_point == c) return fourBytes(entry.pointer); } }
// The index maps 0xA3 0xA0 to U+3000 rather than to U+E5E5, for // compatibility with deployed content, so U+E5E5 has no bytes to go // to and cannot round trip. if (c == 0xe5e5) return .{ .failure = c };
if (self.is_gbk and c == 0x20ac) return .{ .bytes = .one(0x80) };
if (!self.glibc) for (encoder_side_table) |entry| { if (entry.code_point == c) return .{ .bytes = .two(entry.bytes[0], entry.bytes[1]) }; };
if (gb18030_index.pointer(c)) |pointer| { const leading: u8 = @intCast(pointer / 190 + 0x81); const trailing = pointer % 190; const offset: u8 = if (trailing < 0x3f) 0x40 else 0x41; return .{ .bytes = .two(leading, @intCast(trailing + offset)) }; }
if (self.is_gbk) return .{ .failure = c };
return fourBytes(rangesPointer(c) orelse return .{ .failure = c }); }};
/// The four bytes of a four-byte pointer.fn fourBytes(four_byte_pointer: u32) Encoded { var pointer = four_byte_pointer; const byte1 = pointer / first_place; pointer %= first_place; const byte2 = pointer / second_place; pointer %= second_place; const byte3 = pointer / third_place; const byte4 = pointer % third_place; return .{ .bytes = .four( @intCast(byte1 + 0x81), @intCast(byte2 + 0x30), @intCast(byte3 + 0x81), @intCast(byte4 + 0x30), ) };}
test "glibc's GB18030 reads back every code point as itself" { var encoder: Encoder = .{ .glibc = true }; try std.testing.expectEqualSlices(u8, "\xa3\xa0", encoder.handle(0xe5e5).bytes.slice()); try std.testing.expectEqualSlices(u8, "\x82\x35\x90\x37", encoder.handle(0xe81e).bytes.slice()); try std.testing.expectEqualSlices(u8, "\xfe\x59", encoder.handle(0x9fb4).bytes.slice()); try std.testing.expectEqualSlices(u8, "\xa1\xa1", encoder.handle(0x3000).bytes.slice());
var queue: ByteQueue = .{ .input = "\x82\x35\x90\x37\x80" }; var decoder: Decoder = .{ .glibc = true }; for (0..3) |_| try std.testing.expectEqual(Decoded.cont, decoder.handle(&queue, queue.next())); try std.testing.expectEqual(Decoded{ .code_point = 0xe81e }, decoder.handle(&queue, queue.next())); try std.testing.expectEqual(Decoded.failure, decoder.handle(&queue, queue.next()));}