From d25058fdecefa9584b13336951d010d13cd61ed0 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 24 Jul 2024 20:58:04 +0200 Subject: [PATCH] Add null to terminate strings --- go.mod | 5 ++++- go.sum | 14 ++++++++++++ pdu/global.go | 2 ++ pdu/submit.go | 59 ++++++++++++++++++++++++++++++++++----------------- 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index 27b1fc7..e37b6c8 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module smpptester go 1.22.4 -require github.com/yuin/gopher-lua v1.1.1 +require ( + github.com/warthog618/sms v0.3.0 + github.com/yuin/gopher-lua v1.1.1 +) diff --git a/go.sum b/go.sum index e7daa0c..24283eb 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,16 @@ +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/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= diff --git a/pdu/global.go b/pdu/global.go index d06cdb6..a0c8beb 100644 --- a/pdu/global.go +++ b/pdu/global.go @@ -1,3 +1,5 @@ package pdu var ByteBufferPool = NewBufferPoolManager() +const NULL = byte(0x00) +var NULL_ARR = []byte{NULL} \ No newline at end of file diff --git a/pdu/submit.go b/pdu/submit.go index a2b8c9e..254a039 100644 --- a/pdu/submit.go +++ b/pdu/submit.go @@ -5,6 +5,8 @@ import ( "encoding/ascii85" "encoding/binary" "fmt" + + "github.com/warthog618/sms/encoding/gsm7" ) type ( @@ -69,32 +71,41 @@ func (p *SUBMIT_SM) Encode(buf *bytes.Buffer) error { if buf == nil { return fmt.Errorf("cannot encode into nil buffer") } + asciiEncoder := ascii85.NewEncoder(buf) + // TODO: Implement encodings bsed on p.data_coding + messageEncoder := gsm7.NewEncoder() + p.header.Encode(buf) - n := ascii85.Encode(buf.Bytes(), []byte(p.service_type)) - buf.Truncate(n) + asciiEncoder.Write([]byte(p.service_type)) + buf.Write(NULL_ARR) + asciiEncoder.Write([]byte(p.service_type)) + buf.Write(NULL_ARR) binary.Write(buf, binary.BigEndian, p.source_addr_ton) binary.Write(buf, binary.BigEndian, p.source_addr_npi) - n = ascii85.Encode(buf.Bytes(), []byte(p.source_addr)) - buf.Truncate(n) + asciiEncoder.Write([]byte(p.source_addr)) + buf.Write(NULL_ARR) binary.Write(buf, binary.BigEndian, p.dest_addr_ton) binary.Write(buf, binary.BigEndian, p.dest_addr_npi) - n = ascii85.Encode(buf.Bytes(), []byte(p.destination_addr)) - buf.Truncate(n) + asciiEncoder.Write([]byte(p.destination_addr)) + buf.Write(NULL_ARR) binary.Write(buf, binary.BigEndian, p.esm_class) binary.Write(buf, binary.BigEndian, p.protocol_id) binary.Write(buf, binary.BigEndian, p.priority_flag) - n = ascii85.Encode(buf.Bytes(), []byte(p.schedule_delivery_time)) - buf.Truncate(n) - n = ascii85.Encode(buf.Bytes(), []byte(p.validity_period)) - buf.Truncate(n) + asciiEncoder.Write([]byte(p.schedule_delivery_time)) + buf.Write(NULL_ARR) + asciiEncoder.Write([]byte(p.validity_period)) + buf.Write(NULL_ARR) binary.Write(buf, binary.BigEndian, p.registered_delivery) binary.Write(buf, binary.BigEndian, p.replace_if_present) 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_length) - // TODO: Implement encodings bsed on p.data_coding - n = ascii85.Encode(buf.Bytes(), []byte(p.short_message)) - buf.Truncate(n) + encodedMsg, err := messageEncoder.Encode([]byte(p.short_message)) + if err != nil { + return err + } + buf.Write(encodedMsg) + buf.Write(NULL_ARR) return nil } func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error { @@ -106,23 +117,33 @@ func (p *SUBMIT_SM) Decode(buf *bytes.Buffer) error { func (p *SUBMIT_SM) Size() int { var size int size += p.header.Size() - size += len(p.service_type) * 1 + size += 1 + len(p.service_type) size += 1 // source_addr_ton size += 1 // source_addr_npi - size += len(p.source_addr) * 1 + size += 1 + len(p.source_addr) size += 1 // dest_addr_ton size += 1 // dest_addr_npi - size += len(p.destination_addr) * 1 + size += 1 + len(p.destination_addr) size += 1 // esm_class size += 1 // protocol_id size += 1 // priority_flag - size += len(p.schedule_delivery_time) * 1 - size += len(p.validity_period) * 1 + size += 1 + len(p.schedule_delivery_time) + size += 1 + len(p.validity_period) size += 1 // registered_delivery size += 1 // replace_if_present size += 1 // data_coding size += 1 // sm_default_msg_id size += 1 // sm_length - size += len(p.short_message) * 1 + // TODO: Handle encoding based on p.data_coding + 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 }