Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

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