cpgpexchattachedfileevents.cpp

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

CPP
1,252 行
字号
								(VOID ** )&pOutput, &nAttOutSize, &FYEO);
				}

				if (!nError && (dwAttSize != nAttOutSize))
				{
					ULONG ulAttOutSize = nAttOutSize;
					LARGE_INTEGER li = {0,0};
					ULARGE_INTEGER uli = {ulAttOutSize, 0};
					ULONG ulWritten = 0;
					ULARGE_INTEGER uliSeek = {0};
						
					if (m_pBackupMsg && !bRestore)
					{
						m_pBackupMsg->AddAttachment(ulNum, FALSE,
									pval[ivalFilename].Value.lpszA,
									pval[ivalLongFilename].Value.lpszA, 
									pval[ivalDisplayName].Value.lpszA, pAttachText, 
									dwAttSize);
					}

					hrRet = pstrmAttach->Seek(li, STREAM_SEEK_SET, &uliSeek);
					pgpAssert(S_OK == hrRet);
					if(S_OK != hrRet)
						goto attach_cleanup;

					hrRet = pstrmAttach->Write(pOutput, ulAttOutSize, &ulWritten);
					pgpAssert(S_OK == hrRet);
					pgpAssert(ulAttOutSize == ulWritten);
					if((S_OK != hrRet) || (ulAttOutSize != ulWritten))
						goto attach_cleanup;

					hrRet = pstrmAttach->SetSize(uli);
					pgpAssert(S_OK == hrRet);
					if(S_OK != hrRet)
						goto attach_cleanup;

					hrRet = pstrmAttach->Commit(STGC_DEFAULT);
					pgpAssert(S_OK == hrRet);
					if(S_OK != hrRet)
						goto attach_cleanup;

					//copy the property values obtained
					memcpy(PropValArr, pval, sizeof(PropValArr));
					PropValArr[ivalFilename].Value.lpszA = NULL;
					PropValArr[ivalLongFilename].Value.lpszA = NULL;

					//allocate and copy the short file name to be set
					pgpAssert(PT_STRING8 == PROP_TYPE(PropValArr[ivalFilename].ulPropTag));
					pgpAssert(NULL == PropValArr[ivalFilename].Value.lpszA);

					PropValArr[ivalFilename].Value.lpszA = _strdup(szFileName);
					pgpAssert(NULL != PropValArr[ivalFilename].Value.lpszA);
					if(NULL == PropValArr[ivalFilename].Value.lpszA)
						goto attach_cleanup;

					//allocate for longfilename property
					pgpAssert(PT_STRING8 == PROP_TYPE(PropValArr[ivalLongFilename].ulPropTag));
					pgpAssert(NULL == PropValArr[ivalLongFilename].Value.lpszA);

					PropValArr[ivalLongFilename].Value.lpszA = _strdup(szLongFileName);
					pgpAssert(NULL != PropValArr[ivalLongFilename].Value.lpszA);
					if(NULL == PropValArr[ivalLongFilename].Value.lpszA)
						goto attach_cleanup;

					//if we could not get the display name property
					if(PT_ERROR == PROP_TYPE(PropValArr[ivalDisplayName].ulPropTag))
					{
						//make the display name property same as the long file name property
						//NOTE: we do not have a problem during freeing since we do not free
						//this buffer irrespective of display name being found or not. when 
						//it is found it points to the original getprops buffer otherwise it 
						//points to strduped buffer that was passed as long file name value
						PropValArr[ivalDisplayName].Value.lpszA = 
								PropValArr[ivalLongFilename].Value.lpszA;
						pgpAssert(NULL != PropValArr[ivalDisplayName].Value.lpszA);

						//set the property type appropriately
						PropValArr[ivalDisplayName].ulPropTag = 
							CHANGE_PROP_TYPE(PR_DISPLAY_NAME, PT_STRING8);
					}

					hrRet = pAttach->SetProps(6, PropValArr, NULL);
					pgpAssert(S_OK == hrRet);
					if(FAILED(hrRet))
						goto attach_cleanup;

					hrRet = pAttach->SaveChanges(0);
					pgpAssert(S_OK == hrRet);
					if(FAILED(hrRet))
						goto attach_cleanup;
					
				}

				break;
				
			case ATTACH_BY_REFERENCE:
				UIDisplayStringID(hwnd, IDS_E_CANTDECRYPTATTACH);
				nError = kPGPError_UserAbort;
				break;
				
			case ATTACH_BY_REF_RESOLVE:
				UIDisplayStringID(hwnd, IDS_E_CANTDECRYPTATTACH);
				nError = kPGPError_UserAbort;
				break;
				
			case ATTACH_BY_REF_ONLY:
				UIDisplayStringID(hwnd, IDS_E_CANTDECRYPTATTACH);
				nError = kPGPError_UserAbort;
				break;
				
			case ATTACH_EMBEDDED_MSG:
				/*
				if (!bRestore)
				{
					IMessage *pEmbedMsg = 0;

					pAttach->OpenProperty(PR_ATTACH_DATA_OBJ, &IID_IMessage, 0, 
						MAPI_MODIFY, (LPUNKNOWN *) &pEmbedMsg);
					
					_bNested = TRUE;
					DecryptVerifyMessage(hwnd, pEmbedMsg, NULL);
					_bNested = FALSE;

					pEmbedMsg->SaveChanges(0);
					pEmbedMsg->Release();
				}
				*/
				break;
				
			case ATTACH_OLE:
				{
					HRESULT hr;
					IStorage *pStg = NULL;

					hr = pAttach->OpenProperty(PR_ATTACH_DATA_OBJ, &IID_IStorage, 
							0, MAPI_MODIFY, (LPUNKNOWN *) &pStg);

					if (pStg != NULL)
					{
						IEnumSTATSTG *pEnum;
						STATSTG stat;

						pStg->EnumElements(0, NULL, 0, &pEnum);
						while (pEnum->Next(1, &stat, NULL) == S_OK)
						{
							char szOleName[256];

							wcstombs(szOleName, stat.pwcsName, sizeof(szOleName));
							if (!strncmp(&szOleName[1], "Ole", 4) ||
								!strncmp(&szOleName[1], "MailStream", 11) ||
								!strncmp(szOleName, "RichEditFlags", 14))
							{
								CoTaskMemFree(stat.pwcsName);
								continue;
							}

							if (stat.cbSize.LowPart)
							{
								hr = pStg->OpenStream(stat.pwcsName, NULL, 
										STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0,
										&pstrmAttach);
								
								if (hr != S_OK)
									continue;
							
								if (bRestore)
								{
									PGPOLEBackupList *pOLE;

									pOLE = m_pBackupMsg->FindOLEAttachment(ulNum,
												stat.pwcsName);
									if (!pOLE)
										continue;

									nError = kPGPError_NoErr;
									pOutput = pOLE->szData;
									nAttOutSize = pOLE->dwSize;
								}
								else
								{
									pstrmAttach->Stat(&AttStreamStats, 
										STATFLAG_NONAME);
									dwAttSize = AttStreamStats.cbSize.LowPart;
									pAttachText = (char *) calloc(dwAttSize+1, 
										sizeof(char));
									pstrmAttach->Read(pAttachText, dwAttSize, 
										&dwAttSize);
									pAttachText[dwAttSize] = 0;
									
									nError = DecryptVerifyBuffer(UIGetInstance(), 
												hwnd, _pgpContext, _tlsContext, 
												szName, szFile, pAttachText, 
												dwAttSize, FALSE, TRUE, FALSE, 
												(VOID ** )&pOutput, &nAttOutSize, 
												&FYEO);
								}

								if (!nError && (dwAttSize != nAttOutSize))
								{
									ULONG ulAttOutSize = nAttOutSize;
									LARGE_INTEGER li = {0,0};
									ULARGE_INTEGER uli = {ulAttOutSize, 0};
									ULONG ulWritten;
									ULARGE_INTEGER uliSeek;
									
									if (m_pBackupMsg && !bRestore)
									{
										m_pBackupMsg->AddOLE(ulNum, stat.pwcsName,
											pAttachText, dwAttSize);
									}

									pstrmAttach->Seek(li, STREAM_SEEK_SET, &uliSeek);
									pstrmAttach->Write(pOutput, ulAttOutSize, &ulWritten);
									pstrmAttach->SetSize(uli);
									pstrmAttach->Commit(STGC_DEFAULT);
									if (!bRestore)
										PGPFreeData(pOutput);
								}

								if ((dwAttSize == nAttOutSize) && pOutput && 
									!bRestore)
									PGPFreeData(pOutput);

								pstrmAttach->Release();
								pstrmAttach = NULL;
								free(pAttachText);
								pAttachText = NULL;
							}
							
							CoTaskMemFree(stat.pwcsName);
							if ((dwAttSize > 0) && (dwAttSize == nAttOutSize))
								break;
						}
						
						pEnum->Release();
						pStg->Commit(STGC_DEFAULT);
						pStg->Release();
					}
				}
				pAttach->SaveChanges(0);
				break;
				
			default:
				break;
		}//end switch (pval[ivalAttachMethod].Value.l)

		attach_cleanup:

		//free up both the properties for which we might have allocated
		if(NULL != PropValArr[ivalFilename].Value.lpszA)
		{
			free(PropValArr[ivalFilename].Value.lpszA);
			PropValArr[ivalFilename].Value.lpszA = NULL;
		}
		if(NULL != PropValArr[ivalLongFilename].Value.lpszA)
		{
			free(PropValArr[ivalLongFilename].Value.lpszA);
			PropValArr[ivalLongFilename].Value.lpszA = NULL;
		}

		//reset the setprops property value list
		ZeroMemory(PropValArr, sizeof(PropValArr));

		//free up the getprops value array
		if(NULL != pval)
		{
			hrRet = MAPIFreeBuffer(pval);
			pgpAssert(S_OK == hrRet);
			pval = NULL;
		}

		if (NULL != pAttachText)
		{
			free(pAttachText);
			pAttachText = NULL;
		}
		if(NULL != pAttach)
		{
			pAttach->Release();
			pAttach = NULL;
		}
		if(NULL != pstrmAttach)
		{
			pstrmAttach->Release();
			pstrmAttach = NULL;
		}

		if(!bRestore)
		{
			if (NULL != pOutput)
			{
				PGPFreeData(pOutput);
				pOutput = NULL;
			}

			if(NULL != szFileName)
			{
				free(szFileName);
				szFileName = NULL;
			}
			if(NULL != szLongFileName)
			{
				free(szLongFileName);
				szLongFileName = NULL;
			}
		}
		
		if (nError)
			break;
	}//for each attachment

	return nError;
}


