⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cutil-base64.c

📁 Linux环境下常用功能的实现
💻 C
字号:
#include "cutil.h"static char b64EncTable[64] = {    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',       'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',       'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',       'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'};/** * cutil_base64_from_string * @pszInputString:	要求转换的字符串 * @nInputLen: 要求转换的长度 * @pszOutputString: 保存转换的结果 *  * 将字符串编码转变为BASE64的编码 *  * Returns: 成功操作返回转换后长度 失败操作返回-1 */int cutil_base64_from_string(unsigned char * pszInputString, int nInputLen, 		unsigned char * pszOutputString){    int nInputStrPos;    int nOutputStrPos;    unsigned char cInputByte[3];    unsigned char outBytes[4];    int nOutputLen;    int nLineLen = 0;    if(!pszInputString || (nInputLen == 0))     	return -1;    nOutputLen = ((nInputLen + 2) / 3) * 4;    nOutputLen += (nOutputLen / 76);    for(nInputStrPos = 0, nOutputStrPos = 0; nInputStrPos < nInputLen;     	nInputStrPos += 3, nOutputStrPos += 4)	{		cInputByte[0] = pszInputString[nInputStrPos];		cInputByte[1] = cInputByte[2] = 0;        if((nInputStrPos + 1) < nInputLen)		{			cInputByte[1] = pszInputString[nInputStrPos + 1];            if((nInputStrPos + 2) < nInputLen)				cInputByte[2] = pszInputString[nInputStrPos + 2];        }                    outBytes[0] = cInputByte[0] >> 2;        outBytes[1] = ((cInputByte[0] & 0x03) << 4) | (cInputByte[1] >> 4);        outBytes[2] = ((cInputByte[1] & 0x0F) << 2) | (cInputByte[2] >> 6);        outBytes[3] = cInputByte[2] & 0x3F;                    pszOutputString[nOutputStrPos] = b64EncTable[outBytes[0]];        pszOutputString[nOutputStrPos + 1] = b64EncTable[outBytes[1]];                    if((nInputStrPos + 1) < nInputLen) 			pszOutputString[nOutputStrPos + 2] = b64EncTable[outBytes[2]];        else            pszOutputString[nOutputStrPos + 2] = '=';                if((nInputStrPos + 2) < nInputLen) 			pszOutputString[nOutputStrPos + 3] = b64EncTable[outBytes[3]];        else            pszOutputString[nOutputStrPos + 3] = '=';                 nLineLen ++;        if(nLineLen == 19)		{			pszOutputString[nOutputStrPos + 4] = '\n';			nOutputStrPos ++;			nLineLen = 0;        }    }    return nOutputStrPos;}/* * 校验是否为BASE64编码格式 */static int ValidBase64(char cInputChar){    if(((cInputChar >= 'A') && (cInputChar <= 'Z')) ||       ((cInputChar >= 'a') && (cInputChar <= 'z')) ||       ((cInputChar >= '0') && (cInputChar <= '9')) ||       (cInputChar == '+') ||        (cInputChar == '/') ||        (cInputChar == '='))	{        return 1;    }    return 0;}static unsigned char DecodeBase64Char(unsigned char cInputChar){    if((cInputChar >= 'A') && (cInputChar <= 'Z'))     {        return (cInputChar - 'A');    }     else if((cInputChar >= 'a') && (cInputChar <= 'z'))     {        return((cInputChar - 'a') + 26);    }     else if((cInputChar >= '0') && (cInputChar <= '9'))     {        return((cInputChar - '0') + 52);    }     else if(cInputChar == '+')     {        return 62;    }    return 63;}    /** * cutil_base64_to_string * @pszInputString:	要求转换的字符串 * @nInputLen:	要求转换的长度 * @pszOutputString: 保存转换的结果 *  * @函数说明: 将BASE64编码转变为字符串编码  * Returns: 成功返回转换的长度 失败返回 -1 */int cutil_base64_to_string(unsigned char * pszInputString, 			unsigned int nInputLen, unsigned char * pszOutputString){    unsigned int nRealLen;    unsigned int nInputStrPos;    unsigned int nOutputStrPos;    char cInputByte[4];    if(!pszInputString || (nInputLen == 0))     	return 0;    nOutputStrPos = 0;    for(nInputStrPos = 0; nInputStrPos < nInputLen; nInputStrPos ++)     {		if(ValidBase64(pszInputString[nInputStrPos])) 		{            if(nInputStrPos > nOutputStrPos)             	pszInputString[nOutputStrPos] = pszInputString[nInputStrPos];            nOutputStrPos ++;        }    }    pszInputString[nOutputStrPos] = '\0';    nRealLen = (nOutputStrPos / 4) * 3;     if(pszInputString[nOutputStrPos - 1] == '=')     	nRealLen --;    if(pszInputString[nOutputStrPos - 2] == '=')     	nRealLen --;    for(nInputStrPos = 0, nOutputStrPos = 0; nOutputStrPos < nRealLen;    	nInputStrPos += 4, nOutputStrPos += 3)	{		cInputByte[0] = DecodeBase64Char(pszInputString[nInputStrPos]);        cInputByte[1] = cInputByte[2] = cInputByte[3] = 0;        if((nInputStrPos + 1) < nInputLen) 			cInputByte[1] = DecodeBase64Char(pszInputString[nInputStrPos + 1]);                     if((nInputStrPos + 2) < nInputLen)			cInputByte[2] = DecodeBase64Char(pszInputString[nInputStrPos + 2]);        if((nInputStrPos + 3) < nInputLen)			cInputByte[3] = DecodeBase64Char(pszInputString[nInputStrPos + 3]);         pszOutputString[nOutputStrPos]         	= ((cInputByte[0] << 2) | (cInputByte[1] >> 4));        	        if(pszInputString[nInputStrPos + 2] != '=')        {			pszOutputString[nOutputStrPos + 1] 				= (((cInputByte[1] & 0xF) << 4) | (cInputByte[2] >> 2));		}		        if(pszInputString[nInputStrPos + 3] != '=')        	{			pszOutputString[nOutputStrPos + 2] 				= (((cInputByte[2] & 0x3) << 6) | cInputByte[3]);		}    }	return nRealLen;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -