-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint.go
More file actions
149 lines (123 loc) · 3.09 KB
/
Copy pathint.go
File metadata and controls
149 lines (123 loc) · 3.09 KB
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
package binaryext
import errors "github.com/weathersource/go-errors"
var (
// ErrOverflow occurs when more bytes are provided than can
// be handled by the receiving type.
ErrOverflow = errors.NewInvalidArgumentError("Byte count exceeds expectation.")
// ErrEmpty occurs when an empty slice of bytes is received
// for writing to a type.
ErrEmpty = errors.NewInvalidArgumentError("No bytes provided.")
)
// putUint encodes a uint64 to []byte.
func putUint(x uint64, maxbytes int) []byte {
buf := make([]byte, 0, maxbytes)
for x >= 0x100 {
buf = append(buf, byte(x))
x >>= 8
}
buf = append(buf, byte(x))
return buf
}
// getUint decodes a uint64 from []byte
func getUint(buf []byte, maxbytes int) (uint64, error) {
var x uint64
var s uint8
for i, b := range buf {
if i > maxbytes-1 && b > 0 {
return 0, ErrOverflow
}
if i == len(buf)-1 {
return x | uint64(b)<<s, nil
}
x |= uint64(b&0xff) << s
s += 8
}
return 0, ErrEmpty
}
// putInt encodes a int64 to []byte.
func putInt(x int64, maxbytes int) []byte {
ux := uint64(x) << 1
if x < 0 {
ux = ^ux
}
return putUint(ux, maxbytes)
}
// getInt decodes a int64 from []byte
func getInt(buf []byte, maxbytes int) (int64, error) {
ux, err := getUint(buf, maxbytes) // ok to continue in presence of error
x := int64(ux >> 1)
if ux&1 != 0 {
x = ^x
}
return x, err
}
// PutUint64 encodes a uint64 to []byte.
func PutUint64(x uint64) []byte {
return putUint(x, 8)
}
// PutUint32 encodes a uint32 to []byte.
func PutUint32(x uint32) []byte {
return putUint(uint64(x), 4)
}
// PutUint16 encodes a uint16 to []byte.
func PutUint16(x uint16) []byte {
return putUint(uint64(x), 2)
}
// PutUint8 encodes a uint8 to []byte.
func PutUint8(x uint8) []byte {
return putUint(uint64(x), 1)
}
// Uint64 decodes a uint64 from []byte
func Uint64(buf []byte) (uint64, error) {
return getUint(buf, 8)
}
// Uint32 decodes a uint32 from []byte
func Uint32(buf []byte) (uint32, error) {
x, err := getUint(buf, 4)
return uint32(x), err
}
// Uint16 decodes a uint16 from []byte
func Uint16(buf []byte) (uint16, error) {
x, err := getUint(buf, 2)
return uint16(x), err
}
// Uint8 decodes a uint8 from []byte
func Uint8(buf []byte) (uint8, error) {
x, err := getUint(buf, 1)
return uint8(x), err
}
// PutInt64 encodes a int64 to []byte.
func PutInt64(x int64) []byte {
return putInt(x, 8)
}
// PutInt32 encodes a int32 to []byte.
func PutInt32(x int32) []byte {
return putInt(int64(x), 4)
}
// PutInt16 encodes a int16 to []byte.
func PutInt16(x int16) []byte {
return putInt(int64(x), 2)
}
// PutInt8 encodes a int8 to []byte.
func PutInt8(x int8) []byte {
return putInt(int64(x), 1)
}
// Int64 decodes a int64 from []byte
func Int64(buf []byte) (int64, error) {
return getInt(buf, 8)
}
// Int32 decodes a int32 from []byte
func Int32(buf []byte) (int32, error) {
x, err := getInt(buf, 4)
return int32(x), err
}
// Int16 decodes a int16 from []byte
func Int16(buf []byte) (int16, error) {
x, err := getInt(buf, 2)
return int16(x), err
}
// Int8 decodes a int8 from []byte
func Int8(buf []byte) (int8, error) {
x, err := getInt(buf, 1)
return int8(x), err
}