|
|
@ -2,8 +2,11 @@ package sds
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"golang.org/x/text/encoding"
|
|
|
|
|
|
|
|
"golang.org/x/text/encoding/charmap"
|
|
|
|
|
|
|
|
"golang.org/x/text/encoding/unicode"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
/* Text related types and functions */
|
|
|
|
/* Text related types and functions */
|
|
|
@ -11,12 +14,67 @@ import (
|
|
|
|
// TextEncoding enum according to [AI] 29.5.4.1
|
|
|
|
// TextEncoding enum according to [AI] 29.5.4.1
|
|
|
|
type TextEncoding byte
|
|
|
|
type TextEncoding byte
|
|
|
|
|
|
|
|
|
|
|
|
// All supported text encoding schemes, according to [AI] table 29.29
|
|
|
|
// All defined text encoding schemes, according to [AI] table 29.29
|
|
|
|
const (
|
|
|
|
const (
|
|
|
|
Packed7Bit TextEncoding = 0
|
|
|
|
Packed7Bit TextEncoding = iota
|
|
|
|
ISO8859_1 TextEncoding = 1
|
|
|
|
ISO8859_1
|
|
|
|
|
|
|
|
ISO8859_2
|
|
|
|
|
|
|
|
ISO8859_3
|
|
|
|
|
|
|
|
ISO8859_4
|
|
|
|
|
|
|
|
ISO8859_5
|
|
|
|
|
|
|
|
ISO8859_6
|
|
|
|
|
|
|
|
ISO8859_7
|
|
|
|
|
|
|
|
ISO8859_8
|
|
|
|
|
|
|
|
ISO8859_9
|
|
|
|
|
|
|
|
ISO8859_10
|
|
|
|
|
|
|
|
ISO8859_13
|
|
|
|
|
|
|
|
ISO8859_14
|
|
|
|
|
|
|
|
ISO8859_15
|
|
|
|
|
|
|
|
CodePage437
|
|
|
|
|
|
|
|
CodePage737
|
|
|
|
|
|
|
|
CodePage850
|
|
|
|
|
|
|
|
CodePage852
|
|
|
|
|
|
|
|
CodePage855
|
|
|
|
|
|
|
|
CodePage857
|
|
|
|
|
|
|
|
CodePage860
|
|
|
|
|
|
|
|
CodePage861
|
|
|
|
|
|
|
|
CodePage863
|
|
|
|
|
|
|
|
CodePage865
|
|
|
|
|
|
|
|
CodePage866
|
|
|
|
|
|
|
|
CodePage869
|
|
|
|
|
|
|
|
UTF16BE
|
|
|
|
|
|
|
|
VISCII
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TextCodecs contains encoding.Encoding instances for all supported text encoding schemes.
|
|
|
|
|
|
|
|
// Beware that not all defined schemes are actually supported here.
|
|
|
|
|
|
|
|
var TextCodecs = map[TextEncoding]encoding.Encoding{
|
|
|
|
|
|
|
|
ISO8859_1: charmap.ISO8859_1,
|
|
|
|
|
|
|
|
ISO8859_2: charmap.ISO8859_2,
|
|
|
|
|
|
|
|
ISO8859_3: charmap.ISO8859_3,
|
|
|
|
|
|
|
|
ISO8859_4: charmap.ISO8859_4,
|
|
|
|
|
|
|
|
ISO8859_5: charmap.ISO8859_5,
|
|
|
|
|
|
|
|
ISO8859_6: charmap.ISO8859_6,
|
|
|
|
|
|
|
|
ISO8859_7: charmap.ISO8859_7,
|
|
|
|
|
|
|
|
ISO8859_8: charmap.ISO8859_8,
|
|
|
|
|
|
|
|
ISO8859_9: charmap.ISO8859_9,
|
|
|
|
|
|
|
|
ISO8859_10: charmap.ISO8859_10,
|
|
|
|
|
|
|
|
ISO8859_13: charmap.ISO8859_13,
|
|
|
|
|
|
|
|
ISO8859_14: charmap.ISO8859_14,
|
|
|
|
|
|
|
|
ISO8859_15: charmap.ISO8859_15,
|
|
|
|
|
|
|
|
CodePage437: charmap.CodePage437,
|
|
|
|
|
|
|
|
CodePage850: charmap.CodePage850,
|
|
|
|
|
|
|
|
CodePage852: charmap.CodePage852,
|
|
|
|
|
|
|
|
CodePage855: charmap.CodePage855,
|
|
|
|
|
|
|
|
CodePage860: charmap.CodePage860,
|
|
|
|
|
|
|
|
CodePage863: charmap.CodePage863,
|
|
|
|
|
|
|
|
CodePage865: charmap.CodePage865,
|
|
|
|
|
|
|
|
CodePage866: charmap.CodePage866,
|
|
|
|
|
|
|
|
UTF16BE: unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var fallbackCodec encoding.Encoding = charmap.ISO8859_1 // be lenient and use ISO8859-1 as fallback if anything goes havoc
|
|
|
|
|
|
|
|
|
|
|
|
// TextBytes returns the length in bytes of an encoded text with
|
|
|
|
// TextBytes returns the length in bytes of an encoded text with
|
|
|
|
// the given number of characters and the given encoding
|
|
|
|
// the given number of characters and the given encoding
|
|
|
|
func TextBytes(encoding TextEncoding, length int) int {
|
|
|
|
func TextBytes(encoding TextEncoding, length int) int {
|
|
|
@ -95,39 +153,40 @@ func (h TextHeader) Length() int {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DecodePayloadText decodes the actual text content using the given encoding scheme according to [AI] 29.5.4
|
|
|
|
// DecodePayloadText decodes the actual text content using the given encoding scheme according to [AI] 29.5.4
|
|
|
|
func DecodePayloadText(encoding TextEncoding, bytes []byte) (string, error) {
|
|
|
|
func DecodePayloadText(textEncoding TextEncoding, bytes []byte) (string, error) {
|
|
|
|
switch encoding {
|
|
|
|
var decoder *encoding.Decoder
|
|
|
|
case ISO8859_1: // only ISO8859-1 at the moment
|
|
|
|
codec, ok := TextCodecs[textEncoding]
|
|
|
|
return decodeISO8859_1(bytes)
|
|
|
|
if ok {
|
|
|
|
default: // be lenient and use ISO8859-1 as fallback
|
|
|
|
decoder = codec.NewDecoder()
|
|
|
|
log.Printf("encoding 0x%x is currently not supported, using ISO8859-1 as fallback", encoding)
|
|
|
|
} else { // we have no matching codec, but be lenient and use the fallback
|
|
|
|
return decodeISO8859_1(bytes)
|
|
|
|
decoder = fallbackCodec.NewDecoder()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func decodeISO8859_1(bytes []byte) (string, error) {
|
|
|
|
utf8, err := decoder.Bytes(bytes)
|
|
|
|
utf8Buf := make([]rune, len(bytes))
|
|
|
|
return string(utf8), err
|
|
|
|
for i, b := range bytes {
|
|
|
|
|
|
|
|
utf8Buf[i] = rune(b)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(utf8Buf), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func AppendEncodedPayloadText(bytes []byte, bits int, text string, encoding TextEncoding) ([]byte, int) {
|
|
|
|
// AppendEncodedPayloadText encodes the given payload text using the given text encoding and appends the result to the given byte slice.
|
|
|
|
|
|
|
|
func AppendEncodedPayloadText(bytes []byte, bits int, text string, textEncoding TextEncoding) ([]byte, int) {
|
|
|
|
var encodedBytes []byte
|
|
|
|
var encodedBytes []byte
|
|
|
|
var encodedBits int
|
|
|
|
var encodedBits int
|
|
|
|
switch encoding {
|
|
|
|
var err error
|
|
|
|
case ISO8859_1: // only ISO8859-1 at the moment
|
|
|
|
|
|
|
|
encodedBytes, encodedBits = encodeISO8859_1(text)
|
|
|
|
var encoder *encoding.Encoder
|
|
|
|
default: // be lenient and use ISO8859-1 as fallback
|
|
|
|
codec, ok := TextCodecs[textEncoding]
|
|
|
|
encodedBytes, encodedBits = encodeISO8859_1(text)
|
|
|
|
if ok {
|
|
|
|
|
|
|
|
encoder = codec.NewEncoder()
|
|
|
|
|
|
|
|
} else { // we have no matching codec, but be lenient and use the fallback
|
|
|
|
|
|
|
|
encoder = fallbackCodec.NewEncoder()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
encodedBytes, err = encoder.Bytes([]byte(text))
|
|
|
|
|
|
|
|
if err != nil { // something went wrong, but be lenient and use the fallback
|
|
|
|
|
|
|
|
encodedBytes = []byte(text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedBits = len(encodedBytes) * 8
|
|
|
|
|
|
|
|
|
|
|
|
bytes = append(bytes, encodedBytes...)
|
|
|
|
bytes = append(bytes, encodedBytes...)
|
|
|
|
bits += encodedBits
|
|
|
|
bits += encodedBits
|
|
|
|
return bytes, bits
|
|
|
|
return bytes, bits
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func encodeISO8859_1(text string) ([]byte, int) {
|
|
|
|
|
|
|
|
return []byte(text), len(text) * 8
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|