index.cjs.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.read = read;
  6. exports.write = write;
  7. function read(buffer, offset, isLE, mLen, nBytes) {
  8. var e, m;
  9. var eLen = nBytes * 8 - mLen - 1;
  10. var eMax = (1 << eLen) - 1;
  11. var eBias = eMax >> 1;
  12. var nBits = -7;
  13. var i = isLE ? nBytes - 1 : 0;
  14. var d = isLE ? -1 : 1;
  15. var s = buffer[offset + i];
  16. i += d;
  17. e = s & (1 << -nBits) - 1;
  18. s >>= -nBits;
  19. nBits += eLen;
  20. for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  21. m = e & (1 << -nBits) - 1;
  22. e >>= -nBits;
  23. nBits += mLen;
  24. for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  25. if (e === 0) {
  26. e = 1 - eBias;
  27. } else if (e === eMax) {
  28. return m ? NaN : (s ? -1 : 1) * Infinity;
  29. } else {
  30. m = m + Math.pow(2, mLen);
  31. e = e - eBias;
  32. }
  33. return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
  34. }
  35. function write(buffer, value, offset, isLE, mLen, nBytes) {
  36. var e, m, c;
  37. var eLen = nBytes * 8 - mLen - 1;
  38. var eMax = (1 << eLen) - 1;
  39. var eBias = eMax >> 1;
  40. var rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0;
  41. var i = isLE ? 0 : nBytes - 1;
  42. var d = isLE ? 1 : -1;
  43. var s = value < 0 || value === 0 && 1 / value < 0 ? 1 : 0;
  44. value = Math.abs(value);
  45. if (isNaN(value) || value === Infinity) {
  46. m = isNaN(value) ? 1 : 0;
  47. e = eMax;
  48. } else {
  49. e = Math.floor(Math.log(value) / Math.LN2);
  50. if (value * (c = Math.pow(2, -e)) < 1) {
  51. e--;
  52. c *= 2;
  53. }
  54. if (e + eBias >= 1) {
  55. value += rt / c;
  56. } else {
  57. value += rt * Math.pow(2, 1 - eBias);
  58. }
  59. if (value * c >= 2) {
  60. e++;
  61. c /= 2;
  62. }
  63. if (e + eBias >= eMax) {
  64. m = 0;
  65. e = eMax;
  66. } else if (e + eBias >= 1) {
  67. m = (value * c - 1) * Math.pow(2, mLen);
  68. e = e + eBias;
  69. } else {
  70. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
  71. e = 0;
  72. }
  73. }
  74. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
  75. e = e << mLen | m;
  76. eLen += mLen;
  77. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
  78. buffer[offset + i - d] |= s * 128;
  79. }