base64.js 5.4 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 , len = 8 - (c+1);
  64. for(; i < len ; ++i){
  65. binary.push(binaryArray[i]);
  66. }
  67. for(var j = 0 ; j < c-1 ; ++j){
  68. binary.push(1);
  69. binary.push(0);
  70. var sum = 6;
  71. while(--sum >= 0){
  72. binary.push(binaryArray[i++]);
  73. }
  74. }
  75. return binary;
  76. };
  77. var __BASE64 = {
  78. /**
  79. *BASE64 Encode
  80. */
  81. encoder:function(str){
  82. var base64_Index = [];
  83. var binaryArray = [];
  84. for(var i = 0 , len = str.length ; i < len ; ++i){
  85. var unicode = str.charCodeAt(i);
  86. var _tmpBinary = _toBinary(unicode);
  87. if(unicode < 0x80){
  88. var _tmpdiff = 8 - _tmpBinary.length;
  89. while(--_tmpdiff >= 0){
  90. _tmpBinary.unshift(0);
  91. }
  92. binaryArray = binaryArray.concat(_tmpBinary);
  93. }else if(unicode >= 0x80 && unicode <= 0x7FF){
  94. binaryArray = binaryArray.concat(_toUTF8Binary(2 , _tmpBinary));
  95. }else if(unicode >= 0x800 && unicode <= 0xFFFF){//UTF-8 3byte
  96. binaryArray = binaryArray.concat(_toUTF8Binary(3 , _tmpBinary));
  97. }else if(unicode >= 0x10000 && unicode <= 0x1FFFFF){//UTF-8 4byte
  98. binaryArray = binaryArray.concat(_toUTF8Binary(4 , _tmpBinary));
  99. }else if(unicode >= 0x200000 && unicode <= 0x3FFFFFF){//UTF-8 5byte
  100. binaryArray = binaryArray.concat(_toUTF8Binary(5 , _tmpBinary));
  101. }else if(unicode >= 4000000 && unicode <= 0x7FFFFFFF){//UTF-8 6byte
  102. binaryArray = binaryArray.concat(_toUTF8Binary(6 , _tmpBinary));
  103. }
  104. }
  105. var extra_Zero_Count = 0;
  106. for(var i = 0 , len = binaryArray.length ; i < len ; i+=6){
  107. var diff = (i+6)-len;
  108. if(diff == 2){
  109. extra_Zero_Count = 2;
  110. }else if(diff == 4){
  111. extra_Zero_Count = 4;
  112. }
  113. //if(extra_Zero_Count > 0){
  114. // len += extra_Zero_Count+1;
  115. //}
  116. var _tmpExtra_Zero_Count = extra_Zero_Count;
  117. while(--_tmpExtra_Zero_Count >= 0){
  118. binaryArray.push(0);
  119. }
  120. base64_Index.push(_toDecimal(binaryArray.slice(i , i+6)));
  121. }
  122. var base64 = '';
  123. for(var i = 0 , len = base64_Index.length ; i < len ; ++i){
  124. base64 += BASE64_MAPPING[base64_Index[i]];
  125. }
  126. for(var i = 0 , len = extra_Zero_Count/2 ; i < len ; ++i){
  127. base64 += '=';
  128. }
  129. return base64;
  130. },
  131. /**
  132. *BASE64 Decode for UTF-8
  133. */
  134. decoder : function(_base64Str){
  135. var _len = _base64Str.length;
  136. var extra_Zero_Count = 0;
  137. /**
  138. *计算在进行BASE64编码的时候,补了几个0
  139. */
  140. if(_base64Str.charAt(_len-1) == '='){
  141. //alert(_base64Str.charAt(_len-1));
  142. //alert(_base64Str.charAt(_len-2));
  143. if(_base64Str.charAt(_len-2) == '='){//两个等号说明补了4个0
  144. extra_Zero_Count = 4;
  145. _base64Str = _base64Str.substring(0 , _len-2);
  146. }else{//一个等号说明补了2个0
  147. extra_Zero_Count = 2;
  148. _base64Str = _base64Str.substring(0 , _len - 1);
  149. }
  150. }
  151. var binaryArray = [];
  152. for(var i = 0 , len = _base64Str.length; i < len ; ++i){
  153. var c = _base64Str.charAt(i);
  154. for(var j = 0 , size = BASE64_MAPPING.length ; j < size ; ++j){
  155. if(c == BASE64_MAPPING[j]){
  156. var _tmp = _toBinary(j);
  157. /*不足6位的补0*/
  158. var _tmpLen = _tmp.length;
  159. if(6-_tmpLen > 0){
  160. for(var k = 6-_tmpLen ; k > 0 ; --k){
  161. _tmp.unshift(0);
  162. }
  163. }
  164. binaryArray = binaryArray.concat(_tmp);
  165. break;
  166. }
  167. }
  168. }
  169. if(extra_Zero_Count > 0){
  170. binaryArray = binaryArray.slice(0 , binaryArray.length - extra_Zero_Count);
  171. }
  172. var unicode = [];
  173. var unicodeBinary = [];
  174. for(var i = 0 , len = binaryArray.length ; i < len ; ){
  175. if(binaryArray[i] == 0){
  176. unicode=unicode.concat(_toDecimal(binaryArray.slice(i,i+8)));
  177. i += 8;
  178. }else{
  179. var sum = 0;
  180. while(i < len){
  181. if(binaryArray[i] == 1){
  182. ++sum;
  183. }else{
  184. break;
  185. }
  186. ++i;
  187. }
  188. unicodeBinary = unicodeBinary.concat(binaryArray.slice(i+1 , i+8-sum));
  189. i += 8 - sum;
  190. while(sum > 1){
  191. unicodeBinary = unicodeBinary.concat(binaryArray.slice(i+2 , i+8));
  192. i += 8;
  193. --sum;
  194. }
  195. unicode = unicode.concat(_toDecimal(unicodeBinary));
  196. unicodeBinary = [];
  197. }
  198. }
  199. return unicode;
  200. }
  201. };
  202. //window.BASE64 = __BASE64;
  203. module.exports = {
  204. CusBASE64: __BASE64
  205. }
  206. })();