base64_utf8 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. (function(global) {
  2. 'use strict';
  3. if (global.Base64) return;
  4. var version = "2.1.1";
  5. // if node.js, we use Buffer
  6. var buffer;
  7. if (typeof module !== 'undefined' && module.exports) {
  8. buffer = require('buffer').Buffer;
  9. }
  10. // constants
  11. var b64chars
  12. = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  13. var b64tab = function(bin) {
  14. var t = {};
  15. for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;
  16. return t;
  17. }(b64chars);
  18. var fromCharCode = String.fromCharCode;
  19. // encoder stuff
  20. var cb_utob = function(c) {
  21. if (c.length < 2) {
  22. var cc = c.charCodeAt(0);
  23. return cc < 0x80 ? c
  24. : cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6))
  25. + fromCharCode(0x80 | (cc & 0x3f)))
  26. : (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f))
  27. + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
  28. + fromCharCode(0x80 | ( cc & 0x3f)));
  29. } else {
  30. var cc = 0x10000
  31. + (c.charCodeAt(0) - 0xD800) * 0x400
  32. + (c.charCodeAt(1) - 0xDC00);
  33. return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07))
  34. + fromCharCode(0x80 | ((cc >>> 12) & 0x3f))
  35. + fromCharCode(0x80 | ((cc >>> 6) & 0x3f))
  36. + fromCharCode(0x80 | ( cc & 0x3f)));
  37. }
  38. };
  39. var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
  40. var utob = function(u) {
  41. return u.replace(re_utob, cb_utob);
  42. };
  43. var cb_encode = function(ccc) {
  44. var padlen = [0, 2, 1][ccc.length % 3],
  45. ord = ccc.charCodeAt(0) << 16
  46. | ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8)
  47. | ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),
  48. chars = [
  49. b64chars.charAt( ord >>> 18),
  50. b64chars.charAt((ord >>> 12) & 63),
  51. padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),
  52. padlen >= 1 ? '=' : b64chars.charAt(ord & 63)
  53. ];
  54. return chars.join('');
  55. };
  56. var btoa = global.btoa || function(b) {
  57. return b.replace(/[\s\S]{1,3}/g, cb_encode);
  58. };
  59. var _encode = buffer
  60. ? function (u) { return _utf8_encode((new buffer(u)).toString('base64')) }
  61. : function (u) { return _utf8_encode(btoa(utob(u))) }
  62. ;
  63. var encode = function(u, urisafe) {
  64. return !urisafe
  65. ? _encode(u)
  66. : _encode(u).replace(/[+\/]/g, function(m0) {
  67. return m0 == '+' ? '-' : '_';
  68. }).replace(/=/g, '');
  69. };
  70. var encodeURI = function(u) { return encode(u, true) };
  71. // decoder stuff
  72. var re_btou = new RegExp([
  73. '[\xC0-\xDF][\x80-\xBF]',
  74. '[\xE0-\xEF][\x80-\xBF]{2}',
  75. '[\xF0-\xF7][\x80-\xBF]{3}'
  76. ].join('|'), 'g');
  77. var cb_btou = function(cccc) {
  78. switch(cccc.length) {
  79. case 4:
  80. var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
  81. | ((0x3f & cccc.charCodeAt(1)) << 12)
  82. | ((0x3f & cccc.charCodeAt(2)) << 6)
  83. | (0x3f & cccc.charCodeAt(3)),
  84. offset = cp - 0x10000;
  85. return (fromCharCode((offset >>> 10) + 0xD800)
  86. + fromCharCode((offset & 0x3FF) + 0xDC00));
  87. case 3:
  88. return fromCharCode(
  89. ((0x0f & cccc.charCodeAt(0)) << 12)
  90. | ((0x3f & cccc.charCodeAt(1)) << 6)
  91. | (0x3f & cccc.charCodeAt(2))
  92. );
  93. default:
  94. return fromCharCode(
  95. ((0x1f & cccc.charCodeAt(0)) << 6)
  96. | (0x3f & cccc.charCodeAt(1))
  97. );
  98. }
  99. };
  100. var _utf8_encode = function ( string ) {
  101. string = string.replace(/\r\n/g,"\n");
  102. var utftext = "";
  103. for (var n = 0; n < string.length; n++) {
  104. var c = string.charCodeAt(n);
  105. if (c < 128) {
  106. utftext += String.fromCharCode(c);
  107. }
  108. else if((c > 127) && (c < 2048)) {
  109. utftext += String.fromCharCode((c >> 6) | 192);
  110. utftext += String.fromCharCode((c & 63) | 128);
  111. }
  112. else {
  113. utftext += String.fromCharCode((c >> 12) | 224);
  114. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  115. utftext += String.fromCharCode((c & 63) | 128);
  116. }
  117. }
  118. return utftext;
  119. };
  120. var _utf8_decode = function (utftext) {
  121. var string = "";
  122. var i = 0;
  123. var c = c1 = c2 = 0;
  124. while ( i < utftext.length ) {
  125. c = utftext.charCodeAt(i);
  126. if (c < 128) {
  127. string += String.fromCharCode(c);
  128. i++;
  129. }
  130. else if((c > 191) && (c < 224)) {
  131. c2 = utftext.charCodeAt(i+1);
  132. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  133. i += 2;
  134. }
  135. else {
  136. c2 = utftext.charCodeAt(i+1);
  137. c3 = utftext.charCodeAt(i+2);
  138. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  139. i += 3;
  140. }
  141. }
  142. return string;
  143. };
  144. var btou = function(b) {
  145. return b.replace(re_btou, cb_btou);
  146. };
  147. var cb_decode = function(cccc) {
  148. var len = cccc.length,
  149. padlen = len % 4,
  150. n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0)
  151. | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)
  152. | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0)
  153. | (len > 3 ? b64tab[cccc.charAt(3)] : 0),
  154. chars = [
  155. fromCharCode( n >>> 16),
  156. fromCharCode((n >>> 8) & 0xff),
  157. fromCharCode( n & 0xff)
  158. ];
  159. chars.length -= [0, 0, 2, 1][padlen];
  160. return chars.join('');
  161. };
  162. var atob = global.atob || function(a){
  163. return a.replace(/[\s\S]{1,4}/g, cb_decode);
  164. };
  165. var _decode = buffer
  166. ? function(a) { return (new buffer(a, 'base64')).toString() }
  167. : function(a) { return btou(atob(a)) };
  168. var decode = function(a){
  169. a = _utf8_decode( a );
  170. return _decode(
  171. a.replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' })
  172. .replace(/[^A-Za-z0-9\+\/]/g, '')
  173. );
  174. };
  175. // export Base64
  176. global.Base64 = {
  177. VERSION: version,
  178. atob: atob,
  179. btoa: btoa,
  180. fromBase64: decode,
  181. toBase64: encode,
  182. utob: utob,
  183. encode: encode,
  184. encodeURI: encodeURI,
  185. btou: btou,
  186. decode: decode
  187. };
  188. // if ES5 is available, make Base64.extendString() available
  189. if (typeof Object.defineProperty === 'function') {
  190. var noEnum = function(v){
  191. return {value:v,enumerable:false,writable:true,configurable:true};
  192. };
  193. global.Base64.extendString = function () {
  194. Object.defineProperty(
  195. String.prototype, 'fromBase64', noEnum(function () {
  196. return decode(this)
  197. }));
  198. Object.defineProperty(
  199. String.prototype, 'toBase64', noEnum(function (urisafe) {
  200. return encode(this, urisafe)
  201. }));
  202. Object.defineProperty(
  203. String.prototype, 'toBase64URI', noEnum(function () {
  204. return encode(this, true)
  205. }));
  206. };
  207. }
  208. // that's it!
  209. })(this);