-
Notifications
You must be signed in to change notification settings - Fork 12
/
tlv.go
61 lines (49 loc) · 1.46 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
package tlv
import (
"encoding/binary"
"github.com/ubavic/bas-celik/card/cardErrors"
)
// Parses simple TLV-encoded data and returns a map of tags to values.
// It assumes that tag and length are encoded with two bytes each.
func ParseTLV(data []byte) (map[uint][]byte, error) {
if len(data) == 0 {
return nil, cardErrors.ErrInvalidLength
}
m := make(map[uint][]byte)
offset := uint(0)
for {
tag := uint(binary.LittleEndian.Uint16(data[offset:]))
length := uint(binary.LittleEndian.Uint16(data[offset+2:]))
offset += 4
if offset+length > uint(len(data)) {
return nil, cardErrors.ErrInvalidLength
}
value := data[offset : offset+length]
m[tag] = value
offset += length
if offset >= uint(len(data)) {
break
}
}
return m, nil
}
// Assigns the value from the provided fields map to the target string, based on the specified tag.
// If the tag is not present in the map, the target is set to an empty string.
func AssignField[T comparable](fields map[T][]byte, tag T, target *string) {
val, ok := fields[tag]
if ok {
*target = string(val)
} else {
*target = ""
}
}
// Assigns a boolean value from the provided fields map to the target, based on the specified tag.
// If the tag is not present in the map or the value is not 0x31, the target is set to false.
func AssignBoolField(fields map[uint][]byte, tag uint, target *bool) {
val, ok := fields[tag]
if ok && len(val) == 1 && val[0] == 0x31 {
*target = true
} else {
*target = false
}
}