Compare commits
5 Commits
7b4fcf3de1
...
b74a7c842f
Author | SHA1 | Date | |
---|---|---|---|
b74a7c842f | |||
d477951fb4 | |||
4c92723df0 | |||
d0c868ca5c | |||
7001f2c51a |
@@ -4,11 +4,19 @@ import "bytes"
|
|||||||
|
|
||||||
type ASCIICoder struct{}
|
type ASCIICoder struct{}
|
||||||
|
|
||||||
func (c *ASCIICoder) Encode(s string, buf *bytes.Buffer) error {
|
func (c *ASCIICoder) Encode(s *string, buf *bytes.Buffer) error {
|
||||||
buf.WriteString(s)
|
// These should be ASCII but UTF8 is a superset of ASCII so hopefully this'll be fine
|
||||||
|
buf.WriteString(*s)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ASCIICoder) Decode(buf *bytes.Buffer) (string, error) {
|
func (c *ASCIICoder) Decode(buf *bytes.Buffer) (string, error) {
|
||||||
return buf.String(), nil
|
return buf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c ASCIICoder) EncodesInto(s *string) int {
|
||||||
|
return len(*s)
|
||||||
|
}
|
||||||
|
func (c ASCIICoder) DecodesInto(buf *bytes.Buffer) int {
|
||||||
|
return buf.Len()
|
||||||
|
}
|
||||||
|
@@ -11,7 +11,7 @@ func TestASCIIEncodeSimpleASCIIString(t *testing.T) {
|
|||||||
input := "Hello, World!"
|
input := "Hello, World!"
|
||||||
|
|
||||||
expected := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}
|
expected := []byte{72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33}
|
||||||
err := coder.Encode(input, &buf)
|
err := coder.Encode(&input, &buf)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, but got %v", err)
|
t.Errorf("Expected no error, but got %v", err)
|
||||||
@@ -46,7 +46,7 @@ func TestASCIIEncodeEmptyString(t *testing.T) {
|
|||||||
input := ""
|
input := ""
|
||||||
|
|
||||||
expected := []byte{}
|
expected := []byte{}
|
||||||
err := coder.Encode(input, &buf)
|
err := coder.Encode(&input, &buf)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, but got %v", err)
|
t.Errorf("Expected no error, but got %v", err)
|
||||||
|
@@ -3,6 +3,8 @@ package encoding
|
|||||||
import "bytes"
|
import "bytes"
|
||||||
|
|
||||||
type Coder interface {
|
type Coder interface {
|
||||||
Encode(s string, buf *bytes.Buffer) error
|
Encode(s *string, buf *bytes.Buffer) error
|
||||||
Decode(buf *bytes.Buffer) (string, error)
|
Decode(buf *bytes.Buffer) (string, error)
|
||||||
}
|
EncodesInto(s *string) int
|
||||||
|
DecodesInto(buf *bytes.Buffer) int
|
||||||
|
}
|
||||||
|
@@ -11,15 +11,15 @@ type GSM7Coder struct{}
|
|||||||
// Otherwise Encode will allocate memory as it sees fit
|
// Otherwise Encode will allocate memory as it sees fit
|
||||||
// Which is fine but not optimal
|
// Which is fine but not optimal
|
||||||
// Preallocate the buffer with the size of EncodesInto bytes
|
// Preallocate the buffer with the size of EncodesInto bytes
|
||||||
func (c *GSM7Coder) Encode(s string, buf *bytes.Buffer) error {
|
func (c *GSM7Coder) Encode(s *string, buf *bytes.Buffer) error {
|
||||||
// utf8 := *(*[]byte)(unsafe.Pointer(&s))
|
// utf8 := *(*[]byte)(unsafe.Pointer(&s))
|
||||||
utf8 := []byte(s)
|
utf8 := []byte(*s)
|
||||||
var (
|
var (
|
||||||
offset int = 1
|
offset int = 1
|
||||||
bitshift byte = 0
|
bitshift byte = 0
|
||||||
leap, shift bool
|
leap, shift bool
|
||||||
)
|
)
|
||||||
encodedSize := GSM7EncodesInto(&s)
|
encodedSize := c.EncodesInto(s)
|
||||||
cap := buf.Cap()
|
cap := buf.Cap()
|
||||||
if cap < encodedSize {
|
if cap < encodedSize {
|
||||||
buf.Grow(encodedSize - cap)
|
buf.Grow(encodedSize - cap)
|
||||||
@@ -77,7 +77,7 @@ func (c *GSM7Coder) Decode(buf *bytes.Buffer) (string, error) {
|
|||||||
bitshift byte = 0
|
bitshift byte = 0
|
||||||
leap bool
|
leap bool
|
||||||
)
|
)
|
||||||
outLength := GSM7DecodesInto(buf)
|
outLength := c.DecodesInto(buf)
|
||||||
lengthDiff := outLength - len(gsm7)
|
lengthDiff := outLength - len(gsm7)
|
||||||
gsm7 = append(gsm7, make([]byte, lengthDiff)...)
|
gsm7 = append(gsm7, make([]byte, lengthDiff)...)
|
||||||
start := len(gsm7) - 2
|
start := len(gsm7) - 2
|
||||||
@@ -121,7 +121,7 @@ func InsertAt(data *[]byte, index int, value byte) {
|
|||||||
(*data)[index] = value
|
(*data)[index] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func GSM7EncodesInto(s *string) int {
|
func (c GSM7Coder) EncodesInto(s *string) int {
|
||||||
slen := len(*s)
|
slen := len(*s)
|
||||||
enclen := slen * 7 / 8
|
enclen := slen * 7 / 8
|
||||||
if slen%8 != 0 {
|
if slen%8 != 0 {
|
||||||
@@ -129,7 +129,7 @@ func GSM7EncodesInto(s *string) int {
|
|||||||
}
|
}
|
||||||
return enclen
|
return enclen
|
||||||
}
|
}
|
||||||
func GSM7DecodesInto(buf *bytes.Buffer) int {
|
func (c GSM7Coder) DecodesInto(buf *bytes.Buffer) int {
|
||||||
blen := buf.Len()
|
blen := buf.Len()
|
||||||
declen := blen * 8 / 7
|
declen := blen * 8 / 7
|
||||||
return declen
|
return declen
|
||||||
|
@@ -22,7 +22,7 @@ func TestGSM7EncodeSimpleASCIIString(t *testing.T) {
|
|||||||
input := short8nString
|
input := short8nString
|
||||||
|
|
||||||
expected := short8nStringEncodedBytes
|
expected := short8nStringEncodedBytes
|
||||||
err := coder.Encode(input, &buf)
|
err := coder.Encode(&input, &buf)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, but got %v", err)
|
t.Errorf("Expected no error, but got %v", err)
|
||||||
@@ -39,7 +39,7 @@ func TestGSM7EncodeComplexASCIIString(t *testing.T) {
|
|||||||
input := longNot8nString
|
input := longNot8nString
|
||||||
|
|
||||||
expected := longNot8nStringEncodedBytes
|
expected := longNot8nStringEncodedBytes
|
||||||
err := coder.Encode(input, &buf)
|
err := coder.Encode(&input, &buf)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, but got %v", err)
|
t.Errorf("Expected no error, but got %v", err)
|
||||||
@@ -56,7 +56,7 @@ func TestGSM7EncodeComplex8nASCIIString(t *testing.T) {
|
|||||||
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
||||||
|
|
||||||
expected := long8nStringEncodedBytes
|
expected := long8nStringEncodedBytes
|
||||||
err := coder.Encode(input, &buf)
|
err := coder.Encode(&input, &buf)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, but got %v", err)
|
t.Errorf("Expected no error, but got %v", err)
|
||||||
@@ -70,11 +70,11 @@ func TestGSM7EncodeComplex8nASCIIString(t *testing.T) {
|
|||||||
func TestGSM7EncodeDoesNotAllocateMoreThanNecessary(t *testing.T) {
|
func TestGSM7EncodeDoesNotAllocateMoreThanNecessary(t *testing.T) {
|
||||||
coder := &GSM7Coder{}
|
coder := &GSM7Coder{}
|
||||||
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
input := "Ducks are fucking great, they quacks, O quackers, what the fuck."
|
||||||
buf := bytes.NewBuffer(make([]byte, GSM7EncodesInto(&input)))
|
buf := bytes.NewBuffer(make([]byte, coder.EncodesInto(&input)))
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
|
|
||||||
expected := buf.Cap()
|
expected := buf.Cap()
|
||||||
err := coder.Encode(input, buf)
|
err := coder.Encode(&input, buf)
|
||||||
actual := buf.Cap()
|
actual := buf.Cap()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -93,7 +93,7 @@ func TestGSM7EncodeDoesAllocateWhenNecessary(t *testing.T) {
|
|||||||
buf.Reset()
|
buf.Reset()
|
||||||
|
|
||||||
original := buf.Cap()
|
original := buf.Cap()
|
||||||
err := coder.Encode(input, &buf)
|
err := coder.Encode(&input, &buf)
|
||||||
modified := buf.Cap()
|
modified := buf.Cap()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -111,7 +111,7 @@ func TestGSM7EncodeEmptyString(t *testing.T) {
|
|||||||
input := ""
|
input := ""
|
||||||
|
|
||||||
expected := []byte{}
|
expected := []byte{}
|
||||||
err := coder.Encode(input, &buf)
|
err := coder.Encode(&input, &buf)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, but got %v", err)
|
t.Errorf("Expected no error, but got %v", err)
|
||||||
@@ -271,7 +271,7 @@ func TestDeletesLastValue(t *testing.T) {
|
|||||||
func TestGSM7EncodesIntoSmallString(t *testing.T) {
|
func TestGSM7EncodesIntoSmallString(t *testing.T) {
|
||||||
input := short8nString
|
input := short8nString
|
||||||
expected := 7
|
expected := 7
|
||||||
actual := GSM7EncodesInto(&input)
|
actual := GSM7Coder{}.EncodesInto(&input)
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||||
}
|
}
|
||||||
@@ -280,7 +280,7 @@ func TestGSM7EncodesIntoSmallString(t *testing.T) {
|
|||||||
func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) {
|
func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) {
|
||||||
input := longNot8nString
|
input := longNot8nString
|
||||||
expected := 33
|
expected := 33
|
||||||
actual := GSM7EncodesInto(&input)
|
actual := GSM7Coder{}.EncodesInto(&input)
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||||
}
|
}
|
||||||
@@ -289,7 +289,7 @@ func TestGSM7EncodesIntoLargerNot8nString(t *testing.T) {
|
|||||||
func TestGSM7EncodesIntoLarger8nString(t *testing.T) {
|
func TestGSM7EncodesIntoLarger8nString(t *testing.T) {
|
||||||
input := long8nString
|
input := long8nString
|
||||||
expected := 56
|
expected := 56
|
||||||
actual := GSM7EncodesInto(&input)
|
actual := GSM7Coder{}.EncodesInto(&input)
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||||
}
|
}
|
||||||
@@ -298,7 +298,7 @@ func TestGSM7EncodesIntoLarger8nString(t *testing.T) {
|
|||||||
func TestGSM7DecodesIntoSmallString(t *testing.T) {
|
func TestGSM7DecodesIntoSmallString(t *testing.T) {
|
||||||
input := short8nStringEncodedBytes
|
input := short8nStringEncodedBytes
|
||||||
expected := 8
|
expected := 8
|
||||||
actual := GSM7DecodesInto(bytes.NewBuffer(input))
|
actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||||
}
|
}
|
||||||
@@ -307,7 +307,7 @@ func TestGSM7DecodesIntoSmallString(t *testing.T) {
|
|||||||
func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) {
|
func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) {
|
||||||
input := longNot8nStringEncodedBytes
|
input := longNot8nStringEncodedBytes
|
||||||
expected := 37
|
expected := 37
|
||||||
actual := GSM7DecodesInto(bytes.NewBuffer(input))
|
actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||||
}
|
}
|
||||||
@@ -316,7 +316,7 @@ func TestGSM7DecodesIntoLargerNot8nString(t *testing.T) {
|
|||||||
func TestGSM7DecodesIntoLarger8nString(t *testing.T) {
|
func TestGSM7DecodesIntoLarger8nString(t *testing.T) {
|
||||||
input := long8nStringEncodedBytes
|
input := long8nStringEncodedBytes
|
||||||
expected := 64
|
expected := 64
|
||||||
actual := GSM7DecodesInto(bytes.NewBuffer(input))
|
actual := GSM7Coder{}.DecodesInto(bytes.NewBuffer(input))
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Errorf("Expected %d, but got %d", expected, actual)
|
t.Errorf("Expected %d, but got %d", expected, actual)
|
||||||
}
|
}
|
||||||
@@ -330,7 +330,7 @@ func BenchmarkGSM7EncodeSimpleASCIIString(b *testing.B) {
|
|||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
coder.Encode(input, &buf)
|
coder.Encode(&input, &buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -341,7 +341,7 @@ func BenchmarkGSM7EncodeComplexASCIIString(b *testing.B) {
|
|||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
coder.Encode(input, &buf)
|
coder.Encode(&input, &buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,7 +352,7 @@ func BenchmarkGSM7EncodeComplex8nASCIIString(b *testing.B) {
|
|||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
coder.Encode(input, &buf)
|
coder.Encode(&input, &buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -362,8 +362,8 @@ func BenchmarkGSM7EncodeComplex8nASCIIStringPrealloc(b *testing.B) {
|
|||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
buf := bytes.NewBuffer(make([]byte, GSM7EncodesInto(&input)))
|
buf := bytes.NewBuffer(make([]byte, coder.EncodesInto(&input)))
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
coder.Encode(input, buf)
|
coder.Encode(&input, buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,10 +4,16 @@ import "bytes"
|
|||||||
|
|
||||||
type UCS2Coder struct{}
|
type UCS2Coder struct{}
|
||||||
|
|
||||||
func (c *UCS2Coder) Encode(s string, buf *bytes.Buffer) {
|
func (c *UCS2Coder) Encode(s *string, buf *bytes.Buffer) error {
|
||||||
|
panic("UCS2 not implemented yet")
|
||||||
|
}
|
||||||
|
func (c *UCS2Coder) Decode(buf *bytes.Buffer) (string, error) {
|
||||||
panic("UCS2 not implemented yet")
|
panic("UCS2 not implemented yet")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *UCS2Coder) Decode(buf *bytes.Buffer) string {
|
func (c UCS2Coder) EncodesInto(s *string) int {
|
||||||
panic("UCS2 not implemented yet")
|
return len(*s) * 2
|
||||||
|
}
|
||||||
|
func (c UCS2Coder) DecodesInto(buf *bytes.Buffer) int {
|
||||||
|
return buf.Len() / 2
|
||||||
}
|
}
|
||||||
|
5
go.mod
5
go.mod
@@ -2,7 +2,4 @@ module smpptester
|
|||||||
|
|
||||||
go 1.22.4
|
go 1.22.4
|
||||||
|
|
||||||
require (
|
require github.com/yuin/gopher-lua v1.1.1
|
||||||
github.com/warthog618/sms v0.3.0
|
|
||||||
github.com/yuin/gopher-lua v1.1.1
|
|
||||||
)
|
|
||||||
|
14
go.sum
14
go.sum
@@ -1,16 +1,2 @@
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
|
||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
|
||||||
github.com/warthog618/sms v0.3.0 h1:LYAb5ngmu2qjNExgji3B7xi2tIZ9+DsuE9pC5xs4wwc=
|
|
||||||
github.com/warthog618/sms v0.3.0/go.mod h1:+bYZGeBxu003sxD5xhzsrIPBAjPBzTABsRTwSpd7ld4=
|
|
||||||
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
|
@@ -2,12 +2,10 @@ package pdu
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/ascii85"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/warthog618/sms/encoding/gsm7"
|
"smpptester/encoding"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@@ -67,55 +65,43 @@ type (
|
|||||||
SUBMIT_MULTI struct{}
|
SUBMIT_MULTI struct{}
|
||||||
SUBMIT_MULTI_RESP struct{}
|
SUBMIT_MULTI_RESP struct{}
|
||||||
)
|
)
|
||||||
|
// 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
|
||||||
|
|
||||||
func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
|
func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error {
|
||||||
if buf == nil {
|
if buf == nil {
|
||||||
return fmt.Errorf("cannot encode into nil buffer")
|
return fmt.Errorf("cannot encode into nil buffer")
|
||||||
}
|
}
|
||||||
// This won't do...
|
messageEncoder := p.GetEncoder()
|
||||||
// TODO: Implement your own encoders and shit
|
|
||||||
// ASCII is easy
|
|
||||||
// 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()
|
|
||||||
|
|
||||||
p.header.Encode(buf)
|
p.header.Encode(buf)
|
||||||
n, err := asciiEncoder.Write([]byte("OOO"))
|
// These should be ASCII but UTF8 is a superset of ASCII so hopefully this'll be fine
|
||||||
if err != nil {
|
buf.WriteString(p.service_type)
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.Println(n)
|
|
||||||
// asciiEncoder.Write([]byte(p.service_type))
|
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.source_addr_ton)
|
binary.Write(buf, binary.BigEndian, p.source_addr_ton)
|
||||||
binary.Write(buf, binary.BigEndian, p.source_addr_npi)
|
binary.Write(buf, binary.BigEndian, p.source_addr_npi)
|
||||||
asciiEncoder.Write([]byte(p.source_addr))
|
buf.WriteString(p.source_addr)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.dest_addr_ton)
|
binary.Write(buf, binary.BigEndian, p.dest_addr_ton)
|
||||||
binary.Write(buf, binary.BigEndian, p.dest_addr_npi)
|
binary.Write(buf, binary.BigEndian, p.dest_addr_npi)
|
||||||
asciiEncoder.Write([]byte(p.destination_addr))
|
buf.WriteString(p.destination_addr)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.esm_class)
|
binary.Write(buf, binary.BigEndian, p.esm_class)
|
||||||
binary.Write(buf, binary.BigEndian, p.protocol_id)
|
binary.Write(buf, binary.BigEndian, p.protocol_id)
|
||||||
binary.Write(buf, binary.BigEndian, p.priority_flag)
|
binary.Write(buf, binary.BigEndian, p.priority_flag)
|
||||||
asciiEncoder.Write([]byte(p.schedule_delivery_time))
|
buf.WriteString(p.schedule_delivery_time)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
asciiEncoder.Write([]byte(p.validity_period))
|
buf.WriteString(p.validity_period)
|
||||||
buf.Write(NULL_ARR)
|
buf.Write(NULL_ARR)
|
||||||
binary.Write(buf, binary.BigEndian, p.registered_delivery)
|
binary.Write(buf, binary.BigEndian, p.registered_delivery)
|
||||||
binary.Write(buf, binary.BigEndian, p.replace_if_present)
|
binary.Write(buf, binary.BigEndian, p.replace_if_present)
|
||||||
binary.Write(buf, binary.BigEndian, p.data_coding)
|
binary.Write(buf, binary.BigEndian, p.data_coding)
|
||||||
binary.Write(buf, binary.BigEndian, p.sm_default_msg_id)
|
binary.Write(buf, binary.BigEndian, p.sm_default_msg_id)
|
||||||
binary.Write(buf, binary.BigEndian, p.sm_length)
|
binary.Write(buf, binary.BigEndian, p.sm_length)
|
||||||
encodedMsg, err := messageEncoder.Encode([]byte(p.short_message))
|
err := messageEncoder.Encode(&p.short_message, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buf.Write(encodedMsg)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
|
func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error {
|
||||||
@@ -144,20 +130,24 @@ func (p *SUBMIT_SM) Size() int {
|
|||||||
size += 1 // data_coding
|
size += 1 // data_coding
|
||||||
size += 1 // sm_default_msg_id
|
size += 1 // sm_default_msg_id
|
||||||
size += 1 // sm_length
|
size += 1 // sm_length
|
||||||
// TODO: Handle encoding based on p.data_coding
|
size += p.GetEncoder().EncodesInto(&p.short_message)
|
||||||
switch p.data_coding {
|
|
||||||
case 0b00000000: // GSM7
|
|
||||||
size += (len(p.short_message)*7 + 8 - 1) / 8
|
|
||||||
case 0b00000001: // ASCII
|
|
||||||
size += len(p.short_message)
|
|
||||||
case 0b00000011: // LATIN1
|
|
||||||
size += len(p.short_message)
|
|
||||||
case 0b00001000: // UCS2
|
|
||||||
size += len(p.short_message) * 2
|
|
||||||
}
|
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
func (p *SUBMIT_SM) UpdateSize() {
|
func (p *SUBMIT_SM) UpdateSize() {
|
||||||
p.header.command_length = uint32(p.Size())
|
p.header.command_length = uint32(p.Size())
|
||||||
p.sm_length = byte(len(p.short_message))
|
p.sm_length = byte(len(p.short_message))
|
||||||
}
|
}
|
||||||
|
func (p *SUBMIT_SM) GetEncoder() encoding.Coder {
|
||||||
|
switch p.data_coding {
|
||||||
|
case 0b00000000: // GSM7
|
||||||
|
return &encoding.GSM7Coder{}
|
||||||
|
case 0b00000001: // ASCII
|
||||||
|
return &encoding.ASCIICoder{}
|
||||||
|
// case 0b00000011: // LATIN1
|
||||||
|
// return &encoding.LATIN1Coder{}
|
||||||
|
case 0b00001000: // UCS2
|
||||||
|
return &encoding.UCS2Coder{}
|
||||||
|
default:
|
||||||
|
return &encoding.ASCIICoder{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -39,7 +39,7 @@ func TestEncodeFunctionCorrectlyEncodesAllFields(t *testing.T) {
|
|||||||
t.Fatalf("expected no error, got %v", err)
|
t.Fatalf("expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := []byte{0,0,0,107,0,0,0,4,0,0,0,0,0,5,196,163,79,77,86,52,0,3,1,56,48,51,54,50,0,1,1,56,49,50,51,52,53,48,48,49,48,48,48,0,0,0,0,0,49,56,48,49,48,53,49,50,48,48,48,48,48,48,52,43,0,1,0,0,0,37,82,101,112,108,121,32,89,101,115,32,116,111,32,111,112,116,32,105,110,32,111,114,32,78,111,32,116,111,32,111,112,116,32,111,117,116,46}
|
expected := []byte{0, 0, 0, 107, 0, 0, 0, 4, 0, 0, 0, 0, 0, 5, 196, 163, 79, 77, 86, 52, 0, 3, 1, 56, 48, 51, 54, 50, 0, 1, 1, 56, 49, 50, 51, 52, 53, 48, 48, 49, 48, 48, 48, 0, 0, 0, 0, 0, 49, 56, 48, 49, 48, 53, 49, 50, 48, 48, 48, 48, 48, 48, 52, 43, 0, 1, 0, 1, 0, 37, 82, 101, 112, 108, 121, 32, 89, 101, 115, 32, 116, 111, 32, 111, 112, 116, 32, 105, 110, 32, 111, 114, 32, 78, 111, 32, 116, 111, 32, 111, 112, 116, 32, 111, 117, 116, 46}
|
||||||
if !bytes.Equal(buf.Bytes(), expected) {
|
if !bytes.Equal(buf.Bytes(), expected) {
|
||||||
t.Fatalf("expected %v, got %v", expected, buf.Bytes())
|
t.Fatalf("expected %v, got %v", expected, buf.Bytes())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user