forked from warthog618/sms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpdu.go
1205 lines (1108 loc) · 28.6 KB
/
tpdu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
//
// Copyright © 2018 Kent Gibson <[email protected]>.
// Package tpdu provides the TPDU type and conversions to and from its binary
// form.
package tpdu
import (
"bytes"
"encoding/binary"
"github.com/warthog618/sms/encoding/gsm7"
)
// TPDU represents all SMS TPDUs.
type TPDU struct {
// Direction indicates whether the TPDU is mobile originated (MO) or
// terminated (MT).
Direction Direction
// FirstOctet is the first octet of all TPDUs.
FirstOctet FirstOctet
// OA contains the TP-OA Originating Address field.
//
// Only applies to SMS-DELIVER
OA Address
// FCS contains the TP-FCS Failure Cause field.
//
// Only applies to SMS-DELIVER-REPORT and SMS-SUBMIT-REPORT
FCS byte
// MR contains the TP-MP Message Reference field.
//
// Only applies to SMS-COMMAND, SMS-SUBMIT and SMS-STATUS-REPORT
MR byte
// CT contains the TP-CT Command Type field.
//
// Only applies to SMS-COMMAND
CT byte
// MN contains the TP-MN Message Number field.
//
// Only applies to SMS-COMMAND
MN byte
// DA contains the TP-DA Destination Address field.
//
// Only applies to SMS-COMMAND and SMS-SUBMIT
DA Address
// RA contains the TP-RA Recipient Address field.
//
// Only applies to SMS-STATUS-REPORT
RA Address
// PI contains the TP-PI Parameter Indicator field.
//
// Only applies to SMS-DELIVER-REPORT and SMS-SUBMIT-REPORT
PI PI
// SCTS contains the TP-SCTS Service Center Time Stamp field.
//
// The SCTS timestamp indicates the time the SMS was sent.
// The time is the originator's local time, the timezone of which may
// differ from the receiver's.
//
// Only applies to SMS-DELIVER, SMS-SUBMIT-REPORT and SMS-STATUS-REPORT
SCTS Timestamp
// DT contains the TP-DT Discharge Time field.
//
// Only applies to SMS-STATUS-REPORT
DT Timestamp
// ST contains the TP-ST Status field.
//
// Only applies to SMS-STATUS-REPORT
ST byte
// PID contains the TP-PID field.
PID byte
// DCS contains the TP-DCS Data Coding Scheme field.
DCS DCS
// VP contains the TP-VP Validity Period field.
//
// Only applies to SMS-SUBMIT
VP ValidityPeriod
// UDH contains the TP-UDH User Data Header field.
UDH UserDataHeader
// UD contains the short message from the User Data.
//
// It does not include the User Data Header, which is provided separately
// in the UDH.
// The interpretation of UD depends on the Alphabet:
// For Alpha7Bit, UD is an array of GSM7 septets, each septet stored in the
// lower 7 bits of a byte.
// These have NOT been converted to the corresponding UTF8.
// Use the gsm7 package to convert to UTF8.
// For AlphaUCS2, UD is an array of UCS2 characters packed into a byte
// array in Big Endian.
// These have NOT been converted to the corresponding UTF8.
// Use the usc2 package to convert to UTF8.
// For Alpha8Bit, UD contains the raw octets.
UD UserData
}
// New creates a new TPDU
func New(options ...Option) (*TPDU, error) {
t := TPDU{OA: NewAddress(), DA: NewAddress(), RA: NewAddress()}
for _, option := range options {
err := option.ApplyTPDUOption(&t)
if err != nil {
return nil, err
}
}
return &t, nil
}
// NewDeliver creates a new TPDU of type SmsDeliver.
func NewDeliver(options ...Option) (*TPDU, error) {
options = append([]Option{SmsDeliver}, options...)
return New(options...)
}
// NewSubmit creates a new TPDU of type SmsSubmit.
func NewSubmit(options ...Option) (*TPDU, error) {
options = append([]Option{SmsSubmit}, options...)
return New(options...)
}
// Alphabet returns the alphabet field from the DCS of the SMS TPDU.
func (t *TPDU) Alphabet() (Alphabet, error) {
return t.DCS.Alphabet()
}
// ConcatInfo extracts the segmentation info contained in the provided User
// Data Header.
func (t *TPDU) ConcatInfo() (segments, seqno, mref int, ok bool) {
return t.UDH.ConcatInfo()
}
// IsSingleSegment returns true unless the TPDU is part of a multi-part
// message.
func (t *TPDU) IsSingleSegment() bool {
_, _, _, ok := t.ConcatInfo()
return !ok
}
// MTI returns the MessageType from the first octet of the SMS TPDU.
func (t *TPDU) MTI() MessageType {
return t.FirstOctet.MTI()
}
// Counter provides a reference couunter that is incremented every time Count
// is called.
type Counter interface {
Count() int
}
type segmentationConfig struct {
// concat IE factory
ief func(msgCount int, segCount int, segment int) InformationElement
// concat ref generator
cr Counter
// MR generator
mr Counter
}
// SegmentationOption provides an option to modify the behaviour of segmentation.
type SegmentationOption func(*segmentationConfig)
// Segment returns the set of SMS TPDUs required to transmit the message.
//
// The TPDU acts as the template for the generated TPDUs and provides all the
// fields in the resulting TPDUs, other than the UD, which is populated using
// the message. For multi-part messages, the UDH provided in the TPDU is
// extended with a concatenation IE. The TPDU UDH must not contain a
// concatenation IE (ID 0 or 8) or the resulting TPDUs will be non-conformant.
func (t TPDU) Segment(msg []byte, options ...SegmentationOption) []TPDU {
if len(msg) == 0 {
return nil
}
cfg := segmentationConfig{newInfoElement, nil, nil}
for _, o := range options {
o(&cfg)
}
bs := t.UDBlockSize()
if len(msg) <= bs {
// single segment
t.UD = msg
if cfg.mr != nil {
t.MR = byte(cfg.mr.Count())
}
return []TPDU{t}
}
// add contcat IE and recalc bs
t.SetUDH(append(t.UDH, cfg.ief(0, 0, 0)))
bs = t.UDBlockSize()
t.UDH = t.UDH[:len(t.UDH)-1]
alpha, _ := t.Alphabet()
chunks := chunk(msg, alpha, bs)
count := len(chunks)
pdus := make([]TPDU, count)
concatRef := 1
if cfg.cr != nil {
concatRef = cfg.cr.Count()
}
for i := 0; i < count; i++ {
pdus[i] = t
if cfg.mr != nil {
pdus[i].MR = byte(cfg.mr.Count())
}
udh := append(t.UDH[:0:0], t.UDH...)
udh = append(udh, cfg.ief(concatRef, count, i+1))
pdus[i].SetUDH(udh)
pdus[i].UD = chunks[i]
}
return pdus
}
// With16BitConcatRef specifies the usage of concat IEs with 16 bit reference
// numbers (ID=8).
//
// By default 8bit reference numbers are used.
var With16BitConcatRef = func(so *segmentationConfig) {
so.ief = newInfoElement16bit
}
// WithMR provides an MR generator to provide the TP-MR field for TPDUs.
//
// By default the MR is copied from the template TPDU.
func WithMR(mr Counter) SegmentationOption {
return func(so *segmentationConfig) {
so.mr = mr
}
}
// WithConcatRef provides a generator to provide the reference for concatenation IEs.
//
// By default the field is set to 1, which is only suitable for one-off messages.
func WithConcatRef(cr Counter) SegmentationOption {
return func(so *segmentationConfig) {
so.cr = cr
}
}
// SetDCS sets the dcs field and the corresponding bit of the PI.
func (t *TPDU) SetDCS(dcs byte) {
t.PI |= PiDCS
t.DCS = DCS(dcs)
}
// SetPID sets the TPDU pid field and the corresponding bit of the PI.
func (t *TPDU) SetPID(pid byte) {
t.PI |= PiPID
t.PID = pid
}
// SetVP sets the validity period and the corresponding VPF bits
// in the firstOctet.
func (t *TPDU) SetVP(vp ValidityPeriod) {
t.FirstOctet &^= FoVPFMask
t.FirstOctet |= (FirstOctet(vp.Format<<FoVPFShift) & FoVPFMask)
t.VP = vp
}
// SetUD sets the TPDU ud field and the corresponding bit of the PI.
func (t *TPDU) SetUD(ud UserData) {
t.UD = ud
if ud == nil {
t.PI &^= PiUDL
} else {
t.PI |= PiUDL
}
}
// SetUDH sets the User Data Header of the TPDU and the TP-UDHI flag.
func (t *TPDU) SetUDH(udh UserDataHeader) {
t.UDH = udh
if udh == nil {
t.FirstOctet &^= FoUDHI
} else {
t.PI |= PiUDL
t.FirstOctet |= FoUDHI
}
}
// SmsType returns the type of SMS-TPDU this TPDU represents.
func (t *TPDU) SmsType() SmsType {
return smsType(t.FirstOctet.MTI(), t.Direction)
}
// SetSmsType returns the type of SMS-TPDU this TPDU represents.
func (t *TPDU) SetSmsType(st SmsType) error {
if st < 0 || st > SmsCommand {
return ErrInvalid
}
t.Direction = st.Direction()
t.FirstOctet = t.FirstOctet.WithMTI(st.MTI())
return nil
}
// UDBlockSize returns the maximum size of a block of UserData that can fit in
// this TPDU.
//
// The interpretation of the size depends on the encoding - for 7bit encoding
// it is the number of septets. For all other encodings it is the number of
// octets.
func (t *TPDU) UDBlockSize() int {
var bs int
switch t.SmsType() {
case SmsSubmit, SmsDeliver:
bs = 140
case SmsCommand:
bs = 146 // conservative
// precise answer depends on variable length fields...
case SmsSubmitReport:
if t.FCS == 0 {
bs = 152 // for RP-ACK
} else {
bs = 151 // for RP-ERROR
}
case SmsDeliverReport:
if t.FCS == 0 {
bs = 159 // for RP-ACK
} else {
bs = 158 // for RP-ERROR
}
case SmsStatusReport:
bs = 131 // conservative
// precise answer depends on variable length fields...
}
alpha, _ := t.Alphabet()
udhl := t.UDHL()
if alpha == Alpha7Bit {
// work in septets
bs = (bs * 8) / 7
if udhl == 0 {
return bs
}
// remove septets used by UDH, including UDHL and fill bits
bs -= ((udhl+1)*8 + 6) / 7
return bs
}
if udhl > 0 {
bs -= (udhl + 1)
}
if alpha == AlphaUCS2 {
bs &^= 0x1
}
return bs
}
// UDHI returns the User Data Header Indicator bit from the SMS TPDU first
// octet.
//
// This is generally the same as testing the length of the udh - unless the dcs
// has been intentionally overwritten to create an inconsistency.
func (t *TPDU) UDHI() bool {
return t.FirstOctet.UDHI()
}
// UDHL returns the encoded length of the UDH, not including the UDHL itself.
func (t *TPDU) UDHL() int {
return t.UDH.UDHL()
}
// MarshalBinary marshals a SMS TPDU into the corresponding byte array.
func (t *TPDU) MarshalBinary() (dst []byte, err error) {
st := smsType(t.FirstOctet.MTI(), t.Direction)
switch st {
case SmsDeliver:
dst, err = t.marshalDeliver()
case SmsDeliverReport:
dst, err = t.marshalDeliverReport()
case SmsSubmitReport:
dst, err = t.marshalSubmitReport()
case SmsSubmit:
dst, err = t.marshalSubmit()
case SmsStatusReport:
dst, err = t.marshalStatusReport()
case SmsCommand:
dst, err = t.marshalCommand()
default:
return nil, ErrUnsupportedSmsType(st)
}
if err != nil {
err = EncodeError(st.String(), err)
}
return
}
func (t *TPDU) marshalCommand() ([]byte, error) {
da, err := t.DA.MarshalBinary()
if err != nil {
return nil, EncodeError("da", err)
}
cdl := len(t.UD)
l := 6 + len(da) + cdl
b := make([]byte, 0, l)
b = append(b, byte(t.FirstOctet), t.MR, t.PID, t.CT, t.MN)
b = append(b, da...)
b = append(b, byte(cdl))
b = append(b, t.UD...)
return b, nil
}
func (t *TPDU) marshalDeliver() ([]byte, error) {
oa, err := t.OA.MarshalBinary()
if err != nil {
return nil, EncodeError("oa", err)
}
scts, err := t.SCTS.MarshalBinary()
if err != nil {
return nil, EncodeError("scts", err)
}
ud, err := t.encodeUserData()
if err != nil {
return nil, EncodeError("ud", err)
}
l := 3 + len(oa) + len(scts) + len(ud)
b := make([]byte, 0, l)
b = append(b, byte(t.FirstOctet))
b = append(b, oa...)
b = append(b, t.PID, byte(t.DCS))
b = append(b, scts...)
b = append(b, ud...)
return b, nil
}
func (t *TPDU) marshalDeliverReport() ([]byte, error) {
ud := []byte{}
if t.PI.UDL() {
var err error
ud, err = t.encodeUserData()
if err != nil {
return nil, EncodeError("ud", err)
}
}
l := 5 + len(ud) // assume FCS, PID and DCS
b := make([]byte, 0, l)
b = append(b, byte(t.FirstOctet))
if t.FCS != 0 {
b = append(b, t.FCS)
}
b = append(b, byte(t.PI))
if t.PI.PID() {
b = append(b, t.PID)
}
if t.PI.DCS() {
b = append(b, byte(t.DCS))
}
b = append(b, ud...)
return b, nil
}
func (t *TPDU) marshalStatusReport() ([]byte, error) {
ra, err := t.RA.MarshalBinary()
if err != nil {
return nil, EncodeError("ra", err)
}
scts, err := t.SCTS.MarshalBinary()
if err != nil {
return nil, EncodeError("scts", err)
}
dt, err := t.DT.MarshalBinary()
if err != nil {
return nil, EncodeError("dt", err)
}
var ud []byte
if t.PI.UDL() {
ud, err = t.encodeUserData()
if err != nil {
return nil, EncodeError("ud", err)
}
}
l := 6 + len(ra) + len(scts) + len(dt) + len(ud) // assume PID and DCS
b := make([]byte, 0, l)
b = append(b, byte(t.FirstOctet), t.MR)
b = append(b, ra...)
b = append(b, scts...)
b = append(b, dt...)
b = append(b, t.ST)
if t.PI == 0x00 {
return b, nil
}
b = append(b, byte(t.PI))
if t.PI.PID() {
b = append(b, t.PID)
}
if t.PI.DCS() {
b = append(b, byte(t.DCS))
}
b = append(b, ud...)
return b, nil
}
func (t *TPDU) marshalSubmit() ([]byte, error) {
da, err := t.DA.MarshalBinary()
if err != nil {
return nil, EncodeError("da", err)
}
ud, err := t.encodeUserData()
if err != nil {
return nil, EncodeError("ud", err)
}
var vp []byte
if t.VP.Format != VpfNotPresent {
vp, err = t.VP.MarshalBinary()
if err != nil {
return nil, EncodeError("vp", err)
}
}
l := 4 + len(da) + len(ud) + len(vp)
b := make([]byte, 0, l)
b = append(b, byte(t.FirstOctet), t.MR)
b = append(b, da...)
b = append(b, t.PID, byte(t.DCS))
b = append(b, vp...)
b = append(b, ud...)
return b, nil
}
func (t *TPDU) marshalSubmitReport() ([]byte, error) {
scts, err := t.SCTS.MarshalBinary()
if err != nil {
return nil, EncodeError("scts", err)
}
var ud []byte
if t.PI.UDL() {
ud, err = t.encodeUserData()
if err != nil {
return nil, EncodeError("ud", err)
}
}
l := 5 + len(scts) + len(ud) // assume PID and DCS
b := make([]byte, 0, l)
b = append(b, byte(t.FirstOctet))
if t.FCS != 0 {
b = append(b, t.FCS)
}
b = append(b, byte(t.PI))
b = append(b, scts...)
if t.PI.PID() {
b = append(b, t.PID)
}
if t.PI.DCS() {
b = append(b, byte(t.DCS))
}
b = append(b, ud...)
return b, nil
}
// UnmarshalBinary unmarshals a SMS TPDU from the corresponding byte array.
//
// In the case of error the TPDU will be partially unmarshalled, up to the
// point that the decoding error was detected.
func (t *TPDU) UnmarshalBinary(src []byte) (err error) {
if len(src) < 1 {
return NewDecodeError("tpdu.firstOctet", 0, ErrUnderflow)
}
t.FirstOctet = FirstOctet(src[0])
st := smsType(t.FirstOctet.MTI(), t.Direction)
switch st {
case SmsDeliver:
err = t.unmarshalDeliver(src[1:])
case SmsDeliverReport:
err = t.unmarshalDeliverReport(src[1:])
case SmsSubmitReport:
err = t.unmarshalSubmitReport(src[1:])
case SmsSubmit:
err = t.unmarshalSubmit(src[1:])
case SmsStatusReport:
err = t.unmarshalStatusReport(src[1:])
case SmsCommand:
err = t.unmarshalCommand(src[1:])
default:
t.FirstOctet = 0
return NewDecodeError("tpdu.firstOctet", 0, ErrUnsupportedSmsType(st))
}
if err != nil {
return NewDecodeError(st.String(), 1, err)
}
return nil
}
func (t *TPDU) unmarshalCommand(src []byte) (err error) {
b := bytes.NewBuffer(src)
t.MR, err = b.ReadByte()
if err != nil {
return NewDecodeError("mr", len(src)-b.Len(), err)
}
t.PID, err = b.ReadByte()
if err != nil {
return NewDecodeError("pid", len(src)-b.Len(), err)
}
t.CT, err = b.ReadByte()
if err != nil {
return NewDecodeError("ct", len(src)-b.Len(), err)
}
t.MN, err = b.ReadByte()
if err != nil {
return NewDecodeError("mn", len(src)-b.Len(), err)
}
n, err := t.DA.UnmarshalBinary(b.Bytes())
if err != nil {
return NewDecodeError("da", len(src)-b.Len(), err)
}
b.Next(n)
t.DCS = Dcs8BitData // force TPDU to interpret UD as 8bit, if not set already
err = t.decodeUserData(b.Bytes())
if err != nil {
return NewDecodeError("ud", len(src)-b.Len(), err)
}
return nil
}
func (t *TPDU) unmarshalDeliver(src []byte) (err error) {
n, err := t.OA.UnmarshalBinary(src)
if err != nil {
return NewDecodeError("oa", 0, err)
}
b := bytes.NewBuffer(src[n:])
t.PID, err = b.ReadByte()
if err != nil {
return NewDecodeError("pid", len(src)-b.Len(), err)
}
dcs, err := b.ReadByte()
if err != nil {
return NewDecodeError("dcs", len(src)-b.Len(), err)
}
t.DCS = DCS(dcs)
err = t.SCTS.UnmarshalBinary(b.Bytes())
if err != nil {
return NewDecodeError("scts", len(src)-b.Len(), err)
}
b.Next(7)
err = t.decodeUserData(b.Bytes())
if err != nil {
return NewDecodeError("ud", len(src)-b.Len(), err)
}
return nil
}
func (t *TPDU) unmarshalDeliverReport(src []byte) error {
ri := 0
if len(src) <= ri {
return NewDecodeError("fcs", ri, ErrUnderflow)
}
t.FCS = src[ri]
ri++
if len(src) <= ri {
return NewDecodeError("pi", ri, ErrUnderflow)
}
t.PI = PI(src[ri])
ri++
if t.PI.PID() {
if len(src) <= ri {
return NewDecodeError("pid", ri, ErrUnderflow)
}
t.PID = src[ri]
ri++
}
if t.PI.DCS() {
if len(src) <= ri {
return NewDecodeError("dcs", ri, ErrUnderflow)
}
t.DCS = DCS(src[ri])
ri++
}
if t.PI.UDL() {
err := t.decodeUserData(src[ri:])
if err != nil {
return NewDecodeError("ud", ri, err)
}
}
return nil
}
func (t *TPDU) unmarshalStatusReport(src []byte) error {
ri := 0
if len(src) <= ri {
return NewDecodeError("mr", ri, ErrUnderflow)
}
t.MR = src[ri]
ri++
n, err := t.RA.UnmarshalBinary(src[ri:])
if err != nil {
return NewDecodeError("ra", ri, err)
}
ri += n
if len(src) < ri+7 {
return NewDecodeError("scts", ri, ErrUnderflow)
}
err = t.SCTS.UnmarshalBinary(src[ri : ri+7])
if err != nil {
return NewDecodeError("scts", ri, err)
}
ri += 7
if len(src) < ri+7 {
return NewDecodeError("dt", ri, ErrUnderflow)
}
err = t.DT.UnmarshalBinary(src[ri : ri+7])
if err != nil {
return NewDecodeError("dt", ri, err)
}
ri += 7
if len(src) <= ri {
return NewDecodeError("st", ri, ErrUnderflow)
}
t.ST = src[ri]
ri++
if len(src) > ri {
return t.unmarshalSROptionals(ri, src)
}
return nil
}
// unmarshal the optional fields at the end of the StatusReport TPDU.
func (t *TPDU) unmarshalSROptionals(ri int, src []byte) error {
t.PI = PI(src[ri])
ri++
if t.PI.PID() {
if len(src) <= ri {
return NewDecodeError("pid", ri, ErrUnderflow)
}
t.PID = src[ri]
ri++
}
if t.PI.DCS() {
if len(src) <= ri {
return NewDecodeError("dcs", ri, ErrUnderflow)
}
t.DCS = DCS(src[ri])
ri++
}
if t.PI.UDL() {
err := t.decodeUserData(src[ri:])
if err != nil {
return NewDecodeError("ud", ri, err)
}
}
return nil
}
func (t *TPDU) unmarshalSubmit(src []byte) error {
if len(src) < 1 {
return NewDecodeError("mr", 0, ErrUnderflow)
}
t.MR = src[0]
ri := 1
n, err := t.DA.UnmarshalBinary(src[ri:])
if err != nil {
return NewDecodeError("da", ri, err)
}
ri += n
if len(src) <= ri {
return NewDecodeError("pid", ri, ErrUnderflow)
}
t.PID = src[ri]
ri++
if len(src) <= ri {
return NewDecodeError("dcs", ri, ErrUnderflow)
}
t.DCS = DCS(src[ri])
ri++
n, err = t.VP.UnmarshalBinary(src[ri:], t.FirstOctet.VPF())
if err != nil {
return NewDecodeError("vp", ri, err)
}
ri += n
err = t.decodeUserData(src[ri:])
if err != nil {
return NewDecodeError("ud", ri, err)
}
return nil
}
func (t *TPDU) unmarshalSubmitReport(src []byte) error {
ri := 0
if len(src) < 1 {
return NewDecodeError("fcs", ri, ErrUnderflow)
}
t.FCS = src[ri]
ri++
if len(src) <= ri {
return NewDecodeError("pi", ri, ErrUnderflow)
}
t.PI = PI(src[ri])
ri++
if len(src) < ri+7 {
return NewDecodeError("scts", ri, ErrUnderflow)
}
err := t.SCTS.UnmarshalBinary(src[ri : ri+7])
if err != nil {
return NewDecodeError("scts", ri, err)
}
ri += 7
if t.PI.PID() {
if len(src) <= ri {
return NewDecodeError("pid", ri, ErrUnderflow)
}
t.PID = src[ri]
ri++
}
if t.PI.DCS() {
if len(src) <= ri {
return NewDecodeError("dcs", ri, ErrUnderflow)
}
t.DCS = DCS(src[ri])
ri++
}
if t.PI.UDL() {
err := t.decodeUserData(src[ri:])
if err != nil {
return NewDecodeError("ud", ri, err)
}
}
return nil
}
// decodeUserData unmarshals the User Data field from the binary src.
func (t *TPDU) decodeUserData(src []byte) error {
if len(src) < 1 {
return NewDecodeError("udl", 0, ErrUnderflow)
}
udl := int(src[0])
if udl == 0 {
return nil
}
var udh UserDataHeader
sml7 := 0
ri := 1
alphabet, err := t.Alphabet()
if err != nil {
return NewDecodeError("alphabet", ri, err)
}
if alphabet == Alpha7Bit {
sml7 = udl
// length is septets - convert to octets
udl = (sml7*7 + 7) / 8
}
if len(src) < ri+udl {
return NewDecodeError("sm", ri, ErrUnderflow)
}
if len(src) > ri+udl {
return NewDecodeError("ud", ri, ErrOverlength)
}
var udhl int // Note that in this context udhl includes itself.
udhi := t.UDHI()
if udhi {
udh = make(UserDataHeader, 0)
l, err := udh.UnmarshalBinary(src[ri:])
if err != nil {
return NewDecodeError("udh", ri, err)
}
udhl = l
ri += udhl
}
if ri == len(src) {
t.UDH = udh
return nil
}
switch alphabet {
case Alpha7Bit:
sm, err := decode7Bit(sml7, udhl, src[ri:])
if err != nil {
return NewDecodeError("sm", ri, err)
}
t.UD = sm
case AlphaUCS2:
if len(src[ri:])&0x01 == 0x01 {
return NewDecodeError("sm", ri, ErrOddUCS2Length)
}
fallthrough
case Alpha8Bit:
t.UD = append([]byte(nil), src[ri:]...)
}
t.UDH = udh
return nil
}
// decode7Bit decodes the GSM7 encoded binary src into a byte array.
//
// sml is the number of septets expected, and udhl is the number of octets in
// the UDH, including the UDHL field.
func decode7Bit(sml, udhl int, src []byte) ([]byte, error) {
var fillBits int
if udhl > 0 {
if dangling := udhl % 7; dangling != 0 {
fillBits = 7 - dangling
}
sml = sml - (udhl*8+fillBits)/7
}
sm := gsm7.Unpack7Bit(src, fillBits)
// this is a double check on the math and should never trip...
if len(sm) < sml {
return nil, ErrUnderflow
}
if len(sm) > sml {
if len(sm) > sml+1 || sm[sml] != 0 {
return nil, ErrOverlength
}
// drop trailing 0 septet
sm = sm[:sml]
}
return sm, nil
}
// encodeUserData marshals the User Data into binary.
//
// The User Data Header is also encoded if present.
// If Alphabet is GSM7 then the User Data is assumed to be unpacked GSM7
// septets and is packed prior to encoding.
// For other alphabet values the User Data is encoded as is.
// No checks of encoded size are performed here as that depends on concrete
// TPDU type, and that can check the length of the returned b.
func (t *TPDU) encodeUserData() (b []byte, err error) {
udh, err := t.UDH.MarshalBinary()
if err != nil {
// never trips as UDH marshalling never fails...
return nil, EncodeError("udh", err)
}
ud := t.UD
alphabet, err := t.Alphabet()
if err != nil {
return nil, EncodeError("alphabet", err)
}
udl := len(t.UD) // assume octets
switch alphabet {
case Alpha7Bit:
fillBits := 0
if dangling := len(udh) % 7; dangling != 0 {
fillBits = 7 - dangling
}
ud = gsm7.Pack7Bit(t.UD, fillBits)
// udl is in septets so convert
if udl > 0 {
udl = udl + (len(udh)*8+fillBits)/7
} else {
udl = (len(udh) * 8) / 7
}
case AlphaUCS2:
if udl&0x01 == 0x01 {
return nil, EncodeError("sm", ErrOddUCS2Length)
}
fallthrough
case Alpha8Bit:
// udl is in octets
udl = udl + len(udh)
}
b = make([]byte, 0, 1+len(udh)+len(ud))
b = append(b, byte(udl))
b = append(b, udh...)
b = append(b, ud...)
return b, nil
}
// MaxUDL is the maximum number of octets that can be encoded into the UD.
// Note that for 7bit encoding this can result in up to 160 septets.
const MaxUDL = 140
// MessageType identifies the type of TPDU encoded in a binary stream, as
// defined in 3GPP TS 23.040 Section 9.2.3.1.
// Note that the direction of the TPDU must also be known to determine how to
// interpret the TPDU.
type MessageType int
const (
// MtDeliver identifies the message as a SMS-Deliver or SMS-Deliver-Report
// TPDU.
MtDeliver MessageType = iota
// MtSubmit identifies the message as a SMS-Submit or SMS-Submit-Report
// TPDU.
MtSubmit
// MtCommand identifies the message as a SMS-Command or SMS-Status-Report
// TPDU.
MtCommand
// MtReserved identifies the message as an unknown type of SMS TPDU.
MtReserved
)
// ApplyTPDUOption sets the TPDU MTI.
func (mti MessageType) ApplyTPDUOption(t *TPDU) error {
t.FirstOctet = t.FirstOctet.WithMTI(mti)
return nil
}