Add null to terminate strings

This commit is contained in:
2024-07-24 20:58:04 +02:00
parent 2453d639ff
commit d25058fdec
4 changed files with 60 additions and 20 deletions

View File

@@ -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
}