Advertisement
rg443

javascript base64

Jan 19th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* http://www.onicos.com/staff/iz/amuse/javascript/expert/base64.txt
  2.  * Version: 1.0
  3.  * LastModified: Dec 25 1999
  4.  * This library is free.  You can redistribute it and/or modify it.
  5.  */
  6.  
  7. /*
  8.  * Interfaces:
  9.  * b64 = base64encode(data);
  10.  * data = base64decode(b64);
  11.  */
  12.  
  13.  
  14. var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  15. var base64DecodeChars = new Array(
  16.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  17.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  18.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  19.     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  20.     -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
  21.     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  22.     -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  23.     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
  24.  
  25. function base64encode(str) {
  26.     var out, i, len;
  27.     var c1, c2, c3;
  28.  
  29.     len = str.length;
  30.     i = 0;
  31.     out = "";
  32.     while(i < len) {
  33.     c1 = str.charCodeAt(i++) & 0xff;
  34.     if(i == len)
  35.     {
  36.         out += base64EncodeChars.charAt(c1 >> 2);
  37.         out += base64EncodeChars.charAt((c1 & 0x3) << 4);
  38.         out += "==";
  39.         break;
  40.     }
  41.     c2 = str.charCodeAt(i++);
  42.     if(i == len)
  43.     {
  44.         out += base64EncodeChars.charAt(c1 >> 2);
  45.         out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
  46.         out += base64EncodeChars.charAt((c2 & 0xF) << 2);
  47.         out += "=";
  48.         break;
  49.     }
  50.     c3 = str.charCodeAt(i++);
  51.     out += base64EncodeChars.charAt(c1 >> 2);
  52.     out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
  53.     out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
  54.     out += base64EncodeChars.charAt(c3 & 0x3F);
  55.     }
  56.     return out;
  57. }
  58.  
  59. function base64decode(str) {
  60.     var c1, c2, c3, c4;
  61.     var i, len, out;
  62.  
  63.     len = str.length;
  64.     i = 0;
  65.     out = "";
  66.     while(i < len) {
  67.     /* c1 */
  68.     do {
  69.         c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
  70.     } while(i < len && c1 == -1);
  71.     if(c1 == -1)
  72.         break;
  73.  
  74.     /* c2 */
  75.     do {
  76.         c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
  77.     } while(i < len && c2 == -1);
  78.     if(c2 == -1)
  79.         break;
  80.  
  81.     out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
  82.  
  83.     /* c3 */
  84.     do {
  85.         c3 = str.charCodeAt(i++) & 0xff;
  86.         if(c3 == 61)
  87.         return out;
  88.         c3 = base64DecodeChars[c3];
  89.     } while(i < len && c3 == -1);
  90.     if(c3 == -1)
  91.         break;
  92.  
  93.     out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
  94.  
  95.     /* c4 */
  96.     do {
  97.         c4 = str.charCodeAt(i++) & 0xff;
  98.         if(c4 == 61)
  99.         return out;
  100.         c4 = base64DecodeChars[c4];
  101.     } while(i < len && c4 == -1);
  102.     if(c4 == -1)
  103.         break;
  104.     out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
  105.     }
  106.     return out;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement