平安校园
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

236 рядки
8.1 KiB

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