⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 QP(Quoted-Printable-Content-Transfer-Encoding)编码解码程序
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
// unit : MIME-QP Encode/Decode
// function : MIME-QP Encode/Decode
// author : Song Yujiao     syj720@163.com
// date : 2008-06-13
//Quoted-Printable编码的基本方法是:输入数据在33-60、62-126范围内的,直接输出;
//其它的需编码为"="加两个字节的HEX码(大写)。为保证输出行不超过规定长度,可在行尾加"=\r\n"序列作为软回车。
//---------------------------------------------------------------------------
int MyStrToHex(char c);
int DecodeQuoted(const char* pSrc, unsigned char*&pDst, int nSrcLen);
void   uqp(unsigned   char*   uDst, unsigned   char   first, unsigned   char   second);
int EncodeQuoted(const unsigned char* pSrc, char* pDst, int nSrcLen, int nMaxLineLen)
{
        //char   buf[100];
        //pDst=buf;
        //memset(pDst,0,100);

        int nDstLen;
        int nLineLen;
        //char *dst =pDst ;
        int i ;


        nDstLen = 0;
        nLineLen = 0;
        //nMaxLineLen=76;
        for (i = 0; i < nSrcLen; i++, pSrc++)
        {
                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;
}

int DecodeQuoted(const char* pSrc, unsigned char*&pDst, int nSrcLen)
{

//    char *pTemp="你好";
//    int iLen = strlen(pTemp);
//    unsigned int uiTemp=0;
//    char ctemp ;
//    for(int i=0;i<iLen;i++)
//    {
//        uiTemp = (unsigned int)(pTemp[i]&0x000000ff);
//        ctemp = (char)uiTemp;
//    }
        int nDstLen;
        int i;
        i = 0;
        nDstLen = 0;
        nDstLen = nSrcLen;
        unsigned char* pcRe=NULL;
        if(nDstLen==0)
        {
            return 0;
        }
        else
        pcRe = new char[nDstLen];
        memset(pcRe,'\0',nDstLen);
        pDst=pcRe;
        unsigned char cTemp[3]={'\0','\0','\0'};
        while (i < nSrcLen)
        {
                //nDstLen = 0;
                if (strncmp(pSrc, "=\r\n", 3) == 0)
                {
                        pSrc += 3;
                        i += 3;
                }
                else
                {
                        if (*pSrc == '=')
                        {
                                memcpy(cTemp,pSrc+1,2);
                                memset(cTemp+2,'\0',1);
                                //sscanf(pSrc, "=%02X", pDst);
                               // pDst++;
                                pSrc += 3;
                                i += 3;
                        //}
//                        else
//                        {
//                                *pcRe++ = *pSrc++;
//                                //*pDst = (unsigned char)*pSrc++;
//                                i++;
//                        }
//                        uDst = *pDst;
//                        first = pDst[1];
//                        second = pDst[2];
                        //uqp(uDst,first,second);
                        uqp(pcRe,cTemp[0],cTemp[1]);
                        pcRe++;
                        pDst;
                        //pDst++;
                        //nDstLen++;
                        }
                        else
                        {
                                *pcRe++ = *pSrc++;
                                //*pDst = (unsigned char)*pSrc++;
                                i++;
                        }
                }
                *pcRe = '\r\n';
                //memset(pcRe,'\0',nDstLen-i+1);
        }
        //*pcRe = '\0';
        return nDstLen;
}

void   uqp(unsigned  char* uDst, unsigned char first, unsigned char second)
                        /* sour:解码后的字符 ;first:qp码的第一个字符 ;second:qp码的第二个字符; sour为返回值 */
{
       unsigned int uiFist=MyStrToHex(first)<<4;
       unsigned int uiSecond=MyStrToHex(second);
       unsigned char ucTemp=(uiFist+uiSecond)&0x000000ff;
        //uDst = &pDst;
        //unsigned char *upDst;
        //upDst = NULL;
        //upDst = new char[6];
//        if(first>=65)   first-=55;
//        else   first-=48;
//        if(second>=65)   second-=55;
//        else   second-=48;
        *uDst=((unsigned char)ucTemp);
        //*uDst=ucFist<<4;
        //*uDst+=ucSecond;
        //*upDst = uDst;
}

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        AnsiString in_str =Edit1->Text ;
        char *pOut=NULL;
        int aLen = strlen(in_str.c_str());
        Edit2->Text = EncodeQuoted(in_str.c_str(), pOut, aLen, 76);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
        AnsiString in_str =Memo1->Text ;
        AnsiString strPath = Edit3->Text;
        unsigned char*pOut=NULL;
        int alen = strlen(in_str.c_str());
        //int nLen = alen/3;
        //pOut = new char[nLen+1];
        int iLen = DecodeQuoted(in_str.c_str(),pOut, alen);
        DeleteFile(strPath);
        int iFile = FileCreate(strPath);
        if(iFile>0)
        {
                FileWrite(iFile,pOut,iLen);
                FileClose(iFile);
        }
}
//---------------------------------------------------------------------------
int MyStrToHex(char c)
{
    if(c>='0'&&c<='9')
    {
        return c-0x30;
    }
    else if(c>='a'&&c<='f')
    {
        return c-0x57;
    }
    else if(c>='A'&&c<='F')
    {
        return c-0x37;
    }
    return 0;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -