You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bitreader.js 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. node-bzip - a pure-javascript Node.JS module for decoding bzip2 data
  3. Copyright (C) 2012 Eli Skeggs
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, see
  14. http://www.gnu.org/licenses/lgpl-2.1.html
  15. Adapted from bzip2.js, copyright 2011 antimatter15 (antimatter15@gmail.com).
  16. Based on micro-bunzip by Rob Landley (rob@landley.net).
  17. Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
  18. which also acknowledges contributions by Mike Burrows, David Wheeler,
  19. Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
  20. Robert Sedgewick, and Jon L. Bentley.
  21. */
  22. var BITMASK = [0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF];
  23. // offset in bytes
  24. var BitReader = function(stream) {
  25. this.stream = stream;
  26. this.bitOffset = 0;
  27. this.curByte = 0;
  28. this.hasByte = false;
  29. };
  30. BitReader.prototype._ensureByte = function() {
  31. if (!this.hasByte) {
  32. this.curByte = this.stream.readByte();
  33. this.hasByte = true;
  34. }
  35. };
  36. // reads bits from the buffer
  37. BitReader.prototype.read = function(bits) {
  38. var result = 0;
  39. while (bits > 0) {
  40. this._ensureByte();
  41. var remaining = 8 - this.bitOffset;
  42. // if we're in a byte
  43. if (bits >= remaining) {
  44. result <<= remaining;
  45. result |= BITMASK[remaining] & this.curByte;
  46. this.hasByte = false;
  47. this.bitOffset = 0;
  48. bits -= remaining;
  49. } else {
  50. result <<= bits;
  51. var shift = remaining - bits;
  52. result |= (this.curByte & (BITMASK[bits] << shift)) >> shift;
  53. this.bitOffset += bits;
  54. bits = 0;
  55. }
  56. }
  57. return result;
  58. };
  59. // seek to an arbitrary point in the buffer (expressed in bits)
  60. BitReader.prototype.seek = function(pos) {
  61. var n_bit = pos % 8;
  62. var n_byte = (pos - n_bit) / 8;
  63. this.bitOffset = n_bit;
  64. this.stream.seek(n_byte);
  65. this.hasByte = false;
  66. };
  67. // reads 6 bytes worth of data using the read method
  68. BitReader.prototype.pi = function() {
  69. var buf = new Buffer(6), i;
  70. for (i = 0; i < buf.length; i++) {
  71. buf[i] = this.read(8);
  72. }
  73. return buf.toString('hex');
  74. };
  75. module.exports = BitReader;