jbase64.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. *create by 2012-08-25 pm 17:48
  3. *@author hexinglun@gmail.com
  4. *BASE64 Encode and Decode By UTF-8 unicode
  5. *可以和java的BASE64编码和解码互相转化
  6. */
  7. (function () {
  8. var BASE64_MAPPING = [
  9. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  10. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  11. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  12. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  13. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  14. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  15. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  16. '4', '5', '6', '7', '8', '9', '+', '/'
  17. ];
  18. /**
  19. *ascii convert to binary
  20. */
  21. var _toBinary = function (ascii) {
  22. var binary = new Array();
  23. while (ascii > 0) {
  24. var b = ascii % 2;
  25. ascii = Math.floor(ascii / 2);
  26. binary.push(b);
  27. }
  28. /*
  29. var len = binary.length;
  30. if(6-len > 0){
  31. for(var i = 6-len ; i > 0 ; --i){
  32. binary.push(0);
  33. }
  34. }*/
  35. binary.reverse();
  36. return binary;
  37. };
  38. /**
  39. *binary convert to decimal
  40. */
  41. var _toDecimal = function (binary) {
  42. var dec = 0;
  43. var p = 0;
  44. for (var i = binary.length - 1; i >= 0; --i) {
  45. var b = binary[i];
  46. if (b == 1) {
  47. dec += Math.pow(2, p);
  48. }
  49. ++p;
  50. }
  51. return dec;
  52. };
  53. /**
  54. *unicode convert to utf-8
  55. */
  56. var _toUTF8Binary = function (c, binaryArray) {
  57. var mustLen = (8 - (c + 1)) + ((c - 1) * 6);
  58. var fatLen = binaryArray.length;
  59. var diff = mustLen - fatLen;
  60. while (--diff >= 0) {
  61. binaryArray.unshift(0);
  62. }
  63. var binary = [];
  64. var _c = c;
  65. while (--_c >= 0) {
  66. binary.push(1);
  67. }
  68. binary.push(0);
  69. var i = 0, len = 8 - (c + 1);
  70. for (; i < len; ++i) {
  71. binary.push(binaryArray[i]);
  72. }
  73. for (var j = 0; j < c - 1; ++j) {
  74. binary.push(1);
  75. binary.push(0);
  76. var sum = 6;
  77. while (--sum >= 0) {
  78. binary.push(binaryArray[i++]);
  79. }
  80. }
  81. return binary;
  82. };
  83. var __BASE64 = {
  84. /**
  85. *BASE64 Encode
  86. */
  87. encoder: function (str) {
  88. var base64_Index = [];
  89. var binaryArray = [];
  90. for (var i = 0, len = str.length; i < len; ++i) {
  91. var unicode = str.charCodeAt(i);
  92. var _tmpBinary = _toBinary(unicode);
  93. if (unicode < 0x80) {
  94. var _tmpdiff = 8 - _tmpBinary.length;
  95. while (--_tmpdiff >= 0) {
  96. _tmpBinary.unshift(0);
  97. }
  98. binaryArray = binaryArray.concat(_tmpBinary);
  99. } else if (unicode >= 0x80 && unicode <= 0x7FF) {
  100. binaryArray = binaryArray.concat(_toUTF8Binary(2, _tmpBinary));
  101. } else if (unicode >= 0x800 && unicode <= 0xFFFF) {//UTF-8 3byte
  102. binaryArray = binaryArray.concat(_toUTF8Binary(3, _tmpBinary));
  103. } else if (unicode >= 0x10000 && unicode <= 0x1FFFFF) {//UTF-8 4byte
  104. binaryArray = binaryArray.concat(_toUTF8Binary(4, _tmpBinary));
  105. } else if (unicode >= 0x200000 && unicode <= 0x3FFFFFF) {//UTF-8 5byte
  106. binaryArray = binaryArray.concat(_toUTF8Binary(5, _tmpBinary));
  107. } else if (unicode >= 4000000 && unicode <= 0x7FFFFFFF) {//UTF-8 6byte
  108. binaryArray = binaryArray.concat(_toUTF8Binary(6, _tmpBinary));
  109. }
  110. }
  111. var extra_Zero_Count = 0;
  112. for (var i = 0, len = binaryArray.length; i < len; i += 6) {
  113. var diff = (i + 6) - len;
  114. if (diff == 2) {
  115. extra_Zero_Count = 2;
  116. } else if (diff == 4) {
  117. extra_Zero_Count = 4;
  118. }
  119. //if(extra_Zero_Count > 0){
  120. // len += extra_Zero_Count+1;
  121. //}
  122. var _tmpExtra_Zero_Count = extra_Zero_Count;
  123. while (--_tmpExtra_Zero_Count >= 0) {
  124. binaryArray.push(0);
  125. }
  126. base64_Index.push(_toDecimal(binaryArray.slice(i, i + 6)));
  127. }
  128. var base64 = '';
  129. for (var i = 0, len = base64_Index.length; i < len; ++i) {
  130. base64 += BASE64_MAPPING[base64_Index[i]];
  131. }
  132. for (var i = 0, len = extra_Zero_Count / 2; i < len; ++i) {
  133. base64 += '=';
  134. }
  135. return base64;
  136. },
  137. /**
  138. *BASE64 Decode for UTF-8
  139. */
  140. decoder: function (_base64Str) {
  141. var _len = _base64Str.length;
  142. var extra_Zero_Count = 0;
  143. /**
  144. *计算在进行BASE64编码的时候,补了几个0
  145. */
  146. if (_base64Str.charAt(_len - 1) == '=') {
  147. //alert(_base64Str.charAt(_len-1));
  148. //alert(_base64Str.charAt(_len-2));
  149. if (_base64Str.charAt(_len - 2) == '=') {//两个等号说明补了4个0
  150. extra_Zero_Count = 4;
  151. _base64Str = _base64Str.substring(0, _len - 2);
  152. } else {//一个等号说明补了2个0
  153. extra_Zero_Count = 2;
  154. _base64Str = _base64Str.substring(0, _len - 1);
  155. }
  156. }
  157. var binaryArray = [];
  158. for (var i = 0, len = _base64Str.length; i < len; ++i) {
  159. var c = _base64Str.charAt(i);
  160. for (var j = 0, size = BASE64_MAPPING.length; j < size; ++j) {
  161. if (c == BASE64_MAPPING[j]) {
  162. var _tmp = _toBinary(j);
  163. /*不足6位的补0*/
  164. var _tmpLen = _tmp.length;
  165. if (6 - _tmpLen > 0) {
  166. for (var k = 6 - _tmpLen; k > 0; --k) {
  167. _tmp.unshift(0);
  168. }
  169. }
  170. binaryArray = binaryArray.concat(_tmp);
  171. break;
  172. }
  173. }
  174. }
  175. if (extra_Zero_Count > 0) {
  176. binaryArray = binaryArray.slice(0, binaryArray.length - extra_Zero_Count);
  177. }
  178. var unicode = [];
  179. var unicodeBinary = [];
  180. for (var i = 0, len = binaryArray.length; i < len;) {
  181. if (binaryArray[i] == 0) {
  182. unicode = unicode.concat(_toDecimal(binaryArray.slice(i, i + 8)));
  183. i += 8;
  184. } else {
  185. var sum = 0;
  186. while (i < len) {
  187. if (binaryArray[i] == 1) {
  188. ++sum;
  189. } else {
  190. break;
  191. }
  192. ++i;
  193. }
  194. unicodeBinary = unicodeBinary.concat(binaryArray.slice(i + 1, i + 8 - sum));
  195. i += 8 - sum;
  196. while (sum > 1) {
  197. unicodeBinary = unicodeBinary.concat(binaryArray.slice(i + 2, i + 8));
  198. i += 8;
  199. --sum;
  200. }
  201. unicode = unicode.concat(_toDecimal(unicodeBinary));
  202. unicodeBinary = [];
  203. }
  204. }
  205. return unicode;
  206. }
  207. };
  208. window.BASE64 = __BASE64;
  209. })();