之前的这篇文章稍微有些繁琐,作为模块使用的话要改很多地方,不太方便,于是简化了一下代码。
//
// Created by fumiama on 2020/3/28.
//
#include <stdio.h>
#include <string.h>
#define OUTSIZE 4
#define OFFSET 22
char out[OUTSIZE];
char list[173];
#define based(based64, out){\
out[0] = ((list[based64[0]] << 2) & 0xfc) |\
((list[based64[1]] >> 4) & 0x03);\
out[1] = ((list[based64[1]] << 4) & 0xf0) |\
((list[based64[2]] >> 2) & 0x0f);\
out[2] = ((list[based64[2]] << 6) & 0xc0) |\
(list[based64[3]] & 0x3f);\
out[3] = '\0';\
}\
int main(int argc, char *argv[]){
int b64 = 'A';
char txt = 0;
for(; b64 <= 'Z'; b64++, txt++) list[b64] = txt;
for(b64 = 'a'; b64 <= 'z'; b64++, txt++) list[b64] = txt;
for(b64 = '0'; b64 <= '9'; b64++, txt++) list[b64] = txt;
list['+'] = txt++;
list['/'] = txt;
char *str = argv[1];
int index = 0;
FILE *fpo = fopen("./b64decode", "wb");
int len = strlen(str);
while(str[len - 1] == '=') str[len-- - 1] = 0;
for(index = OFFSET; index < len; index += 4){
based((str + index), out)
fwrite(out, 1, (len - index < 3)?len - index - 1:3, fpo);
}
fclose(fpo);
return 0;
}