basex.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Generated by IcedCoffeeScript 1.7.1-c
  2. (function() {
  3. var BaseX, BigInteger, base32, base58, buffer_to_ui8a, nbi, nbs, nbv, _ref;
  4. _ref = require('bn'), nbv = _ref.nbv, nbi = _ref.nbi, BigInteger = _ref.BigInteger;
  5. nbs = require('./bn').nbs;
  6. buffer_to_ui8a = require('./util').buffer_to_ui8a;
  7. BaseX = (function() {
  8. function BaseX(alphabet) {
  9. var a, i, _i, _len, _ref1;
  10. this.alphabet = alphabet;
  11. this.base = this.alphabet.length;
  12. this.basebn = nbv(this.base);
  13. this.lookup = {};
  14. _ref1 = this.alphabet;
  15. for (i = _i = 0, _len = _ref1.length; _i < _len; i = ++_i) {
  16. a = _ref1[i];
  17. this.lookup[a] = i;
  18. }
  19. }
  20. BaseX.prototype.encode = function(buffer) {
  21. var c, chars, num, pad, q, r, _i, _len;
  22. num = nbi().fromBuffer(buffer);
  23. chars = (function() {
  24. var _ref1, _results;
  25. _results = [];
  26. while (num.compareTo(BigInteger.ZERO) > 0) {
  27. _ref1 = num.divideAndRemainder(this.basebn), q = _ref1[0], r = _ref1[1];
  28. c = this.alphabet[r.intValue()];
  29. num = q;
  30. _results.push(c);
  31. }
  32. return _results;
  33. }).call(this);
  34. chars.reverse();
  35. pad = [];
  36. for (_i = 0, _len = buffer.length; _i < _len; _i++) {
  37. c = buffer[_i];
  38. if (c === 0) {
  39. pad.push(this.alphabet[0]);
  40. } else {
  41. break;
  42. }
  43. }
  44. return (pad.concat(chars)).join('');
  45. };
  46. BaseX.prototype.decode = function(str) {
  47. var base, c, char_index, i, num, pad, start, _i, _j, _len, _ref1;
  48. num = BigInteger.ZERO;
  49. base = BigInteger.ONE;
  50. i = 0;
  51. for (i = _i = 0, _len = str.length; _i < _len; i = ++_i) {
  52. c = str[i];
  53. if (c !== this.alphabet[0]) {
  54. break;
  55. }
  56. }
  57. start = i;
  58. pad = new Buffer((function() {
  59. var _j, _results;
  60. _results = [];
  61. for (i = _j = 0; 0 <= start ? _j < start : _j > start; i = 0 <= start ? ++_j : --_j) {
  62. _results.push(0);
  63. }
  64. return _results;
  65. })());
  66. _ref1 = str.slice(start);
  67. for (i = _j = _ref1.length - 1; _j >= 0; i = _j += -1) {
  68. c = _ref1[i];
  69. if ((char_index = this.lookup[c]) == null) {
  70. throw new Error('Value passed is not a valid BaseX string.');
  71. }
  72. num = num.add(base.multiply(nbv(char_index)));
  73. base = base.multiply(this.basebn);
  74. }
  75. return Buffer.concat([pad, new Buffer(num.toByteArray())]);
  76. };
  77. return BaseX;
  78. })();
  79. exports.base58 = base58 = new BaseX('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz');
  80. exports.base32 = base32 = new BaseX('abcdefghijkmnpqrstuvwxyz23456789');
  81. exports.base91 = new BaseX("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`~!@#$%^&*()-_=+{}[]|;:,<>./?");
  82. }).call(this);