binaryReader.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // BinaryReader
  2. // Refactored by Vjeux <vjeuxx@gmail.com>
  3. // http://blog.vjeux.com/2010/javascript/javascript-binary-reader.html
  4. // Original
  5. //+ Jonas Raoni Soares Silva
  6. //@ http://jsfromhell.com/classes/binary-parser [rev. #1]
  7. BinaryReader = function (data) {
  8. this._buffer = data;
  9. this._pos = 0;
  10. };
  11. BinaryReader.prototype = {
  12. /* Public */
  13. readInt8: function (){ return this._decodeInt(8, true); },
  14. readUInt8: function (){ return this._decodeInt(8, false); },
  15. readInt16: function (){ return this._decodeInt(16, true); },
  16. readUInt16: function (){ return this._decodeInt(16, false); },
  17. readInt32: function (){ return this._decodeInt(32, true); },
  18. readUInt32: function (){ return this._decodeInt(32, false); },
  19. readFloat: function (){ return this._decodeFloat(23, 8); },
  20. readDouble: function (){ return this._decodeFloat(52, 11); },
  21. readChar: function () { return this.readString(1); },
  22. readString: function (length) {
  23. this._checkSize(length * 8);
  24. var result = this._buffer.substr(this._pos, length);
  25. this._pos += length;
  26. return result;
  27. },
  28. seek: function (pos) {
  29. this._pos = pos;
  30. this._checkSize(0);
  31. },
  32. getPosition: function () {
  33. return this._pos;
  34. },
  35. getSize: function () {
  36. return this._buffer.length;
  37. },
  38. /* Private */
  39. _decodeFloat: function(precisionBits, exponentBits){
  40. var length = precisionBits + exponentBits + 1;
  41. var size = length >> 3;
  42. this._checkSize(length);
  43. var bias = Math.pow(2, exponentBits - 1) - 1;
  44. var signal = this._readBits(precisionBits + exponentBits, 1, size);
  45. var exponent = this._readBits(precisionBits, exponentBits, size);
  46. var significand = 0;
  47. var divisor = 2;
  48. // var curByte = length + (-precisionBits >> 3) - 1;
  49. var curByte = 0;
  50. do {
  51. var byteValue = this._readByte(++curByte, size);
  52. var startBit = precisionBits % 8 || 8;
  53. var mask = 1 << startBit;
  54. while (mask >>= 1) {
  55. if (byteValue & mask) {
  56. significand += 1 / divisor;
  57. }
  58. divisor *= 2;
  59. }
  60. } while (precisionBits -= startBit);
  61. this._pos += size;
  62. return exponent == (bias << 1) + 1 ? significand ? NaN : signal ? -Infinity : +Infinity
  63. : (1 + signal * -2) * (exponent || significand ? !exponent ? Math.pow(2, -bias + 1) * significand
  64. : Math.pow(2, exponent - bias) * (1 + significand) : 0);
  65. },
  66. _decodeInt: function(bits, signed){
  67. var x = this._readBits(0, bits, bits / 8), max = Math.pow(2, bits);
  68. var result = signed && x >= max / 2 ? x - max : x;
  69. this._pos += bits / 8;
  70. return result;
  71. },
  72. //shl fix: Henri Torgemane ~1996 (compressed by Jonas Raoni)
  73. _shl: function (a, b){
  74. for (++b; --b; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1);
  75. return a;
  76. },
  77. _readByte: function (i, size) {
  78. return this._buffer.charCodeAt(this._pos + size - i - 1) & 0xff;
  79. },
  80. _readBits: function (start, length, size) {
  81. var offsetLeft = (start + length) % 8;
  82. var offsetRight = start % 8;
  83. var curByte = size - (start >> 3) - 1;
  84. var lastByte = size + (-(start + length) >> 3);
  85. var diff = curByte - lastByte;
  86. var sum = (this._readByte(curByte, size) >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1);
  87. if (diff && offsetLeft) {
  88. sum += (this._readByte(lastByte++, size) & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight;
  89. }
  90. while (diff) {
  91. sum += this._shl(this._readByte(lastByte++, size), (diff-- << 3) - offsetRight);
  92. }
  93. return sum;
  94. },
  95. _checkSize: function (neededBits) {
  96. if (!(this._pos + Math.ceil(neededBits / 8) < this._buffer.length)) {
  97. throw new Error("Index out of bound");
  98. }
  99. }
  100. };