高分求教编码问题!

weasea 2003-06-13 10:25:16
文件编码:
int CSMTPAttachment::Base64BufferSize(int nInputSize)
{
int nOutSize = (nInputSize+2)/3*4; // 3:4 conversion ratio
nOutSize += strlen(EOL)*nOutSize/BASE64_MAXLINE + 3; // Space for newlines and NUL
return nOutSize;
}

BOOL CSMTPAttachment::EncodeBase64(const char* pszIn, int nInLen, char* pszOut, int nOutSize, int* nOutLen)
{
//Input Parameter validation
ASSERT(pszIn);
ASSERT(pszOut);
ASSERT(nOutSize);
ASSERT(nOutSize >= Base64BufferSize(nInLen));

#ifndef _DEBUG
//justs get rid of "unreferenced formal parameter"
//compiler warning when doing a release build
nOutSize;
#endif

//Set up the parameters prior to the main encoding loop
int nInPos = 0;
int nOutPos = 0;
int nLineLen = 0;

// Get three characters at a time from the input buffer and encode them
for (int i=0; i<nInLen/3; ++i)
{
//Get the next 2 characters
int c1 = pszIn[nInPos++] & 0xFF;
int c2 = pszIn[nInPos++] & 0xFF;
int c3 = pszIn[nInPos++] & 0xFF;

//Encode into the 4 6 bit characters
pszOut[nOutPos++] = m_base64tab[(c1 & 0xFC) >> 2];
pszOut[nOutPos++] = m_base64tab[((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4)];
pszOut[nOutPos++] = m_base64tab[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)];
pszOut[nOutPos++] = m_base64tab[c3 & 0x3F];
nLineLen += 4;

//Handle the case where we have gone over the max line boundary
if (nLineLen >= BASE64_MAXLINE-3)
{
char* cp = EOL;
pszOut[nOutPos++] = *cp++;
if (*cp)
pszOut[nOutPos++] = *cp;
nLineLen = 0;
}
}

// Encode the remaining one or two characters in the input buffer
char* cp;
switch (nInLen % 3)
{
case 0:
{
cp = EOL;
pszOut[nOutPos++] = *cp++;
if (*cp)
pszOut[nOutPos++] = *cp;
break;
}
case 1:
{
int c1 = pszIn[nInPos] & 0xFF;
pszOut[nOutPos++] = m_base64tab[(c1 & 0xFC) >> 2];
pszOut[nOutPos++] = m_base64tab[((c1 & 0x03) << 4)];
pszOut[nOutPos++] = '=';
pszOut[nOutPos++] = '=';
cp = EOL;
pszOut[nOutPos++] = *cp++;
if (*cp)
pszOut[nOutPos++] = *cp;
break;
}
case 2:
{
int c1 = pszIn[nInPos++] & 0xFF;
int c2 = pszIn[nInPos] & 0xFF;
pszOut[nOutPos++] = m_base64tab[(c1 & 0xFC) >> 2];
pszOut[nOutPos++] = m_base64tab[((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4)];
pszOut[nOutPos++] = m_base64tab[((c2 & 0x0F) << 2)];
pszOut[nOutPos++] = '=';
cp = EOL;
pszOut[nOutPos++] = *cp++;
if (*cp)
pszOut[nOutPos++] = *cp;
break;
}
default:
{
ASSERT(FALSE);
break;
}
}
pszOut[nOutPos] = 0;
*nOutLen = nOutPos;
return TRUE;
}

这个是Module : SMTP.CPP
Purpose: Implementation for a MFC class encapsulation of the SMTP protocol
Created: PJN / 22-05-1998

不大理解哦
哪儿有算法啊!!!

...全文
53 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
akiy 2003-06-13
  • 打赏
  • 举报
回复
同意楼上
如果实在找不到,你再找我,说不定我还留着
alongzju 2003-06-13
  • 打赏
  • 举报
回复
base64算法很简单的,google上面随便搜索一下都有。
Linux2001 2003-06-13
  • 打赏
  • 举报
回复
同意楼上的
Earthdog 2003-06-13
  • 打赏
  • 举报
回复
int nOutSize = (nInputSize+2)/3*4;

Base64的编码结果就是每3个字节编码成4个字节,最后面不足三个的同样也要编码成4个字节

所以如果nInputSize不是3的倍数的话,除以3再乘以4,得出来的长度无法包括最后一个字节或两个字节的编码串长度,所以加了一个2
weasea 2003-06-13
  • 打赏
  • 举报
回复
例子的偶不要
刚才问题想通了我!!

leojeff 2003-06-13
  • 打赏
  • 举报
回复
有这里有个VB的例子,你看一下吧,其实算法是一样的。


Private Const base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

Public Function Base64_Encode(strSource As String) As String
Const BASE64_TABLE As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim strTempLine As String
Dim j As Integer
For j = 1 To (Len(strSource) - Len(strSource) Mod 3) Step 3
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) \ 4) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, ((Asc(Mid(strSource, j, 1)) Mod 4) * 16 _
+ Asc(Mid(strSource, j + 1, 1)) \ 16) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, ((Asc(Mid(strSource, j + 1, 1)) Mod 16) * 4 _
+ Asc(Mid(strSource, j + 2, 1)) \ 64) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j + 2, 1)) Mod 64) + 1, 1)
Next j
If Not (Len(strSource) Mod 3) = 0 Then
If (Len(strSource) Mod 3) = 2 Then
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) \ 4) + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) Mod 4) * 16 _
+ Asc(Mid(strSource, j + 1, 1)) \ 16 + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j + 1, 1)) Mod 16) * 4 + 1, 1)
strTempLine = strTempLine & "="
ElseIf (Len(strSource) Mod 3) = 1 Then
strTempLine = strTempLine + Mid(BASE64_TABLE, Asc(Mid(strSource, j, 1)) \ 4 + 1, 1)
strTempLine = strTempLine + Mid(BASE64_TABLE, (Asc(Mid(strSource, j, 1)) Mod 4) * 16 + 1, 1)
strTempLine = strTempLine & "=="
End If
End If
Base64_Encode = strTempLine
End Function

