/*
 * <copyright> 
 * Copyright (c) 2001 by Hyperwave AG 
 * </copyright>
 *
 * <file>
 * Name: Base64Encoder.js
 * Created: 2001-09-12
 * $Id: Encoder.js,v 1.3 2002/09/20 10:04:49 szwoelf Exp $
 * </file>
 */

initPackage ( "com.hyperwave.util" );

//----------------------------------------------------------------------
/**
 * Static class represents a utility for encoding strings with different 
 * encodings. Currently only base64 is implemented.
 */
// <JSClass Name="com.hyperwave.util.Encoder">


  //-----------------------------------------------------------------
  /**
   * Public constructor, not to be used!
   * @param aParam: void | String: only used for internal inheritance
   */
   com.hyperwave.util.Encoder = function( aParam )
   {
    if ( aParam == "__proto__" )
      return;
   }
   class$ = doInherit( com.hyperwave.util.Encoder );
   
   //-----------------------------------------------------------------
   /**
    * Static method for encoding strings on base 64. Note that this
    * method will use the native encoding if possible.
    *
    * @param theString: String: string to encode.
    */
    if (typeof encodeBase64 != "undefined")
      // Hyperwave environment (hwjs and wavemaster)
      class$.static_.encodeBase64 = encodeBase64;
    else if (typeof btoa != "undefined")
    {
      class$.static_.encodeBase64 = function ( theString )
      {    
        // Netscape browsers do know encoding
        // Note: (NS6) call btoa on the window and not on the static object
        return ( btoa ( theString ) );
      }
    }
    else 
      class$.static_.encodeBase64 = function ( theString )
      {    
        var pem_array = [
            //       0   1   2   3   4   5   6   7
                    'A','B','C','D','E','F','G','H', // 0
                    'I','J','K','L','M','N','O','P', // 1
                    'Q','R','S','T','U','V','W','X', // 2
                    'Y','Z','a','b','c','d','e','f', // 3
                    'g','h','i','j','k','l','m','n', // 4
                    'o','p','q','r','s','t','u','v', // 5
                    'w','x','y','z','0','1','2','3', // 6
                    '4','5','6','7','8','9','+','/'  // 7
            ];
      
        var out_buf = [];
      
        
        // enocodeAtom - Take three bytes of input and encode it as 4
        // printable characters. Note that if the length in len is less
        // than three is encodes either one or two '=' signs to indicate
        // padding characters. 
        function encodeAtom ( data, offset, len )
        {
          var a, b, c;
      
          if (len == 1) {
              a = data.charCodeAt (offset);
              b = 0;
              c = 0;
              out_buf [out_buf.length] = (pem_array[(a >>> 2) & 0x3F]);
              out_buf [out_buf.length] = (pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
              out_buf [out_buf.length] = ('=');
              out_buf [out_buf.length] = ('=');
          } else if (len == 2) {
              a = data.charCodeAt (offset);
              b = data.charCodeAt (offset + 1);
              c = 0;
              out_buf [out_buf.length] = (pem_array[(a >>> 2) & 0x3F]);
              out_buf [out_buf.length] = (pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
              out_buf [out_buf.length] = (pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
              out_buf [out_buf.length] = ('=');
          } else {
              a = data.charCodeAt (offset);
              b = data.charCodeAt (offset + 1);
              c = data.charCodeAt (offset + 2);
              out_buf [out_buf.length] = (pem_array[(a >>> 2) & 0x3F]);
              out_buf [out_buf.length] = (pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
              out_buf [out_buf.length] = (pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
              out_buf [out_buf.length] = (pem_array[c & 0x3F]);
          }
        }
      
        // step through the whole string and encode it bit by bit
        for ( var i = 0, ind = theString.length; i < theString.length; i+=3, ind -= 3 )
        {
          var len = 3;
          if ( ind < 3 )
            len = ind;
          encodeAtom ( theString, i, len );
        }
        
        return out_buf.join ( "" );
      }
    
    
    class$.static_.main = function ()
    {
      var s = "super:test";
      var x = com.hyperwave.util.Encoder.static_.encodeBase64 ( s );
      
      if ( typeof writeln != "undefined" )
      {
        writeln ( "passwd:" + s );
        writeln ( "encoded:" + x );
      }
      else
      {
        document.writeln ( "passwd:<b>" + s + "</b><br>" );
        document.writeln ( "encoded:<b>" + x + "</b><br>" );
      }
    }
    
// </JSClass>

