-
Notifications
You must be signed in to change notification settings - Fork 46
/
decoder.go
306 lines (269 loc) · 7.86 KB
/
decoder.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// Copyright 2019 Insolar Technologies GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package reference
import (
"bytes"
"errors"
"fmt"
"io"
"strings"
"sync"
)
type ByteDecodeFunc func(s string, target io.ByteWriter) (stringRead int, err error)
type IdentityDecoder func(base *Global, name string) *Global
type DecoderOptions uint8
const (
AllowLegacy DecoderOptions = 1 << iota
AllowRecords
IgnoreParity
)
var defaultDecoderOnce sync.Once
var defaultDecoder GlobalDecoder
func DefaultDecoder() GlobalDecoder {
defaultDecoderOnce.Do(func() {
defaultDecoder = NewDefaultDecoder(AllowLegacy | AllowRecords)
})
return defaultDecoder
}
type GlobalDecoder interface {
Decode(ref string) (Global, error)
}
type decoder struct {
byteDecoderFactory ByteDecoderFactory
legacyDecoder ByteDecodeFunc
defaultDecoder ByteDecodeFunc
nameDecoder IdentityDecoder
options DecoderOptions
}
func NewDecoder(options DecoderOptions, factory ByteDecoderFactory) GlobalDecoder {
return &decoder{
byteDecoderFactory: factory,
legacyDecoder: factory.LegacyDecoder(),
defaultDecoder: factory.DefaultDecoder(),
options: options,
}
}
func NewDefaultDecoder(options DecoderOptions) GlobalDecoder {
return NewDecoder(options, NewByteDecoderFactory())
}
func (v decoder) Decode(ref string) (Global, error) {
schemaPos := strings.IndexRune(ref, ':')
if schemaPos >= 0 {
decoder, err := v.parseSchema(ref[:schemaPos], ref)
if err != nil {
return Global{}, err
}
return v.parseReference(ref[schemaPos+1:], decoder)
}
// try to parse the legacy format
if v.options&AllowLegacy != 0 && len(ref) >= 2*len(LegacyDomainName)+1 {
domainPos := strings.IndexRune(ref, '.')
if domainPos >= len(LegacyDomainName) && ref[domainPos+1:] == LegacyDomainName {
return v.parseLegacyAddress(ref, domainPos)
}
}
return v.parseReference(ref, v.defaultDecoder)
}
func (v decoder) parseLegacyAddress(ref string, domainPos int) (Global, error) {
var result Global
w := result.addressLocal.asWriter()
_, err := v.legacyDecoder(ref[:domainPos], w)
switch {
case err != nil:
break
case !w.isFull():
err = errors.New("insufficient length")
case result.addressLocal.getScope() != 0: // there is no scope for legacy
err = errors.New("invalid scope")
case !result.tryConvertToSelf():
err = errors.New("invalid self reference")
default:
return result, nil
}
return result, fmt.Errorf("unable to parse legacy reference, %s: ref=%s", err.Error(), ref)
}
func (v decoder) parseSchema(schema, refFull string) (ByteDecodeFunc, error) {
parts := strings.Split(schema, "+")
switch len(parts) {
case 1:
if parts[0] == SchemaV1 {
return v.defaultDecoder, nil
}
case 2:
switch {
case parts[0] == SchemaV1:
parts[0] = parts[1]
case parts[1] == SchemaV1:
//
default:
return nil, fmt.Errorf("unsupported schema: schema=%s, ref=%s", schema, refFull)
}
default:
return nil, fmt.Errorf("invalid schema: schema=%s", schema)
}
decoder := v.byteDecoderFactory.GetByteDecoder(parts[0])
if decoder == nil {
return nil, fmt.Errorf("unknown decoder: schema=%s, decoder=%s, ref=%s", schema, parts[0], refFull)
}
return decoder, nil
}
func (v decoder) parseAuthority(ref string) (authority string, remaining string) {
if len(ref) < 3 || ref[:2] != "//" {
return "", ref
}
ref = ref[2:]
pos := strings.IndexRune(ref, '/')
if pos < 0 {
return ref, ""
}
return ref[:pos], ref[pos+1:]
}
func (v decoder) parseReference(refFull string, byteDecoder ByteDecodeFunc) (Global, error) {
_, ref := v.parseAuthority(refFull)
if len(ref) == 0 {
return Global{}, fmt.Errorf("empty reference body: ref=%s", refFull)
}
parityPos := strings.IndexRune(ref, '/')
var parity []byte
switch {
case parityPos == 0:
return Global{}, fmt.Errorf("empty reference body: ref=%s", refFull)
case parityPos > 0:
encodedParity := ref[parityPos+1:]
if encodedParity[0] != '2' {
return Global{}, fmt.Errorf("invalid parity prefix: ref=%s, parity=%s", refFull, encodedParity)
}
buf := bytes.NewBuffer(make([]byte, 0, LocalBinaryPulseAndScopeSize))
_, err := byteDecoder(encodedParity[1:], buf)
if err != nil {
return Global{}, fmt.Errorf("unable to decode parity: ref=%s, err=%v", refFull, err)
}
ref = ref[:parityPos]
if v.options&IgnoreParity == 0 {
parity = buf.Bytes()
}
}
var result Global
err := v.parseAddress(ref, byteDecoder, &result)
if err == nil && parity != nil {
err = result.CheckParity(parity)
}
if err == nil {
return result, nil
}
return result, fmt.Errorf("invalid reference, %s: ref=%s", err.Error(), refFull)
}
func (v decoder) parseAddress(ref string, byteDecoder ByteDecodeFunc, result *Global) error {
domainPos := strings.IndexRune(ref, '.')
switch {
case domainPos == 0:
return errors.New("empty reference body")
case domainPos > 0:
domainName := ref[domainPos+1:]
localAddrName := ref[:domainPos]
switch domainName {
case "":
return errors.New("empty domain name")
case RecordDomainName:
if v.options&AllowRecords == 0 {
return errors.New("record reference is not allowed")
}
return v.parseBinaryAddress(localAddrName, byteDecoder, &result.addressLocal)
case LegacyDomainName:
return errors.New("legacy domain name")
default:
resolveBase := &Global{}
err := v.parseBinaryAddress(domainName, byteDecoder, &resolveBase.addressLocal)
switch {
case err == nil:
if !resolveBase.tryConvertToSelf() {
return errors.New("invalid reference base")
}
case err == errAliasedReference:
if v.nameDecoder == nil {
return errors.New("aliases are not allowed")
}
resolveBase = v.nameDecoder(nil, domainName)
if resolveBase == nil {
return errors.New("unknown domain alias")
}
default:
return err
}
return v.parseAddressWithBase(localAddrName, resolveBase, byteDecoder, result)
}
default:
return v.parseAddressWithBase(ref, &Global{}, byteDecoder, result)
}
}
func (v decoder) parseAddressWithBase(name string, base *Global, byteDecoder ByteDecodeFunc, result *Global) error {
err := v.parseBinaryAddress(name, byteDecoder, &result.addressLocal)
switch {
case err != nil:
if err != errAliasedReference {
return err
}
if v.nameDecoder == nil {
return errors.New("aliases are not allowed")
}
resolved := v.nameDecoder(base, name)
if resolved == nil {
return errors.New("unknown object alias")
}
*result = *resolved
case base.IsEmpty():
if result.IsEmpty() {
return nil
}
if !result.tryConvertToSelf() {
return errors.New("invalid self reference")
}
case result.addressLocal.IsEmpty():
return errors.New("empty local part of reference")
default:
if !result.tryApplyBase(base) {
return errors.New("scope mismatch between base and local parts of address")
}
}
return nil
}
var errAliasedReference = errors.New("record reference alias")
func (v decoder) parseBinaryAddress(name string, byteDecoder ByteDecodeFunc, result *Local) error {
switch name[0] {
case '0':
if len(name) == 1 {
return nil
}
_, err := byteDecoder(name[1:], result.asWriter())
if err != nil {
return err
}
case '1':
w := result.asWriter()
_, err := byteDecoder(name[1:], w)
if err != nil {
return err
}
if !w.isFull() {
return errors.New("insufficient address length")
}
case '2', '3', '4', '5', '6', '7', '8', '9':
return errors.New("unsupported address prefix")
default:
return errAliasedReference
}
return nil
}