-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
81 lines (73 loc) · 1.63 KB
/
utils.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
package asn1ber
import (
"io"
)
func DecodeUnknownComponent(input io.Reader, oslist ...io.Writer) (int, error) {
var output io.Writer
if len(oslist) > 0 {
output = oslist[0]
} else {
output = nil
}
length := new(BerLength)
byteCount, err := length.Decode(input)
if err != nil {
return byteCount, err
} else if output != nil {
_, _ = length.Encode(output)
}
lengthVal := length.Length
berTag := new(BerTag)
if lengthVal < 0 {
n, err := berTag.Decode(input)
byteCount += n
if err != nil {
return byteCount, err
} else if output != nil {
_, _ = berTag.Encode(output)
}
for !berTag.Equals(0, 0, 0) {
n, err := DecodeUnknownComponent(input, output)
byteCount += n
if err != nil {
return byteCount, err
}
n, err = berTag.Decode(input)
if err != nil {
return byteCount, err
} else if output != nil {
_, _ = berTag.Encode(output)
}
byteCount += n
}
err = ReadEocByte(input)
byteCount += 1 // Exactly one byte read
if err != nil {
return byteCount, err
} else if input != nil {
_, _ = output.Write([]byte{0}) // write end of content
}
return byteCount, nil
} else {
contentBytes := make([]byte, lengthVal)
_, err := io.ReadFull(input, contentBytes)
byteCount += lengthVal
if err != nil {
return byteCount, err
} else if output != nil {
output.Write(contentBytes)
}
return byteCount + lengthVal, nil
}
}
func ReadByte(input io.Reader) (int, error) {
b := []byte{0}
_, err := input.Read(b)
if err != nil {
return -1, err
}
return int(b[0]), nil
}
func WriteByte(output io.Writer, val int) (int, error) {
return output.Write([]byte{byte(val)})
}