]> jfr.im git - irc/quakenet/qwebirc.git/blame - js/base64.js
Merge pull request #373 from retropc/kill_flash
[irc/quakenet/qwebirc.git] / js / base64.js
CommitLineData
402c7470
CP
1/*
2Originally from: https://github.com/MaxArt2501/base64-js
3
4The MIT License (MIT)
5
6Copyright (c) 2014 MaxArt2501
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11
12THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13*/
14
15var B64TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
16 B64RE = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
17
18qwebirc.util.b64Encode = function(string) {
19 string = String(string);
20 var bitmap, a, b, c,
21 result = "", i = 0,
22 rest = string.length % 3; // To determine the final padding
23
24 for (; i < string.length;) {
25 if ((a = string.charCodeAt(i++)) > 255
26 || (b = string.charCodeAt(i++)) > 255
27 || (c = string.charCodeAt(i++)) > 255)
28 throw new TypeError("Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.");
29
30 bitmap = (a << 16) | (b << 8) | c;
31 result += B64TABLE.charAt(bitmap >> 18 & 63) + B64TABLE.charAt(bitmap >> 12 & 63)
32 + B64TABLE.charAt(bitmap >> 6 & 63) + B64TABLE.charAt(bitmap & 63);
33 }
34
35 // If there's need of padding, replace the last 'A's with equal signs
36 return rest ? result.slice(0, rest - 3) + "===".substring(rest) : result;
37}
38
39
40qwebirc.util.b64Decode = function(string) {
41 // atob can work with strings with whitespaces, even inside the encoded part,
42 // but only \t, \n, \f, \r and ' ', which can be stripped.
43 string = String(string).replace(/[\t\n\f\r ]+/g, "");
44 if (!B64RE.test(string))
45 throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
46
47 // Adding the padding if missing, for semplicity
48 string += "==".slice(2 - (string.length & 3));
49 var bitmap, result = "", r1, r2, i = 0;
50 for (; i < string.length;) {
51 bitmap = B64TABLE.indexOf(string.charAt(i++)) << 18 | B64TABLE.indexOf(string.charAt(i++)) << 12
52 | (r1 = B64TABLE.indexOf(string.charAt(i++))) << 6 | (r2 = B64TABLE.indexOf(string.charAt(i++)));
53
54 result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255)
55 : r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255)
56 : String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
57 }
58 return result;
59}