(file) Return to Base64.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

Diff for /pegasus/src/Pegasus/Common/Base64.cpp between version 1.5 and 1.13

version 1.5, 2002/02/08 17:13:14 version 1.13, 2003/03/06 23:05:28
Line 1 
Line 1 
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
 // //
 // Copyright (c) 2000, 2001 BMC Software, Hewlett-Packard Company, IBM,  // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
 // The Open Group, Tivoli Systems // The Open Group, Tivoli Systems
 // //
 // Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
Line 27 
Line 27 
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
 #include "Base64.h"  
 #include <cassert>  
 #include <strstream> #include <strstream>
 #include <string> #include <string>
 #include <Pegasus/Common/String.h>  #include "Base64.h"
 #include <Pegasus/Common/Base64.h>  #include <Pegasus/Common/ArrayInternal.h>
 #include <Pegasus/Common/Array.h>  
   #ifdef PEGASUS_OS_OS400
   // OS400 is EBCDIC so it needs to convert string and characters to ASCII
   // prior to decode and encode.  The encoding and decoding are done in ASCII.
   #include <Pegasus/Common/OS400ConvertChar.h>
   #endif
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
 PEGASUS_USING_STD; PEGASUS_USING_STD;
Line 57 
Line 60 
         return retArray;         return retArray;
     // for every character in the input array taken 3 bytes at a time     // for every character in the input array taken 3 bytes at a time
  
   #ifdef PEGASUS_OS_OS400
       //For OS400 convert from ebcdic to ascii
       EtoA((char *)vby.getData(), vby.size());
   #endif
   
     for (Uint32 i=0; i < vby.size(); i+=3)     for (Uint32 i=0; i < vby.size(); i+=3)
     {     {
  
Line 84 
Line 92 
         if (i+1<vby.size())         if (i+1<vby.size())
             retArray.append( _Encode(by6));             retArray.append( _Encode(by6));
         else         else
   // NOTE:  If this code changes the OS400 code below may also need applicable changes
   #ifdef PEGASUS_OS_OS400
               retArray.append(0x3D); /* ascii '=' */
   #else
             retArray.append('=');             retArray.append('=');
   #endif
   
  
         if (i+2<vby.size())         if (i+2<vby.size())
             retArray.append( _Encode(by7));             retArray.append( _Encode(by7));
         else         else
   // NOTE:  If this code changes the OS400 code below may also need applicable changes
   #ifdef PEGASUS_OS_OS400
               retArray.append(0x3D); /* ascii '=' */
   #else
             retArray.append('=');             retArray.append('=');
   #endif
   
           /* ATTN: Need to fix this. It adds unwanted cr-lf after 4 chars.
  
         if (i % (76/4*3) == 0)         if (i % (76/4*3) == 0)
         {         {
             retArray.append( '\r');             retArray.append( '\r');
             retArray.append( '\n');             retArray.append( '\n');
         }         }
           */
     };     };
  
   #ifdef PEGASUS_OS_OS400
       //For OS400 convert from ascii to ebcdic
       AtoE((char *)retArray.getData(), retArray.size());
   #endif
   
     return retArray;     return retArray;
 }; };
 /*I checked for the zero length. The algorithm would also work for zero length input stream, but I’m pretty adamant about handling border conditions. They are often the culprits of run-time production failures. /*I checked for the zero length. The algorithm would also work for zero length input stream, but I’m pretty adamant about handling border conditions. They are often the culprits of run-time production failures.
 The algorithm goes thru each three bytes of data at a time. The first thing I do is to shift the bits around from three 8-bit values to four 6-bit values. Then I encode the 6-bit values and add then one at a time to the output stream. This is actually quite inefficient. The STL character array is being allocated one byte at a time. The algorithm would be much faster, if I pre-allocated that array. I’ll leave that as an optimization practical exercise for the reader. The algorithm goes thru each three bytes of data at a time. The first thing I do is to shift the bits around from three 8-bit values to four 6-bit values. Then I encode the 6-bit values and add then one at a time to the output stream. This is actually quite inefficient. The STL character array is being allocated one byte at a time. The algorithm would be much faster, if I pre-allocated that array. I’ll leave that as an optimization practical exercise for the reader.
   */
  
 /*  The decode static method takes a base-64 stream and converts it /*  The decode static method takes a base-64 stream and converts it
     to an array of 8-bit values.     to an array of 8-bit values.
 */ */
 Array<Uint8> Base64::decode(const Array<Sint8> strInput) Array<Uint8> Base64::decode(const Array<Sint8> strInput)
 { {
   #ifdef PEGASUS_OS_OS400
       //For OS400 convert from ebcdic to ascii
       EtoA((char *)strInput.getData(), strInput.size());
   #endif
   
     //Strip any non-base64 characters from the input     //Strip any non-base64 characters from the input
     PEGASUS_STD(string) str;     PEGASUS_STD(string) str;
     for (Uint32 j=0;j<strInput.size();j++)     for (Uint32 j=0;j<strInput.size();j++)
Line 124 
Line 156 
         return retArray;         return retArray;
  
     //  comment     //  comment
     for (int i=0; i < str.length();i+=4)      for (size_t i=0; i < str.length();i+=4)
     {     {
   // NOTE:  If this code changes the OS400 code below may also need applicable changes
   #ifdef PEGASUS_OS_OS400
           char c1=0x41,c2=0x41,c3=0x41,c4=0x41;  /* ascii 'A' */
   #else
         char c1='A',c2='A',c3='A',c4='A';         char c1='A',c2='A',c3='A',c4='A';
   #endif
  
         c1 = str[i];         c1 = str[i];
         if (i+1<str.length())         if (i+1<str.length())
Line 151 
Line 188 
         retArray.append( (by1<<2)|(by2>>4) );         retArray.append( (by1<<2)|(by2>>4) );
  
         // append second byte if not padding         // append second byte if not padding
   // NOTE:  If this code changes the OS400 code below may also need applicable changes
   #ifdef PEGASUS_OS_OS400
           if (c3 != 0x3d)  /* ascii '=' */
   #else
         if (c3 != '=')         if (c3 != '=')
   #endif
             retArray.append( ((by2&0xf)<<4)|(by3>>2) );             retArray.append( ((by2&0xf)<<4)|(by3>>2) );
  
   // NOTE:  If this code changes the OS400 code below may also need applicable changes
   #ifdef PEGASUS_OS_OS400
           if (c4 != 0x3d)  /* ascii '=' */
   #else
         if (c4 != '=')         if (c4 != '=')
   #endif
             retArray.append( ((by3&0x3)<<6)|by4 );             retArray.append( ((by3&0x3)<<6)|by4 );
     }     }
  
   #ifdef PEGASUS_OS_OS400
       //For OS400 convert from ascii to ebcdic
       AtoE((char *)retArray.getData(), retArray.size());
   #endif
   
     return retArray;     return retArray;
 }; };
  