void CPGPexch::WipeMessageAttachment(char *szFile)
{
	if (_pgpContext && IsntPGPError(_errContext))
	{
		FILELIST *pList = NULL;
		
		if (PGPscAddToFileList(&pList, szFile, NULL, NULL))
			PGPscWipeFileList(NULL, _pgpContext, pList,
				WIPE_DONOTWARN | WIPE_DONOTPROGRESS,0);
	}
	return;
}

void CPGPexch::AddToWipeList(char *szFile)
{
	ExchWipeList *pHead = (ExchWipeList *) _pWipeList;
	ExchWipeList *pTail = NULL;
	ExchWipeList *pCurrent;

	while (pHead != NULL)
	{
		if (!stricmp(pHead->szFile, szFile))
			return;

		pTail = pHead;
		pHead = pHead->pNext;
	}

	pCurrent = (ExchWipeList *) PGPNewData(_memoryMgr, sizeof(ExchWipeList), 0);
	pCurrent->szFile = (char *) PGPNewData(_memoryMgr, strlen(szFile)+1, 0);
	strcpy(pCurrent->szFile, szFile);
	pCurrent->pNext = NULL;
	
	if (pTail != NULL)
		pTail->pNext = pCurrent;
	else
		_pWipeList = (void *) pCurrent;

	return;
}


