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

📄 apidecode.c

📁 base64 encode source
💻 C
字号:
#include "ApiDecode.h"

/*=== FuncHeader ===============================================================================
 *	[FunctionName]
 *	    BOOL API_Decode_Base64( BYTE *pInText , DWORD InLen , BYTE *pOutText , DWORD *UsedBufLen )
 *	[Function]
 *	    Decode Base64 Code to Content Text
 *	[Input Parameter]
 *	    pInText     :Base64 Code pointer
 *	    InLen       :Base64 Code length,if it is no larger than 0 ,
 *	                 the program can calculate the real size in BYTE
 *	[Output Parameter]
 *	    pOutText    :the Content Text buffer pointer
 *	    UsedBufLen  :if the decoding procedure success,the length of buffer used in BYTE;
 *                   else it's 0,means the pointer illegal(is NULL)
 *	[Return]
 *	    whether the decoding procedure is successful or not(FAILURE/SUCCESS)
 *  [Note]
 *      InLen,OutBufMaxLen,*UsedBufLen do not include '\0',and the result is that it does not add '\0' to the outbuff.
 *      Because it does not calculate the needed size , so must be sure that the out buffer is enough.
===== EndHeader ===============================================================================*/
static BOOL DB_IsValid(BYTE InChar,BYTE *CharValue)	/* DB = Decode Base64	*/
{/* if return is SUCCESS,the CharValue is valid	*/
	BOOL ret;	/* return value	*/

	if ( InChar >= 'A' && InChar <= 'Z' )		/* Base64 Alphabet 0~25		*/
	{
		*CharValue = InChar - 'A' ;
		ret = SUCCESS;
	}
	else if ( InChar >= 'a' && InChar <= 'z' )	/* Base64 Alphabet 26~51	*/
	{
		*CharValue = InChar - 'a' + 26;
		ret = SUCCESS;
	}
	else if ( InChar >= '0' && InChar <= '9' )	/* Base64 Alphabet 52~61	*/
	{
		*CharValue = InChar - '0' + 52;
		ret = SUCCESS;
	}
	else if ( InChar == '+' )					/* Base64 Alphabet 62		*/
	{
		*CharValue = 62;
		ret = SUCCESS;
	}
	else if ( InChar == '/' )					/* Base64 Alphabet 63		*/
	{
		*CharValue = 63;
		ret = SUCCESS;
	}
	else if ( InChar == '=' )					/* Base64 Alphabet PAD , means the end of data stream	*/
	{
		*CharValue = '=';						/* stail use this value	*/
		ret = FAILURE;							/* so it means invalid	*/
	}
	else										/* all not in Base64 Alphabet are invalid	*/
	{
		*CharValue = 0;
		ret = FAILURE;
	}

	return ret;
}

BOOL API_Decode_Base64( BYTE *pInText , DWORD InLen , BYTE *pOutText , DWORD *UsedBufLen )
{
	DWORD InByteIndex;			/* BYTE Index of Input Text	*/
	BOOL  IsOK = FAILURE ;		/* return value	*/
	BYTE  Group[4];				/* 4-byte group	*/
	BYTE  GroupIndex;			/* GroupIndex	*/
	BYTE  CharValue;			/* the decode char index in Base64 Alphabet	*/

	*UsedBufLen = 0;			/* Initial	*/
	if ( pInText != NULL && pOutText != NULL )
	{
		if ( InLen < 1 )	/* Illegal Length,we need to calculate it	*/
		{
			InLen = strlen ( pInText );
		}

		/* Now,we can convert	*/
		InByteIndex = 0;	/* from start	*/
		while ( InByteIndex < InLen )
		{
			/* Read valid data to 4-byte Group	*/
			for ( GroupIndex = 0; GroupIndex < 4; GroupIndex ++ )
			{
				while ( InByteIndex < InLen && (DB_IsValid( *pInText , &CharValue) == FAILURE) )
				{
					if ( CharValue == '=' )	/* this is the end of input text	*/
					{
						InByteIndex = InLen;
						break;
					}
					InByteIndex ++;
					pInText ++;
				}
				if ( InByteIndex >= InLen )
				{
					break;
				}
				Group[GroupIndex] = CharValue;
				InByteIndex ++;
				pInText ++;
			}
			
			switch ( GroupIndex )	
			{
			
			case 2:	/* Only 2 bytes are valid	*/
				*pOutText = ( Group[0] << 2 ) & 0xFC | ( Group[1] >> 4 ) & 0x03;
				(*UsedBufLen) ++;
				break;
			case 3:	/* Only 3 bytes are valid	*/
				*(pOutText ++) = ( Group[0] << 2 ) & 0xFC | ( Group[1] >> 4 ) & 0x03;
				*pOutText = ( Group[1] << 4 ) & 0xF0 | ( Group[2] >> 2 ) & 0x0F;
				(*UsedBufLen) += 2;
				break;
			case 4:	/* Read successfully	*/
				/* convert 4-byte group to 3-byte group	*/
				*(pOutText ++) = ( Group[0] << 2 ) & 0xFC | ( Group[1] >> 4 ) & 0x03;
				*(pOutText ++) = ( Group[1] << 4 ) & 0xF0 | ( Group[2] >> 2 ) & 0x0F;
				*(pOutText ++) = ( Group[2] << 6 ) & 0xC0 | Group[3] & 0x3F;
				(*UsedBufLen) += 3;
				break;
			default:/* Illegal data	*/
				break;
			}
		}
		IsOK = SUCCESS;
	}
	
	return IsOK;
}

⌨️ 快捷键说明

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