1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // WebSafeBase64Escape and Unescape.
6 function B64_encode(bytes
, opt_length
) {
7 if (!opt_length
) opt_length
= bytes
.length
;
9 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
14 while (opt_length
--) {
16 accu
|= bytes
[inputIndex
++];
19 var i
= (accu
>> (shift
- 6)) & 63;
20 result
+= b64out
.charAt(i
);
27 var i
= (accu
>> (shift
- 6)) & 63;
28 result
+= b64out
.charAt(i
);
33 // Normal base64 encode; not websafe, including padding.
34 function base64_encode(bytes
, opt_length
) {
35 if (!opt_length
) opt_length
= bytes
.length
;
37 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
42 while (opt_length
--) {
44 accu
|= bytes
[inputIndex
++];
47 var i
= (accu
>> (shift
- 6)) & 63;
48 result
+= b64out
.charAt(i
);
55 var i
= (accu
>> (shift
- 6)) & 63;
56 result
+= b64out
.charAt(i
);
58 while (result
.length
% 4) result
+= '=';
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 0, 0,
65 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 0, 0, 0, 0, 0, 0,
66 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
67 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 0, 0, 0, 0, 64,
68 0, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
69 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 0, 0, 0, 0, 0
72 function B64_decode(string
) {
76 for (var i
= 0; i
< string
.length
; ++i
) {
77 var c
= string
.charCodeAt(i
);
78 if (c
< 32 || c
> 127 || !B64_inmap
[c
- 32]) return [];
80 accu
|= (B64_inmap
[c
- 32] - 1);
83 bytes
.push((accu
>> (shift
- 8)) & 255);