base64.js 5.5 KB

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