cpgpexchattachedfileevents.cpp

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

CPP
1,252 行
字号
								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);
					}

					if (!nError)
					{
						pgpAssert(NULL != pval);
						hrRet = pAttach->SetProps(6, PropValArr, NULL);
						pgpAssert(S_OK == hrRet);
						if(S_OK != hrRet)
							goto attach_cleanup;
					}

				}
				else
				{
					if (pval[ivalRenderingPos].Value.l > -1)
						pval[ivalRenderingPos].Value.l = nOutSize + ulRow;

					hrRet = pAttach->SetProps(6, pval, NULL);
					pgpAssert(S_OK == hrRet);
					if(S_OK != hrRet)
						goto attach_cleanup;
				}

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

				break;
				
			case ATTACH_BY_REFERENCE:
				UIDisplayStringID(hwnd, IDS_E_CANTENCRYPTATTACH);
				nError = kPGPError_UserAbort;
				break;
				
			case ATTACH_BY_REF_RESOLVE:
				UIDisplayStringID(hwnd, IDS_E_CANTENCRYPTATTACH);
				nError = kPGPError_UserAbort;
				break;
				
			case ATTACH_BY_REF_ONLY:
				UIDisplayStringID(hwnd, IDS_E_CANTENCRYPTATTACH);
				nError = kPGPError_UserAbort;
				break;
				
			case ATTACH_EMBEDDED_MSG:
				{
					IMessage *pmsg = NULL;
					BOOL bOrigConvert;

					hrRet = pAttach->OpenProperty(PR_ATTACH_DATA_OBJ, &IID_IMessage, 0, 
						MAPI_MODIFY, (LPUNKNOWN *) &pmsg);
					pgpAssert((S_OK == hrRet) && (NULL != pmsg));

					if(SUCCEEDED(hrRet) && (NULL != pmsg))
					{
						bOrigConvert = _bConvert;
						_bConvert = TRUE;
						nError = EncryptSignMessage(hwnd, pmsg, prds, pSignOptions);
						_bConvert = bOrigConvert;

						hrRet = pmsg->SaveChanges(0);
						pgpAssert(S_OK == hrRet);

						pmsg->Release();
						pmsg = NULL;
					}
				}

				pgpAssert(NULL != pAttach);
				hrRet = pAttach->SaveChanges(0);
				pgpAssert(SUCCEEDED(hrRet));

				break;

			case ATTACH_OLE:
				// Don't sign attachments unless we're also encrypting them
				if (_bEncrypt)
				{
					IStorage *pStg = NULL;

					hrRet = pAttach->OpenProperty(PR_ATTACH_DATA_OBJ, &IID_IStorage,
							0, MAPI_MODIFY, (LPUNKNOWN *) &pStg);
					pgpAssert(SUCCEEDED(hrRet) && (NULL != pStg));
					if (SUCCEEDED(hrRet) && (NULL != pStg))
					{
						IEnumSTATSTG *pEnum = NULL;
						STATSTG stat = {0};

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

								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)
								{
									pgpAssert(NULL == pstrmAttach);
									hrRet = pStg->OpenStream(stat.pwcsName, NULL, 
											STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0,
											&pstrmAttach);
									pgpAssert(SUCCEEDED(hrRet) && (NULL != pstrmAttach));
									if (FAILED(hrRet) || (NULL == pstrmAttach))
										continue;
									
									hrRet = pstrmAttach->Stat(&AttStreamStats, STATFLAG_NONAME);
									pgpAssert(S_OK == hrRet);

									dwAttSize = AttStreamStats.cbSize.LowPart;

									pAttachText = (char *) calloc(dwAttSize+1, sizeof(char));
									pgpAssert(NULL != pAttachText);
									if(NULL != pAttachText)
									{
										hrRet = pstrmAttach->Read(pAttachText, dwAttSize, &dwAttSize);
										pgpAssert(S_OK == hrRet);

										pAttachText[dwAttSize] = 0;
									}
									
									nError = EncryptSignBuffer(UIGetInstance(), hwnd, 
												_pgpContext, _tlsContext, szName, 
												szFile, pAttachText, dwAttSize, prds, 
												NULL, pSignOptions, 
												(VOID ** )&pOutput, &nAttOutSize, 
												_bEncrypt, _bSign, TRUE, FALSE, 
												m_pExtMdlessCallBack,
												m_pemOurMdlessCallBack);
									
									if (!nError)
									{
										ULONG ulAttOutSize = nAttOutSize;
										LARGE_INTEGER li = {0};
										ULARGE_INTEGER uli = {ulAttOutSize, 0};
										ULONG ulWritten = 0;
										ULARGE_INTEGER uliSeek = {0};
										
										hrRet = pstrmAttach->Seek(li, STREAM_SEEK_SET, &uliSeek);
										pgpAssert(S_OK == hrRet);

										hrRet = pstrmAttach->Write(pOutput, ulAttOutSize, &ulWritten);
										pgpAssert(S_OK == hrRet);

										hrRet = pstrmAttach->SetSize(uli);
										pgpAssert(S_OK == hrRet);

										hrRet = pstrmAttach->Commit(STGC_DEFAULT);
										pgpAssert(S_OK == hrRet);

										PGPFreeData(pOutput);
										pOutput = NULL;
									}

									free(pAttachText);
									pAttachText = NULL;
								}

								CoTaskMemFree(stat.pwcsName);
							}
						}
			
						pEnum->Release();
						pEnum = NULL;

						hrRet = pStg->Commit(STGC_DEFAULT);
						pgpAssert(S_OK == hrRet);

						pStg->Release();
						pStg = NULL;
					}
				}
				if (pval[ivalRenderingPos].Value.l > -1)
					pval[ivalRenderingPos].Value.l = nOutSize + ulRow;

				hrRet = pAttach->SetProps(6, pval, NULL);
				pgpAssert(SUCCEEDED(hrRet));

				hrRet = pAttach->SaveChanges(0);
				pgpAssert(SUCCEEDED(hrRet));

				break;
				
			default:
				break;
		}
		
		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));

		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 (NULL != szFileName)
		{
			free(szFileName);
			szFileName = NULL;
		}

		if (NULL != szLongFileName)
		{
			free(szLongFileName);
			szLongFileName = NULL;
		}

		if (NULL != pOutput)
		{
			PGPFreeData(pOutput);
			pOutput = NULL;
		}
		
		if (nError)
			break;
	}

	return nError;
}


