base64.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (function(f) {
  2. 'use strict';
  3. /* istanbul ignore else */
  4. if (typeof exports === 'object' && exports != null &&
  5. typeof exports.nodeType !== 'number') {
  6. module.exports = f ();
  7. } else if (typeof define === 'function' && define.amd != null) {
  8. define ([], f);
  9. } else {
  10. var base64 = f ();
  11. var global = typeof self !== 'undefined' ? self : $.global;
  12. if (typeof global.btoa !== 'function') global.btoa = base64.btoa;
  13. if (typeof global.atob !== 'function') global.atob = base64.atob;
  14. }
  15. } (function() {
  16. 'use strict';
  17. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  18. function InvalidCharacterError(message) {
  19. this.message = message;
  20. }
  21. InvalidCharacterError.prototype = new Error ();
  22. InvalidCharacterError.prototype.name = 'InvalidCharacterError';
  23. // encoder
  24. // [https://gist.github.com/999166] by [https://github.com/nignag]
  25. function btoa(input) {
  26. var str = String (input);
  27. for (
  28. // initialize result and counter
  29. var block, charCode, idx = 0, map = chars, output = '';
  30. // if the next str index does not exist:
  31. // change the mapping table to "="
  32. // check if d has no fractional digits
  33. str.charAt (idx | 0) || (map = '=', idx % 1);
  34. // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
  35. output += map.charAt (63 & block >> 8 - idx % 1 * 8)
  36. ) {
  37. charCode = str.charCodeAt (idx += 3 / 4);
  38. if (charCode > 0xFF) {
  39. throw new InvalidCharacterError ("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
  40. }
  41. block = block << 8 | charCode;
  42. }
  43. return output;
  44. }
  45. // decoder
  46. // [https://gist.github.com/1020396] by [https://github.com/atk]
  47. function atob(input) {
  48. var str = (String (input)).replace (/[=]+$/, ''); // #31: ExtendScript bad parse of /=
  49. if (str.length % 4 === 1) {
  50. throw new InvalidCharacterError ("'atob' failed: The string to be decoded is not correctly encoded.");
  51. }
  52. for (
  53. // initialize result and counters
  54. var bc = 0, bs, buffer, idx = 0, output = '';
  55. // get next character
  56. buffer = str.charAt (idx++); // eslint-disable-line no-cond-assign
  57. // character found in table? initialize bit storage and add its ascii value;
  58. ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
  59. // and if not first of each 4 characters,
  60. // convert the first 8 bits to one ascii character
  61. bc++ % 4) ? output += String.fromCharCode (255 & bs >> (-2 * bc & 6)) : 0
  62. ) {
  63. // try to find character in table (0-63, not found => -1)
  64. buffer = chars.indexOf (buffer);
  65. }
  66. return output;
  67. }
  68. return {btoa: btoa, atob: atob};
  69. }));