clmisc.c

来自「PGP8.0源码 请认真阅读您的文件包然后写出其具体功能」· C语言 代码 · 共 2,781 行 · 第 1/5 页

C
2,781
字号
/*____________________________________________________________________________
	Copyright (C) 2002 PGP Corporation
	All rights reserved.

	CLmisc.c - PGP ClientLib DLL miscellaneous routines

	$Id: CLmisc.c,v 1.137 2002/11/21 22:02:56 pbj Exp $
____________________________________________________________________________*/
#include "pgpPFLConfig.h"

// project header files
#include "PGPclx.h"
#include "pgpImageList.h"
#include "pgpWin32IPC.h"

#include "pgpFileSpec.h"
#include "pgpMemoryIO.h"
#include "pgpWordWrap.h"
#include "pgpFeatures.h"
#include "pgpNetPrefs.h"
#include "pgpDebug.h"
#include "pgpNetHostUtils.h"
#include "pgpUnicode.h"
#include "pgpUnicodeWin32.h"
#include "UTF8Edit.h"

#include <ole2.h>
#include <shellapi.h>
#include <shlobj.h>
#include <htmlhelp.h>
#include <mmsystem.h>
#define COMPILE_MULTIMON_STUBS
#include <multimon.h>

#define CHICKENANDEGG() TRUE //PGPclEnterprise()

// constants
// turn on/off the storage of the randseed file name in the prefs
#define PGP_PREFS_RANDSEEDFILE	0

#ifndef CSIDL_COMMON_APPDATA
#define CSIDL_COMMON_APPDATA	0x0023
#endif
#ifndef CSIDL_LOCAL_APPDATA
#define CSIDL_LOCAL_APPDATA		0x001C
#endif
#ifndef CSIDL_FLAG_CREATE
#define CSIDL_FLAG_CREATE		0x8000
#endif

extern FARPROC g_fpSHGetFolderPath;


typedef struct {
	PGPContextRef			context;
	LPSTR					pszRemoteHost;
	PGPKeyDBObjRef			keyAuthenticating;
	PGPtlsCipherSuiteNum	tlsCipher;
	PGPKeyDBRef				keydbMain;
	CHAR					szName[kPGPMaxUserIDSize];
	PGPclAuthType			authtype;
	PGPValidity				validityThreshold;
} CONFIRMAUTHSTRUCT, *PCONFIRMAUTHSTRUCT;

// external globals
extern HINSTANCE g_hInst;

// local globals
static DWORD aIds[] = {			// Help IDs
    IDC_SERVERNAME,		IDH_PGPCLMISC_AUTHSERVERNAME,
    IDC_KEYNAME,		IDH_PGPCLMISC_AUTHKEYNAME,
    IDC_FINGERPRINT,	IDH_PGPCLMISC_AUTHKEYFINGERPRINT,
    IDC_VALIDITY,		IDH_PGPCLMISC_AUTHKEYVALIDITY,
    IDC_IMPORTKEY,		IDH_PGPCLMISC_AUTHIMPORTKEY,
	IDC_CERTIFICATE,	IDH_PGPCLMISC_AUTHCERTIFICATE,
	IDC_SIGNATURE,		IDH_PGPCLMISC_AUTHSIGNATURE,
	IDC_EXCHANGE,		IDH_PGPCLMISC_AUTHEXCHANGE,
	IDC_CIPHER,			IDH_PGPCLMISC_AUTHCIPHER,
	IDC_HASH,			IDH_PGPCLMISC_AUTHHASH,
	IDOK,				IDH_PGPCLMISC_AUTHCONFIRM,
    0,0
};

//	___________________________________________________
//
//	Internal memory allocation routines
//

VOID*
clAlloc (UINT uBytes)
{
	VOID* p;
	p = malloc (uBytes);
	if (p)
		memset (p, 0, uBytes);
	return p;
}


VOID
clFree (VOID* p)
{
	if (p)
		free (p);
}

//	___________________________________________________
//
//	Message box routine using string table resource IDs

