This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathBase64.js
48 lines (45 loc) · 1.51 KB
/
Base64.js
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
/*******************************************************************************
* @license
* Copyright (c) 2014 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors: IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env browser, amd*/
define(function() {
var handlesWhitespace = (function(){
try {
return atob("AA==") === atob("A A = =");
} catch(e) {
return false;
}
})();
function encode(buffer) {
buffer = (buffer instanceof Uint8Array) ? buffer : new Uint8Array(buffer);
var result = [];
for (var i = 0, length = buffer.length; i < length; i += 0x10000) {
result.push(String.fromCharCode.apply(null, buffer.subarray(i, Math.min(length, i + 0x10000))));
}
return btoa(result.join(""));
}
function decode(base64) {
base64 = String(base64 !== undefined ? base64 : "");
if (!handlesWhitespace) {
base64 = base64.replace(/\s/g, '');
}
var text = atob(base64);
var length = text.length;
var buffer = new Uint8Array(length);
for (var i = 0; i < length; i++) {
buffer[i] = text.charCodeAt(i);
}
return buffer;
}
return {
encode: encode,
decode: decode
};
});