summaryrefslogtreecommitdiff
path: root/src/lib/Base64.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/Base64.js')
-rw-r--r--src/lib/Base64.js49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/lib/Base64.js b/src/lib/Base64.js
new file mode 100644
index 0000000..e83d2a5
--- /dev/null
+++ b/src/lib/Base64.js
@@ -0,0 +1,49 @@
+
+'use strict';
+// we need this because of https://bugzilla.mozilla.org/show_bug.cgi?id=439276
+
+var Base64 = {
+
+ purify: function(input) {
+ return input.replace(/[^A-Za-z0-9\+\/=]+/g, '');
+ },
+
+ alt: function(s) {
+ // URL base64 variant, see http://en.wikipedia.org/wiki/Base64#URL_applications
+ return s.replace(/-/g, '+').replace(/_/g, '/')
+ },
+
+ decode: function (input, strict) {
+ var output = '';
+ var chr1, chr2, chr3;
+ var enc1, enc2, enc3, enc4;
+ var i = 0;
+
+ // if (/[^A-Za-z0-9\+\/\=]/.test(input)) return ""; // we don't need this, caller checks for us
+
+ const k = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+ while (i < input.length) {
+
+ enc1 = k.indexOf(input.charAt(i++));
+ enc2 = k.indexOf(input.charAt(i++));
+ enc3 = k.indexOf(input.charAt(i++));
+ enc4 = k.indexOf(input.charAt(i++));
+
+ chr1 = (enc1 << 2) | (enc2 >> 4);
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
+ chr3 = ((enc3 & 3) << 6) | enc4;
+
+ output += String.fromCharCode(chr1);
+
+ if (enc3 != 64) {
+ output += String.fromCharCode(chr2);
+ }
+ if (enc4 != 64) {
+ output += String.fromCharCode(chr3);
+ }
+
+ }
+ return output;
+
+ }
+};