UNPKG

3.68 kBJavaScriptView Raw
1"use strict";
2
3/**
4 * Implementation of atob() according to the HTML and Infra specs, except that
5 * instead of throwing INVALID_CHARACTER_ERR we return null.
6 */
7function atob(data) {
8 if (arguments.length === 0) {
9 throw new TypeError("1 argument required, but only 0 present.");
10 }
11
12 // Web IDL requires DOMStrings to just be converted using ECMAScript
13 // ToString, which in our case amounts to using a template literal.
14 data = `${data}`;
15 // "Remove all ASCII whitespace from data."
16 data = data.replace(/[ \t\n\f\r]/g, "");
17 // "If data's length divides by 4 leaving no remainder, then: if data ends
18 // with one or two U+003D (=) code points, then remove them from data."
19 if (data.length % 4 === 0) {
20 data = data.replace(/==?$/, "");
21 }
22 // "If data's length divides by 4 leaving a remainder of 1, then return
23 // failure."
24 //
25 // "If data contains a code point that is not one of
26 //
27 // U+002B (+)
28 // U+002F (/)
29 // ASCII alphanumeric
30 //
31 // then return failure."
32 if (data.length % 4 === 1 || /[^+/0-9A-Za-z]/.test(data)) {
33 return null;
34 }
35 // "Let output be an empty byte sequence."
36 let output = "";
37 // "Let buffer be an empty buffer that can have bits appended to it."
38 //
39 // We append bits via left-shift and or. accumulatedBits is used to track
40 // when we've gotten to 24 bits.
41 let buffer = 0;
42 let accumulatedBits = 0;
43 // "Let position be a position variable for data, initially pointing at the
44 // start of data."
45 //
46 // "While position does not point past the end of data:"
47 for (let i = 0; i < data.length; i++) {
48 // "Find the code point pointed to by position in the second column of
49 // Table 1: The Base 64 Alphabet of RFC 4648. Let n be the number given in
50 // the first cell of the same row.
51 //
52 // "Append to buffer the six bits corresponding to n, most significant bit
53 // first."
54 //
55 // atobLookup() implements the table from RFC 4648.
56 buffer <<= 6;
57 buffer |= atobLookup(data[i]);
58 accumulatedBits += 6;
59 // "If buffer has accumulated 24 bits, interpret them as three 8-bit
60 // big-endian numbers. Append three bytes with values equal to those
61 // numbers to output, in the same order, and then empty buffer."
62 if (accumulatedBits === 24) {
63 output += String.fromCharCode((buffer & 0xff0000) >> 16);
64 output += String.fromCharCode((buffer & 0xff00) >> 8);
65 output += String.fromCharCode(buffer & 0xff);
66 buffer = accumulatedBits = 0;
67 }
68 // "Advance position by 1."
69 }
70 // "If buffer is not empty, it contains either 12 or 18 bits. If it contains
71 // 12 bits, then discard the last four and interpret the remaining eight as
72 // an 8-bit big-endian number. If it contains 18 bits, then discard the last
73 // two and interpret the remaining 16 as two 8-bit big-endian numbers. Append
74 // the one or two bytes with values equal to those one or two numbers to
75 // output, in the same order."
76 if (accumulatedBits === 12) {
77 buffer >>= 4;
78 output += String.fromCharCode(buffer);
79 } else if (accumulatedBits === 18) {
80 buffer >>= 2;
81 output += String.fromCharCode((buffer & 0xff00) >> 8);
82 output += String.fromCharCode(buffer & 0xff);
83 }
84 // "Return output."
85 return output;
86}
87/**
88 * A lookup table for atob(), which converts an ASCII character to the
89 * corresponding six-bit number.
90 */
91
92const keystr =
93 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
94
95function atobLookup(chr) {
96 const index = keystr.indexOf(chr);
97 // Throw exception if character is not in the lookup string; should not be hit in tests
98 return index < 0 ? undefined : index;
99}
100
101module.exports = atob;