/
rencode_generated.go
356 lines (347 loc) · 7.58 KB
/
rencode_generated.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//
// go-rencode v0.1.8 - Go implementation of rencode - fast (basic)
// object serialization similar to bencode
// Copyright (C) 2015~2019 gdm85 - https://github.com/gdm85/go-rencode/
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package rencode
import (
"fmt"
"math"
"math/big"
)
func (r *Encoder) encodeSingle(data interface{}) error {
if data == nil {
return r.EncodeNone()
}
switch x := data.(type) {
case big.Int:
s := x.String()
if len(s) > MAX_INT_LENGTH {
return fmt.Errorf("Number is longer than %d characters", MAX_INT_LENGTH)
}
return r.EncodeBigNumber(s)
case List:
if x.Length() < LIST_FIXED_COUNT {
_, err := r.w.Write([]byte{byte(LIST_FIXED_START + x.Length())})
if err != nil {
return err
}
for _, v := range x.Values() {
err = r.Encode(v)
if err != nil {
return err
}
}
return nil
}
_, err := r.w.Write([]byte{byte(CHR_LIST)})
if err != nil {
return err
}
for _, v := range x.Values() {
err = r.Encode(v)
if err != nil {
return err
}
}
_, err = r.w.Write([]byte{byte(CHR_TERM)})
return err
case Dictionary:
if x.Length() < DICT_FIXED_COUNT {
_, err := r.w.Write([]byte{byte(DICT_FIXED_START + x.Length())})
if err != nil {
return err
}
keys := x.Keys()
for i, v := range x.Values() {
err = r.Encode(keys[i])
if err != nil {
return err
}
err = r.Encode(v)
if err != nil {
return err
}
}
return nil
}
_, err := r.w.Write([]byte{byte(CHR_DICT)})
if err != nil {
return err
}
keys := x.Keys()
for i, v := range x.Values() {
err = r.Encode(keys[i])
if err != nil {
return err
}
err = r.Encode(v)
if err != nil {
return err
}
}
_, err = r.w.Write([]byte{byte(CHR_TERM)})
return err
case bool:
return r.EncodeBool(x)
case float32:
return r.EncodeFloat32(x)
case float64:
return r.EncodeFloat64(x)
case []byte:
return r.EncodeBytes(x)
case string:
// all strings will be treated as byte arrays
return r.EncodeBytes([]byte(x))
case int8:
return r.EncodeInt8(x)
case int:
if math.MinInt8 <= x && x <= math.MaxInt8 {
return r.EncodeInt8(int8(x))
}
if math.MinInt16 <= x && x <= math.MaxInt16 {
return r.EncodeInt16(int16(x))
}
if math.MinInt32 <= x && x <= math.MaxInt32 {
return r.EncodeInt32(int32(x))
}
return r.EncodeInt64(int64(x))
case uint8:
if x <= math.MaxInt8 {
return r.EncodeInt8(int8(x))
}
case uint16:
if x <= math.MaxInt8 {
return r.EncodeInt8(int8(x))
}
if x <= math.MaxInt16 {
return r.EncodeInt16(int16(x))
}
case int16:
if math.MinInt8 <= x && x <= math.MaxInt8 {
return r.EncodeInt8(int8(x))
}
return r.EncodeInt16(int16(x))
case uint32:
if x <= math.MaxInt8 {
return r.EncodeInt8(int8(x))
}
if x <= math.MaxInt16 {
return r.EncodeInt16(int16(x))
}
if x <= math.MaxInt32 {
return r.EncodeInt32(int32(x))
}
case int32:
if math.MinInt8 <= x && x <= math.MaxInt8 {
return r.EncodeInt8(int8(x))
}
if math.MinInt16 <= x && x <= math.MaxInt16 {
return r.EncodeInt16(int16(x))
}
return r.EncodeInt32(int32(x))
case int64:
if math.MinInt8 <= x && x <= math.MaxInt8 {
return r.EncodeInt8(int8(x))
}
if math.MinInt16 <= x && x <= math.MaxInt16 {
return r.EncodeInt16(int16(x))
}
if math.MinInt32 <= x && x <= math.MaxInt32 {
return r.EncodeInt32(int32(x))
}
return r.EncodeInt64(int64(x))
case uint64, uint:
s := fmt.Sprintf("%d", data)
if len(s) > MAX_INT_LENGTH {
return fmt.Errorf("Number is longer than %d characters", MAX_INT_LENGTH)
}
return r.EncodeBigNumber(s)
default:
return fmt.Errorf("could not encode data of type %T", data)
}
panic("unexpected fallthrough")
}
func convertAssignInteger(src, dest interface{}) error {
switch sv := src.(type) {
case big.Int:
switch dv := dest.(type) {
case *big.Int:
*dv = sv
return nil
}
case int64:
switch dv := dest.(type) {
case *int64:
*dv = sv
return nil
case *int16:
if sv > math.MaxInt16 || sv < math.MinInt16 {
return ConversionOverflow{"int64", "int16"}
}
*dv = int16(sv)
return nil
case *int32:
if sv > math.MaxInt32 || sv < math.MinInt32 {
return ConversionOverflow{"int64", "int32"}
}
*dv = int32(sv)
return nil
case *int:
*dv = int(sv)
return nil
case *int8:
if sv > math.MaxInt8 || sv < math.MinInt8 {
return ConversionOverflow{"int64", "int8"}
}
*dv = int8(sv)
return nil
}
case int:
switch dv := dest.(type) {
case *int:
*dv = sv
return nil
case *int8:
if sv > math.MaxInt8 || sv < math.MinInt8 {
return ConversionOverflow{"int", "int8"}
}
*dv = int8(sv)
return nil
case *int16:
if sv > math.MaxInt16 || sv < math.MinInt16 {
return ConversionOverflow{"int", "int16"}
}
*dv = int16(sv)
return nil
case *int32:
if sv > math.MaxInt32 || sv < math.MinInt32 {
return ConversionOverflow{"int", "int32"}
}
*dv = int32(sv)
return nil
case *int64:
*dv = int64(sv)
return nil
}
case int8:
switch dv := dest.(type) {
case *int8:
*dv = sv
return nil
case *int64:
*dv = int64(sv)
return nil
case *int:
*dv = int(sv)
return nil
case *int16:
*dv = int16(sv)
return nil
case *int32:
*dv = int32(sv)
return nil
}
case uint8:
switch dv := dest.(type) {
case *uint8:
*dv = sv
return nil
case *uint16:
*dv = uint16(sv)
return nil
case *uint32:
*dv = uint32(sv)
return nil
}
case uint16:
switch dv := dest.(type) {
case *uint16:
*dv = sv
return nil
case *uint8:
if sv > math.MaxUint8 {
return ConversionOverflow{"uint16", "uint8"}
}
*dv = uint8(sv)
return nil
case *uint32:
*dv = uint32(sv)
return nil
}
case int16:
switch dv := dest.(type) {
case *int16:
*dv = sv
return nil
case *int32:
*dv = int32(sv)
return nil
case *int64:
*dv = int64(sv)
return nil
case *int:
*dv = int(sv)
return nil
case *int8:
if sv > math.MaxInt8 || sv < math.MinInt8 {
return ConversionOverflow{"int16", "int8"}
}
*dv = int8(sv)
return nil
}
case uint32:
switch dv := dest.(type) {
case *uint32:
*dv = sv
return nil
case *uint8:
if sv > math.MaxUint8 {
return ConversionOverflow{"uint32", "uint8"}
}
*dv = uint8(sv)
return nil
case *uint16:
if sv > math.MaxUint16 {
return ConversionOverflow{"uint32", "uint16"}
}
*dv = uint16(sv)
return nil
}
case int32:
switch dv := dest.(type) {
case *int32:
*dv = sv
return nil
case *int:
*dv = int(sv)
return nil
case *int8:
if sv > math.MaxInt8 || sv < math.MinInt8 {
return ConversionOverflow{"int32", "int8"}
}
*dv = int8(sv)
return nil
case *int16:
if sv > math.MaxInt16 || sv < math.MinInt16 {
return ConversionOverflow{"int32", "int16"}
}
*dv = int16(sv)
return nil
case *int64:
*dv = int64(sv)
return nil
}
}
return fmt.Errorf("cannot convert from %T into %T", src, dest)
}