base64.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Base64 = void 0;
  4. const _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  5. class Base64 {
  6. encode(input) {
  7. let output = '';
  8. let chr1;
  9. let chr2;
  10. let chr3;
  11. let enc1;
  12. let enc2;
  13. let enc3;
  14. let enc4;
  15. let i = 0;
  16. while (i < input.length) {
  17. chr1 = input.charCodeAt(i++);
  18. chr2 = input.charCodeAt(i++);
  19. chr3 = input.charCodeAt(i++);
  20. enc1 = chr1 >> 2;
  21. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  22. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  23. enc4 = chr3 & 63;
  24. if (isNaN(chr2))
  25. enc3 = enc4 = 64;
  26. else if (isNaN(chr3))
  27. enc4 = 64;
  28. output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  29. }
  30. return output;
  31. }
  32. encodeIgnoreUtf8(inputBytes) {
  33. let output = '';
  34. let chr1;
  35. let chr2;
  36. let chr3;
  37. let enc1;
  38. let enc2;
  39. let enc3;
  40. let enc4;
  41. let i = 0;
  42. while (i < inputBytes.length) {
  43. chr1 = inputBytes[i++];
  44. chr2 = inputBytes[i++];
  45. chr3 = inputBytes[i++];
  46. enc1 = chr1 >> 2;
  47. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  48. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  49. enc4 = chr3 & 63;
  50. if (isNaN(chr2))
  51. enc3 = enc4 = 64;
  52. else if (isNaN(chr3))
  53. enc4 = 64;
  54. output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  55. }
  56. return output;
  57. }
  58. decode(input) {
  59. let output = '';
  60. let chr1;
  61. let chr2;
  62. let chr3;
  63. let enc1;
  64. let enc2;
  65. let enc3;
  66. let enc4;
  67. let i = 0;
  68. // eslint-disable-next-line no-useless-escape
  69. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
  70. while (i < input.length) {
  71. enc1 = _keyStr.indexOf(input.charAt(i++));
  72. enc2 = _keyStr.indexOf(input.charAt(i++));
  73. enc3 = _keyStr.indexOf(input.charAt(i++));
  74. enc4 = _keyStr.indexOf(input.charAt(i++));
  75. chr1 = (enc1 << 2) | (enc2 >> 4);
  76. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  77. chr3 = ((enc3 & 3) << 6) | enc4;
  78. output = output + String.fromCharCode(chr1);
  79. if (enc3 != 64)
  80. output = output + String.fromCharCode(chr2);
  81. if (enc4 != 64)
  82. output = output + String.fromCharCode(chr3);
  83. }
  84. return this._utf8_decode(output);
  85. }
  86. decodeToByteArray(input) {
  87. let output = '';
  88. let chr1;
  89. let chr2;
  90. let chr3;
  91. let enc1;
  92. let enc2;
  93. let enc3;
  94. let enc4;
  95. let i = 0;
  96. // eslint-disable-next-line no-useless-escape
  97. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
  98. while (i < input.length) {
  99. enc1 = _keyStr.indexOf(input.charAt(i++));
  100. enc2 = _keyStr.indexOf(input.charAt(i++));
  101. enc3 = _keyStr.indexOf(input.charAt(i++));
  102. enc4 = _keyStr.indexOf(input.charAt(i++));
  103. chr1 = (enc1 << 2) | (enc2 >> 4);
  104. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  105. chr3 = ((enc3 & 3) << 6) | enc4;
  106. output = output + String.fromCharCode(chr1);
  107. if (enc3 != 64)
  108. output = output + String.fromCharCode(chr2);
  109. if (enc4 != 64)
  110. output = output + String.fromCharCode(chr3);
  111. }
  112. return this._out2ByteArray(output);
  113. }
  114. _out2ByteArray(utftext) {
  115. const byteArray = new Array(utftext.length);
  116. let i = 0;
  117. let c = 0;
  118. while (i < utftext.length) {
  119. c = utftext.charCodeAt(i);
  120. byteArray[i] = c;
  121. i++;
  122. }
  123. return byteArray;
  124. }
  125. _utf8_encode(string) {
  126. string = string.replace(/\r\n/g, '\n');
  127. let utftext = '';
  128. for (let n = 0; n < string.length; n++) {
  129. const c = string.charCodeAt(n);
  130. if (c < 128) {
  131. utftext += String.fromCharCode(c);
  132. }
  133. else if (c > 127 && c < 2048) {
  134. utftext += String.fromCharCode((c >> 6) | 192);
  135. utftext += String.fromCharCode((c & 63) | 128);
  136. }
  137. else {
  138. utftext += String.fromCharCode((c >> 12) | 224);
  139. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  140. utftext += String.fromCharCode((c & 63) | 128);
  141. }
  142. }
  143. return utftext;
  144. }
  145. _utf8_decode(utftext) {
  146. let string = '';
  147. let i = 0;
  148. let c = 0;
  149. let c2 = 0;
  150. let c3 = 0;
  151. while (i < utftext.length) {
  152. c = utftext.charCodeAt(i);
  153. if (c < 128) {
  154. string += String.fromCharCode(c);
  155. i++;
  156. }
  157. else if (c > 191 && c < 224) {
  158. c2 = utftext.charCodeAt(i + 1);
  159. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  160. i += 2;
  161. }
  162. else {
  163. c2 = utftext.charCodeAt(i + 1);
  164. c3 = utftext.charCodeAt(i + 2);
  165. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  166. i += 3;
  167. }
  168. }
  169. return string;
  170. }
  171. }
  172. exports.Base64 = Base64;
  173. //# sourceMappingURL=base64.js.map