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

📄 pgpclientencode.c

📁 vc环境下的pgp源码
💻 C
📖 第 1 页 / 共 4 页
字号:
/*____________________________________________________________________________
	pgpClientEncode.c
	
	Copyright (C) 1997 Network Associates Inc. and affiliated companies.
	All rights reserved.

	$Id: pgpClientEncode.c,v 1.102.6.1 1999/06/11 06:14:33 heller Exp $
____________________________________________________________________________*/

#include <string.h>

#include "pgpSDKBuildFlags.h"

#include "pgpErrors.h"
#include "pgpFileSpec.h"
#include "pgpMem.h"

#include "pgpContext.h"
#include "pgpEncode.h"
#include "pgpEncodePriv.h"
#include "pgpKeys.h"
#include "pgpOptionList.h"
#include "pgpOpaqueStructs.h"
#include "pgpKDBInt.h"
#include "pgpHashPriv.h"
#include "pgpSymmetricCipherPriv.h"

#if PGP_MACINTOSH
#include "MacFiles.h"
#endif

	static PGPError
pgpEncode(
	PGPContextRef		context,
	PGPOptionListRef 	firstOption,
	va_list				args)
{
	PGPError			err;
	PGPOptionListRef	optionList;
	
	pgpAssert( pgpContextIsValid( context ) );
	
	optionList = pgpBuildOptionListArgs( context, FALSE, firstOption, args );
	
	err = pgpGetOptionListError( optionList );
	if( IsntPGPError( err ) )
	{
		err = pgpEncodeInternal( context, optionList );
	}
	
	PGPFreeOptionList( optionList );

	return( err );
}

	PGPError
PGPEncode(
	PGPContextRef		context,
	PGPOptionListRef 	firstOption,
	...)
{
	PGPError	err;
	va_list		args;
	
	pgpAssert( pgpContextIsValid( context ) );
	
	if( pgpContextIsValid( context ) )
	{
		va_start( args, firstOption );
			err = pgpEncode( context, firstOption, args );
		va_end( args );
	}
	else
	{
		va_start( args, firstOption );
		pgpFreeVarArgOptionList( firstOption, args);
		va_end( args );
		
		err = kPGPError_BadParams;
	}
	
	return( err );
}

	static PGPError
pgpDecode(
	PGPContextRef		context,
	PGPOptionListRef	firstOption,
	va_list				args)
{
	PGPError			err;
	PGPOptionListRef	optionList;
	
	pgpAssert( pgpContextIsValid( context ) );
	
	optionList = pgpBuildOptionListArgs( context, FALSE, firstOption, args );
	
	err = pgpGetOptionListError( optionList );
	if( IsntPGPError( err ) )
	{
		err = pgpDecodeInternal( context, optionList );
		
	}
	
	PGPFreeOptionList( optionList );
	
	return( err );
}

	PGPError
PGPDecode(
	PGPContextRef 		context,
	PGPOptionListRef	firstOption,
	...)
{
	PGPError	err;
	va_list		args;
	
	pgpAssert( pgpContextIsValid( context ) );
	
	if( pgpContextIsValid( context ) )
	{
		va_start( args, firstOption );
			err = pgpDecode( context, firstOption, args );
		va_end( args );
	}
	else
	{
		va_start( args, firstOption );
		pgpFreeVarArgOptionList( firstOption, args);
		va_end( args );
		
		err = kPGPError_BadParams;
	}
	
	return( err );
}

	PGPError
PGPNewOptionList(
	PGPContextRef		context,
	PGPOptionListRef *	outList )
{
	PGPOptionListRef	optionList;
	PGPError			err	= kPGPError_NoErr;
	
	PGPValidatePtr( outList );
	*outList	= kInvalidPGPOptionListRef;
	PGPValidateContext( context );
	
	/* Option lists built with PGPNewOptionList are always persistent */
	optionList = pgpNewOptionList( context, TRUE );
	if ( pgpOptionListIsReal( optionList ) )
	{
		*outList	= optionList;
	}
	else
	{
		err	= pgpOptionListToError( optionList );
	}
	
	return( err );
}

	PGPError
PGPAddJobOptions(
	PGPJobRef			job,
	PGPOptionListRef	firstOption,
	...)
{
	PGPError	err	= kPGPError_NoErr;
	va_list		args;
		
	PGPValidatePtr( job );
	
	va_start( args, firstOption );
		err = pgpAddJobOptionsArgs( job, firstOption, args );
	va_end( args );
	
	return( err );
}

	PGPError
PGPAppendOptionList(
	PGPOptionListRef	optionList,
	PGPOptionListRef	firstOption,
	...) 
{
	PGPError	err = kPGPError_NoErr;
	
	/* don't validate 'optionList' here */
	{
		va_list	args;
		
		va_start( args, firstOption );
			err = pgpAppendOptionListArgs( optionList, firstOption, args );
		va_end( args );
	}
	
	return( err );
}

	PGPError
