-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadbitstream.js
55 lines (46 loc) · 1.31 KB
/
readbitstream.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
55
/* Copyright (C) 2015 Miquel Àngel Farré Guiu*/
function ReadBitStream(binary) //jBinary object as input
{
this.file = binary;
this._bits_left = 0;
this._buffer = 0;
}
ReadBitStream.prototype.seekg = function(position)
{
var offset_bits = position%8;
var offset_bytes = position >> 3;
this.file.seek(offset_bytes);
this._bits_left = 0;
this._buffer = 0;
if(offset_bits > 0)
{
this.read(offset_bits);
}
}
ReadBitStream.prototype.tellg = function()
{
var pos = this.file.tell()*8 - this._bits_left;
return pos;
}
ReadBitStream.prototype.read = function(n_bits_to_read)
{
if(n_bits_to_read > 64)
n_bits_to_read = 64;
var to_return = 0;
if(n_bits_to_read > 0)
{
var mask = 255;
var still_to_read = n_bits_to_read;
while( still_to_read > this._bits_left)
{
to_return = to_return + (this._buffer << (still_to_read - this._bits_left));
this._buffer = this.file.read('uint8');
still_to_read = still_to_read - this._bits_left;
this._bits_left = 8;
}
this._bits_left = this._bits_left - still_to_read;
to_return = to_return + (this._buffer >> this._bits_left);
this._buffer = this._buffer&(mask>>(8-this._bits_left));
}
return to_return;
}