forked from linxGnu/gosmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codings.go
300 lines (233 loc) · 6.87 KB
/
codings.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
package data
import (
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/encoding/unicode"
)
const (
// GSM7BITCoding is gsm-7bit coding
GSM7BITCoding byte = 0x00
// ASCIICoding is ascii coding
ASCIICoding byte = 0x01
// BINARY8BIT1Coding is 8-bit binary coding
BINARY8BIT1Coding byte = 0x02
// LATIN1Coding is iso-8859-1 coding
LATIN1Coding byte = 0x03
// BINARY8BIT2Coding is 8-bit binary coding
BINARY8BIT2Coding byte = 0x04
// CYRILLICCoding is iso-8859-5 coding
CYRILLICCoding byte = 0x06
// HEBREWCoding is iso-8859-8 coding
HEBREWCoding byte = 0x07
// UCS2Coding is UCS2 coding
UCS2Coding byte = 0x08
)
// EncDec wraps encoder and decoder interface.
type EncDec interface {
Encode(str string) ([]byte, error)
Decode([]byte) (string, error)
}
// Encoding interface.
type Encoding interface {
EncDec
DataCoding() byte
}
func encode(str string, encoder *encoding.Encoder) ([]byte, error) {
return encoder.Bytes([]byte(str))
}
func decode(data []byte, decoder *encoding.Decoder) (st string, err error) {
tmp, err := decoder.Bytes(data)
if err == nil {
st = string(tmp)
}
return
}
// CustomEncoding is wrapper for user-defined data encoding.
type CustomEncoding struct {
encDec EncDec
coding byte
}
// NewCustomEncoding creates new custom encoding.
func NewCustomEncoding(coding byte, encDec EncDec) Encoding {
return &CustomEncoding{
coding: coding,
encDec: encDec,
}
}
// Encode string.
func (c *CustomEncoding) Encode(str string) ([]byte, error) {
return c.encDec.Encode(str)
}
// Decode data to string.
func (c *CustomEncoding) Decode(data []byte) (string, error) {
return c.encDec.Decode(data)
}
// DataCoding flag.
func (c *CustomEncoding) DataCoding() byte {
return c.coding
}
type gsm7bit struct {
packed bool
}
func (c *gsm7bit) Encode(str string) ([]byte, error) {
return encode(str, GSM7(c.packed).NewEncoder())
}
func (c *gsm7bit) Decode(data []byte) (string, error) {
return decode(data, GSM7(c.packed).NewDecoder())
}
func (c *gsm7bit) DataCoding() byte { return GSM7BITCoding }
func (c *gsm7bit) ShouldSplit(text string, octetLimit uint) (shouldSplit bool) {
if c.packed {
return uint((len(text)*7+7)/8) > octetLimit
} else {
return uint(len(text)) > octetLimit
}
}
func (c *gsm7bit) EncodeSplit(text string, octetLimit uint) (allSeg [][]byte, err error) {
if octetLimit < 64 {
octetLimit = 134
}
allSeg = [][]byte{}
runeSlice := []rune(text)
fr, to := 0, int(octetLimit)
for fr < len(runeSlice) {
if to > len(runeSlice) {
to = len(runeSlice)
}
seg, err := c.Encode(string(runeSlice[fr:to]))
if err != nil {
return nil, err
}
allSeg = append(allSeg, seg)
fr, to = to, to+int(octetLimit)
}
return
}
type ascii struct{}
func (*ascii) Encode(str string) ([]byte, error) {
return []byte(str), nil
}
func (*ascii) Decode(data []byte) (string, error) {
return string(data), nil
}
func (*ascii) DataCoding() byte { return ASCIICoding }
type iso88591 struct{}
func (*iso88591) Encode(str string) ([]byte, error) {
return encode(str, charmap.ISO8859_1.NewEncoder())
}
func (*iso88591) Decode(data []byte) (string, error) {
return decode(data, charmap.ISO8859_1.NewDecoder())
}
func (*iso88591) DataCoding() byte { return LATIN1Coding }
type binary8bit1 struct{}
func (*binary8bit1) Encode(_ string) ([]byte, error) {
return []byte{}, ErrNotImplEncode
}
func (*binary8bit1) Decode(_ []byte) (string, error) {
return "", ErrNotImplDecode
}
func (*binary8bit1) DataCoding() byte { return BINARY8BIT1Coding }
type binary8bit2 struct{}
func (*binary8bit2) Encode(_ string) ([]byte, error) {
return []byte{}, ErrNotImplEncode
}
func (*binary8bit2) Decode(_ []byte) (string, error) {
return "", ErrNotImplDecode
}
func (*binary8bit2) DataCoding() byte { return BINARY8BIT2Coding }
type iso88595 struct{}
func (*iso88595) Encode(str string) ([]byte, error) {
return encode(str, charmap.ISO8859_5.NewEncoder())
}
func (*iso88595) Decode(data []byte) (string, error) {
return decode(data, charmap.ISO8859_5.NewDecoder())
}
func (*iso88595) DataCoding() byte { return CYRILLICCoding }
type iso88598 struct{}
func (*iso88598) Encode(str string) ([]byte, error) {
return encode(str, charmap.ISO8859_8.NewEncoder())
}
func (*iso88598) Decode(data []byte) (string, error) {
return decode(data, charmap.ISO8859_8.NewDecoder())
}
func (*iso88598) DataCoding() byte { return HEBREWCoding }
type ucs2 struct{}
func (*ucs2) Encode(str string) ([]byte, error) {
tmp := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
return encode(str, tmp.NewEncoder())
}
func (*ucs2) Decode(data []byte) (string, error) {
tmp := unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
return decode(data, tmp.NewDecoder())
}
func (*ucs2) ShouldSplit(text string, octetLimit uint) (shouldSplit bool) {
return uint(len(text)*2) > octetLimit
}
func (c *ucs2) EncodeSplit(text string, octetLimit uint) (allSeg [][]byte, err error) {
if octetLimit < 64 {
octetLimit = 134
}
allSeg = [][]byte{}
runeSlice := []rune(text)
hextetLim := int(octetLimit / 2) // round down
// hextet = 16 bits, the correct terms should be hexadectet
fr, to := 0, hextetLim
for fr < len(runeSlice) {
if to > len(runeSlice) {
to = len(runeSlice)
}
seg, err := c.Encode(string(runeSlice[fr:to]))
if err != nil {
return nil, err
}
allSeg = append(allSeg, seg)
fr, to = to, to+hextetLim
}
return
}
func (*ucs2) DataCoding() byte { return UCS2Coding }
var (
// GSM7BIT is gsm-7bit encoding.
GSM7BIT Encoding = &gsm7bit{packed: false}
// GSM7BITPACKED is packed gsm-7bit encoding.
// Most of SMSC(s) use unpack version.
// Should be tested before using.
GSM7BITPACKED Encoding = &gsm7bit{packed: true}
// ASCII is ascii encoding.
ASCII Encoding = &ascii{}
// BINARY8BIT1 is binary 8-bit encoding.
BINARY8BIT1 Encoding = &binary8bit1{}
// LATIN1 encoding.
LATIN1 Encoding = &iso88591{}
// BINARY8BIT2 is binary 8-bit encoding.
BINARY8BIT2 Encoding = &binary8bit2{}
// CYRILLIC encoding.
CYRILLIC Encoding = &iso88595{}
// HEBREW encoding.
HEBREW Encoding = &iso88598{}
// UCS2 encoding.
UCS2 Encoding = &ucs2{}
)
var codingMap = map[byte]Encoding{
GSM7BITCoding: GSM7BIT,
ASCIICoding: ASCII,
BINARY8BIT1Coding: BINARY8BIT1,
LATIN1Coding: LATIN1,
BINARY8BIT2Coding: BINARY8BIT2,
CYRILLICCoding: CYRILLIC,
HEBREWCoding: HEBREW,
UCS2Coding: UCS2,
}
// FromDataCoding returns encoding from DataCoding value.
func FromDataCoding(code byte) (enc Encoding) {
enc = codingMap[code]
return
}
// Splitter extend encoding object by defining a split function
// that split a string into multiple segments
// Each segment string, when encoded, must be within a certain octet limit
type Splitter interface {
// ShouldSplit check if the encoded data of given text should be splitted under octetLimit
ShouldSplit(text string, octetLimit uint) (should bool)
EncodeSplit(text string, octetLimit uint) ([][]byte, error)
}