-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecode.go
206 lines (182 loc) · 3.98 KB
/
decode.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
package llsd
import (
"encoding/ascii85"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
"math"
"strconv"
"strings"
"time"
)
type ScalarType int
const (
Base16 = "base16"
Base64 = "base64"
Base85 = "base85"
)
const (
Undefined ScalarType = iota
Boolean
Integer
Real
UUIDType
String
Binary
Date
URI
)
func (t ScalarType) String() string {
switch t {
case Undefined:
return "undef"
case Boolean:
return "boolean"
case Integer:
return "integer"
case Real:
return "real"
case UUIDType:
return "uuid"
case String:
return "string"
case Binary:
return "binary"
case Date:
return "date"
case URI:
return "uri"
default:
return strconv.Itoa(int(t))
}
}
type ArrayStart struct{}
type ArrayEnd struct{}
type MapStart struct{}
type MapEnd struct{}
type Token any
type Scalar struct {
Type ScalarType
Data []byte
Attr map[string]string
}
type UUID [16]byte
type Key string
type URL string
func (u UUID) String() string {
return hex.EncodeToString(u[:])
}
type TokenReader interface {
Token() (Token, error) // Get next LLSD token
Offset() int64 // Input stream offset
}
type scalarDecoder interface {
real([]byte) (float64, error)
uuid([]byte) (UUID, error)
integer([]byte) (int64, error)
binary([]byte, string) ([]byte, error)
date([]byte) (time.Time, error)
boolean([]byte) (bool, error)
}
type textDecoder struct{}
func (d *textDecoder) real(c []byte) (float64, error) {
// Default value = 0.0
if len(c) == 0 || c == nil {
return 0.0, nil
}
f, err := strconv.ParseFloat(string(c), 64)
if err != nil {
return 0, err
}
return f, nil
}
func (d *textDecoder) uuid(c []byte) (UUID, error) {
// Default value = 00000000-00000000-00000000-00000000
if len(c) == 0 || c == nil {
return [16]byte{}, nil
}
h, err := hex.DecodeString(strings.ReplaceAll(string(c), "-", ""))
if err != nil {
return [16]byte{}, err
}
var u UUID
copy(u[:], h[:16])
return u, nil
}
func (d *textDecoder) integer(c []byte) (int64, error) {
i, err := strconv.Atoi(string(c))
return int64(i), err
}
func (d *textDecoder) binary(c []byte, encoding string) ([]byte, error) {
if len(c) == 0 || c == nil {
return c, nil
}
switch encoding {
case Base16, "":
dst := make([]byte, hex.DecodedLen(len(c)))
_, err := hex.Decode(dst, c)
return dst, err
case Base64:
dst := make([]byte, base64.StdEncoding.DecodedLen(len(c)))
_, err := base64.StdEncoding.Decode(dst, c)
return dst, err
case Base85:
dst := make([]byte, ascii85.MaxEncodedLen(len(c)))
_, _, err := ascii85.Decode(dst, c, true)
return dst, err
default:
return nil, fmt.Errorf("Unknown encoding \"%s\"", encoding)
}
}
func (d *textDecoder) boolean(c []byte) (bool, error) {
if len(c) == 0 || c == nil {
return false, nil
}
if string(c) == "1" || string(c) == "true" {
return true, nil
} else if string(c) == "0" || string(c) == "false" {
return false, nil
}
return false, fmt.Errorf("Invalid boolean value %s", c)
}
func (d *textDecoder) date(c []byte) (time.Time, error) {
if len(c) == 0 || c == nil {
return time.Unix(0, 0), nil
}
return time.Parse(time.RFC3339, string(c))
}
type binaryDecoder struct{}
func (d *binaryDecoder) real(b []byte) (float64, error) {
// Default value = 0.0
if len(b) == 0 || b == nil {
return 0.0, nil
}
bits := binary.BigEndian.Uint64(b)
return math.Float64frombits(bits), nil
}
func (d *binaryDecoder) uuid(b []byte) (UUID, error) {
// Default value = 00000000-00000000-00000000-00000000
if len(b) == 0 || b == nil {
return [16]byte{}, nil
}
var u UUID
copy(u[:], b[:16])
return u, nil
}
func (d *binaryDecoder) integer(b []byte) (int64, error) {
return int64(binary.BigEndian.Uint32(b)), nil
}
func (d *binaryDecoder) binary(b []byte, encoding string) ([]byte, error) {
return b, nil
}
func (d *binaryDecoder) boolean(b []byte) (bool, error) {
return len(b) > 1, nil
}
func (d *binaryDecoder) date(b []byte) (time.Time, error) {
epoch, err := d.integer(b)
if err != nil {
return time.Unix(0, 0), err
}
return time.Unix(epoch, 0), nil
}