概述
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;
}
最后
以上就是爱听歌小馒头为你收集整理的base64解码函数的全部内容,希望文章能够帮你解决base64解码函数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复