atob.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. function atobLookup(chr) {
  87. if (/[A-Z]/.test(chr)) {
  88. return chr.charCodeAt(0) - "A".charCodeAt(0);
  89. }
  90. if (/[a-z]/.test(chr)) {
  91. return chr.charCodeAt(0) - "a".charCodeAt(0) + 26;
  92. }
  93. if (/[0-9]/.test(chr)) {
  94. return chr.charCodeAt(0) - "0".charCodeAt(0) + 52;
  95. }
  96. if (chr === "+") {
  97. return 62;
  98. }
  99. if (chr === "/") {
  100. return 63;
  101. }
  102. // Throw exception; should not be hit in tests
  103. return undefined;
  104. }
  105. module.exports = atob;