web.atob.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var globalThis = require('../internals/global-this');
  4. var getBuiltIn = require('../internals/get-built-in');
  5. var uncurryThis = require('../internals/function-uncurry-this');
  6. var call = require('../internals/function-call');
  7. var fails = require('../internals/fails');
  8. var toString = require('../internals/to-string');
  9. var validateArgumentsLength = require('../internals/validate-arguments-length');
  10. var c2i = require('../internals/base64-map').c2i;
  11. var disallowed = /[^\d+/a-z]/i;
  12. var whitespaces = /[\t\n\f\r ]+/g;
  13. var finalEq = /[=]{1,2}$/;
  14. var $atob = getBuiltIn('atob');
  15. var $Array = Array;
  16. var fromCharCode = String.fromCharCode;
  17. var charAt = uncurryThis(''.charAt);
  18. var replace = uncurryThis(''.replace);
  19. var join = uncurryThis([].join);
  20. var exec = uncurryThis(disallowed.exec);
  21. var BASIC = !!$atob && !fails(function () {
  22. return $atob('aGk=') !== 'hi';
  23. });
  24. var NO_SPACES_IGNORE = BASIC && fails(function () {
  25. return $atob(' ') !== '';
  26. });
  27. var NO_ENCODING_CHECK = BASIC && !fails(function () {
  28. $atob('a');
  29. });
  30. var NO_ARG_RECEIVING_CHECK = BASIC && !fails(function () {
  31. $atob();
  32. });
  33. var WRONG_ARITY = BASIC && $atob.length !== 1;
  34. var FORCED = !BASIC || NO_SPACES_IGNORE || NO_ENCODING_CHECK || NO_ARG_RECEIVING_CHECK || WRONG_ARITY;
  35. // `atob` method
  36. // https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob
  37. $({ global: true, bind: true, enumerable: true, forced: FORCED }, {
  38. atob: function atob(data) {
  39. validateArgumentsLength(arguments.length, 1);
  40. // `webpack` dev server bug on IE global methods - use call(fn, global, ...)
  41. if (BASIC && !NO_SPACES_IGNORE && !NO_ENCODING_CHECK) return call($atob, globalThis, data);
  42. var string = replace(toString(data), whitespaces, '');
  43. var position = 0;
  44. var bc = 0;
  45. var length, chr, bs;
  46. if (!(string.length & 3)) {
  47. string = replace(string, finalEq, '');
  48. }
  49. length = string.length;
  50. var lenmod = length & 3;
  51. if (lenmod === 1 || exec(disallowed, string)) {
  52. throw new (getBuiltIn('DOMException'))('The string is not correctly encoded', 'InvalidCharacterError');
  53. }
  54. // (length >> 2) is equivalent for length / 4 floored; * 3 then multiplies the
  55. // number of bytes for full quanta
  56. // lenmod is length % 4; if there's 2 or 3 bytes it's 1 or 2 bytes of extra output
  57. // respectively, so -1, however use a ternary to ensure 0 does not get -1 onto length
  58. var output = new $Array((length >> 2) * 3 + (lenmod ? lenmod - 1 : 0));
  59. var outputIndex = 0;
  60. while (position < length) {
  61. chr = charAt(string, position++);
  62. bs = bc & 3 ? (bs << 6) + c2i[chr] : c2i[chr];
  63. if (bc++ & 3) output[outputIndex++] = fromCharCode(255 & bs >> (-2 * bc & 6));
  64. }
  65. return join(output, '');
  66. }
  67. });