1 // Base64.cpp: implementation of the CBase64 class.
3 //////////////////////////////////////////////////////////////////////
10 static char THIS_FILE
[]=__FILE__
;
14 //////////////////////////////////////////////////////////////////////
16 //////////////////////////////////////////////////////////////////////
17 // Static Member Initializers
20 // The 7-bit alphabet used to encode binary information
21 const char m_sBase64Alphabet
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23 const int f_nMask
[] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
37 CStringA
CBase64::Encode( IN
const char* szEncoding
, IN
int nSize
)
39 CStringA sOutput
= "";
44 ASSERT( szEncoding
!= NULL
);
45 if( szEncoding
== NULL
)
47 m_szInput
= szEncoding
;
51 nDigit
= read_bits( nNumBits
, &nNumBits
, lp
);
54 sOutput
+= m_sBase64Alphabet
[ (int)nDigit
];
55 nDigit
= read_bits( nNumBits
, &nNumBits
, lp
);
57 // Pad with '=' as per RFC 1521
58 while( sOutput
.GetLength() % 4 != 0 )
65 // The size of the output buffer must not be less than
66 // 3/4 the size of the input buffer. For simplicity,
67 // make them the same size.
68 // return : ½âÂëºóÊý¾Ý³¤¶È
69 int CBase64::Decode ( IN
const char* szDecoding
, char* szOutput
)
76 ASSERT( szDecoding
!= NULL
);
77 ASSERT( szOutput
!= NULL
);
78 if( szOutput
== NULL
)
80 if( szDecoding
== NULL
)
83 if( sInput
.GetLength() == 0 )
89 for( i
= 0; i
< 256; i
++ )
90 nDecode
[i
] = -2; // Illegal digit
91 for( i
=0; i
< 64; i
++ )
93 nDecode
[ m_sBase64Alphabet
[ i
] ] = i
;
94 nDecode
[ m_sBase64Alphabet
[ i
] | 0x80 ] = i
; // Ignore 8th bit
96 nDecode
[ '=' | 0x80 ] = -1; // Ignore MIME padding char
99 // Clear the output buffer
100 memset( szOutput
, 0, sInput
.GetLength() + 1 );
104 for( lp
= 0, i
= 0; lp
< sInput
.GetLength(); lp
++ )
107 nDigit
= nDecode
[ c
& 0x7F ];
112 else if( nDigit
>= 0 )
113 // i (index into output) is incremented by write_bits()
114 write_bits( nDigit
& 0x3F, 6, szOutput
, i
);
119 UINT
CBase64::read_bits(int nNumBits
, int * pBitsRead
, int& lp
)
122 while( ( m_nBitsRemaining
< nNumBits
) &&
123 ( lp
< m_nInputSize
) )
125 int c
= m_szInput
[ lp
++ ];
127 m_lBitStorage
|= (c
& 0xff);
128 m_nBitsRemaining
+= 8;
130 if( m_nBitsRemaining
< nNumBits
)
132 lScratch
= m_lBitStorage
<< ( nNumBits
- m_nBitsRemaining
);
133 *pBitsRead
= m_nBitsRemaining
;
134 m_nBitsRemaining
= 0;
138 lScratch
= m_lBitStorage
>> ( m_nBitsRemaining
- nNumBits
);
139 *pBitsRead
= nNumBits
;
140 m_nBitsRemaining
-= nNumBits
;
142 return (UINT
)lScratch
& f_nMask
[nNumBits
];
146 void CBase64::write_bits(UINT nBits
,
153 m_lBitStorage
= (m_lBitStorage
<< nNumBits
) | nBits
;
154 m_nBitsRemaining
+= nNumBits
;
155 while( m_nBitsRemaining
> 7 )
157 nScratch
= m_lBitStorage
>> (m_nBitsRemaining
- 8);
158 szOutput
[ i
++ ] = nScratch
& 0xFF;
159 m_nBitsRemaining
-= 8;