atob.js 3.6 KB

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