blob: 7bcb396ba2c816bccf0f31d85a578251d718e539 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001/*
2node-bzip - a pure-javascript Node.JS module for decoding bzip2 data
3
4Copyright (C) 2012 Eli Skeggs
5
6Permission is hereby granted, free of charge, to any person obtaining
7a copy of this software and associated documentation files (the
8"Software"), to deal in the Software without restriction, including
9without limitation the rights to use, copy, modify, merge, publish,
10distribute, sublicense, and/or sell copies of the Software, and to
11permit persons to whom the Software is furnished to do so, subject to
12the following conditions:
13
14The above copyright notice and this permission notice shall be
15included in all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25Adapted from bzip2.js, copyright 2011 antimatter15 (antimatter15@gmail.com).
26
27Based on micro-bunzip by Rob Landley (rob@landley.net).
28
29Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
30which also acknowledges contributions by Mike Burrows, David Wheeler,
31Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
32Robert Sedgewick, and Jon L. Bentley.
33*/
34
35var BITMASK = [0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF];
36
37// offset in bytes
38var BitReader = function(stream) {
39 this.stream = stream;
40 this.bitOffset = 0;
41 this.curByte = 0;
42 this.hasByte = false;
43};
44
45BitReader.prototype._ensureByte = function() {
46 if (!this.hasByte) {
47 this.curByte = this.stream.readByte();
48 this.hasByte = true;
49 }
50};
51
52// reads bits from the buffer
53BitReader.prototype.read = function(bits) {
54 var result = 0;
55 while (bits > 0) {
56 this._ensureByte();
57 var remaining = 8 - this.bitOffset;
58 // if we're in a byte
59 if (bits >= remaining) {
60 result <<= remaining;
61 result |= BITMASK[remaining] & this.curByte;
62 this.hasByte = false;
63 this.bitOffset = 0;
64 bits -= remaining;
65 } else {
66 result <<= bits;
67 var shift = remaining - bits;
68 result |= (this.curByte & (BITMASK[bits] << shift)) >> shift;
69 this.bitOffset += bits;
70 bits = 0;
71 }
72 }
73 return result;
74};
75
76// seek to an arbitrary point in the buffer (expressed in bits)
77BitReader.prototype.seek = function(pos) {
78 var n_bit = pos % 8;
79 var n_byte = (pos - n_bit) / 8;
80 this.bitOffset = n_bit;
81 this.stream.seek(n_byte);
82 this.hasByte = false;
83};
84
85// reads 6 bytes worth of data using the read method
86BitReader.prototype.pi = function() {
87 var buf = new Buffer(6), i;
88 for (i = 0; i < buf.length; i++) {
89 buf[i] = this.read(8);
90 }
91 return buf.toString('hex');
92};
93
94module.exports = BitReader;