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