// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The single-byte charsets as glibc 2.44 has them. //! //! None of these is a table of its own. Each is a table this library already //! has -- the Encoding Standard's, or Microsoft's EBCDIC pages as Unicode.org //! publishes them -- put through a rule or a short list of exceptions, all at //! compile time. The exceptions are the places where glibc departs from that //! source, found by asking glibc about every byte and written here in our own //! words. Nothing is copied out of glibc; `tools/crosscheck_glibc.py` //! checks the result against it, byte by byte and code point by code point. //! //! The decoders and encoders are the ordinary single-byte and EBCDIC ones, //! handed these tables instead. const std = @import("std"); const byte_table = @import("../byte_table.zig"); const codec = @import("../codec.zig"); const ebcdic_tables = @import("../tables/ebcdic.zig"); const Glibc = @import("../glibc.zig").Glibc; const whatwg = @import("../tables/single_byte.zig"); const unmapped = codec.unmapped; /// One departure from the table a charset starts from: what a byte decodes /// to instead, or null if glibc leaves it unassigned. const Exception = struct { byte: u8, code_point: ?u16 }; /// The upper half of ISO-8859-1, which is the identity: 0x80 to 0x9F are the /// C1 controls and the rest is Latin-1. const latin_1 = blk: { var table: [128]u16 = undefined; for (&table, 0x80..) |*entry, byte| entry.* = byte; break :blk table; }; /// ASCII has no upper half at all. const ascii: [128]u16 = @splat(unmapped); /// The standard's windows-125x and windows-874 assign every position, filling /// the ones Microsoft left empty with the C1 control of the same number. /// glibc leaves them empty, which is what Microsoft's own tables do. fn withoutFills(comptime table: [128]u16) [128]u16 { var result = table; for (result[0..0x20], 0x80..) |*entry, byte| { if (entry.* == byte) entry.* = unmapped; } return result; } /// The ISO 8859 parts that a Windows code page extends: the same graphic /// characters, with 0x80 to 0x9F the C1 controls rather than Microsoft's /// punctuation. fn withC1(comptime table: [128]u16) [128]u16 { var result = table; for (result[0..0x20], 0x80..) |*entry, byte| entry.* = byte; return result; } fn except(comptime table: anytype, comptime first_byte: u8, comptime exceptions: []const Exception) @TypeOf(table) { var result = table; for (exceptions) |exception| { result[exception.byte - first_byte] = exception.code_point orelse unmapped; } return result; } const tables = struct { const ansi_x3_4_1968 = ascii; const iso_8859_1 = latin_1; const iso_8859_9 = withC1(whatwg.windows_1254); const iso_8859_11 = withC1(whatwg.windows_874); /// TIS-620 has nothing at 0xA0, where ISO-8859-11 put the no-break space, /// and no C1 controls. const tis_620 = except(withC1(whatwg.windows_874), 0x80, &(blk: { var list: [0x21]Exception = undefined; for (&list, 0x80..) |*e, byte| e.* = .{ .byte = byte, .code_point = null }; break :blk list; })); const ibm874 = withoutFills(whatwg.windows_874); const cp1250 = withoutFills(whatwg.windows_1250); const cp1251 = withoutFills(whatwg.windows_1251); const cp1252 = withoutFills(whatwg.windows_1252); const cp1253 = withoutFills(whatwg.windows_1253); const cp1254 = withoutFills(whatwg.windows_1254); const cp1256 = withoutFills(whatwg.windows_1256); const cp1257 = withoutFills(whatwg.windows_1257); /// Code page 1255 leaves 0xCA unassigned too, where the standard has /// U+05BA HEBREW POINT HOLAM HASER FOR VAV. const cp1255 = except(withoutFills(whatwg.windows_1255), 0x80, &.{ .{ .byte = 0xca, .code_point = null }, }); const cp1258 = withoutFills(whatwg.windows_1258); /// KOI8-U as RFC 2319 has it: 0xAE and 0xBE are box drawing, where the /// standard puts KOI8-RU's Belarusian short U. const koi8_u = except(whatwg.koi8_u, 0x80, &.{ .{ .byte = 0xae, .code_point = 0x255d }, .{ .byte = 0xbe, .code_point = 0x256c }, }); /// KOI8-RU keeps the short U at 0xAE and 0xBE, and swaps nine of the /// symbols in 0x93 to 0x9F for typographic ones. const koi8_ru = except(whatwg.koi8_u, 0x80, &.{ .{ .byte = 0x93, .code_point = 0x201c }, .{ .byte = 0x96, .code_point = 0x201d }, .{ .byte = 0x97, .code_point = 0x2014 }, .{ .byte = 0x98, .code_point = 0x2116 }, .{ .byte = 0x99, .code_point = 0x2122 }, .{ .byte = 0x9b, .code_point = 0x00bb }, .{ .byte = 0x9c, .code_point = 0x00ae }, .{ .byte = 0x9d, .code_point = 0x00ab }, .{ .byte = 0x9f, .code_point = 0x00a4 }, }); /// glibc reads 0xC6 as GREEK CAPITAL LETTER DELTA rather than INCREMENT, /// and the Apple logo at 0xF0 as U+E01E rather than U+F8FF. const macintosh = except(whatwg.macintosh, 0x80, &.{ .{ .byte = 0xc6, .code_point = 0x0394 }, .{ .byte = 0xf0, .code_point = 0xe01e }, }); /// The currency sign at 0xFF, where the standard has the euro sign. const mac_uk = except(whatwg.x_mac_cyrillic, 0x80, &.{ .{ .byte = 0xff, .code_point = 0x00a4 }, }); /// The currency sign at 0xFF, and the cent sign at 0xA2 where the /// standard has the Ukrainian Ghe with upturn. const cp10007 = except(whatwg.x_mac_cyrillic, 0x80, &.{ .{ .byte = 0xa2, .code_point = 0x00a2 }, .{ .byte = 0xff, .code_point = 0x00a4 }, }); /// Code page 875 as glibc has it: 0x74 is NABLA and 0xDD the middle dot, /// where Microsoft has the no-break space and the Greek ano teleia; and /// 0x6A, and the six positions Microsoft spells as SUBSTITUTE alongside /// the real one at 0x3F, are unassigned. const ibm875 = except(ebcdic_tables.cp875, 0x00, &.{ .{ .byte = 0x74, .code_point = 0x2207 }, .{ .byte = 0xdd, .code_point = 0x00b7 }, .{ .byte = 0x6a, .code_point = null }, .{ .byte = 0xdc, .code_point = null }, .{ .byte = 0xe1, .code_point = null }, .{ .byte = 0xec, .code_point = null }, .{ .byte = 0xed, .code_point = null }, .{ .byte = 0xfc, .code_point = null }, .{ .byte = 0xfd, .code_point = null }, }); /// Code page 1026 as glibc has it: the ogonek at 0x9D rather than the /// cedilla, and the em dash at 0xBC rather than the macron. const ibm1026 = except(ebcdic_tables.cp1026, 0x00, &.{ .{ .byte = 0x9d, .code_point = 0x02db }, .{ .byte = 0xbc, .code_point = 0x2014 }, }); }; /// The upper half of a glibc single-byte charset, for the ordinary /// single-byte decoder. Null for the EBCDIC pages and for the multi-byte /// charsets. pub fn index(charset: Glibc) ?*const [128]u16 { return switch (charset) { .ansi_x3_4_1968 => &tables.ansi_x3_4_1968, .iso_8859_1 => &tables.iso_8859_1, .iso_8859_2 => &whatwg.iso_8859_2, .iso_8859_3 => &whatwg.iso_8859_3, .iso_8859_4 => &whatwg.iso_8859_4, .iso_8859_5 => &whatwg.iso_8859_5, .iso_8859_6 => &whatwg.iso_8859_6, .iso_8859_7 => &whatwg.iso_8859_7, .iso_8859_8 => &whatwg.iso_8859_8, .iso_8859_9 => &tables.iso_8859_9, .iso_8859_10 => &whatwg.iso_8859_10, .iso_8859_11 => &tables.iso_8859_11, .iso_8859_13 => &whatwg.iso_8859_13, .iso_8859_14 => &whatwg.iso_8859_14, .iso_8859_15 => &whatwg.iso_8859_15, .iso_8859_16 => &whatwg.iso_8859_16, .tis_620 => &tables.tis_620, .ibm874 => &tables.ibm874, .cp1250 => &tables.cp1250, .cp1251 => &tables.cp1251, .cp1252 => &tables.cp1252, .cp1253 => &tables.cp1253, .cp1254 => &tables.cp1254, .cp1256 => &tables.cp1256, .cp1257 => &tables.cp1257, .koi8_r => &whatwg.koi8_r, .koi8_u => &tables.koi8_u, .koi8_ru => &tables.koi8_ru, .macintosh => &tables.macintosh, .mac_uk => &tables.mac_uk, .cp10007 => &tables.cp10007, .ibm866 => &whatwg.ibm866, else => null, }; } /// The upper half of a code page whose combining marks glibc composes with /// the character before them, for `glibc_combining.zig` rather than the /// ordinary single-byte decoder. pub fn combiningIndex(charset: Glibc) ?*const [128]u16 { return switch (charset) { .cp1255 => &tables.cp1255, .cp1258 => &tables.cp1258, else => null, }; } /// The whole table of a glibc EBCDIC page, for the ordinary EBCDIC decoder. pub fn ebcdicIndex(charset: Glibc) ?*const [256]u16 { return switch (charset) { .ibm037 => &ebcdic_tables.cp037, .ibm500 => &ebcdic_tables.cp500, .ibm875 => &tables.ibm875, .ibm1026 => &tables.ibm1026, else => null, }; } /// The same tables sorted by code point, for the encoders, built once at /// compile time for each charset. const reversed_single = blk: { @setEvalBranchQuota(10_000_000); var result: [std.enums.values(Glibc).len]?byte_table.Reversed(128) = @splat(null); for (std.enums.values(Glibc), 0..) |member, i| { if (index(member)) |table| result[i] = byte_table.reverse(table.*, 0x80); } const final = result; break :blk final; }; const reversed_whole = blk: { @setEvalBranchQuota(10_000_000); var result: [std.enums.values(Glibc).len]?byte_table.Reversed(256) = @splat(null); for (std.enums.values(Glibc), 0..) |member, i| { if (ebcdicIndex(member)) |table| result[i] = byte_table.reverse(table.*, 0x00); } const final = result; break :blk final; }; pub fn reversedIndex(charset: Glibc) ?*const byte_table.Reversed(128) { return if (reversed_single[@backingInt(charset)]) |*table| table else null; } pub fn reversedEbcdicIndex(charset: Glibc) ?*const byte_table.Reversed(256) { return if (reversed_whole[@backingInt(charset)]) |*table| table else null; } test "every single-byte charset reverses to the table it came from" { for (std.enums.values(Glibc)) |charset| { if (index(charset)) |forward| { const backward = reversedIndex(charset).?; for (forward, 0x80..) |code_point, byte| { if (code_point == unmapped) continue; try std.testing.expectEqual(@as(?u8, @intCast(byte)), backward.byte(code_point)); } } if (ebcdicIndex(charset)) |forward| { const backward = reversedEbcdicIndex(charset).?; for (forward, 0..) |code_point, byte| { if (code_point == unmapped) continue; const back = backward.byte(code_point).?; // IBM037 and IBM500 keep Microsoft's several spellings of // SUBSTITUTE, which come back as the lowest of them. if (back != byte) try std.testing.expectEqual(@as(u16, 0x001a), code_point); } } } } test "the rules do what they say" { try std.testing.expectEqual(@as(u16, 0x0080), tables.iso_8859_1[0]); try std.testing.expectEqual(@as(u16, 0x00ff), tables.iso_8859_1[0x7f]); try std.testing.expectEqual(@as(u16, unmapped), tables.ansi_x3_4_1968[0]); // windows-1252 fills 0x81 with U+0081; glibc does not. try std.testing.expectEqual(@as(u16, 0x0081), whatwg.windows_1252[0x01]); try std.testing.expectEqual(@as(u16, unmapped), tables.cp1252[0x01]); try std.testing.expectEqual(@as(u16, 0x20ac), tables.cp1252[0x00]); // ISO-8859-9 has Latin-5's Ğ at 0xD0 and a C1 control at 0x80. try std.testing.expectEqual(@as(u16, 0x011e), tables.iso_8859_9[0x50]); try std.testing.expectEqual(@as(u16, 0x0080), tables.iso_8859_9[0x00]); try std.testing.expectEqual(@as(u16, unmapped), tables.tis_620[0x20]); try std.testing.expectEqual(@as(u16, 0x0e01), tables.tis_620[0x21]); }