-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitwriter.go
67 lines (54 loc) · 1.43 KB
/
bitwriter.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
package flate
import (
"encoding/binary"
"github.com/chronos-tachyon/assert"
"github.com/chronos-tachyon/huffman"
)
type bitwriter interface {
outputBufferWrite([]byte) bool
outputBufferWriteU16(binary.ByteOrder, uint16) bool
outputBufferWriteU32(binary.ByteOrder, uint32) bool
outputBitsWrite(byte, block) bool
outputBitsWriteHC(huffman.Code) bool
outputBitsFlush() bool
sendEvent(Event)
}
type bitcounter struct {
numBits uint64
}
func (bc bitcounter) length() uint64 {
return bc.numBits
}
func (bc *bitcounter) outputBufferWrite(p []byte) bool {
bc.outputBitsFlush()
bc.numBits += uint64(len(p)) << 3
return true
}
func (bc *bitcounter) outputBufferWriteU16(bo binary.ByteOrder, x uint16) bool {
bc.outputBitsFlush()
bc.numBits += 16
return true
}
func (bc *bitcounter) outputBufferWriteU32(bo binary.ByteOrder, x uint32) bool {
bc.outputBitsFlush()
bc.numBits += 32
return true
}
func (bc *bitcounter) outputBitsWrite(size byte, bits block) bool {
assert.Assertf(size <= bitsPerBlock, "size %d > bitsPerBlock %d", size, bitsPerBlock)
bc.numBits += uint64(size)
return true
}
func (bc *bitcounter) outputBitsWriteHC(hc huffman.Code) bool {
return bc.outputBitsWrite(hc.Size, block(hc.Bits))
}
func (bc *bitcounter) outputBitsFlush() bool {
remainder := (bc.numBits & 7)
if remainder != 0 {
bc.numBits += (8 - remainder)
}
return true
}
func (bc *bitcounter) sendEvent(Event) {
}
var _ bitwriter = (*bitcounter)(nil)