Base64代码

base64.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> // bindata待编码数据buffer base64 编码后数据buffer binlength 待编码数据大小
char* base64_encode(const unsigned char* bindata, char* base64, int binlength);
// base64编码字符串 bindata 解码后buffer
int base64_decode(const char* base64, unsigned char* bindata); int main()
{
FILE* fp = NULL;
unsigned int imageSize; //图片字节数
char* imageBin;
char* imageBase64;
char* imageOutput;
size_t result;
char* ret;
unsigned int base64StrLength; fp = fopen("lena.bmp", "rb"); //待编码图片
if (NULL == fp)
{
printf("file open file");
return -1;
}
//获取图片大小
fseek(fp, 0L, SEEK_END);
imageSize = ftell(fp);
fseek(fp, 0L, SEEK_SET); //分配内存存储整个图片
imageBin = (char*)malloc(sizeof(char) * imageSize);
if (NULL == imageBin)
{
printf("malloc failed");
return -1;
} //读取图片
result = fread(imageBin, 1, imageSize, fp);
if (result != imageSize)
{
printf("file read failed");
return -1;
}
fclose(fp); //分配编码后图片所在buffer
imageBase64 = (char*)malloc(sizeof(char) * imageSize * 2);//因为编码一版会比源数据大1/3的样子,这里直接申请源文件一倍的空间
if (NULL == imageBase64)
{
printf("malloc failed");
return -1;
} //base64编码
base64_encode(imageBin, imageBase64, imageSize);
base64StrLength = strlen(imageBase64);
printf("base64 str length:%d\n", base64StrLength);
printf("将图片读入out.txt中\n");
FILE* file = fopen("out.txt", "wb");
if (file == NULL)
{
printf("Error!");
exit(1);
}
//将Base64编码写入文件
int i = 0;
while (imageBase64[i] != NULL)
{
fputc(imageBase64[i++], file);
}
fclose(file); //分配存储解码数据buffer
imageOutput = (char*)malloc(sizeof(char) * imageSize);//解码后应该和源图片大小一致
if (NULL == imageBase64)
{
printf("malloc failed");
return -1;
}
base64_decode(imageBase64, imageOutput); fp = fopen("output.bmp", "wb");
if (NULL == fp)
{
printf("file open file");
return -1;
}
fwrite(imageOutput, 1, imageSize, fp);
fclose(fp); free(imageBin);
free(imageBase64);
free(imageOutput); return 0;
} const char* base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char* base64_encode(const unsigned char* bindata, char* base64, int binlength)
{
int i, j;
unsigned char current; for (i = 0, j = 0; i < binlength; i += 3)
{
current = (bindata[i] >> 2);
current &= (unsigned char)0x3F;
base64[j++] = base64char[(int)current]; current = ((unsigned char)(bindata[i] << 4)) & ((unsigned char)0x30);
if (i + 1 >= binlength)
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
base64[j++] = '=';
break;
}
current |= ((unsigned char)(bindata[i + 1] >> 4)) & ((unsigned char)0x0F);
base64[j++] = base64char[(int)current]; current = ((unsigned char)(bindata[i + 1] << 2)) & ((unsigned char)0x3C);
if (i + 2 >= binlength)
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ((unsigned char)(bindata[i + 2] >> 6)) & ((unsigned char)0x03);
base64[j++] = base64char[(int)current]; current = ((unsigned char)bindata[i + 2]) & ((unsigned char)0x3F);
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return 0;
} int base64_decode(const char* base64, unsigned char* bindata)
{
int i, j;
unsigned char k;
unsigned char temp[4];
for (i = 0, j = 0; base64[i] != '\0'; i += 4)
{
memset(temp, 0xFF, sizeof(temp));
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i])
temp[0] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 1])
temp[1] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 2])
temp[2] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 3])
temp[3] = k;
} bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2)) & 0xFC)) |
((unsigned char)((unsigned char)(temp[1] >> 4) & 0x03));
if (base64[i + 2] == '=')
break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4)) & 0xF0)) |
((unsigned char)((unsigned char)(temp[2] >> 2) & 0x0F));
if (base64[i + 3] == '=')
break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6)) & 0xF0)) |
((unsigned char)(temp[3] & 0x3F));
}
return j;
}

unistd.h

#pragma once
/** This file is part of the Mingw32 package.
unistd.h maps (roughly) to io.h
*/
#ifndef _UNISTD_H
#define _UNISTD_H
#include <io.h>
#include <process.h>
#endif /* _UNISTD_H */

