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

📄 pgpber.c

📁 可以实现对邮件的加密解密以及签名
💻 C
📖 第 1 页 / 共 3 页
字号:
	if( sign )
	{
		/* Sign extend */
		for( i = length; i < sizeof( PGPInt32 ); i++)
			*outInt |= ( 0xFF << ( i * 8 ) );
	}

error:
	return err;
}

	PGPError
pgpBERReadString(
	PGPByte *			s,
	PGPSize				length,
	PGPByte *			outString )
{
	PGPValidatePtr( s );
	PGPValidatePtr( outString );

	pgpCopyMemory( s, outString, length );

	return kPGPError_NoErr;
}

	PGPError
PGPberRewind(
	PGPberElementRef	ber )
{
	PGPValidatePtr( ber );

	return PGPberSetIndex( ber, 0 );
}

	PGPError
PGPberPeek(
	PGPberElementRef	ber,
	PGPberType *		tag,
	PGPSize *			len )
{
	PGPByte *			string		= NULL;
	PGPUInt32			unitLength	= 0;
	PGPError			err			= kPGPError_NoErr;

	PGPValidateBERElementRef( ber );
	PGPValidatePtr( tag );
	PGPValidatePtr( len );

	if( ber->nextReadIndex == ber->length )
	{
		err = kPGPError_EndOfIteration;
		goto done;
	}

	string = ber->encoding;
	string += ber->nextReadIndex;

	err = pgpBERReadTag( string, tag, (PGPSize *)&unitLength ); CKERR;
	string += unitLength;

	err = pgpBERReadLength( string, len, (PGPSize *)&unitLength ); CKERR;
	string += unitLength;

error:
done:
	return err;
}

	PGPError
PGPberNextPrimitive(
	PGPberElementRef	ber )
{
	PGPldapType			tag				= kPGPldapType_None;
	PGPSize				len				= 0;
	PGPSize				unitLength		= 0;
	PGPByte *			string			= NULL;
	PGPError			err				= kPGPError_NoErr;

	PGPValidateBERElementRef( ber );

	string = ber->encoding + ber->nextReadIndex;

	if( ber->nextReadIndex == ber->length )
	{
		err = kPGPError_EndOfIteration;
		goto done;
	}

	do
	{
		if( ber->nextReadIndex == ber->length )
		{
			err = kPGPError_NoErr;
			goto done;
		}

		ber->nextReadIndex = ( string - ber->encoding );

		err = pgpBERReadTag( string, (PGPberType *)&tag, &unitLength ); CKERR;
		string += unitLength;

		err = pgpBERReadLength( string, &len, &unitLength ); CKERR;
		string += unitLength;

	} while ( tag & kPGPberMask_Constructed );

error:
done:
	return err;
}

	PGPError
PGPberNextConstructed(
	PGPberElementRef	ber )
{
	PGPldapType			tag				= kPGPldapType_None;
	PGPSize				len				= 0;
	PGPSize				unitLength		= 0;
	PGPByte *			string			= NULL;
	PGPError			err				= kPGPError_NoErr;

	PGPValidateBERElementRef( ber );

	string = ber->encoding + ber->nextReadIndex;

	if( ber->nextReadIndex == ber->length )
	{
		err = kPGPError_EndOfIteration;
		goto done;
	}

	do
	{
		if( ber->nextReadIndex == ber->length )
		{
			err = kPGPError_NoErr;
			goto done;
		}

		err = pgpBERReadTag( string, (PGPberType *)&tag, &unitLength ); CKERR;
		string += unitLength;

		err = pgpBERReadLength( string, &len, &unitLength ); CKERR;
		string += unitLength;

		if( !( tag & kPGPberMask_Constructed ) )	/* primitive */
			string += len;

		ber->nextReadIndex = ( string - ber->encoding );

	} while ( !( tag & kPGPberMask_Constructed ) );

error:
done:
	return err;
}


	PGPError
