-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclone_cbor.go
301 lines (263 loc) · 8.72 KB
/
clone_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package gson
func cborclone(in, out []byte, config *Config) int {
return cborcloneM[in[0]](in, out, config)
}
func cborclonet0smallint(in, out []byte, config *Config) int {
out[0] = in[0]
return 1
}
func cborclonet0info24(in, out []byte, config *Config) int {
return copy(out, in[:2])
}
func cborclonet0info25(in, out []byte, config *Config) int {
return copy(out, in[:3])
}
func cborclonet0info26(in, out []byte, config *Config) int {
return copy(out, in[:5])
}
func cborclonet0info27(in, out []byte, config *Config) int {
return copy(out, in[:9])
}
func cborclonet1smallint(in, out []byte, config *Config) int {
out[0] = in[0]
return 1
}
func cborclonet1info24(in, out []byte, config *Config) int {
return copy(out, in[:2])
}
func cborclonet1info25(in, out []byte, config *Config) int {
return copy(out, in[:3])
}
func cborclonet1info26(in, out []byte, config *Config) int {
return copy(out, in[:5])
}
func cborclonet1info27(in, out []byte, config *Config) int {
return copy(out, in[:9])
}
func cborclonet2(in, out []byte, config *Config) int {
ln, n := cborItemLength(in)
return copy(out, in[:n+ln])
}
func cborclonet2indefinite(in, out []byte, config *Config) int {
out[0] = in[0]
n := 1
for in[n] != brkstp {
n += cborclone(in[n:], out[n:], config)
}
out[n] = in[n]
return n + 1
}
func cborclonet3(in, out []byte, config *Config) int {
ln, n := cborItemLength(in)
return copy(out, in[:n+ln])
}
func cborclonet3indefinite(in, out []byte, config *Config) int {
out[0] = in[0]
n := 1
for in[n] != brkstp {
n += cborclone(in[n:], out[n:], config)
}
out[n] = in[n]
return n + 1
}
func cborclonet4(in, out []byte, config *Config) int {
ln, n := cborItemLength(in)
copy(out, in[:n])
for i := 0; i < ln; i++ {
n += cborclone(in[n:], out[n:], config)
}
return n
}
func cborclonet4indefinite(in, out []byte, config *Config) int {
out[0] = in[0]
n := 1
for in[n] != brkstp {
n += cborclone(in[n:], out[n:], config)
}
out[n] = in[n]
return n + 1
}
func cborclonet5(in, out []byte, config *Config) int {
ln, n := cborItemLength(in)
copy(out, in[:n])
for i := 0; i < ln; i++ {
n += cborclone(in[n:], out[n:], config)
n += cborclone(in[n:], out[n:], config)
}
return n
}
func cborclonet5indefinite(in, out []byte, config *Config) int {
out[0] = in[0]
n := 1
for in[n] != brkstp {
n += cborclone(in[n:], out[n:], config)
n += cborclone(in[n:], out[n:], config)
}
out[n] = in[n]
return n + 1
}
func cborcloneFalse(in, out []byte, config *Config) int {
out[0] = in[0]
return 1
}
func cborcloneTrue(in, out []byte, config *Config) int {
out[0] = in[0]
return 1
}
func cborcloneNull(in, out []byte, config *Config) int {
out[0] = in[0]
return 1
}
func cborcloneUndefined(in, out []byte, config *Config) int {
out[0] = in[0]
return 1
}
func cborcloneSimpleType(in, out []byte, config *Config) int {
out[0], out[1] = in[0], in[1]
return 2
}
func cborcloneFloat16(in, out []byte, config *Config) int {
return copy(out, in[:3])
}
func cborcloneFloat32(in, out []byte, config *Config) int {
return copy(out, in[:5])
}
func cborcloneFloat64(in, out []byte, config *Config) int {
return copy(out, in[:9])
}
func cborcloneTag(in, out []byte, config *Config) int {
byt := (in[0] & 0x1f) | cborType0 // fix as positive num
in[0] = byt
n := cborclone(in, out, config)
in[0], out[0] = byt, byt
n += cborclone(in[n:], out[n:], config)
return n
}
var cborcloneM = make(map[byte]func([]byte, []byte, *Config) int)
func init() {
makePanic := func(msg string) func([]byte, []byte, *Config) int {
return func(_, _ []byte, _ *Config) int { panic(msg) }
}
//-- type0 (unsigned integer)
// 1st-byte 0..23
for i := byte(0); i < cborInfo24; i++ {
cborcloneM[cborHdr(cborType0, i)] = cborclonet0smallint
}
// 1st-byte 24..27
cborcloneM[cborHdr(cborType0, cborInfo24)] = cborclonet0info24
cborcloneM[cborHdr(cborType0, cborInfo25)] = cborclonet0info25
cborcloneM[cborHdr(cborType0, cborInfo26)] = cborclonet0info26
cborcloneM[cborHdr(cborType0, cborInfo27)] = cborclonet0info27
// 1st-byte 28..31
msg := "cbor decode value type0 reserved info"
cborcloneM[cborHdr(cborType0, 28)] = makePanic(msg)
cborcloneM[cborHdr(cborType0, 29)] = makePanic(msg)
cborcloneM[cborHdr(cborType0, 30)] = makePanic(msg)
msg = "cbor decode value type0 indefnite"
cborcloneM[cborHdr(cborType0, cborIndefiniteLength)] = makePanic(msg)
//-- type1 (signed integer)
// 1st-byte 0..23
for i := byte(0); i < cborInfo24; i++ {
cborcloneM[cborHdr(cborType1, i)] = cborclonet1smallint
}
// 1st-byte 24..27
cborcloneM[cborHdr(cborType1, cborInfo24)] = cborclonet1info24
cborcloneM[cborHdr(cborType1, cborInfo25)] = cborclonet1info25
cborcloneM[cborHdr(cborType1, cborInfo26)] = cborclonet1info26
cborcloneM[cborHdr(cborType1, cborInfo27)] = cborclonet1info27
// 1st-byte 28..31
msg = "cbor decode value type1 reserved info"
cborcloneM[cborHdr(cborType1, 28)] = makePanic(msg)
cborcloneM[cborHdr(cborType1, 29)] = makePanic(msg)
cborcloneM[cborHdr(cborType1, 30)] = makePanic(msg)
msg = "cbor decode value type1 indefnite"
cborcloneM[cborHdr(cborType1, cborIndefiniteLength)] = makePanic(msg)
//-- type2 (byte string)
// 1st-byte 0..27
for i := 0; i < 28; i++ {
cborcloneM[cborHdr(cborType2, byte(i))] = cborclonet2
}
// 1st-byte 28..31
msg = "cbor decode value type2 reserved info"
cborcloneM[cborHdr(cborType2, 28)] = makePanic(msg)
cborcloneM[cborHdr(cborType2, 29)] = makePanic(msg)
cborcloneM[cborHdr(cborType2, 30)] = makePanic(msg)
msg = "cbor decode value type2 indefnite"
cborcloneM[cborHdr(cborType2, cborIndefiniteLength)] = cborclonet2indefinite
//-- type3 (string)
// 1st-byte 0..27
for i := 0; i < 28; i++ {
cborcloneM[cborHdr(cborType3, byte(i))] = cborclonet3
}
// 1st-byte 28..31
msg = "cbor decode value type3 reserved info"
cborcloneM[cborHdr(cborType3, 28)] = makePanic(msg)
cborcloneM[cborHdr(cborType3, 29)] = makePanic(msg)
cborcloneM[cborHdr(cborType3, 30)] = makePanic(msg)
cborcloneM[cborHdr(cborType3, cborIndefiniteLength)] = cborclonet3indefinite
//-- type4 (array)
// 1st-byte 0..27
for i := 0; i < 28; i++ {
cborcloneM[cborHdr(cborType4, byte(i))] = cborclonet4
}
// 1st-byte 28..31
msg = "cbor decode value type4 reserved info"
cborcloneM[cborHdr(cborType4, 28)] = makePanic(msg)
cborcloneM[cborHdr(cborType4, 29)] = makePanic(msg)
cborcloneM[cborHdr(cborType4, 30)] = makePanic(msg)
cborcloneM[cborHdr(cborType4, cborIndefiniteLength)] = cborclonet4indefinite
//-- type5 (map)
// 1st-byte 0..27
for i := 0; i < 28; i++ {
cborcloneM[cborHdr(cborType5, byte(i))] = cborclonet5
}
// 1st-byte 28..31
msg = "cbor decode value type5 reserved info"
cborcloneM[cborHdr(cborType5, 28)] = makePanic(msg)
cborcloneM[cborHdr(cborType5, 29)] = makePanic(msg)
cborcloneM[cborHdr(cborType5, 30)] = makePanic(msg)
cborcloneM[cborHdr(cborType5, cborIndefiniteLength)] = cborclonet5indefinite
//-- type6
// 1st-byte 0..23
for i := byte(0); i < cborInfo24; i++ {
cborcloneM[cborHdr(cborType6, i)] = cborcloneTag
}
// 1st-byte 24..27
cborcloneM[cborHdr(cborType6, cborInfo24)] = cborcloneTag
cborcloneM[cborHdr(cborType6, cborInfo25)] = cborcloneTag
cborcloneM[cborHdr(cborType6, cborInfo26)] = cborcloneTag
cborcloneM[cborHdr(cborType6, cborInfo27)] = cborcloneTag
// 1st-byte 28..31
msg = "cbor decode value type6 reserved info"
cborcloneM[cborHdr(cborType6, 28)] = makePanic(msg)
cborcloneM[cborHdr(cborType6, 29)] = makePanic(msg)
cborcloneM[cborHdr(cborType6, 30)] = makePanic(msg)
msg = "cbor decode value type6 indefnite"
cborcloneM[cborHdr(cborType6, cborIndefiniteLength)] = makePanic(msg)
//-- type7 (simple types / floats / break-stop)
// 1st-byte 0..19
for i := byte(0); i < 20; i++ {
cborcloneM[cborHdr(cborType7, i)] =
func(i byte) func([]byte, []byte, *Config) int {
return func(in, out []byte, _ *Config) int {
out[0] = in[0]
return 1
}
}(i)
}
// 1st-byte 20..23
cborcloneM[cborHdr(cborType7, cborSimpleTypeFalse)] = cborcloneFalse
cborcloneM[cborHdr(cborType7, cborSimpleTypeTrue)] = cborcloneTrue
cborcloneM[cborHdr(cborType7, cborSimpleTypeNil)] = cborcloneNull
cborcloneM[cborHdr(cborType7, cborSimpleUndefined)] = cborcloneUndefined
cborcloneM[cborHdr(cborType7, cborSimpleTypeByte)] = cborcloneSimpleType
cborcloneM[cborHdr(cborType7, cborFlt16)] = cborcloneFloat16
cborcloneM[cborHdr(cborType7, cborFlt32)] = cborcloneFloat32
cborcloneM[cborHdr(cborType7, cborFlt64)] = cborcloneFloat64
// 1st-byte 28..31
msg = "cbor decode value type7 simple type"
cborcloneM[cborHdr(cborType7, 28)] = makePanic(msg)
cborcloneM[cborHdr(cborType7, 29)] = makePanic(msg)
cborcloneM[cborHdr(cborType7, 30)] = makePanic(msg)
cborcloneM[cborHdr(cborType7, cborItemBreak)] = makePanic(msg)
}