-
Notifications
You must be signed in to change notification settings - Fork 37
/
cbor_bytes.go
51 lines (39 loc) · 894 Bytes
/
cbor_bytes.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
package abi
import (
"fmt"
"io"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
)
type CborBytes []byte
func (t *CborBytes) MarshalCBOR(w io.Writer) error {
if len(*t) > cbg.ByteArrayMaxLen {
return xerrors.Errorf("byte array was too long")
}
if err := cbg.WriteMajorTypeHeader(w, cbg.MajByteString, uint64(len(*t))); err != nil {
return err
}
_, err := w.Write((*t)[:])
return err
}
func (t *CborBytes) UnmarshalCBOR(r io.Reader) error {
br := cbg.GetPeeker(r)
maj, extra, err := cbg.CborReadHeader(br)
if err != nil {
return err
}
if extra > cbg.ByteArrayMaxLen {
return fmt.Errorf("byte array too large (%d)", extra)
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
}
if extra > 0 {
ret := make([]byte, extra)
if _, err := io.ReadFull(br, ret[:]); err != nil {
return err
}
*t = ret
}
return nil
}