a zip code crypto-currency system good for red ONLY

index.js 2.0KB

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