⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cbase64coding.cpp

📁 Base64Coding源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
               character_4 = END_OF_BASE64_ENCODED_DATA;
            }
         }
         else
         {
            character_3 = END_OF_BASE64_ENCODED_DATA;
            character_4 = END_OF_BASE64_ENCODED_DATA;
         }
      }
      else
      {
         character_2 = END_OF_BASE64_ENCODED_DATA;
         character_3 = END_OF_BASE64_ENCODED_DATA;
         character_4 = END_OF_BASE64_ENCODED_DATA;
      }

      if ( character_1 == END_OF_BASE64_ENCODED_DATA ||
           character_2 == END_OF_BASE64_ENCODED_DATA )
      {
         destination.SetSize( add_index );
         return( TRUE );
      }

      character_1 = m_DecoderTable[ character_1 ];
      character_2 = m_DecoderTable[ character_2 ];

      byte_to_add = static_cast< BYTE>( ( ( character_1 << 2 ) | ( ( character_2 & 0x30 ) >> 4 ) ) );

      destination.SetAt( add_index, byte_to_add );
      add_index++;

      if ( character_3 == END_OF_BASE64_ENCODED_DATA )
      {
         destination.SetSize( add_index );
         return( TRUE );
      }

      character_3 = m_DecoderTable[ character_3 ];

      byte_to_add = static_cast< BYTE >( ( ( ( ( character_2 & 0x0F ) << 4 ) | ( ( character_3 & 0x3C ) >> 2 ) ) ) );

      destination.SetAt( add_index, byte_to_add );
      add_index++;

      if ( character_4 == END_OF_BASE64_ENCODED_DATA )
      {
         destination.SetSize( add_index );
         return( TRUE );
      }

      character_4 = m_DecoderTable[ character_4 ];

      byte_to_add = static_cast< BYTE >( ( ( ( character_3 & 0x03 ) << 6 ) | character_4 ) );

      destination.SetAt( add_index, byte_to_add );
      add_index++;
   }

   destination.SetSize( add_index );

   return( return_value );
}

BOOL CBase64Coding::Encode( const CByteArray& source, CByteArray& destination )
{

   // We don't want to make this static so we can reduce our
   // footprint in the library

   const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

   int source_index              = 0;
   int number_of_bytes_to_encode = source.GetSize();
   
   BYTE byte_to_add = 0;
   BYTE byte_1      = 0;
   BYTE byte_2      = 0;
   BYTE byte_3      = 0;

   const BYTE * input_buffer = source.GetData();

   DWORD add_index = (DWORD) ( (double) number_of_bytes_to_encode / (double) 0.75 ) + 1;
   add_index += ( ( number_of_bytes_to_encode % 3 ) + 1 );

   destination.SetSize( add_index );
   add_index = 0;

   while( source_index < number_of_bytes_to_encode )
   {
      // Output the first byte

      byte_1 = input_buffer[ source_index ];
      byte_to_add = alphabet[ ( byte_1 >> 2 ) ];

      destination.SetAt( add_index, byte_to_add );
      add_index++;

      source_index++;

      if ( source_index >= number_of_bytes_to_encode )
      {
         // We're at the end of the data to encode

         byte_2 = 0;
         byte_to_add = alphabet[ ( ( ( byte_1 & 0x03 ) << 4 ) | ( ( byte_2 & 0xF0 ) >> 4 ) ) ];

         destination.SetAt( add_index, byte_to_add );
         add_index++;

         destination.SetAt( add_index, END_OF_BASE64_ENCODED_DATA );
         add_index++;

         destination.SetAt( add_index, END_OF_BASE64_ENCODED_DATA );
         add_index++;

         destination.SetSize( add_index );
         return( TRUE );
      }
      else
      {
         byte_2 = input_buffer[ source_index ];
      }

      byte_to_add = alphabet[ ( ( ( byte_1 & 0x03 ) << 4 ) | ( ( byte_2 & 0xF0 ) >> 4 ) ) ];
      destination.SetAt( add_index, byte_to_add );
      add_index++;

      source_index++;

      if ( source_index >= number_of_bytes_to_encode )
      {
         // We ran out of bytes, we need to add the last half of byte_2 and pad
         byte_3 = 0;

         byte_to_add = alphabet[ ( ( ( byte_2 & 0x0F ) << 2 ) | ( ( byte_3 & 0xC0 ) >> 6 ) ) ];

         destination.SetAt( add_index, byte_to_add );
         add_index++;

         destination.SetAt( add_index, END_OF_BASE64_ENCODED_DATA );
         add_index++;

         destination.SetSize( add_index );
         return( TRUE );
      }
      else
      {
         byte_3 = input_buffer[ source_index ];
      }

      source_index++;

      byte_to_add = alphabet[ ( ( ( byte_2 & 0x0F ) << 2 ) | ( ( byte_3 & 0xC0 ) >> 6 ) ) ];

      destination.SetAt( add_index, byte_to_add );
      add_index++;

      byte_to_add = alphabet[ ( byte_3 & 0x3F ) ];

      destination.SetAt( add_index, byte_to_add );
      add_index++;
   }

   destination.SetAt( add_index, END_OF_BASE64_ENCODED_DATA );
   add_index++;

   destination.SetSize( add_index );
   return( TRUE );
}

