forked from illuspas/Node-Media-Server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_core_bitop.js
54 lines (44 loc) · 1.05 KB
/
node_core_bitop.js
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
class Bitop {
constructor(buffer) {
this.buffer = buffer;
this.buflen = buffer.length;
this.bufpos = 0;
this.bufoff = 0;
this.iserro = false;
}
read(n) {
let v = 0;
let d = 0;
while (n) {
if (n < 0 || this.bufpos >= this.buflen) {
this.iserro = true;
return 0;
}
this.iserro = false;
d = this.bufoff + n > 8 ? 8 - this.bufoff : n;
v <<= d;
v += (this.buffer[this.bufpos] >> (8 - this.bufoff - d)) & (0xff >> (8 - d))
this.bufoff += d;
n -= d;
if (this.bufoff == 8) {
this.bufpos++;
this.bufoff = 0;
}
}
return v;
}
look(n) {
let p = this.bufpos;
let o = this.bufoff;
let v = this.read(n);
this.bufpos = p;
this.bufoff = o;
return v;
}
read_golomb() {
let n;
for (n = 0; this.read(1) == 0 && !this.iserro; n++);
return (1 << n) + this.read(n) - 1;
}
}
module.exports = Bitop;