base64解码函数

int base64_decode(const void *src, size_t src_size,
                         size_t *src_pos_r, buffer_t *dest)
{
    const  char *src_c = src;
    size_t src_pos;
    unsigned char input[4], output[3];
    int ret = 1;
    char * word = "=";
    for (src_pos = 0; src_pos+3 < src_size; ) { 
        input[0] = b64dec[src_c[src_pos]];
        if (input[0] == 0xff) {
            if (unlikely(!IS_EMPTY(src_c[src_pos]))) {
                ret = -1; 
                while(input[0] == 0xff){
                    src_pos++;
                    input[0] = b64dec[src_c[src_pos]];
                }   
            }else{
                src_pos++;
                continue;
            }   
        }   


        input[1] = b64dec[src_c[src_pos+1]];
        while (unlikely(input[1] == 0xff)  ) { 
            if (memcmp(&src_c[src_pos+1], word, 1)){
                ret = -1; 
                src_pos++;
                input[1] = b64dec[src_c[src_pos+1]];
            }   
            else
            {   
                goto out;
            }   
        }   
        output[0] = (input[0] << 2) | (input[1] >> 4); 


        input[2] = b64dec[src_c[src_pos+2]];
        while (input[2] == 0xff  ) {
            if ( memcmp(&src_c[src_pos+2] ,word, 1) && memcmp(&src_c[src_pos+3], word, 1)) {
                ret = -1;
                src_pos++;
                input[2] = b64dec[src_c[src_pos+2]];
                continue;
            }
            buffer_append(dest, output, 1);
            ret = 0;
            src_pos += 4;
            goto out;
        }


        output[1] = (input[1] << 4) | (input[2] >> 2);
        input[3] = b64dec[src_c[src_pos+3]];
        while (input[3] == 0xff  ) {
            if (memcmp(&src_c[src_pos+3],word, 1)) {
                ret = -1;
                src_pos++;
                input[3] = b64dec[src_c[src_pos+3]];
                continue;
            }
            buffer_append(dest, output, 2);
            ret = 0;
            src_pos += 4;
            goto out;
        }


        output[2] = ((input[2] << 6) & 0xc0) | input[3];
        buffer_append(dest, output, 3);
        src_pos += 4;
    }
out:
    for (; src_pos < src_size; src_pos++) {
        if (!IS_EMPTY(src_c[src_pos]))
            break;
    }


    if (src_pos_r != NULL)
        *src_pos_r = src_pos;


    return ret;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值