Skip to content

Commit cc6f74e

Browse files
committed
Update jquery.base64.js
1 parent 943dc96 commit cc6f74e

File tree

1 file changed

+141
-189
lines changed

1 file changed

+141
-189
lines changed

jquery.base64.js

Lines changed: 141 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -1,190 +1,142 @@
1-
/*jslint adsafe: false, bitwise: true, browser: true, cap: false, css: false,
2-
debug: false, devel: true, eqeqeq: true, es5: false, evil: false,
3-
forin: false, fragment: false, immed: true, laxbreak: false, newcap: true,
4-
nomen: false, on: false, onevar: true, passfail: false, plusplus: true,
5-
regexp: false, rhino: true, safe: false, strict: false, sub: false,
6-
undef: true, white: false, widget: false, windows: false */
7-
/*global jQuery: false, window: false */
8-
//"use strict";
9-
10-
/*
11-
* Original code (c) 2010 Nick Galbreath
12-
* http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript
13-
*
14-
* jQuery port (c) 2010 Carlo Zottmann
15-
* http://github.com/carlo/jquery-base64
16-
*
17-
* Permission is hereby granted, free of charge, to any person
18-
* obtaining a copy of this software and associated documentation
19-
* files (the "Software"), to deal in the Software without
20-
* restriction, including without limitation the rights to use,
21-
* copy, modify, merge, publish, distribute, sublicense, and/or sell
22-
* copies of the Software, and to permit persons to whom the
23-
* Software is furnished to do so, subject to the following
24-
* conditions:
25-
*
26-
* The above copyright notice and this permission notice shall be
27-
* included in all copies or substantial portions of the Software.
28-
*
29-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30-
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
31-
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32-
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
33-
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
34-
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
35-
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
36-
* OTHER DEALINGS IN THE SOFTWARE.
37-
*/
38-
39-
/* base64 encode/decode compatible with window.btoa/atob
40-
*
41-
* window.atob/btoa is a Firefox extension to convert binary data (the "b")
42-
* to base64 (ascii, the "a").
43-
*
44-
* It is also found in Safari and Chrome. It is not available in IE.
45-
*
46-
* if (!window.btoa) window.btoa = $.base64.encode
47-
* if (!window.atob) window.atob = $.base64.decode
48-
*
49-
* The original spec's for atob/btoa are a bit lacking
50-
* https://developer.mozilla.org/en/DOM/window.atob
51-
* https://developer.mozilla.org/en/DOM/window.btoa
52-
*
53-
* window.btoa and $.base64.encode takes a string where charCodeAt is [0,255]
54-
* If any character is not [0,255], then an exception is thrown.
55-
*
56-
* window.atob and $.base64.decode take a base64-encoded string
57-
* If the input length is not a multiple of 4, or contains invalid characters
58-
* then an exception is thrown.
59-
*/
60-
61-
jQuery.base64 = ( function( $ ) {
62-
63-
var _PADCHAR = "=",
64-
_ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
65-
_VERSION = "1.0";
66-
67-
68-
function _getbyte64( s, i ) {
69-
// This is oddly fast, except on Chrome/V8.
70-
// Minimal or no improvement in performance by using a
71-
// object with properties mapping chars to value (eg. 'A': 0)
72-
73-
var idx = _ALPHA.indexOf( s.charAt( i ) );
74-
75-
if ( idx === -1 ) {
76-
throw "Cannot decode base64";
77-
}
78-
79-
return idx;
80-
}
81-
82-
83-
function _decode( s ) {
84-
var pads = 0,
85-
i,
86-
b10,
87-
imax = s.length,
88-
x = [];
89-
90-
s = String( s );
91-
92-
if ( imax === 0 ) {
93-
return s;
94-
}
95-
96-
if ( imax % 4 !== 0 ) {
97-
throw "Cannot decode base64";
98-
}
99-
100-
if ( s.charAt( imax - 1 ) === _PADCHAR ) {
101-
pads = 1;
102-
103-
if ( s.charAt( imax - 2 ) === _PADCHAR ) {
104-
pads = 2;
105-
}
106-
107-
// either way, we want to ignore this last block
108-
imax -= 4;
109-
}
110-
111-
for ( i = 0; i < imax; i += 4 ) {
112-
b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 ) | _getbyte64( s, i + 3 );
113-
x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff, b10 & 0xff ) );
114-
}
115-
116-
switch ( pads ) {
117-
case 1:
118-
b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 );
119-
x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff ) );
120-
break;
121-
122-
case 2:
123-
b10 = ( _getbyte64( s, i ) << 18) | ( _getbyte64( s, i + 1 ) << 12 );
124-
x.push( String.fromCharCode( b10 >> 16 ) );
125-
break;
126-
}
127-
128-
return x.join( "" );
129-
}
130-
131-
132-
function _getbyte( s, i ) {
133-
var x = s.charCodeAt( i );
134-
135-
if ( x > 255 ) {
136-
throw "INVALID_CHARACTER_ERR: DOM Exception 5";
137-
}
138-
139-
return x;
140-
}
141-
142-
143-
function _encode( s ) {
144-
if ( arguments.length !== 1 ) {
145-
throw "SyntaxError: exactly one argument required";
146-
}
147-
148-
s = String( s );
149-
150-
var i,
151-
b10,
152-
x = [],
153-
imax = s.length - s.length % 3;
154-
155-
if ( s.length === 0 ) {
156-
return s;
157-
}
158-
159-
for ( i = 0; i < imax; i += 3 ) {
160-
b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 ) | _getbyte( s, i + 2 );
161-
x.push( _ALPHA.charAt( b10 >> 18 ) );
162-
x.push( _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) );
163-
x.push( _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) );
164-
x.push( _ALPHA.charAt( b10 & 0x3f ) );
165-
}
166-
167-
switch ( s.length - imax ) {
168-
case 1:
169-
b10 = _getbyte( s, i ) << 16;
170-
x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _PADCHAR + _PADCHAR );
171-
break;
172-
173-
case 2:
174-
b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 );
175-
x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) + _PADCHAR );
176-
break;
1+
/**
2+
*
3+
* Base64 encode / decode
4+
* http://www.webtoolkit.info/
5+
*
6+
**/
7+
8+
var Base64 = {
9+
10+
// private property
11+
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
12+
13+
// public method for encoding
14+
encode : function (input) {
15+
var output = "";
16+
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
17+
var i = 0;
18+
19+
input = Base64._utf8_encode(input);
20+
21+
while (i < input.length) {
22+
23+
chr1 = input.charCodeAt(i++);
24+
chr2 = input.charCodeAt(i++);
25+
chr3 = input.charCodeAt(i++);
26+
27+
enc1 = chr1 >> 2;
28+
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
29+
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
30+
enc4 = chr3 & 63;
31+
32+
if (isNaN(chr2)) {
33+
enc3 = enc4 = 64;
34+
} else if (isNaN(chr3)) {
35+
enc4 = 64;
36+
}
37+
38+
output = output +
39+
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
40+
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
41+
42+
}
43+
44+
return output;
45+
},
46+
47+
// public method for decoding
48+
decode : function (input) {
49+
var output = "";
50+
var chr1, chr2, chr3;
51+
var enc1, enc2, enc3, enc4;
52+
var i = 0;
53+
54+
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
55+
56+
while (i < input.length) {
57+
58+
enc1 = this._keyStr.indexOf(input.charAt(i++));
59+
enc2 = this._keyStr.indexOf(input.charAt(i++));
60+
enc3 = this._keyStr.indexOf(input.charAt(i++));
61+
enc4 = this._keyStr.indexOf(input.charAt(i++));
62+
63+
chr1 = (enc1 << 2) | (enc2 >> 4);
64+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
65+
chr3 = ((enc3 & 3) << 6) | enc4;
66+
67+
output = output + String.fromCharCode(chr1);
68+
69+
if (enc3 != 64) {
70+
output = output + String.fromCharCode(chr2);
71+
}
72+
if (enc4 != 64) {
73+
output = output + String.fromCharCode(chr3);
74+
}
75+
76+
}
77+
78+
output = Base64._utf8_decode(output);
79+
80+
return output;
81+
82+
},
83+
84+
// private method for UTF-8 encoding
85+
_utf8_encode : function (string) {
86+
string = string.replace(/\r\n/g,"\n");
87+
var utftext = "";
88+
89+
for (var n = 0; n < string.length; n++) {
90+
91+
var c = string.charCodeAt(n);
92+
93+
if (c < 128) {
94+
utftext += String.fromCharCode(c);
95+
}
96+
else if((c > 127) && (c < 2048)) {
97+
utftext += String.fromCharCode((c >> 6) | 192);
98+
utftext += String.fromCharCode((c & 63) | 128);
99+
}
100+
else {
101+
utftext += String.fromCharCode((c >> 12) | 224);
102+
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
103+
utftext += String.fromCharCode((c & 63) | 128);
104+
}
105+
106+
}
107+
108+
return utftext;
109+
},
110+
111+
// private method for UTF-8 decoding
112+
_utf8_decode : function (utftext) {
113+
var string = "";
114+
var i = 0;
115+
var c = c1 = c2 = 0;
116+
117+
while ( i < utftext.length ) {
118+
119+
c = utftext.charCodeAt(i);
120+
121+
if (c < 128) {
122+
string += String.fromCharCode(c);
123+
i++;
124+
}
125+
else if((c > 191) && (c < 224)) {
126+
c2 = utftext.charCodeAt(i+1);
127+
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
128+
i += 2;
129+
}
130+
else {
131+
c2 = utftext.charCodeAt(i+1);
132+
c3 = utftext.charCodeAt(i+2);
133+
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
134+
i += 3;
135+
}
136+
137+
}
138+
139+
return string;
177140
}
178-
179-
return x.join( "" );
180-
}
181-
182-
183-
return {
184-
decode: _decode,
185-
encode: _encode,
186-
VERSION: _VERSION
187-
};
188-
189-
}( jQuery ) );
190-
141+
142+
}

0 commit comments

Comments
 (0)