generated from cunicu/skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tlv.go
158 lines (124 loc) · 2.63 KB
/
tlv.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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
// Package tlv implements various version (ASN.1 BER, Simple, Compact) TLV encoding using in ISO 7816-4.
package tlv
import (
"bytes"
"encoding"
"errors"
"slices"
)
var errInvalidLength = errors.New("invalid length")
type TagValue struct {
Tag Tag
Value []byte
Children TagValues
SkipLength bool
}
func (tv TagValue) Equal(w TagValue) bool {
if !tv.Tag.IsConstructed() {
return bytes.Equal(tv.Value, w.Value)
}
return tv.Children.Equal(w.Children)
}
func New(t Tag, values ...any) (tv TagValue) {
tv.Tag = t
for _, value := range values {
switch v := value.(type) {
case encoding.BinaryMarshaler:
data, err := v.MarshalBinary()
if err != nil {
panic("failed to marshal")
}
tv.Value = append(tv.Value, data...)
case byte:
tv.Value = append(tv.Value, v)
case []byte:
tv.Value = append(tv.Value, v...)
case string:
tv.Value = append(tv.Value, []byte(v)...)
case TagValue:
tv.Children = append(tv.Children, v)
case TagValues:
tv.Children = append(tv.Children, v...)
}
}
return tv
}
type TagValues []TagValue
func (tvs TagValues) Get(tag Tag) ([]byte, TagValues, bool) {
for _, tv := range tvs {
if tv.Tag == tag {
return tv.Value, tv.Children, true
}
}
return nil, nil, false
}
func (tvs *TagValues) Put(tv TagValue) {
*tvs = append(*tvs, tv)
}
func (tvs *TagValues) Pop(tag Tag) (TagValue, bool) {
for i, tv := range *tvs {
if tv.Tag == tag {
*tvs = slices.Delete(*tvs, i, i+1)
return tv, true
}
}
return TagValue{}, false
}
func (tvs TagValues) GetChild(tag Tag, subs ...Tag) ([]byte, TagValues, bool) {
value, children, ok := tvs.Get(tag)
if ok {
if len(subs) > 0 && len(children) > 0 {
return children.GetChild(subs[0], subs[1:]...)
}
return value, children, true
}
return nil, nil, false
}
func (tvs *TagValues) GetAll(tag Tag) (s TagValues) {
for _, tv := range *tvs {
if tv.Tag == tag {
s = append(s, tv)
}
}
return s
}
func (tvs *TagValues) DeleteAll(tag Tag) (removed int) {
var n, r int
for _, tv := range *tvs {
if tv.Tag != tag {
(*tvs)[n] = tv
n++
} else {
r++
}
}
(*tvs) = (*tvs)[:n]
return r
}
func (tvs *TagValues) PopAll(tag Tag) (s TagValues) {
var n int
for _, tv := range *tvs {
if tv.Tag != tag {
(*tvs)[n] = tv
n++
} else {
s = append(s, tv)
}
}
(*tvs) = (*tvs)[:n]
return s
}
func (tvs TagValues) Equal(w TagValues) bool {
if len(tvs) != len(w) {
return false
}
for i, vc := range tvs {
wc := w[i]
if !vc.Equal(wc) {
return false
}
}
return true
}