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

📄 searchfilter.c

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 C
字号:
/*____________________________________________________________________________
	Copyright (C) 2002 PGP Corporation
	All rights reserved.
	
	$Id: SearchFilter.c,v 1.11 2002/10/29 17:44:44 pbj Exp $
____________________________________________________________________________*/
#include <windows.h>
#include <windowsx.h>
#include <time.h>
#include <assert.h>
#include <commctrl.h>

#include "pgpKeys.h"
#include "pgpErrors.h"
#include "pgpUtilities.h"
#include "pgpBuild.h"

#include "Search.h"
#include "SearchFilter.h"
#include "PGPclientLib.h"
#include "pgpWin32Errors.h"
#include "pgpUnicodeWin32.h"
#include "resource.h"

#include "UTF8Edit.h"

#define	IS					0
#define IS_NOT				1
#define IS_ON_OR_BEFORE		1
#define IS_AT_LEAST			1
#define IS_NOT_FROM			1
#define CONTAINS			2
#define IS_ON_OR_AFTER		2
#define IS_AT_MOST			2
#define DOES_NOT_CONTAIN	3
#define IS_SIGNED_BY		4
#define IS_NOT_SIGNED_BY	5

#define DH_KEY_TYPE			0
#define RSA_KEY_TYPE		1

#define REVOKED				0
#define DISABLED			1

extern PGPContextRef	g_context; 


static PGPError 
sKeyIdFilterFromKeySet(	PGPKeySetRef keySet,
								PGPFilterRef* pfilter)
{
	PGPError		error	= kPGPError_NoErr;
	PGPKeyIterRef	iter	= kInvalidPGPKeyIterRef;
	PGPKeyDBObjRef	key		= kInvalidPGPKeyDBObjRef;
	PGPFilterRef	aFilter = kInvalidPGPFilterRef;
	PGPFilterRef	idFilter= kInvalidPGPFilterRef;
	PGPKeyID		keyId;

	error = PGPNewKeyIterFromKeySet (keySet, &iter);

	if(IsntPGPError(error) )
	{
		PGPError iterError = kPGPError_NoErr;

		do
		{
			iterError = PGPKeyIterNextKeyDBObj(iter, 
					kPGPKeyDBObjType_Key, &key);

			if( IsntPGPError(iterError) )
			{
				error = PGPGetKeyID(key, &keyId);

				if( IsntPGPError(error) )
				{
					error = PGPNewKeyDBObjDataFilter(g_context, 
							kPGPSigProperty_KeyID,
							&keyId, sizeof(PGPKeyID),
							kPGPMatchCriterion_Equal,
							&aFilter );

					if(idFilter == kInvalidPGPFilterRef)
					{
						idFilter = aFilter;
					}
					else
					{
						PGPFilterRef combinedFilter = NULL;

						error = PGPUnionFilters(idFilter,
												aFilter, 
												&combinedFilter);

						idFilter = combinedFilter;
					}
				}
			}

		} while(IsntPGPError(iterError) && IsntPGPError(error));

		PGPFreeKeyIter(iter);
	}


	if( IsntPGPError(error) )
	{
		*pfilter = idFilter;
	}

	return error;
}


