C语言base64编码与解码
来源:互联网 发布:java jar 设置jdk 编辑:程序博客网 时间:2024/05/17 19:59
昨天工作中遇到需要将char *转换成base64的问题,把这块单独提取出来作为一个模块。
这里有几个参数需要解释一下:
char *out:一块buffer的首地址,这块buffer就是用来存放我们转码后的内容的。当然要注意提前malloc内存。
char *in:原字符串的首地址,指向需要转码的内容,即原字符串内容。
int inlen:原字符串长度。
int maxlen:可支持的转换后的字符串的最大长度,超出这个长度函数会返回-3,且out指向’\0’。
函数返回值:正常情况下返回转换后字符串的实际长度,错误返回错误码。
#define INVALID_ARG -1
#define WRONG_FORMAT -2
#define OUTPUT_OVERFLOW -3
base64.h
#ifndef __BASE64_H__#define __BASE64_H__/*************************************************Function: base64_to_asciiDescription: decodes string from base64 to asciiInput: const char *in: the input string (NIL-terminated)int inlen: length of input stringint maxlen: the output buffer size limit, 0 to ignoreOutput: unsigned char *out: decoded output stringReturn: length of output string on successfulless than 0 on error occur, then the output is invalidOthers: *************************************************/int base64_decode(unsigned char *out, unsigned char *in, int inlen, int maxlen);/*************************************************Function: ascii_to_base64Description: encodes string from ascii to base64Input: const char *in: the input string (NIL-terminated)int inlen: length of input stringint maxlen: the output buffer size limit, 0 to ignoreOutput: unsigned char *out: decoded output stringReturn: length of output string on successfulless than 0 on error occur, then the output is invalidOthers: *************************************************/int base64_encode(char *out, const unsigned char *in, int inlen, int maxlen);#endif
base64.c
#include <ctype.h>#include <string.h>#include <syslog.h>#define BAD -1#define INVALID_ARG -1#define WRONG_FORMAT -2#define OUTPUT_OVERFLOW -3static const char base64digits[] = // characters used for base64 code "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";static const char base64val[] = {// base64 alphabet BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, 62, BAD, BAD, BAD, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, BAD, BAD, BAD, BAD, BAD, BAD, BAD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, BAD, BAD, BAD, BAD, BAD, BAD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, BAD, BAD, BAD, BAD, BAD};#define ENCODE64(c) (((c) & 0xC0) ? BAD : base64digits[(int)c])#define DECODE64(c) (((c) & 0x80) ? BAD : base64val[(int)c])//~#define DECODE64(c) (isascii(c) ? base64val[c] : BAD)/*************************************************Function: base64_to_asciiDescription: decodes string from base64 to asciiInput: const char *in: the input string (NIL-terminated)int inlen: length of input stringint maxlen: the output buffer size limit, 0 to ignoreOutput: unsigned char *out: decoded output stringReturn: length of output string on successfulless than 0 on error occur, then the output is invalidOthers: *************************************************///int base64_decode(unsigned char *out, const unsigned char *in, int inlen, int maxlen) {int base64_decode(unsigned char *out, unsigned char *in, int inlen, int maxlen) { //~int inlen = strlen (in); int outlen = 0; int over = 0; // decode over flag register char digit0, digit1, digit2, digit3; unsigned char *out_org = out; memset(out, 0x00, maxlen); // check if arguments valid if (!out || !in) { return INVALID_ARG; } // decode each four base64 characters for (; inlen >= 4; inlen -= 4, in += 4) { // update output length and check overflow if (++outlen >= maxlen && maxlen) { *out_org = '\0'; return OUTPUT_OVERFLOW; } if ((digit0 = DECODE64(in[0])) == BAD) { *out_org = '\0'; return WRONG_FORMAT; } if ((digit1 = DECODE64(in[1])) == BAD) { *out_org = '\0'; return WRONG_FORMAT; } *out++ = ((digit0 << 2) & 0xFC) | ((digit1 >> 4) & 0x03); if (in[2] != '=') { // update output length and check overflow if (++outlen >= maxlen && maxlen) { *out_org = '\0'; return OUTPUT_OVERFLOW; } if ((digit2 = DECODE64(in[2])) == BAD) { *out_org = '\0'; return WRONG_FORMAT; } *out++ = ((digit1 << 4) & 0xF0) | ((digit2 >> 2) & 0x0F); if (in[3] != '=') { // update output length and check overflow if (++outlen >= maxlen && maxlen) { *out_org = '\0'; return OUTPUT_OVERFLOW; } if ((digit3 = DECODE64(in[3])) == BAD) { *out_org = '\0'; return WRONG_FORMAT; } *out++ = ((digit2 << 6) & 0xC0) | (digit3 & 0x3F); } else { over = 1; break; } } else { over = 1; break; } } // there cannt have tail-fragment except after '=' if (!over && inlen > 0) { *out_org = '\0'; return WRONG_FORMAT; } // terminate the output string *out = '\0'; return outlen;}/*************************************************Function: ascii_to_base64Description: encodes string from ascii to base64Input: const char *in: the input string (NIL-terminated)int inlen: length of input stringint maxlen: the output buffer size limit, 0 to ignoreOutput: unsigned char *out: decoded output stringReturn: length of output string on successfulless than 0 on error occur, then the output is invalidOthers: *************************************************/int base64_encode(char *out, const unsigned char *in, int inlen, int maxlen) { int outlen = 0; char *out_org = out; memset(out, 0x00, maxlen); // check if arguments valid if (!out || !in) { return INVALID_ARG; } // encode each three ascii characters for (; inlen >= 3; inlen -= 3, in += 3) { // update output length and check overflow outlen += 4; if (outlen >= maxlen && maxlen) { // use >= because there must have a '\0' *out_org = '\0'; return OUTPUT_OVERFLOW; } if ((*out++ = ENCODE64((in[0] >> 2) & 0x3F)) == BAD) { *out_org = '\0'; return WRONG_FORMAT; } if ((*out++ = ENCODE64(((in[0] << 4) & 0x30) | ((in[1] >> 4) & 0x0F))) == BAD) { *out_org = '\0'; return WRONG_FORMAT; } if ((*out++ = ENCODE64(((in[1] << 2) & 0x3C) | ((in[2] >> 6) & 0x03))) == BAD) { *out_org = '\0'; return WRONG_FORMAT; } if ((*out++ = ENCODE64(in[2] & 0x3F)) == BAD) { *out_org = '\0'; return WRONG_FORMAT; } } // encode tail-fragment if exist if (inlen > 0) { char fragment; // update output length and check overflow outlen += 4; if (outlen >= maxlen && maxlen) { *out_org = '\0'; return OUTPUT_OVERFLOW; } if ((*out++ = ENCODE64((in[0] >> 2) & 0x3F)) == BAD) { *out_org = '\0'; return WRONG_FORMAT; } fragment = (in[0] << 4) & 0x30; if (inlen > 1) { fragment |= (in[1] >> 4) & 0x0F; } if ((*out++ = ENCODE64(fragment)) == BAD) { *out_org = '\0'; return WRONG_FORMAT; } if (inlen < 2) { *out++ = '='; } else { if ((*out++ = ENCODE64((in[1] << 2) & 0x3C)) == BAD) { *out_org = '\0'; return WRONG_FORMAT; } } *out++ = '='; } // terminate the output string *out = '\0'; return outlen;}
0 0
- C语言base64编码与解码
- Base64的编码与解码 (C语言)
- base64编码解码的实现(C语言)
- base64编码解码的实现(C语言)
- Base64编码解码的实现(C语言)
- Base64编码解码c语言实现
- Base64编码解码C语言实现
- Base64编码解码的实现(C语言)
- base64 编码解码 c语言实现
- base64编码、解码的C语言实现
- Base64编码解码C语言实现
- base64编码、解码的C语言实现
- base64编码、解码的C语言实现
- base64编码、解码的C语言实现
- BASE64编码、解码的C语言实现
- C语言实现Base64编码/解码
- .net C#实现Base64编码与解码
- C#实现Base64编码与解码
- 批处理常用特殊符号
- 编码,解码,乱码,问题详解
- 关于java开发的性能问题总结
- HSI、HSV、RGB、CMYK、HSL、HSB、Ycc、XYZ、Lab、YUV颜色模型的区别
- Android UI - 实现广告Banner轮播效果
- C语言base64编码与解码
- 指针易混肴的定义方式详解
- dos和sh等shell命令的学习
- Thread.CurrentThread.getName 与 this.getName()
- Android Linker 与 SO 加壳技术
- 利用栈结构二进制数转十进制数代码实现c++
- appcan多图片上传
- 截图+贴图 Snipaste
- ios性能测试-实时查看内存/cpu