[795] | 1 | // This code was written by Tyler Akins and has been placed in the |
---|
| 2 | // public domain. It would be nice if you left this header intact. |
---|
| 3 | // Base64 code from Tyler Akins -- http://rumkin.com |
---|
| 4 | |
---|
| 5 | keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; |
---|
| 6 | |
---|
| 7 | function encode64(input) { |
---|
| 8 | var output = ""; |
---|
| 9 | var chr1, chr2, chr3; |
---|
| 10 | var enc1, enc2, enc3, enc4; |
---|
| 11 | var i = 0; |
---|
| 12 | |
---|
| 13 | do { |
---|
| 14 | chr1 = input.charCodeAt(i++); |
---|
| 15 | chr2 = input.charCodeAt(i++); |
---|
| 16 | chr3 = input.charCodeAt(i++); |
---|
| 17 | |
---|
| 18 | enc1 = chr1 >> 2; |
---|
| 19 | enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); |
---|
| 20 | enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); |
---|
| 21 | enc4 = chr3 & 63; |
---|
| 22 | |
---|
| 23 | if (isNaN(chr2)) { |
---|
| 24 | enc3 = enc4 = 64; |
---|
| 25 | } else if (isNaN(chr3)) { |
---|
| 26 | enc4 = 64; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + |
---|
| 30 | keyStr.charAt(enc3) + keyStr.charAt(enc4); |
---|
| 31 | } while (i < input.length); |
---|
| 32 | return output; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | function decode64(input) { |
---|
| 36 | var output = ""; |
---|
| 37 | var chr1, chr2, chr3; |
---|
| 38 | var enc1, enc2, enc3, enc4; |
---|
| 39 | var i = 0; |
---|
| 40 | |
---|
| 41 | // remove all characters that are not A-Z, a-z, 0-9, +, /, or = |
---|
| 42 | input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); |
---|
| 43 | |
---|
| 44 | do { |
---|
| 45 | enc1 = keyStr.indexOf(input.charAt(i++)); |
---|
| 46 | enc2 = keyStr.indexOf(input.charAt(i++)); |
---|
| 47 | enc3 = keyStr.indexOf(input.charAt(i++)); |
---|
| 48 | enc4 = keyStr.indexOf(input.charAt(i++)); |
---|
| 49 | |
---|
| 50 | chr1 = (enc1 << 2) | (enc2 >> 4); |
---|
| 51 | chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); |
---|
| 52 | chr3 = ((enc3 & 3) << 6) | enc4; |
---|
| 53 | |
---|
| 54 | output = output + String.fromCharCode(chr1); |
---|
| 55 | |
---|
| 56 | if (enc3 != 64) { |
---|
| 57 | output = output + String.fromCharCode(chr2); |
---|
| 58 | } |
---|
| 59 | if (enc4 != 64) { |
---|
| 60 | output = output + String.fromCharCode(chr3); |
---|
| 61 | } |
---|
| 62 | } while (i < input.length); |
---|
| 63 | |
---|
| 64 | return output; |
---|
| 65 | } |
---|