-
Notifications
You must be signed in to change notification settings - Fork 0
/
numbers.go
181 lines (168 loc) · 4.44 KB
/
numbers.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
// Byteman
// For the full copyright and license information, please view the LICENSE.txt file.
package byteman
import (
"encoding/binary"
)
const (
// IntSize represents the supported integer value size.
IntSize = 32 << (^uint(0) >> 63) // 32 or 64
)
// FromUint returns a byte slice by the given Uint and byte order (endianness).
func FromUint(number interface{}, bo ByteOrder) []byte {
switch v := number.(type) {
case uint:
b := make([]byte, IntSize/8)
if bo.Type() == ByteOrderTypeBigEndian {
if IntSize == 64 {
binary.BigEndian.PutUint64(b, uint64(v))
} else {
binary.BigEndian.PutUint32(b, uint32(v))
}
} else {
if IntSize == 64 {
binary.LittleEndian.PutUint64(b, uint64(v))
} else {
binary.LittleEndian.PutUint32(b, uint32(v))
}
}
return b
case uint8:
b := make([]byte, 1)
b[0] = v
return b
case uint16:
b := make([]byte, 2)
if bo.Type() == ByteOrderTypeBigEndian {
binary.BigEndian.PutUint16(b, v)
} else {
binary.LittleEndian.PutUint16(b, v)
}
return b
case uint32:
b := make([]byte, 4)
if bo.Type() == ByteOrderTypeBigEndian {
binary.BigEndian.PutUint32(b, v)
} else {
binary.LittleEndian.PutUint32(b, v)
}
return b
case uint64:
b := make([]byte, 8)
if bo.Type() == ByteOrderTypeBigEndian {
binary.BigEndian.PutUint64(b, v)
} else {
binary.LittleEndian.PutUint64(b, v)
}
return b
default:
return nil
}
}
// FromInt returns a byte slice by the given int and byte order (endianness).
func FromInt(number interface{}, bo ByteOrder) []byte {
switch v := number.(type) {
case int:
return FromUint(uint(v), bo)
case int8:
return FromUint(uint8(v), bo)
case int16:
return FromUint(uint16(v), bo)
case int32:
return FromUint(uint32(v), bo)
case int64:
return FromUint(uint64(v), bo)
default:
return nil
}
}
// Uint returns an uint value by the given byte slice and byte order (endianness).
func Uint(b []byte, bo ByteOrder) uint {
if len(b) == 8 && IntSize == 64 {
if bo.Type() == ByteOrderTypeBigEndian {
return uint(binary.BigEndian.Uint64(b))
}
return uint(binary.LittleEndian.Uint64(b))
} else if len(b) >= 4 {
if bo.Type() == ByteOrderTypeBigEndian {
return uint(binary.BigEndian.Uint32(b))
}
return uint(binary.LittleEndian.Uint32(b))
}
return 0
}
// Uint16 returns an uint16 value by the given byte slice and byte order (endianness).
func Uint16(b []byte, bo ByteOrder) uint16 {
if len(b) == 2 {
if bo.Type() == ByteOrderTypeBigEndian {
return binary.BigEndian.Uint16(b)
}
return binary.LittleEndian.Uint16(b)
}
return 0
}
// Uint32 returns an uint32 value by the given byte slice and byte order (endianness).
func Uint32(b []byte, bo ByteOrder) uint32 {
if len(b) == 4 {
if bo.Type() == ByteOrderTypeBigEndian {
return binary.BigEndian.Uint32(b)
}
return binary.LittleEndian.Uint32(b)
}
return 0
}
// Uint64 returns an uint64 value by the given byte slice and byte order (endianness).
func Uint64(b []byte, bo ByteOrder) uint64 {
if len(b) == 8 {
if bo.Type() == ByteOrderTypeBigEndian {
return binary.BigEndian.Uint64(b)
}
return binary.LittleEndian.Uint64(b)
}
return 0
}
// Int returns an int value by the given byte slice and byte order (endianness).
func Int(b []byte, bo ByteOrder) int {
if len(b) == 8 && IntSize == 64 {
if bo.Type() == ByteOrderTypeBigEndian {
return int(binary.BigEndian.Uint64(b))
}
return int(binary.LittleEndian.Uint64(b))
} else if len(b) >= 4 {
if bo.Type() == ByteOrderTypeBigEndian {
return int(binary.BigEndian.Uint32(b))
}
return int(binary.LittleEndian.Uint32(b))
}
return 0
}
// Int16 returns an int16 value by the given byte slice and byte order (endianness).
func Int16(b []byte, bo ByteOrder) int16 {
if len(b) == 2 {
if bo.Type() == ByteOrderTypeBigEndian {
return int16(binary.BigEndian.Uint16(b))
}
return int16(binary.LittleEndian.Uint16(b))
}
return 0
}
// Int32 returns an int32 value by the given byte slice and byte order (endianness).
func Int32(b []byte, bo ByteOrder) int32 {
if len(b) == 4 {
if bo.Type() == ByteOrderTypeBigEndian {
return int32(binary.BigEndian.Uint32(b))
}
return int32(binary.LittleEndian.Uint32(b))
}
return 0
}
// Int64 returns an int64 value by the given byte slice and byte order (endianness).
func Int64(b []byte, bo ByteOrder) int64 {
if len(b) == 8 {
if bo.Type() == ByteOrderTypeBigEndian {
return int64(binary.BigEndian.Uint64(b))
}
return int64(binary.LittleEndian.Uint64(b))
}
return 0
}