-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutils.go
565 lines (498 loc) · 12.6 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package typegen
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
"reflect"
"sort"
"sync"
cid "github.com/ipfs/go-cid"
)
const (
maxCidLength = 100
maxHeaderSize = 9
)
const (
MajUnsignedInt = 0
MajNegativeInt = 1
MajByteString = 2
MajTextString = 3
MajArray = 4
MajMap = 5
MajTag = 6
MajOther = 7
)
var maxLengthError = fmt.Errorf("length beyond maximum allowed")
type CBORUnmarshaler interface {
UnmarshalCBOR(io.Reader) error
}
type CBORMarshaler interface {
MarshalCBOR(io.Writer) error
}
func readByte(r io.Reader) (byte, error) {
// try to cast to a concrete type, it's much faster than casting to an
// interface.
switch r := r.(type) {
case *bytes.Buffer:
return r.ReadByte()
case *bytes.Reader:
return r.ReadByte()
case *bufio.Reader:
return r.ReadByte()
case *peeker:
return r.ReadByte()
case *CborReader:
return readByte(r.r)
case io.ByteReader:
return r.ReadByte()
}
var buf [1]byte
_, err := io.ReadFull(r, buf[:1])
return buf[0], err
}
func CborReadHeader(br io.Reader) (byte, uint64, error) {
if cr, ok := br.(*CborReader); ok {
return cr.ReadHeader()
}
first, err := readByte(br)
if err != nil {
return 0, 0, err
}
defer func() {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
}()
maj := (first & 0xe0) >> 5
low := first & 0x1f
switch {
case low < 24:
return maj, uint64(low), nil
case low == 24:
next, err := readByte(br)
if err != nil {
return 0, 0, err
}
if next < 24 {
return 0, 0, fmt.Errorf("cbor input was not canonical (lval 24 with value < 24)")
}
return maj, uint64(next), nil
case low == 25:
scratch := make([]byte, 2)
if _, err := io.ReadAtLeast(br, scratch[:2], 2); err != nil {
return 0, 0, err
}
val := uint64(binary.BigEndian.Uint16(scratch[:2]))
if val <= math.MaxUint8 {
return 0, 0, fmt.Errorf("cbor input was not canonical (lval 25 with value <= MaxUint8)")
}
return maj, val, nil
case low == 26:
scratch := make([]byte, 4)
if _, err := io.ReadAtLeast(br, scratch[:4], 4); err != nil {
return 0, 0, err
}
val := uint64(binary.BigEndian.Uint32(scratch[:4]))
if val <= math.MaxUint16 {
return 0, 0, fmt.Errorf("cbor input was not canonical (lval 26 with value <= MaxUint16)")
}
return maj, val, nil
case low == 27:
scratch := make([]byte, 8)
if _, err := io.ReadAtLeast(br, scratch, 8); err != nil {
return 0, 0, err
}
val := binary.BigEndian.Uint64(scratch)
if val <= math.MaxUint32 {
return 0, 0, fmt.Errorf("cbor input was not canonical (lval 27 with value <= MaxUint32)")
}
return maj, val, nil
default:
return 0, 0, fmt.Errorf("invalid header: (%x)", first)
}
}
func readByteBuf(r io.Reader, scratch []byte) (byte, error) {
// Reading a single byte from these buffers is much faster than copying
// into a slice.
switch r := r.(type) {
case *bytes.Buffer:
return r.ReadByte()
case *bytes.Reader:
return r.ReadByte()
case *bufio.Reader:
return r.ReadByte()
case *peeker:
return r.ReadByte()
case *CborReader:
return readByte(r.r)
case io.ByteReader:
return r.ReadByte()
}
_, err := io.ReadFull(r, scratch[:1])
return scratch[0], err
}
// same as the above, just tries to allocate less by using a passed in scratch buffer
func CborReadHeaderBuf(br io.Reader, scratch []byte) (byte, uint64, error) {
first, err := readByteBuf(br, scratch)
if err != nil {
return 0, 0, err
}
defer func() {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
}()
maj := (first & 0xe0) >> 5
low := first & 0x1f
switch {
case low < 24:
return maj, uint64(low), nil
case low == 24:
next, err := readByteBuf(br, scratch)
if err != nil {
return 0, 0, err
}
if next < 24 {
return 0, 0, fmt.Errorf("cbor input was not canonical (lval 24 with value < 24)")
}
return maj, uint64(next), nil
case low == 25:
if _, err := io.ReadAtLeast(br, scratch[:2], 2); err != nil {
return 0, 0, err
}
val := uint64(binary.BigEndian.Uint16(scratch[:2]))
if val <= math.MaxUint8 {
return 0, 0, fmt.Errorf("cbor input was not canonical (lval 25 with value <= MaxUint8)")
}
return maj, val, nil
case low == 26:
if _, err := io.ReadAtLeast(br, scratch[:4], 4); err != nil {
return 0, 0, err
}
val := uint64(binary.BigEndian.Uint32(scratch[:4]))
if val <= math.MaxUint16 {
return 0, 0, fmt.Errorf("cbor input was not canonical (lval 26 with value <= MaxUint16)")
}
return maj, val, nil
case low == 27:
if _, err := io.ReadAtLeast(br, scratch[:8], 8); err != nil {
return 0, 0, err
}
val := binary.BigEndian.Uint64(scratch[:8])
if val <= math.MaxUint32 {
return 0, 0, fmt.Errorf("cbor input was not canonical (lval 27 with value <= MaxUint32)")
}
return maj, val, nil
default:
return 0, 0, fmt.Errorf("invalid header: (%x)", first)
}
}
func CborWriteHeader(w io.Writer, t byte, l uint64) error {
return WriteMajorTypeHeader(w, t, l)
}
// TODO: No matter what I do, this function *still* allocates. Its super frustrating.
// See issue: /~https://github.com/golang/go/issues/33160
func WriteMajorTypeHeader(w io.Writer, t byte, l uint64) error {
if w, ok := w.(*CborWriter); ok {
return w.WriteMajorTypeHeader(t, l)
}
switch {
case l < 24:
_, err := w.Write([]byte{(t << 5) | byte(l)})
return err
case l < (1 << 8):
_, err := w.Write([]byte{(t << 5) | 24, byte(l)})
return err
case l < (1 << 16):
var b [3]byte
b[0] = (t << 5) | 25
binary.BigEndian.PutUint16(b[1:3], uint16(l))
_, err := w.Write(b[:])
return err
case l < (1 << 32):
var b [5]byte
b[0] = (t << 5) | 26
binary.BigEndian.PutUint32(b[1:5], uint32(l))
_, err := w.Write(b[:])
return err
default:
var b [9]byte
b[0] = (t << 5) | 27
binary.BigEndian.PutUint64(b[1:], uint64(l))
_, err := w.Write(b[:])
return err
}
}
// Same as the above, but uses a passed in buffer to avoid allocations
func WriteMajorTypeHeaderBuf(buf []byte, w io.Writer, t byte, l uint64) error {
switch {
case l < 24:
buf[0] = (t << 5) | byte(l)
_, err := w.Write(buf[:1])
return err
case l < (1 << 8):
buf[0] = (t << 5) | 24
buf[1] = byte(l)
_, err := w.Write(buf[:2])
return err
case l < (1 << 16):
buf[0] = (t << 5) | 25
binary.BigEndian.PutUint16(buf[1:3], uint16(l))
_, err := w.Write(buf[:3])
return err
case l < (1 << 32):
buf[0] = (t << 5) | 26
binary.BigEndian.PutUint32(buf[1:5], uint32(l))
_, err := w.Write(buf[:5])
return err
default:
buf[0] = (t << 5) | 27
binary.BigEndian.PutUint64(buf[1:9], uint64(l))
_, err := w.Write(buf[:9])
return err
}
}
func CborEncodeMajorType(t byte, l uint64) []byte {
switch {
case l < 24:
var b [1]byte
b[0] = (t << 5) | byte(l)
return b[:1]
case l < (1 << 8):
var b [2]byte
b[0] = (t << 5) | 24
b[1] = byte(l)
return b[:2]
case l < (1 << 16):
var b [3]byte
b[0] = (t << 5) | 25
binary.BigEndian.PutUint16(b[1:3], uint16(l))
return b[:3]
case l < (1 << 32):
var b [5]byte
b[0] = (t << 5) | 26
binary.BigEndian.PutUint32(b[1:5], uint32(l))
return b[:5]
default:
var b [9]byte
b[0] = (t << 5) | 27
binary.BigEndian.PutUint64(b[1:], uint64(l))
return b[:]
}
}
func ReadTaggedByteArray(br io.Reader, exptag uint64, maxlen uint64) (bs []byte, err error) {
maj, extra, err := CborReadHeader(br)
if err != nil {
return nil, err
}
defer func() {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
}()
if maj != MajTag {
return nil, fmt.Errorf("expected cbor type 'tag' in input")
}
if extra != exptag {
return nil, fmt.Errorf("expected tag %d", exptag)
}
return ReadByteArray(br, maxlen)
}
func ReadByteArray(br io.Reader, maxlen uint64) ([]byte, error) {
maj, extra, err := CborReadHeader(br)
if err != nil {
return nil, err
}
if maj != MajByteString {
return nil, fmt.Errorf("expected cbor type 'byte string' in input")
}
if extra > maxlen {
return nil, fmt.Errorf("string in cbor input too long, maxlen: %d", maxlen)
}
buf := make([]byte, extra)
if _, err := io.ReadAtLeast(br, buf, int(extra)); err != nil {
return nil, err
}
return buf, nil
}
// WriteByteArray encodes a byte array as a cbor byte-string.
func WriteByteArray(bw io.Writer, bytes []byte) error {
writer := NewCborWriter(bw)
if err := writer.WriteMajorTypeHeader(MajByteString, uint64(len(bytes))); err != nil {
return err
}
if _, err := writer.Write(bytes); err != nil {
return err
}
return nil
}
var stringBufPool = sync.Pool{
New: func() interface{} {
b := make([]byte, MaxLength)
return &b
},
}
func ReadString(r io.Reader) (string, error) {
return ReadStringWithMax(r, MaxLength)
}
func ReadStringWithMax(r io.Reader, maxLength uint64) (string, error) {
maj, l, err := CborReadHeader(r)
if err != nil {
return "", err
}
if maj != MajTextString {
return "", fmt.Errorf("got tag %d while reading string value (l = %d)", maj, l)
}
if l > maxLength {
return "", fmt.Errorf("string in input was too long")
}
bufp := stringBufPool.Get().(*[]byte)
if cap(*bufp) < int(l) {
*bufp = append((*bufp)[:cap(*bufp)], make([]byte, int(l)-cap(*bufp))...)
}
buf := (*bufp)[:l] // shares same backing array as pooled slice
defer func() {
// optimizes to memclr
for i := range buf {
buf[i] = 0
}
stringBufPool.Put(bufp)
}()
_, err = io.ReadAtLeast(r, buf, int(l))
if err != nil {
return "", err
}
return string(buf), nil
}
// ReadFullStringIntoBuf will read a string off the given stream, consuming the
// entire cbor item if the string on the stream is longer than the buffer,
// the string is discarded and 'false' is returned
// Note: Will only read data into the buffer if the data fits into the buffer,
// otherwise the bytes are discarded entirely
func ReadFullStringIntoBuf(cr *CborReader, buf []byte, maxLength uint64) (int, bool, error) {
maj, l, err := cr.ReadHeader()
if err != nil {
return 0, false, err
}
if maj != MajTextString {
return 0, false, fmt.Errorf("got tag %d while reading string value (l = %d)", maj, l)
}
if l > maxLength {
return 0, false, fmt.Errorf("string in input was too long")
}
if l > uint64(len(buf)) {
if err := discard(cr, int(l)); err != nil {
return 0, false, nil
}
return 0, false, nil
}
n, err := io.ReadFull(cr, buf[:l])
if err != nil {
return n, false, err
}
return int(l), true, nil
}
// Deprecated: use ReadString
func ReadStringBuf(r io.Reader, _ []byte) (string, error) {
return ReadString(r)
}
func ReadCid(br io.Reader) (cid.Cid, error) {
buf, err := ReadTaggedByteArray(br, 42, 512)
if err != nil {
return cid.Undef, err
}
return bufToCid(buf)
}
func bufToCid(buf []byte) (cid.Cid, error) {
if len(buf) == 0 {
return cid.Undef, fmt.Errorf("undefined cid")
}
if len(buf) < 2 {
return cid.Undef, fmt.Errorf("cbor serialized CIDs must have at least two bytes")
}
if buf[0] != 0 {
return cid.Undef, fmt.Errorf("cbor serialized CIDs must have binary multibase")
}
return cid.Cast(buf[1:])
}
var byteArrZero = []byte{0}
func WriteCid(w io.Writer, c cid.Cid) error {
cw := NewCborWriter(w)
if err := cw.WriteMajorTypeHeader(MajTag, 42); err != nil {
return err
}
if c == cid.Undef {
return fmt.Errorf("undefined cid")
// return CborWriteHeader(w, MajByteString, 0)
}
if err := cw.WriteMajorTypeHeader(MajByteString, uint64(c.ByteLen()+1)); err != nil {
return err
}
// that binary multibase prefix...
if _, err := cw.Write(byteArrZero); err != nil {
return err
}
if _, err := cw.WriteString(c.KeyString()); err != nil {
return err
}
return nil
}
func WriteCidBuf(buf []byte, w io.Writer, c cid.Cid) error {
if err := WriteMajorTypeHeaderBuf(buf, w, MajTag, 42); err != nil {
return err
}
if c == cid.Undef {
return fmt.Errorf("undefined cid")
// return CborWriteHeader(w, MajByteString, 0)
}
if err := WriteMajorTypeHeaderBuf(buf, w, MajByteString, uint64(c.ByteLen()+1)); err != nil {
return err
}
// that binary multibase prefix...
if _, err := w.Write(byteArrZero); err != nil {
return err
}
if _, err := c.WriteBytes(w); err != nil {
return err
}
return nil
}
// sort type example objects on name of type
func sortTypeNames(obs []any) []any {
temp := make([]tnAny, len(obs))
for i, ob := range obs {
v := reflect.ValueOf(ob)
if v.Kind() == reflect.Pointer {
v = v.Elem()
}
temp[i] = tnAny{v.Type().Name(), ob}
}
sortref := tnAnySorter(temp)
sort.Sort(&sortref)
out := make([]any, len(obs))
for i, rec := range temp {
out[i] = rec.ob
}
return out
}
// type-name and any
type tnAny struct {
name string
ob any
}
type tnAnySorter []tnAny
// sort.Interface
func (tas *tnAnySorter) Len() int {
return len(*tas)
}
func (tas *tnAnySorter) Less(i, j int) bool {
return (*tas)[i].name < (*tas)[j].name
}
func (tas *tnAnySorter) Swap(i, j int) {
t := (*tas)[i]
(*tas)[i] = (*tas)[j]
(*tas)[j] = t
}