BOOL CBase64Coding::Encode( const CByteArray& source, CString& destination_string )
{

   const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

   int loop_index                = 0;
   int number_of_bytes_to_encode = source.GetSize();

   BYTE byte_to_add = 0;
   BYTE byte_1      = 0;
   BYTE byte_2      = 0;
   BYTE byte_3      = 0;

   const BYTE * input_buffer = source.GetData();

   DWORD number_of_bytes_encoded = (DWORD) ( (double) number_of_bytes_to_encode / (double) 0.75 ) + 1;

   // Now add in the CR/LF pairs, each line is truncated at 72 characters

   // 2000-05-12
   // Thanks go to Ilia Golubev (ilia@varicom.co.il) for finding a bug here.
   // I was using number_of_bytes_to_encode rather than number_of_bytes_encoded.

   number_of_bytes_encoded += (DWORD)( ( ( number_of_bytes_encoded / BASE64_NUMBER_OF_CHARACTERS_PER_LINE ) + 1 ) * 2 );

   LPTSTR destination = destination_string.GetBuffer( number_of_bytes_encoded );

   number_of_bytes_encoded = 0;

   while( loop_index < number_of_bytes_to_encode )
   {
      // Output the first byte

      byte_1 = input_buffer[ loop_index ];
      byte_to_add = alphabet[ ( byte_1 >> 2 ) ];

      destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( byte_to_add );
      number_of_bytes_encoded++;

      loop_index++;

      if ( loop_index >= number_of_bytes_to_encode )
      {
         // We're at the end of the data to encode

         byte_2 = 0;
         byte_to_add = alphabet[ ( ( ( byte_1 & 0x03 ) << 4 ) | ( ( byte_2 & 0xF0 ) >> 4 ) ) ];

         destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( byte_to_add );
         number_of_bytes_encoded++;

         destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( END_OF_BASE64_ENCODED_DATA );
         number_of_bytes_encoded++;

         destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( END_OF_BASE64_ENCODED_DATA );

         // 1999-09-01
         // Thanks go to Yurong Lin (ylin@dial.pipex.com) for finding a bug here.
         // We must NULL terminate the string before letting CString have the buffer back.

         destination[ number_of_bytes_encoded + 1 ] = 0;

         destination_string.ReleaseBuffer( -1 );

         return( TRUE );
      }
      else
      {
         byte_2 = input_buffer[ loop_index ];
      }

      byte_to_add = alphabet[ ( ( ( byte_1 & 0x03 ) << 4 ) | ( ( byte_2 & 0xF0 ) >> 4 ) ) ];

      destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( byte_to_add );
      number_of_bytes_encoded++;

      loop_index++;

      if ( loop_index >= number_of_bytes_to_encode )
      {
         // We ran out of bytes, we need to add the last half of byte_2 and pad
         byte_3 = 0;

         byte_to_add = alphabet[ ( ( ( byte_2 & 0x0F ) << 2 ) | ( ( byte_3 & 0xC0 ) >> 6 ) ) ];

         destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( byte_to_add );
         number_of_bytes_encoded++;

         destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( END_OF_BASE64_ENCODED_DATA );

         // 1999-09-01
         // Thanks go to Yurong Lin (ylin@dial.pipex.com) for finding a bug here.
         // We must NULL terminate the string before letting CString have the buffer back.

         destination[ number_of_bytes_encoded + 1 ] = 0;

         destination_string.ReleaseBuffer( -1 );

         return( TRUE );
      }
      else
      {
         byte_3 = input_buffer[ loop_index ];
      }

      loop_index++;

      byte_to_add = alphabet[ ( ( ( byte_2 & 0x0F ) << 2 ) | ( ( byte_3 & 0xC0 ) >> 6 ) ) ];

      destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( byte_to_add );
      number_of_bytes_encoded++;

      byte_to_add = alphabet[ ( byte_3 & 0x3F ) ];

      destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( byte_to_add );
      number_of_bytes_encoded++;

      if ( ( number_of_bytes_encoded % BASE64_NUMBER_OF_CHARACTERS_PER_LINE ) == 0 )
      {
         destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( CARRIAGE_RETURN );
         number_of_bytes_encoded++;

         destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( LINE_FEED );
         number_of_bytes_encoded++;
      }
   }

   destination[ number_of_bytes_encoded ] = static_cast< TCHAR >( END_OF_BASE64_ENCODED_DATA );

   // 1999-09-01
   // Thanks go to Yurong Lin (ylin@dial.pipex.com) for finding a bug here.
   // We must NULL terminate the string before letting CString have the buffer back.

   destination[ number_of_bytes_encoded + 1 ] = 0;

   destination_string.ReleaseBuffer( -1 );

   return( TRUE );
}

