-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbufwriter.js
More file actions
92 lines (82 loc) · 2.36 KB
/
Copy pathbufwriter.js
File metadata and controls
92 lines (82 loc) · 2.36 KB
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
/*
* Copyright (c) 2022 RethinkDNS and its authors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { W, bufferView } from "./config.js";
import { MaskTop } from "./bufreader.js";
/**
* The BitWriter will create a stream of bytes, letting you write a certain
* number of bits at a time. This is part of the encoder, so it is not
* optimized for memory or speed.
*/
export function BitWriter() {
this.init();
}
BitWriter.prototype = {
init: function () {
this.bits = [];
this.bytes = [];
this.bits16 = [];
this.top = 0;
},
write16(data, numBits) {
// todo: throw error?
if (numBits > 16) {
log.e("writes upto 16 lsb bits; out of range: " + numBits);
return;
}
const n = data;
const brim = 16 - (this.top % 16);
const cur = (this.top / 16) | 0;
const e = this.bits16[cur] | 0;
let remainingBits = 0;
// clear msb
let b = n & MaskTop[16][16 - numBits];
// shift to bit pos to be right at brim-th bit
if (brim >= numBits) {
b = b << (brim - numBits);
} else {
// shave right most bits if there are too many bits than
// what the current element at the brim can accomodate
remainingBits = numBits - brim;
b = b >>> remainingBits;
}
// overlay b on current element, e.
b = e | b;
this.bits16[cur] = b;
// account for the left-over bits shaved off by brim
if (remainingBits > 0) {
b = n & MaskTop[16][16 - remainingBits];
b = b << (16 - remainingBits);
this.bits16[cur + 1] = b;
}
// update top to reflect the bits included
this.top += numBits;
},
/**
* Write some data to the bit string; number(bits) <= 32.
*/
write: function (data, numBits) {
while (numBits > 0) {
// take 16 and then the leftover pass it to write16
const i = ((numBits - 1) / 16) | 0;
const b = data >>> (i * 16);
const l = numBits % 16 === 0 ? 16 : numBits % 16;
this.write16(b, l);
numBits -= l;
}
return;
},
getData: function () {
return this.bitsToBytes();
},
/**
* Get the bitstring represented as a javascript string of bytes
*/
bitsToBytes: function () {
return bufferView[W].from(this.bits16);
},
};