base64.js 3.1 KB

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