      1 //
      2 // NOTE : The original code is wrapped so that the defined functions don't collide with existing ones.
      3 // See http://michaux.ca/articles/javascript-namespacing.
      4 // See http://msdn.microsoft.com/en-us/library/259s7zc1%28v=vs.85%29.aspx
      5 //
      6
      7 /** @namespace */
      8 Crypto = typeof Crypto != 'undefined' ? Crypto : {};
      9 /** @namespace */
     10 Crypto.Base64 = (function(/*window, undefined, $*/) {
     11
     12 //
     13 // START OF ORIGINAL CODE
     14 //
     15
     16
     17 /* OpenPGP radix-64/base64 string encoding/decoding
     18  * Copyright 2005 Herbert Hanewinkel, www.haneWIN.de
     19  * version 1.0, check www.haneWIN.de for the latest version
     20
     21  * This software is provided as-is, without express or implied warranty.
     22  * Permission to use, copy, modify, distribute or sell this software, with or
     23  * without fee, for any purpose and by any individual or organization, is hereby
     24  * granted, provided that the above copyright notice and this paragraph appear
     25  * in all copies. Distribution as a part of an application or binary must
     26  * include the above copyright notice in the documentation and/or other materials
     27  * provided with the application or distribution.
     28  */
     29
     30 var b64s='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
     31
     32 function s2r(t)
     33 {
     34  var a, c, n;
     35  var r='', l=0, s=0;
     36  var tl=t.length;
     37
     38  for(n=0; n<tl; n++)
     39  {
     40   c=t.charCodeAt(n);
     41   if(s == 0)
     42   {
     43    r+=b64s.charAt((c>>2)&63);
     44    a=(c&3)<<4;
     45   }
     46   else if(s==1)
     47   {
     48    r+=b64s.charAt((a|(c>>4)&15));
     49    a=(c&15)<<2;
     50   }
     51   else if(s==2)
     52   {
     53    r+=b64s.charAt(a|((c>>6)&3));
     54    l+=1;
     55    if((l%60)==0) r+="\n";
     56    r+=b64s.charAt(c&63);
     57   }
     58   l+=1;
     59   if((l%60)==0) r+="\n";
     60
     61   s+=1;
     62   if(s==3) s=0;
     63  }
     64  if(s>0)
     65  {
     66   r+=b64s.charAt(a);
     67   l+=1;
     68   if((l%60)==0) r+="\n";
     69   r+='=';
     70   l+=1;
     71  }
     72  if(s==1)
     73  {
     74   if((l%60)==0) r+="\n";
     75   r+='=';
     76  }
     77
     78  return r;
     79 }
     80
     81 function r2s(t)
     82 {
     83  var c, n;
     84  var r='', s=0, a=0;
     85  var tl=t.length;
     86
     87  for(n=0; n<tl; n++)
     88  {
     89   c=b64s.indexOf(t.charAt(n));
     90   if(c >= 0)
     91   {
     92    if(s) r+=String.fromCharCode(a | (c>>(6-s))&255);
     93    s=(s+2)&7;
     94    a=(c<<s)&255;
     95   }
     96  }
     97  return r;
     98 }
     99
    100 //
    101 // END OF ORIGINAL CODE
    102 //
    103
    104 return {
    105     s2r: s2r,
    106     r2s: r2s,
    107 };
    108
    109 })(/*window, undefined, $*/);