/
binary.go
357 lines (314 loc) · 9.45 KB
/
binary.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
package astikit
import (
"encoding/binary"
"errors"
"io"
)
// BitsWriter represents an object that can write individual bits into a writer
// in a developer-friendly way. Check out the Write method for more information.
// This is particularly helpful when you want to build a slice of bytes based
// on individual bits for testing purposes.
type BitsWriter struct {
bo binary.ByteOrder
cache byte
cacheLen byte
bsCache []byte
w io.Writer
writeCb BitsWriterWriteCallback
}
type BitsWriterWriteCallback func([]byte)
// BitsWriterOptions represents BitsWriter options
type BitsWriterOptions struct {
ByteOrder binary.ByteOrder
// WriteCallback is called every time when full byte is written
WriteCallback BitsWriterWriteCallback
Writer io.Writer
}
// NewBitsWriter creates a new BitsWriter
func NewBitsWriter(o BitsWriterOptions) (w *BitsWriter) {
w = &BitsWriter{
bo: o.ByteOrder,
bsCache: make([]byte, 1),
w: o.Writer,
writeCb: o.WriteCallback,
}
if w.bo == nil {
w.bo = binary.BigEndian
}
return
}
func (w *BitsWriter) SetWriteCallback(cb BitsWriterWriteCallback) {
w.writeCb = cb
}
// Write writes bits into the writer. Bits are only written when there are
// enough to create a byte. When using a string or a bool, bits are added
// from left to right as if
// Available types are:
// - string("10010"): processed as n bits, n being the length of the input
// - []byte: processed as n bytes, n being the length of the input
// - bool: processed as one bit
// - uint8/uint16/uint32/uint64: processed as n bits, if type is uintn
func (w *BitsWriter) Write(i interface{}) error {
// Transform input into "10010" format
switch a := i.(type) {
case string:
for _, r := range a {
var err error
if r == '1' {
err = w.writeBit(1)
} else {
err = w.writeBit(0)
}
if err != nil {
return err
}
}
case []byte:
return w.writeByteSlice(a)
case bool:
if a {
return w.writeBit(1)
} else {
return w.writeBit(0)
}
case uint8:
return w.writeFullByte(a)
case uint16:
return w.writeFullInt(uint64(a), 2)
case uint32:
return w.writeFullInt(uint64(a), 4)
case uint64:
return w.writeFullInt(a, 8)
default:
return errors.New("astikit: invalid type")
}
return nil
}
// Writes exactly n bytes from bs
// Writes first n bytes of bs if len(bs) > n
// Pads with padByte at the end if len(bs) < n
func (w *BitsWriter) WriteBytesN(bs []byte, n int, padByte uint8) error {
if n == 0 {
return nil
}
if len(bs) >= n {
return w.writeByteSlice(bs[:n])
}
if err := w.writeByteSlice(bs); err != nil {
return err
}
// no bytes.Repeat here to avoid allocation
for i := 0; i < n-len(bs); i++ {
if err := w.writeFullByte(padByte); err != nil {
return err
}
}
return nil
}
func (w *BitsWriter) writeByteSlice(in []byte) error {
if len(in) == 0 {
return nil
}
if w.cacheLen != 0 {
for _, b := range in {
if err := w.writeFullByte(b); err != nil {
return err
}
}
} else {
return w.write(in)
}
return nil
}
func (w *BitsWriter) write(b []byte) error {
if _, err := w.w.Write(b); err != nil {
return err
}
if w.writeCb != nil {
for i := range b {
w.writeCb(b[i : i+1])
}
}
return nil
}
func (w *BitsWriter) writeFullInt(in uint64, len int) error {
if w.bo == binary.BigEndian {
return w.writeBitsN(in, len*8)
} else {
for i := 0; i < len; i++ {
err := w.writeFullByte(byte(in >> (i * 8)))
if err != nil {
return err
}
}
}
return nil
}
func (w *BitsWriter) flushBsCache() error {
return w.write(w.bsCache)
}
func (w *BitsWriter) writeFullByte(b byte) error {
if w.cacheLen == 0 {
w.bsCache[0] = b
} else {
w.bsCache[0] = w.cache | (b >> w.cacheLen)
w.cache = b << (8 - w.cacheLen)
}
return w.flushBsCache()
}
func (w *BitsWriter) writeBit(bit byte) error {
if bit != 0 {
w.cache |= 1 << (7 - w.cacheLen)
}
w.cacheLen++
if w.cacheLen == 8 {
w.bsCache[0] = w.cache
if err := w.flushBsCache(); err != nil {
return err
}
w.cacheLen = 0
w.cache = 0
}
return nil
}
func (w *BitsWriter) writeBitsN(toWrite uint64, n int) (err error) {
toWrite &= ^uint64(0) >> (64 - n)
for n > 0 {
if w.cacheLen == 0 {
if n >= 8 {
n -= 8
w.bsCache[0] = byte(toWrite >> n)
if err = w.flushBsCache(); err != nil {
return
}
} else {
w.cacheLen = uint8(n)
w.cache = byte(toWrite << (8 - w.cacheLen))
n = 0
}
} else {
free := int(8 - w.cacheLen)
m := n
if m >= free {
m = free
}
if n <= free {
w.cache |= byte(toWrite << (free - m))
} else {
w.cache |= byte(toWrite >> (n - m))
}
n -= m
w.cacheLen += uint8(m)
if w.cacheLen == 8 {
w.bsCache[0] = w.cache
if err = w.flushBsCache(); err != nil {
return err
}
w.cacheLen = 0
w.cache = 0
}
}
}
return
}
// WriteN writes the input into n bits
func (w *BitsWriter) WriteN(i interface{}, n int) error {
var toWrite uint64
switch a := i.(type) {
case uint8:
toWrite = uint64(a)
case uint16:
toWrite = uint64(a)
case uint32:
toWrite = uint64(a)
case uint64:
toWrite = a
default:
return errors.New("astikit: invalid type")
}
return w.writeBitsN(toWrite, n)
}
// BitsWriterBatch allows to chain multiple Write* calls and check for error only once
// For more info see https://github.com/asticode/go-astikit/pull/6
type BitsWriterBatch struct {
err error
w *BitsWriter
}
func NewBitsWriterBatch(w *BitsWriter) BitsWriterBatch {
return BitsWriterBatch{
w: w,
}
}
// Calls BitsWriter.Write if there was no write error before
func (b *BitsWriterBatch) Write(i interface{}) {
if b.err == nil {
b.err = b.w.Write(i)
}
}
// Calls BitsWriter.WriteN if there was no write error before
func (b *BitsWriterBatch) WriteN(i interface{}, n int) {
if b.err == nil {
b.err = b.w.WriteN(i, n)
}
}
// Calls BitsWriter.WriteBytesN if there was no write error before
func (b *BitsWriterBatch) WriteBytesN(bs []byte, n int, padByte uint8) {
if b.err == nil {
b.err = b.w.WriteBytesN(bs, n, padByte)
}
}
// Returns first write error
func (b *BitsWriterBatch) Err() error {
return b.err
}
var byteHamming84Tab = [256]uint8{
0x01, 0xff, 0xff, 0x08, 0xff, 0x0c, 0x04, 0xff, 0xff, 0x08, 0x08, 0x08, 0x06, 0xff, 0xff, 0x08,
0xff, 0x0a, 0x02, 0xff, 0x06, 0xff, 0xff, 0x0f, 0x06, 0xff, 0xff, 0x08, 0x06, 0x06, 0x06, 0xff,
0xff, 0x0a, 0x04, 0xff, 0x04, 0xff, 0x04, 0x04, 0x00, 0xff, 0xff, 0x08, 0xff, 0x0d, 0x04, 0xff,
0x0a, 0x0a, 0xff, 0x0a, 0xff, 0x0a, 0x04, 0xff, 0xff, 0x0a, 0x03, 0xff, 0x06, 0xff, 0xff, 0x0e,
0x01, 0x01, 0x01, 0xff, 0x01, 0xff, 0xff, 0x0f, 0x01, 0xff, 0xff, 0x08, 0xff, 0x0d, 0x05, 0xff,
0x01, 0xff, 0xff, 0x0f, 0xff, 0x0f, 0x0f, 0x0f, 0xff, 0x0b, 0x03, 0xff, 0x06, 0xff, 0xff, 0x0f,
0x01, 0xff, 0xff, 0x09, 0xff, 0x0d, 0x04, 0xff, 0xff, 0x0d, 0x03, 0xff, 0x0d, 0x0d, 0xff, 0x0d,
0xff, 0x0a, 0x03, 0xff, 0x07, 0xff, 0xff, 0x0f, 0x03, 0xff, 0x03, 0x03, 0xff, 0x0d, 0x03, 0xff,
0xff, 0x0c, 0x02, 0xff, 0x0c, 0x0c, 0xff, 0x0c, 0x00, 0xff, 0xff, 0x08, 0xff, 0x0c, 0x05, 0xff,
0x02, 0xff, 0x02, 0x02, 0xff, 0x0c, 0x02, 0xff, 0xff, 0x0b, 0x02, 0xff, 0x06, 0xff, 0xff, 0x0e,
0x00, 0xff, 0xff, 0x09, 0xff, 0x0c, 0x04, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00, 0xff, 0xff, 0x0e,
0xff, 0x0a, 0x02, 0xff, 0x07, 0xff, 0xff, 0x0e, 0x00, 0xff, 0xff, 0x0e, 0xff, 0x0e, 0x0e, 0x0e,
0x01, 0xff, 0xff, 0x09, 0xff, 0x0c, 0x05, 0xff, 0xff, 0x0b, 0x05, 0xff, 0x05, 0xff, 0x05, 0x05,
0xff, 0x0b, 0x02, 0xff, 0x07, 0xff, 0xff, 0x0f, 0x0b, 0x0b, 0xff, 0x0b, 0xff, 0x0b, 0x05, 0xff,
0xff, 0x09, 0x09, 0x09, 0x07, 0xff, 0xff, 0x09, 0x00, 0xff, 0xff, 0x09, 0xff, 0x0d, 0x05, 0xff,
0x07, 0xff, 0xff, 0x09, 0x07, 0x07, 0x07, 0xff, 0xff, 0x0b, 0x03, 0xff, 0x07, 0xff, 0xff, 0x0e,
}
// ByteHamming84Decode hamming 8/4 decodes
func ByteHamming84Decode(i uint8) (o uint8, ok bool) {
o = byteHamming84Tab[i]
if o == 0xff {
return
}
ok = true
return
}
var byteParityTab = [256]uint8{
0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01,
0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01,
0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01,
0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01,
0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01,
0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01,
0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01,
0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01,
0x00, 0x01, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
}
// ByteParity returns the byte parity
func ByteParity(i uint8) (o uint8, ok bool) {
ok = byteParityTab[i] == 1
o = i & 0x7f
return
}