我是靠谱客的博主 成就鞋垫,最近开发中收集的这篇文章主要介绍base64的编解码函数,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include<stdio.h>


//将0到64 转值成对应的字符
const char *BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


//3*8=4*6  将3个字节每6位拆分成4个字节,由于拆分后的每个字节只有6位,所以值为0到63,再将对应的值转成上面对应的字符就可以了
void _base64_encode_triple(unsigned char triple[3], char result[4])
{
    int tripleValue, i;


    tripleValue = triple[0];
    tripleValue *= 256;
    tripleValue += triple[1];
    tripleValue *= 256;
    tripleValue += triple[2];//3个字节的值大小


    for (i=0; i<4; i++)
    {
result[3-i] = BASE64_CHARS[tripleValue%64];//tripleValue%64 为取最后6位的值
tripleValue /= 64;//去掉最后6位的值
    }
}


/**
 * encode an array of bytes using Base64 (RFC 3548)
 *
 * @param source the source buffer
 * @param sourcelen the length of the source buffer
 * @param target the target buffer
 * @param targetlen the length of the target buffer
 * @return 1 on success, 0 otherwise
 */
int base64_encode(unsigned char *source, unsigned int sourcelen, char *target, unsigned int targetlen)
{
    /* check if the result will fit in the target buffer */
    if ((sourcelen+2)/3*4 > targetlen-1)
return 0;


    /* encode all full triples */
    while (sourcelen >= 3)
    {
_base64_encode_triple(source, target);//每次转3个字节
sourcelen -= 3;
source += 3;
target += 4;
    }


    /* encode the last one or two characters */
    if (sourcelen > 0)//剩下少于3个字节的最后一次做特殊处理
    {
unsigned char temp[3];
memset(temp, 0, sizeof(temp));
memcpy(temp, source, sourcelen);
_base64_encode_triple(temp, target);
target[3] = '=';
if (sourcelen == 1)
   target[2] = '=';


target += 4;
    }


    /* terminate the string */
    target[0] = 0;


    return 1;
}


/**
 * decode a base64 string and put the result in the same buffer as the source
 *
 * This function does not handle decoded data that contains the null byte
 * very well as the size of the decoded data is not returned.
 *
 * The result will be zero terminated.
 *
 * @deprecated use base64_decode instead
 *
 * @param str buffer for the source and the result
 */
void str_b64decode(char* str)
{
    size_t decoded_length;


    decoded_length = base64_decode(str, (unsigned char *)str, strlen(str));
    str[decoded_length] = '';
}


/**
 * decode base64 encoded data
 *
 * @param source the encoded data (zero terminated)
 * @param target pointer to the target buffer
 * @param targetlen length of the target buffer
 * @return length of converted data on success, -1 otherwise
 */
int base64_decode(const char *source, unsigned char *target, unsigned int targetlen) 
{
    const char *cur;
    unsigned char *dest, *max_dest;
    int d, dlast, phase;
    unsigned char c;
//将编码后的字符转成0 到63之间的值
    static int table[256] = {
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,  /* 20-2F */
        52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,  /* 30-3F */
        -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,  /* 40-4F */
        15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,  /* 50-5F */
        -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,  /* 60-6F */
        41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,  /* 70-7F */
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 80-8F */
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 90-9F */
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* A0-AF */
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* B0-BF */
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* C0-CF */
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* D0-DF */
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* E0-EF */
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
    };


    d = dlast = phase = 0;
    dest = target;
    max_dest = dest+targetlen;


    for (cur = source; *cur != '' && dest<max_dest; ++cur) //从第一个字符开始处理
    {
        d = table[(int)*cur];//获取对应字符的值
        if (d != -1) 
        {
            switch(phase) 
            {
case 0://如果是第一个字符,由于只有6位没有够满一个字节,不处理,留到下一个字符一起处理
   ++phase;
   break;
case 1://已经解析出2个字符,占12位,够满第一个字节,可以处理,取出前一个字符的6位和后一个字符的2位一起组成第一个字节
   c = ((dlast << 2) | ((d & 0x30) >> 4));
   *dest++ = c;
   ++phase;
   break;
case 2:
   c = (((dlast & 0xf) << 4) | ((d & 0x3c) >> 2));
   *dest++ = c;
   ++phase;
   break;
case 3:
   c = (((dlast & 0x03 ) << 6) | d);
   *dest++ = c;
   phase = 0;
   break;
   }
   
            dlast = d;
        }
    }


    /* we decoded the whole buffer */
    if (*cur == '') 
    {
return dest-target;
    }


    /* we did not convert the whole data, buffer was to small */
    return -1;
}



最后

以上就是成就鞋垫为你收集整理的base64的编解码函数的全部内容,希望文章能够帮你解决base64的编解码函数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部