📄 decodeevent.c
字号:
/*____________________________________________________________________________
decodeEvent.c
Copyright (C) 2003, 2004 PGP Corporation
All rights reserved.
This file contains functions which are used to handle events
passed back by PGPOEventHandler
$Id: decodeEvent.c 48493 2006-10-12 21:19:56Z vinnie $
____________________________________________________________________________*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#ifndef PGP_WIN32
#include <unistd.h>
#else
#include <direct.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include "pgpFeatures.h"
#include "pgpKeys.h"
#include "pgpPublicKey.h"
#include "pgpHash.h"
#include "pgpUtilities.h"
#include "pgpKeyServer.h"
#include "optest.h"
static void STATUS_LOG(const char *fmt, ...)
{
va_list marker;
va_start( marker, fmt );
if(gVerbose_flag)
{
OPTESTVPrintF(fmt, marker);
}
va_end( marker );
}
static char *extname (const char *name)
{
const char *ext;
if(!name) return 0;
for (ext = name + strlen(name) ; ext != name && ( *ext != '.' && *ext != '/') ; ext--)
;
if(ext == name ) return 0;
else return (char*)(ext+1);
}
void InitDecodeInfo( DecodeInfo* info)
{
info->outBuf = NULL;
info->outBufSize = 0;
info->sessionKey = NULL;
info->sessionKeySize = 0;
info->outFile = NULL;
info->keyCount = 0;
info->keyIter = kInvalidPGPKeyIterRef;
}
void CleanUpDecodeInfo( DecodeInfo* info)
{
if(info->outBuf)
{
PGPFreeData(info->outBuf);
info->outBuf = NULL;
info->outBufSize = 0;
}
if(info->sessionKey)
{
PGPFreeData(info->sessionKey);
info->sessionKey = NULL;
info->sessionKeySize = 0;
}
if( PGPFileSpecRefIsValid(info->outFile))
{
PGPFreeFileSpec(info->outFile);
info->outFile = NULL;
}
}
static char *analyzeEventTxt[] = {
"Encrypted message",
"Signed message",
"Detached signature",
"Key data ",
"Non-pgp message",
"X.509 certificate",
"SMIME body"} ;
static char *KeyserverEventTxt[] = {
"Invalid",
"Opening",
"Querying",
"ReceivingResults",
"ProcessingResults",
"Uploading",
"Deleting",
"Disabling",
"Closing",
"UnableToSecureConnection",
"ConnectionSecured"};
#if HAVE_PRAGMA_MARK
#pragma mark
#endif
PGPError OptestEventHandler(PGPContextRef context, PGPEvent *event, PGPUserValue userValue)
{
PGPError err = kPGPError_NoErr;
DecodeInfo* info = (DecodeInfo*)userValue;
static PGPBoolean inKeyGen, inNull;
static int lastProgress;
static PGPEventRecipientsData *recipdata = NULL;
if( event->type != kPGPEvent_KeyGenEvent)
{
if(inKeyGen) STATUS_LOG("\n");
inKeyGen = FALSE;
}
if( event->type != kPGPEvent_NullEvent)
{
if(inNull) STATUS_LOG("\n");
inNull = FALSE;
}
switch(event->type)
{
#if HAVE_PRAGMA_MARK
#pragma mark ----- Initial Event-----
#endif
case kPGPEvent_InitialEvent:
inKeyGen = FALSE;
inNull = FALSE;
lastProgress = 0;
info->keyIter = kInvalidPGPKeyIterRef;
STATUS_LOG("--> Init Event: \n");
break;
#if HAVE_PRAGMA_MARK
#pragma mark ----- Final Event-----
#endif
case kPGPEvent_FinalEvent:
STATUS_LOG("--> Final Event\n");
lastProgress = 0;
if( recipdata) { PGPFreeData(recipdata); recipdata = NULL; };
if( PGPKeyIterRefIsValid( info->keyIter ) )
{
PGPFreeKeyIter( info->keyIter );
info->keyIter = kInvalidPGPKeyIterRef;
};
if(info->outBuf != NULL)
{
if(info->option == kDecode_DumpTar)
{
dumpTAR(info->outBuf, (int)(info->outBufSize), FALSE);
PGPFreeData(info->outBuf);
info->outBuf = NULL;
info->outBufSize = 0;
}
else if(info->option == kDecode_Ignore_Output)
{
}
else
{
STATUS_LOG(" Decoded %d bytes \n--------------\n", (int)info->outBufSize);
if(strlen((char*)info->outBuf) < info->outBufSize)
{
if(info->outBufSize < 128)
dumpHex(info->outBuf, (int)(info->outBufSize), 0);
else
{
dumpHex(info->outBuf, 128, 0);
STATUS_LOG(" .....\n");
dumpHex(info->outBuf + (int)(info->outBufSize) - 128, 128, (int)(info->outBufSize) - 128);
}
}
else
STATUS_LOG("%s\n", (char *)info->outBuf);
STATUS_LOG("--------------\n");
}
}
break;
#if HAVE_PRAGMA_MARK
#pragma mark ----- Error Event-----
#endif
case kPGPEvent_ErrorEvent:
{
PGPEventErrorData *d = &event->data.errorData;
STATUS_LOG("--> Error Event: %d \n", d->error);
}
break;
#if HAVE_PRAGMA_MARK
#pragma mark ----- Warning Event-----
#endif
case kPGPEvent_WarningEvent:
{
PGPEventWarningData *d = &event->data.warningData;
STATUS_LOG("--> Warning Event: %d \n", d->warning);
if(d->warning == kPGPError_KeyInvalid && d->warningArg)
{
PGPKeySetRef keyset = d->warningArg;
PGPUInt32 numKeys;
err = PGPCountKeys(keyset, &numKeys);
STATUS_LOG(" %d invalid key%s: ", numKeys, numKeys==1?"":"s");
if(numKeys)
{
PGPKeyIterRef iter = kInvalidPGPKeyIterRef;
PGPKeyDBObjRef aKey = kInvalidPGPKeyDBObjRef;
err = PGPNewKeyIterFromKeySet(keyset, &iter);
while( IsntPGPError( PGPKeyIterNextKeyDBObj( iter, kPGPKeyDBObjType_Key, &aKey) ) )
{
PGPKeyID keyID;
PGPChar8 outString[ kPGPMaxKeyIDStringSize ];
PGPGetKeyID( aKey, &keyID );
PGPGetKeyIDString( &keyID, kPGPKeyIDString_Full, outString);
STATUS_LOG(" %s",outString);
}
STATUS_LOG("\n");
if( PGPKeyIterRefIsValid( iter ) ) PGPFreeKeyIter( iter );
}
}
}
break;
#if HAVE_PRAGMA_MARK
#pragma mark ----- KeyGen Event-----
#endif
case kPGPEvent_KeyGenEvent:
{
PGPUInt32 state = event->data.keyGenData.state;
if(!inKeyGen)
{
STATUS_LOG("--> KenGen Event: ");
inKeyGen = TRUE;
}
STATUS_LOG("%c",state);
fflush(stdout);
}
break;
#if HAVE_PRAGMA_MARK
#pragma mark ----- Null Event-----
#endif
case kPGPEvent_NullEvent:
{
PGPEventNullData *d = &event->data.nullData;
int progress = (int)(( (float) d->bytesWritten / d->bytesTotal) * 100);
if(!inNull)
{
STATUS_LOG("--> Null Event: ");
inNull = TRUE;
}
if(progress == lastProgress)
STATUS_LOG(".");
else
STATUS_LOG(" %d%%",progress );
lastProgress = progress;
fflush(stdout);
}
break;
#if HAVE_PRAGMA_MARK
#pragma mark ----- Analyze Event-----
#endif
case kPGPEvent_AnalyzeEvent:
{
PGPEventAnalyzeData *d = &event->data.analyzeData;
STATUS_LOG("--> Analyzing Type %d data: %s\n", d->sectionType,
(d->sectionType <= kPGPAnalyze_SMIMEBody ?analyzeEventTxt[d->sectionType]: "Unknown"));
}
break;
#if HAVE_PRAGMA_MARK
#pragma mark ----- Begin Lex Event-----
#endif
case kPGPEvent_BeginLexEvent:
{
PGPEventBeginLexData *d = &event->data.beginLexData;
STATUS_LOG("--> Begin decoding section %d\n",d->sectionNumber);
}
break;
#if HAVE_PRAGMA_MARK
#pragma mark ----- End Lex Event-----
#endif
case kPGPEvent_EndLexEvent:
{
PGPEventEndLexData *d = &event->data.endLexData;
STATUS_LOG("--> End decoding section %d\n",d->sectionNumber);
}
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -