endecode-lib.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * 注册命名空间:baidu.endecode
  3. */
  4. baidu.namespace.register("baidu.endecode");
  5. /**
  6. * 本库提供几个常用方法:
  7. * 1、baidu.endecode.uniEncode(text); 将中文进行Unicode编码并输出
  8. * 2、baidu.endecode.base64Encode(text); 将文字进行base64编码并输出
  9. * 3、baidu.endecode.base64Decode(text); 将经过base64编码的文字进行base64解码并输出
  10. * 4、baidu.endecode.utf8Encode(text); 将文字进行utf-8编码并输出
  11. * 5、baidu.endecode.utf8Decode(text); 将经过utf-8编码的文字进行utf-8解码并输出
  12. */
  13. baidu.endecode = (function () {
  14. //base64编码字符集
  15. var _base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  16. //base64解码字符集
  17. var _base64DecodeChars = new Array(
  18. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  19. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  20. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  21. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  22. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  23. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  24. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  25. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
  26. /**
  27. * 此方法实现中文向Unicode的转码,与Jre中的"native2ascii"命令一样
  28. * @param {String} text 需要进行转码的字符串
  29. * @return {String} Unicode码
  30. */
  31. var _uniEncode = function (text) {
  32. text = escape(text.toString()).replace(/\+/g, "%2B");
  33. var matches = text.match(/(%([0-9A-F]{2}))/gi);
  34. if (matches) {
  35. for (var matchid = 0; matchid < matches.length; matchid++) {
  36. var code = matches[matchid].substring(1, 3);
  37. if (parseInt(code, 16) >= 128) {
  38. text = text.replace(matches[matchid], '%u00' + code);
  39. }
  40. }
  41. }
  42. text = text.replace('%25', '%u0025').replace(/%/g, "\\");
  43. return text;
  44. };
  45. /**
  46. * 此方法用于将Unicode码解码为正常字符串
  47. * @param {Object} text
  48. */
  49. var _uniDecode = function (text) {
  50. text = text.replace(/\\/g, "%").replace('%U','%u').replace('%u0025', '%25');
  51. text = unescape(text.toString().replace(/%2B/g, "+"));
  52. var matches = text.match(/(%u00([0-9A-F]{2}))/gi);
  53. if (matches) {
  54. for (var matchid = 0; matchid < matches.length; matchid++) {
  55. var code = matches[matchid].substring(1, 3);
  56. var x = Number("0x" + code);
  57. if (x >= 128) {
  58. text = text.replace(matches[matchid], code);
  59. }
  60. }
  61. }
  62. text = unescape(text.toString().replace(/%2B/g, "+"));
  63. return text;
  64. };
  65. /**
  66. * 此方法用于将文字进行UTF-8编码
  67. * @param {Object} str 源码
  68. * @return {String} UTF-8码
  69. */
  70. var _utf8Encode = function (str) {
  71. var out, i, len, c;
  72. out = "";
  73. len = str.length;
  74. for (i = 0; i < len; i++) {
  75. c = str.charCodeAt(i);
  76. if ((c >= 0x0001) && (c <= 0x007F)) {
  77. out += str.charAt(i);
  78. } else if (c > 0x07FF) {
  79. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
  80. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
  81. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  82. } else {
  83. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
  84. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  85. }
  86. }
  87. return out;
  88. };
  89. /**
  90. * 此方法用于将文字进行UTF-8解码
  91. * @param {Object} str
  92. * @return {String} 原文字
  93. */
  94. var _utf8Decode = function (str) {
  95. var out, i, len, c;
  96. var char2, char3;
  97. out = "";
  98. len = str.length;
  99. i = 0;
  100. while (i < len) {
  101. c = str.charCodeAt(i++);
  102. switch (c >> 4) {
  103. case 0:
  104. case 1:
  105. case 2:
  106. case 3:
  107. case 4:
  108. case 5:
  109. case 6:
  110. case 7:
  111. // 0xxxxxxx
  112. out += str.charAt(i - 1);
  113. break;
  114. case 12:
  115. case 13:
  116. // 110x xxxx  10xx xxxx
  117. char2 = str.charCodeAt(i++);
  118. out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
  119. break;
  120. case 14:
  121. // 1110 xxxx 10xx xxxx 10xx xxxx
  122. char2 = str.charCodeAt(i++);
  123. char3 = str.charCodeAt(i++);
  124. out += String.fromCharCode(((c & 0x0F) << 12) |
  125. ((char2 & 0x3F) << 6) |
  126. ((char3 & 0x3F) << 0));
  127. break;
  128. }
  129. }
  130. return out;
  131. };
  132. /**
  133. * 此方法用于将文字进行base64编码
  134. * @param {Object} str 源码
  135. * @return {String} base64码
  136. */
  137. var _base64Encode = function (str) {
  138. var out, i, len;
  139. var c1, c2, c3;
  140. len = str.length;
  141. i = 0;
  142. out = "";
  143. while (i < len) {
  144. c1 = str.charCodeAt(i++) & 0xff;
  145. if (i == len) {
  146. out += _base64EncodeChars.charAt(c1 >> 2);
  147. out += _base64EncodeChars.charAt((c1 & 0x3) << 4);
  148. out += "==";
  149. break;
  150. }
  151. c2 = str.charCodeAt(i++);
  152. if (i == len) {
  153. out += _base64EncodeChars.charAt(c1 >> 2);
  154. out += _base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  155. out += _base64EncodeChars.charAt((c2 & 0xF) << 2);
  156. out += "=";
  157. break;
  158. }
  159. c3 = str.charCodeAt(i++);
  160. out += _base64EncodeChars.charAt(c1 >> 2);
  161. out += _base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
  162. out += _base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
  163. out += _base64EncodeChars.charAt(c3 & 0x3F);
  164. }
  165. return out;
  166. };
  167. /**
  168. * 此方法用于将文字进行base64解码
  169. * @param {Object} str 源码
  170. * @return {String} 源码
  171. */
  172. var _base64Decode = function (str) {
  173. var c1, c2, c3, c4;
  174. var i, len, out;
  175. len = str.length;
  176. i = 0;
  177. out = "";
  178. while (i < len) {
  179. /* c1 */
  180. do {
  181. c1 = _base64DecodeChars[str.charCodeAt(i++) & 0xff];
  182. }
  183. while (i < len && c1 == -1);
  184. if (c1 == -1)
  185. break;
  186. /* c2 */
  187. do {
  188. c2 = _base64DecodeChars[str.charCodeAt(i++) & 0xff];
  189. }
  190. while (i < len && c2 == -1);
  191. if (c2 == -1)
  192. break;
  193. out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
  194. /* c3 */
  195. do {
  196. c3 = str.charCodeAt(i++) & 0xff;
  197. if (c3 == 61)
  198. return out;
  199. c3 = _base64DecodeChars[c3];
  200. }
  201. while (i < len && c3 == -1);
  202. if (c3 == -1)
  203. break;
  204. out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
  205. /* c4 */
  206. do {
  207. c4 = str.charCodeAt(i++) & 0xff;
  208. if (c4 == 61)
  209. return out;
  210. c4 = _base64DecodeChars[c4];
  211. }
  212. while (i < len && c4 == -1);
  213. if (c4 == -1)
  214. break;
  215. out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
  216. }
  217. return out;
  218. };
  219. /**
  220. * 中文,一般情况下Unicode是UTF-16实现,长度2位,而UTF-8编码是3位
  221. * @param str
  222. * @return {String}
  223. */
  224. var _utf16to8 = function (str) {
  225. var out, i, len, c;
  226. out = "";
  227. len = str.length;
  228. for (i = 0; i < len; i++) {
  229. c = str.charCodeAt(i);
  230. if ((c >= 0x0001) && (c <= 0x007F)) {
  231. out += str.charAt(i);
  232. } else if (c > 0x07FF) {
  233. out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
  234. out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
  235. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  236. } else {
  237. out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
  238. out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
  239. }
  240. }
  241. return out;
  242. };
  243. return {
  244. uniEncode:_uniEncode,
  245. uniDecode:_uniDecode,
  246. base64Encode:_base64Encode,
  247. base64Decode:_base64Decode,
  248. utf8Encode:_utf8Encode,
  249. utf8Decode:_utf8Decode,
  250. utf16to8:_utf16to8
  251. };
  252. })();