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