将Base64编码存于文件中

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> // bindata待编码数据buffer base64 编码后数据buffer binlength 待编码数据大小
char* base64_encode(const unsigned char* bindata, char* base64, int binlength);
// base64编码字符串 bindata 解码后buffer
int base64_decode(const char* base64, unsigned char* bindata); int main()
{
FILE* fp = NULL;
unsigned int imageSize; //图片字节数
char* imageBin;
char* imageBase64;
char* imageOutput;
size_t result;
char* ret;
unsigned int base64StrLength; fp = fopen("lena.bmp", "rb"); //待编码图片
if (NULL == fp)
{
printf("file open file");
return -1;
}
//获取图片大小
fseek(fp, 0L, SEEK_END);
imageSize = ftell(fp);
fseek(fp, 0L, SEEK_SET); //分配内存存储整个图片
imageBin = (char*)malloc(sizeof(char) * imageSize);
if (NULL == imageBin)
{
printf("malloc failed");
return -1;
} //读取图片
result = fread(imageBin, 1, imageSize, fp);
if (result != imageSize)
{
printf("file read failed");
return -1;
}
fclose(fp); //分配编码后图片所在buffer
imageBase64 = (char*)malloc(sizeof(char) * imageSize * 2);//因为编码一版会比源数据大1/3的样子,这里直接申请源文件一倍的空间
if (NULL == imageBase64)
{
printf("malloc failed");
return -1;
} //base64编码
base64_encode(imageBin, imageBase64, imageSize);
base64StrLength = strlen(imageBase64);
printf("base64 str length:%d\n", base64StrLength);
printf("将图片读入out.txt中\n");
FILE* file = fopen("out.txt", "wb");
if (file == NULL)
{
printf("Error!");
exit(1);
}
//将Base64编码写入文件
int i = 0;
while (imageBase64[i] != NULL)
{
fputc(imageBase64[i++], file);
}
fclose(file); //分配存储解码数据buffer
imageOutput = (char*)malloc(sizeof(char) * imageSize);//解码后应该和源图片大小一致
if (NULL == imageBase64)
{
printf("malloc failed");
return -1;
}
base64_decode(imageBase64, imageOutput); fp = fopen("output.bmp", "wb");
if (NULL == fp)
{
printf("file open file");
return -1;
}
fwrite(imageOutput, 1, imageSize, fp);
fclose(fp); free(imageBin);
free(imageBase64);
free(imageOutput); return 0;
} const char* base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char* base64_encode(const unsigned char* bindata, char* base64, int binlength)
{
int i, j;
unsigned char current; for (i = 0, j = 0; i < binlength; i += 3)
{
current = (bindata[i] >> 2);
current &= (unsigned char)0x3F;
base64[j++] = base64char[(int)current]; current = ((unsigned char)(bindata[i] << 4)) & ((unsigned char)0x30);
if (i + 1 >= binlength)
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
base64[j++] = '=';
break;
}
current |= ((unsigned char)(bindata[i + 1] >> 4)) & ((unsigned char)0x0F);
base64[j++] = base64char[(int)current]; current = ((unsigned char)(bindata[i + 1] << 2)) & ((unsigned char)0x3C);
if (i + 2 >= binlength)
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ((unsigned char)(bindata[i + 2] >> 6)) & ((unsigned char)0x03);
base64[j++] = base64char[(int)current]; current = ((unsigned char)bindata[i + 2]) & ((unsigned char)0x3F);
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return 0;
} int base64_decode(const char* base64, unsigned char* bindata)
{
int i, j;
unsigned char k;
unsigned char temp[4];
for (i = 0, j = 0; base64[i] != '\0'; i += 4)
{
memset(temp, 0xFF, sizeof(temp));
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i])
temp[0] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 1])
temp[1] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 2])
temp[2] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 3])
temp[3] = k;
} bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2)) & 0xFC)) |
((unsigned char)((unsigned char)(temp[1] >> 4) & 0x03));
if (base64[i + 2] == '=')
break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4)) & 0xF0)) |
((unsigned char)((unsigned char)(temp[2] >> 2) & 0x0F));
if (base64[i + 3] == '=')
break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6)) & 0xF0)) |
((unsigned char)(temp[3] & 0x3F));
}
return j;
}

最新文章

  1. 微信ios版6.2更新 聊天记录迁移更快捷朋友圈可翻译
  2. jsonp实现跨域请求
  3. sprintf函数php的详细使用方法
  4. 【转】下载太慢?简单设置让iTunes提速十几倍
  5. BZOJ 3969 low power
  6. 内存泄露 Memory Leaks
  7. easyui 点击combox 文本框 显示下拉 panel
  8. Android 通过调用系统,如接口 谷歌语音、百度语音、科大讯飞语音等语音识别方法对话框
  9. CF 672 div2 D
  10. tp框架实现验证码验证
  11. Gradle 下载的依赖包在什么位置?
  12. JDK源码分析(四)—— ConcurrentHashMap
  13. ACM ICPC 2017 Warmup Contest 9 I
  14. vue2.0s中eventBus实现兄弟组件通信
  15. topcoder srm 435 div1
  16. ApplicationContext(四)BeanFactory 功能扩展
  17. mysql查询当前系统时间
  18. 内存与cpu的关系
  19. c++从文件中读取一行数据并保存在数组中
  20. mini Linux制作过程(25/01)

热门文章

  1. 35岁的程序员正在消失?No,我认识了一个50岁的程序员!
  2. VMware虚拟机中共享文件夹 开机启动
  3. 一键开启win10全局utf8,编码问题说拜拜
  4. 51node1256 乘法匿元(扩展欧几里得)
  5. hystrix线程池隔离的原理与验证
  6. JS的各种数据类型
  7. 爬虫在linux下启动selenium-安装谷歌浏览器和驱动(傻瓜式教程)
  8. oracle oracle sqldeveloper 12505 创建连接失败
  9. elasticsearch mysql配置
  10. 栈&amp;队列&amp;并查集&amp;哈希表(julyedu网课整理)