-
Notifications
You must be signed in to change notification settings - Fork 790
/
crc32.go
55 lines (45 loc) · 1010 Bytes
/
crc32.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
package kafka
import (
"encoding/binary"
"hash/crc32"
)
type crc32Writer struct {
table *crc32.Table
buffer [8]byte
crc32 uint32
}
func (w *crc32Writer) update(b []byte) {
w.crc32 = crc32.Update(w.crc32, w.table, b)
}
func (w *crc32Writer) writeInt8(i int8) {
w.buffer[0] = byte(i)
w.update(w.buffer[:1])
}
func (w *crc32Writer) writeInt16(i int16) {
binary.BigEndian.PutUint16(w.buffer[:2], uint16(i))
w.update(w.buffer[:2])
}
func (w *crc32Writer) writeInt32(i int32) {
binary.BigEndian.PutUint32(w.buffer[:4], uint32(i))
w.update(w.buffer[:4])
}
func (w *crc32Writer) writeInt64(i int64) {
binary.BigEndian.PutUint64(w.buffer[:8], uint64(i))
w.update(w.buffer[:8])
}
func (w *crc32Writer) writeBytes(b []byte) {
n := len(b)
if b == nil {
n = -1
}
w.writeInt32(int32(n))
w.update(b)
}
func (w *crc32Writer) Write(b []byte) (int, error) {
w.update(b)
return len(b), nil
}
func (w *crc32Writer) WriteString(s string) (int, error) {
w.update([]byte(s))
return len(s), nil
}