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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2007-2017. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%% Description: Implements base 64 encode and decode. See RFC4648.
-module(base64).
-export([encode/1, decode/1, mime_decode/1,
encode_to_string/1, decode_to_string/1, mime_decode_to_string/1]).
%% The following type is a subtype of string() for return values
%% of (some) functions of this module.
-type ascii_string() :: [1..255].
-type ascii_binary() :: binary().
-spec encode_to_string(Data) -> Base64String when
Data :: ascii_string() | ascii_binary(),
Base64String :: ascii_string().
encode_to_string(Bin) when is_binary(Bin) ->
encode_to_string(binary_to_list(Bin));
encode_to_string(List) when is_list(List) ->
encode_list_to_string(List).
-spec encode(Data) -> Base64 when
Data :: ascii_string() | ascii_binary(),
Base64 :: ascii_binary().
encode(Bin) when is_binary(Bin) ->
encode_binary(Bin, <<>>);
encode(List) when is_list(List) ->
encode_list(List, <<>>).
encode_list_to_string([]) ->
[];
encode_list_to_string([B1]) ->
[b64e(B1 bsr 2),
b64e((B1 band 3) bsl 4), $=, $=];
encode_list_to_string([B1,B2]) ->
[b64e(B1 bsr 2),
b64e(((B1 band 3) bsl 4) bor (B2 bsr 4)),
b64e((B2 band 15) bsl 2), $=];
encode_list_to_string([B1,B2,B3|Ls]) ->
BB = (B1 bsl 16) bor (B2 bsl 8) bor B3,
[b64e(BB bsr 18),
b64e((BB bsr 12) band 63),
b64e((BB bsr 6) band 63),
b64e(BB band 63) | encode_list_to_string(Ls)].
encode_binary(<<>>, A) ->
A;
encode_binary(<<B1:8>>, A) ->
<<A/binary,(b64e(B1 bsr 2)):8,(b64e((B1 band 3) bsl 4)):8,$=:8,$=:8>>;
encode_binary(<<B1:8, B2:8>>, A) ->
<<A/binary,(b64e(B1 bsr 2)):8,
(b64e(((B1 band 3) bsl 4) bor (B2 bsr 4))):8,
(b64e((B2 band 15) bsl 2)):8, $=:8>>;
encode_binary(<<B1:8, B2:8, B3:8, Ls/bits>>, A) ->
BB = (B1 bsl 16) bor (B2 bsl 8) bor B3,
encode_binary(Ls,
<<A/binary,(b64e(BB bsr 18)):8,
(b64e((BB bsr 12) band 63)):8,
(b64e((BB bsr 6) band 63)):8,
(b64e(BB band 63)):8>>).
encode_list([], A) ->
A;
encode_list([B1], A) ->
<<A/binary,(b64e(B1 bsr 2)):8,(b64e((B1 band 3) bsl 4)):8,$=:8,$=:8>>;
encode_list([B1,B2], A) ->
<<A/binary,(b64e(B1 bsr 2)):8,
(b64e(((B1 band 3) bsl 4) bor (B2 bsr 4))):8,
(b64e((B2 band 15) bsl 2)):8, $=:8>>;
encode_list([B1,B2,B3|Ls], A) ->
BB = (B1 bsl 16) bor (B2 bsl 8) bor B3,
encode_list(Ls,
<<A/binary,(b64e(BB bsr 18)):8,
(b64e((BB bsr 12) band 63)):8,
(b64e((BB bsr 6) band 63)):8,
(b64e(BB band 63)):8>>).
%% mime_decode strips away all characters not Base64 before
%% converting, whereas decode crashes if an illegal character is found
-spec decode(Base64) -> Data when
Base64 :: ascii_string() | ascii_binary(),
Data :: ascii_binary().
decode(Bin) when is_binary(Bin) ->
decode_binary(Bin, <<>>);
decode(List) when is_list(List) ->
decode_list(List, <<>>).
-spec mime_decode(Base64) -> Data when
Base64 :: ascii_string() | ascii_binary(),
Data :: ascii_binary().
mime_decode(Bin) when is_binary(Bin) ->
mime_decode_binary(Bin, <<>>);
mime_decode(List) when is_list(List) ->
mime_decode_list(List, <<>>).
%% mime_decode_to_string strips away all characters not Base64 before
%% converting, whereas decode_to_string crashes if an illegal
%% character is found
-spec decode_to_string(Base64) -> DataString when
Base64 :: ascii_string() | ascii_binary(),
DataString :: ascii_string().
decode_to_string(Bin) when is_binary(Bin) ->
decode_to_string(binary_to_list(Bin));
decode_to_string(List) when is_list(List) ->
decode_list_to_string(List).
-spec mime_decode_to_string(Base64) -> DataString when
Base64 :: ascii_string() | ascii_binary(),
DataString :: ascii_string().
mime_decode_to_string(Bin) when is_binary(Bin) ->
mime_decode_to_string(binary_to_list(Bin));
mime_decode_to_string(List) when is_list(List) ->
mime_decode_list_to_string(List).
%% Skipping pad character if not at end of string. Also liberal about
%% excess padding and skipping of other illegal (non-base64 alphabet)
%% characters. See section 3.3 of RFC4648
mime_decode_list([0 | Cs], A) ->
mime_decode_list(Cs, A);
mime_decode_list([C1 | Cs], A) ->
case b64d(C1) of
B1 when is_integer(B1) -> mime_decode_list(Cs, A, B1);
_ -> mime_decode_list(Cs, A) % eq is padding
end;
mime_decode_list([], A) ->
A.
mime_decode_list([0 | Cs], A, B1) ->
mime_decode_list(Cs, A, B1);
mime_decode_list([C2 | Cs], A, B1) ->
case b64d(C2) of
B2 when is_integer(B2) ->
mime_decode_list(Cs, A, B1, B2);
_ -> mime_decode_list(Cs, A, B1) % eq is padding
end.
mime_decode_list([0 | Cs], A, B1, B2) ->
mime_decode_list(Cs, A, B1, B2);
mime_decode_list([C3 | Cs], A, B1, B2) ->
case b64d(C3) of
B3 when is_integer(B3) ->
mime_decode_list(Cs, A, B1, B2, B3);
eq=B3 ->
mime_decode_list_after_eq(Cs, A, B1, B2, B3);
_ -> mime_decode_list(Cs, A, B1, B2)
end.
mime_decode_list([0 | Cs], A, B1, B2, B3) ->
mime_decode_list(Cs, A, B1, B2, B3);
mime_decode_list([C4 | Cs], A, B1, B2, B3) ->
case b64d(C4) of
B4 when is_integer(B4) ->
mime_decode_list(Cs, <<A/bits,B1:6,B2:6,B3:6,B4:6>>);
eq ->
mime_decode_list_after_eq(Cs, A, B1, B2, B3);
_ -> mime_decode_list(Cs, A, B1, B2, B3)
end.
mime_decode_list_after_eq([0 | Cs], A, B1, B2, B3) ->
mime_decode_list_after_eq(Cs, A, B1, B2, B3);
mime_decode_list_after_eq([C | Cs], A, B1, B2, B3) ->
case b64d(C) of
B when is_integer(B) ->
%% More valid data, skip the eq as invalid
case B3 of
eq -> mime_decode_list(Cs, A, B1, B2, B);
_ -> mime_decode_list(Cs, <<A/bits,B1:6,B2:6,B3:6,B:6>>)
end;
_ -> mime_decode_list_after_eq(Cs, A, B1, B2, B3)
end;
mime_decode_list_after_eq([], A, B1, B2, eq) ->
<<A/binary,B1:6,(B2 bsr 4):2>>;
mime_decode_list_after_eq([], A, B1, B2, B3) ->
<<A/binary,B1:6,B2:6,(B3 bsr 2):4>>.
mime_decode_binary(<<0:8, Cs/bits>>, A) ->
mime_decode_binary(Cs, A);
mime_decode_binary(<<C1:8, Cs/bits>>, A) ->
case b64d(C1) of
B1 when is_integer(B1) -> mime_decode_binary(Cs, A, B1);
_ -> mime_decode_binary(Cs, A) % eq is padding
end;
mime_decode_binary(<<>>, A) ->
A.
mime_decode_binary(<<0:8, Cs/bits>>, A, B1) ->
mime_decode_binary(Cs, A, B1);
mime_decode_binary(<<C2:8, Cs/bits>>, A, B1) ->
case b64d(C2) of
B2 when is_integer(B2) ->
mime_decode_binary(Cs, A, B1, B2);
_ -> mime_decode_binary(Cs, A, B1) % eq is padding
end.
mime_decode_binary(<<0:8, Cs/bits>>, A, B1, B2) ->
mime_decode_binary(Cs, A, B1, B2);
mime_decode_binary(<<C3:8, Cs/bits>>, A, B1, B2) ->
case b64d(C3) of
B3 when is_integer(B3) ->
mime_decode_binary(Cs, A, B1, B2, B3);
eq=B3 ->
mime_decode_binary_after_eq(Cs, A, B1, B2, B3);
_ -> mime_decode_binary(Cs, A, B1, B2)
end.
mime_decode_binary(<<0:8, Cs/bits>>, A, B1, B2, B3) ->
mime_decode_binary(Cs, A, B1, B2, B3);
mime_decode_binary(<<C4:8, Cs/bits>>, A, B1, B2, B3) ->
case b64d(C4) of
B4 when is_integer(B4) ->
mime_decode_binary(Cs, <<A/bits,B1:6,B2:6,B3:6,B4:6>>);
eq ->
mime_decode_binary_after_eq(Cs, A, B1, B2, B3);
_ -> mime_decode_binary(Cs, A, B1, B2, B3)
end.
mime_decode_binary_after_eq(<<0:8, Cs/bits>>, A, B1, B2, B3) ->
mime_decode_binary_after_eq(Cs, A, B1, B2, B3);
mime_decode_binary_after_eq(<<C:8, Cs/bits>>, A, B1, B2, B3) ->
case b64d(C) of
B when is_integer(B) ->
%% More valid data, skip the eq as invalid
case B3 of
eq -> mime_decode_binary(Cs, A, B1, B2, B);
_ -> mime_decode_binary(Cs, <<A/bits,B1:6,B2:6,B3:6,B:6>>)
end;
_ -> mime_decode_binary_after_eq(Cs, A, B1, B2, B3)
end;
mime_decode_binary_after_eq(<<>>, A, B1, B2, eq) ->
<<A/binary,B1:6,(B2 bsr 4):2>>;
mime_decode_binary_after_eq(<<>>, A, B1, B2, B3) ->
<<A/binary,B1:6,B2:6,(B3 bsr 2):4>>.
mime_decode_list_to_string([0 | Cs]) ->
mime_decode_list_to_string(Cs);
mime_decode_list_to_string([C1 | Cs]) ->
case b64d(C1) of
B1 when is_integer(B1) -> mime_decode_list_to_string(Cs, B1);
_ -> mime_decode_list_to_string(Cs) % eq is padding
end;
mime_decode_list_to_string([]) ->
[].
mime_decode_list_to_string([0 | Cs], B1) ->
mime_decode_list_to_string(Cs, B1);
mime_decode_list_to_string([C2 | Cs], B1) ->
case b64d(C2) of
B2 when is_integer(B2) ->
mime_decode_list_to_string(Cs, B1, B2);
_ -> mime_decode_list_to_string(Cs, B1) % eq is padding
end.
mime_decode_list_to_string([0 | Cs], B1, B2) ->
mime_decode_list_to_string(Cs, B1, B2);
mime_decode_list_to_string([C3 | Cs], B1, B2) ->
case b64d(C3) of
B3 when is_integer(B3) ->
mime_decode_list_to_string(Cs, B1, B2, B3);
eq=B3 -> mime_decode_list_to_string_after_eq(Cs, B1, B2, B3);
_ -> mime_decode_list_to_string(Cs, B1, B2)
end.
mime_decode_list_to_string([0 | Cs], B1, B2, B3) ->
mime_decode_list_to_string(Cs, B1, B2, B3);
mime_decode_list_to_string([C4 | Cs], B1, B2, B3) ->
case b64d(C4) of
B4 when is_integer(B4) ->
Bits4x6 = (B1 bsl 18) bor (B2 bsl 12) bor (B3 bsl 6) bor B4,
Octet1 = Bits4x6 bsr 16,
Octet2 = (Bits4x6 bsr 8) band 16#ff,
Octet3 = Bits4x6 band 16#ff,
[Octet1, Octet2, Octet3 | mime_decode_list_to_string(Cs)];
eq ->
mime_decode_list_to_string_after_eq(Cs, B1, B2, B3);
_ -> mime_decode_list_to_string(Cs, B1, B2, B3)
end.
mime_decode_list_to_string_after_eq([0 | Cs], B1, B2, B3) ->
mime_decode_list_to_string_after_eq(Cs, B1, B2, B3);
mime_decode_list_to_string_after_eq([C | Cs], B1, B2, B3) ->
case b64d(C) of
B when is_integer(B) ->
%% More valid data, skip the eq as invalid
case B3 of
eq -> mime_decode_list_to_string(Cs, B1, B2, B);
_ ->
Bits4x6 = (B1 bsl 18) bor (B2 bsl 12) bor (B3 bsl 6) bor B,
Octet1 = Bits4x6 bsr 16,
Octet2 = (Bits4x6 bsr 8) band 16#ff,
Octet3 = Bits4x6 band 16#ff,
[Octet1, Octet2, Octet3 | mime_decode_list_to_string(Cs)]
end;
_ -> mime_decode_list_to_string_after_eq(Cs, B1, B2, B3)
end;
mime_decode_list_to_string_after_eq([], B1, B2, eq) ->
binary_to_list(<<B1:6,(B2 bsr 4):2>>);
mime_decode_list_to_string_after_eq([], B1, B2, B3) ->
binary_to_list(<<B1:6,B2:6,(B3 bsr 2):4>>).
decode_list([C1 | Cs], A) ->
case b64d(C1) of
ws -> decode_list(Cs, A);
B1 -> decode_list(Cs, A, B1)
end;
decode_list([], A) ->
A.
decode_list([C2 | Cs], A, B1) ->
case b64d(C2) of
ws -> decode_list(Cs, A, B1);
B2 -> decode_list(Cs, A, B1, B2)
end.
decode_list([C3 | Cs], A, B1, B2) ->
case b64d(C3) of
ws -> decode_list(Cs, A, B1, B2);
B3 -> decode_list(Cs, A, B1, B2, B3)
end.
decode_list([C4 | Cs], A, B1, B2, B3) ->
case b64d(C4) of
ws -> decode_list(Cs, A, B1, B2, B3);
eq when B3 =:= eq -> only_ws(Cs, <<A/binary,B1:6,(B2 bsr 4):2>>);
eq -> only_ws(Cs, <<A/binary,B1:6,B2:6,(B3 bsr 2):4>>);
B4 -> decode_list(Cs, <<A/binary,B1:6,B2:6,B3:6,B4:6>>)
end.
decode_binary(<<C1:8, Cs/bits>>, A) ->
case b64d(C1) of
ws -> decode_binary(Cs, A);
B1 -> decode_binary(Cs, A, B1)
end;
decode_binary(<<>>, A) ->
A.
decode_binary(<<C2:8, Cs/bits>>, A, B1) ->
case b64d(C2) of
ws -> decode_binary(Cs, A, B1);
B2 -> decode_binary(Cs, A, B1, B2)
end.
decode_binary(<<C3:8, Cs/bits>>, A, B1, B2) ->
case b64d(C3) of
ws -> decode_binary(Cs, A, B1, B2);
B3 -> decode_binary(Cs, A, B1, B2, B3)
end.
decode_binary(<<C4:8, Cs/bits>>, A, B1, B2, B3) ->
case b64d(C4) of
ws -> decode_binary(Cs, A, B1, B2, B3);
eq when B3 =:= eq -> only_ws_binary(Cs, <<A/binary,B1:6,(B2 bsr 4):2>>);
eq -> only_ws_binary(Cs, <<A/binary,B1:6,B2:6,(B3 bsr 2):4>>);
B4 -> decode_binary(Cs, <<A/binary,B1:6,B2:6,B3:6,B4:6>>)
end.
only_ws_binary(<<>>, A) ->
A;
only_ws_binary(<<C:8, Cs/bits>>, A) ->
case b64d(C) of
ws -> only_ws_binary(Cs, A);
_ -> erlang:error(function_clause)
end.
decode_list_to_string([C1 | Cs]) ->
case b64d(C1) of
ws -> decode_list_to_string(Cs);
B1 -> decode_list_to_string(Cs, B1)
end;
decode_list_to_string([]) ->
[].
decode_list_to_string([C2 | Cs], B1) ->
case b64d(C2) of
ws -> decode_list_to_string(Cs, B1);
B2 -> decode_list_to_string(Cs, B1, B2)
end.
decode_list_to_string([C3 | Cs], B1, B2) ->
case b64d(C3) of
ws -> decode_list_to_string(Cs, B1, B2);
B3 -> decode_list_to_string(Cs, B1, B2, B3)
end.
decode_list_to_string([C4 | Cs], B1, B2, B3) ->
case b64d(C4) of
ws ->
decode_list_to_string(Cs, B1, B2, B3);
eq when B3 =:= eq ->
only_ws(Cs, binary_to_list(<<B1:6,(B2 bsr 4):2>>));
eq ->
only_ws(Cs, binary_to_list(<<B1:6,B2:6,(B3 bsr 2):4>>));
B4 ->
Bits4x6 = (B1 bsl 18) bor (B2 bsl 12) bor (B3 bsl 6) bor B4,
Octet1 = Bits4x6 bsr 16,
Octet2 = (Bits4x6 bsr 8) band 16#ff,
Octet3 = Bits4x6 band 16#ff,
[Octet1, Octet2, Octet3 | decode_list_to_string(Cs)]
end.
only_ws([], A) ->
A;
only_ws([C | Cs], A) ->
case b64d(C) of
ws -> only_ws(Cs, A);
_ -> erlang:error(function_clause)
end.
%%%========================================================================
%%% Internal functions
%%%========================================================================
%% accessors
-compile({inline, [{b64d, 1}]}).
%% One-based decode map.
b64d(X) ->
element(X,
{bad,bad,bad,bad,bad,bad,bad,bad,ws,ws,bad,bad,ws,bad,bad, %1-15
bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad, %16-31
ws,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,62,bad,bad,bad,63, %32-47
52,53,54,55,56,57,58,59,60,61,bad,bad,bad,eq,bad,bad, %48-63
bad,0,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,bad,bad,bad,bad,bad,
bad,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,bad,bad,bad,bad,bad,
bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,
bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,
bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,
bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,
bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,
bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,
bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,
bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad,bad}).
-compile({inline, [{b64e, 1}]}).
b64e(X) ->
element(X+1,
{$A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M, $N,
$O, $P, $Q, $R, $S, $T, $U, $V, $W, $X, $Y, $Z,
$a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n,
$o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z,
$0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $+, $/}).
|