This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
int.go
447 lines (386 loc) · 8.54 KB
/
int.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package wire
import (
"encoding/binary"
"errors"
"io"
)
// Bool
func WriteBool(b bool, w io.Writer, n *int, err *error) {
var bb byte
if b {
bb = 0x01
} else {
bb = 0x00
}
WriteTo([]byte{bb}, w, n, err)
}
func ReadBool(r io.Reader, n *int, err *error) bool {
var buf [1]byte
ReadFull(buf[:], r, n, err)
switch buf[0] {
case 0x00:
return false
case 0x01:
return true
default:
setFirstErr(err, errors.New("Invalid bool"))
return false
}
}
func PutBool(buf []byte, b bool) {
if b {
buf[0] = 0x01
} else {
buf[0] = 0x00
}
}
func GetBool(buf []byte) (bool, error) {
switch buf[0] {
case 0x00:
return false, nil
case 0x01:
return true, nil
default:
return false, errors.New("Invalid bool")
}
}
// Byte
func WriteByte(b byte, w io.Writer, n *int, err *error) {
WriteTo([]byte{b}, w, n, err)
}
func ReadByte(r io.Reader, n *int, err *error) byte {
var buf [1]byte
ReadFull(buf[:], r, n, err)
return buf[0]
}
// Int8
func WriteInt8(i int8, w io.Writer, n *int, err *error) {
WriteByte(byte(i), w, n, err)
}
func ReadInt8(r io.Reader, n *int, err *error) int8 {
return int8(ReadByte(r, n, err))
}
// Uint8
func WriteUint8(i uint8, w io.Writer, n *int, err *error) {
WriteByte(byte(i), w, n, err)
}
func ReadUint8(r io.Reader, n *int, err *error) uint8 {
return uint8(ReadByte(r, n, err))
}
// Int16
func WriteInt16(i int16, w io.Writer, n *int, err *error) {
var buf [2]byte
binary.BigEndian.PutUint16(buf[:], uint16(i))
WriteTo(buf[:], w, n, err)
}
func ReadInt16(r io.Reader, n *int, err *error) int16 {
var buf [2]byte
ReadFull(buf[:], r, n, err)
return int16(binary.BigEndian.Uint16(buf[:]))
}
func PutInt16(buf []byte, i int16) {
binary.BigEndian.PutUint16(buf, uint16(i))
}
func GetInt16(buf []byte) int16 {
return int16(binary.BigEndian.Uint16(buf))
}
// Uint16
func WriteUint16(i uint16, w io.Writer, n *int, err *error) {
var buf [2]byte
binary.BigEndian.PutUint16(buf[:], uint16(i))
WriteTo(buf[:], w, n, err)
}
func ReadUint16(r io.Reader, n *int, err *error) uint16 {
var buf [2]byte
ReadFull(buf[:], r, n, err)
return uint16(binary.BigEndian.Uint16(buf[:]))
}
func PutUint16(buf []byte, i uint16) {
binary.BigEndian.PutUint16(buf, i)
}
func GetUint16(buf []byte) uint16 {
return binary.BigEndian.Uint16(buf)
}
// []Uint16
func WriteUint16s(iz []uint16, w io.Writer, n *int, err *error) {
WriteUint32(uint32(len(iz)), w, n, err)
for _, i := range iz {
WriteUint16(i, w, n, err)
if *err != nil {
return
}
}
}
func ReadUint16s(r io.Reader, n *int, err *error) []uint16 {
length := ReadUint32(r, n, err)
if *err != nil {
return nil
}
iz := make([]uint16, length)
for j := uint32(0); j < length; j++ {
ii := ReadUint16(r, n, err)
if *err != nil {
return nil
}
iz[j] = ii
}
return iz
}
// Int32
func WriteInt32(i int32, w io.Writer, n *int, err *error) {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], uint32(i))
WriteTo(buf[:], w, n, err)
}
func ReadInt32(r io.Reader, n *int, err *error) int32 {
var buf [4]byte
ReadFull(buf[:], r, n, err)
return int32(binary.BigEndian.Uint32(buf[:]))
}
func PutInt32(buf []byte, i int32) {
binary.BigEndian.PutUint32(buf, uint32(i))
}
func GetInt32(buf []byte) int32 {
return int32(binary.BigEndian.Uint32(buf))
}
// Uint32
func WriteUint32(i uint32, w io.Writer, n *int, err *error) {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], uint32(i))
WriteTo(buf[:], w, n, err)
}
func ReadUint32(r io.Reader, n *int, err *error) uint32 {
var buf [4]byte
ReadFull(buf[:], r, n, err)
return uint32(binary.BigEndian.Uint32(buf[:]))
}
func PutUint32(buf []byte, i uint32) {
binary.BigEndian.PutUint32(buf, i)
}
func GetUint32(buf []byte) uint32 {
return binary.BigEndian.Uint32(buf)
}
// Int64
func WriteInt64(i int64, w io.Writer, n *int, err *error) {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], uint64(i))
WriteTo(buf[:], w, n, err)
}
func ReadInt64(r io.Reader, n *int, err *error) int64 {
var buf [8]byte
ReadFull(buf[:], r, n, err)
return int64(binary.BigEndian.Uint64(buf[:]))
}
func PutInt64(buf []byte, i int64) {
binary.BigEndian.PutUint64(buf, uint64(i))
}
func GetInt64(buf []byte) int64 {
return int64(binary.BigEndian.Uint64(buf))
}
// Uint64
func WriteUint64(i uint64, w io.Writer, n *int, err *error) {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], uint64(i))
WriteTo(buf[:], w, n, err)
}
func ReadUint64(r io.Reader, n *int, err *error) uint64 {
var buf [8]byte
ReadFull(buf[:], r, n, err)
return uint64(binary.BigEndian.Uint64(buf[:]))
}
func PutUint64(buf []byte, i uint64) {
binary.BigEndian.PutUint64(buf, i)
}
func GetUint64(buf []byte) uint64 {
return binary.BigEndian.Uint64(buf)
}
// Varint
func UvarintSize(i uint64) int {
return uvarintSize(i) + 1 // The first byte encodes uvarintSize(i)
}
func uvarintSize(i uint64) int {
if i == 0 {
return 0
}
if i < 1<<8 {
return 1
}
if i < 1<<16 {
return 2
}
if i < 1<<24 {
return 3
}
if i < 1<<32 {
return 4
}
if i < 1<<40 {
return 5
}
if i < 1<<48 {
return 6
}
if i < 1<<56 {
return 7
}
return 8
}
func WriteVarint(i int, w io.Writer, n *int, err *error) {
var negate = false
if i < 0 {
negate = true
i = -i
}
var size = uvarintSize(uint64(i))
if negate {
// e.g. 0xF1 for a single negative byte
WriteUint8(uint8(size+0xF0), w, n, err)
} else {
WriteUint8(uint8(size), w, n, err)
}
if size > 0 {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], uint64(i))
WriteTo(buf[(8-size):], w, n, err)
}
}
func ReadVarint(r io.Reader, n *int, err *error) int {
var size = ReadUint8(r, n, err)
var negate = false
if (size >> 4) == 0xF {
negate = true
size = size & 0x0F
}
if size > 8 {
setFirstErr(err, errors.New("Varint overflow"))
return 0
}
if size == 0 {
if negate {
setFirstErr(err, errors.New("Varint does not allow negative zero"))
}
return 0
}
var buf [8]byte
ReadFull(buf[(8-size):], r, n, err)
var i = int(binary.BigEndian.Uint64(buf[:]))
if negate {
return -i
} else {
return i
}
}
func PutVarint(buf []byte, i int) (n int, err error) {
var negate = false
if i < 0 {
negate = true
i = -i
}
var size = uvarintSize(uint64(i))
if len(buf) < size+1 {
return 0, errors.New("Insufficient buffer length")
}
if negate {
// e.g. 0xF1 for a single negative byte
buf[0] = byte(size + 0xF0)
} else {
buf[0] = byte(size)
}
if size > 0 {
var buf2 [8]byte
binary.BigEndian.PutUint64(buf2[:], uint64(i))
copy(buf[1:], buf2[(8-size):])
}
return size + 1, nil
}
func GetVarint(buf []byte) (i int, n int, err error) {
if len(buf) == 0 {
return 0, 0, errors.New("Insufficent buffer length")
}
var size = int(buf[0])
var negate = false
if (size >> 4) == 0xF {
negate = true
size = size & 0x0F
}
if size > 8 {
return 0, 0, errors.New("Varint overflow")
}
if size == 0 {
if negate {
return 0, 0, errors.New("Varint does not allow negative zero")
}
return 0, 1, nil
}
if len(buf) < 1+size {
return 0, 0, errors.New("Insufficient buffer length")
}
var buf2 [8]byte
copy(buf2[(8-size):], buf[1:1+size])
i = int(binary.BigEndian.Uint64(buf2[:]))
if negate {
return -i, size + 1, nil
} else {
return i, size + 1, nil
}
}
// Uvarint
func WriteUvarint(i uint, w io.Writer, n *int, err *error) {
var size = uvarintSize(uint64(i))
WriteUint8(uint8(size), w, n, err)
if size > 0 {
var buf [8]byte
binary.BigEndian.PutUint64(buf[:], uint64(i))
WriteTo(buf[(8-size):], w, n, err)
}
}
func ReadUvarint(r io.Reader, n *int, err *error) uint {
var size = ReadUint8(r, n, err)
if size > 8 {
setFirstErr(err, errors.New("Uvarint overflow"))
return 0
}
if size == 0 {
return 0
}
var buf [8]byte
ReadFull(buf[(8-size):], r, n, err)
return uint(binary.BigEndian.Uint64(buf[:]))
}
func PutUvarint(buf []byte, i uint) (n int, err error) {
var size = uvarintSize(uint64(i))
if len(buf) < size+1 {
return 0, errors.New("Insufficient buffer length")
}
buf[0] = byte(size)
if size > 0 {
var buf2 [8]byte
binary.BigEndian.PutUint64(buf2[:], uint64(i))
copy(buf[1:], buf2[(8-size):])
}
return size + 1, nil
}
func GetUvarint(buf []byte) (i uint, n int, err error) {
if len(buf) == 0 {
return 0, 0, errors.New("Insufficent buffer length")
}
var size = int(buf[0])
if size > 8 {
return 0, 0, errors.New("Uvarint overflow")
}
if size == 0 {
return 0, 1, nil
}
if len(buf) < 1+size {
return 0, 0, errors.New("Insufficient buffer length")
}
var buf2 [8]byte
copy(buf2[(8-size):], buf[1:1+size])
i = uint(binary.BigEndian.Uint64(buf2[:]))
return i, size + 1, nil
}
func setFirstErr(err *error, newErr error) {
if *err == nil && newErr != nil {
*err = newErr
}
}