atob.js 3.5 KB

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