Skip to content

Commit

Permalink
Added support for Korean & Chinese code pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Ne0nd0g committed Dec 15, 2023
1 parent 2b02f8c commit 4c60e45
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions os/windows/pkg/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
// X Packages
"golang.org/x/sys/windows"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/korean"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/encoding/traditionalchinese"
"golang.org/x/text/transform"
)

Expand All @@ -51,6 +54,30 @@ func DecodeString(encoded []byte) (decoded string, err error) {
return
}
decoded = string(t)
// 936 is the default code page for Simplified Chinese
case 936:
t, e := io.ReadAll(transform.NewReader(bytes.NewReader(encoded), simplifiedchinese.GBK.NewDecoder()))
if e != nil {
err = fmt.Errorf("os/windows/pkg/text.DecodeString(): there was an error decoding the string to Simplified Chinese GBK: %s", e)
return
}
decoded = string(t)
// 949 is the default code page for Korean
case 949:
t, e := io.ReadAll(transform.NewReader(bytes.NewReader(encoded), korean.EUCKR.NewDecoder()))
if e != nil {
err = fmt.Errorf("os/windows/pkg/text.DecodeString(): there was an error decoding the string to Korean EUCKR: %s", e)
return
}
decoded = string(t)
// 950 is the default code page for Traditional Chinese
case 950:
t, e := io.ReadAll(transform.NewReader(bytes.NewReader(encoded), traditionalchinese.Big5.NewDecoder()))
if e != nil {
err = fmt.Errorf("os/windows/pkg/text.DecodeString(): there was an error decoding the string to Traditional Chinese Big5: %s", e)
return
}
decoded = string(t)
default:
decoded = fmt.Sprintf("\n***The output was not valid UTF-8 and there isn't a configured decoder for code page %d***\n\n", codePage)
decoded += string(bytes.ToValidUTF8(encoded, []byte("�")))
Expand Down

0 comments on commit 4c60e45

Please sign in to comment.