📄 decode.c
字号:
#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <iconv.h>#include "decode.h"#include "logUtil.h"const char DeBase64Tab[] ={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, // '+' 0, 0, 0, 63, // '/' 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9' 0, 0, 0, 0, 0, 0, 0, 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, // 'A'-'Z' 0, 0, 0, 0, 0, 0, 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, // 'a'-'z'};int DecodeBase64(const char* pSrc, unsigned char* pDst, int nSrcLen){ int nDstLen; // 输出的字符计数 int nValue; // 解码用到的长整数 int i; unsigned int n; i = 0; nDstLen = 0; // 取4个字符,解码到一个长整数,再经过移位得到3个字节 while (i < nSrcLen) { if (*pSrc != '\r' && *pSrc!='\n') { n = *pSrc++; nValue = DeBase64Tab[n] << 18; n = *pSrc++; nValue += DeBase64Tab[n] << 12; *pDst++ = (nValue & 0x00ff0000) >> 16; nDstLen++; if (*pSrc != '=') { n = *pSrc++; nValue += DeBase64Tab[n] << 6; *pDst++ = (nValue & 0x0000ff00) >> 8; nDstLen++; if (*pSrc != '=') { n = *pSrc++; nValue += DeBase64Tab[n]; *pDst++ =nValue & 0x000000ff; nDstLen++; } } i += 4; } else // 回车换行,跳过 { pSrc++; i++; } } // 输出加个结束符 *pDst = '\0'; return nDstLen;}int DecodeQuoted(const char* pSrc, unsigned char* pDst, int nSrcLen){ int nDstLen; // 输出的字符计数 int i; i = 0; nDstLen = 0; while (i < nSrcLen) { if (strncmp(pSrc, "=\r\n", 3) == 0) // 软回车,跳过 { pSrc += 3; i += 3; } else { if (*pSrc == '=') // 是编码字节 { sscanf(pSrc, "=%02X", (unsigned int *)pDst); pDst++; pSrc += 3; i += 3; } else // 非编码字节 { *pDst++ = (unsigned char)*pSrc++; i++; } nDstLen++; } } // 输出加个结束符 *pDst = '\0'; return nDstLen;}int code_convert(char* from_charset, char* to_charset, char* inbuf, int inlen, char* outbuf, int outlen){ iconv_t cd; char** pin = &inbuf; char** pout = &outbuf; cd = iconv_open(to_charset,from_charset); if(cd == 0) return -1; memset(outbuf,0,outlen); if(iconv(cd,pin,(unsigned int *)&inlen,pout,(unsigned int*)&outlen) == -1) return -1; iconv_close(cd); return 0; }//UNICODE码转为GB2312码 //成功则返回一个动态分配的char*变量,需要在使用完毕后手动free,失败返回NULLchar* u2g(char *inbuf) { int nOutLen = 2 * strlen(inbuf) - 1; char* szOut = (char*)malloc(nOutLen); if (-1 == code_convert("utf-8","gb2312",inbuf,strlen(inbuf),szOut,nOutLen)) { free(szOut); szOut = NULL; } return szOut;} //GB2312码转为UNICODE码 //成功则返回一个动态分配的char*变量,需要在使用完毕后手动free,失败返回NULLchar* g2u(char *inbuf){ int nOutLen = 2 * strlen(inbuf) - 1; char* szOut = (char*)malloc(nOutLen); if (-1 == code_convert("gb2312","utf-8",inbuf,strlen(inbuf),szOut,nOutLen)) { free(szOut); szOut = NULL; } return szOut;}//GBK码转为UNICODE码 //成功则返回一个动态分配的char*变量,需要在使用完毕后手动free,失败返回NULLchar* gbk2u(char *inbuf){ int nOutLen = 2 * strlen(inbuf) - 1; char* szOut = (char*)malloc(nOutLen); if (-1 == code_convert("gbk","utf-8",inbuf,strlen(inbuf),szOut,nOutLen)) { free(szOut); szOut = NULL; } return szOut;}//判断是否被编码过int is_encode(char *subject){ u_int32_t len = strlen(subject); if (subject[len-2]=='\r' && subject[len-1]=='\n') { if (len<=9) return 0; } else { if (len<=7) return 0; } if (subject[0]!='=' || subject[1]!='?') return 0; if (subject[len-2]=='\r' && subject[len-1]=='\n') { if (subject[len-4]!='?' || subject[len-3]!='=') return 0; } else { if (subject[len-2]!='?' || subject[len-1]!='=') return 0; } return 1;}//反编码stringint analysis_encode_string(struct_decode *p, char *string){ char *pos=NULL; char *sign1=NULL; char *sign2=NULL; char *end = NULL; u_int32_t charset_len = 0; u_int32_t content_len = 0; if (is_encode(string)==0) return 0; pos = string+2; sign1 = strchr(pos, '?'); if (!sign1) { //doLog(0, "no find sing1!\n"); return 0; } charset_len = strlen(pos)-strlen(sign1); if (charset_len<=0) { //doLog(0, "charset_len<=0!\n"); return 0; } sign2 = strchr(sign1+1, '?'); if (!sign2) { //doLog(0, "no find sing2!\n"); return 0; } p->encode[0]=sign1[1]; p->encode[1]='\0'; end = strrchr(sign2+1, '?'); if (!end) { //doLog(0, "no find end!\n"); return 0; } if (strlen(end)==2) { if (end[1]!='=') { //doLog(0, "strlen(end)==2, end[1]!=\'=\'\n"); return 0; } } else if (strlen(end)==4) { if (end[1]!='='||end[2]!='\r'||end[3]!='\n') { //doLog(0, "strlen(end)==4, end[1]!=\'=\'||end[2]!=\'\\r\'||end[3]!=\'\\n\'\n"); return 0; } } else { //doLog(0, "strlen(end)!=2 && strlen(end)!=4 strlen(end)=%u\n", strlen(end)); return 0; } content_len = strlen(sign2+1) - strlen(end); if (p->encode[0]=='B'||p->encode[0]=='b')//BASE64编码后的字符串长度一定是4的整数倍 { if (content_len%4!=0) { //doLog(0, "encode is BASE64, content_len%4!=0, content_len = %u\n", content_len); return 0; } } //else //doLog(0, "encode is Quoted\n"); p->charset = (char *)malloc(charset_len+1); memset(p->charset, 0, charset_len+1); memcpy(p->charset, pos, charset_len); p->source = (char *)malloc(content_len+1); memset(p->source, 0, content_len+1); memcpy(p->source, sign2+1, content_len); p->dest = (unsigned char *)malloc(content_len+1); memset(p->dest, 0, content_len+1); if (p->encode[0]=='B'|| p->encode[0]=='b') p->d_len = DecodeBase64(p->source, p->dest, content_len); else if (p->encode[0]=='Q'|| p->encode[0]=='q') p->d_len = DecodeQuoted(p->source, p->dest, content_len); return 1;}void free_struct_decode(struct_decode *p){ if (p->charset!=NULL) free(p->charset); if (p->source!=NULL) free(p->source); if (p->dest!=NULL) free(p->dest); return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -