Nav apraksta

base64.js 6.6KB

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