PGPBuildOptionList(
	PGPContextRef		context,
	PGPOptionListRef *	outList,
	PGPOptionListRef firstOption,
	...) 
{
	PGPOptionListRef	optionList;
	PGPError			err	= kPGPError_NoErr;

	PGPValidatePtr( outList );
	*outList	= NULL;
	PGPValidateContext( context );
	
	/* don't validate 'firstOption' here */
	{
		va_list	args;
		
		/* Option lists built with pgpBuildOptionListInternal are
		   always persistent */
		va_start( args, firstOption );
			optionList = pgpBuildOptionListArgs( context,
				TRUE, firstOption, args );
		va_end( args );
	}
	
	if ( pgpOptionListIsReal( optionList ) )
	{
		*outList	= optionList;
	}
	else
	{
		err	= pgpOptionListToError( optionList );
	}
	
	return( err );
}

	PGPError
PGPCopyOptionList(
	PGPOptionListRef	optionList,
	PGPOptionListRef *		outList
	) 
{
	PGPOptionListRef	newOptionList;
	PGPError			err	= kPGPError_NoErr;
	
	PGPValidatePtr( outList );
	*outList	= NULL;
	PGPValidateParam( pgpOptionListIsValid( optionList ) );
	
	newOptionList = pgpCopyOptionList( optionList );
	
	if ( pgpOptionListIsReal( newOptionList ) )
	{
		*outList	= newOptionList;
	}
	else
	{
		err	= pgpOptionListToError( newOptionList );
	}
	
	return( err );
}

	PGPError
PGPFreeOptionList(PGPOptionListRef optionList)
{
	PGPError	err = kPGPError_NoErr;
	
	PGPValidateParam( pgpOptionListIsValid( optionList ) );
	
	pgpFreeOptionList( optionList );
	
	return( err );
}



/*
**	This is the handler proc for standard allocated options.
**	The free procedure calls pgpContextMemFree() to release
**	the allocation and the copy procedure calls pgpContextMemAlloc()
**	to create a copy.
*/

	static PGPError
AllocatedOptionHandlerProc(
	PGPContextRef				context,
	PGPOptionHandlerOperation 	operation,
	PGPOptionType				type,
	PGPOptionValue				inputValue,
	PGPSize 					inputValueSize,
	PGPOptionValue 				*outputValue,
	PGPSize						*outputValueSize)
{
	PGPError	err = kPGPError_NoErr;
	
	switch( operation )
	{
		case kPGPOptionHandler_FreeDataOperation:
		{
			if( type == kPGPOptionType_AdditionalRecipientRequestKeySet ||
				type == kPGPOptionType_RevocationKeySet ) {
				PGPOAdditionalRecipientRequestKeySetDesc	*descriptor;
				descriptor = (PGPOAdditionalRecipientRequestKeySetDesc *)
														inputValue.asPtr;
				PGPFreeKeySet( descriptor->arKeySetRef );
			}
				
			pgpContextMemFree( context, inputValue.asPtr );
			break;
		}
			
		case kPGPOptionHandler_CopyDataOperation:
		{
			pgpAssertAddrValid( outputValue, PGPOptionValue );
			pgpAssertAddrValid( outputValueSize, PGPSize );

			outputValue->asPtr = pgpContextMemAlloc( context, inputValueSize,
													 0 );
			if( IsntNull( outputValue->asPtr ) )
			{	
				pgpCopyMemory( inputValue.asPtr, outputValue->asPtr,
							   inputValueSize );
				
				if( type == kPGPOptionType_AdditionalRecipientRequestKeySet||
					type == kPGPOptionType_RevocationKeySet ) {
					PGPOAdditionalRecipientRequestKeySetDesc	*descriptor;
					descriptor = (PGPOAdditionalRecipientRequestKeySetDesc *)
															outputValue->asPtr;
					PGPIncKeySetRefCount( descriptor->arKeySetRef );
				}

				*outputValueSize = inputValueSize;
			}
			else
			{
				err = kPGPError_OutOfMemory;
			}
			
			break;
		}
		
		default:
		{
			err = kPGPError_UnknownRequest;
			break;
		}
	}

	return( err );
}

/*
**	This is the handler proc for options which don't need any
**	routine copying, but for which certain ones may need special
**	treatment.
*/

	static PGPError
