标题:100 行代码搞定 base64 编码
只看楼主
RockCarry
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:13
帖 子:662
专家分:58
注 册:2005-8-5
结帖率:95.65%
已结贴  问题点数:20 回复次数:2 
100 行代码搞定 base64 编码
base64 这个还是比较简单的,简单写了一下,100 行左右

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef unsigned char uint8_t;
typedef unsigned int  uint32_t;

uint8_t base64_to_char(uint8_t v)
{
    if (v < 26) {
        return 'A' + (v - 0 );
    } else if (v < 52) {
        return 'a' + (v - 26);
    } else if (v < 62) {
        return '0' + (v - 52);
    } else if (v == 62) {
        return '+';
    } else {
        return '/';
    }
}

uint8_t char_to_base64(uint8_t c, int *padding)
{
    if (c >= 'A' && c <= 'Z') {
        return c - 'A' + 0;
    } else if (c >= 'a' && c <= 'z') {
        return c - 'a' + 26;
    } else if (c >= '0' && c <= '9') {
        return c - '0' + 52;
    } else if (c == '+') {
        return 62;
    } else if (c == '/') {
        return 63;
    } else if (c == '=') {
        (*padding)++;
        return 0;
    } else {
        return -1;
    }
}

void base64_encode_3bytes(uint8_t *dst, uint8_t *src)
{
    uint32_t data = (src[0] << 16) | (src[1] << 8) | (src[2] << 0);
    *dst++ = base64_to_char((data >> 18) & 0x3f);
    *dst++ = base64_to_char((data >> 12) & 0x3f);
    *dst++ = base64_to_char((data >>  6) & 0x3f);
    *dst++ = base64_to_char((data >>  0) & 0x3f);
}

int base64_decode_4bytes(uint8_t *dst, uint8_t *src)
{
    uint32_t data    = 0;
    int      padding = 0;
    data |= char_to_base64(*src++, &padding); data <<= 6;
    data |= char_to_base64(*src++, &padding); data <<= 6;
    data |= char_to_base64(*src++, &padding); data <<= 6;
    data |= char_to_base64(*src++, &padding); data <<= 0;
    *dst++ = (data >> 16) & 0xff;
    *dst++ = (data >>  8) & 0xff;
    *dst++ = (data >>  0) & 0xff;
    return padding;
}

int main(int argc, char *argv[])
{
    FILE *fpsrc;
    FILE *fpdst;
    int   encode;
    int   padding;
    int   ret = -1;
    uint8_t buf[4];

    if (argc < 4) {
        printf(
            "+-----------+\n"
            " base64 tool \n"
            "+-----------+\n"
            "usage:\n"
            "  decoding: %s -d input output\n"
            "  encoding: %s -e input output\n",
            argv[0], argv[0]
        );
        return 0;
    }

    fpsrc = fopen(argv[2], "rb");
    fpdst = fopen(argv[3], "wb");
    if (!fpsrc || !fpdst) {
        printf("failed to open file(s) !\n");
        goto done;
    }

    encode = !strcmp("-e", argv[1]);
    if (encode) {
        while (1) {
            ret = fread(buf, 1, 3, fpsrc);
            if (ret <= 0 || ret > 3) break;
            padding = 3 - ret;
            memset(buf + ret, 0, padding);
            base64_encode_3bytes(buf, buf);
            fwrite(buf , 1, 4 - padding, fpdst);
            fwrite("==", 1, padding    , fpdst);
        }
    } else {
        while (1) {
            ret = fread(buf, 1, 4, fpsrc);
            if (ret <= 0 || ret > 4) break;
            padding = base64_decode_4bytes(buf, buf);
            fwrite(buf, 1, 3 - padding, fpdst);
        }
    }

done:
    if (fpsrc) fclose(fpsrc);
    if (fpdst) fclose(fpdst);
    return ret;
}






搜索更多相关主题的帖子: int return ret data c++ 
2017-11-13 10:52
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
得分:20 
不明觉厉~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-11-13 13:03
chaw899
Rank: 2
等 级:禁止访问
帖 子:48
专家分:11
注 册:2018-11-29
得分:0 
回复 楼主 RockCarry
版主,代码存在bug。我把mp3用base64编码,然后用你的程序进行解码。
解码后的mp3无法打开,对比前后mp3的哈希值发生了变化。
2019-07-09 17:26



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-482418-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.428898 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved