posts - 13, comments - 0, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Base64编解码函数

Posted on 2010-10-29 10:37 午夜凉饭 阅读(512) 评论(0)  编辑 收藏 引用 所属分类: Linux
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
//decode
const char Base64IdxTab[128] =
{

    255,255,255,255,  255,255,255,255,  255,255,255,255,  255,255,255,255,

    255,255,255,255,  255,255,255,255,  255,255,255,255,  255,255,255,255,

    255,255,255,255,  255,255,255,255,  255,255,255,62,   255,255,255,63,

    52,53,54,55,      56,57,58,59,      60,61,255,255,    255,255,255,255,

    255,0,1,2,        3,4,5,6,          7,8,9,10,         11,12,13,14,

    15,16,17,18,      19,20,21,22,      23,24,25,255,     255,255,255,255,

    255,26,27,28,     29,30,31,32,      33,34,35,36,      37,38,39,40,

    41,42,43,44,      45,46,47,48,      49,50,51,255,     255,255,255,255
};
#define BVal(x) Base64IdxTab[x]
int DecodeBase64(char * pInput, char * pOutput)
{

        int i = 0;

        int iCnt = 0;

        int iSrcLen = (int)strlen(pInput);

   

        char * p = pOutput;

   

        for (i=0; i < iSrcLen; i++)

        {

            if (pInput[i] > 127) continue;

            if (pInput[i] == '=') return p-pOutput+1;

   

            char a = BVal(pInput[i]);

            if (a == 255) continue;

           

            switch (iCnt)

            {

            case 0:

                {

                    *p = a << 2;

                    iCnt++;

                }

                break;

   

            case 1:

                {

                    *p++ |= a >> 4;

                    *p = a << 4;

                    iCnt++;

                }

                break;

   

            case 2:

                {

                    *p++ |= a >> 2;

                    *p = a << 6;

                    iCnt++;

                }

                break;

            case 3:

                {

                    *p++ |= a;

                    iCnt = 0;

                }

                break;

            }

        }

   

        *p = 0x00;

        return p-pOutput;
}
//encode
const char Base64ValTab[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define AVal(x) Base64ValTab[x]
int EncodeBase64(char * pInput, char * pOutput)
{
    int i = 0;
    int loop = 0;
    int remain = 0;
    int iDstLen = 0;
    int iSrcLen = (int)strlen(pInput);

    loop = iSrcLen/3;
    remain = iSrcLen%3;

    // also can encode native char one by one as decode method
    // but because all of char in native string  is to be encoded so encode 3-chars one time is easier.

    for (i=0; i < loop; i++)
    {
        char a1 = (pInput[i*3] >> 2);
        char a2 = ( ((pInput[i*3] & 0x03) << 4) | (pInput[i*3+1] >> 4) );
        char a3 = ( ((pInput[i*3+1] & 0x0F) << 2) | ((pInput[i*3+2] & 0xC0) >> 6) );
        char a4 = (pInput[i*3+2] & 0x3F);

        pOutput[i*4] = AVal(a1);
        pOutput[i*4+1] = AVal(a2);
        pOutput[i*4+2] = AVal(a3);
        pOutput[i*4+3] = AVal(a4);
    }

    iDstLen = i*4;

    if (remain == 1)
    {
        // should pad two equal sign
        i = iSrcLen-1;
        char a1 = (pInput[i] >> 2);
        char a2 = ((pInput[i] & 0x03) << 4);
       
        pOutput[iDstLen++] = AVal(a1);
        pOutput[iDstLen++] = AVal(a2);
        pOutput[iDstLen++] = '=';
        pOutput[iDstLen++] = '=';
        pOutput[iDstLen] = 0x00;
    }
    else if (remain == 2)
    {
        // should pad one equal sign
        i = iSrcLen-2;
        char a1 = (pInput[i] >> 2);
        char a2 = ( ((pInput[i] & 0x03) << 4) | (pInput[i+1] >> 4));
        char a3 = ( (pInput[i+1] & 0x0F) << 2);

        pOutput[iDstLen++] = AVal(a1);
        pOutput[iDstLen++] = AVal(a2);
        pOutput[iDstLen++] = AVal(a3);
        pOutput[iDstLen++] = '=';
        pOutput[iDstLen] = 0x00;
    }
    else
    {
        // just division by 3
        pOutput[iDstLen] = 0x00;
    }

    return iDstLen;
}
     

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理