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 */
11 * Original code (c) 2010 Nick Galbreath
12 * http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript
14 * jQuery port (c) 2010 Carlo Zottmann
15 * http://github.com/carlo/jquery-base64
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
26 * The above copyright notice and this permission notice shall be
27 * included in all copies or substantial portions of the Software.
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.
39 /* base64 encode/decode compatible with window.btoa/atob
41 * window.atob/btoa is a Firefox extension to convert binary data (the "b")
42 * to base64 (ascii, the "a").
44 * It is also found in Safari and Chrome. It is not available in IE.
46 * if (!window.btoa) window.btoa = $.base64.encode
47 * if (!window.atob) window.atob = $.base64.decode
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
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.
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.
61 jQuery.base64 = ( function( $ ) {
64 _ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
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)
73 var idx = _ALPHA.indexOf( s.charAt( i ) );
76 throw "Cannot decode base64";
83 function _decode( s ) {
96 if ( imax % 4 !== 0 ) {
97 throw "Cannot decode base64";
100 if ( s.charAt( imax - 1 ) === _PADCHAR ) {
103 if ( s.charAt( imax - 2 ) === _PADCHAR ) {
107 // either way, we want to ignore this last block
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 ) );
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 ) );
123 b10 = ( _getbyte64( s, i ) << 18) | ( _getbyte64( s, i + 1 ) << 12 );
124 x.push( String.fromCharCode( b10 >> 16 ) );
132 function _getbyte( s, i ) {
133 var x = s.charCodeAt( i );
136 throw "INVALID_CHARACTER_ERR: DOM Exception 5";
143 function _encode( s ) {
144 if ( arguments.length !== 1 ) {
145 throw "SyntaxError: exactly one argument required";
153 imax = s.length - s.length % 3;
155 if ( s.length === 0 ) {
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 ) );
167 switch ( s.length - imax ) {
169 b10 = _getbyte( s, i ) << 16;
170 x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _PADCHAR + _PADCHAR );
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 );