bitreader.js 2.9 KB

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