Line 168 
Line 220 
 */ */
 inline PEGASUS_COMMON_LINKAGE char Base64::_Encode(Uint8 uc) inline PEGASUS_COMMON_LINKAGE char Base64::_Encode(Uint8 uc)
 { {
   // NOTE:  If this code changes the OS400 code below may also need applicable changes
   #if !defined(PEGASUS_OS_OS400)
     if (uc < 26)     if (uc < 26)
         return 'A'+uc;         return 'A'+uc;
  
Line 181 
Line 235 
         return '+';         return '+';
  
     return '/';     return '/';
   #else
       if (uc < 26)
           return 0x41+uc; /* ascii 'A' */
   
       if (uc < 52)
           return 0x61+(uc-26);  /* ascii 'a' */
   
       if (uc < 62)
           return 0x30+(uc-52);  /* ascii '0' */
   
       if (uc == 62)
           return 0x2B;  /* ascii '+' */
   
       return 0x2F;  /* ascii '/' */
   #endif
 }; };
  
  //Helper function returns true is a character is a valid base-64 character and false otherwise.  //Helper function returns true is a character is a valid base-64 character and false otherwise.
Line 188 
Line 257 
 inline Boolean Base64::_IsBase64(char c) inline Boolean Base64::_IsBase64(char c)
 { {
  
   // NOTE:  If this code changes the OS400 code below may also need applicable changes
   #if !defined(PEGASUS_OS_OS400)
     if (c >= 'A' && c <= 'Z')     if (c >= 'A' && c <= 'Z')
         return true;         return true;
  
Line 205 
Line 276 
  
     if (c == '=')     if (c == '=')
         return true;         return true;
   #else
       if (c >= 0x41 && c <= 0x5A)  /* ascii 'A' & 'Z' */
           return true;
   
       if (c >= 0x61 && c <= 0x7A)  /* ascii 'a' & 'z' */
           return true;
   
       if (c >= 0x30 && c <= 0x39) /* ascii '0' & '9' */
           return true;
   
       if (c == 0x2B) /* ascii '+' */
           return true;
   
       if (c == 0x2F) /* ascii '/' */
           return true;
   
       if (c == 0x3D) /* ascii '=' */
           return true;
   #endif
  
     return false;     return false;
 }; };
Line 212 
Line 302 
  // Translate one base-64 character into a six bit pattern  // Translate one base-64 character into a six bit pattern
 inline Uint8 Base64::_Decode(char c) inline Uint8 Base64::_Decode(char c)
 { {
   // NOTE:  If this code changes the OS400 code below may also need applicable changes
   #if !defined(PEGASUS_OS_OS400)
     if (c >= 'A' && c <= 'Z')     if (c >= 'A' && c <= 'Z')
         return c - 'A';         return c - 'A';
     if (c >= 'a' && c <= 'z')     if (c >= 'a' && c <= 'z')
Line 222 
Line 314 
  
     if (c == '+')     if (c == '+')
         return 62;         return 62;
   #else
       if (c >= 0x41 && c <= 0x5A)  /* ascii 'A' & 'Z' */
           return c - 0x41;        /* ascii 'A' */
       if (c >= 0x61 && c <= 0x7A)  /* ascii 'a' & 'z' */
           return c - 0x61 + 26;    /* ascii 'a' */
   
       if (c >= 0x30 && c <= 0x39) /* ascii '0' & '9' */
           return c - 0x30 + 52;  /* ascii '0' */
   
       if (c == 0x2B)  /* ascii '+' */
           return 62;
   #endif
  
     return 63;     return 63;
 }; };


Legend:
Removed from v.1.5  
changed lines
  Added in v.1.13

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2