From acd6f804f4910381e24f1c33e671e3f339e1ddfa Mon Sep 17 00:00:00 2001 From: Fabio Lima Santos Date: Fri, 1 Nov 2013 14:55:57 -0200 Subject: [PATCH] Changing the behaviour to enable handling URL safe base64 strings. In some implementations, the '=' characters are omitted, leaving the size of the string inconsistent with the ( imax % 4 == 0 ) restriction. --- jquery.base64.js | 7 ++++--- jquery.base64.min.js | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/jquery.base64.js b/jquery.base64.js index 8cda79d..76d33f9 100644 --- a/jquery.base64.js +++ b/jquery.base64.js @@ -64,7 +64,6 @@ jQuery.base64 = ( function( $ ) { _ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", _VERSION = "1.0"; - function _getbyte64( s, i ) { // This is oddly fast, except on Chrome/V8. // Minimal or no improvement in performance by using a @@ -93,8 +92,10 @@ jQuery.base64 = ( function( $ ) { return s; } - if ( imax % 4 !== 0 ) { - throw "Cannot decode base64"; + // handle base64 URL safe strings + while ( imax % 4 !== 0 ) { + s += '='; + imax = s.length; } if ( s.charAt( imax - 1 ) === _PADCHAR ) { diff --git a/jquery.base64.min.js b/jquery.base64.min.js index 2687c41..e3dbc28 100644 --- a/jquery.base64.min.js +++ b/jquery.base64.min.js @@ -1 +1 @@ -"use strict";jQuery.base64=(function($){var _PADCHAR="=",_ALPHA="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",_VERSION="1.0";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}imax-=4}for(i=0;i>16,(b10>>8)&255,b10&255))}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)&255));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>18));x.push(_ALPHA.charAt((b10>>12)&63));x.push(_ALPHA.charAt((b10>>6)&63));x.push(_ALPHA.charAt(b10&63))}switch(s.length-imax){case 1:b10=_getbyte(s,i)<<16;x.push(_ALPHA.charAt(b10>>18)+_ALPHA.charAt((b10>>12)&63)+_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)&63)+_ALPHA.charAt((b10>>6)&63)+_PADCHAR);break}return x.join("")}return{decode:_decode,encode:_encode,VERSION:_VERSION}}(jQuery)); \ No newline at end of file +"use strict";jQuery.base64=function(e){function i(e,t){var r=n.indexOf(e.charAt(t));if(r===-1){throw"Cannot decode base64"}return r}function s(e){var n=0,r,s,o=e.length,u=[];e=String(e);if(o===0){return e}while(o%4!==0){e+="=";o=e.length}if(e.charAt(o-1)===t){n=1;if(e.charAt(o-2)===t){n=2}o-=4}for(r=0;r>16,s>>8&255,s&255))}switch(n){case 1:s=i(e,r)<<18|i(e,r+1)<<12|i(e,r+2)<<6;u.push(String.fromCharCode(s>>16,s>>8&255));break;case 2:s=i(e,r)<<18|i(e,r+1)<<12;u.push(String.fromCharCode(s>>16));break}return u.join("")}function o(e,t){var n=e.charCodeAt(t);if(n>255){throw"INVALID_CHARACTER_ERR: DOM Exception 5"}return n}function u(e){if(arguments.length!==1){throw"SyntaxError: exactly one argument required"}e=String(e);var r,i,s=[],u=e.length-e.length%3;if(e.length===0){return e}for(r=0;r>18));s.push(n.charAt(i>>12&63));s.push(n.charAt(i>>6&63));s.push(n.charAt(i&63))}switch(e.length-u){case 1:i=o(e,r)<<16;s.push(n.charAt(i>>18)+n.charAt(i>>12&63)+t+t);break;case 2:i=o(e,r)<<16|o(e,r+1)<<8;s.push(n.charAt(i>>18)+n.charAt(i>>12&63)+n.charAt(i>>6&63)+t);break}return s.join("")}var t="=",n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",r="1.0";return{decode:s,encode:u,VERSION:r}}(jQuery)