PageRenderTime 31ms CodeModel.GetById 18ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/projekty/all_elements/files/resources/lib/base/jquery.base64.js

https://bitbucket.org/baniol/mobilang
JavaScript | 142 lines | 92 code | 6 blank | 44 comment | 21 complexity | 98eb56cdbfd5fd99183cc77fa964d3c3 MD5 | raw file
  1	
  2	/**
  3	 * jQuery BASE64 functions
  4	 * 
  5	 * 	<code>
  6	 * 		Encodes the given data with base64. 
  7	 * 		String $.base64Encode ( String str )
  8	 *		<br />
  9	 * 		Decodes a base64 encoded data.
 10	 * 		String $.base64Decode ( String str )
 11	 * 	</code>
 12	 * 
 13	 * Encodes and Decodes the given data in base64.
 14	 * This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.
 15	 * Base64-encoded data takes about 33% more space than the original data. 
 16	 * This javascript code is used to encode / decode data using base64 (this encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean). Script is fully compatible with UTF-8 encoding. You can use base64 encoded data as simple encryption mechanism.
 17	 * If you plan using UTF-8 encoding in your project don't forget to set the page encoding to UTF-8 (Content-Type meta tag). 
 18	 * This function orginally get from the WebToolkit and rewrite for using as the jQuery plugin.
 19	 * 
 20	 * Example
 21	 * 	Code
 22	 * 		<code>
 23	 * 			$.base64Encode("I'm Persian."); 
 24	 * 		</code>
 25	 * 	Result
 26	 * 		<code>
 27	 * 			"SSdtIFBlcnNpYW4u"
 28	 * 		</code>
 29	 * 	Code
 30	 * 		<code>
 31	 * 			$.base64Decode("SSdtIFBlcnNpYW4u");
 32	 * 		</code>
 33	 * 	Result
 34	 * 		<code>
 35	 * 			"I'm Persian."
 36	 * 		</code>
 37	 * 
 38	 * @alias Muhammad Hussein Fattahizadeh < muhammad [AT] semnanweb [DOT] com >
 39	 * @link http://www.semnanweb.com/jquery-plugin/base64.html
 40	 * @see http://www.webtoolkit.info/
 41	 * @license http://www.gnu.org/licenses/gpl.html [GNU General Public License]
 42	 * @param {jQuery} {base64Encode:function(input))
 43	 * @param {jQuery} {base64Decode:function(input))
 44	 * @return string
 45	 */
 46	
 47	(function($){
 48		
 49		var keyString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 50		
 51		var uTF8Encode = function(string) {
 52			string = string.replace(/\x0d\x0a/g, "\x0a");
 53			var output = "";
 54			for (var n = 0; n < string.length; n++) {
 55				var c = string.charCodeAt(n);
 56				if (c < 128) {
 57					output += String.fromCharCode(c);
 58				} else if ((c > 127) && (c < 2048)) {
 59					output += String.fromCharCode((c >> 6) | 192);
 60					output += String.fromCharCode((c & 63) | 128);
 61				} else {
 62					output += String.fromCharCode((c >> 12) | 224);
 63					output += String.fromCharCode(((c >> 6) & 63) | 128);
 64					output += String.fromCharCode((c & 63) | 128);
 65				}
 66			}
 67			return output;
 68		};
 69		
 70		var uTF8Decode = function(input) {
 71			var string = "";
 72			var i = 0;
 73			var c = c1 = c2 = 0;
 74			while ( i < input.length ) {
 75				c = input.charCodeAt(i);
 76				if (c < 128) {
 77					string += String.fromCharCode(c);
 78					i++;
 79				} else if ((c > 191) && (c < 224)) {
 80					c2 = input.charCodeAt(i+1);
 81					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
 82					i += 2;
 83				} else {
 84					c2 = input.charCodeAt(i+1);
 85					c3 = input.charCodeAt(i+2);
 86					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
 87					i += 3;
 88				}
 89			}
 90			return string;
 91		}
 92		
 93		$.extend({
 94			base64Encode: function(input) {
 95				var output = "";
 96				var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
 97				var i = 0;
 98				input = uTF8Encode(input);
 99				while (i < input.length) {
100					chr1 = input.charCodeAt(i++);
101					chr2 = input.charCodeAt(i++);
102					chr3 = input.charCodeAt(i++);
103					enc1 = chr1 >> 2;
104					enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
105					enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
106					enc4 = chr3 & 63;
107					if (isNaN(chr2)) {
108						enc3 = enc4 = 64;
109					} else if (isNaN(chr3)) {
110						enc4 = 64;
111					}
112					output = output + keyString.charAt(enc1) + keyString.charAt(enc2) + keyString.charAt(enc3) + keyString.charAt(enc4);
113				}
114				return output;
115			},
116			base64Decode: function(input) {
117				var output = "";
118				var chr1, chr2, chr3;
119				var enc1, enc2, enc3, enc4;
120				var i = 0;
121				input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
122				while (i < input.length) {
123					enc1 = keyString.indexOf(input.charAt(i++));
124					enc2 = keyString.indexOf(input.charAt(i++));
125					enc3 = keyString.indexOf(input.charAt(i++));
126					enc4 = keyString.indexOf(input.charAt(i++));
127					chr1 = (enc1 << 2) | (enc2 >> 4);
128					chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
129					chr3 = ((enc3 & 3) << 6) | enc4;
130					output = output + String.fromCharCode(chr1);
131					if (enc3 != 64) {
132						output = output + String.fromCharCode(chr2);
133					}
134					if (enc4 != 64) {
135						output = output + String.fromCharCode(chr3);
136					}
137				}
138				output = uTF8Decode(output);
139				return output;
140			}
141		});
142	})(jQuery);