-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathgsm7.go
372 lines (341 loc) · 13.5 KB
/
gsm7.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
package encoding
import (
"bytes"
"errors"
"math"
"golang.org/x/text/encoding"
"golang.org/x/text/transform"
)
// ErrInvalidCharacter means a given character can not be represented in GSM 7-bit encoding.
//
// This can only happen during encoding.
var ErrInvalidCharacter = errors.New("invalid gsm7 character")
// ErrInvalidByte means that a given byte is outside of the GSM 7-bit encoding range.
//
// This can only happen during decoding.
var ErrInvalidByte = errors.New("invalid gsm7 byte")
/*
GSM 7-bit default alphabet and extension table
Source: https://en.wikipedia.org/wiki/GSM_03.38#GSM_7-bit_default_alphabet_and_extension_table_of_3GPP_TS_23.038_/_GSM_03.38
*/
const escapeSequence = 0x1B
var forwardLookup = map[rune]byte{
'@': 0x00, '£': 0x01, '$': 0x02, '¥': 0x03, 'è': 0x04, 'é': 0x05, 'ù': 0x06, 'ì': 0x07,
'ò': 0x08, 'Ç': 0x09, '\n': 0x0a, 'Ø': 0x0b, 'ø': 0x0c, '\r': 0x0d, 'Å': 0x0e, 'å': 0x0f,
'Δ': 0x10, '_': 0x11, 'Φ': 0x12, 'Γ': 0x13, 'Λ': 0x14, 'Ω': 0x15, 'Π': 0x16, 'Ψ': 0x17,
'Σ': 0x18, 'Θ': 0x19, 'Ξ': 0x1a /* 0x1B */, 'Æ': 0x1c, 'æ': 0x1d, 'ß': 0x1e, 'É': 0x1f,
' ': 0x20, '!': 0x21, '"': 0x22, '#': 0x23, '¤': 0x24, '%': 0x25, '&': 0x26, '\'': 0x27,
'(': 0x28, ')': 0x29, '*': 0x2a, '+': 0x2b, ',': 0x2c, '-': 0x2d, '.': 0x2e, '/': 0x2f,
'0': 0x30, '1': 0x31, '2': 0x32, '3': 0x33, '4': 0x34, '5': 0x35, '6': 0x36, '7': 0x37,
'8': 0x38, '9': 0x39, ':': 0x3a, ';': 0x3b, '<': 0x3c, '=': 0x3d, '>': 0x3e, '?': 0x3f,
'¡': 0x40, 'A': 0x41, 'B': 0x42, 'C': 0x43, 'D': 0x44, 'E': 0x45, 'F': 0x46, 'G': 0x47,
'H': 0x48, 'I': 0x49, 'J': 0x4a, 'K': 0x4b, 'L': 0x4c, 'M': 0x4d, 'N': 0x4e, 'O': 0x4f,
'P': 0x50, 'Q': 0x51, 'R': 0x52, 'S': 0x53, 'T': 0x54, 'U': 0x55, 'V': 0x56, 'W': 0x57,
'X': 0x58, 'Y': 0x59, 'Z': 0x5a, 'Ä': 0x5b, 'Ö': 0x5c, 'Ñ': 0x5d, 'Ü': 0x5e, '§': 0x5f,
'¿': 0x60, 'a': 0x61, 'b': 0x62, 'c': 0x63, 'd': 0x64, 'e': 0x65, 'f': 0x66, 'g': 0x67,
'h': 0x68, 'i': 0x69, 'j': 0x6a, 'k': 0x6b, 'l': 0x6c, 'm': 0x6d, 'n': 0x6e, 'o': 0x6f,
'p': 0x70, 'q': 0x71, 'r': 0x72, 's': 0x73, 't': 0x74, 'u': 0x75, 'v': 0x76, 'w': 0x77,
'x': 0x78, 'y': 0x79, 'z': 0x7a, 'ä': 0x7b, 'ö': 0x7c, 'ñ': 0x7d, 'ü': 0x7e, 'à': 0x7f,
}
var forwardEscape = map[rune]byte{
'\f': 0x0A, '^': 0x14, '{': 0x28, '}': 0x29, '\\': 0x2F, '[': 0x3C, '~': 0x3D, ']': 0x3E, '|': 0x40, '€': 0x65,
}
var reverseLookup = map[byte]rune{
0x00: '@', 0x01: '£', 0x02: '$', 0x03: '¥', 0x04: 'è', 0x05: 'é', 0x06: 'ù', 0x07: 'ì',
0x08: 'ò', 0x09: 'Ç', 0x0a: '\n', 0x0b: 'Ø', 0x0c: 'ø', 0x0d: '\r', 0x0e: 'Å', 0x0f: 'å',
0x10: 'Δ', 0x11: '_', 0x12: 'Φ', 0x13: 'Γ', 0x14: 'Λ', 0x15: 'Ω', 0x16: 'Π', 0x17: 'Ψ',
0x18: 'Σ', 0x19: 'Θ', 0x1a: 'Ξ' /* 0x1B */, 0x1c: 'Æ', 0x1d: 'æ', 0x1e: 'ß', 0x1f: 'É',
0x20: ' ', 0x21: '!', 0x22: '"', 0x23: '#', 0x24: '¤', 0x25: '%', 0x26: '&', 0x27: '\'',
0x28: '(', 0x29: ')', 0x2a: '*', 0x2b: '+', 0x2c: ',', 0x2d: '-', 0x2e: '.', 0x2f: '/',
0x30: '0', 0x31: '1', 0x32: '2', 0x33: '3', 0x34: '4', 0x35: '5', 0x36: '6', 0x37: '7',
0x38: '8', 0x39: '9', 0x3a: ':', 0x3b: ';', 0x3c: '<', 0x3d: '=', 0x3e: '>', 0x3f: '?',
0x40: '¡', 0x41: 'A', 0x42: 'B', 0x43: 'C', 0x44: 'D', 0x45: 'E', 0x46: 'F', 0x47: 'G',
0x48: 'H', 0x49: 'I', 0x4a: 'J', 0x4b: 'K', 0x4c: 'L', 0x4d: 'M', 0x4e: 'N', 0x4f: 'O',
0x50: 'P', 0x51: 'Q', 0x52: 'R', 0x53: 'S', 0x54: 'T', 0x55: 'U', 0x56: 'V', 0x57: 'W',
0x58: 'X', 0x59: 'Y', 0x5a: 'Z', 0x5b: 'Ä', 0x5c: 'Ö', 0x5d: 'Ñ', 0x5e: 'Ü', 0x5f: '§',
0x60: '¿', 0x61: 'a', 0x62: 'b', 0x63: 'c', 0x64: 'd', 0x65: 'e', 0x66: 'f', 0x67: 'g',
0x68: 'h', 0x69: 'i', 0x6a: 'j', 0x6b: 'k', 0x6c: 'l', 0x6d: 'm', 0x6e: 'n', 0x6f: 'o',
0x70: 'p', 0x71: 'q', 0x72: 'r', 0x73: 's', 0x74: 't', 0x75: 'u', 0x76: 'v', 0x77: 'w',
0x78: 'x', 0x79: 'y', 0x7a: 'z', 0x7b: 'ä', 0x7c: 'ö', 0x7d: 'ñ', 0x7e: 'ü', 0x7f: 'à',
}
var reverseEscape = map[byte]rune{
0x0A: '\f', 0x14: '^', 0x28: '{', 0x29: '}', 0x2F: '\\', 0x3C: '[', 0x3D: '~', 0x3E: ']', 0x40: '|', 0x65: '€',
}
// Returns the characters, in the given text, that can not be represented in GSM 7-bit encoding.
func ValidateGSM7String(text string) []rune {
invalidChars := make([]rune, 0, 4)
for _, r := range text {
if _, ok := forwardLookup[r]; !ok {
if _, ok := forwardEscape[r]; !ok {
invalidChars = append(invalidChars, r)
}
}
}
return invalidChars
}
// Returns the bytes, in the given buffer, that are outside of the GSM 7-bit encoding range.
func ValidateGSM7Buffer(buffer []byte) []byte {
invalidBytes := make([]byte, 0, 4)
count := 0
for count < len(buffer) {
b := buffer[count]
if b == escapeSequence {
count++
if count >= len(buffer) {
invalidBytes = append(invalidBytes, b)
break
}
e := buffer[count]
if _, ok := reverseEscape[e]; !ok {
invalidBytes = append(invalidBytes, b, e)
}
} else if _, ok := reverseLookup[b]; !ok {
invalidBytes = append(invalidBytes, b)
}
count++
}
return invalidBytes
}
// GSM7 returns a GSM 7-bit Bit Encoding.
//
// Set the packed flag to true if you wish to convert septets to octets,
// this should be false for most SMPP providers.
func GSM7(packed bool) encoding.Encoding {
return gsm7Encoding{packed: packed}
}
type gsm7Encoding struct {
packed bool
}
func (g gsm7Encoding) NewDecoder() *encoding.Decoder {
return &encoding.Decoder{Transformer: &gsm7Decoder{
packed: g.packed,
}}
}
func (g gsm7Encoding) NewEncoder() *encoding.Encoder {
return &encoding.Encoder{Transformer: &gsm7Encoder{
packed: g.packed,
}}
}
func (g gsm7Encoding) String() string {
if g.packed {
return "GSM 7-bit (Packed)"
}
return "GSM 7-bit (Unpacked)"
}
type gsm7Decoder struct {
packed bool
}
func (g *gsm7Decoder) Reset() {
/* not needed */
}
func (g *gsm7Decoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
if len(src) == 0 {
return 0, 0, nil
}
septets := src
if g.packed {
septets = make([]byte, 0, len(src))
count := 0
remain := len(src) - count
for remain > 0 {
// Unpack by converting octets into septets.
if remain >= 7 {
septets = append(septets, src[count+0]&0x7F<<0)
septets = append(septets, (src[count+1]&0x3F<<1)|(src[count+0]&0x80>>7))
septets = append(septets, (src[count+2]&0x1F<<2)|(src[count+1]&0xC0>>6))
septets = append(septets, (src[count+3]&0x0F<<3)|(src[count+2]&0xE0>>5))
septets = append(septets, (src[count+4]&0x07<<4)|(src[count+3]&0xF0>>4))
septets = append(septets, (src[count+5]&0x03<<5)|(src[count+4]&0xF8>>3))
septets = append(septets, (src[count+6]&0x01<<6)|(src[count+5]&0xFC>>2))
if src[count+6] > 0 {
septets = append(septets, src[count+6]&0xFE>>1)
}
count += 7
} else if remain >= 6 {
septets = append(septets, src[count+0]&0x7F<<0)
septets = append(septets, (src[count+1]&0x3F<<1)|(src[count+0]&0x80>>7))
septets = append(septets, (src[count+2]&0x1F<<2)|(src[count+1]&0xC0>>6))
septets = append(septets, (src[count+3]&0x0F<<3)|(src[count+2]&0xE0>>5))
septets = append(septets, (src[count+4]&0x07<<4)|(src[count+3]&0xF0>>4))
septets = append(septets, (src[count+5]&0x03<<5)|(src[count+4]&0xF8>>3))
count += 6
} else if remain >= 5 {
septets = append(septets, src[count+0]&0x7F<<0)
septets = append(septets, (src[count+1]&0x3F<<1)|(src[count+0]&0x80>>7))
septets = append(septets, (src[count+2]&0x1F<<2)|(src[count+1]&0xC0>>6))
septets = append(septets, (src[count+3]&0x0F<<3)|(src[count+2]&0xE0>>5))
septets = append(septets, (src[count+4]&0x07<<4)|(src[count+3]&0xF0>>4))
count += 5
} else if remain >= 4 {
septets = append(septets, src[count+0]&0x7F<<0)
septets = append(septets, (src[count+1]&0x3F<<1)|(src[count+0]&0x80>>7))
septets = append(septets, (src[count+2]&0x1F<<2)|(src[count+1]&0xC0>>6))
septets = append(septets, (src[count+3]&0x0F<<3)|(src[count+2]&0xE0>>5))
count += 4
} else if remain >= 3 {
septets = append(septets, src[count+0]&0x7F<<0)
septets = append(septets, (src[count+1]&0x3F<<1)|(src[count+0]&0x80>>7))
septets = append(septets, (src[count+2]&0x1F<<2)|(src[count+1]&0xC0>>6))
count += 3
} else if remain >= 2 {
septets = append(septets, src[count+0]&0x7F<<0)
septets = append(septets, (src[count+1]&0x3F<<1)|(src[count+0]&0x80>>7))
count += 2
} else if remain >= 1 {
septets = append(septets, src[count+0]&0x7F<<0)
count += 1
} else {
break
}
remain = len(src) - count
}
}
if g.packed && len(septets)%8 == 0 && septets[len(septets)-1] == 0x0d {
septets = septets[:len(septets)-1]
}
nSeptet := 0
builder := bytes.NewBufferString("")
for nSeptet < len(septets) {
b := septets[nSeptet]
if b == escapeSequence {
nSeptet++
if nSeptet >= len(septets) {
return 0, 0, ErrInvalidByte
}
e := septets[nSeptet]
if r, ok := reverseEscape[e]; ok {
builder.WriteRune(r)
} else {
return 0, 0, ErrInvalidByte
}
} else if r, ok := reverseLookup[b]; ok {
builder.WriteRune(r)
} else {
return 0, 0, ErrInvalidByte
}
nSeptet++
}
text := builder.Bytes()
nDst = len(text)
if len(dst) < nDst {
return 0, 0, transform.ErrShortDst
}
for x, b := range text {
dst[x] = b
}
return nDst, nSrc, err
}
type gsm7Encoder struct {
packed bool
}
func (g *gsm7Encoder) Reset() {
/* no needed */
}
func (g *gsm7Encoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
if len(src) == 0 {
return 0, 0, nil
}
text := string(src) // work with []rune (a.k.a string) instead of []byte
septets := make([]byte, 0, len(text))
for _, r := range text {
if v, ok := forwardLookup[r]; ok {
septets = append(septets, v)
} else if v, ok := forwardEscape[r]; ok {
septets = append(septets, escapeSequence, v)
} else {
return 0, 0, ErrInvalidCharacter
}
nSrc++
}
nDst = len(septets)
unpackedLen := nDst
if g.packed {
nDst = int(math.Ceil(float64(len(septets)) * 7 / 8))
}
if len(dst) < nDst {
return 0, 0, transform.ErrShortDst
}
if !g.packed {
for x, v := range septets {
dst[x] = v
}
return nDst, nSrc, nil
}
nDst = 0
nSeptet := 0
remain := len(septets) - nSeptet
for remain > 0 {
// Pack by converting septets into octets.
if remain >= 8 {
dst[nDst+0] = (septets[nSeptet+0] & 0x7F >> 0) | (septets[nSeptet+1] & 0x01 << 7)
dst[nDst+1] = (septets[nSeptet+1] & 0x7E >> 1) | (septets[nSeptet+2] & 0x03 << 6)
dst[nDst+2] = (septets[nSeptet+2] & 0x7C >> 2) | (septets[nSeptet+3] & 0x07 << 5)
dst[nDst+3] = (septets[nSeptet+3] & 0x78 >> 3) | (septets[nSeptet+4] & 0x0F << 4)
dst[nDst+4] = (septets[nSeptet+4] & 0x70 >> 4) | (septets[nSeptet+5] & 0x1F << 3)
dst[nDst+5] = (septets[nSeptet+5] & 0x60 >> 5) | (septets[nSeptet+6] & 0x3F << 2)
dst[nDst+6] = (septets[nSeptet+6] & 0x40 >> 6) | (septets[nSeptet+7] & 0x7F << 1)
nSeptet += 8
nDst += 7
} else if remain >= 7 {
dst[nDst+0] = (septets[nSeptet+0] & 0x7F >> 0) | (septets[nSeptet+1] & 0x01 << 7)
dst[nDst+1] = (septets[nSeptet+1] & 0x7E >> 1) | (septets[nSeptet+2] & 0x03 << 6)
dst[nDst+2] = (septets[nSeptet+2] & 0x7C >> 2) | (septets[nSeptet+3] & 0x07 << 5)
dst[nDst+3] = (septets[nSeptet+3] & 0x78 >> 3) | (septets[nSeptet+4] & 0x0F << 4)
dst[nDst+4] = (septets[nSeptet+4] & 0x70 >> 4) | (septets[nSeptet+5] & 0x1F << 3)
dst[nDst+5] = (septets[nSeptet+5] & 0x60 >> 5) | (septets[nSeptet+6] & 0x3F << 2)
dst[nDst+6] = septets[nSeptet+6] & 0x40 >> 6
nSeptet += 7
nDst += 7
} else if remain >= 6 {
dst[nDst+0] = (septets[nSeptet+0] & 0x7F >> 0) | (septets[nSeptet+1] & 0x01 << 7)
dst[nDst+1] = (septets[nSeptet+1] & 0x7E >> 1) | (septets[nSeptet+2] & 0x03 << 6)
dst[nDst+2] = (septets[nSeptet+2] & 0x7C >> 2) | (septets[nSeptet+3] & 0x07 << 5)
dst[nDst+3] = (septets[nSeptet+3] & 0x78 >> 3) | (septets[nSeptet+4] & 0x0F << 4)
dst[nDst+4] = (septets[nSeptet+4] & 0x70 >> 4) | (septets[nSeptet+5] & 0x1F << 3)
dst[nDst+5] = septets[nSeptet+5] & 0x60 >> 5
nSeptet += 6
nDst += 6
} else if remain >= 5 {
dst[nDst+0] = (septets[nSeptet+0] & 0x7F >> 0) | (septets[nSeptet+1] & 0x01 << 7)
dst[nDst+1] = (septets[nSeptet+1] & 0x7E >> 1) | (septets[nSeptet+2] & 0x03 << 6)
dst[nDst+2] = (septets[nSeptet+2] & 0x7C >> 2) | (septets[nSeptet+3] & 0x07 << 5)
dst[nDst+3] = (septets[nSeptet+3] & 0x78 >> 3) | (septets[nSeptet+4] & 0x0F << 4)
dst[nDst+4] = septets[nSeptet+4] & 0x70 >> 4
nSeptet += 5
nDst += 5
} else if remain >= 4 {
dst[nDst+0] = (septets[nSeptet+0] & 0x7F >> 0) | (septets[nSeptet+1] & 0x01 << 7)
dst[nDst+1] = (septets[nSeptet+1] & 0x7E >> 1) | (septets[nSeptet+2] & 0x03 << 6)
dst[nDst+2] = (septets[nSeptet+2] & 0x7C >> 2) | (septets[nSeptet+3] & 0x07 << 5)
dst[nDst+3] = septets[nSeptet+3] & 0x78 >> 3
nSeptet += 4
nDst += 4
} else if remain >= 3 {
dst[nDst+0] = (septets[nSeptet+0] & 0x7F >> 0) | (septets[nSeptet+1] & 0x01 << 7)
dst[nDst+1] = (septets[nSeptet+1] & 0x7E >> 1) | (septets[nSeptet+2] & 0x03 << 6)
dst[nDst+2] = septets[nSeptet+2] & 0x7C >> 2
nSeptet += 3
nDst += 3
} else if remain >= 2 {
dst[nDst+0] = (septets[nSeptet+0] & 0x7F >> 0) | (septets[nSeptet+1] & 0x01 << 7)
dst[nDst+1] = septets[nSeptet+1] & 0x7E >> 1
nSeptet += 2
nDst += 2
} else if remain >= 1 {
dst[nDst+0] = septets[nSeptet+0] & 0x7F >> 0
nSeptet += 1
nDst += 1
} else {
break
}
remain = len(septets) - nSeptet
}
// When there are 7 spare bits in the last octet of a message, these bits are set to the 7-bit code of the CR control
// (also used as a padding filler) instead of being set to zero (where they would be confused with the 7-bit code of an '@' character).
if g.packed && (unpackedLen*7)%8 == 1 {
if v := dst[nDst-1]; v == 0x01 || v == 0x00 {
dst[nDst-1] = v | 0x0d<<1
}
}
return nDst, nSrc, err
}