-
Notifications
You must be signed in to change notification settings - Fork 4
/
cbor_types.go
70 lines (58 loc) · 1.35 KB
/
cbor_types.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
package types
import (
"bytes"
"fmt"
"io"
cbg "github.com/whyrusleeping/cbor-gen"
)
type CborString string
func (cb CborString) MarshalCBOR(w io.Writer) error {
if len(cb) > cbg.MaxLength {
return fmt.Errorf("cborstring exceed max length")
}
if err := cbg.WriteMajorTypeHeader(w, cbg.MajTextString, uint64(len(cb))); err != nil {
return err
}
if _, err := io.WriteString(w, string(cb)); err != nil {
return err
}
return nil
}
func (cb *CborString) UnmarshalCBOR(r io.Reader) error {
str, err := cbg.ReadString(r)
if err != nil {
return err
}
*cb = CborString(str)
return nil
}
type CborInt = cbg.CborInt
type CborUint uint64
func (cb CborUint) MarshalCBOR(w io.Writer) error {
cw := cbg.NewCborWriter(w)
return cw.WriteMajorTypeHeader(cbg.MajUnsignedInt, uint64(cb))
}
func (cb *CborUint) UnmarshalCBOR(r io.Reader) error {
cr := cbg.NewCborReader(r)
maj, extra, err := cr.ReadHeader()
if err != nil {
return err
}
if maj != cbg.MajUnsignedInt {
return fmt.Errorf("wrong type for uint64 field")
}
*cb = CborUint(extra)
return nil
}
// CBORBytes Wraps already-serialized bytes as CBOR-marshalable.
type CBORBytes []byte
func (b CBORBytes) MarshalCBOR(w io.Writer) error {
_, err := w.Write(b)
return err
}
func (b *CBORBytes) UnmarshalCBOR(r io.Reader) error {
var c bytes.Buffer
_, err := c.ReadFrom(r)
*b = c.Bytes()
return err
}