// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The EBCDIC code pages. //! //! Shaped like the single-byte encodings and not quite one of them: EBCDIC //! shares nothing with ASCII, so there is no half of the table that can be //! assumed and no shortcut for a byte below 0x80. The letters are not even //! contiguous -- A to I, J to R and S to Z sit in three runs with gaps //! between them, a hole punched by the card codes the encoding grew out of -- //! which is why code that arranges bytes by arithmetic rather than by table //! gets EBCDIC wrong. //! //! Neither decoder nor encoder has any state. Both are here rather than in //! `single_byte.zig` because the byte offset and the table width differ, and //! because these are not in the Encoding Standard. const std = @import("std"); const byte_table = @import("../byte_table.zig"); const codec = @import("../codec.zig"); const tables = @import("../tables/ebcdic.zig"); const ByteQueue = codec.ByteQueue; const CodePage = @import("../ebcdic.zig").CodePage; const Decoded = codec.Decoded; const Encoded = codec.Encoded; pub fn index(code_page: CodePage) *const [256]u16 { return switch (code_page) { .cp037 => &tables.cp037, .cp500 => &tables.cp500, .cp875 => &tables.cp875, .cp1026 => &tables.cp1026, }; } const reversed = struct { const cp037 = byte_table.reverse(tables.cp037, 0x00); const cp500 = byte_table.reverse(tables.cp500, 0x00); const cp875 = byte_table.reverse(tables.cp875, 0x00); const cp1026 = byte_table.reverse(tables.cp1026, 0x00); }; pub fn reversedIndex(code_page: CodePage) *const byte_table.Reversed(256) { return switch (code_page) { .cp037 => &reversed.cp037, .cp500 => &reversed.cp500, .cp875 => &reversed.cp875, .cp1026 => &reversed.cp1026, }; } pub const Decoder = struct { index: *const [256]u16, pub fn handle(self: *Decoder, _: *ByteQueue, byte: ?u8) Decoded { const b = byte orelse return .finished; // Microsoft's four pages assign all 256 bytes, spelling an unassigned // position as U+001A rather than leaving it out, but glibc's IBM875 // leaves seven of them empty. const code_point = self.index[b]; if (code_point == codec.unmapped) return .failure; return .{ .code_point = code_point }; } }; pub const Encoder = struct { index: *const byte_table.Reversed(256), pub fn handle(self: *Encoder, code_point: ?u21) Encoded { const c = code_point orelse return .finished; const byte = self.index.byte(c) orelse return .{ .failure = c }; return .{ .bytes = .one(byte) }; } }; test "every byte decodes, and all but the unassigned ones come back" { for (std.enums.values(CodePage)) |code_page| { const forward = index(code_page); const backward = reversedIndex(code_page); for (forward, 0..) |code_point, byte| { const back = backward.byte(code_point).?; // The round trip holds except where the table spells several // bytes the same way, which is how Microsoft writes "unassigned": // those all come back as the lowest of them. if (back != byte) try std.testing.expectEqual(@as(u16, 0x001a), code_point); } } }