-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
cbor.go
117 lines (105 loc) · 2.98 KB
/
cbor.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
package cbor
import (
"bytes"
"fmt"
"math/big"
"github.com/fxamacker/cbor/v2"
"github.com/pkg/errors"
)
// ParseDietCBOR attempts to coerce the input byte array into valid CBOR.
// Assumes the input is "diet" CBOR which is like CBOR, except:
// 1. It is guaranteed to always be a map
// 2. It may or may not include the opening and closing markers "{}"
func ParseDietCBOR(b []byte) (map[string]interface{}, error) {
b = autoAddMapDelimiters(b)
var m map[interface{}]interface{}
if err := cbor.Unmarshal(b, &m); err != nil {
return nil, err
}
coerced, err := CoerceInterfaceMapToStringMap(m)
if err != nil {
return nil, err
}
output, ok := coerced.(map[string]interface{})
if !ok {
return nil, errors.New("cbor data cannot be coerced to map")
}
return output, nil
}
// Similar to ParseDietCBOR but outputs to a concrete struct, which meets the
// "top-level map" requirement of "diet" CBOR.
func ParseDietCBORToStruct(b []byte, v interface{}) error {
b = autoAddMapDelimiters(b)
return cbor.Unmarshal(b, v)
}
// ParseStandardCBOR parses CBOR in "standards compliant" mode.
// Literal values are passed through "as-is".
// The input is not assumed to be a map.
// Empty inputs will return nil.
func ParseStandardCBOR(b []byte) (a interface{}, err error) {
if len(b) == 0 {
return nil, nil
}
if err = cbor.Unmarshal(b, &a); err != nil {
return nil, err
}
return
}
// Automatically add missing start map and end map to a CBOR encoded buffer
func autoAddMapDelimiters(b []byte) []byte {
if len(b) == 0 || (len(b) > 1 && (b[0]>>5) != 5) {
var buffer bytes.Buffer
buffer.Write([]byte{0xbf})
buffer.Write(b)
buffer.Write([]byte{0xff})
return buffer.Bytes()
}
return b
}
// CoerceInterfaceMapToStringMap converts map[interface{}]interface{} (interface maps) to
// map[string]interface{} (string maps) and []interface{} with interface maps to string maps.
// Relevant when serializing between CBOR and JSON.
//
// It also handles the CBOR 'bignum' type as documented here: https://tools.ietf.org/html/rfc7049#section-2.4.2
func CoerceInterfaceMapToStringMap(in interface{}) (interface{}, error) {
switch typed := in.(type) {
case map[string]interface{}:
for k, v := range typed {
coerced, err := CoerceInterfaceMapToStringMap(v)
if err != nil {
return nil, err
}
typed[k] = coerced
}
return typed, nil
case map[interface{}]interface{}:
m := map[string]interface{}{}
for k, v := range typed {
coercedKey, ok := k.(string)
if !ok {
return nil, fmt.Errorf("unable to coerce key %T %v to a string", k, k)
}
coerced, err := CoerceInterfaceMapToStringMap(v)
if err != nil {
return nil, err
}
m[coercedKey] = coerced
}
return m, nil
case []interface{}:
r := make([]interface{}, len(typed))
for i, v := range typed {
coerced, err := CoerceInterfaceMapToStringMap(v)
if err != nil {
return nil, err
}
r[i] = coerced
}
return r, nil
case big.Int:
value, _ := (in).(big.Int)
return &value, nil
default:
return in, nil
}
}