You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.0 KiB

  1. // Github: https://github.com/shdwjk/Roll20API/blob/master/Base64/Base64.js
  2. // By: The Aaron, Arcane Scriptomancer
  3. // Contact: https://app.roll20.net/users/104025/the-aaron
  4. // modified from: http://www.webtoolkit.info/
  5. const Base64 = (() => { // eslint-disable-line no-unused-vars
  6. const version = '0.3.2';
  7. const lastUpdate = 1576507905;
  8. const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  9. const checkInstall = () => {
  10. log('-=> Base64 v'+version+' <=- ['+(new Date(lastUpdate*1000))+']');
  11. };
  12. // private method for UTF-8 encoding
  13. const utf8_encode = (string) => {
  14. let utftext = '';
  15. for (let n = 0; n < string.length; n++) {
  16. let c1 = string.charCodeAt(n);
  17. if (c1 < 128) {
  18. utftext += String.fromCharCode(c1);
  19. }
  20. else if((c1 > 127) && (c1 < 2048)) {
  21. utftext += String.fromCharCode((c1 >> 6) | 192);
  22. utftext += String.fromCharCode((c1 & 63) | 128);
  23. }
  24. else {
  25. utftext += String.fromCharCode((c1 >> 12) | 224);
  26. utftext += String.fromCharCode(((c1 >> 6) & 63) | 128);
  27. utftext += String.fromCharCode((c1 & 63) | 128);
  28. }
  29. }
  30. return utftext;
  31. };
  32. // private method for UTF-8 decoding
  33. const utf8_decode = (utftext) => {
  34. let string = '';
  35. let i = 0;
  36. while ( i < utftext.length ) {
  37. let c1 = utftext.charCodeAt(i);
  38. if (c1 < 128) {
  39. string += String.fromCharCode(c1);
  40. i++;
  41. }
  42. else if((c1 > 191) && (c1 < 224)) {
  43. let c2 = utftext.charCodeAt(i+1);
  44. string += String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
  45. i += 2;
  46. }
  47. else {
  48. let c2 = utftext.charCodeAt(i+1);
  49. let c3 = utftext.charCodeAt(i+2);
  50. string += String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  51. i += 3;
  52. }
  53. }
  54. return string;
  55. };
  56. const encode = (input) => {
  57. let output = '';
  58. let i = 0;
  59. input = utf8_encode(input);
  60. while (i < input.length) {
  61. let chr1 = input.charCodeAt(i++);
  62. let chr2 = input.charCodeAt(i++);
  63. let chr3 = input.charCodeAt(i++);
  64. let enc1 = chr1 >> 2;
  65. let enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  66. let enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  67. let enc4 = chr3 & 63;
  68. if (isNaN(chr2)) {
  69. enc3 = enc4 = 64;
  70. } else if (isNaN(chr3)) {
  71. enc4 = 64;
  72. }
  73. output = output +
  74. keyStr.charAt(enc1) + keyStr.charAt(enc2) +
  75. keyStr.charAt(enc3) + keyStr.charAt(enc4);
  76. }
  77. return output;
  78. };
  79. // public method for decoding
  80. const decode = (input) => {
  81. let output = '';
  82. let i = 0;
  83. input = input.replace(/[^A-Za-z0-9+/=]/g, "");
  84. while (i < input.length) {
  85. let enc1 = keyStr.indexOf(input.charAt(i++));
  86. let enc2 = keyStr.indexOf(input.charAt(i++));
  87. let enc3 = keyStr.indexOf(input.charAt(i++));
  88. let enc4 = keyStr.indexOf(input.charAt(i++));
  89. let chr1 = (enc1 << 2) | (enc2 >> 4);
  90. let chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  91. let chr3 = ((enc3 & 3) << 6) | enc4;
  92. output = output + String.fromCharCode(chr1);
  93. if (enc3 !== 64) {
  94. output = output + String.fromCharCode(chr2);
  95. }
  96. if (enc4 !== 64) {
  97. output = output + String.fromCharCode(chr3);
  98. }
  99. }
  100. output = utf8_decode(output);
  101. return output;
  102. };
  103. on("ready",()=>{
  104. checkInstall();
  105. });
  106. return {
  107. encode: encode,
  108. decode: decode
  109. };
  110. })();