Compare commits
7 Commits
e6199da5d6
...
b2a588e667
Author | SHA1 | Date | |
---|---|---|---|
b2a588e667 | |||
db61da9fc2 | |||
9fbcdd86cc | |||
76d7102e72 | |||
74928d9f7a | |||
a6339587d4 | |||
48d28def47 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
jstester/node_modules
|
||||
encoding/tmp
|
||||
|
51
encoding/.air.toml
Normal file
51
encoding/.air.toml
Normal file
@@ -0,0 +1,51 @@
|
||||
root = "."
|
||||
testdata_dir = "testdata"
|
||||
tmp_dir = "tmp"
|
||||
|
||||
[build]
|
||||
args_bin = []
|
||||
bin = "tmp\\main.exe"
|
||||
cmd = "go test ."
|
||||
delay = 1000
|
||||
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
|
||||
exclude_file = []
|
||||
exclude_regex = []
|
||||
exclude_unchanged = false
|
||||
follow_symlink = false
|
||||
full_bin = ""
|
||||
include_dir = []
|
||||
include_ext = ["go", "tpl", "tmpl", "html"]
|
||||
include_file = []
|
||||
kill_delay = "0s"
|
||||
log = "build-errors.log"
|
||||
poll = false
|
||||
poll_interval = 0
|
||||
post_cmd = []
|
||||
pre_cmd = []
|
||||
rerun = false
|
||||
rerun_delay = 100
|
||||
send_interrupt = false
|
||||
stop_on_error = false
|
||||
|
||||
[color]
|
||||
app = ""
|
||||
build = "yellow"
|
||||
main = "magenta"
|
||||
runner = "green"
|
||||
watcher = "cyan"
|
||||
|
||||
[log]
|
||||
main_only = false
|
||||
time = false
|
||||
|
||||
[misc]
|
||||
clean_on_exit = true
|
||||
|
||||
[proxy]
|
||||
app_port = 0
|
||||
enabled = false
|
||||
proxy_port = 0
|
||||
|
||||
[screen]
|
||||
clear_on_rebuild = true
|
||||
keep_scroll = true
|
103
encoding/gsm7.go
103
encoding/gsm7.go
@@ -3,16 +3,23 @@ package encoding
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
type GSM7Coder struct{}
|
||||
|
||||
// Make sure buffer can fit EncodesInto bytes
|
||||
// Otherwise Encode will allocate memory as it sees fit
|
||||
// Which is fine but not optimal
|
||||
// Preallocate the buffer with the size of EncodesInto bytes
|
||||
func (c *GSM7Coder) Encode(s string, buf *bytes.Buffer) error {
|
||||
// utf8 := *(*[]byte)(unsafe.Pointer(&s))
|
||||
utf8 := []byte(s)
|
||||
var offset int = 1
|
||||
var bitshift byte = 0
|
||||
var leap, shift bool
|
||||
var (
|
||||
offset int = 1
|
||||
bitshift byte = 0
|
||||
leap, shift bool
|
||||
)
|
||||
|
||||
for index, septet := range utf8 {
|
||||
if septet > 0b01111111 {
|
||||
@@ -56,9 +63,97 @@ func (c *GSM7Coder) Encode(s string, buf *bytes.Buffer) error {
|
||||
offset = 1
|
||||
}
|
||||
}
|
||||
log.Println(buf.Cap(), buf.Len())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *GSM7Coder) Decode(buf *bytes.Buffer) (string, error) {
|
||||
return buf.String(), nil
|
||||
gsm7 := buf.Bytes()
|
||||
var (
|
||||
offset int = (len(gsm7) / 8) + 1
|
||||
bitshift byte = 0
|
||||
leap, shift bool
|
||||
)
|
||||
outLength := DecodesInto(buf)
|
||||
lengthDiff := outLength - len(gsm7)
|
||||
gsm7 = append(gsm7, make([]byte, lengthDiff)...)
|
||||
|
||||
// We don't care about the last byte
|
||||
// Unless it's the %8.......
|
||||
// We'll deal with that later
|
||||
for index := len(gsm7) - 2; index >= 0; index-- {
|
||||
octet := gsm7[index]
|
||||
bitshift = byte((index + 1) % 8)
|
||||
if bitshift == 7 {
|
||||
leap = true
|
||||
}
|
||||
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d CurrentByte:%08b (%-3d) OffsetByte(%-3d):%08b (%-3d) Leap:%5v", index, offset, bitshift, gsm7[index], gsm7[index], index+offset, gsm7[index+offset], gsm7[index+offset], leap)
|
||||
|
||||
mask := byte(255 << (8 - bitshift))
|
||||
masked := (mask & octet) >> (8 - bitshift)
|
||||
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d Mask:%08b Masked:%08b", index, offset, bitshift, mask, masked)
|
||||
if leap {
|
||||
InsertAt(&gsm7, index+offset, masked)
|
||||
} else {
|
||||
gsm7[index+offset] |= masked
|
||||
}
|
||||
// Remove last bitshift bits
|
||||
gsm7[index] <<= bitshift
|
||||
// Move the remaining bit once to the right to form septet instead of octet
|
||||
gsm7[index] >>= 1
|
||||
|
||||
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d CurrentByte:%08b (%-3d) OffsetByte(%-3d):%08b (%-3d) Leap:%5v", index, offset, bitshift, gsm7[index], gsm7[index], index+offset, gsm7[index+offset], gsm7[index+offset], leap)
|
||||
leap = false
|
||||
continue
|
||||
|
||||
if !leap {
|
||||
buf.WriteByte(gsm7[index+offset])
|
||||
}
|
||||
if index == len(gsm7)-1 && gsm7[index] > 0 {
|
||||
buf.WriteByte(gsm7[index])
|
||||
}
|
||||
// log.Printf("Index:%-3d Offset:%-3d Bitshift:%-3d CurrentByte:%08b (%-3d) OffsetByte:%08b (%-3d) Leap:%5v", index, offset, bitshift, gsm7[index], gsm7[index], gsm7[index-offset], gsm7[index-offset], leap)
|
||||
if bitshift >= 7 {
|
||||
if leap {
|
||||
// log.Printf("Shift at Index:%-3d Offset:%-3d Bitshift:%-3d", index, offset, bitshift)
|
||||
leap = false
|
||||
bitshift = 0
|
||||
offset++
|
||||
shift = true
|
||||
continue
|
||||
}
|
||||
// log.Printf("Leap at Index:%-3d Offset:%-3d Bitshift:%-3d", index, offset, bitshift)
|
||||
leap = true
|
||||
bitshift = 6
|
||||
}
|
||||
if shift {
|
||||
offset = 1
|
||||
}
|
||||
}
|
||||
// log.Printf("Result: %+v", gsm7)
|
||||
// for _, v := range gsm7 {
|
||||
// log.Printf("%08b", v)
|
||||
// }
|
||||
return string(gsm7), nil
|
||||
}
|
||||
|
||||
// Allocation free
|
||||
// Which means data MUST have space for value
|
||||
func InsertAt(data *[]byte, index int, value byte) {
|
||||
copy((*data)[index+1:], (*data)[index:])
|
||||
(*data)[index] = value
|
||||
}
|
||||
|
||||
func EncodesInto(s *string) int {
|
||||
slen := len(*s)
|
||||
enclen := slen * 7 / 8
|
||||
if slen%8 != 0 {
|
||||
enclen++
|
||||
}
|
||||
return enclen
|
||||
}
|
||||
func DecodesInto(buf *bytes.Buffer) int {
|
||||
blen := buf.Len()
|
||||
declen := blen * 8 / 7
|
||||
return declen
|
||||
}
|
||||
|
@@ -93,54 +93,217 @@ func TestGSM7DecodeSimpleASCIIString(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7DecodeComplexASCIIString(t *testing.T) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
// func TestGSM7DecodeComplexASCIIString(t *testing.T) {
|
||||
// coder := &GSM7Coder{}
|
||||
// var buf bytes.Buffer
|
||||
// input := []byte{0b11000111, 0b00110111, 0b10011011, 0b01011100, 0b01110110, 0b10000011, 0b11100100, 0b11100001, 0b11111100, 0b00011100, 0b00000100, 0b01100111, 0b10000111, 0b11110011, 0b00101100, 0b11010000, 0b00010000, 0b00011101, 0b10011110, 0b10100111, 0b11011101, 0b01100111, 0b10010000, 0b00111011, 0b01111101, 0b01000110, 0b11010011, 0b01000001, 0b11100001, 0b01111011, 0b00111000, 0b11101111, 0b00000010}
|
||||
|
||||
// expected := "Golden rays play, Chasing night away."
|
||||
// buf.Write(input)
|
||||
// actual, err := coder.Decode(&buf)
|
||||
|
||||
// if err != nil {
|
||||
// t.Errorf("Expected no error, but got %v", err)
|
||||
// }
|
||||
|
||||
// if actual != expected {
|
||||
// t.Errorf("Expected '%v', but got '%v'", expected, actual)
|
||||
// }
|
||||
// }
|
||||
|
||||
// func TestGSM7DecodeComplex8nASCIIString(t *testing.T) {
|
||||
// coder := &GSM7Coder{}
|
||||
// var buf bytes.Buffer
|
||||
// input := []byte{0b11000100, 0b11111010, 0b01111000, 0b00111101, 0b00000111, 0b10000101, 0b11100101, 0b01100101, 0b10010000, 0b10111001, 0b00111110, 0b01011110, 0b10100111, 0b11011101, 0b01100111, 0b11010000, 0b01011001, 0b01011110, 0b00001110, 0b11010011, 0b01011001, 0b00100000, 0b00111010, 0b10111010, 0b10011100, 0b00000111, 0b11000101, 0b11101011, 0b11100001, 0b11110001, 0b01111010, 0b11001110, 0b00000010, 0b00111101, 0b01000001, 0b11110001, 0b01111010, 0b01111000, 0b10111100, 0b00101110, 0b11001011, 0b11100111, 0b00101100, 0b11010000, 0b00011101, 0b00011101, 0b10100110, 0b10000011, 0b11101000, 0b11101000, 0b00110010, 0b11001000, 0b01011100, 0b00011111, 0b10101111, 0b01011101}
|
||||
|
||||
// expected := "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
||||
// buf.Write(input)
|
||||
// actual, err := coder.Decode(&buf)
|
||||
|
||||
// if err != nil {
|
||||
// t.Errorf("Expected no error, but got %v", err)
|
||||
// }
|
||||
|
||||
// if actual != expected {
|
||||
// t.Errorf("Expected '%v', but got '%v'", expected, actual)
|
||||
// }
|
||||
// }
|
||||
|
||||
// func TestGSM7DecodeEmptyString(t *testing.T) {
|
||||
// coder := &GSM7Coder{}
|
||||
// buf := bytes.NewBuffer([]byte{})
|
||||
|
||||
// expected := ""
|
||||
// actual, err := coder.Decode(buf)
|
||||
|
||||
// if err != nil {
|
||||
// t.Errorf("Expected no error, but got %v", err)
|
||||
// }
|
||||
|
||||
// if actual != expected {
|
||||
// t.Errorf("Expected '%v', but got '%v'", expected, actual)
|
||||
// }
|
||||
// }
|
||||
|
||||
// region insertat
|
||||
func TestInsertAtBeginning(t *testing.T) {
|
||||
data := []byte{2, 3, 4, 0}
|
||||
InsertAt(&data, 0, 1)
|
||||
expected := []byte{1, 2, 3, 4}
|
||||
if !bytes.Equal(data, expected) {
|
||||
t.Errorf("expected %v, got %v", expected, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertInMiddle(t *testing.T) {
|
||||
data := []byte{2, 3, 4, 0}
|
||||
InsertAt(&data, 1, 5)
|
||||
expected := []byte{2, 5, 3, 4}
|
||||
if !bytes.Equal(data, expected) {
|
||||
t.Errorf("expected %v, got %v", expected, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertAtEnd(t *testing.T) {
|
||||
data := []byte{1, 2, 3, 0}
|
||||
InsertAt(&data, 3, 4)
|
||||
expected := []byte{1, 2, 3, 4}
|
||||
if !bytes.Equal(data, expected) {
|
||||
t.Errorf("expected %v, got %v", expected, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexOutOfBounds(t *testing.T) {
|
||||
data := []byte{2, 3, 4}
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Errorf("The code did not panic")
|
||||
}
|
||||
}()
|
||||
InsertAt(&data, 4, 5)
|
||||
}
|
||||
|
||||
func TestNegativeIndex(t *testing.T) {
|
||||
data := []byte{2, 3, 4}
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Errorf("The code did not panic")
|
||||
}
|
||||
}()
|
||||
InsertAt(&data, -1, 1)
|
||||
}
|
||||
|
||||
func TestMaintainsOrderAfterInsertion(t *testing.T) {
|
||||
data := []byte{2, 3, 4, 0}
|
||||
InsertAt(&data, 1, 1)
|
||||
expected := []byte{2, 1, 3, 4}
|
||||
if !bytes.Equal(data, expected) {
|
||||
t.Errorf("expected %v, got %v", expected, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaintainsLength(t *testing.T) {
|
||||
data := []byte{2, 3, 4, 0}
|
||||
InsertAt(&data, 1, 1)
|
||||
if len(data) != 4 {
|
||||
t.Errorf("expected length 4, got %d", len(data))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeletesLastValue(t *testing.T) {
|
||||
data := []byte{2, 3, 4, 5}
|
||||
InsertAt(&data, 0, 1)
|
||||
expected := []byte{1, 2, 3, 4}
|
||||
if !bytes.Equal(data, expected) {
|
||||
t.Errorf("expected %v, got %v", expected, data)
|
||||
}
|
||||
}
|
||||
|
||||
// region misc tests
|
||||
func TestGSM7EncodesIntoSmallString(t *testing.T) {
|
||||
input := "Sunshine"
|
||||
expected := 7
|
||||
actual := EncodesInto(&input)
|
||||
if actual != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) {
|
||||
input := "Golden rays play, Chasing night away."
|
||||
expected := 33
|
||||
actual := EncodesInto(&input)
|
||||
if actual != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7EncodesIntoLarger8nString(t *testing.T) {
|
||||
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
||||
expected := 56
|
||||
actual := EncodesInto(&input)
|
||||
if actual != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7DecodesIntoSmallString(t *testing.T) {
|
||||
input := []byte{0b11010011, 0b10111010, 0b01111011, 0b10001110, 0b01001110, 0b10111011, 0b11001011}
|
||||
|
||||
expected := "Golden rays play, Chasing night away."
|
||||
buf.Write(input)
|
||||
actual, err := coder.Decode(&buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
expected := 8
|
||||
actual := DecodesInto(bytes.NewBuffer(input))
|
||||
if actual != expected {
|
||||
t.Errorf("Expected '%v', but got '%v'", expected, actual)
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7DecodeComplex8nASCIIString(t *testing.T) {
|
||||
func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) {
|
||||
input := []byte{0b11000111, 0b00110111, 0b10011011, 0b01011100, 0b01110110, 0b10000011, 0b11100100, 0b11100001, 0b11111100, 0b00011100, 0b00000100, 0b01100111, 0b10000111, 0b11110011, 0b00101100, 0b11010000, 0b00010000, 0b00011101, 0b10011110, 0b10100111, 0b11011101, 0b01100111, 0b10010000, 0b00111011, 0b01111101, 0b01000110, 0b11010011, 0b01000001, 0b11100001, 0b01111011, 0b00111000, 0b11101111, 0b00000010}
|
||||
expected := 37
|
||||
actual := DecodesInto(bytes.NewBuffer(input))
|
||||
if actual != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7DecodesIntoLarger8nString(t *testing.T) {
|
||||
input := []byte{0b11000100, 0b11111010, 0b01111000, 0b00111101, 0b00000111, 0b10000101, 0b11100101, 0b01100101, 0b10010000, 0b10111001, 0b00111110, 0b01011110, 0b10100111, 0b11011101, 0b01100111, 0b11010000, 0b01011001, 0b01011110, 0b00001110, 0b11010011, 0b01011001, 0b00100000, 0b00111010, 0b10111010, 0b10011100, 0b00000111, 0b11000101, 0b11101011, 0b11100001, 0b11110001, 0b01111010, 0b11001110, 0b00000010, 0b00111101, 0b01000001, 0b11110001, 0b01111010, 0b01111000, 0b10111100, 0b00101110, 0b11001011, 0b11100111, 0b00101100, 0b11010000, 0b00011101, 0b00011101, 0b10100110, 0b10000011, 0b11101000, 0b11101000, 0b00110010, 0b11001000, 0b01011100, 0b00011111, 0b10101111, 0b01011101}
|
||||
expected := 64
|
||||
actual := DecodesInto(bytes.NewBuffer(input))
|
||||
if actual != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
// region benchmark
|
||||
func BenchmarkGSM7EncodeSimpleASCIIString(b *testing.B) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := []byte{0b11000100, 0b11111010, 0b01111000, 0b00111101, 0b00000111, 0b10000101, 0b11100101, 0b01100101, 0b10010000, 0b10111001, 0b00111110, 0b01011110, 0b10100111, 0b11011101, 0b01100111, 0b11010000, 0b01011001, 0b01011110, 0b00001110, 0b11010011, 0b01011001, 0b00100000, 0b00111010, 0b10111010, 0b10011100, 0b00000111, 0b11000101, 0b11101011, 0b11100001, 0b11110001, 0b01111010, 0b11001110, 0b00000010, 0b00111101, 0b01000001, 0b11110001, 0b01111010, 0b01111000, 0b10111100, 0b00101110, 0b11001011, 0b11100111, 0b00101100, 0b11010000, 0b00011101, 0b00011101, 0b10100110, 0b10000011, 0b11101000, 0b11101000, 0b00110010, 0b11001000, 0b01011100, 0b00011111, 0b10101111, 0b01011101}
|
||||
input := "Sunshine"
|
||||
b.ResetTimer()
|
||||
|
||||
expected := "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
||||
buf.Write(input)
|
||||
actual, err := coder.Decode(&buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Errorf("Expected '%v', but got '%v'", expected, actual)
|
||||
for i := 0; i < b.N; i++ {
|
||||
coder.Encode(input, &buf)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGSM7DecodeEmptyString(t *testing.T) {
|
||||
func BenchmarkGSM7EncodeComplexASCIIString(b *testing.B) {
|
||||
coder := &GSM7Coder{}
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
var buf bytes.Buffer
|
||||
input := "Golden rays play, Chasing night away."
|
||||
b.ResetTimer()
|
||||
|
||||
expected := ""
|
||||
actual, err := coder.Decode(buf)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, but got %v", err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Errorf("Expected '%v', but got '%v'", expected, actual)
|
||||
for i := 0; i < b.N; i++ {
|
||||
coder.Encode(input, &buf)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGSM7EncodeComplex8nASCIIString(b *testing.B) {
|
||||
coder := &GSM7Coder{}
|
||||
var buf bytes.Buffer
|
||||
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
coder.Encode(input, &buf)
|
||||
}
|
||||
}
|
||||
|
@@ -78,6 +78,7 @@ func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
|
||||
// UCS2 should also be fairly easy, use uint16 or something
|
||||
// GSM7 will not be easy
|
||||
// See https://www.codeproject.com/Tips/470755/Encoding-Decoding-7-bit-User-Data-for-SMS-PDU-PDU
|
||||
// Another great site: https://doubleblak.com/blogPost.php?k=7bitpdu
|
||||
asciiEncoder := ascii85.NewEncoder(buf)
|
||||
// TODO: Implement encodings bsed on p.data_coding
|
||||
messageEncoder := gsm7.NewEncoder()
|
||||
|
Reference in New Issue
Block a user