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

📄 pgpgroups.c

📁 可以实现对邮件的加密解密以及签名
💻 C
📖 第 1 页 / 共 5 页
字号:
	
	pgpEnterPGPErrorFunction();

	pgpDebugWhackMemory( item, sizeof(*item) );
	
	if ( iter->curIndex == iter->numItems )
	{
		/* already at end of list */
		err	= kPGPError_EndOfIteration;
	}
	else
	{
		if ( iter->curIndex == kBeforeFirstItemIndex )
			iter->curIndex	= 0;
		else
			iter->curIndex	+= 1;
			
		if ( iter->curIndex == iter->numItems )
			err	= kPGPError_EndOfIteration;
		else
			*item	= *(iter->items[ iter->curIndex ]);
	}
	
	
	return( err );
}





	static PGPError
sFindGroupByName(
	PGPGroupSetRef	set,
	const char *	name,
	PGPGroupID *	outID )
{
	PGPUInt32	index;
	PGPError	err	= kPGPError_ItemNotFound;
	
	*outID	= kPGPInvalidGroupID;
	
	for( index = 0; index < set->numGroups; ++index )
	{
		PGPGroup const *	group;
		
		group	= &set->groupArray[ index ];
		if ( pgpCompareStringsIgnoreCase( group->name, name ) == 0 )
		{
			*outID	= group->id;
			err	= kPGPError_NoErr;
			break;
		}
	}
	
	return( err );
}



/*____________________________________________________________________________
	Recursively add a group into the destination set, merging all its
	entries that don't exist in the destination.
____________________________________________________________________________*/
	static PGPError
sAddGroupIntoDifferentSet(
	PGPGroup const *	srcGroup,
	PGPGroupSetRef		destSet,
	PGPGroupID *		resultID )
{
	PGPError	err		= kPGPError_NoErr;
	PGPGroupID	destID	= kPGPInvalidGroupID;
	
	/* first make sure the group exists in destination set */
	if ( sFindGroupByName( destSet,
			srcGroup->name, &destID ) == kPGPError_ItemNotFound )
	{
		err	= PGPNewGroup( destSet,
				srcGroup->name, srcGroup->description, &destID);
	}
	if ( IsntPGPError( err ) )
	{
		PGPUInt32	itemIndex;
	
		/* loop over all items in srcGroup */
		for( itemIndex = 0; itemIndex < srcGroup->numItems; ++itemIndex )
		{
			PGPGroupItem const *	item;
			
			item	= &srcGroup->itemsArray[ itemIndex ];
			if ( item->type == kPGPGroupItem_Group )
			{
				/* recursively add subgroup */
				PGPGroup *		subGroup;
				PGPGroupID		newID;
				PGPGroupItem	newItem;
				
				err	= sFindGroupByID( srcGroup->set,
							item->u.groupID, &subGroup );
				if ( IsPGPError( err ) )
					break;
					
				err	= sAddGroupIntoDifferentSet( subGroup, destSet, &newID );
				if ( IsPGPError( err ) )
					break;
					
				/* now add the newly created group to this group */
				newItem.type		= kPGPGroupItem_Group;
				newItem.userValue	= 0;
				newItem.u.groupID	= newID;
				item				= &newItem;
			}
			
			/* add the key or group to dest set if it doesn't already exist */
			err	= PGPAddItemToGroup( destSet, item, destID );
			if ( err == kPGPError_ItemAlreadyExists )
				err	= kPGPError_NoErr;
				
			if ( IsPGPError( err ) )
				break;
		}
	}
	
	*resultID	= destID;
	return( err );
}


/*____________________________________________________________________________
	Merge a group from one set into another.  If a group with the same name
	exists, items found in the source set are all added (if not already there)
	into the destination set.
	
	Recursively adds everything.
____________________________________________________________________________*/
	PGPError
PGPMergeGroupIntoDifferentSet(
	PGPGroupSetRef		fromSet,
	PGPGroupID			fromID,
	PGPGroupSetRef		toSet )
{
	PGPError			err	= kPGPError_NoErr;
	PGPGroup const *	group	= NULL;
	
	PGPValidateGroupSet( fromSet );
	PGPValidateGroupSet( toSet );
	
	pgpEnterPGPErrorFunction();

	err	= sFindGroupByID( fromSet, fromID, (PGPGroup **)&group );
	if ( IsntPGPError( err ) )
	{
		PGPGroupID	newID;
		
		err	= sAddGroupIntoDifferentSet( group, toSet, &newID );
	}
	
	return( err );
}


	PGPError
PGPMergeGroupSets(
	PGPGroupSetRef	fromSet,
	PGPGroupSetRef	intoSet )
{
	PGPUInt32	index;
	PGPError	err	= kPGPError_NoErr;
	
	PGPValidateGroupSet( fromSet );
	PGPValidateGroupSet( intoSet );
	
	pgpEnterPGPErrorFunction();

	for( index = 0; index < fromSet->numGroups; ++index )
	{
		PGPGroupID	fromID;
		
		err	= PGPGetIndGroupID( fromSet, index, &fromID );
		if ( IsPGPError( err ) )
			break;
		
		err	= PGPMergeGroupIntoDifferentSet( fromSet, fromID, intoSet );
		if ( IsPGPError( err ) )
			break;
	}
	
	return( err );
}

	PGPError
PGPCopyGroupSet(
	PGPGroupSetRef	sourceSet,
	PGPGroupSetRef	*destSet)
{
	PGPError	err;
	
	PGPValidateGroupSet( sourceSet );
	PGPValidatePtr( destSet );
	
	pgpEnterPGPErrorFunction();

	err = PGPNewGroupSet( PGPGetGroupSetContext( sourceSet ), destSet );
	if( IsntPGPError( err ) )
	{
		err = PGPMergeGroupSets( sourceSet, *destSet );
		if( IsPGPError( err ) )
		{
			(void) PGPFreeGroupSet( *destSet );
			*destSet = kInvalidPGPGroupSetRef;
		}
	}
	
	return( err );
}

#if PGP_DEBUG /* [ */
#if PGP_MACINTOSH

	static PGPError
sTestCreate(
	PGPContextRef		context,
	PGPGroupSetRef *	outSet,
	PGPGroupID *		outMaster )
{
	PGPError		err	= kPGPError_NoErr;
	PGPGroupSetRef	set	= NULL;
	
	*outSet		= NULL;
	*outMaster	= kPGPInvalidGroupID;
			
	err	= PGPNewGroupSet( context, &set );
	if ( IsntPGPError( err ) )
	{
		PGPUInt32		index;
		PGPUInt32		kNumTestGroups	= 500;
		PGPBoolean		needsCommit;
		PGPGroupItem	item;
		PGPGroupID		masterID;
		PGPUInt32		numKeys;
		PGPUInt32		numItems;
		
		*outSet	= set;
		
		needsCommit	= PGPGroupSetNeedsCommit( set );
		pgpAssert( needsCommit );
			
		for( index = 0; index < kNumTestGroups; ++index )
		{
			PGPGroupID	newID;
			Str255		pStr;
			char		name[ 256 ];
			
			NumToString( index, pStr );
			PToCString( pStr, name );
			
			err	= PGPNewGroup( set, name, "test group", &newID );
			if ( IsPGPError( err ))
				break;
				
			/* add a key to the gruop */
			pgpClearMemory( &item, sizeof( item ));
			item.type	= kPGPGroupItem_KeyID;
			err	= PGPAddItemToGroup( set, &item, newID );
			if ( IsPGPError( err ))
				break;
		}
		
		/* add all existing groups to the master group */
		if ( IsntPGPError( err ) )
		{
			PGPUInt32	numGroups;
			
			err	= PGPCountGroupsInSet( set, &numGroups );
			pgpAssertNoErr( err );
			
			if ( IsntPGPError( err ) )
			{
				err	= PGPNewGroup( set, "master group",
							"contains all the other groups",
							&masterID);
				pgpAssertNoErr( err );
			}
		
			if ( IsntPGPError( err ) )
			{
				*outMaster	= masterID;
				
				for( index = 0; index < numGroups; ++index )
				{
					PGPGroupID	id;
					
					err	= PGPGetIndGroupID( set, index, &id);
					pgpAssertNoErr( err );
					if ( IsPGPError( err ))
						break;
						
					item.type		= kPGPGroupItem_Group;
					item.u.groupID	= id;
					err	= PGPAddItemToGroup( set, &item, masterID );
					pgpAssertNoErr( err );
					if ( IsPGPError( err ))
						break;
				}
			
				if ( IsntPGPError( err ) )
				{
					err	= PGPCountGroupItems( set, masterID, TRUE,
							&numKeys, &numItems );
					pgpAssertNoErr( err );
					pgpAssert( numItems - numKeys == numGroups );
				}
			}
		}
		
		if ( IsPGPError( err ) )
		{
			PGPFreeGroupSet( set );
			*outSet	= NULL;
		}
	}
	return( err );
}


	static PGPError
sIteratorTest(
	PGPGroupSetRef		set,
	PGPGroupID			id )
{
	PGPError			err	= kPGPError_NoErr;
	PGPGroupItemIterRef	iter;
	PGPGroupItem		item;
	
	/* test iterators */
	err	= PGPNewGroupItemIter( set, id, kPGPGroupIterFlags_AllItems, &iter );
	pgpAssertNoErr( err );
	while ( IsntPGPError( err = PGPGroupItemIterNext( iter, &item ) ) )
	{
		/* just iterate through all */
	}
	pgpAssert( err == kPGPError_EndOfIteration );
	err	= PGPFreeGroupItemIter( iter );
	pgpAssertNoErr( err );

	/* now do it recursively */
	err	= PGPNewGroupItemIter( set, id,
				kPGPGroupIterFlags_AllRecursive, &iter);
	pgpAssertNoErr( err );
	while ( IsntPGPError( err = PGPGroupItemIterNext( iter, &item ) ) )
	{
		/* just iterate through all */
	}
	pgpAssert( err == kPGPError_EndOfIteration );
	err	= PGPFreeGroupItemIter( iter );
	pgpAssertNoErr( err );
	
	return( err );
}


	static PGPError
sSaveRestoreTest( PGPGroupSetRef 	set )
{
	FSSpec			fsSpec;
	PGPFileSpecRef	fileSpec	= NULL;
	PGPError		err			= kPGPError_NoErr;
	PGPContextRef	context		= PGPGetGroupSetContext( set );
	
#if PGP_MACINTOSH
	pgpAssert( PGPGroupSetNeedsCommit( set ) );
	FSMakeFSSpec( -1, fsRtDirID, "\pGroups Test", &fsSpec );
	err	= PGPNewFileSpecFromFSSpec( context, &fsSpec, &fileSpec );
	pgpAssertNoErr( err );
#else
	err	= kPGPError_FileNotFound;
#endif

	if ( IsntPGPError( err ) )
		err	= PGPSaveGroupSetToFile( set, fileSpec );
	pgpAssertNoErr( err );
	if ( IsntPGPError( err ) )
	{
		PGPGroupSetRef	tempSet;
		
		err	= PGPNewGroupSetFromFile( context, fileSpec, &tempSet );
		pgpAssertNoErr( err );
		
		err	= PGPFreeGroupSet( tempSet );
		pgpAssertNoErr( err );
	}

	PGPFreeFileSpec( fileSpec );
	
	return( err );
}

	static PGPError
