1 | package brotli
|
---|
2 |
|
---|
3 | import "encoding/binary"
|
---|
4 |
|
---|
5 | /* Copyright 2010 Google Inc. All Rights Reserved.
|
---|
6 |
|
---|
7 | Distributed under MIT license.
|
---|
8 | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
---|
9 | */
|
---|
10 |
|
---|
11 | /* Write bits into a byte array. */
|
---|
12 |
|
---|
13 | /* This function writes bits into bytes in increasing addresses, and within
|
---|
14 | a byte least-significant-bit first.
|
---|
15 |
|
---|
16 | The function can write up to 56 bits in one go with WriteBits
|
---|
17 | Example: let's assume that 3 bits (Rs below) have been written already:
|
---|
18 |
|
---|
19 | BYTE-0 BYTE+1 BYTE+2
|
---|
20 |
|
---|
21 | 0000 0RRR 0000 0000 0000 0000
|
---|
22 |
|
---|
23 | Now, we could write 5 or less bits in MSB by just sifting by 3
|
---|
24 | and OR'ing to BYTE-0.
|
---|
25 |
|
---|
26 | For n bits, we take the last 5 bits, OR that with high bits in BYTE-0,
|
---|
27 | and locate the rest in BYTE+1, BYTE+2, etc. */
|
---|
28 | func writeBits(n_bits uint, bits uint64, pos *uint, array []byte) {
|
---|
29 | /* This branch of the code can write up to 56 bits at a time,
|
---|
30 | 7 bits are lost by being perhaps already in *p and at least
|
---|
31 | 1 bit is needed to initialize the bit-stream ahead (i.e. if 7
|
---|
32 | bits are in *p and we write 57 bits, then the next write will
|
---|
33 | access a byte that was never initialized). */
|
---|
34 | p := array[*pos>>3:]
|
---|
35 | v := uint64(p[0])
|
---|
36 | v |= bits << (*pos & 7)
|
---|
37 | binary.LittleEndian.PutUint64(p, v)
|
---|
38 | *pos += n_bits
|
---|
39 | }
|
---|
40 |
|
---|
41 | func writeSingleBit(bit bool, pos *uint, array []byte) {
|
---|
42 | if bit {
|
---|
43 | writeBits(1, 1, pos, array)
|
---|
44 | } else {
|
---|
45 | writeBits(1, 0, pos, array)
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | func writeBitsPrepareStorage(pos uint, array []byte) {
|
---|
50 | assert(pos&7 == 0)
|
---|
51 | array[pos>>3] = 0
|
---|
52 | }
|
---|