From 38cb7aae0959e230026fd26318a1dcb3342308b8 Mon Sep 17 00:00:00 2001
From: Anne van Kesteren The btoa(data)
method must throw an "InvalidCharacterError
" DOMException
if data contains any character whose code point is greater than U+00FF. Otherwise, the
- user agent must convert data to a sequence of octets whose nth octet is the
- eight-bit representation of the code point of the nth character of data, and
- then must apply the base64 algorithm to that sequence of octets, and return the result.
The atob(data)
method, when invoked, must run the following steps:
Let decodedData be the result of running forgiving-base64 decode + on data.
Let position be a pointer into data, initially - pointing at the start of the string.
Remove all ASCII whitespace from data.
If the length of data divides by 4 leaving no remainder, then: if - data ends with one or two U+003D EQUALS SIGN (=) characters, remove them from - data.
If the length of data divides by 4 leaving a remainder of 1, throw an
- "InvalidCharacterError
" DOMException
and abort these
- steps.
If data contains a character that is not in the following list of
- characters and character ranges, throw an "InvalidCharacterError
"
- DOMException
and abort these steps:
Let output be a string, initially empty.
Let buffer be a buffer that can have bits appended to it, initially - empty.
While position does not point past the end of data, run these - substeps:
- -Find the character pointed to by position in the first column of the - following table. Let n be the number given in the second cell of the same - row.
- -Character - | Number - |
---|---|
A | 0 - |
B | 1 - |
C | 2 - |
D | 3 - |
E | 4 - |
F | 5 - |
G | 6 - |
H | 7 - |
I | 8 - |
J | 9 - |
K | 10 - |
L | 11 - |
M | 12 - |
N | 13 - |
O | 14 - |
P | 15 - |
Q | 16 - |
R | 17 - |
S | 18 - |
T | 19 - |
U | 20 - |
V | 21 - |
W | 22 - |
X | 23 - |
Y | 24 - |
Z | 25 - |
a | 26 - |
b | 27 - |
c | 28 - |
d | 29 - |
e | 30 - |
f | 31 - |
g | 32 - |
h | 33 - |
i | 34 - |
j | 35 - |
k | 36 - |
l | 37 - |
m | 38 - |
n | 39 - |
o | 40 - |
p | 41 - |
q | 42 - |
r | 43 - |
s | 44 - |
t | 45 - |
u | 46 - |
v | 47 - |
w | 48 - |
x | 49 - |
y | 50 - |
z | 51 - |
0 | 52 - |
1 | 53 - |
2 | 54 - |
3 | 55 - |
4 | 56 - |
5 | 57 - |
6 | 58 - |
7 | 59 - |
8 | 60 - |
9 | 61 - |
+ | 62 - |
/ | 63 - |
Append to buffer the six bits corresponding to number, most significant bit first.
If buffer has accumulated 24 bits, interpret them as three 8-bit - big-endian numbers. Append the three characters with code points equal to those numbers to output, in the same order, and then empty buffer.
Advance position by one character.
If buffer is not empty, it contains either 12 or 18 bits. If it contains - 12 bits, discard the last four and interpret the remaining eight as an 8-bit big-endian number. - If it contains 18 bits, discard the last two and interpret the remaining 16 as two 8-bit - big-endian numbers. Append the one or two characters with code points equal to those one or two - numbers to output, in the same order.
- -The discarded bits mean that, for instance, atob("YQ")
and
- atob("YR")
both return "a
".
Return output.
If decodedData is failure, then throw an
+ "InvalidCharacterError
" DOMException
.
Return decodedData.