1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! Base64 encoding and decoding.
//!
//! This module implements RFC 4648 sections 4 and 5. The basic idea is to
//! convert every six bits of binary data into a printable character. This
//! results in a 3:4 size ratio, an overhead of 33%.
//!
//! The basic algorithm for Base64-encoding data is:
//! 1. Interpret the sequence of bytes as a sequence of bits (within each byte,
//! bits are ordered from most to least significant).
//! 2. Append zero bits until the number of bits is divisible by 6.
//! 3. Interpret the sequence of bits as a sequence of 6-bit indices (the bits
//! in the sequence are ordered from most to least significant).
//! 4. Replace each 6-bit index with a 1-byte character from the table below.
//! 5. Optionally, append padding characters until the number of characters is
//! divisible by 4.
//!
//! The basic algorithm for Base64-decoding data is:
//! 1. Append padding bytes until the number of characters is divisible by 4.
//! 2. Form groups of 4 consecutive characters in the sequence.
//! 3. Determine the number of output bytes in each 4-character group, based on
//! the number of padding characters at the end of the group, based on the
//! table below.
//! 4. Replace each character with a 6-bit index from the table below (padding
//! bytes are interpreted as indices with value 0).
//! 5. Interpret the sequence of groups of 4 6-bit indices as a sequence of
//! 24-bit strings (the bits in each index are ordered from most to least
//! significant).
//! 6. Interpret the sequence of 24-bit strings as a sequence of groups of 3
//! 8-bit bytes (the bits in the sequence are ordered from most to least
//! significant).
//! 7. Truncate each group of 3 bytes to the number of actual output bytes,
//! based on the value computed at step 3.
//! 8. Interpret the sequence of groups of bytes as a sequence of bytes.
//!
//! The following table relates, within the encoding of up to 3 bytes of binary
//! data or the decoding of 4 characters, the number of padding bytes at the end
//! of the encoded data to the number of bytes in the decoded data:
//!
//! ```text
//! Amount of Padding | 0 1 2 3 4
//! Non-Padding Bytes | 4 3 2 1 0
//! Input Size | 3 2 1 0
//! ```
//!
//! All blank entries represent invalid combinations.
//!
//! The following table relates 6-bit indices to printable characters, for the
//! RFC 6468 section 4 encoding `base64`:
//!
//! ```text
//! Bin Dec Hex | Hex ASCII | Bin Dec Hex | Hex ASCII
//! 000000 0 0 | 41 A | 100000 32 20 | 67 g
//! 000001 1 1 | 42 B | 100001 33 21 | 68 h
//! 000010 2 2 | 43 C | 100010 34 22 | 69 i
//! 000011 3 3 | 44 D | 100011 35 23 | 6A j
//! 000100 4 4 | 45 E | 100100 36 24 | 6B k
//! 000101 5 5 | 46 F | 100101 37 25 | 6C l
//! 000110 6 6 | 47 G | 100110 38 26 | 6D m
//! 000111 7 7 | 48 H | 100111 39 27 | 6E n
//! 001000 8 8 | 49 I | 101000 40 28 | 6F o
//! 001001 9 9 | 4A J | 101001 41 29 | 70 p
//! 001010 10 A | 4B K | 101010 42 2A | 71 q
//! 001011 11 B | 4C L | 101011 43 2B | 72 r
//! 001100 12 C | 4D M | 101100 44 2C | 73 s
//! 001101 13 D | 4E N | 101101 45 2D | 74 t
//! 001110 14 E | 4F O | 101110 46 2E | 75 u
//! 001111 15 F | 50 P | 101111 47 2F | 76 v
//! 010000 16 10 | 51 Q | 110000 48 30 | 77 w
//! 010001 17 11 | 52 R | 110001 49 31 | 78 x
//! 010010 18 12 | 53 S | 110010 50 32 | 79 y
//! 010011 19 13 | 54 T | 110011 51 33 | 7A z
//! 010100 20 14 | 55 U | 110100 52 34 | 30 0
//! 010101 21 15 | 56 V | 110101 53 35 | 31 1
//! 010110 22 16 | 57 W | 110110 54 36 | 32 2
//! 010111 23 17 | 58 X | 110111 55 37 | 33 3
//! 011000 24 18 | 59 Y | 111000 56 38 | 34 4
//! 011001 25 19 | 5A Z | 111001 57 39 | 35 5
//! 011010 26 1A | 61 a | 111010 58 3A | 36 6
//! 011011 27 1B | 62 b | 111011 59 3B | 37 7
//! 011100 28 1C | 63 c | 111100 60 3C | 38 8
//! 011101 29 1D | 64 d | 111101 61 3D | 39 9
//! 011110 30 1E | 65 e | 111110 62 3E | 2B +
//! 011111 31 1F | 66 f | 111111 63 3F | 2F /
//! ```
//!
//! The character `=` (hex `3D`) is used for padding.
//!
//! The following table relates 6-bit indices to printable characters, for the
//! RFC 6468 section 5 encoding `base64url`:
//!
//! ```text
//! Bin Dec Hex | Hex ASCII | Bin Dec Hex | Hex ASCII
//! 000000 0 0 | 41 A | 100000 32 20 | 67 g
//! 000001 1 1 | 42 B | 100001 33 21 | 68 h
//! 000010 2 2 | 43 C | 100010 34 22 | 69 i
//! 000011 3 3 | 44 D | 100011 35 23 | 6A j
//! 000100 4 4 | 45 E | 100100 36 24 | 6B k
//! 000101 5 5 | 46 F | 100101 37 25 | 6C l
//! 000110 6 6 | 47 G | 100110 38 26 | 6D m
//! 000111 7 7 | 48 H | 100111 39 27 | 6E n
//! 001000 8 8 | 49 I | 101000 40 28 | 6F o
//! 001001 9 9 | 4A J | 101001 41 29 | 70 p
//! 001010 10 A | 4B K | 101010 42 2A | 71 q
//! 001011 11 B | 4C L | 101011 43 2B | 72 r
//! 001100 12 C | 4D M | 101100 44 2C | 73 s
//! 001101 13 D | 4E N | 101101 45 2D | 74 t
//! 001110 14 E | 4F O | 101110 46 2E | 75 u
//! 001111 15 F | 50 P | 101111 47 2F | 76 v
//! 010000 16 10 | 51 Q | 110000 48 30 | 77 w
//! 010001 17 11 | 52 R | 110001 49 31 | 78 x
//! 010010 18 12 | 53 S | 110010 50 32 | 79 y
//! 010011 19 13 | 54 T | 110011 51 33 | 7A z
//! 010100 20 14 | 55 U | 110100 52 34 | 30 0
//! 010101 21 15 | 56 V | 110101 53 35 | 31 1
//! 010110 22 16 | 57 W | 110110 54 36 | 32 2
//! 010111 23 17 | 58 X | 110111 55 37 | 33 3
//! 011000 24 18 | 59 Y | 111000 56 38 | 34 4
//! 011001 25 19 | 5A Z | 111001 57 39 | 35 5
//! 011010 26 1A | 61 a | 111010 58 3A | 36 6
//! 011011 27 1B | 62 b | 111011 59 3B | 37 7
//! 011100 28 1C | 63 c | 111100 60 3C | 38 8
//! 011101 29 1D | 64 d | 111101 61 3D | 39 9
//! 011110 30 1E | 65 e | 111110 62 3E | 2D -
//! 011111 31 1F | 66 f | 111111 63 3F | 5F _
//! ```
//!
//! The character `=` (hex `3D`) is used for padding.
use fmt;
/// The specific Base64 format to use.
/// A decoding error.
;