annotate base64.js @ 0:96898e3812a5

initial push
author Sonny Piers <sonny.piers@gmail.com>
date Sun, 18 Dec 2011 19:03:28 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
1 // This code was written by Tyler Akins and has been placed in the
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
2 // public domain. It would be nice if you left this header intact.
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
3 // Base64 code from Tyler Akins -- http://rumkin.com
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
4
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
5 var Base64 = (function () {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
6 var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
7
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
8 var obj = {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
9 /**
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
10 * Encodes a string in base64
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
11 * @param {String} input The string to encode in base64.
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
12 */
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
13 encode: function (input) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
14 var output = "";
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
15 var chr1, chr2, chr3;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
16 var enc1, enc2, enc3, enc4;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
17 var i = 0;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
18
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
19 do {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
20 chr1 = input.charCodeAt(i++);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
21 chr2 = input.charCodeAt(i++);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
22 chr3 = input.charCodeAt(i++);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
23
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
24 enc1 = chr1 >> 2;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
25 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
26 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
27 enc4 = chr3 & 63;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
28
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
29 if (isNaN(chr2)) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
30 enc3 = enc4 = 64;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
31 } else if (isNaN(chr3)) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
32 enc4 = 64;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
33 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
34
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
35 output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
36 keyStr.charAt(enc3) + keyStr.charAt(enc4);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
37 } while (i < input.length);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
38
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
39 return output;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
40 },
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
41
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
42 /**
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
43 * Decodes a base64 string.
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
44 * @param {String} input The string to decode.
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
45 */
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
46 decode: function (input) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
47 var output = "";
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
48 var chr1, chr2, chr3;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
49 var enc1, enc2, enc3, enc4;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
50 var i = 0;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
51
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
52 // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
53 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
54
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
55 do {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
56 enc1 = keyStr.indexOf(input.charAt(i++));
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
57 enc2 = keyStr.indexOf(input.charAt(i++));
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
58 enc3 = keyStr.indexOf(input.charAt(i++));
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
59 enc4 = keyStr.indexOf(input.charAt(i++));
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
60
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
61 chr1 = (enc1 << 2) | (enc2 >> 4);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
62 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
63 chr3 = ((enc3 & 3) << 6) | enc4;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
64
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
65 output = output + String.fromCharCode(chr1);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
66
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
67 if (enc3 != 64) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
68 output = output + String.fromCharCode(chr2);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
69 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
70 if (enc4 != 64) {
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
71 output = output + String.fromCharCode(chr3);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
72 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
73 } while (i < input.length);
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
74
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
75 return output;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
76 }
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
77 };
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
78
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
79 return obj;
96898e3812a5 initial push
Sonny Piers <sonny.piers@gmail.com>
parents:
diff changeset
80 })();