Files
smpp-tester/encoding/gsm7.go
PhatPhuckDave 7f9717266f
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Has been cancelled
Run Tests / Test (push) Has been cancelled
Implement GSM7 packing
2024-07-28 13:00:34 +02:00

54 lines
971 B
Go

package encoding
import (
"bytes"
"fmt"
)
type GSM7Coder struct{}
var masks = []byte{
0b00000000,
0b00000001,
0b00000011,
0b00000111,
0b00001111,
0b00011111,
0b00111111,
0b01111111,
0b11111111,
}
func (c *GSM7Coder) Encode(s string, buf *bytes.Buffer) error {
// utf8 := *(*[]byte)(unsafe.Pointer(&s))
utf8 := []byte(s)
var offset byte = 1
var bitshift byte = 1
for index, septet := range utf8 {
if septet > 0b01111111 {
return fmt.Errorf("invalid character at index %d", index)
}
bindex := byte(index)
if bindex == 0 {
continue
}
mask := masks[bitshift]
masked := (mask & septet) << (8 - bitshift)
utf8[bindex-offset] |= masked
utf8[bindex] >>= bitshift
buf.WriteByte(utf8[bindex-offset])
bitshift++
if utf8[bindex] == 0 {
offset++
bitshift = 1
}
}
return nil
}
func (c *GSM7Coder) Decode(buf *bytes.Buffer) (string, error) {
return buf.String(), nil
}