forked from warthog618/sms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserdata.go
421 lines (382 loc) · 11.8 KB
/
userdata.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
// SPDX-License-Identifier: MIT
//
// Copyright © 2018 Kent Gibson <[email protected]>.
package tpdu
import (
"encoding/binary"
"github.com/warthog618/sms/encoding/gsm7"
"github.com/warthog618/sms/encoding/gsm7/charset"
"github.com/warthog618/sms/encoding/ucs2"
)
// UserData represents the User Data field as defined in 3GPP TS 23.040 Section
// 9.2.3.24.
//
// The UserData is comprised of an optional User Data Header and a short
// message field.
type UserData []byte
// UserDataHeader represents the header section of the User Data as defined in
// 3GPP TS 23.040 Section 9.2.3.24.
type UserDataHeader []InformationElement
// InformationElement represents one of the information elements contained in
// the User Data Header.
type InformationElement struct {
ID byte
Data []byte
}
func (ie InformationElement) marshalledLen() int {
return 2 + len(ie.Data)
}
// UDHL returns the encoded length of the UDH, not including the UDHL itself.
func (udh UserDataHeader) UDHL() int {
udhl := 0
for _, ie := range udh {
udhl += ie.marshalledLen()
}
return udhl
}
// MarshalBinary marshals the User Data Header, including the UDHL, into
// binary.
func (udh UserDataHeader) MarshalBinary() ([]byte, error) {
if len(udh) == 0 {
return nil, nil
}
udhl := udh.UDHL()
b := make([]byte, 0, udhl+1)
b = append(b, byte(udhl))
for _, ie := range udh {
b = append(b, ie.ID, byte(len(ie.Data)))
b = append(b, ie.Data...)
}
return b, nil
}
// UnmarshalBinary reads the InformationElements from the binary User Data
// Header.
//
// The src contains the complete UDH, including the UDHL and all IEs.
// The function returns the number of bytes read from src, and any error
// detected while unmarshalling.
func (udh *UserDataHeader) UnmarshalBinary(src []byte) (int, error) {
if len(src) < 1 {
return 0, NewDecodeError("udhl", 0, ErrUnderflow)
}
udhl := int(src[0])
udhl++ // so it includes itself
ri := 1
if len(src) < udhl {
return ri, NewDecodeError("ie", ri, ErrUnderflow)
}
ies := []InformationElement(nil)
for ri < udhl {
if udhl < ri+2 {
return ri, NewDecodeError("ie", ri, ErrUnderflow)
}
var ie InformationElement
ie.ID = src[ri]
ri++
iedl := int(src[ri])
ri++
if len(src) < ri+iedl {
return ri, NewDecodeError("ied", ri, ErrUnderflow)
}
ie.Data = append([]byte(nil), src[ri:ri+iedl]...)
ri += iedl
ies = append(ies, ie)
}
*udh = ies
return udhl, nil
}
// IE returns the last instance of the IE with the given id in the UDH.
//
// If no such IE is found then the function returns false.
func (udh UserDataHeader) IE(id byte) (InformationElement, bool) {
for i := len(udh) - 1; i >= 0; i-- {
if udh[i].ID == id {
return udh[i], true
}
}
return InformationElement{}, false
}
// IEs returns all instances of the IEs with the given id in the UDH.
func (udh UserDataHeader) IEs(id byte) []InformationElement {
ies := []InformationElement(nil)
for _, ie := range udh {
if ie.ID == id {
ies = append(ies, ie)
}
}
return ies
}
// ConcatInfo extracts the segmentation info contained in the provided User
// Data Header.
//
// If the UDH contains no segmentation information then ok is false and zero
// values are returned.
// The returned values do not distinguish between 8bit and 16bit message
// reference numbers.
func (udh UserDataHeader) ConcatInfo() (segments, seqno, mref int, ok bool) {
if len(udh) == 0 {
// single segment - most likely case
return
}
if segments, seqno, mref, ok = udh.ConcatInfo8(); ok {
return
}
return udh.ConcatInfo16()
}
// ConcatInfo8 extracts the segmentation info contained in the provided User
// Data Header, for the 8bit message reference case.
//
// If the UDH contains no segmentation information then ok is false and zero
// values are returned.
func (udh UserDataHeader) ConcatInfo8() (segments, seqno, mref int, ok bool) {
if c, k := udh.IE(0x00); k && len(c.Data) == 3 {
ok = true
mref = int(c.Data[0])
segments = int(c.Data[1])
seqno = int(c.Data[2])
}
return
}
// ConcatInfo16 extracts the segmentation info contained in the provided User
// Data Header, for the 16bit message reference case.
//
// If the UDH contains no segmentation information then ok is false and zero
// values are returned.
func (udh UserDataHeader) ConcatInfo16() (segments, seqno, mref int, ok bool) {
if c, k := udh.IE(0x08); k && len(c.Data) == 4 {
ok = true
mref = int(binary.BigEndian.Uint16(c.Data[0:2]))
segments = int(c.Data[2])
seqno = int(c.Data[3])
}
return
}
type udDecodeConfig struct {
locking map[int]bool
shift map[int]bool
}
// UDDecodeOption provides behavioural modifiers for DecodeUserData,
// specifically the character sets available to decode GSM7.
type UDDecodeOption interface {
applyDecodeOption(udDecodeConfig) udDecodeConfig
}
// DecodeUserData converts TPDU UD into the corresponding UTF8 message.
//
// The UD is expected to be unpacked, as stored in TPDU UD. If the UD is GSM7
// encoded then it is translated to UTF8 with the default character set, or
// with the character set specified in the UDH, assuming the corresponding
// language has been registered with the UDDecoder. If the UDH specifies a
// character set that has not been registered then the translation will fall
// back to the default character set.
func DecodeUserData(ud UserData, udh UserDataHeader, alpha Alphabet, options ...UDDecodeOption) ([]byte, error) {
switch alpha {
case AlphaUCS2:
m, err := ucs2.Decode(ud)
return []byte(string(m)), err
case Alpha8Bit:
return ud, nil
case Alpha7Bit:
fallthrough
default:
cfg := udDecodeConfig{locking: map[int]bool{}, shift: map[int]bool{}}
for _, option := range options {
cfg = option.applyDecodeOption(cfg)
}
options := []gsm7.DecoderOption{}
if ie, ok := udh.IE(lockingIEI); ok {
if len(ie.Data) >= 1 {
nli := int(ie.Data[0])
if _, ok := cfg.locking[nli]; ok {
options = append(options, gsm7.WithCharset(nli))
}
}
}
if ie, ok := udh.IE(shiftIEI); ok {
if len(ie.Data) >= 1 {
nli := int(ie.Data[0])
if _, ok := cfg.shift[nli]; ok {
options = append(options, gsm7.WithExtCharset(nli))
}
}
}
return gsm7.Decode(ud, options...)
}
}
type udEncodeConfig struct {
locking []int // locking charsets in order
shift []int // shift charsets in order
}
// UDEncodeOption provides behavioural modifiers for EncodeUserData,
// specifically the locking and shift character sets available, in addition to
// the default character set.
type UDEncodeOption interface {
applyEncodeOption(udEncodeConfig) udEncodeConfig
}
// CharsetOption adds the locking and shift character sets available for
// encoding and decoding.
//
// These are in addition to the default character set.
type CharsetOption struct {
nli []int
}
func (o CharsetOption) applyDecodeOption(d udDecodeConfig) udDecodeConfig {
for _, n := range o.nli {
d.locking[n] = true
d.shift[n] = true
}
return d
}
func (o CharsetOption) applyEncodeOption(e udEncodeConfig) udEncodeConfig {
e.locking = append(e.locking, o.nli...)
e.shift = append(e.shift, o.nli...)
return e
}
// LockingCharsetOption adds to the locking character sets available for
// encoding and decoding.
//
// These are in addition to the default character set.
type LockingCharsetOption struct {
nli []int
}
func (o LockingCharsetOption) applyDecodeOption(d udDecodeConfig) udDecodeConfig {
for _, n := range o.nli {
d.locking[n] = true
}
return d
}
func (o LockingCharsetOption) applyEncodeOption(e udEncodeConfig) udEncodeConfig {
e.locking = append(e.locking, o.nli...)
return e
}
// ShiftCharsetOption adds the shift character sets available for encoding
// and decoding.
//
// These are in addition to the default character set.
type ShiftCharsetOption struct {
nli []int
}
func (o ShiftCharsetOption) applyDecodeOption(d udDecodeConfig) udDecodeConfig {
for _, n := range o.nli {
d.shift[n] = true
}
return d
}
func (o ShiftCharsetOption) applyEncodeOption(e udEncodeConfig) udEncodeConfig {
e.shift = append(e.shift, o.nli...)
return e
}
// AllCharsetsOption specifies that all character sets are available for
// encoding and decoding.
type AllCharsetsOption struct{}
func (o AllCharsetsOption) applyDecodeOption(d udDecodeConfig) udDecodeConfig {
for nli := charset.Start; nli < charset.End; nli++ {
d.locking[nli] = true
d.shift[nli] = true
}
return d
}
func (o AllCharsetsOption) applyEncodeOption(e udEncodeConfig) udEncodeConfig {
e.locking = make([]int, charset.Size)
for nli := charset.Start; nli < charset.End; nli++ {
e.locking[nli-1] = nli
}
e.shift = e.locking
return e
}
// WithAllCharsets makes all possible character sets available to encode or
// decode.
//
// This is equivalent to calling WithCharset with all possible
// NationalLanguageIdentifiers, in increasing order.
var WithAllCharsets = AllCharsetsOption{}
// WithCharset sets the set of character sets available to encode or decode.
//
// These are in addition to the default character set.
func WithCharset(nli ...int) CharsetOption {
return CharsetOption{nli}
}
// WithLockingCharset sets the set of locking character sets available to
// encode or decode.
//
// These are in addition to the default character set.
func WithLockingCharset(nli ...int) LockingCharsetOption {
return LockingCharsetOption{nli}
}
// WithShiftCharset sets the set of shift character sets available to
// encode or decode.
//
// These are in addition to the default character set.
func WithShiftCharset(nli ...int) ShiftCharsetOption {
return ShiftCharsetOption{nli}
}
const (
shiftIEI byte = 24
lockingIEI byte = 25
)
// EncodeUserData converts a UTF8 message into corresponding TPDU User Data.
//
// Note that the UD size is not limited to the size available in a single TPDU,
// and so may need to be segmented into several concatenated messages. Encode
// attempts to pick the most compact alphabet for the given message. It assumes
// GSM7 is the most compact, and, if the default character set is insufficient,
// tries combinations of supported language character sets, in the order they
// were added to the UDEncoder.
//
// This is not optimal as it performs language selection on the whole message,
// rather than determining the best for each segment in turn. (which is totally
// allowed as stated in 3GPP TS 23.040 9.2.3.24.15 + 16), but this may be a
// safer approach - to allow for the decoder being non-compliant, and the
// benefit of per-segment language encoding is minimal. In most cases there is
// no benefit at all.
//
// Failing GSM7 conversion it falls back to UCS2/UTF16.
func EncodeUserData(msg []byte, options ...UDEncodeOption) (UserData, UserDataHeader, Alphabet) {
enc, err := gsm7.Encode([]byte(msg)) // default charset
if err == nil {
return enc, nil, Alpha7Bit
}
cfg := udEncodeConfig{}
for _, option := range options {
cfg = option.applyEncodeOption(cfg)
}
// try locking tables with default shift
for _, nli := range cfg.locking {
enc, err = gsm7.Encode(msg, gsm7.WithCharset(nli))
if err == nil {
return enc, UserDataHeader{
InformationElement{ID: lockingIEI, Data: []byte{byte(nli)}},
},
Alpha7Bit
}
}
// try default with language shift tables
for _, nli := range cfg.shift {
enc, err = gsm7.Encode(msg, gsm7.WithExtCharset(nli))
if err == nil {
return enc, UserDataHeader{
InformationElement{ID: shiftIEI, Data: []byte{byte(nli)}},
},
Alpha7Bit
}
}
// try combination of locking AND shift for same charset
for _, nli := range cfg.locking {
for _, snli := range cfg.shift {
if nli != snli {
continue
}
enc, err = gsm7.Encode(msg, gsm7.WithCharset(nli), gsm7.WithExtCharset(nli))
if err == nil {
return enc, UserDataHeader{
InformationElement{ID: lockingIEI, Data: []byte{byte(nli)}},
InformationElement{ID: shiftIEI, Data: []byte{byte(nli)}},
},
Alpha7Bit
}
}
}
// could also try other combos of locking AND shift, but unlikely to help??...
// fallback to ucs-2
enc = ucs2.Encode([]rune(string(msg)))
return enc, nil, AlphaUCS2
}