forked from fernomac/ion-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bits.go
285 lines (228 loc) · 5.32 KB
/
bits.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
package ion
import (
"math/big"
"time"
)
// uintLen pre-calculates the length, in bytes, of the given uint value.
func uintLen(v uint64) uint64 {
len := uint64(1)
v >>= 8
for v > 0 {
len++
v >>= 8
}
return len
}
// appendUint appends a uint value to the given slice. The reader is
// expected to know how many bytes the value takes up.
func appendUint(b []byte, v uint64) []byte {
var buf [8]byte
i := 7
buf[i] = byte(v & 0xFF)
v >>= 8
for v > 0 {
i--
buf[i] = byte(v & 0xFF)
v >>= 8
}
return append(b, buf[i:]...)
}
// intLen pre-calculates the length, in bytes, of the given int value.
func intLen(n int64) uint64 {
if n == 0 {
return 0
}
mag := uint64(n)
if n < 0 {
mag = uint64(-n)
}
len := uintLen(mag)
// If the high bit is a one, we need an extra byte to store the sign bit.
hb := mag >> ((len - 1) * 8)
if hb&0x80 != 0 {
len++
}
return len
}
// appendInt appends a (signed) int to the given slice. The reader is
// expected to know how many bytes the value takes up.
func appendInt(b []byte, n int64) []byte {
if n == 0 {
return b
}
neg := false
mag := uint64(n)
if n < 0 {
neg = true
mag = uint64(-n)
}
var buf [8]byte
bits := buf[:0]
bits = appendUint(bits, mag)
if bits[0]&0x80 == 0 {
// We've got space we can use for the sign bit.
if neg {
bits[0] ^= 0x80
}
} else {
// We need to add more space.
bit := byte(0)
if neg {
bit = 0x80
}
b = append(b, bit)
}
return append(b, bits...)
}
// bigIntLen pre-calculates the length, in bytes, of the given big.Int value.
func bigIntLen(v *big.Int) uint64 {
if v.Sign() == 0 {
return 0
}
bitl := v.BitLen()
bytel := bitl / 8
// Either bitl is evenly divisibly by 8, in which case we need another
// byte for the sign bit, or its not in which case we need to round up
// (but will then have room for the sign bit).
return uint64(bytel) + 1
}
// appendBigInt appends a (signed) big.Int to the given slice. The reader is
// expected to know how many bytes the value takes up.
func appendBigInt(b []byte, v *big.Int) []byte {
sign := v.Sign()
if sign == 0 {
return b
}
bits := v.Bytes()
if bits[0]&0x80 == 0 {
// We've got space we can use for the sign bit.
if sign < 0 {
bits[0] ^= 0x80
}
} else {
// We need to add more space.
bit := byte(0)
if sign < 0 {
bit = 0x80
}
b = append(b, bit)
}
return append(b, bits...)
}
// varUintLen pre-calculates the length, in bytes, of the given varUint value.
func varUintLen(v uint64) uint64 {
len := uint64(1)
v >>= 7
for v > 0 {
len++
v >>= 7
}
return len
}
// appendVarUint appends a variable-length-encoded uint to the given slice.
// Each byte stores seven bits of value; the high bit is a flag marking the
// last byte of the value.
func appendVarUint(b []byte, v uint64) []byte {
var buf [10]byte
i := 9
buf[i] = 0x80 | byte(v&0x7F)
v >>= 7
for v > 0 {
i--
buf[i] = byte(v & 0x7F)
v >>= 7
}
return append(b, buf[i:]...)
}
// varIntLen pre-calculates the length, in bytes, of the given varInt value.
func varIntLen(v int64) uint64 {
mag := uint64(v)
if v < 0 {
mag = uint64(-v)
}
// Reserve one extra bit of the first byte for sign.
len := uint64(1)
mag >>= 6
for mag > 0 {
len++
mag >>= 7
}
return len
}
// appendVarInt appends a variable-length-encoded int to the given slice.
// Most bytes store seven bits of value; the high bit is a flag marking the
// last byte of the value. The first byte additionally stores a sign bit.
func appendVarInt(b []byte, v int64) []byte {
var buf [10]byte
signbit := byte(0)
mag := uint64(v)
if v < 0 {
signbit = 0x40
mag = uint64(-v)
}
next := mag >> 6
if next == 0 {
// The whole thing fits in one byte.
return append(b, 0x80|signbit|byte(mag&0x3F))
}
i := 9
buf[i] = 0x80 | byte(mag&0x7F)
mag >>= 7
next = mag >> 6
for next > 0 {
i--
buf[i] = byte(mag & 0x7F)
mag >>= 7
next = mag >> 6
}
i--
buf[i] = signbit | byte(mag&0x3F)
return append(b, buf[i:]...)
}
// tagLen pre-calculates the length, in bytes, of a tag.
func tagLen(len uint64) uint64 {
if len < 0x0E {
return 1
}
return 1 + varUintLen(len)
}
// appendTag appends a code+len tag to the given slice.
func appendTag(b []byte, code byte, len uint64) []byte {
if len < 0x0E {
// Short form, with length embedded in the code byte.
return append(b, code|byte(len))
}
// Long form, with separate length.
b = append(b, code|0x0E)
return appendVarUint(b, len)
}
// timeLen pre-calculates the length, in bytes, of the given time value.
func timeLen(offset int, utc time.Time) uint64 {
ret := varIntLen(int64(offset))
// Almost certainly two but let's be safe.
ret += varUintLen(uint64(utc.Year()))
// Month, day, hour, minute, and second are all guaranteed to be one byte.
ret += 5
ns := utc.Nanosecond()
if ns > 0 {
ret++ // varIntLen(-9)
ret += intLen(int64(ns))
}
return ret
}
// appendTime appends a timestamp value
func appendTime(b []byte, offset int, utc time.Time) []byte {
b = appendVarInt(b, int64(offset))
b = appendVarUint(b, uint64(utc.Year()))
b = appendVarUint(b, uint64(utc.Month()))
b = appendVarUint(b, uint64(utc.Day()))
b = appendVarUint(b, uint64(utc.Hour()))
b = appendVarUint(b, uint64(utc.Minute()))
b = appendVarUint(b, uint64(utc.Second()))
ns := utc.Nanosecond()
if ns > 0 {
b = appendVarInt(b, -9)
b = appendInt(b, int64(ns))
}
return b
}