-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcbor.go
208 lines (178 loc) · 5.9 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
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
package gofast
import "encoding/binary"
const cborMaxSmallInt = 23
const ( // major types.
cborType0 byte = iota << 5 // unsigned integer
cborType1 // negative integer
cborType2 // byte string
cborType3 // text string
cborType4 // array
cborType5 // map
cborType6 // tagged data-item
cborType7 // floating-point, simple-types and break-stop
)
const ( // associated information for type0 and type1.
// 0..23 actual value
cborInfo24 byte = iota + 24 // followed by 1-byte data-item
cborInfo25 // followed by 2-byte data-item
cborInfo26 // followed by 4-byte data-item
cborInfo27 // followed by 8-byte data-item
// 28..30 reserved
cborIndefiniteLength = 31 // for byte-string, string, arr, map
)
const ( // simple types for type7
// 0..19 unassigned
cborSimpleTypeFalse byte = iota + 20 // encodes nil type
cborSimpleTypeTrue
cborSimpleTypeNil
cborSimpleUndefined
cborSimpleTypeByte // the actual type in next byte 32..255
cborFlt16 // IEEE 754 Half-Precision Float
cborFlt32 // IEEE 754 Single-Precision Float
cborFlt64 // IEEE 754 Double-Precision Float
// 28..30 reserved
cborItemBreak = 31 // stop-code for indefinite-length items
)
const ( // pre-defined tag values
tagDateTime uint64 = iota // datetime as utf-8 string
tagEpoch // datetime as +/- int or +/- float
tagPosBignum // as []bytes
tagNegBignum // as []bytes
tagDecimalFraction // decimal fraction as array of [2]num
tagBigFloat // as array of [2]num
// tag 6 (unassigned as per spec). says frame carries a POST request.
tagPost
// tag 7 (unassigned as per spec). says frame carries a STREAM message.
tagStream
// tag 8 (unassigned as per spec). says frame carries stream FINISH.
tagFinish
// unassigned 9..20
// TODO: tagBase64URL, tagBase64, tagBase16
tagBase64URL = iota + 12 // interpret []byte as base64 format
tagBase64 // interpret []byte as base64 format
tagBase16 // interpret []byte as base16 format
tagCborEnc // embedd another CBOR message
// unassigned 25..31
tagURI = iota + 19 // defined in rfc3986
tagBase64URLEnc // base64 encoded url as text strings
tagBase64Enc // base64 encoded byte-string as text strings
tagRegexp // PCRE and ECMA262 regular expression
tagMime // MIME defined by rfc2045
// tag 43 (unassigned as per spec). says payload is encoded message
// that shall be passed on to the subscribed handler.
tagMsg = iota + 25
// tag 44 (unassigned as per spec). place-holder for "id" header key.
tagID
// tag 45 (unassigned as per spec). place-holder for "data" header key.
tagData
// tag 46 (unassigned as per spec). says payload is compressed using
// Gzip compression method.
tagGzip
// tag 47 (unassinged as per spec). says payload is compressed using
// Lzw compression method.
tagLzw
tagCborPrefix = 55799
)
// TagOpaqueStart starting value for opaque-space
const TagOpaqueStart = 266
// TagOpaqueEnd ending value for opaque-space
const TagOpaqueEnd = 15309735
var brkstp = cborHdr(cborType7, cborItemBreak)
func cborMajor(b byte) byte {
return b & 0xe0
}
func cborInfo(b byte) byte {
return b & 0x1f
}
func cborHdr(major, info byte) byte {
return (major & 0xe0) | (info & 0x1f)
}
func tag2cbor(tag uint64, buf []byte) int {
n := valuint642cbor(tag, buf)
buf[0] = (buf[0] & 0x1f) | cborType6 // fix the type as tag.
return n
}
func valuint82cbor(item byte, buf []byte) int {
if item <= cborMaxSmallInt {
buf[0] = cborHdr(cborType0, item) // 0..23
return 1
}
buf[0] = cborHdr(cborType0, cborInfo24)
buf[1] = item // 24..255
return 2
}
func valuint162cbor(item uint16, buf []byte) int {
if item < 256 {
return valuint82cbor(byte(item), buf)
}
buf[0] = cborHdr(cborType0, cborInfo25)
binary.BigEndian.PutUint16(buf[1:], item) // 256..65535
return 3
}
func valuint322cbor(item uint32, buf []byte) int {
if item < 65536 {
return valuint162cbor(uint16(item), buf) // 0..65535
}
buf[0] = cborHdr(cborType0, cborInfo26)
binary.BigEndian.PutUint32(buf[1:], item) // 65536 to 4294967295
return 5
}
func valuint642cbor(item uint64, buf []byte) int {
if item < 4294967296 {
return valuint322cbor(uint32(item), buf) // 0..4294967295
}
// 4294967296 .. 18446744073709551615
buf[0] = cborHdr(cborType0, cborInfo27)
binary.BigEndian.PutUint64(buf[1:], item)
return 9
}
func valbytes2cbor(item []byte, buf []byte) int {
n := valuint642cbor(uint64(len(item)), buf)
buf[0] = (buf[0] & 0x1f) | cborType2 // fix the type from type0->type2
copy(buf[n:], item)
return n + len(item)
}
func valtext2cbor(item string, buf []byte) int {
n := valbytes2cbor(str2bytes(item), buf)
buf[0] = (buf[0] & 0x1f) | cborType3 // fix the type from type2->type3
return n
}
func arrayStart(buf []byte) int {
// indefinite length array
buf[0] = cborHdr(cborType4, byte(cborIndefiniteLength))
return 1
}
func mapStart(buf []byte) int {
// indefinite length map
buf[0] = cborHdr(cborType5, byte(cborIndefiniteLength))
return 1
}
func breakStop(buf []byte) int {
// break stop for indefinite array or map
buf[0] = cborHdr(cborType7, byte(cborItemBreak))
return 1
}
func cborItemLength(buf []byte) (int64, int) {
if y := cborInfo(buf[0]); y < cborInfo24 {
return int64(y), 1
} else if y == cborInfo24 {
if len(buf) < 2 {
return -1, -1
}
return int64(buf[1]), 2
} else if y == cborInfo25 {
if len(buf) < 3 {
return -1, -1
}
return int64(binary.BigEndian.Uint16(buf[1:])), 3
} else if y == cborInfo26 {
if len(buf) < 5 {
return -1, -1
}
return int64(binary.BigEndian.Uint32(buf[1:])), 5
}
if len(buf) < 9 {
return -1, -1
}
return int64(binary.BigEndian.Uint64(buf[1:])), 9 // info27
}