PGPberNext(
	PGPberElementRef	ber )
{
	PGPldapType			tag				= kPGPldapType_None;
	PGPSize				len				= 0;
	PGPSize				unitLength		= 0;
	PGPByte *			string			= NULL;
	PGPError			err				= kPGPError_NoErr;

	PGPValidateBERElementRef( ber );

	string = ber->encoding + ber->nextReadIndex;

	if( ber->nextReadIndex == ber->length )
	{
		err = kPGPError_EndOfIteration;
		goto done;
	}

	err = pgpBERReadTag( string, (PGPberType *)&tag, &unitLength ); CKERR;
	string += unitLength;

	err = pgpBERReadLength( string, &len, &unitLength ); CKERR;
	string += unitLength;

	ber->nextReadIndex = ( string - ber->encoding );

error:
done:
	return err;
}


	PGPError
PGPberSkip(
	PGPberElementRef	ber )
{
	PGPldapType			tag				= kPGPldapType_None;
	PGPSize				len				= 0;
	PGPSize				unitLength		= 0;
	PGPByte *			string			= NULL;
	PGPError			err			= kPGPError_NoErr;

	PGPValidateBERElementRef( ber );

	if( ber->nextReadIndex == ber->length )
	{
		err = kPGPError_EndOfIteration;
		goto done;
	}

	string = ber->encoding + ber->nextReadIndex;

	err = pgpBERReadTag( string, (PGPberType *)&tag, &unitLength ); CKERR;
	string += unitLength;

	err = pgpBERReadLength( string, &len, &unitLength ); CKERR;
	string += unitLength;

	string += len;

	ber->nextReadIndex = ( string - ber->encoding );

error:
done:
	return err;
}

	PGPError
pgpBERFormatSpecifierToTag(
	PGPberFormatSpecifier		fmt,
	PGPberType *				berType )
{
	PGPError					err	= kPGPError_NoErr;

	PGPValidatePtr( berType );

	/* This conversion will only work with primitive BER types. */

	switch( fmt )
	{
		case kPGPberFormatSpecifier_Boolean:
			*berType = kPGPberType_Boolean;
			break;

		case kPGPberFormatSpecifier_Int:
			*berType = kPGPberType_Int;
			break;

		case kPGPberFormatSpecifier_Octetstring:
		case kPGPberFormatSpecifier_String:
			*berType = kPGPberType_Octetstring;
			break;

		case kPGPberFormatSpecifier_NULL:
			*berType = kPGPberType_NULL;
			break;

		case kPGPberFormatSpecifier_Enumeration:
			*berType = kPGPberType_Enumeration;
			break;

		default:
			*berType = kPGPberType_None;
			err = kPGPError_BadParams;
			break;
	}

	return err;
}

	PGPError
PGPberRead(
	PGPberElementRef	ber,
	const char *		fmt,
	... )
{
	va_list				argList;
	PGPUInt32			fmtIndex	= 0;
	PGPBoolean			nextIndex	= FALSE;
	PGPError			err 		= kPGPError_NoErr;
	PGPberType			tag			= kPGPberType_None;
	PGPberType			switchTag	= kPGPberType_None;
	PGPSize				length		= 0;
	PGPUInt32			unitLength	= 0;
	PGPByte	*			string		= NULL;

	PGPInt32 *			outInt		= NULL;
	PGPldapType *		outTag		= NULL;
	PGPByte *			outString	= NULL;
	PGPBoolean *		outBoolean	= NULL;

	PGPValidateBERElementRef( ber );
	PGPValidatePtr( fmt );

	va_start( argList, fmt );

	string = ber->encoding;
	string += ber->nextReadIndex;

	/* Run through the ber encoded string and look for elements */
	/*
	 * For each element, we need to look at:
	 * 1.	the tag
	 * 2.	the length
	 * 3.	the contents
	 */
	while(	( string < ( ber->encoding + ber->length ) ) && 
			( fmt[fmtIndex] != '\0' ) )
	{
		err = pgpBERReadTag( string, (PGPberType *)&tag, (PGPSize *)&unitLength ); CKERR;
		string += unitLength;

		err = pgpBERReadLength( string, &length, (PGPSize *)&unitLength ); CKERR;
		string += unitLength;

		if( length > (PGPUInt32)( ber->encoding + ber->length - string ) )
		{
			err = kPGPError_CorruptData;
			goto error;
		}

		switchTag = tag;
		if( fmt[fmtIndex] == kPGPberFormatSpecifier_Force )
		{
			/*
			 * We expect this to be a certain type, but the tag may not
			 * indicate the correct type.  We need to look at the format
			 * specifier and see what we think it should be.
			 */
			fmtIndex++;
			if( fmt[fmtIndex] == '\0' )
			{
				err = kPGPError_BadParams;
				CKERR;
			}

			err = pgpBERFormatSpecifierToTag( (PGPberFormatSpecifier)fmt[fmtIndex],
						&switchTag ); CKERR;
		}

		switch( switchTag )
		{
			case kPGPberType_Boolean:
				if( fmt[fmtIndex] == kPGPberFormatSpecifier_Boolean )
				{
					nextIndex = TRUE;

					outBoolean = va_arg( argList, PGPBoolean * );
					err = pgpBERReadInt( string, length, (PGPInt32 *) outBoolean ); CKERR;
				}
				
				string += length;

				break;

			case kPGPberType_Int:
				if( fmt[fmtIndex] == kPGPberFormatSpecifier_Int )
				{
					nextIndex = TRUE;

					outInt = va_arg( argList, PGPInt32 * );
					err = pgpBERReadInt( string, length, outInt ); CKERR;
				}

				string += length;

				break;

			case kPGPberType_Octetstring:
				if( ( fmt[fmtIndex] == kPGPberFormatSpecifier_String ) ||
					( fmt[fmtIndex] == kPGPberFormatSpecifier_Octetstring) )
				{
					nextIndex = TRUE;

					outString = va_arg( argList, PGPByte * );
					err = pgpBERReadString( string, length, outString ); CKERR;
				
					if( fmt[fmtIndex] == kPGPberFormatSpecifier_String )
						outString[length] = '\0';
				}

				string += length;

				break;

			case kPGPberType_NULL:
				/*
				 * There's really nothing to read, so we'll just skip the
				 * NULLs 
				 */
				if( fmt[fmtIndex] == kPGPberFormatSpecifier_NULL )
					nextIndex = TRUE;

				string += length;

				break;

			case kPGPberType_Enumeration:
				if( fmt[fmtIndex] == kPGPberFormatSpecifier_Enumeration )
				{
					nextIndex = TRUE;

					outInt = va_arg( argList, PGPInt32 * );
					err = pgpBERReadInt( string, length, outInt ); CKERR;
				}

				string += length;

				break;

			case kPGPberType_Sequence:
				if( fmt[fmtIndex] == kPGPberFormatSpecifier_BeginSequence )
					nextIndex = TRUE;

				break;

			case kPGPberType_Set:
				if( fmt[fmtIndex] == kPGPberFormatSpecifier_BeginSet )
					nextIndex = TRUE;

				break;

			default:
				break;
		}

		if( fmt[fmtIndex] == kPGPberFormatSpecifier_Tag )
		{
			outTag = va_arg( argList, PGPldapType * );
			*outTag = (PGPldapType)tag;

			nextIndex = TRUE;
		}

		if( nextIndex )
			fmtIndex++;
		nextIndex = FALSE;
	}

	ber->nextReadIndex = ( string - ber->encoding );

	va_end( argList );

error:
	return err;
}

	PGPError
PGPberGetLength(
	PGPberElementRef	ber,
	PGPSize *			length )
{
	PGPValidateBERElementRef( ber );
	PGPValidatePtr( length );

	*length = ber->length;

	return kPGPError_NoErr;
}

	PGPError
PGPberGetEncoding(
	PGPberElementRef	ber,
	PGPByte **			encoding )
{
	PGPValidateBERElementRef( ber );
	PGPValidatePtr( encoding );

	*encoding = ber->encoding;

	return kPGPError_NoErr;
}

	PGPError
PGPberGetIndex(
	PGPberElementRef	ber,
	PGPUInt32 *			index )
{
	PGPValidateBERElementRef( ber );
	PGPValidatePtr( index );

	*index = ber->nextReadIndex;

	return kPGPError_NoErr;
}

	PGPError
PGPberSetIndex(
	PGPberElementRef	ber,
	PGPUInt32			index )
{
	PGPValidateBERElementRef( ber );

	ber->nextReadIndex = index;

	return kPGPError_NoErr;
}


/*__Editor_settings____

	Local Variables:
	tab-width: 4
	End:
	vi: ts=4 sw=4
	vim: si
_____________________*/

⌨️ 快捷键说明

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