📄 util_base64coder.cpp
字号:
if (m_DecoderTable[character_4] == BASE64_UNKNOWN_VALUE)
{
destination.RemoveAll();
return(FALSE);
}
}
if (character_2 == BASE64_END_OF_BUFFER ||
character_3 == BASE64_END_OF_BUFFER ||
character_4 == BASE64_END_OF_BUFFER)
{
destination.RemoveAll();
return(FALSE);
}
}
else
{
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 UTIL_Base64Coder::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 UTIL_Base64Coder::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 >(13);
number_of_bytes_encoded++;
destination[number_of_bytes_encoded] = static_cast< TCHAR >(10);
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);
}
bool UTIL_Base64Coder::Encode(const char *source, int len, CString& destination)
{
if (len < 0) return false;
else if (len == 0) { destination = ""; return true; }
CByteArray src_array;
src_array.SetSize(len);
void *src_buffer = src_array.GetData();
if (src_buffer == NULL) return false;
memcpy(src_buffer, source, len);
return Encode(src_array, destination);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -