MIME之Quoted-Printable编解码 选择自 bhw98 的 Blog

Quoted-Printable也是MIME邮件中常用的编码方式之一。同Base64一样,它也将输入的字符串或数据编码成全是ASCII码的可打印字符串。

Quoted-Printable编码的基本方法是:输入数据在33-60、62-126范围内的,直接输出;其它的需编码为“=”加两个字节的HEX码(大写)。为保证输出行不超过规定长度,可在行尾加“=/r/n”序列作为软回车。

Quoted-Printable也是MIME邮件中常用的编码方式之一。同Base64一样,它也将输入的字符串或数据编码成全是ASCII码的可打印字符串。

Quoted-Printable编码的基本方法是:输入数据在33-60、62-126范围内的,直接输出;其它的需编码为“=”加两个字节的HEX码(大写)。为保证输出行不超过规定长度,可在行尾加“=/r/n”序列作为软回车。

int EncodeQuoted(const unsigned char* pSrc, char* pDst, int nSrcLen, int nMaxLineLen)
{
int nDstLen; // 输出的字符计数
int nLineLen; // 输出的行长度计数

nDstLen = 0;
nLineLen = 0;

for (int i = 0; i < nSrcLen; i++, pSrc++)
{
// ASCII 33-60, 62-126原样输出,其余的需编码
if ((*pSrc >= '!') && (*pSrc <= '~') && (*pSrc != '='))
{
*pDst++ = (char)*pSrc;
nDstLen++;
nLineLen++;
}
else
{
sprintf(pDst, "=%02X", *pSrc);
pDst += 3;
nDstLen += 3;
nLineLen += 3;
}

// 输出换行?
if (nLineLen >= nMaxLineLen - 3)
{
sprintf(pDst, "=/r/n");
pDst += 3;
nDstLen += 3;
nLineLen = 0;
}
}

// 输出加个结束符
*pDst = '/0';

return nDstLen;
}

Quoted-Printable解码很简单,将编码过程反过来就行了。

int DecodeQuoted(const char* pSrc, unsigned char* pDst, int nSrcLen)
{
int nDstLen; // 输出的字符计数
int i;

i = 0;
nDstLen = 0;

while (i < nSrcLen)
{
if (strncmp(pSrc, "=/r/n", 3) == 0) // 软回车,跳过
{
pSrc += 3;
i += 3;
}
else
{
if (*pSrc == '=') // 是编码字节
{
sscanf(pSrc, "=%02X", pDst);
pDst++;
pSrc += 3;
i += 3;
}
else // 非编码字节
{
*pDst++ = (unsigned char)*pSrc++;
i++;
}

nDstLen++;
}
}

// 输出加个结束符
*pDst = '/0';

return nDstLen;
}

 

[相关资源]

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值