sMergeTest( PGPGroupSetRef set )
{
	PGPError		err	= kPGPError_NoErr;
	PGPGroupSetRef	toSet;
	PGPUInt32		numGroups;
	PGPUInt32		index;
	
	/* copy the entire group into another group */
	err	= PGPCountGroupsInSet( set, &numGroups );
	pgpAssertNoErr( err );
	if ( IsntPGPError( err ) )
	{
		err	= PGPNewGroupSet( PGPGetGroupSetContext(set), &toSet );
		pgpAssertNoErr( err );
		if ( IsntPGPError( err ) )
		{
			for( index = 0; index < numGroups; ++index )
			{
				PGPGroupID	id;
				
				err	= PGPGetIndGroupID( set, index, &id );
				pgpAssertNoErr( err );
				if ( IsPGPError( err ) )
					break;
				
				err	= PGPMergeGroupIntoDifferentSet( set, id, toSet );
				pgpAssertNoErr( err );
				if ( IsPGPError( err ) )
					break;
			}
			err	= PGPFreeGroupSet( toSet );
			pgpAssertNoErr( err );
		}
	}
	
	return( err );
}


	static PGPError
sDeleteTest( PGPGroupSetRef set )
{
	PGPError	err	= kPGPError_NoErr;
	PGPUInt32	index;
	PGPUInt32	numGroups;
	
	err	= PGPCountGroupsInSet( set, &numGroups );
	
	for( index = 0; index < numGroups; ++index )
	{
		PGPGroupID	id;
		
		err	= PGPGetIndGroupID( set, 0, &id );
		pgpAssertNoErr( err );
		if ( IsntPGPError( err ) )
		{
			err	= PGPDeleteGroup( set, id );
			pgpAssertNoErr( err );
		}
	}
	return( err );
}


	static PGPError
sMiscTest( PGPGroupSetRef set )
{
	PGPError	err	= kPGPError_NoErr;
	PGPGroupID	id;
	PGPUInt32	numGroups;
	
	err	= PGPCountGroupsInSet( set, &numGroups );
	pgpAssertNoErr( err );
	if ( IsntPGPError( err ) )
	{
		PGPUInt32		index;
		PGPGroupInfo	info;
		
		for( index = 0; index < numGroups; ++index )
		{
			err	= PGPGetIndGroupID( set, 0, &id );
			if (IsntPGPError( err ) )
			{
				err	= PGPGetGroupInfo( set, id, &info );
				pgpAssertNoErr( err );
			}
		}
	}
	
	return( err );
}

	void
TestGroups()
{
	PGPGroupSetRef			set;
	PGPError				err;
	PGPContextRef			context;
	PGPCustomContextInfo	info;
	
	pgpClearMemory( &info, sizeof( info ) );
	
	info.sdkAPIVersion 	= kPGPsdkAPIVersion;
	info.memoryMgr		= PGPGetDefaultMemoryMgr();
	
	err = PGPNewContextCustom( &info, &context );
	if ( IsntPGPError( err ) )
	{
		PGPGroupID			masterID;
			
		err	= sTestCreate( context, &set, &masterID );
		if ( IsntPGPError( err ) )
		{
			err	= sIteratorTest( set, masterID );
			pgpAssertNoErr( err );
			
			if (IsntPGPError( err ) )
			{
				err	= sMiscTest( set );
				pgpAssertNoErr( err );
			}
			
			if (IsntPGPError( err ) )
			{
				err	= sSaveRestoreTest( set );
				pgpAssertNoErr( err );
			}
			
			if (IsntPGPError( err ) )
			{
				err	= sMergeTest( set );
				pgpAssertNoErr( err );
			}
			
			if (IsntPGPError( err ) )
			{
				err	= sDeleteTest( set );
				pgpAssertNoErr( err );
			}


			err	= PGPFreeGroupSet( set );
			pgpAssertNoErr( err );
		}
		PGPFreeContext( context);
	}
	pgpAssertNoErr( err );
}

#endif
#endif /* ] */



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