📄 pgpencsubr.c
字号:
PGPANN_PGPMIME_HEADER_LINES,
(unsigned char *)&mimeHeaderLines,
sizeof(mimeHeaderLines) ) ) )
goto error;
mimeBodyOff += mimeHeaderLines;
}
*mimeBodyOffPtr = mimeBodyOff;
}
if( IsntNull( mimeSeparator ) ) {
if( IsPGPError( err = pipeBuf->annotate( pipeBuf, NULL,
PGPANN_PGPMIME_SEPARATOR,
(unsigned char *)mimeSeparator,
kPGPMimeSeparatorSize ) ) )
goto error;
}
}
error:
return err;
}
/* Line ending control */
PGPLineEndType
pgpGetDefaultLineEndType ()
{
#if defined(PGP_MSDOS) || defined(PGP_WIN32)
return kPGPLineEnd_CRLF;
#elif PGP_MACINTOSH
return kPGPLineEnd_CR;
#else
return kPGPLineEnd_LF;
#endif
}
/* Parse an optionlist structure and extract callback info */
PGPError
pgpSetupCallback(
PGPOptionListRef optionList,
PGPEventHandlerProcPtr *func, /* Output params */
PGPUserValue *userValue,
PGPBoolean *fNullEvents
)
{
PGPError err;
PGPBoolean lNullEvents;
/* Init return data to default states */
pgpa( pgpaAddrValid( func, PGPEventHandlerProcPtr ) );
pgpa( pgpaAddrValid( userValue, PGPUserValue ) );
pgpa( pgpaAddrValid( fNullEvents, PGPBoolean ) );
*func = NULL;
*userValue = (PGPUserValue)0;
*fNullEvents = FALSE;
if( IsPGPError( err = pgpFindOptionArgs( optionList,
kPGPOptionType_EventHandler, FALSE,
"%p%p", func, userValue ) ) )
goto error;
if( IsPGPError( err = pgpFindOptionArgs( optionList,
kPGPOptionType_SendNullEvents, FALSE,
"%b", &lNullEvents ) ) )
goto error;
if( lNullEvents ) {
/* Wants notification of null events */
if( IsNull (*func) ) {
pgpDebugMsg(
"Error: Null events requested without event handler" );
err = kPGPError_BadParams;
goto error;
}
*fNullEvents = TRUE;
}
return kPGPError_NoErr;
error:
*func = NULL;
*userValue = (PGPUserValue)0;
*fNullEvents = FALSE;
return err;
}
/* Open input specification if there is one. Optionally set up filename
* literal structure, and seed RNG.
*/
PGPError
pgpSetupInput(
PGPContextRef context, /* Input params */
PGPOptionListRef optionList,
PgpLiteralParams *literal,
PGPRandomContext *rng,
PGPBoolean inputMode,
PGPBoolean detachedSignature,
PFLConstFileSpecRef *inFileRef, /* Output params */
PGPFileRead **pfrin,
PGPFileDataType *inFileDataType,
PGPBoolean *bLargeFile,
PGPByte **inBufPtr,
PGPSize *inBufLength
)
{
PGPOption op; /* Selected option from list */
PGPError err; /* Error return code */
static char fyeomagic[] = "_CONSOLE"; /* Display-only mode */
PGPUInt32 localEncode; /* Special input encoding (macbin) */
PGPFileOpenFlags localEncodeFlags; /* Flag form of localEncode */
PGPByte const *peekBuf; /* For RNG seeding from file */
PGPSize peekLength; /* Length of peekBuf */
PGPFileOffset fileSize;
/* Init return data to default states */
pgpa( pgpaAddrValid( inFileRef, PFLConstFileSpecRef ) );
pgpa( pgpaAddrValid( pfrin, PGPFileRead * ) );
pgpa( pgpaAddrValid( inFileDataType, PGPFileDataType ) );
pgpa( pgpaAddrValid( inBufPtr, PGPByte * ) );
pgpa( pgpaAddrValid( inBufLength, PGPSize ) );
*inFileRef = NULL;
*pfrin = NULL;
*inFileDataType = kPGPFileDataType_Unknown;
*inBufPtr = NULL;
*inBufLength = 0;
*bLargeFile=FALSE;
if( IsntNull( literal) ) {
pgpa( pgpaAddrValid( literal, PgpLiteralParams ) );
literal->filename = NULL;
literal->timestamp = ( PGPUInt32 ) 0;
}
if( IsntNull( literal ) ) {
/* First check "for your eyes only" mode, flag with magic filename */
PGPUInt32 fFYEO;
if( IsPGPError( err = pgpFindOptionArgs( optionList,
kPGPOptionType_ForYourEyesOnly, FALSE,
"%d", &fFYEO ) ) )
goto error;
if( fFYEO ) {
literal->filename = pgpAllocCString( context, fyeomagic );
}
}
/* See if there is an input file specified */
if( IsPGPError( err = pgpSearchOptionSingle( optionList,
kPGPOptionType_InputFileRef, &op ) ) )
goto error;
if( IsOp( op ) ) {
/* File input */
if( IsPGPError( err = pgpOptionPtr( &op, (void **)inFileRef ) ) )
goto error;
if( IsntNull( literal ) ) {
if( IsNull( literal->filename ) ) {
char *filerefname;
void *vfilerefname;
if( IsPGPError( err = pgpFindOptionArgs( optionList,
kPGPOptionType_InputFileName, FALSE,
"%p", &vfilerefname ) ) )
goto error;
filerefname = (char *)vfilerefname;
if( IsntNull( filerefname ) ) {
literal->filename = pgpAllocCString( context,
filerefname );
} else {
filerefname = pgpGetFileRefName( *inFileRef );
literal->filename = pgpAllocCString( context,
filerefname );
PGPFreeData( filerefname );
}
}
}
localEncodeFlags = (PGPFileOpenFlags)0;
if( inputMode ) {
if( IsPGPError( err = pgpSearchOptionSingle( optionList,
kPGPOptionType_LocalEncoding, &op ) ) )
goto error;
if( IsOp( op ) ) {
if( IsPGPError( err = pgpOptionUInt( &op, &localEncode ) ) )
goto error;
localEncodeFlags = pgpLocalEncodingToFlags( localEncode );
}
}
localEncodeFlags |= kPGPFileOpenReadPerm;
if( detachedSignature) {
localEncodeFlags |= kPGPFileOpenLocalEncodeHashOnly;
}
pgpCalcFileSize(*inFileRef,localEncodeFlags,&fileSize);
if(fileSize>0x000000007fffffff)
{
*bLargeFile=TRUE;
}
/* Open input file */
*pfrin = pgpFileRefReadCreate( context, *inFileRef,
localEncodeFlags, &err, inFileDataType );
if( IsNull( *pfrin ) ) {
goto error;
}
if( IsntNull( rng ) ) {
/* Add some data from the file to the RNG for more entropy */
peekBuf = pgpFileReadPeek( *pfrin, &peekLength );
pgpRandomAddBytes( rng, peekBuf, peekLength );
}
}
/* See if there is a buffer specified */
if( IsPGPError( err = pgpSearchOptionSingle( optionList,
kPGPOptionType_InputBuffer, &op ) ) )
goto error;
if( IsOp( op ) ) {
/* Buffer input */
if ( IsntNull( *inFileRef ) ) {
/* Error if buffer input also specified */
pgpDebugMsg( "Error: multiple input options" );
err = kPGPError_BadParams;
goto error;
}
if( IsntNull( literal ) ) {
/*
* Fake up filename if FYEO mode above didn't set it.
* Earlier versions of PGP used the fake filename "stdin".
* However this cannot be distinguished from the case where
* a file with that name is encrypted. We will change to using
* an empty string for this case, if no name was specified.
*/
if( IsNull( literal->filename ) ) {
char *filerefname;
void *vfilerefname;
if( IsPGPError( err = pgpFindOptionArgs( optionList,
kPGPOptionType_InputFileName, FALSE,
"%p", &vfilerefname ) ) )
goto error;
filerefname = (char *)vfilerefname;
if( IsNull( filerefname ) ) {
filerefname = (char *)"";
}
literal->filename = pgpAllocCString( context, filerefname );
}
}
if( IsPGPError( err = pgpOptionPtrLength( &op,
(void **)inBufPtr, inBufLength ) ) )
goto error;
if( IsntNull( rng ) ) {
/* Pre-seed RNG with some entropy from buffer */
pgpRandomAddBytes( rng, *inBufPtr, pgpMin( 1024, *inBufLength ) );
}
}
/* Must have some input */
if ( IsNull( *inBufPtr ) && IsNull( *inFileRef ) ) {
// pgpDebugMsg( "Error: no input options" );
err = kPGPError_BadParams;
goto error;
}
return kPGPError_NoErr;
error:
*inFileRef = NULL;
if( IsntNull( *pfrin ) ) {
pgpFileReadDestroy( *pfrin );
*pfrin = NULL;
}
*inBufPtr = NULL;
*inBufLength = 0;
if( IsntNull( literal ) ) {
if( IsntNull( literal->filename ) ) {
pgpContextMemFree( context, (char *)literal->filename );
literal->filename = NULL;
}
}
return err;
}
/*
* Copy all of input to a buffer, from optionlist. Leaves it in a
* buffer if it is already there. Returns *mustfree=TRUE if the
* caller must free the buffer after using it, which will be the case
* if we must allocate one.
*/
PGPError
pgpSetupInputToBuffer(
PGPContextRef context, /* Input params */
PGPOptionListRef optionList,
PGPByte **bufPtr, /* Output params */
PGPSize *bufLength,
PGPBoolean *mustFree
)
{
PFLConstFileSpecRef fileRef;
PGPFileRead *pfrin = NULL;
PGPPipeline *head = NULL;
PGPFileDataType inFileDataType;
PGPError err = kPGPError_NoErr;
PGPBoolean bLargeFile;
pgpAssert( IsntNull( bufPtr ) );
pgpAssert( IsntNull( bufLength ) );
pgpAssert( IsntNull( mustFree ) );
*bufPtr = NULL;
*bufLength = 0;
*mustFree = FALSE;
err = pgpSetupInput( context, optionList, NULL, NULL, FALSE, FALSE,
&fileRef, &pfrin, &inFileDataType, &bLargeFile, bufPtr, bufLength );
if( IsPGPError( err ) )
goto error;
if( IsntNull( pfrin ) ) {
/* Read whole file into memory buffer */
pgpVariableMemModCreate( context, &head, MAX_PGPSize );
if( IsNull( head ) ) {
err = kPGPError_OutOfMemory;
goto error;
}
err = pgpFileReadPump( pfrin, head );
if( IsPGPError( err ) )
goto error;
err = head->annotate( head, NULL, PGPANN_MEM_BUFSIZE,
(PGPByte *)bufLength, sizeof( *bufLength ) );
if( IsPGPError( err ) )
goto error;
err = head->annotate( head, NULL, PGPANN_MEM_MEMBUF,
(PGPByte *)bufPtr, sizeof( *bufPtr ) );
if( IsPGPError( err ) )
return err;
*mustFree = TRUE;
}
error:
if( IsntNull( pfrin ) )
pgpFileReadDestroy( pfrin );
if( IsntNull( head ) )
head->teardown( head );
if( IsPGPError( err ) ) {
*bufPtr = NULL;
*bufLength = 0;
*mustFree = FALSE;
}
return err;
}
/*
* Decode an input buffer which contains a PEM format base64 encoded
* certificate. Return in a dynamically allocated output buffer.
*/
PGPError
pgpRemovePEMEncoding(
PGPContextRef context, /* Input params */
PGPByte const *inBuf,
PGPSize inBufLength,
PGPByte **pOutBuf, /* Output params */
PGPSize *pOutBufLength )
{
void *outBuf = NULL;
PGPSize outBufLength = 0;
PGPSize inlen;
PGPInt32 outlen;
PGPError err = kPGPError_NoErr;
PGPMemoryMgrRef mgr;
static char sBegin[] = "-----BEGIN ";
static char sEnd[] = "-----END ";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -