Groundwork for multi codepage support

This commit is contained in:
Matthew R. Wilson committed 2025-11-14 23:54:11 -08:00
1 parent c0ad185cd7
commit 2f96d51ac3
3 files changed
+82 -25

No files matched your search

+79 -22
View File
@@ -8,8 +8,18 @@ import (
"unicode/utf8" "unicode/utf8"
) )
// After careful consideration, I have decided that the code page we will // Implementations of Charset provide EBCDIC<->UTF-8 translation.
// support for EBCDIC is IBM CP 1047. type Charset interface {
// Decode converts a slice of EBCDIC bytes into a UTF-8 string.
Decode(e []byte) string
// Encode converts a UTF-8 string into a slice of EBCDIC bytes.
Encode(s string) []byte
}
// After careful consideration, I have decided that the default code page we
// will support for EBCDIC is IBM CP 1047. Other code pages may be globally
// selected with the SetCodepage() function.
// //
// In suite3270 (e.g. c3270/x3270), the default code page is what it calls // In suite3270 (e.g. c3270/x3270), the default code page is what it calls
// "brackets". This is CP37 with the [, ], Ý, and ¨ characters swapped around. // "brackets". This is CP37 with the [, ], Ý, and ¨ characters swapped around.
@@ -28,11 +38,49 @@ import (
// In IBM PCOMM, CP37 is the default. For correct mapping of [, ], Ý, ¨, ^, // In IBM PCOMM, CP37 is the default. For correct mapping of [, ], Ý, ¨, ^,
// and ¬, you must switch the session parameters from "037 United States" to // and ¬, you must switch the session parameters from "037 United States" to
// "1047 United States". // "1047 United States".
var currentCodepage Charset = Codepage1047()
// SetCodepage sets the codepage/character set that go3270 uses. This is a
// global setting, so if you're expecting clients to be configured to use a
// character set other than go3270's default, cp1047, you should probably set
// this during your application initialization and then leave it unchanged
// after. This is _not_ a per-connection setting.
func SetCodepage(cs Charset) {
currentCodepage = cs
}
// Internal implementation of the Charset interface we'll use for the codepage
// support we provide.
type charset struct {
// EBCDIC byte to Unicode code point for bytes 0x00-0xFF
e2u []rune
// Unicode code point to EBCDIC byte for codepoints 0x00-0xFF
u2e []byte
// Map of Unicode code points to EBCDIC bytes for codepoints >0xFF
highu2e map[rune]byte
// The EBCDIC substitute character to use if there is no EBCDIC character
// for the requested Unicode code point (typically 0x3F).
esub byte
// The "graphic escape" EBCDIC byte (is it ever anything other than 0x0E?)
ge byte
// Graphic escape codepage EBCDIC byte to Unicode code point for bytes
// 0x00-0xFF. Use rune '�' for unmapped bytes.
ge2u []rune
// Map of Unicode code points to graphic escape EBCDIC bytes.
u2ge map[rune]byte
}
// IBM CP 1047 <-> Unicode mappings from: // IBM CP 1047 <-> Unicode mappings from:
// https://raw.githubusercontent.com/unicode-org/icu-data/refs/heads/main/charset/data/ucm/glibc-IBM1047-2.1.2.ucm // https://raw.githubusercontent.com/unicode-org/icu-data/refs/heads/main/charset/data/ucm/glibc-IBM1047-2.1.2.ucm
func Codepage1047() Charset {
var cp1047ToUnicode = []rune{ return &charset{
e2u: []rune{
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
/* 0x */ 0x00, 0x01, 0x02, 0x03, 0x9C, 0x09, 0x86, 0x7F, 0x97, 0x8D, 0x8E, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, /* 0x */ 0x00, 0x01, 0x02, 0x03, 0x9C, 0x09, 0x86, 0x7F, 0x97, 0x8D, 0x8E, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
/* 1x */ 0x10, 0x11, 0x12, 0x13, 0x9D, 0x85, 0x08, 0x87, 0x18, 0x19, 0x92, 0x8F, 0x1C, 0x1D, 0x1E, 0x1F, /* 1x */ 0x10, 0x11, 0x12, 0x13, 0x9D, 0x85, 0x08, 0x87, 0x18, 0x19, 0x92, 0x8F, 0x1C, 0x1D, 0x1E, 0x1F,
@@ -50,9 +98,8 @@ var cp1047ToUnicode = []rune{
/* Dx */ 0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0xB9, 0xFB, 0xFC, 0xF9, 0xFA, 0xFF, /* Dx */ 0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0xB9, 0xFB, 0xFC, 0xF9, 0xFA, 0xFF,
/* Ex */ 0x5C, 0xF7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0xB2, 0xD4, 0xD6, 0xD2, 0xD3, 0xD5, /* Ex */ 0x5C, 0xF7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0xB2, 0xD4, 0xD6, 0xD2, 0xD3, 0xD5,
/* Fx */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0xB3, 0xDB, 0xDC, 0xD9, 0xDA, 0x9F, /* Fx */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0xB3, 0xDB, 0xDC, 0xD9, 0xDA, 0x9F,
} },
u2e: []byte{
var unicodeToCP1047 = []byte{
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
/* 0x */ 0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F, 0x16, 0x05, 0x25, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, /* 0x */ 0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F, 0x16, 0x05, 0x25, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
/* 1x */ 0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26, 0x18, 0x19, 0x3F, 0x27, 0x1C, 0x1D, 0x1E, 0x1F, /* 1x */ 0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26, 0x18, 0x19, 0x3F, 0x27, 0x1C, 0x1D, 0x1E, 0x1F,
@@ -70,12 +117,22 @@ var unicodeToCP1047 = []byte{
/* Dx */ 0xAC, 0x69, 0xED, 0xEE, 0xEB, 0xEF, 0xEC, 0xBF, 0x80, 0xFD, 0xFE, 0xFB, 0xFC, 0xBA, 0xAE, 0x59, /* Dx */ 0xAC, 0x69, 0xED, 0xEE, 0xEB, 0xEF, 0xEC, 0xBF, 0x80, 0xFD, 0xFE, 0xFB, 0xFC, 0xBA, 0xAE, 0x59,
/* Ex */ 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9C, 0x48, 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, /* Ex */ 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9C, 0x48, 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57,
/* Fx */ 0x8C, 0x49, 0xCD, 0xCE, 0xCB, 0xCF, 0xCC, 0xE1, 0x70, 0xDD, 0xDE, 0xDB, 0xDC, 0x8D, 0x8E, 0xDF, /* Fx */ 0x8C, 0x49, 0xCD, 0xCE, 0xCB, 0xCF, 0xCC, 0xE1, 0x70, 0xDD, 0xDE, 0xDB, 0xDC, 0x8D, 0x8E, 0xDF,
},
highu2e: map[rune]byte{},
esub: 0x3f,
ge: 0x08,
ge2u: cp310ToUnicode,
u2ge: unicodeToCP310,
}
} }
// Furthermore, certain characters are supported in the "graphic escape" // Certain characters are supported in the "graphic escape" CP310. These are
// CP310. These are arbitrary Unicode code points, so we will look them up via // arbitrary Unicode code points, so we will look them up via a map. For
// a map. For simplicity of our mapping implementation, we will not support the // simplicity of our mapping implementation, we will not support the italic
// italic underlined A-Z characters that require combining characters. // underlined A-Z characters that require combining characters.
//
// We will share this map among all of the codepages that we provide
// implementations for.
// //
// https://public.dhe.ibm.com/software/globalization/gcoc/attachments/CP00310.pdf // https://public.dhe.ibm.com/software/globalization/gcoc/attachments/CP00310.pdf
var unicodeToCP310 = map[rune]byte{ var unicodeToCP310 = map[rune]byte{
@@ -127,25 +184,25 @@ var cp310ToUnicode = []rune{
// decode will convert a CP1047 byte array into a UTF-8 Go string, handling // decode will convert a CP1047 byte array into a UTF-8 Go string, handling
// graphic escape to CP310. // graphic escape to CP310.
func decode(b []byte) string { func (cp *charset) Decode(b []byte) string {
runes := make([]rune, 0, len(b)) runes := make([]rune, 0, len(b))
var escape bool var escape bool
for i := range b { for i := range b {
if escape { if escape {
escape = false escape = false
if cp310ToUnicode[b[i]] != '�' { if cp.ge2u[b[i]] != '�' {
runes = append(runes, cp310ToUnicode[b[i]]) runes = append(runes, cp.ge2u[b[i]])
} else { } else {
runes = append(runes, 0x1A) // Unicode substitute runes = append(runes, 0x1A) // Unicode substitute
} }
} else { } else {
// Enter graphic escape mode if necessary. // Enter graphic escape mode if necessary.
if b[i] == 0x08 { if b[i] == cp.ge {
escape = true escape = true
continue continue
} }
// Otherwise perform the mapping. // Otherwise perform the mapping.
runes = append(runes, cp1047ToUnicode[b[i]]) runes = append(runes, cp.e2u[b[i]])
} }
} }
@@ -153,7 +210,7 @@ func decode(b []byte) string {
} }
// encode will convert a UTF-8 Go string into a CP1047 byte array. // encode will convert a UTF-8 Go string into a CP1047 byte array.
func encode(s string) []byte { func (cp *charset) Encode(s string) []byte {
out := make([]byte, 0, len(s)) out := make([]byte, 0, len(s))
for len(s) > 0 { for len(s) > 0 {
@@ -163,14 +220,14 @@ func encode(s string) []byte {
break break
} }
if int(r) < len(unicodeToCP1047) { if int(r) < len(cp.u2e) {
out = append(out, unicodeToCP1047[r]) out = append(out, cp.u2e[r])
} else if v, ok := unicodeToCP310[r]; ok { } else if v, ok := cp.u2ge[r]; ok {
// include graphic escape character to switch to CP310 // include graphic escape character to switch to CP310
out = append(out, 0x08, v) out = append(out, cp.ge, v)
} else { } else {
// replacement/substitute character // replacement/substitute character
out = append(out, 0x3f) out = append(out, cp.esub)
} }
s = s[size:] s = s[size:]
} }
+2 -2
View File
@@ -160,7 +160,7 @@ func readFields(c net.Conn, fm fieldmap, cols int) (map[string]string, error) {
if eor { if eor {
// Finish the current field // Finish the current field
if infield { if infield {
value := decode(fieldval.Bytes()) value := currentCodepage.Decode(fieldval.Bytes())
debugf("Field %d: %s\n", fieldpos, value) debugf("Field %d: %s\n", fieldpos, value)
handleField(fieldpos, value, fm, values) handleField(fieldpos, value, fm, values)
} }
@@ -172,7 +172,7 @@ func readFields(c net.Conn, fm fieldmap, cols int) (map[string]string, error) {
if b == 0x11 { if b == 0x11 {
// Finish the previous field, if necessary // Finish the previous field, if necessary
if infield { if infield {
value := decode(fieldval.Bytes()) value := currentCodepage.Decode(fieldval.Bytes())
debugf("Field %d: %s\n", fieldpos, value) debugf("Field %d: %s\n", fieldpos, value)
handleField(fieldpos, value, fm, values) handleField(fieldpos, value, fm, values)
} }
+1 -1
View File
@@ -286,7 +286,7 @@ func showScreenInternal(screen Screen, values map[string]string,
} }
} }
if content != "" { if content != "" {
b.Write(encode(content)) b.Write(currentCodepage.Encode(content))
} }
// If a writable field, add it to the field map. We add 1 to bufaddr // If a writable field, add it to the field map. We add 1 to bufaddr