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