source: frontend/node_modules/core-js-pure/internals/uint8-from-base64.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.8 KB
Line 
1'use strict';
2var globalThis = require('../internals/global-this');
3var uncurryThis = require('../internals/function-uncurry-this');
4var anObjectOrUndefined = require('../internals/an-object-or-undefined');
5var aString = require('../internals/a-string');
6var hasOwn = require('../internals/has-own-property');
7var base64Map = require('../internals/base64-map');
8var getAlphabetOption = require('../internals/get-alphabet-option');
9var notDetached = require('../internals/array-buffer-not-detached');
10
11var base64Alphabet = base64Map.c2i;
12var base64UrlAlphabet = base64Map.c2iUrl;
13
14var SyntaxError = globalThis.SyntaxError;
15var TypeError = globalThis.TypeError;
16var at = uncurryThis(''.charAt);
17
18var skipAsciiWhitespace = function (string, index) {
19 var length = string.length;
20 for (;index < length; index++) {
21 var chr = at(string, index);
22 if (chr !== ' ' && chr !== '\t' && chr !== '\n' && chr !== '\f' && chr !== '\r') break;
23 } return index;
24};
25
26var decodeBase64Chunk = function (chunk, alphabet, throwOnExtraBits) {
27 var chunkLength = chunk.length;
28
29 if (chunkLength < 4) {
30 chunk += chunkLength === 2 ? 'AA' : 'A';
31 }
32
33 var triplet = (alphabet[at(chunk, 0)] << 18)
34 + (alphabet[at(chunk, 1)] << 12)
35 + (alphabet[at(chunk, 2)] << 6)
36 + alphabet[at(chunk, 3)];
37
38 var chunkBytes = [
39 (triplet >> 16) & 255,
40 (triplet >> 8) & 255,
41 triplet & 255
42 ];
43
44 if (chunkLength === 2) {
45 if (throwOnExtraBits && chunkBytes[1] !== 0) {
46 throw new SyntaxError('Extra bits');
47 }
48 return [chunkBytes[0]];
49 }
50
51 if (chunkLength === 3) {
52 if (throwOnExtraBits && chunkBytes[2] !== 0) {
53 throw new SyntaxError('Extra bits');
54 }
55 return [chunkBytes[0], chunkBytes[1]];
56 }
57
58 return chunkBytes;
59};
60
61var writeBytes = function (bytes, elements, written) {
62 var elementsLength = elements.length;
63 for (var index = 0; index < elementsLength; index++) {
64 bytes[written + index] = elements[index];
65 }
66 return written + elementsLength;
67};
68
69/* eslint-disable max-statements, max-depth -- TODO */
70module.exports = function (string, options, into, maxLength) {
71 aString(string);
72 anObjectOrUndefined(options);
73 var alphabet = getAlphabetOption(options) === 'base64' ? base64Alphabet : base64UrlAlphabet;
74 var lastChunkHandling = options ? options.lastChunkHandling : undefined;
75
76 if (lastChunkHandling === undefined) lastChunkHandling = 'loose';
77
78 if (lastChunkHandling !== 'loose' && lastChunkHandling !== 'strict' && lastChunkHandling !== 'stop-before-partial') {
79 throw new TypeError('Incorrect `lastChunkHandling` option');
80 }
81
82 if (into) notDetached(into.buffer);
83
84 var stringLength = string.length;
85 var bytes = into || [];
86 var written = 0;
87 var read = 0;
88 var chunk = '';
89 var index = 0;
90
91 if (maxLength) while (true) {
92 index = skipAsciiWhitespace(string, index);
93 if (index === stringLength) {
94 if (chunk.length > 0) {
95 if (lastChunkHandling === 'stop-before-partial') {
96 break;
97 }
98 if (lastChunkHandling === 'loose') {
99 if (chunk.length === 1) {
100 throw new SyntaxError('Malformed padding: exactly one additional character');
101 }
102 written = writeBytes(bytes, decodeBase64Chunk(chunk, alphabet, false), written);
103 } else {
104 throw new SyntaxError('Missing padding');
105 }
106 }
107 read = stringLength;
108 break;
109 }
110 var chr = at(string, index);
111 ++index;
112 if (chr === '=') {
113 if (chunk.length < 2) {
114 throw new SyntaxError('Padding is too early');
115 }
116 index = skipAsciiWhitespace(string, index);
117 if (chunk.length === 2) {
118 if (index === stringLength) {
119 if (lastChunkHandling === 'stop-before-partial') {
120 break;
121 }
122 throw new SyntaxError('Malformed padding: only one =');
123 }
124 if (at(string, index) === '=') {
125 ++index;
126 index = skipAsciiWhitespace(string, index);
127 }
128 }
129 if (index < stringLength) {
130 throw new SyntaxError('Unexpected character after padding');
131 }
132 written = writeBytes(bytes, decodeBase64Chunk(chunk, alphabet, lastChunkHandling === 'strict'), written);
133 read = stringLength;
134 break;
135 }
136 if (!hasOwn(alphabet, chr)) {
137 throw new SyntaxError('Unexpected character');
138 }
139 var remainingBytes = maxLength - written;
140 if (remainingBytes === 1 && chunk.length === 2 || remainingBytes === 2 && chunk.length === 3) {
141 // special case: we can fit exactly the number of bytes currently represented by chunk, so we were just checking for `=`
142 break;
143 }
144
145 chunk += chr;
146 if (chunk.length === 4) {
147 written = writeBytes(bytes, decodeBase64Chunk(chunk, alphabet, false), written);
148 chunk = '';
149 read = index;
150 if (written === maxLength) {
151 break;
152 }
153 }
154 }
155
156 return { bytes: bytes, read: read, written: written };
157};
Note: See TracBrowser for help on using the repository browser.