// End of source

#if 0
<HTML>

<HEAD>
<TITLE>WFC - CBase64Coding</TITLE>
<META name="keywords" content="WFC, MFC extension library, freeware class library, Win32, MIME encoding, base 64, source code">
<META name="description" content="This C++ class let's you MIME encode bytes to text using base64.">
</HEAD>

<BODY>

<H1>CBase64Coding</H1>

$Revision: 14 $<BR><HR>

<H2>Description</H2>

This class gives you the ability to encode/decode data using base64.

<H2>Constructors</H2>

<DL COMPACT>

<DT><PRE><B>CBase64Coding</B>()<DD>
Constructs this object.

</DL>

<H2>Methods</H2>

<DL COMPACT>

<DT><PRE>BOOL <B><A NAME="Decode">Decode</A></B>( const CByteArray&amp; source, CByteArray&amp; destination )
BOOL <B>Decode</B>( const CString&amp;    source, CByteArray&amp; destination )</PRE><DD>
This method takes base64 encoded text and produces the bytes. It decodes
the base64 encoding.

<DT><PRE>BOOL <B><A NAME="Encode">Encode</A></B>( const CByteArray&amp; source, CByteArray&amp; destination )
BOOL <B>Encode</B>( const CByteArray&amp; source, CString&amp;    destination )</PRE><DD>
This method takes bytes and turns them into base64 text.

</DL>

<H2>Example</H2>
<PRE><CODE>#include &lt;wfc.h&gt;

int _tmain( int number_of_command_line_arguments, LPCTSTR command_line_arguments[] )
{
   <A HREF="WfcTrace.htm">WFCTRACEINIT</A>( TEXT( &quot;_tmain()&quot; ) );

   CByteArray bytes;

   get_file_contents( command_line_arguments[ 0 ], bytes );

   <B>CBase64Coding</B> encoder;

   CString encoded_data;

   if ( encoder.Encode( bytes, encoded_data ) != FALSE )
   {
      _tprintf( TEXT( &quot;%s\n&quot;, (LPCTSTR) encoded_data );
   }
}</CODE></PRE>
<HR><I>Copyright, 2000, <A HREF="mailto:wfc@pobox.com">Samuel R. Blackburn</A></I><BR>
$Workfile: CBase64Coding.cpp $<BR>
$Modtime: 5/12/00 3:39p $
</BODY>

</HTML>
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -