Advertisement
haizaar

utf8 safe base64 module for YUI3

Nov 21st, 2011
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. *
  3. *  Base64 encode / decode
  4. *  http://www.webtoolkit.info/
  5. *
  6. *  Wrapped into YUI3 module by Zaar Hai haizaar at haizaar dot com
  7. *
  8. **/
  9.  
  10. YUI.add("webtoolkit-base64", function(Y){
  11.  
  12.  
  13.         // private property
  14.         var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  15.  
  16.         // public method for encoding
  17.         function encode(input) {
  18.                 var output = "";
  19.                 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  20.                 var i = 0;
  21.  
  22.                 input = _utf8_encode(input);
  23.  
  24.                 while (i < input.length) {
  25.  
  26.                         chr1 = input.charCodeAt(i++);
  27.                         chr2 = input.charCodeAt(i++);
  28.                         chr3 = input.charCodeAt(i++);
  29.  
  30.                         enc1 = chr1 >> 2;
  31.                         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  32.                         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  33.                         enc4 = chr3 & 63;
  34.  
  35.                         if (isNaN(chr2)) {
  36.                                 enc3 = enc4 = 64;
  37.                         } else if (isNaN(chr3)) {
  38.                                 enc4 = 64;
  39.                         }
  40.  
  41.                         output = output +
  42.                         _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
  43.                         _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  44.  
  45.                 }
  46.  
  47.                 return output;
  48.         }
  49.  
  50.         // public method for decoding
  51.         function decode(input) {
  52.                 var output = "";
  53.                 var chr1, chr2, chr3;
  54.                 var enc1, enc2, enc3, enc4;
  55.                 var i = 0;
  56.  
  57.                 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  58.  
  59.                 while (i < input.length) {
  60.  
  61.                         enc1 = _keyStr.indexOf(input.charAt(i++));
  62.                         enc2 = _keyStr.indexOf(input.charAt(i++));
  63.                         enc3 = _keyStr.indexOf(input.charAt(i++));
  64.                         enc4 = _keyStr.indexOf(input.charAt(i++));
  65.  
  66.                         chr1 = (enc1 << 2) | (enc2 >> 4);
  67.                         chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  68.                         chr3 = ((enc3 & 3) << 6) | enc4;
  69.  
  70.                         output = output + String.fromCharCode(chr1);
  71.  
  72.                         if (enc3 != 64) {
  73.                                 output = output + String.fromCharCode(chr2);
  74.                         }
  75.                         if (enc4 != 64) {
  76.                                 output = output + String.fromCharCode(chr3);
  77.                         }
  78.  
  79.                 }
  80.  
  81.                 output = _utf8_decode(output);
  82.  
  83.                 return output;
  84.  
  85.         }
  86.  
  87.         // private method for UTF-8 encoding
  88.         function _utf8_encode(string) {
  89.                 string = string.replace(/\r\n/g,"\n");
  90.                 var utftext = "";
  91.  
  92.                 for (var n = 0; n < string.length; n++) {
  93.  
  94.                         var c = string.charCodeAt(n);
  95.  
  96.                         if (c < 128) {
  97.                                 utftext += String.fromCharCode(c);
  98.                         }
  99.                         else if((c > 127) && (c < 2048)) {
  100.                                 utftext += String.fromCharCode((c >> 6) | 192);
  101.                                 utftext += String.fromCharCode((c & 63) | 128);
  102.                         }
  103.                         else {
  104.                                 utftext += String.fromCharCode((c >> 12) | 224);
  105.                                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  106.                                 utftext += String.fromCharCode((c & 63) | 128);
  107.                         }
  108.  
  109.                 }
  110.  
  111.                 return utftext;
  112.         }
  113.  
  114.         // private method for UTF-8 decoding
  115.         function _utf8_decode(utftext) {
  116.                 var string = "";
  117.                 var i = 0;
  118.                 var c = c1 = c2 = 0;
  119.  
  120.                 while ( i < utftext.length ) {
  121.  
  122.                         c = utftext.charCodeAt(i);
  123.  
  124.                         if (c < 128) {
  125.                                 string += String.fromCharCode(c);
  126.                                 i++;
  127.                         }
  128.                         else if((c > 191) && (c < 224)) {
  129.                                 c2 = utftext.charCodeAt(i+1);
  130.                                 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  131.                                 i += 2;
  132.                         }
  133.                         else {
  134.                                 c2 = utftext.charCodeAt(i+1);
  135.                                 c3 = utftext.charCodeAt(i+2);
  136.                                 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  137.                                 i += 3;
  138.                         }
  139.  
  140.                 }
  141.  
  142.                 return string;
  143.         }
  144.  
  145.         Y.namespace("Base64");
  146.         Y.Base64.encode = encode;
  147.         Y.Base64.decode = decode;
  148.  
  149. }, "1.0", {requires: ['base']});
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement