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

📄 pgpcontext.c

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 C
📖 第 1 页 / 共 2 页
字号:
	
	PGPValidateContext( context );
	
	pgpEnterPGPErrorFunction();

	context->userValue = userValue;
	
	return( err );
}


	PGPConnectRef
pgpContextGetConnectRef(
	PGPContextRef 	context )
{
#if PGP_MACINTOSH
	ProcessSerialNumber	processSerial;
	
	context;
	GetCurrentProcess(&processSerial);
	return (PGPConnectRef)processSerial.lowLongOfPSN;
#else
	pgpAssert( pgpContextIsValid( context ) );
	return context->connect_ref;
#endif
}

	void
pgpContextSetConnectRef(
	PGPContextRef 	context,
	PGPConnectRef	connect_ref )
{
#if PGP_MACINTOSH
	context;	connect_ref;
	pgpDebugMsg( "Connection Refs cannot be set manually on the Macintosh" );
#else
	pgpAssert( pgpContextIsValid( context ) );
	context->connect_ref = connect_ref;
#endif
}

	PGPUInt32
pgpContextGetThreadID(
	PGPContextRef	context )
{
	pgpAssert( pgpContextIsValid( context ) );
	return context->currentThread;
}

	struct PGPCacheHeader *
pgpContextGetPassphraseCache(
	PGPContextRef 	context )
{
	pgpAssert( pgpContextIsValid( context ) );
	return context->passphraseCache;
}

	void
pgpContextSetPassphraseCache(
	PGPContextRef 			context,
	struct PGPCacheHeader *	passphraseCache )
{
	pgpAssert( pgpContextIsValid( context ) );
	context->passphraseCache = passphraseCache;
}


	PGPEnv *
pgpContextGetEnvironment(PGPContextRef context)
{
	pgpAssert( pgpContextIsValid( context ) );

	return( context->pgpEnvironment );
}

	PGPKeyDBRef
pgpContextGetFirstKeyDB(PGPContextRef context)
{
	pgpAssert( pgpContextIsValid( context ) );

	return( context->keyDB );
}

	void
pgpContextSetFirstKeyDB(PGPContextRef context, PGPKeyDBRef keyDB)
{
	pgpAssert( pgpContextIsValid( context ) );

	context->keyDB = keyDB;
}

	PGPBoolean
pgpContextGetUpdateNeeded( PGPContextRef context )
{
	pgpAssert( pgpContextIsValid( context ) );

	return context->updateNeeded;
}

	void
pgpContextSetUpdateNeeded( PGPContextRef context, PGPBoolean isNeeded )
{
	pgpAssert( pgpContextIsValid( context ) );

	context->updateNeeded = isNeeded;
}





	static PGPError
sCreateRandomContext( 
	PGPContextRef			context,
	PGPRandomVTBL const *	vtbl,
	PGPRandomContext **		outRandom )
{
	PGPError			err	= kPGPError_NoErr;
	PGPRandomContext *	newRandom	= NULL;
	
	pgpAssert( pgpContextIsValid( context ) );
	
		
	newRandom	= (PGPRandomContext *)
		pgpContextMemAlloc( context, sizeof( *newRandom ),
		kPGPMemoryMgrFlags_Clear );
	if ( IsntNull( newRandom ) )
	{
		newRandom->context	= context;
		newRandom->vtbl		= vtbl;
		newRandom->priv		= NULL;
		newRandom->destroy	= NULL;
	}
	else
	{
		err	= kPGPError_OutOfMemory;
	}

	*outRandom	= newRandom;
	return( err );
}



	PGPRandomContext *
pgpContextGetGlobalPoolRandomContext(PGPContextRef context)
{
	pgpAssert( pgpContextIsValid( context ) );
	
	if ( IsNull( context->randomPoolContext ) )
	{
		PGPRandomContext *	randomContext	= NULL;
		
		(void)sCreateRandomContext( context,
			pgpGetGlobalRandomPoolVTBL(), &randomContext );
		context->randomPoolContext	= randomContext;
		
		if ( IsntNull( context->randomPoolContext ) )
		{
			/*
			* in case there is a problem in reading the random pool,
			* feed in some amount of "random" data to avoid a totally
			* predictible pool
			*/
			pgpRandomCollectEntropy( randomContext );
			pgpRandomCollectEntropy( randomContext );
			pgpRandomCollectEntropy( randomContext );
			pgpRandomCollectEntropy( randomContext );
		}
	}

	return( context->randomPoolContext );
}



	PGPRandomContext *
pgpContextGetDummyRandomContext(PGPContextRef context)
{
	pgpAssert( pgpContextIsValid( context ) );
	
	if ( IsNull( context->dummyRandomContext ) )
	{
		(void)sCreateRandomContext( context,
			pgpGetGlobalDummyRandomPoolVTBL(),
			&context->dummyRandomContext );
	}

	return( context->dummyRandomContext );
}



	PGPRandomContext *
pgpContextGetX9_17RandomContext(PGPContextRef context)
{
	pgpAssert( pgpContextIsValid( context ) );
	
	if ( IsNull( context->randomPoolX9_17 ) )
	{
		context->randomPoolX9_17	= pgpRandomCreate( context );
	}

	return( context->randomPoolX9_17 );
}


/*____________________________________________________________________________
    Fill the user-specified buffer with random values from the global
    random pool.  If we were not able to initialize the random pool
    we return kPGPError_OutOfEntropy.
____________________________________________________________________________*/
	PGPError
PGPContextGetRandomBytes(
	PGPContextRef	context,
	void *			buf,
	PGPSize			len
	)
{
	PGPRandomContext *	randomContext;

	PGPValidatePtr( buf );
	PGPValidateParam( pgpContextIsValid( context ) );

	pgpEnterPGPErrorFunction();

	randomContext = pgpContextGetX9_17RandomContext( context );
	if( IsNull( randomContext )
		|| !PGPGlobalRandomPoolHasMinimumEntropy() )
	{
		return kPGPError_OutOfEntropy;
	}

	pgpRandomGetBytes( randomContext, buf, len );
	
	return kPGPError_NoErr;
}

/*____________________________________________________________________________
    Reserve random bytes in the front end copy (if it exists).  This will
	ensure that we have data for later random requests.  Return the number
    of bytes reserved in total so far.  To just learn that value, call this
    function with 0.
____________________________________________________________________________*/
	PGPUInt32
PGPContextReserveRandomBytes(
	PGPContextRef	context,
	PGPUInt32		minsize
	)
{
	PGPRandomContext *	randomContext;
	PGPUInt32 reserved;

	PGPValidateParam( pgpContextIsValid( context ) );

	pgpEnterPGPErrorFunction();

	randomContext = pgpContextGetX9_17RandomContext( context );
	if( IsNull( randomContext )
		|| !PGPGlobalRandomPoolHasMinimumEntropy() )
	{
		return 0;
	}

	reserved = pgpRandomReserveBytes( randomContext, minsize );
	
	return reserved;
}

#if PGP_WIN32

	static PGPError
pgpSDKInitPlatform(PGPFlags options)
{
	PGPError	err = kPGPError_NoErr, tempErr;
	
	tempErr = pgpSDKInitDriver();
//	pgpAssertNoErr( tempErr );

	/*
	 * The SDK creates a FrontEndThread whether or not it
	 * is being run in "local" mode.  When there is no SDK
	 * service, this thread is responsible for expiring
	 * the passphrase cache.  When there is an SDK service,
	 * the thread receives our key-change notifications.
	 */
	if (options & kPGPFlags_ForceLocalExecution) {
		if (!(options & kPGPFlags_SuppressCacheThread))
			pgpInitFrontEndThread(TRUE);
	}
	else {
		if (!(options & kPGPFlags_SuppressCacheThread))
			pgpInitFrontEndThread(FALSE);
		err = pgpSDKInitBackEnd( kPGPsdkBackEndAPIVersion);
	}

	return err;
}

#elif PGP_MACINTOSH	/* ][ */

	static PGPError
pgpSDKInitPlatform(PGPFlags options)
{
	PGPError	err = kPGPError_NoErr;
	
	(void) options;
	
#if PGPSDK_FRONTEND
	/*
	** Failure to load the driver is not fatal.
	** May not be enough memory is sys heap
	*/

	err = pgpSDKInitDriver();
	pgpAssertNoErr( err );
	
	err = pgpSDKInitBackEnd( kPGPsdkBackEndAPIVersion);
	if( IsntPGPError( err ) ) {
		err = pgpConnect_backRPC();
	}
#endif
	
	return err;
}

#else	/* ][ */
	static PGPError
pgpSDKInitPlatform(PGPFlags options)
{
	PGPError err = kPGPError_NoErr;

#if !PGP_UNIX_DARWIN
	if (!(options & kPGPFlags_SuppressCacheThread))
		pgpInitFrontEndThread(options & kPGPFlags_ForceLocalExecution);
#endif

	if(!(options & kPGPFlags_ForceLocalExecution))
		err = pgpSDKInitBackEnd(kPGPsdkBackEndAPIVersion);

	return err;
}
#endif	/* ] */

static PGPUInt32	sInitedCount		= 0;
static PGPBoolean	sFreeMemoryMgrList	= FALSE;

	PGPError
PGPsdkInit(PGPFlags options)
{
	PGPError	err	= kPGPError_NoErr;
	
	if ( sInitedCount == 0 )
	{
	#if PGPSDK_FRONTEND
		pgpLeaksBeginSession( "PGPsdk" );
	#else
		pgpLeaksBeginSession( "PGPsdkBackEnd" );
	#endif
	
		if( IsNull( PGPGetDefaultMemoryMgr() ) )
		{
			PGPMemoryMgrRef	memoryMgr;
			
			sFreeMemoryMgrList = TRUE;
			
			err = PGPNewMemoryMgr( 0, &memoryMgr );
			pgpAssertNoErr( err );
			if( IsntPGPError( err ) )
			{
				err = PGPSetDefaultMemoryMgr( memoryMgr );
				pgpAssertNoErr( err );
			}
		}
		
		if( IsntPGPError( err ) )
		{
			err	= pgpSDKInitPlatform( options );
			pgpAssertNoErr( err );
		}

		if( IsntPGPError( err ) )
		{
			err	= pgpInitMacBinaryMappings();
			pgpAssertNoErr( err );
		}
		
		if( IsntPGPError( err ) )
		{
			bnInit();
		}

		if( IsntPGPError( err ) )
		{
			err = pgpDefaultRandSeedFile ();
		}

		PGPMutexCreate(&gPassCacheMutex, NULL);
	}
	
	if ( IsntPGPError( err ) )
	{
		++sInitedCount;
	}
	
	return( err );
}


	PGPBoolean
pgpsdkIsInited( void )
{
	return( sInitedCount != 0 );
}

	PGPError
pgpForceSDKCleanup( void )
{
	PGPError	err	= kPGPError_NoErr;
	
	if ( sInitedCount != 0 )
	{
		sInitedCount	= 1;
	}
	
	err	= PGPsdkCleanup();
	return( err );
}

#if PGP_MACINTOSH	/* [ */

	static void
pgpSDKCleanupPlatform(void)
{
#if PGPSDK_FRONTEND
	pgpDisconnect_backRPC();
	pgpSDKCleanupBackEnd();
#endif
}

#elif PGP_OSX

	static void
pgpSDKCleanupPlatform(void)
{
	pgpSDKCleanupBackEnd();
}

#elif PGP_UNIX /* ][ */

	static void
pgpSDKCleanupPlatform(void)
{
	pgpKillFrontEndThread();
	pgpDisconnect_backRPC();
}

#else	/* ][ */

	static void
pgpSDKCleanupPlatform(void)
{
	pgpKillFrontEndThread();
	pgpDisconnect_backRPC();
}

#endif	/* ] */

	PGPError
PGPsdkCleanup( void )
{
	PGPError	err	= kPGPError_NoErr;
	
	pgpAssert( sInitedCount != 0 );
	if ( sInitedCount != 0 )
	{
		--sInitedCount;
		if ( sInitedCount == 0 )
		{
			pgpUnloadTCL();

			pgpDisposeMacBinaryMappings();
			
			pgpSDKCleanupPlatform();

			pgpCleanupRandSeedFile();

			PGPMutexDestroy(&gPassCacheMutex);            

			if( sFreeMemoryMgrList )
				pgpFreeDefaultMemoryMgrList();
			
			pgpLeaksEndSession();
		}
	}
	else
	{
		err	= kPGPError_BadParams;
	}
	
	return( err );
}

	PGPError
PGPSetNotificationCallback( PGPNotificationHandlerProc proc,
	PGPUserValue userValue )
{
	sNotificationProc = proc;
	sNotificationUserValue = userValue;
	return kPGPError_NoErr;
}

/*
 * Call from the asynchronous code which is told that a notify update is
 * ready.
 */
	void
pgpCallNotificationCallback ( PGPNotificationReason notification,
	PGPUInt32 param1, PGPUInt32 param2 )
{
	(void)pgpSetBackendUpdateNeeded();
	if( IsntNull( sNotificationProc ) )
		(*sNotificationProc) ( sNotificationUserValue, notification,
							   param1, param2 );
}


/*__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 + -