base64.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################## */
  16. var BASE64_enc = "ABCDEFGHIJKLMNOP" +
  17. "QRSTUVWXYZabcdef" +
  18. "ghijklmnopqrstuv" +
  19. "wxyz0123456789+/" +
  20. "=";
  21. function base64_encode(str)
  22. {
  23. var result = "";
  24. var c1, c2, c3 = "";
  25. var e1, e2, e3, e4 = "";
  26. var i = 0;
  27. while(i < str.length)
  28. {
  29. c1 = str.charCodeAt(i++);
  30. c2 = str.charCodeAt(i++);
  31. c3 = str.charCodeAt(i++);
  32. e1 = c1 >> 2;
  33. e2 = ((c1 & 3) << 4) | (c2 >> 4);
  34. e3 = ((c2 & 15) << 2) | (c3 >> 6);
  35. e4 = c3 & 63;
  36. if (isNaN(c2))
  37. {
  38. e3 = 64;
  39. e4 = 64;
  40. }
  41. else if (isNaN(c3))
  42. {
  43. e4 = 64;
  44. }
  45. result = result + BASE64_enc.charAt(e1) + BASE64_enc.charAt(e2) +
  46. BASE64_enc.charAt(e3) + BASE64_enc.charAt(e4);
  47. c1 = c2 = c3 = "";
  48. e1 = e2 = e3 = e4 = "";
  49. }
  50. return result;
  51. }