1 Star 0 Fork 46

武林q传 / php screw plus

forked from delxiong / php screw plus 
加入 Gitee
与超过 600 万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
decode.c 2.54 KB
一键复制 编辑 Web IDE 原始数据 按行查看 历史
Del Xiong 提交于 2016-06-22 13:38 . screw plus base version
/**
* `decode.c' - b64
*
* copyright (c) 2014 joseph werle
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
unsigned char *
b64_decode_ex (const char *, size_t, int *);
unsigned char *
b64_decode (const char *src, size_t len,int *decsize) {
return b64_decode_ex(src, len, decsize);
}
unsigned char *
b64_decode_ex (const char *src, size_t len, int *decsize) {
int i = 0;
int j = 0;
int l = 0;
size_t size = 0;
unsigned char *dec = NULL;
unsigned char buf[3];
unsigned char tmp[4];
// alloc
dec = (unsigned char *) malloc(0);
if (NULL == dec) { return NULL; }
// parse until end of source
while (len--) {
// break if char is `=' or not base64 char
if ('=' == src[j]) { break; }
if (!(isalnum(src[j]) || '+' == src[j] || '/' == src[j])) { break; }
// read up to 4 bytes at a time into `tmp'
tmp[i++] = src[j++];
// if 4 bytes read then decode into `buf'
if (4 == i) {
// translate values in `tmp' from table
for (i = 0; i < 4; ++i) {
// find translation char in `b64_table'
for (l = 0; l < 64; ++l) {
if (tmp[i] == b64_table[l]) {
tmp[i] = l;
break;
}
}
}
// decode
buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];
// write decoded buffer to `dec'
dec = (unsigned char *) realloc(dec, size + 3);
for (i = 0; i < 3; ++i) {
dec[size++] = buf[i];
}
// reset
i = 0;
}
}
// remainder
if (i > 0) {
// fill `tmp' with `\0' at most 4 times
for (j = i; j < 4; ++j) {
tmp[j] = '\0';
}
// translate remainder
for (j = 0; j < 4; ++j) {
// find translation char in `b64_table'
for (l = 0; l < 64; ++l) {
if (tmp[j] == b64_table[l]) {
tmp[j] = l;
break;
}
}
}
// decode remainder
buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];
// write remainer decoded buffer to `dec'
dec = (unsigned char *) realloc(dec, size + (i - 1));
for (j = 0; (j < i - 1); ++j) {
dec[size++] = buf[j];
}
}
// Make sure we have enough space to add '\0' character at end.
dec = (unsigned char *) realloc(dec, size + 1);
dec[size] = '\0';
// Return back the size of decoded string if demanded.
if (decsize != NULL) *decsize = size;
return dec;
}

评论 ( 0 )

你可以在登录后,发表评论

C
1
https://gitee.com/php_mysql/php-screw-plus.git
git@gitee.com:php_mysql/php-screw-plus.git
php_mysql
php-screw-plus
php screw plus
master

搜索帮助

105716 1d94204e 1850385 105716 2d26be5c 1850385