-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathbase64.js
128 lines (100 loc) · 3.31 KB
/
base64.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Implements base64 decode and encode in browser that
* it hasn't support of window.btoa and window.atob
* methods.
* Based in Nick Galbreath
* http://code.google.com/p/stringencoders/source/browse/#svn/
* and Carlo Zottmann jQuery port
* http://github.com/carlo/jquery-base64
* Adapted by SeViR in DIGIO
*/
if (!window.atob && !window.btoa){
( function( window ) {
var _PADCHAR = "=",
_ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function _getbyte64( s, i ) {
var idx = _ALPHA.indexOf( s.charAt( i ) );
if ( idx === -1 ) {
throw "Cannot decode base64";
}
return idx;
}
function _decode( s ) {
var pads = 0,
i,
b10,
imax = s.length,
x = [];
s = String( s );
if ( imax === 0 ) {
return s;
}
if ( imax % 4 !== 0 ) {
throw "Cannot decode base64";
}
if ( s.charAt( imax - 1 ) === _PADCHAR ) {
pads = 1;
if ( s.charAt( imax - 2 ) === _PADCHAR ) {
pads = 2;
}
// either way, we want to ignore this last block
imax -= 4;
}
for ( i = 0; i < imax; i += 4 ) {
b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 ) | _getbyte64( s, i + 3 );
x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff, b10 & 0xff ) );
}
switch ( pads ) {
case 1:
b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 );
x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff ) );
break;
case 2:
b10 = ( _getbyte64( s, i ) << 18) | ( _getbyte64( s, i + 1 ) << 12 );
x.push( String.fromCharCode( b10 >> 16 ) );
break;
}
return x.join( "" );
}
function _getbyte( s, i ) {
var x = s.charCodeAt( i );
if ( x > 255 ) {
throw "INVALID_CHARACTER_ERR: DOM Exception 5";
}
return x;
}
function _encode( s ) {
if ( arguments.length !== 1 ) {
throw "SyntaxError: exactly one argument required";
}
s = String( s );
var i,
b10,
x = [],
imax = s.length - s.length % 3;
if ( s.length === 0 ) {
return s;
}
for ( i = 0; i < imax; i += 3 ) {
b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 ) | _getbyte( s, i + 2 );
x.push( _ALPHA.charAt( b10 >> 18 ) );
x.push( _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) );
x.push( _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) );
x.push( _ALPHA.charAt( b10 & 0x3f ) );
}
switch ( s.length - imax ) {
case 1:
b10 = _getbyte( s, i ) << 16;
x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _PADCHAR + _PADCHAR );
break;
case 2:
b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 );
x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) + _PADCHAR );
break;
}
return x.join( "" );
}
window.btoa = _encode;
window.atoa = _decode;
})( window );
}