Public Function Base64_Decode(strSource As String) As String
Dim w1 As Integer
Dim w2 As Integer
Dim w3 As Integer
Dim w4 As Integer
Dim n As Integer
Dim retry As String

For n = 1 To Len(strSource) Step 4
w1 = mimedecode(Mid$(strSource, n, 1))
w2 = mimedecode(Mid$(strSource, n + 1, 1))
w3 = mimedecode(Mid$(strSource, n + 2, 1))
w4 = mimedecode(Mid$(strSource, n + 3, 1))
If w2 >= 0 Then retry = retry + Chr$(((w1 * 4 + Int(w2 / 16)) And 255))
If w3 >= 0 Then retry = retry + Chr$(((w2 * 16 + Int(w3 / 4)) And 255))
If w4 >= 0 Then retry = retry + Chr$(((w3 * 64 + w4) And 255))
Next
Base64_Decode = retry
End Function

Private Function mimedecode(strSource As String) As Integer
If Len(strSource) = 0 Then mimedecode = -1: Exit Function
mimedecode = InStr(base64, strSource) - 1
End Function
weasea 2003-06-13
  • 打赏
  • 举报
回复
thx!
正在想!
lsl7909 2003-06-13
  • 打赏
  • 举报
回复
UP,帮你顶一下!
weasea 2003-06-13
  • 打赏
  • 举报
回复
分不能这么给
:)
int nOutSize = (nInputSize+2)/3*4; // 3:4 conversion ratio
nOutSize += strlen(EOL)*nOutSize/BASE64_MAXLINE + 3;
// Space for newlines and NUL
return nOutSize;

int nOutSize = (nInputSize+2)/3*4;
这句话不大理解

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