source: trip-planner-front/node_modules/abab/lib/atob.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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