Initial commit
[yaffs-website] / node_modules / js-base64 / base64_utf8
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  
104                 for (var n = 0; n < string.length; n++) {
105  
106                         var c = string.charCodeAt(n);
107  
108                         if (c < 128) {
109                                 utftext += String.fromCharCode(c);
110                         }
111                         else if((c > 127) && (c < 2048)) {
112                                 utftext += String.fromCharCode((c >> 6) | 192);
113                                 utftext += String.fromCharCode((c & 63) | 128);
114                         }
115                         else {
116                                 utftext += String.fromCharCode((c >> 12) | 224);
117                                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
118                                 utftext += String.fromCharCode((c & 63) | 128);
119                         }
120  
121                 }
122  
123                 return utftext;
124         };
125         var _utf8_decode = function (utftext) {
126                 var string = "";
127                 var i = 0;
128                 var c = c1 = c2 = 0;
129  
130                 while ( i < utftext.length ) {
131  
132                         c = utftext.charCodeAt(i);
133  
134                         if (c < 128) {
135                                 string += String.fromCharCode(c);
136                                 i++;
137                         }
138                         else if((c > 191) && (c < 224)) {
139                                 c2 = utftext.charCodeAt(i+1);
140                                 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
141                                 i += 2;
142                         }
143                         else {
144                                 c2 = utftext.charCodeAt(i+1);
145                                 c3 = utftext.charCodeAt(i+2);
146                                 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
147                                 i += 3;
148                         }
149  
150                 }
151  
152                 return string;
153         };
154     var btou = function(b) {
155         return b.replace(re_btou, cb_btou);
156     };
157     var cb_decode = function(cccc) {
158         var len = cccc.length,
159         padlen = len % 4,
160         n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0)
161             | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)
162             | (len > 2 ? b64tab[cccc.charAt(2)] <<  6 : 0)
163             | (len > 3 ? b64tab[cccc.charAt(3)]       : 0),
164         chars = [
165             fromCharCode( n >>> 16),
166             fromCharCode((n >>>  8) & 0xff),
167             fromCharCode( n         & 0xff)
168         ];
169         chars.length -= [0, 0, 2, 1][padlen];
170         return chars.join('');
171     };
172     var atob = global.atob || function(a){
173         return a.replace(/[\s\S]{1,4}/g, cb_decode);
174     };
175     var _decode = buffer
176         ? function(a) { return (new buffer(a, 'base64')).toString() }
177     : function(a) { return btou(atob(a)) };
178     var decode = function(a){
179         a = _utf8_decode( a );
180         return _decode(
181             a.replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' })
182                 .replace(/[^A-Za-z0-9\+\/]/g, '')
183         );
184     };
185     // export Base64
186     global.Base64 = {
187         VERSION: version,
188         atob: atob,
189         btoa: btoa,
190         fromBase64: decode,
191         toBase64: encode,
192         utob: utob,
193         encode: encode,
194         encodeURI: encodeURI,
195         btou: btou,
196         decode: decode
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     // that's it!
219 })(this);