-
Notifications
You must be signed in to change notification settings - Fork 24
/
ie.go
264 lines (223 loc) · 5.39 KB
/
ie.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
// Copyright 2019-2024 go-tcap authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package tcap
import (
"fmt"
"io"
)
// Tag is a Tag in TCAP IE
type Tag uint8
// Class definitions.
const (
Universal int = iota
ApplicationWide
ContextSpecific
Private
)
// Type definitions.
const (
Primitive int = iota
Constructor
)
// NewTag creates a new Tag.
func NewTag(cls, form, code int) Tag {
return Tag((cls << 6) | (form << 5) | code)
}
// NewUniversalPrimitiveTag creates a new NewUniversalPrimitiveTag.
func NewUniversalPrimitiveTag(code int) Tag {
return NewTag(Universal, Primitive, code)
}
// NewUniversalConstructorTag creates a new NewUniversalConstructorTag.
func NewUniversalConstructorTag(code int) Tag {
return NewTag(Universal, Constructor, code)
}
// NewApplicationWidePrimitiveTag creates a new NewApplicationWidePrimitiveTag.
func NewApplicationWidePrimitiveTag(code int) Tag {
return NewTag(ApplicationWide, Primitive, code)
}
// NewApplicationWideConstructorTag creates a new NewApplicationWideConstructorTag.
func NewApplicationWideConstructorTag(code int) Tag {
return NewTag(ApplicationWide, Constructor, code)
}
// NewContextSpecificPrimitiveTag creates a new NewContextSpecificPrimitiveTag.
func NewContextSpecificPrimitiveTag(code int) Tag {
return NewTag(ContextSpecific, Primitive, code)
}
// NewContextSpecificConstructorTag creates a new NewContextSpecificConstructorTag.
func NewContextSpecificConstructorTag(code int) Tag {
return NewTag(ContextSpecific, Constructor, code)
}
// NewPrivatePrimitiveTag creates a new NewPrivatePrimitiveTag.
func NewPrivatePrimitiveTag(code int) Tag {
return NewTag(Private, Primitive, code)
}
// NewPrivateConstructorTag creates a new NewPrivateConstructorTag.
func NewPrivateConstructorTag(code int) Tag {
return NewTag(Private, Constructor, code)
}
// Class returns the Class retieved from a Tag.
func (t Tag) Class() int {
return int(t) >> 6 & 0x3
}
// Form returns the Form retieved from a Tag.
func (t Tag) Form() int {
return int(t) >> 5 & 0x1
}
// Code returns the Code retieved from a Tag.
func (t Tag) Code() int {
return int(t) & 0x1f
}
// IE is a General Structure of TCAP Information Elements.
type IE struct {
Tag
Length uint8
Value []byte
IE []*IE
}
// NewIE creates a new IE.
func NewIE(tag Tag, value []byte) *IE {
i := &IE{
Tag: tag,
Value: value,
}
i.SetLength()
return i
}
// MarshalBinary returns the byte sequence generated from a IE instance.
func (i *IE) MarshalBinary() ([]byte, error) {
b := make([]byte, i.MarshalLen())
if err := i.MarshalTo(b); err != nil {
return nil, err
}
return b, nil
}
// MarshalTo puts the byte sequence in the byte array given as b.
func (i *IE) MarshalTo(b []byte) error {
if len(b) < 2 {
return io.ErrUnexpectedEOF
}
b[0] = uint8(i.Tag)
b[1] = i.Length
copy(b[2:i.MarshalLen()], i.Value)
return nil
}
// ParseMultiIEs parses multiple (unspecified number of) IEs to []*IE at a time.
func ParseMultiIEs(b []byte) ([]*IE, error) {
var ies []*IE
for {
if len(b) == 0 {
break
}
i, err := ParseIE(b)
if err != nil {
return nil, err
}
ies = append(ies, i)
b = b[i.MarshalLen():]
continue
}
return ies, nil
}
// ParseIE parses given byte sequence as an IE.
func ParseIE(b []byte) (*IE, error) {
i := &IE{}
if err := i.UnmarshalBinary(b); err != nil {
return nil, err
}
return i, nil
}
// UnmarshalBinary sets the values retrieved from byte sequence in an IE.
func (i *IE) UnmarshalBinary(b []byte) error {
l := len(b)
if l < 3 {
return io.ErrUnexpectedEOF
}
i.Tag = Tag(b[0])
i.Length = b[1]
if l < 2+int(i.Length) {
return io.ErrUnexpectedEOF
}
i.Value = b[2 : 2+int(i.Length)]
return nil
}
// ParseAsBer parses given byte sequence as multiple IEs.
//
// Deprecated: use ParseAsBER instead.
func ParseAsBer(b []byte) ([]*IE, error) {
return ParseAsBER(b)
}
// ParseAsBER parses given byte sequence as multiple IEs.
func ParseAsBER(b []byte) ([]*IE, error) {
var ies []*IE
for {
if len(b) == 0 {
break
}
i, err := ParseIERecursive(b)
if err != nil {
return nil, err
}
ies = append(ies, i)
if len(i.IE) == 0 {
b = b[i.MarshalLen():]
continue
}
if i.IE[0].MarshalLen() < i.MarshalLen()-2 {
var l = 2
for _, ie := range i.IE {
l += ie.MarshalLen()
}
b = b[l:]
continue
}
b = b[i.MarshalLen():]
}
return ies, nil
}
// ParseIERecursive parses given byte sequence as an IE.
func ParseIERecursive(b []byte) (*IE, error) {
i := &IE{}
if err := i.ParseRecursive(b); err != nil {
return nil, err
}
return i, nil
}
// ParseRecursive sets the values retrieved from byte sequence in an IE.
func (i *IE) ParseRecursive(b []byte) error {
l := len(b)
if l < 2 {
return io.ErrUnexpectedEOF
}
i.Tag = Tag(b[0])
i.Length = b[1]
if int(i.Length)+2 > len(b) {
return nil
}
i.Value = b[2 : 2+int(i.Length)]
if i.Tag.Form() == 1 {
x, err := ParseAsBER(i.Value)
if err != nil {
return nil
}
i.IE = append(i.IE, x...)
}
return nil
}
// MarshalLen returns the serial length of IE.
func (i *IE) MarshalLen() int {
return 2 + len(i.Value)
}
// SetLength sets the length in Length field.
func (i *IE) SetLength() {
i.Length = uint8(len(i.Value))
}
// String returns IE in human readable string.
func (i *IE) String() string {
return fmt.Sprintf("{Tag: %#x, Length: %d, Value: %x, IE: %v}",
i.Tag,
i.Length,
i.Value,
i.IE,
)
}