Guest User

Untitled

a guest
May 31st, 2011
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if('atob' == typeof window.noFunc) {
  2.     function atob (text) {
  3.             text = text.replace(/\s/g,"");
  4.  
  5.             if(!(/^[a-z0-9\+\/\s]+\={0,2}$/i.test(text)) || text.length % 4 > 0){
  6.                     throw new Error("Not a base64-encoded string.");
  7.             }  
  8.  
  9.             //local variables
  10.             var digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  11.                     cur, prev, digitNum,
  12.                     i=0,
  13.                     result = [];
  14.  
  15.             text = text.replace(/=/g, "");
  16.  
  17.             while(i < text.length){
  18.  
  19.                     cur = digits.indexOf(text.charAt(i));
  20.                     digitNum = i % 4;
  21.  
  22.                     switch(digitNum){
  23.  
  24.                             //case 0: first digit - do nothing, not enough info to work with
  25.  
  26.                             case 1: //second digit
  27.                                     result.push(String.fromCharCode(prev << 2 | cur >> 4));
  28.                                     break;
  29.  
  30.                             case 2: //third digit
  31.                                     result.push(String.fromCharCode((prev & 0x0f) << 4 | cur >> 2));
  32.                                     break;
  33.  
  34.                             case 3: //fourth digit
  35.                                     result.push(String.fromCharCode((prev & 3) << 6 | cur));
  36.                                     break;
  37.                     }
  38.  
  39.                     prev = cur;
  40.                     i++;
  41.             }
  42.  
  43.             return result.join("");
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment