base64.js 5.5 KB

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