-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessage_decode.go
390 lines (364 loc) · 9.1 KB
/
message_decode.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
package pdu
//import "gopkg.in/webnice/debug.v1"
//import "gopkg.in/webnice/log.v2"
import (
"bytes"
"encoding/hex"
"fmt"
runtimeDebug "runtime/debug"
"strconv"
"time"
"gopkg.in/webnice/pdu.v1/encoders"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
func (msg *message) findCommand(src *bytes.Buffer) {
var idx []int
idx = rexDataWithCommand.FindSubmatchIndex(src.Bytes())
if idx == nil {
return
}
msg.Cmd = string(src.Bytes()[idx[2]:idx[3]])
msg.CmdStat, msg.Err = strconv.ParseInt(string(src.Bytes()[idx[4]:idx[5]]), 0, 64)
if msg.Err != nil {
msg.CmdStat = 0
}
msg.CmdAlpha = string(src.Bytes()[idx[6]:idx[7]])
msg.CmdLength, msg.Err = strconv.ParseInt(string(src.Bytes()[idx[8]:idx[9]]), 0, 64)
if msg.Err != nil {
msg.CmdLength = 0
}
}
func (msg *message) findPDU(src *bytes.Buffer) {
var idx []int
idx = rexDataWithoutCommand.FindSubmatchIndex(src.Bytes())
if idx == nil {
return
}
msg.DataSource, msg.Err = hex.DecodeString(string(src.Bytes()[idx[2]:idx[3]]))
}
// Load Service Centre Address
func (msg *message) loadTpSca() {
var tmp byte
var pe int
var buf []byte
tmp = msg.DataSource[msg.Lp]
msg.Lp++
msg.TpScaLen = tmp
if msg.TpScaLen == 0 {
return
}
msg.TpScaTypeSource = msg.DataSource[msg.Lp]
msg.Lp++
pe = msg.Lp + int(msg.TpScaLen)
buf = msg.DataSource[msg.Lp:pe]
msg.TpScaNumber = encoders.NewSemiOctet().DecodeAddress(buf)
msg.TpScaNumericPlan, msg.TpScaType = decodeNumberType(msg.TpScaTypeSource)
msg.Lp += int(msg.TpScaLen) - 1
return
}
// Load Originating Address
func (msg *message) loadTpOa() {
var tmp byte
var pe int
var buf []byte
var dl float64
tmp = msg.DataSource[msg.Lp]
msg.Lp++
msg.TpOaLen = tmp
if msg.TpOaLen == 0 {
return
}
msg.TpOaTypeSource = msg.DataSource[msg.Lp]
msg.Lp++
dl = float64(msg.TpOaLen) * 4 / 8
if float64(int64(dl)) < dl {
dl = float64(int64(dl)) + 1
}
pe = msg.Lp + int(dl)
buf = msg.DataSource[msg.Lp:pe]
msg.TpOaNumber = encoders.NewSemiOctet().DecodeAddress(buf)
msg.TpOaNumericPlan, msg.TpOaType = decodeNumberType(msg.TpOaTypeSource)
msg.Lp += int(dl)
return
}
// Load Protocol identifier (TP-PID)
func (msg *message) loadPid() {
msg.Pid = msg.DataSource[msg.Lp]
msg.Lp++
}
// Load Message Type indicator
func (msg *message) loadMti() {
var tmp byte
msg.MtiSource = msg.DataSource[msg.Lp]
msg.Lp++
tmp = msg.MtiSource
if tmp&(1<<7) > 0 {
msg.MtiReplyPath = true
}
if tmp&(1<<6) > 0 {
msg.MtiUdhiFound = true
}
if tmp&(1<<5) > 0 {
msg.MtiStatusReport = true
}
if tmp&(1<<2) > 0 {
msg.MtiNoMoreMessageToSend = true
}
tmp &= 0x3
switch tmp {
case 0:
msg.MtiSmsType = TypeSmsDeliver
case 1:
msg.MtiSmsType = TypeSmsSubmitReport
case 2:
msg.MtiSmsType = TypeSmsStatusReport
case 3:
msg.MtiSmsType = TypeSmsReserved
}
if msg.MtiSmsType != TypeSmsStatusReport {
return
}
// Status report message ID
msg.TpMr = msg.DataSource[msg.Lp]
msg.Lp++
}
// Load Data coding scheme TP-SCTS
func (msg *message) loadDsc() {
msg.DcsSource = msg.DataSource[msg.Lp]
msg.Lp++
if msg.DcsSource&(1<<4) > 0 {
msg.DscFlash = true
}
if msg.DcsSource&(1<<3) > 0 {
msg.DscUSC2 = true
}
}
// Load User Data (TP-UD)
func (msg *message) loadUd() {
var pe int
msg.SmsDataSourceLength = msg.DataSource[msg.Lp]
msg.Lp++
defer func() {
if e := recover(); e != nil {
msg.Err = fmt.Errorf("Catch panic: %s\nStack is:\n%s", e.(error), string(runtimeDebug.Stack()))
msg.End = true
}
}()
switch msg.DscUSC2 {
case true:
// UTF-16
pe = int(msg.SmsDataSourceLength) + msg.Lp
if pe > len(msg.DataSource) {
msg.Err = fmt.Errorf("Message is corrupted. User data is too short")
msg.End = true
return
}
if len(msg.DataSource) < msg.Lp || len(msg.DataSource) < pe {
msg.Err = fmt.Errorf("User data is corrupted. Expected data length %d, actual data length is %d", pe, len(msg.DataSource))
msg.End = true
return
}
msg.SmsDataSource = msg.DataSource[msg.Lp:pe]
msg.Lp += int(msg.SmsDataSourceLength)
case false:
// 7-bit piece of shit
var dl = float64(msg.SmsDataSourceLength) * 7 / 8
if float64(int64(dl)) < dl {
dl = float64(int64(dl)) + 1
}
pe = msg.Lp + int(dl)
if len(msg.DataSource) < msg.Lp || len(msg.DataSource) < pe {
msg.Err = fmt.Errorf("User data is corrupted. Expected data length %d, actual data length is %d", pe, len(msg.DataSource))
msg.End = true
return
}
msg.SmsDataSource = msg.DataSource[msg.Lp:pe]
msg.Lp += int(dl)
}
}
// Load Time stamp
func (msg *message) loadTimeStamp() time.Time {
var buf []byte
var str string
var y, m, d, H, M, S, t int
defer func() {
if e := recover(); e != nil {
msg.Err = fmt.Errorf("Service Centre Time Stamp (TP-SCTS) is incorrect. Expected data length 12, actual length is %d", len(str))
msg.End = true
}
}()
buf = msg.DataSource[msg.Lp : msg.Lp+7]
msg.Lp += 7
str = encoders.NewSemiOctet().DecodeAddress(buf)
y, msg.Err = strconv.Atoi(str[0:2])
m, msg.Err = strconv.Atoi(str[2:4])
d, msg.Err = strconv.Atoi(str[4:6])
H, msg.Err = strconv.Atoi(str[6:8])
M, msg.Err = strconv.Atoi(str[8:10])
S, msg.Err = strconv.Atoi(str[10:12])
t, msg.Err = strconv.Atoi(str[12:14])
return time.Date(y+2000, time.Month(m), d, H, M, S, 0, time.UTC).Add(time.Hour * time.Duration(t)).In(time.Local)
}
// Load user data header information from user data
func (msg *message) loadUdhi() {
var pe, lp int
if !msg.MtiUdhiFound {
return
}
if len(msg.SmsDataSource) == 0 {
return
}
msg.UdhiLength = msg.SmsDataSource[lp]
lp++
pe = lp + int(msg.UdhiLength)
msg.UdhiSource = msg.SmsDataSource[lp:pe]
lp += int(msg.UdhiLength)
// Cut header from user data
msg.SmsDataSource = msg.SmsDataSource[lp:]
msg.SmsDataSourceLength -= (msg.UdhiLength + uint8(1))
// Decode header
lp = 0
msg.UdhiIei = msg.UdhiSource[lp]
lp++
msg.UdhiIedl = msg.UdhiSource[lp]
lp++
msg.UdhiIedID = uint16(msg.UdhiSource[lp])
lp++
if msg.UdhiIedl == 4 {
msg.UdhiIedID = msg.UdhiIedID << 8
msg.UdhiIedID = msg.UdhiIedID & uint16(msg.UdhiSource[lp])
lp++
}
msg.UdhiNumberParts = msg.UdhiSource[lp]
lp++
msg.UdhiSequenceID = msg.UdhiSource[lp]
}
// Decode loaded user data
func (msg *message) decodeUD() {
defer func() {
if e := recover(); e != nil {
msg.Err = fmt.Errorf("Catch panic: %s\nStack is:\n%s", e.(error), string(runtimeDebug.Stack()))
msg.End = true
}
}()
msg.loadUdhi()
if msg.DscUSC2 {
var buf []byte
var enc = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
var tnsf = enc.NewDecoder()
buf, msg.SmsDataLength, msg.Err = transform.Bytes(tnsf, msg.SmsDataSource)
if msg.SmsDataLength > 0 && msg.Err == nil {
msg.SmsData = string(buf)
} else {
msg.SmsDataLength = 0
}
} else {
msg.SmsData, msg.Err = encoders.New7Bit().Decode(msg.SmsDataSource)
}
}
// Load specified fields status report messages
func (msg *message) loadStatusReport() {
msg.TpSt = msg.DataSource[msg.Lp]
msg.Lp++
switch msg.TpSt {
case 0x00:
msg.TpStType = StatusDelivered
case 0x01:
msg.TpStType = StatusForwarded
case 0x02:
msg.TpStType = StatusReplaced
case 0x20:
msg.TpStType = StatusCongestion
case 0x21:
msg.TpStType = StatusRecipientBusy
case 0x22:
msg.TpStType = StatusRecipientNoResponse
case 0x23:
msg.TpStType = StatusServiceRejected
case 0x24:
msg.TpStType = StatusQosNotAvailableTrying
case 0x25:
msg.TpStType = StatusRecipientError
case 0x40:
msg.TpStType = StatusRpcError
case 0x41:
msg.TpStType = StatusIncompatible
case 0x42:
msg.TpStType = StatusConnectionRejected
case 0x43:
msg.TpStType = StatusNotObtainable
case 0x44:
msg.TpStType = StatusQosNotAvailable
case 0x45:
msg.TpStType = StatusNoINAvailable
case 0x46:
msg.TpStType = StatusMessageExpired
case 0x47:
msg.TpStType = StatusMessageDeletedBySender
case 0x48:
msg.TpStType = StatusMessageDeletedBySmsc
case 0x49:
msg.TpStType = StatusDoesNotExist
}
msg.End = true
}
// Scan Source data scanning
func (msg *message) Scan(src *bytes.Buffer) {
msg.findCommand(src)
msg.findPDU(src)
// Error, PDU data is empty
if len(msg.DataSource) == 0 {
msg.Err = ErrIncorrectPDUdata
msg.End = true
return
}
msg.loadTpSca()
msg.loadMti()
msg.loadTpOa()
// Type of SMS
switch msg.MtiSmsType {
case TypeSmsStatusReport, TypeSmsSubmitReport, TypeSmsDeliverReport:
// The service centre time stamp (TP-SCTS)
msg.ServiceCentreTimeStamp = msg.loadTimeStamp()
// Discharge Time - The TP-DT field indicates the time and date associated with a particular TP-ST outcome
msg.TpDischargeTime = msg.loadTimeStamp()
msg.loadStatusReport()
default:
// Normal SMS
if msg.loadPid(); msg.Err != nil && msg.End {
return
}
if msg.loadDsc(); msg.Err != nil && msg.End {
return
}
// The service centre time stamp (TP-SCTS)
msg.ServiceCentreTimeStamp = msg.loadTimeStamp()
if msg.Err != nil && msg.End {
return
}
if msg.loadUd(); msg.Err != nil && msg.End {
return
}
if msg.decodeUD(); msg.Err != nil && msg.End {
return
}
}
// Clean
msg.DataSource = []byte{}
msg.SmsDataSource = []byte{}
msg.UdhiSource = []byte{}
if msg.End {
return
}
// No header no parts
if !msg.MtiUdhiFound {
msg.End = true
return
}
// Number of parts
if msg.UdhiNumberParts <= 1 {
msg.End = true
return
}
}