int CPGPexch::DecryptVerifyRestoreAttachment(HWND hwnd,
											 IMessage *psmg,
											 SRowSet *prAttach,
											 BOOL bRestore)
{
	ULONG ulRow = 0;
	ULONG ulNum = 0;
	SizedSPropTagArray(6, tagaAttach) = {
		6, {PR_ATTACH_METHOD, PR_ATTACH_TAG, PR_RENDERING_POSITION, 
			PR_ATTACH_FILENAME, PR_ATTACH_LONG_FILENAME, 
			PR_DISPLAY_NAME}};
	enum {ivalAttachMethod=0, ivalAttachTag, ivalRenderingPos, ivalFilename, 
			ivalLongFilename, ivalDisplayName};
	SPropValue *pval = NULL;
	SPropValue PropValArr[6] = {0};
	ULONG ulNumVals;
	LPATTACH pAttach = 0;
	IStream *pstrmAttach = NULL;
	STATSTG AttStreamStats;
	DWORD dwAttSize = 0;
	UINT nAttOutSize = 0;
	char *pAttachText = NULL;
	char *pOutput = NULL;
	char *szFileName = NULL;
	char *szLongFileName = NULL;
	char *szExtension = NULL;
	int nError = 0;
	char szName[256] = {0};
	char szFile[256] = {0};
	BOOL FYEO;
	HRESULT hrRet = S_OK;
	
	UIGetString(szName, sizeof(szName), IDS_LOGNAME);
	UIGetString(szFile, sizeof(szFile), IDS_DLL);

	//for each attachment found in the message
	for (ulRow=0; ulRow < prAttach->cRows; ulRow++)
	{
		ulNum = prAttach->aRow[ulRow].lpProps[0].Value.l;

		//open the attachment
		pgpAssert(NULL == pAttach);
		hrRet = psmg->OpenAttach(ulNum, NULL, MAPI_MODIFY, &pAttach);
		pgpAssert((S_OK == hrRet) && (NULL != pAttach));
		if((S_OK != hrRet) || (NULL == pAttach))
			goto attach_cleanup;

		//get the interesting properties of the attachment
		pgpAssert(NULL == pval);
		hrRet = pAttach->GetProps((SPropTagArray *)&tagaAttach, 0, 
					&ulNumVals, &pval);
		pgpAssert((S_OK == hrRet) || (MAPI_W_ERRORS_RETURNED == hrRet));
		if((S_OK != hrRet) && (MAPI_W_ERRORS_RETURNED != hrRet))
			goto attach_cleanup;

		//if we failed to get short or long file name props 
		if((PT_ERROR == PROP_TYPE(pval[ivalFilename].ulPropTag)) ||
		   (PT_ERROR == PROP_TYPE(pval[ivalLongFilename].ulPropTag)))
			goto attach_cleanup;//bail

		switch (pval[ivalAttachMethod].Value.l)
		{
			case ATTACH_BY_VALUE:
				if (!strstr(pval[ivalFilename].Value.lpszA, ".asc") &&
					!strstr(pval[ivalFilename].Value.lpszA, ".pgp") && !bRestore)
					goto attach_cleanup;//skip the attachment

				pgpAssert(NULL == pstrmAttach);
				hrRet = pAttach->OpenProperty(PR_ATTACH_DATA_BIN, &IID_IStream, 0, 
					MAPI_MODIFY, (LPUNKNOWN *) &pstrmAttach);
				pgpAssert((S_OK == hrRet) && (NULL != pstrmAttach));
				if((S_OK != hrRet) || (NULL == pstrmAttach))
					goto attach_cleanup;

				if (bRestore)
				{
					PGPMsgBackupList *pAtt = NULL;

					pAtt = m_pBackupMsg->FindAttachment(ulNum);
					//pgpAssert(NULL != pAtt); //TODO: attachment number returned for main body is 1 plus the number of attachments
											   //so assertion fails here everytime a message is being restored
					if (NULL == pAtt)
						goto attach_cleanup;

					nError = kPGPError_NoErr;
					szFileName = pAtt->szName;
					szLongFileName = pAtt->szLongName;
					pOutput = pAtt->szData;
					nAttOutSize = pAtt->dwSize;
				}
				else
				{
					hrRet = pstrmAttach->Stat(&AttStreamStats, STATFLAG_NONAME);
					pgpAssert(S_OK == hrRet);
					if(S_OK != hrRet)
						goto attach_cleanup;

					dwAttSize = AttStreamStats.cbSize.LowPart;
					
					pgpAssert(NULL == pAttachText);
					pgpAssert(dwAttSize > 0);
					pAttachText = (char *) calloc(dwAttSize+1, sizeof(char));
					pgpAssert(NULL != pAttachText);
					if(NULL == pAttachText)
						goto attach_cleanup;

					hrRet = pstrmAttach->Read(pAttachText, dwAttSize, &dwAttSize);
					pgpAssert(S_OK == hrRet);
					if(FAILED(hrRet))
						goto attach_cleanup;

					pAttachText[dwAttSize] = 0;
					
					pgpAssert(PT_STRING8 == PROP_TYPE(pval[ivalFilename].ulPropTag));
					pgpAssert(NULL == szFileName);

					//allocate a buffer that has a bit more space for a 3 letter extension
					szFileName = (char *) calloc(strlen(pval[ivalFilename].Value.lpszA)+5, 
									sizeof(char));
					pgpAssert(NULL != szFileName);
					if(NULL == szFileName)
						goto attach_cleanup;

					//copy the short file name to the buffer just allocated
					strcpy(szFileName, pval[ivalFilename].Value.lpszA);
					
					pgpAssert(PT_STRING8 == PROP_TYPE(pval[ivalLongFilename].ulPropTag));
					pgpAssert(NULL == szLongFileName);

					//allocate and copy the long file name returned
					szLongFileName = _strdup(pval[ivalLongFilename].Value.lpszA);
					pgpAssert(NULL != szLongFileName);
					if(NULL == szLongFileName)
						goto attach_cleanup;

					LoseExtension(szFileName);
					LoseExtension(szLongFileName);

					szExtension = strrchr(szLongFileName, '.');
					if (NULL != szExtension)
					{
						strncat(szFileName, szExtension, 4);//add the extension to the short file name
						szExtension = NULL;
					}
					
					nError = DecryptVerifyBuffer(UIGetInstance(), hwnd, 
								_pgpContext, _tlsContext, szLongFileName, szFile,
								pAttachText, dwAttSize, FALSE, TRUE, FALSE, 

⌨️ 快捷键说明

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