SpecialOptionHandlerProc(
	PGPContextRef				context,
	PGPOptionHandlerOperation 	operation,
	PGPOptionType				type,
	PGPOptionValue				inputValue,
	PGPSize 					inputValueSize,
	PGPOptionValue 				*outputValue,
	PGPSize						*outputValueSize)
{
	PGPError	err = kPGPError_NoErr;
	
	(void) context;
	(void) inputValueSize;
	(void) outputValueSize;

	switch( operation )
	{
		case kPGPOptionHandler_FreeDataOperation:
		{
			switch( type ) {
			case kPGPOptionType_EncryptToKey:
			case kPGPOptionType_SignWithKey:
			case kPGPOptionType_KeyGenMasterKey:
				pgpFreeKey( inputValue.asKeyRef );
				break;
			case kPGPOptionType_EncryptToKeySet:
			case kPGPOptionType_KeySetRef:
			case kPGPOptionType_ImportKeysTo:
				PGPFreeKeySet( inputValue.asKeySetRef );
				break;
			case kPGPOptionType_EncryptToUserID:
				pgpFreeUserID( inputValue.asUserIDRef );
				break;
			default:
				break;
			}
			break;
		}
			
		case kPGPOptionHandler_CopyDataOperation:
		{
			switch( type ) {
			case kPGPOptionType_EncryptToKey:
			case kPGPOptionType_SignWithKey:
			case kPGPOptionType_KeyGenMasterKey:
				pgpIncKeyRefCount( outputValue->asKeyRef );
				break;
			case kPGPOptionType_EncryptToKeySet:
			case kPGPOptionType_KeySetRef:
			case kPGPOptionType_ImportKeysTo:
				PGPIncKeySetRefCount( outputValue->asKeySetRef );
				break;
			case kPGPOptionType_EncryptToUserID:
				pgpIncUserIDRefCount( outputValue->asUserIDRef );
				break;
			default:
				break;
			}
			break;
		}
		
		default:
		{
			err = kPGPError_UnknownRequest;
			break;
		}
	}

	return( err );
}


	
	PGPOptionListRef
PGPOLastOption( PGPContextRef context )
{
	pgpValidateOptionContext( context );

	return( kPGPEndOfArgsOptionListRef );
}

	PGPOptionListRef
PGPONullOption( PGPContextRef context )
{
	pgpValidateOptionContext( context );

	return( kPGPNullOptionListRef );
}

	PGPOptionListRef
PGPOInputFile(
	PGPContextRef 	context,
	PGPFileSpecRef fileRef)
{
	pgpValidateOptionContext( context );
	pgpValidateOptionParam( pflFileSpecIsValid( (PFLFileSpecRef)fileRef ) );
	
	return( pgpCreateFileRefOptionList( context,
				kPGPOptionType_InputFileRef, fileRef ) );
}

#if PGP_MACINTOSH	/* [ */

	PGPOptionListRef
PGPOInputFileFSSpec(
	PGPContextRef 	context,
	const FSSpec	*fileSpec)
{
	PGPOptionListRef	optionList;
	PGPFileSpecRef		fileRef;
	
	pgpValidateOptionContext( context );
	pgpValidateOptionParam( FSSpecIsValid( fileSpec ) );
	
	if( IsntPGPError( PGPNewFileSpecFromFSSpec( context,
			fileSpec, &fileRef ) ) )
	{
		optionList = pgpCreateFileRefOptionList( context,
			kPGPOptionType_InputFileRef, fileRef );
			
		PGPFreeFileSpec( fileRef );
	}
	else
	{
		optionList = kPGPOutOfMemoryOptionListRef;
	}
	
	return( optionList );
}

#endif	/* ] */

	PGPOptionListRef
PGPOInputBuffer(
	PGPContextRef	context,
	void const *	buffer,
	PGPSize			bufferSize)
{
	PGPOptionListRef	optionList;
	PGPOptionValue		value;
	
	pgpValidateOptionContext( context );
	pgpValidateOptionParam( IsntNull( buffer ) );
	/* buffer size may be 0 */
	
	value.asConstPtr	= buffer;
	optionList = pgpCreateStandardValueOptionList( context, 
						kPGPOptionType_InputBuffer,
						&value, bufferSize, NULL );

	return( optionList );
}

	PGPOptionListRef
PGPOOutputFile(
	PGPContextRef	context,
	PGPFileSpecRef fileRef)
{
	pgpValidateOptionContext( context );
	pgpValidateOptionParam( pflFileSpecIsValid( (PFLFileSpecRef)fileRef ) );
	
	return( pgpCreateFileRefOptionList( context,
				kPGPOptionType_OutputFileRef, fileRef ) );
}

	PGPOptionListRef
PGPOOutputBuffer(
	PGPContextRef	context,
	void			*buffer,
	PGPSize			bufferSize,
	PGPSize			*outputDataLength)
{
	PGPOptionListRef		optionList;
	PGPOOutputBufferDesc	*descriptor;
	
	pgpValidateOptionParam( IsntNull( outputDataLength ) );
	*outputDataLength	= 0;
	pgpValidateOptionContext( context );
	pgpValidateOptionParam( IsntNull( buffer ) && bufferSize > 0 );

	pgpDebugWhackMemory( buffer, bufferSize );
	
	descriptor = (PGPOOutputBufferDesc *)
		pgpContextMemAlloc( context, sizeof(*descriptor),
		kPGPMemoryMgrFlags_Clear);

⌨️ 快捷键说明

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