| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | /* |
| 2 | node-bzip - a pure-javascript Node.JS module for decoding bzip2 data |
| 3 | |
| 4 | Copyright (C) 2012 Eli Skeggs |
| 5 | |
| 6 | Permission is hereby granted, free of charge, to any person obtaining |
| 7 | a copy of this software and associated documentation files (the |
| 8 | "Software"), to deal in the Software without restriction, including |
| 9 | without limitation the rights to use, copy, modify, merge, publish, |
| 10 | distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | permit persons to whom the Software is furnished to do so, subject to |
| 12 | the following conditions: |
| 13 | |
| 14 | The above copyright notice and this permission notice shall be |
| 15 | included in all copies or substantial portions of the Software. |
| 16 | |
| 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 21 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | |
| 25 | Adapted from bzip2.js, copyright 2011 antimatter15 (antimatter15@gmail.com). |
| 26 | |
| 27 | Based on micro-bunzip by Rob Landley (rob@landley.net). |
| 28 | |
| 29 | Based on bzip2 decompression code by Julian R Seward (jseward@acm.org), |
| 30 | which also acknowledges contributions by Mike Burrows, David Wheeler, |
| 31 | Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten, |
| 32 | Robert Sedgewick, and Jon L. Bentley. |
| 33 | */ |
| 34 | |
| 35 | var BITMASK = [0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF]; |
| 36 | |
| 37 | // offset in bytes |
| 38 | var BitReader = function(stream) { |
| 39 | this.stream = stream; |
| 40 | this.bitOffset = 0; |
| 41 | this.curByte = 0; |
| 42 | this.hasByte = false; |
| 43 | }; |
| 44 | |
| 45 | BitReader.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 |
| 53 | BitReader.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) |
| 77 | BitReader.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 |
| 86 | BitReader.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 | |
| 94 | module.exports = BitReader; |