base64算法实现

来源:互联网 发布:嵌入式软件开发环境 编辑:程序博客网 时间:2024/05/19 06:48

int
base64_encode(unsigned char *out, unsigned char *in, int size)
{
    register int position = 0;
    register unsigned char *dst = out, *src = in;

    in += size;
    while (src < in)
    {
        switch (position)
        {
            case 0:
                *dst = src[0] >> 2;
                ++position;
                break;
            case 1:
                *dst = ((src[0] & 3) << 4) | (src[1] >> 4);
                ++src;
                ++position;
                break;
            case 2:
                *dst = ((src[0] & 0xf) << 2) | (src[1] >> 6);
                ++src;
                ++position;
                break;
            case 3:
                *dst = src[0] & 0x3f;
                ++src;
                position = 0;
                break;
        }
        ++dst;
    }

    for (src = out; src < dst; ++src)
    {
        *src = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[*src];
    }

    if (position) for (; position++ < 4; *dst++ = '=');
    *dst = '/0';

    return dst - out;
}

static unsigned char map[256] =
{
    0x40, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
    0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
    0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
    0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

int
base64_decode(unsigned char *out, unsigned char *in)
{
    register int bytes;
    register unsigned char ch, *dst = out;

    for (bytes = 0; (ch = *in++) && ch != '='; ++bytes)
    {
        if ((ch = map[ch]) == 0xff)
        {
            return -1;
        }
        switch (bytes & 3)
        {
            case 0:
                *dst = ch << 2;
                break;
            case 1:
                *dst++ |= (ch >> 4);
                *dst = (ch & 0xf) << 4;
                break;
                *dst = (ch & 0xf) << 4;
                break;
            case 2:
                *dst++ |= (ch >> 2);
                *dst = ch << 6;
                break;
            case 3:
                *dst++ |= ch;
                break;
        }
    }
    if (bytes & 3 == 1)
    {
        return -1;
    }

    return (bytes >> 2) * 3 + "/x0/x1/x1/x2"[bytes & 3];
}