blob: ec0c3fc53b5a7b36ec24aaaf3996d9735aa5d122 [file] [log] [blame]
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +00001// Copyright 2018 Klaus Post. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4// Based on work Copyright (c) 2013, Yann Collet, released under BSD License.
5
6package huff0
7
8import "fmt"
9
10// bitWriter will write bits.
11// First bit will be LSB of the first byte of output.
12type bitWriter struct {
13 bitContainer uint64
14 nBits uint8
15 out []byte
16}
17
18// bitMask16 is bitmasks. Has extra to avoid bounds check.
19var bitMask16 = [32]uint16{
20 0, 1, 3, 7, 0xF, 0x1F,
21 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF,
22 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0xFFFF,
23 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
24 0xFFFF, 0xFFFF} /* up to 16 bits */
25
26// addBits16NC will add up to 16 bits.
27// It will not check if there is space for them,
28// so the caller must ensure that it has flushed recently.
29func (b *bitWriter) addBits16NC(value uint16, bits uint8) {
30 b.bitContainer |= uint64(value&bitMask16[bits&31]) << (b.nBits & 63)
31 b.nBits += bits
32}
33
34// addBits16Clean will add up to 16 bits. value may not contain more set bits than indicated.
35// It will not check if there is space for them, so the caller must ensure that it has flushed recently.
36func (b *bitWriter) addBits16Clean(value uint16, bits uint8) {
37 b.bitContainer |= uint64(value) << (b.nBits & 63)
38 b.nBits += bits
39}
40
41// addBits16Clean will add up to 16 bits. value may not contain more set bits than indicated.
42// It will not check if there is space for them, so the caller must ensure that it has flushed recently.
43func (b *bitWriter) encSymbol(ct cTable, symbol byte) {
44 enc := ct[symbol]
45 b.bitContainer |= uint64(enc.val) << (b.nBits & 63)
46 b.nBits += enc.nBits
47}
48
49// addBits16ZeroNC will add up to 16 bits.
50// It will not check if there is space for them,
51// so the caller must ensure that it has flushed recently.
52// This is fastest if bits can be zero.
53func (b *bitWriter) addBits16ZeroNC(value uint16, bits uint8) {
54 if bits == 0 {
55 return
56 }
57 value <<= (16 - bits) & 15
58 value >>= (16 - bits) & 15
59 b.bitContainer |= uint64(value) << (b.nBits & 63)
60 b.nBits += bits
61}
62
63// flush will flush all pending full bytes.
64// There will be at least 56 bits available for writing when this has been called.
65// Using flush32 is faster, but leaves less space for writing.
66func (b *bitWriter) flush() {
67 v := b.nBits >> 3
68 switch v {
69 case 0:
70 return
71 case 1:
72 b.out = append(b.out,
73 byte(b.bitContainer),
74 )
75 b.bitContainer >>= 1 << 3
76 case 2:
77 b.out = append(b.out,
78 byte(b.bitContainer),
79 byte(b.bitContainer>>8),
80 )
81 b.bitContainer >>= 2 << 3
82 case 3:
83 b.out = append(b.out,
84 byte(b.bitContainer),
85 byte(b.bitContainer>>8),
86 byte(b.bitContainer>>16),
87 )
88 b.bitContainer >>= 3 << 3
89 case 4:
90 b.out = append(b.out,
91 byte(b.bitContainer),
92 byte(b.bitContainer>>8),
93 byte(b.bitContainer>>16),
94 byte(b.bitContainer>>24),
95 )
96 b.bitContainer >>= 4 << 3
97 case 5:
98 b.out = append(b.out,
99 byte(b.bitContainer),
100 byte(b.bitContainer>>8),
101 byte(b.bitContainer>>16),
102 byte(b.bitContainer>>24),
103 byte(b.bitContainer>>32),
104 )
105 b.bitContainer >>= 5 << 3
106 case 6:
107 b.out = append(b.out,
108 byte(b.bitContainer),
109 byte(b.bitContainer>>8),
110 byte(b.bitContainer>>16),
111 byte(b.bitContainer>>24),
112 byte(b.bitContainer>>32),
113 byte(b.bitContainer>>40),
114 )
115 b.bitContainer >>= 6 << 3
116 case 7:
117 b.out = append(b.out,
118 byte(b.bitContainer),
119 byte(b.bitContainer>>8),
120 byte(b.bitContainer>>16),
121 byte(b.bitContainer>>24),
122 byte(b.bitContainer>>32),
123 byte(b.bitContainer>>40),
124 byte(b.bitContainer>>48),
125 )
126 b.bitContainer >>= 7 << 3
127 case 8:
128 b.out = append(b.out,
129 byte(b.bitContainer),
130 byte(b.bitContainer>>8),
131 byte(b.bitContainer>>16),
132 byte(b.bitContainer>>24),
133 byte(b.bitContainer>>32),
134 byte(b.bitContainer>>40),
135 byte(b.bitContainer>>48),
136 byte(b.bitContainer>>56),
137 )
138 b.bitContainer = 0
139 b.nBits = 0
140 return
141 default:
142 panic(fmt.Errorf("bits (%d) > 64", b.nBits))
143 }
144 b.nBits &= 7
145}
146
147// flush32 will flush out, so there are at least 32 bits available for writing.
148func (b *bitWriter) flush32() {
149 if b.nBits < 32 {
150 return
151 }
152 b.out = append(b.out,
153 byte(b.bitContainer),
154 byte(b.bitContainer>>8),
155 byte(b.bitContainer>>16),
156 byte(b.bitContainer>>24))
157 b.nBits -= 32
158 b.bitContainer >>= 32
159}
160
161// flushAlign will flush remaining full bytes and align to next byte boundary.
162func (b *bitWriter) flushAlign() {
163 nbBytes := (b.nBits + 7) >> 3
164 for i := uint8(0); i < nbBytes; i++ {
165 b.out = append(b.out, byte(b.bitContainer>>(i*8)))
166 }
167 b.nBits = 0
168 b.bitContainer = 0
169}
170
171// close will write the alignment bit and write the final byte(s)
172// to the output.
173func (b *bitWriter) close() error {
174 // End mark
175 b.addBits16Clean(1, 1)
176 // flush until next byte.
177 b.flushAlign()
178 return nil
179}
180
181// reset and continue writing by appending to out.
182func (b *bitWriter) reset(out []byte) {
183 b.bitContainer = 0
184 b.nBits = 0
185 b.out = out
186}