void CPGPexch::WipeAttachmentList(void)
{
	ExchWipeList *pHead = (ExchWipeList *) _pWipeList;
	ExchWipeList *pTail = NULL;
	FILELIST *pWipe = NULL;

	if (_pgpContext && IsntPGPError(_errContext))
	{
		while (pHead != NULL)
		{
			PGPscAddToFileList(&pWipe, pHead->szFile, NULL, NULL);
			PGPFreeData(pHead->szFile);
			pTail = pHead;
			pHead = pHead->pNext;
			PGPFreeData(pTail);
		}

		PGPscWipeFileList(NULL, _pgpContext, pWipe,
			WIPE_DONOTWARN | WIPE_DONOTPROGRESS,0);

		_pWipeList = NULL;
	}
	return;
}

void CPGPexch::LoseExtension(char *pszFileName)
{
	char *szASCExtension = NULL;
	char *szPGPExtension = NULL;
	char *szExtension = NULL;
	char *szEnd = NULL;

	szEnd = pszFileName + strlen(pszFileName) - 1;
	szASCExtension = StrRStrI(pszFileName, szEnd, ".asc");
	szPGPExtension = StrRStrI(pszFileName, szEnd, ".pgp");
	if (szASCExtension && szPGPExtension)
	{
		if (szASCExtension > szPGPExtension)
			szExtension = szASCExtension;
		else
			szExtension = szPGPExtension;
	}
	else if (szASCExtension)
		szExtension = szASCExtension;
	else
		szExtension = szPGPExtension;
	*szExtension = '\0';
}


/*__Editor_settings____

	Local Variables:
	tab-width: 4
	End:
	vi: ts=4 sw=4
	vim: si
_____________________*/

⌨️ 快捷键说明

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