PGPError 
CreateFilter(
		HWND			hwnd, 
		PGPKeyDBRef		keydbMain,
		PGPFilterRef*	pfilter, 
		int*			action)
{
	PGPError error = kPGPError_NoErr;
	HWND hwndAttribute, hwndVerb;
	HWND hwndEditSpecifier, hwndComboSpecifier, hwndComboListSpecifier;
	HWND hwndTime;
	int AttributeSelection = 0;
	int VerbSelection = 0;
	int SpecifierSelection = 0;
	char* SpecifierBuffer = NULL;
	DWORD SpecifierLength = 0;
	int Month = 1;
	int Day = 1;
	int Year = 1970;
	BOOL bNegate = FALSE;
	SYSTEMTIME st;

	assert(hwnd);
	assert(pfilter);
	assert(action);

	// set default action
	*action = ACTION_INTERSECT;

	// find all our window handles
	hwndAttribute			= GetDlgItem(hwnd, IDC_ATTRIBUTE);
	hwndVerb				= GetDlgItem(hwnd, IDC_VERB);
	hwndEditSpecifier		= GetDlgItem(hwnd, IDC_SPECIFIER_EDIT);
	hwndComboSpecifier		= GetDlgItem(hwnd, IDC_SPECIFIER_COMBO);
	hwndComboListSpecifier	= GetDlgItem(hwnd, IDC_SPECIFIER_COMBO_LIST);
	hwndTime				= GetProp(hwnd, "hwndTime");
		
	// find what the user has chosen to search on
	AttributeSelection	= ComboBox_GetCurSel(hwndAttribute);
	VerbSelection		= ComboBox_GetCurSel(hwndVerb);
	SpecifierSelection	= ComboBox_GetCurSel(hwndComboSpecifier);
	if (SpecifierSelection < 0) 
		SpecifierSelection	= ComboBox_GetCurSel(hwndComboListSpecifier);

	// get date currently in time/date picker control
	SendMessage (hwndTime, DTM_GETSYSTEMTIME, 0, (LPARAM)&st);
	// struct tm represents months 0-11  so we have to subtract 1
	Month = st.wMonth - 1; 
	// struct tm represents days 1-31  we have don't subtract 1
	Day =	st.wDay;
	// struct tm represents years as offset from 1900
	Year =	st.wYear - 1900;

	// combo is zero based and the string list is not... adjust Selection
	switch( AttributeSelection + IDS_SEARCH_STRINGTABLE_BASE + 1 ) 
	{
		case IDS_ATTRIBUTE_1: // user id
		{
			SpecifierLength = UTF8EditGetTextLength (hwndEditSpecifier) + 1;
			SpecifierBuffer = (char*) malloc(SpecifierLength);

			if( SpecifierBuffer )
			{
				PGPUInt32	ulen			= 0;

				char		szUTF8[kPGPMaxUserIDSize];

				UTF8EditGetText(hwndEditSpecifier, 
								SpecifierBuffer, 
								SpecifierLength);

				switch( VerbSelection )
				{
					case IS_NOT:
						bNegate = TRUE;
					case IS:
					{
						error = PGPNewKeyDBObjDataFilter(

										g_context, 
										kPGPUserIDProperty_Name,
										SpecifierBuffer, 
										SpecifierLength -1,
										kPGPMatchCriterion_Equal,
										pfilter );

						if( bNegate && IsntPGPError(error) )
						{
							PGPFilterRef NegatedFilter = kInvalidPGPFilterRef;

							error = PGPNegateFilter(*pfilter, 
													&NegatedFilter);

							if( IsntPGPError(error) )
							{
								*pfilter = NegatedFilter;
							}
						}

						break;
					}

					case DOES_NOT_CONTAIN:
						bNegate = TRUE;
					case CONTAINS:
					{
						
						error = PGPNewKeyDBObjDataFilter(
										g_context, 
										kPGPUserIDProperty_Name,
										SpecifierBuffer, 
										SpecifierLength -1,
										kPGPMatchCriterion_SubString,
										pfilter );

						if( bNegate && IsntPGPError(error) )
						{
							PGPFilterRef NegatedFilter = kInvalidPGPFilterRef;

							error = PGPNegateFilter(*pfilter, 
													&NegatedFilter);

							if( IsntPGPError(error) )
							{
								*pfilter = NegatedFilter;
							}
						}

						break;
					}

					case IS_NOT_SIGNED_BY:
						bNegate = TRUE;
					case IS_SIGNED_BY:
					{
						PGPKeySetRef	filteredSet	= kInvalidPGPKeySetRef;

						if(PGPKeyDBRefIsValid (keydbMain))
						{
							PGPFilterRef userIdFilter = kInvalidPGPFilterRef;

							error = PGPNewKeyDBObjDataFilter(g_context, 
									kPGPUserIDProperty_Name,
									szUTF8, 
									ulen,
									kPGPMatchCriterion_SubString,
									&userIdFilter );

							if( IsntPGPError(error) )
							{
								error = PGPFilterKeyDB( keydbMain, 
														userIdFilter, 
														&filteredSet);

								PGPFreeFilter(userIdFilter);
							}

							if( IsntPGPError(error) )
							{
								error = sKeyIdFilterFromKeySet(	filteredSet,
																pfilter);
								PGPFreeKeySet(filteredSet);
							}

							if (!PGPFilterRefIsValid (*pfilter))
							{
								error = kPGPError_Win32_NoSigningKey;
							}

							if( bNegate && IsntPGPError(error) )
							{
								PGPFilterRef NegatedFilter = kInvalidPGPFilterRef;

								error = PGPNegateFilter(*pfilter, 
														&NegatedFilter);

								if( IsntPGPError(error) )
								{
									*pfilter = NegatedFilter;
								}
							}
						}

						break;
					}
				}

				free(SpecifierBuffer);
			}
			else
			{
				error = kPGPError_OutOfMemory;
			}

			break;
		}

		case IDS_ATTRIBUTE_2: // key id
		{
			SpecifierLength = UTF8EditGetTextLength (hwndEditSpecifier) + 1;
			SpecifierBuffer = (char*) malloc(SpecifierLength);

			if( SpecifierBuffer )
			{
				UTF8EditGetText(hwndEditSpecifier, 
								SpecifierBuffer, 
								SpecifierLength);

				switch( VerbSelection )
				{
					case IS_NOT:
						bNegate = TRUE;
					case IS:
					{
						PGPKeyID keyId;


						error = PGPNewKeyIDFromString(	SpecifierBuffer, 
								kPGPPublicKeyAlgorithm_Invalid, &keyId );
						if(IsntPGPError(error))
						{
							error = PGPNewKeyDBObjDataFilter(g_context, 
										kPGPKeyProperty_KeyID, 
										&keyId, sizeof(PGPKeyID),
										kPGPMatchCriterion_Equal, 
										pfilter );
					
							if( bNegate && IsntPGPError(error) )
							{
								PGPFilterRef NegatedFilter = kInvalidPGPFilterRef;

								error = PGPNegateFilter(*pfilter, 
														&NegatedFilter);

								if( IsntPGPError(error) )
								{
									*pfilter = NegatedFilter;
								}
							}
						}
						else
						{
							error = kPGPError_Win32_InvalidKeyIDString;
						}

						break;
					}
				}

				free(SpecifierBuffer);
			}
			else
			{
				error = kPGPError_OutOfMemory;
			}

			break;
		}

		case IDS_ATTRIBUTE_3: // key type
		{
			PGPUInt32 encryptAlgorithm = kPGPPublicKeyAlgorithm_Invalid;

			if( SpecifierSelection == RSA_KEY_TYPE )
			{
				encryptAlgorithm = kPGPPublicKeyAlgorithm_RSA;
			}
			else if( SpecifierSelection == DH_KEY_TYPE )
			{
				encryptAlgorithm = kPGPPublicKeyAlgorithm_DSA;
			}

			error = PGPNewKeyDBObjNumericFilter(g_context,
					kPGPKeyProperty_AlgorithmID,
					encryptAlgorithm, 
					kPGPMatchCriterion_Equal,
			 		pfilter);
			break;
		}

		case IDS_ATTRIBUTE_4: // creation date
		{
			struct tm time;

			memset(&time, 0x00, sizeof(time));

			time.tm_mday	= Day;    /* day of the month - [1,31] */
			time.tm_mon		= Month;  /* months since January - [0,11] */
			time.tm_year	= Year;   /* years since 1900 */

			switch( VerbSelection )
			{

				// In order to simulate an "is date" without the
				// user having to enter the exact hour, min, sec
				// the key was created, we fake it by doing an
				// intersection between the entire day.
				case IS:
				{
					PGPFilterRef filterAM;
					PGPFilterRef filterPM;

					/* midnight AM */
					time.tm_sec		= 0; 
					time.tm_min		= 0;     
					time.tm_hour	= 0;

					error = PGPNewKeyDBObjTimeFilter(g_context, 
							kPGPKeyProperty_Creation,
							PGPGetPGPTimeFromStdTime(mktime(&time)), 
							kPGPMatchCriterion_GreaterOrEqual, 
							&filterAM );

					if(IsPGPError(error))
					{
						break;
					}

					/* just before midnight PM */
					time.tm_sec		= 59; 
					time.tm_min		= 59;     
					time.tm_hour	= 23;

					error = PGPNewKeyDBObjTimeFilter(g_context, 
							kPGPKeyProperty_Creation,
							PGPGetPGPTimeFromStdTime(mktime(&time)), 
							kPGPMatchCriterion_LessOrEqual, 
							&filterPM );

					if(IsntPGPError(error))
					{
						error = PGPIntersectFilters(filterAM,
													filterPM,
													pfilter);
					}
					
					break;
				}

				case IS_ON_OR_BEFORE:
				{
					
					/* just before midnight PM*/
					time.tm_sec		= 59; 
					time.tm_min		= 59;     
					time.tm_hour	= 23;

					error = PGPNewKeyDBObjTimeFilter(g_context, 
							kPGPKeyProperty_Creation,
							PGPGetPGPTimeFromStdTime(mktime(&time)), 
							kPGPMatchCriterion_LessOrEqual, 
							pfilter );
					break;
				}

				case IS_ON_OR_AFTER:
				{
					/* midnight AM*/
					time.tm_sec		= 0; 
					time.tm_min		= 0;     
					time.tm_hour	= 0;

					error = PGPNewKeyDBObjTimeFilter(g_context, 
							kPGPKeyProperty_Creation,
							PGPGetPGPTimeFromStdTime(mktime(&time)), 
							kPGPMatchCriterion_GreaterOrEqual, 
							pfilter );

					break;
				}
			}
			break;
		}

		case IDS_ATTRIBUTE_5: // expiration date
		{
			struct tm time;
			
			memset(&time, 0x00, sizeof(time));

			time.tm_mday	= Day;    /* day of the month - [1,31] */
			time.tm_mon		= Month;  /* months since January - [0,11] */
			time.tm_year	= Year;   /* years since 1900 */

			switch( VerbSelection )
			{

				// In order to simulate an "is date" without the
				// user having to enter the exact hour, min, sec
				// the key was created, we fake it by doing an
				// intersection between the entire day.
				case IS:
				{
					PGPFilterRef filterAM;
					PGPFilterRef filterPM;

					/* midnight AM */
					time.tm_sec		= 0; 
					time.tm_min		= 0;     
					time.tm_hour	= 0;

					error = PGPNewKeyDBObjTimeFilter(g_context, 
							kPGPKeyProperty_Expiration,
							PGPGetPGPTimeFromStdTime(mktime(&time)), 
							kPGPMatchCriterion_GreaterOrEqual, 
							&filterAM );

					if(IsPGPError(error))
					{
						break;
					}

					/* just before midnight PM */
					time.tm_sec		= 59; 
					time.tm_min		= 59;     
					time.tm_hour	= 23;

					error = PGPNewKeyDBObjTimeFilter(g_context, 
							kPGPKeyProperty_Expiration,
							PGPGetPGPTimeFromStdTime(mktime(&time)), 
							kPGPMatchCriterion_LessOrEqual, 
							&filterPM );

					if(IsntPGPError(error))
					{
						error = PGPIntersectFilters(filterAM,
													filterPM,
													pfilter);

					}
					
					break;
				}

				case IS_ON_OR_BEFORE:
				{
					
					/* just before midnight PM*/
					time.tm_sec		= 59; 
					time.tm_min		= 59;     
					time.tm_hour	= 23;

					error = PGPNewKeyDBObjTimeFilter(g_context, 
							kPGPKeyProperty_Expiration,
							PGPGetPGPTimeFromStdTime(mktime(&time)), 
							kPGPMatchCriterion_LessOrEqual, 
							pfilter );
					break;
				}

				case IS_ON_OR_AFTER:
				{
					/* midnight AM*/
					time.tm_sec		= 0; 
					time.tm_min		= 0;     
					time.tm_hour	= 0;

					error = PGPNewKeyDBObjTimeFilter(g_context, 
							kPGPKeyProperty_Expiration,
							PGPGetPGPTimeFromStdTime(mktime(&time)), 
							kPGPMatchCriterion_GreaterOrEqual, 
							pfilter );

					break;
				}
			}
			break;
		}

		case IDS_ATTRIBUTE_6: // key
		{
			if( SpecifierSelection == REVOKED )
			{
				error = PGPNewKeyDBObjBooleanFilter(g_context, 
						kPGPKeyProperty_IsRevoked, 
						(PGPBoolean)!VerbSelection, 
						pfilter );
			}
			else if( SpecifierSelection == DISABLED )
			{
				error = PGPNewKeyDBObjBooleanFilter(g_context, 
						kPGPKeyProperty_IsDisabled, 
						(PGPBoolean)!VerbSelection, 
						pfilter );
			}
			break;
		}

		case IDS_ATTRIBUTE_7: // key size
		{
			SpecifierLength = ComboBox_GetTextLength(hwndComboSpecifier) + 1;
			SpecifierBuffer = (char*) malloc(SpecifierLength);

			if( SpecifierBuffer )
			{
				ComboBox_GetText(	hwndComboSpecifier, 
									SpecifierBuffer, 
									SpecifierLength);

				//MessageBox(NULL, SpecifierBuffer, "Key Size", MB_OK);

				switch( VerbSelection )
				{
					case IS:
					{
						error = PGPNewKeyDBObjNumericFilter(g_context, 
								kPGPKeyProperty_Bits,
								atol(SpecifierBuffer), 
			 					kPGPMatchCriterion_Equal, 
								pfilter );
						break;
					}

					case IS_AT_LEAST:
					{
						error = PGPNewKeyDBObjNumericFilter(g_context, 
								kPGPKeyProperty_Bits,
								atol(SpecifierBuffer), 
			 					kPGPMatchCriterion_GreaterOrEqual, 
								pfilter );
						break;
					}

					case IS_AT_MOST:
					{
						error = PGPNewKeyDBObjNumericFilter(g_context, 
								kPGPKeyProperty_Bits,
								atol(SpecifierBuffer), 
			 					kPGPMatchCriterion_LessOrEqual, 
								pfilter );
						break;
					}
				}

				free(SpecifierBuffer);
			}
			else
			{
				error = kPGPError_OutOfMemory;
			}
			break;
		}

		default:
		{
			MessageBox(NULL, "DefaultFilter: Unknown Attribute", 0, MB_OK);
		}
	}

	return error;
}

⌨️ 快捷键说明

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