Advertisement
Guest User

Untitled

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