-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmw.go
223 lines (176 loc) · 4.64 KB
/
cmw.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
// Copyright 2023 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package cmw
import (
"encoding/json"
"errors"
"fmt"
"github.com/fxamacker/cbor/v2"
)
type Serialization uint
const (
UnknownSerialization = Serialization(iota)
JSONArray
CBORArray
CBORTag
)
// a CMW object holds the internal representation of a RATS conceptual message
// wrapper
type CMW struct {
typ Type
val Value
ind Indicator
serialization Serialization
}
func (o *CMW) SetMediaType(v string) { _ = o.typ.Set(v) }
func (o *CMW) SetContentFormat(v uint16) { _ = o.typ.Set(v) }
func (o *CMW) SetTagNumber(v uint64) { _ = o.typ.Set(v) }
func (o *CMW) SetValue(v []byte) { _ = o.val.Set(v) }
func (o *CMW) SetIndicators(indicators ...Indicator) {
var v Indicator
for _, ind := range indicators {
v.Set(ind)
}
o.ind = v
}
func (o *CMW) SetSerialization(s Serialization) { o.serialization = s }
func (o CMW) GetValue() []byte { return o.val }
func (o CMW) GetType() string { return o.typ.String() }
func (o CMW) GetIndicator() Indicator { return o.ind }
func (o CMW) GetSerialization() Serialization { return o.serialization }
// Deserialize a CMW
func (o *CMW) Deserialize(b []byte) error {
s := sniff(b)
o.serialization = s
switch s {
case JSONArray:
return o.UnmarshalJSON(b)
case CBORArray, CBORTag:
return o.UnmarshalCBOR(b)
}
return errors.New("unknown CMW format")
}
// Serialize a CMW according to its provided Serialization
func (o CMW) Serialize() ([]byte, error) {
s := o.serialization
switch s {
case JSONArray:
return o.MarshalJSON()
case CBORArray, CBORTag:
return o.MarshalCBOR()
}
return nil, fmt.Errorf("invalid serialization format %d", s)
}
func (o CMW) MarshalJSON() ([]byte, error) { return arrayEncode(json.Marshal, &o) }
func (o CMW) MarshalCBOR() ([]byte, error) {
s := o.serialization
switch s {
case CBORArray:
return arrayEncode(cbor.Marshal, &o)
case CBORTag:
return o.encodeCBORTag()
}
return nil, fmt.Errorf("invalid serialization format: want CBORArray or CBORTag, got %d", s)
}
func (o CMW) encodeCBORTag() ([]byte, error) {
var (
tag cbor.RawTag
err error
)
if !o.typ.IsSet() || !o.val.IsSet() {
return nil, fmt.Errorf("type and value MUST be set in CMW")
}
tag.Number, err = o.typ.TagNumber()
if err != nil {
return nil, fmt.Errorf("getting a suitable tag value: %w", err)
}
tag.Content, err = cbor.Marshal(o.val)
if err != nil {
return nil, fmt.Errorf("marshaling tag value: %w", err)
}
return tag.MarshalCBOR()
}
func (o *CMW) UnmarshalCBOR(b []byte) error {
if arrayDecode[cbor.RawMessage](cbor.Unmarshal, b, o) == nil {
o.serialization = CBORArray
return nil
}
if o.decodeCBORTag(b) == nil {
// the serialization attribute is set by decodeCBORTag
return nil
}
return errors.New("invalid CBOR-encoded CMW")
}
func (o *CMW) UnmarshalJSON(b []byte) error {
err := arrayDecode[json.RawMessage](json.Unmarshal, b, o)
o.serialization = JSONArray
return err
}
func (o *CMW) decodeCBORTag(b []byte) error {
var (
v cbor.RawTag
m []byte
err error
)
if err = v.UnmarshalCBOR(b); err != nil {
return fmt.Errorf("unmarshal CMW CBOR Tag: %w", err)
}
if err = cbor.Unmarshal(v.Content, &m); err != nil {
return fmt.Errorf("unmarshal CMW CBOR Tag bstr-wrapped value: %w", err)
}
_ = o.typ.Set(v.Number)
_ = o.val.Set(m)
o.serialization = CBORTag
return nil
}
func sniff(b []byte) Serialization {
if len(b) == 0 {
return UnknownSerialization
}
if b[0] == 0x82 || b[0] == 0x83 {
return CBORArray
} else if b[0] >= 0xc0 && b[0] <= 0xdb {
return CBORTag
} else if b[0] == 0x5b {
return JSONArray
}
return UnknownSerialization
}
type (
arrayDecoder func([]byte, any) error
arrayEncoder func(any) ([]byte, error)
)
func arrayDecode[V json.RawMessage | cbor.RawMessage](
dec arrayDecoder, b []byte, o *CMW,
) error {
var a []V
if err := dec(b, &a); err != nil {
return err
}
alen := len(a)
if alen < 2 || alen > 3 {
return fmt.Errorf("wrong number of entries (%d) in the CMW array", alen)
}
if err := dec(a[0], &o.typ); err != nil {
return fmt.Errorf("unmarshaling type: %w", err)
}
if err := dec(a[1], &o.val); err != nil {
return fmt.Errorf("unmarshaling value: %w", err)
}
if alen == 3 {
if err := dec(a[2], &o.ind); err != nil {
return fmt.Errorf("unmarshaling indicator: %w", err)
}
}
return nil
}
func arrayEncode(enc arrayEncoder, o *CMW) ([]byte, error) {
if !o.typ.IsSet() || !o.val.IsSet() {
return nil, fmt.Errorf("type and value MUST be set in CMW")
}
a := []any{o.typ, o.val}
if !o.ind.Empty() {
a = append(a, o.ind)
}
return enc(a)
}