utility.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /****************************************************************
  2. ** Licensed Materials - Property of IBM
  3. **
  4. ** IBM Cognos Products: drill
  5. **
  6. ** (C) Copyright IBM Corp. 2001, 2010
  7. **
  8. ** US Government Users Restricted Rights - Use, duplication or
  9. ** disclosure restricted by GSA ADP Schedule Contract with
  10. ** IBM Corp.
  11. *****************************************************************/
  12. function encode64(inp)
  13. {
  14. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  15. var output = "";
  16. var chr1, chr2, chr3 = "";
  17. var enc1, enc2, enc3, enc4 = "";
  18. var i = 0;
  19. do
  20. {
  21. chr1 = inp.charCodeAt(i++); //1st byte.
  22. chr2 = inp.charCodeAt(i++); //2nd byte.
  23. chr3 = inp.charCodeAt(i++); //3rd byte.
  24. enc1 = chr1 >> 2;
  25. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  26. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  27. enc4 = chr3 & 63;
  28. if (isNaN(chr2))
  29. {
  30. enc3 = enc4 = 64;
  31. }
  32. else if (isNaN(chr3))
  33. {
  34. enc4 = 64;
  35. }
  36. // Store the 4 encoded bytes
  37. output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
  38. // clean up variables.
  39. chr1 = chr2 = chr3 = "";
  40. enc1 = enc2 = enc3 = enc4 = "";
  41. } while (i < inp.length);
  42. //return the encoded value.
  43. return output;
  44. }