LRESULT
PGPclMessageBox (
		 HWND	hWnd,
		 INT	iCaption,
		 INT	iMessage,
		 ULONG	ulFlags)
{
	CHAR szCaption [128];
	CHAR szMessage [512];

	LoadString (g_hInst, iCaption, szCaption, sizeof(szCaption));
	LoadString (g_hInst, iMessage, szMessage, sizeof(szMessage));

	ulFlags |= MB_SETFOREGROUND;
	return (MessageBox (hWnd, szMessage, szCaption, ulFlags));
}

//	___________________________________________________
//
//	convert SYSTEMTIME structure to number of days from today

PGPError PGPclExport
PGPclSystemTimeToDays (
		 SYSTEMTIME*	pst,
		 INT*			piDays)
{
	SYSTEMTIME	stToday;
	struct tm	tmstruct;
	time_t		timeToday;
	time_t		timeInQuestion;
	UINT		uDayToday;
	UINT		uDayInQuestion;

	pgpAssert (pst != NULL);
	pgpAssert (piDays != NULL);

	*piDays = -1;

	if (pst->wYear > 2037)
		return kPGPError_BadParams;

	GetLocalTime (&stToday);

	tmstruct.tm_mday = stToday.wDay;
	tmstruct.tm_mon = stToday.wMonth -1;
	tmstruct.tm_year = stToday.wYear -1900;
	tmstruct.tm_hour = 12;
	tmstruct.tm_min = 0;
	tmstruct.tm_sec = 0;
	tmstruct.tm_isdst = -1;

	timeToday = mktime (&tmstruct);
	if (timeToday == (time_t)-1)
		return kPGPError_BadParams;

	tmstruct.tm_mday = pst->wDay;
	tmstruct.tm_mon = pst->wMonth -1;
	tmstruct.tm_year = pst->wYear -1900;
	tmstruct.tm_hour = 12;
	tmstruct.tm_min = 0;
	tmstruct.tm_sec = 0;
	tmstruct.tm_isdst = -1;

	timeInQuestion = mktime (&tmstruct);
	if (timeInQuestion == (time_t)-1)
		return kPGPError_BadParams;

	uDayToday = timeToday / 86400;
	uDayInQuestion = timeInQuestion / 86400;

	*piDays = uDayInQuestion - uDayToday;

	return kPGPError_NoErr;
}


//	___________________________________________________
//
//	create logical palette from bitmap color table

static HPALETTE
sCreateDIBPalette (
		  LPBITMAPINFO	lpbmi,
		  LPINT			lpiNumColors)
{
	LPBITMAPINFOHEADER	lpbi;
	LPLOGPALETTE		lpPal;
	HANDLE				hLogPal;
	HPALETTE			hPal = NULL;
	INT					i;

	lpbi = (LPBITMAPINFOHEADER)lpbmi;
	if (lpbi->biBitCount <= 8)
		*lpiNumColors = (1 << lpbi->biBitCount);
	else
		*lpiNumColors = 0;  // No palette needed for 24 BPP DIB

	if (*lpiNumColors)
	{
		hLogPal = GlobalAlloc (GHND, sizeof (LOGPALETTE) +
				sizeof (PALETTEENTRY) * (*lpiNumColors));
		lpPal = (LPLOGPALETTE) GlobalLock (hLogPal);
		lpPal->palVersion = 0x300;
		lpPal->palNumEntries = *lpiNumColors;

		for (i = 0;  i < *lpiNumColors;  i++)
		{
			lpPal->palPalEntry[i].peRed   = lpbmi->bmiColors[i].rgbRed;
			lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen;
			lpPal->palPalEntry[i].peBlue  = lpbmi->bmiColors[i].rgbBlue;
			lpPal->palPalEntry[i].peFlags = 0;
		}
		hPal = CreatePalette (lpPal);
		GlobalUnlock (hLogPal);
		GlobalFree (hLogPal);
   }
   return hPal;
}

//	___________________________________________________
//
//	Load DIB bitmap and associated palette

HBITMAP
CLLoadResourceBitmap (
		HINSTANCE		hInstance,
		LPSTR			lpString,
		HPALETTE FAR*	lphPalette)
{
	HRSRC				hRsrc;
	HGLOBAL				hGlobal;
	HBITMAP				hBitmapFinal = NULL;
	LPBITMAPINFOHEADER	lpbi;
	HDC					hdc;
    INT					iNumColors;

	if (hRsrc = FindResource (hInstance, lpString, RT_BITMAP))
	{
		hGlobal = LoadResource (hInstance, hRsrc);
		lpbi = (LPBITMAPINFOHEADER)LockResource (hGlobal);

		hdc = GetDC(NULL);
		*lphPalette =  sCreateDIBPalette ((LPBITMAPINFO)lpbi, &iNumColors);
		if (*lphPalette)
		{
			SelectPalette (hdc,*lphPalette,FALSE);
			RealizePalette (hdc);
		}

		hBitmapFinal = CreateDIBitmap (hdc,
                   (LPBITMAPINFOHEADER)lpbi,
                   (LONG)CBM_INIT,
                   (LPSTR)lpbi + lpbi->biSize + iNumColors * sizeof(RGBQUAD),
                   (LPBITMAPINFO)lpbi,
                   DIB_RGB_COLORS );

		ReleaseDC (NULL,hdc);
		UnlockResource (hGlobal);
		FreeResource (hGlobal);
	}
	return (hBitmapFinal);
}


//	___________________________________________________
//
//	Broadcast reload message

VOID PGPclExport
PGPclNotifyKeyringChanges (
		PGPclChangeType	type,
		PGPUInt32		uParam)
{
	UINT uMessageID;

	uMessageID = RegisterWindowMessage (kPGPclReloadKeyRingMsg);
	PostMessage (HWND_BROADCAST, uMessageID, (WPARAM)type, uParam);
}

//	___________________________________________________
//
//	Broadcast prefs reload message

VOID PGPclExport
PGPclNotifyPrefsChanges (
		PGPUInt32		uParam)
{
	UINT uMessageID;

	uMessageID = RegisterWindowMessage (kPGPclReloadPrefsMsg);
	PostMessage (HWND_BROADCAST, uMessageID, 0, uParam);
}

//	___________________________________________________
//
//	Broadcast PGPnet prefs reload message

VOID PGPclExport
PGPclNotifyNetPrefsChanges (
		PGPUInt32 			uParam)
{
	UINT uMessageID;

	uMessageID = RegisterWindowMessage (kPGPclReloadNetPrefsMsg);
	PostMessage (HWND_BROADCAST, uMessageID, 0, uParam);
}

//	___________________________________________________
//
//	Broadcast messages indicating keyserver prefs may have changed

VOID PGPclExport
PGPclNotifyKeyserverPrefsChanges (
		PGPUInt32		uParam)
{
	UINT uMessageID;

	uMessageID = RegisterWindowMessage (kPGPclReloadKeyserverPrefsMsg);
	PostMessage (HWND_BROADCAST, uMessageID, 0, uParam);
}

//	___________________________________________________
//
//	add trailing slash

static PGPError
sFinalizePath (
		LPSTR			pszPath, 
		PGPUInt32		uLen)
{
	if (pszPath[lstrlen (pszPath) -1] == '\\')
		return kPGPError_NoErr;
	else
	{
		if (lstrlen (pszPath) < (INT)uLen)
		{
			lstrcat (pszPath, "\\");
			return kPGPError_NoErr;
		}
		else
			return kPGPError_OutputBufferTooSmall;
	}
}

//	___________________________________________________
//
//	get installed path from registry

static PGPError
sGetInstalledPath (
		LPSTR			pszPath, 
		PGPUInt32		uLen)
{
	PGPError	err						= kPGPError_FileNotFound;
	HKEY		hkey;
	LONG		lResult;
	DWORD		dwValueType, dwSize;
	CHAR		sz[256];

	PGPclGetPath (kPGPclRegistryKey, sz, sizeof(sz));
	lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, sz, 0, KEY_READ, &hkey);

	if (lResult == ERROR_SUCCESS)
	{
		dwSize = uLen;
		lResult = RegQueryValueEx (hkey, 
				PGP_INSTALLPATHVALUE, 0, &dwValueType, pszPath, &dwSize);
		RegCloseKey (hkey);

		if (lResult == ERROR_SUCCESS)
			err = sFinalizePath (pszPath, uLen);
	}

	return err;
}


//	___________________________________________________
//
//	get desktop path from registry

static PGPError
sGetDesktopFolder (
		LPSTR			pszPath, 
		PGPUInt32		uLen)
{
	LONG	lResult;
	HKEY	hkey;
	DWORD	dwSize, dwValueType;

	lResult = RegOpenKeyEx (HKEY_CURRENT_USER, 
			"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\"
			"Shell Folders", 
			0, KEY_READ, &hkey);

	if (lResult == ERROR_SUCCESS) 
	{
		dwSize = uLen;
		lResult = RegQueryValueEx (hkey, "Desktop", 0, &dwValueType, 
			pszPath, &dwSize);

		return kPGPError_NoErr;
	}

	return kPGPError_FileNotFound;
}


#if PGP_DEBUG
//	____________________________________________________________
//
//	determine how if to run SDK backend or not

static BOOL
sUseLocalKeyrings (VOID)
{
	PGPBoolean	bLocal			= FALSE;
	HKEY		hkey;
	LONG		lResult;
	DWORD		dw, dwValueType, dwSize;

	lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, 
			PGP_REGISTRYKEY, 0, KEY_READ, &hkey);

	if (lResult == ERROR_SUCCESS) 
	{
		dwSize = sizeof(DWORD);

		lResult = RegQueryValueEx (hkey, "PGPclForceLocalKeyrings", 
				0, &dwValueType, (LPBYTE)&dw, &dwSize);

		if (lResult == ERROR_SUCCESS)
		{
			if (dw == 1)
				bLocal = TRUE;
		}

		RegCloseKey (hkey);
	}

	return bLocal;
}

static VOID
sLocalKeyring (
		BOOL		bSet,
		INT			iFile,
		LPSTR		pszPath,
		INT			ilen)
{
	HKEY		hkey;
	LONG		lResult;
	DWORD		dwValueType, dwSize;
	CHAR		sz[256];

	lResult = RegOpenKeyEx (HKEY_LOCAL_MACHINE, 
			PGP_REGISTRYKEY, 0, KEY_READ|KEY_SET_VALUE, &hkey);

	switch (iFile) {
	case kPGPclPublicKeyringFile :
		lstrcpy (sz, "PGPclLocalPubring");
		break;

	case kPGPclPrivateKeyringFile :
		lstrcpy (sz, "PGPclLocalSecring");
		break;

	case kPGPclNetPublicKeyringFile :
		lstrcpy (sz, "PGPclLocalNetPubring");
		break;

	case kPGPclNetPrivateKeyringFile :
		lstrcpy (sz, "PGPclLocalNetSecring");
		break;
	}

	if (lResult == ERROR_SUCCESS) 
	{
		if (bSet)
		{
			lResult = RegSetValueEx (hkey, sz, 
					0, REG_SZ, (LPBYTE)pszPath, ilen);
		}
		else
		{
			dwSize = ilen;
			lResult = RegQueryValueEx (hkey, sz, 
					0, &dwValueType, (LPBYTE)pszPath, &dwSize);
		}

		RegCloseKey (hkey);
	}
}

#endif //PGP_DEBUG

//	___________________________________________________
//
//	get specified path 
//	note: folder paths include trailing '\'

PGPError PGPclExport
PGPclGetPath (
		PGPclPathIndex	index,
		LPSTR			pszPath, 
		PGPInt32		iLen)
{
	PGPError	err					= kPGPError_NoErr;
	BOOL		bLocalKeyrings		= FALSE;
	PGPPrefRef	prefref;
	CHAR		szPath[MAX_PATH];
	CHAR		sz[256];

	// initialize strings
	sz[0] = '\0';
	szPath[0] = '\0';

	// check parameters
	if (IsNull (pszPath))
		return kPGPError_BadParams;
	pszPath[0] = '\0';

	// make sure SHFOLDER is loaded
	if (IsNull (g_fpSHGetFolderPath))
	{
		err = CLLoadSHFolderGetPath (NULL);
		if (IsPGPError (err))
			return err;
	}

#if PGP_DEBUG
	bLocalKeyrings = sUseLocalKeyrings ();
#endif

	// return path string
	switch (index) {
	case kPGPclRegistryKey :
		lstrcpyn (pszPath, PGP_REGISTRYKEY, iLen);
		break;

	case kPGPclInstallFolder :
		err = sGetInstalledPath (pszPath, iLen);
		break;

	case kPGPclHelpFile :
		err = sGetInstalledPath (pszPath, iLen);
		if (IsntPGPError (err))

⌨️ 快捷键说明

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