Commits

David Chambers committed c58eb4a

Added `throw` statements to more accurately mimic the native implementations.

  • Participants
  • Parent commits d0436f6

Comments (0)

Files changed (2)

 
   var
     characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
-    fromCharCode = String.fromCharCode;
+    fromCharCode = String.fromCharCode,
+    INVALID_CHARACTER_ERR = 'INVALID_CHARACTER_ERR';
 
   // encoder
   window.btoa || (
     window.btoa = function (string) {
       var
         a, b, b1, b2, b3, b4, c, i = 0,
-        len = string.length, result = '';
+        len = string.length, max = Math.max, result = '';
 
       while (i < len) {
         a = string.charCodeAt(i++) || 0;
         b = string.charCodeAt(i++) || 0;
         c = string.charCodeAt(i++) || 0;
 
+        if (max(a, b, c) > 0xFF) {
+          throw INVALID_CHARACTER_ERR;
+        }
+
         b1 = (a >> 2) & 0x3F;
         b2 = ((a & 0x3) << 4) | ((b >> 4) & 0xF);
         b3 = ((b & 0xF) << 2) | ((c >> 6) & 0x3);
   // decoder
   window.atob || (
     window.atob = function (string) {
+      string = string.replace(/=+$/, '');
       var
         a, b, b1, b2, b3, b4, c, i = 0,
         len = string.length, chars = [];
 
+      if (len % 4 == 1) throw INVALID_CHARACTER_ERR;
+
       while (i < len) {
         b1 = characters.indexOf(string.charAt(i++));
         b2 = characters.indexOf(string.charAt(i++));
-(function(c){var a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",b=String.fromCharCode;c.btoa||(c.btoa=function(e){var n,l,m,k,j,g,h,d=0,f=e.length,o="";while(d<f){n=e.charCodeAt(d++)||0;l=e.charCodeAt(d++)||0;h=e.charCodeAt(d++)||0;m=(n>>2)&63;k=((n&3)<<4)|((l>>4)&15);j=((l&15)<<2)|((h>>6)&3);g=h&63;if(!l){j=g=64}else{if(!h){g=64}}o+=a.charAt(m)+a.charAt(k)+a.charAt(j)+a.charAt(g)}return o});c.atob||(c.atob=function(e){var o,m,n,l,k,g,j,d=0,f=e.length,h=[];while(d<f){n=a.indexOf(e.charAt(d++));l=a.indexOf(e.charAt(d++));k=a.indexOf(e.charAt(d++));g=a.indexOf(e.charAt(d++));o=((n&63)<<2)|((l>>4)&3);m=((l&15)<<4)|((k>>2)&15);j=((k&3)<<6)|(g&63);h.push(b(o));if(m){h.push(b(m))}if(j){h.push(b(j))}}return h.join("")})}(this));
+(function(d){var a="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",c=String.fromCharCode,b="INVALID_CHARACTER_ERR";d.btoa||(d.btoa=function(f){var p,n,o,m,k,h,j,e=0,g=f.length,l=Math.max,q="";while(e<g){p=f.charCodeAt(e++)||0;n=f.charCodeAt(e++)||0;j=f.charCodeAt(e++)||0;if(l(p,n,j)>255){throw b}o=(p>>2)&63;m=((p&3)<<4)|((n>>4)&15);k=((n&15)<<2)|((j>>6)&3);h=j&63;if(!n){k=h=64}else{if(!j){h=64}}q+=a.charAt(o)+a.charAt(m)+a.charAt(k)+a.charAt(h)}return q});d.atob||(d.atob=function(f){f=f.replace(/=+$/,"");var p,n,o,m,l,h,k,e=0,g=f.length,j=[];if(g%4==1){throw b}while(e<g){o=a.indexOf(f.charAt(e++));m=a.indexOf(f.charAt(e++));l=a.indexOf(f.charAt(e++));h=a.indexOf(f.charAt(e++));p=((o&63)<<2)|((m>>4)&3);n=((m&15)<<4)|((l>>2)&15);k=((l&3)<<6)|(h&63);j.push(c(p));if(n){j.push(c(n))}if(k){j.push(c(k))}}return j.join("")})}(this));