Base64.cpp
上传用户:xztxsm
上传日期:2007-02-12
资源大小:150k
文件大小:3k
源码类别:

远程控制编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "Base64.h"
  3. #ifdef _DEBUG
  4. #undef THIS_FILE
  5. static char THIS_FILE[]=__FILE__;
  6. #define new DEBUG_NEW
  7. #endif
  8. CString CBase64::m_sBase64Alphabet = 
  9. _T( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" );
  10. int CBase64::m_nMask[] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
  11. CBase64::CBase64()
  12. {
  13. }
  14. CBase64::~CBase64()
  15. {
  16. }
  17. CString CBase64::Encode(LPCTSTR szEncoding, int nSize)
  18. {
  19. CString sOutput = _T( "" );
  20. int nNumBits = 6;
  21. UINT nDigit;
  22. int lp = 0;
  23. ASSERT( szEncoding != NULL );
  24. if( szEncoding == NULL )
  25. return sOutput;
  26. m_szInput = szEncoding;
  27. m_nInputSize = nSize;
  28. m_nBitsRemaining = 0;
  29. nDigit = read_bits( nNumBits, &nNumBits, lp );
  30. while( nNumBits > 0 )
  31. {
  32. sOutput += m_sBase64Alphabet[ (int)nDigit ];
  33. nDigit = read_bits( nNumBits, &nNumBits, lp );
  34. }
  35. // Pad with '=' as per RFC 1521
  36. while( sOutput.GetLength() % 4 != 0 )
  37. {
  38. sOutput += '=';
  39. }
  40. return sOutput;
  41. }
  42. // The size of the output buffer must not be less than
  43. // 3/4 the size of the input buffer. For simplicity,
  44. // make them the same size.
  45. int CBase64::Decode(LPCTSTR szDecoding, LPTSTR szOutput)
  46. {
  47. CString sInput;
  48.     int c, lp =0;
  49. int nDigit;
  50.     int nDecode[ 256 ];
  51. ASSERT( szDecoding != NULL );
  52. ASSERT( szOutput != NULL );
  53. if( szOutput == NULL )
  54. return 0;
  55. if( szDecoding == NULL )
  56. return 0;
  57. sInput = szDecoding;
  58. if( sInput.GetLength() == 0 )
  59. return 0;
  60. // Build Decode Table
  61. //
  62. for( int i = 0; i < 256; i++ ) 
  63. nDecode[i] = -2; // Illegal digit
  64. for( i=0; i < 64; i++ )
  65. {
  66. nDecode[ m_sBase64Alphabet[ i ] ] = i;
  67. nDecode[ m_sBase64Alphabet[ i ] | 0x80 ] = i; // Ignore 8th bit
  68. nDecode[ '=' ] = -1; 
  69. nDecode[ '=' | 0x80 ] = -1; // Ignore MIME padding char
  70.     }
  71. // Clear the output buffer
  72. memset( szOutput, 0, sInput.GetLength() + 1 );
  73. // Decode the Input
  74. //
  75. for( lp = 0, i = 0; lp < sInput.GetLength(); lp++ )
  76. {
  77. c = sInput[ lp ];
  78. nDigit = nDecode[ c & 0x7F ];
  79. if( nDigit < -1 ) 
  80. {
  81. return 0;
  82. else if( nDigit >= 0 ) 
  83. // i (index into output) is incremented by write_bits()
  84. write_bits( nDigit & 0x3F, 6, szOutput, i );
  85.     }
  86. return i;
  87. }
  88. UINT CBase64::read_bits(int nNumBits, int * pBitsRead, int& lp)
  89. {
  90.     ULONG lScratch;
  91.     while( ( m_nBitsRemaining < nNumBits ) && 
  92.    ( lp < m_nInputSize ) ) 
  93. {
  94. int c = m_szInput[ lp++ ];
  95.         m_lBitStorage <<= 8;
  96.         m_lBitStorage |= (c & 0xff);
  97. m_nBitsRemaining += 8;
  98.     }
  99.     if( m_nBitsRemaining < nNumBits ) 
  100. {
  101. lScratch = m_lBitStorage << ( nNumBits - m_nBitsRemaining );
  102. *pBitsRead = m_nBitsRemaining;
  103. m_nBitsRemaining = 0;
  104.     } 
  105. else 
  106. {
  107. lScratch = m_lBitStorage >> ( m_nBitsRemaining - nNumBits );
  108. *pBitsRead = nNumBits;
  109. m_nBitsRemaining -= nNumBits;
  110.     }
  111.     return (UINT)lScratch & m_nMask[nNumBits];
  112. }
  113. void CBase64::write_bits(UINT nBits,
  114.  int nNumBits,
  115.  LPTSTR szOutput,
  116.  int& i)
  117. {
  118. UINT nScratch;
  119. m_lBitStorage = (m_lBitStorage << nNumBits) | nBits;
  120. m_nBitsRemaining += nNumBits;
  121. while( m_nBitsRemaining > 7 ) 
  122. {
  123. nScratch = m_lBitStorage >> (m_nBitsRemaining - 8);
  124. szOutput[ i++ ] = nScratch & 0xFF;
  125. m_nBitsRemaining -= 8;
  126. }
  127. }