Code Viewer

./base64.js File Size: 5.99 KB

  1. /**
  2. *
  3. *  Base64 encode / decode
  4. *  http://www.webtoolkit.info/
  5. *
  6. **/
  7. // private property
  8. var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  9.  
  10. // public method for encoding
  11. function encode(input) {
  12.         _debug.getfuncname(arguments);
  13.         var output = "";
  14.         var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  15.         var i = 0;
  16.  
  17.         input = _utf8_encode(input);
  18.  
  19.         while (i < input.length) {
  20.  
  21.                 chr1 = input.charCodeAt(i++);
  22.                 chr2 = input.charCodeAt(i++);
  23.                 chr3 = input.charCodeAt(i++);
  24.  
  25.                 enc1 = chr1 >> 2;
  26.                 enc2 = ( (chr1 & 3) << 4) | (chr2 >> 4);
  27.                 enc3 = ( (chr2 & 15) << 2) | (chr3 >> 6);
  28.                 enc4 = chr3 & 63;
  29.  
  30.                 if (isNaN(chr2)) {
  31.                         enc3 = enc4 = 64;
  32.                 } else if (isNaN(chr3)) {
  33.                         enc4 = 64;
  34.                 }
  35.  
  36.                 output = output +
  37.                 _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
  38.                 _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  39.  
  40.         }
  41.  
  42.         return output;
  43. }
  44.  
  45. // public method for decoding
  46. function decode(input) {
  47.         _debug.getfuncname(arguments);
  48.         var output = "";
  49.         var chr1, chr2, chr3;
  50.         var enc1, enc2, enc3, enc4;
  51.         var i = 0;
  52.  
  53.         input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  54.  
  55.         while (i < input.length) {
  56.  
  57.                 enc1 = _keyStr.indexOf(input.charAt(i++));
  58.                 enc2 = _keyStr.indexOf(input.charAt(i++));
  59.                 enc3 = _keyStr.indexOf(input.charAt(i++));
  60.                 enc4 = _keyStr.indexOf(input.charAt(i++));
  61.  
  62.                 chr1 = (enc1 << 2) | (enc2 >> 4);
  63.                 chr2 = ( (enc2 & 15) << 4) | (enc3 >> 2);
  64.                 chr3 = ( (enc3 & 3) << 6) | enc4;
  65.  
  66.                 output = output + String.fromCharCode(chr1);
  67.  
  68.                 if (enc3 != 64) {
  69.                         output = output + String.fromCharCode(chr2);
  70.                 }
  71.                 if (enc4 != 64) {
  72.                         output = output + String.fromCharCode(chr3);
  73.                 }
  74.  
  75.         }
  76.  
  77.         output = _utf8_decode(output);
  78.  
  79.         return output;
  80.  
  81. }
  82.  
  83. // private method for UTF-8 encoding
  84. function _utf8_encode(string) {
  85.         _debug.getfuncname(arguments);
  86.         string = string.replace(/\r\n/g,"\n");
  87.         var utftext = "";
  88.  
  89.         for (var n = 0; n < string.length; n++) {
  90.  
  91.                 var c = string.charCodeAt(n);
  92.  
  93.                 if (c < 128) {
  94.                         utftext += String.fromCharCode(c);
  95.                 }
  96.                 else if ( (c > 127) && (c < 2048)) {
  97.                         utftext += String.fromCharCode( (c >> 6) | 192);
  98.                         utftext += String.fromCharCode( (c & 63) | 128);
  99.                 }
  100.                 else {
  101.                         utftext += String.fromCharCode( (c >> 12) | 224);
  102.                         utftext += String.fromCharCode( ( (c >> 6) & 63) | 128);
  103.                         utftext += String.fromCharCode( (c & 63) | 128);
  104.                 }
  105.  
  106.         }
  107.  
  108.         return utftext;
  109. }
  110.  
  111. // private method for UTF-8 decoding
  112. function _utf8_decode(utftext) {
  113.         _debug.getfuncname(arguments);
  114.         var string = "";
  115.         var i = 0;
  116.         var c = c1 = c2 = 0;
  117.  
  118.         while (i < utftext.length) {
  119.  
  120.                 c = utftext.charCodeAt(i);
  121.  
  122.                 if (c < 128) {
  123.                         string += String.fromCharCode(c);
  124.                         i++;
  125.                 }
  126.                 else if ( (c > 191) && (c < 224)) {
  127.                         c2 = utftext.charCodeAt(i+1);
  128.                         string += String.fromCharCode( ( (c & 31) << 6) | (c2 & 63));
  129.                         i += 2;
  130.                 }
  131.                 else {
  132.                         c2 = utftext.charCodeAt(i+1);
  133.                         c3 = utftext.charCodeAt(i+2);
  134.                         string += String.fromCharCode( ( (c & 15) << 12) | ( (c2 & 63) << 6) | (c3 & 63));
  135.                         i += 3;
  136.                 }
  137.  
  138.         }
  139.  
  140.         return string;
  141. }

Version

  • 5.0.0070_20100325_publicbeta1

Developers

Project Details

  • Folders8
  • Files122
  • Total Lines11,867
  • Repository Version70

User Count

  • 162