📄 dbx_rpcc.c
字号:
}
return( CRYPT_OK );
}
/* Initialise query data prior to sending it to the database back-end */
static int initQueryData( COMMAND_INFO *cmd, const COMMAND_INFO *cmdTemplate,
BYTE *encodedDate, DBMS_INFO *dbmsInfo,
const char *command, const void *boundData,
const int boundDataLength, const time_t boundDate,
const int type )
{
int argIndex = 1;
memcpy( cmd, cmdTemplate, sizeof( COMMAND_INFO ) );
cmd->arg[ 0 ] = type;
if( command != NULL )
{
cmd->strArg[ 0 ] = ( void * ) command;
cmd->strArgLen[ 0 ] = strlen( command );
}
if( boundDate > 0 )
{
#ifndef SYSTEM_64BIT
assert( sizeof( time_t ) <= 4 );
#endif /* !SYSTEM_64BIT */
/* Encode the date as a 64-bit value */
memset( encodedDate, 0, 8 );
#ifdef SYSTEM_64BIT
encodedDate[ 3 ] = ( BYTE )( ( boundDate >> 32 ) & 0xFF );
#endif /* SYSTEM_64BIT */
encodedDate[ 4 ] = ( BYTE )( ( boundDate >> 24 ) & 0xFF );
encodedDate[ 5 ] = ( BYTE )( ( boundDate >> 16 ) & 0xFF );
encodedDate[ 6 ] = ( BYTE )( ( boundDate >> 8 ) & 0xFF );
encodedDate[ 7 ] = ( BYTE )( ( boundDate ) & 0xFF );
cmd->noStrArgs++;
cmd->strArg[ argIndex ] = encodedDate;
cmd->strArgLen[ argIndex++ ] = 8;
}
if( boundData != NULL )
{
/* Copy the bound data into non-ephemeral storage where it'll be
accessible to the back-end */
memcpy( dbmsInfo->boundData, boundData, boundDataLength );
cmd->noStrArgs++;
cmd->strArg[ argIndex ] = dbmsInfo->boundData;
cmd->strArgLen[ argIndex++ ] = boundDataLength;
}
return( argIndex );
}
/* Database access functions */
static int openDatabase( DBMS_INFO *dbmsInfo, const char *name,
const int nameLength, const int options,
int *featureFlags )
{
static const COMMAND_INFO cmdTemplate = \
{ DBX_COMMAND_OPEN, COMMAND_FLAG_NONE, 1, 1 };
COMMAND_INFO cmd;
int status;
assert( isWritePtr( dbmsInfo, sizeof( DBMS_INFO ) ) );
/* Dispatch the command */
memcpy( &cmd, &cmdTemplate, sizeof( COMMAND_INFO ) );
cmd.arg[ 0 ] = options;
cmd.strArg[ 0 ] = ( void * ) name;
cmd.strArgLen[ 0 ] = nameLength;
status = DISPATCH_COMMAND_DBX( cmdOpen, cmd, dbmsInfo );
if( cryptStatusOK( status ) && \
( cmd.arg[ 0 ] & DBMS_HAS_BINARYBLOBS ) )
dbmsInfo->flags |= DBMS_FLAG_BINARYBLOBS;
return( status );
}
static void closeDatabase( DBMS_INFO *dbmsInfo )
{
static const COMMAND_INFO cmdTemplate = \
{ DBX_COMMAND_CLOSE, COMMAND_FLAG_NONE, 0, 0 };
COMMAND_INFO cmd;
assert( isWritePtr( dbmsInfo, sizeof( DBMS_INFO ) ) );
/* Dispatch the command */
memcpy( &cmd, &cmdTemplate, sizeof( COMMAND_INFO ) );
DISPATCH_COMMAND_DBX( cmdClose, cmd, dbmsInfo );
}
static void performErrorQuery( DBMS_INFO *dbmsInfo )
{
static const COMMAND_INFO cmdTemplate = \
{ DBX_COMMAND_GETERRORINFO, COMMAND_FLAG_NONE, 0, 1 };
COMMAND_INFO cmd;
int status;
assert( isWritePtr( dbmsInfo, sizeof( DBMS_INFO ) ) );
/* Clear the return values */
memset( dbmsInfo->errorMessage, 0, MAX_ERRMSG_SIZE );
dbmsInfo->errorCode = 0;
/* Dispatch the command */
memcpy( &cmd, &cmdTemplate, sizeof( COMMAND_INFO ) );
cmd.strArg[ 0 ] = dbmsInfo->errorMessage;
cmd.strArgLen[ 0 ] = 0;
status = DISPATCH_COMMAND_DBX( cmdGetErrorInfo, cmd, dbmsInfo );
if( cryptStatusOK( status ) )
{
dbmsInfo->errorCode = cmd.arg[ 0 ];
dbmsInfo->errorMessage[ cmd.strArgLen[ 0 ] ] = '\0';
}
}
static int performUpdate( DBMS_INFO *dbmsInfo, const char *command,
const void *boundData, const int boundDataLength,
const time_t boundDate,
const DBMS_UPDATE_TYPE updateType )
{
static const COMMAND_INFO cmdTemplate = \
{ DBX_COMMAND_UPDATE, COMMAND_FLAG_NONE, 1, 1 };
COMMAND_INFO cmd;
BYTE encodedDate[ 8 + 8 ];
int status;
assert( isWritePtr( dbmsInfo, sizeof( DBMS_INFO ) ) );
assert( updateType > DBMS_UPDATE_NONE && \
updateType < DBMS_UPDATE_LAST );
/* If we're trying to abort a transaction that was never begun, don't
do anything */
if( updateType == DBMS_UPDATE_ABORT && \
!( dbmsInfo->flags & DBMS_FLAG_UPDATEACTIVE ) )
return( CRYPT_OK );
/* Dispatch the command */
initQueryData( &cmd, &cmdTemplate, encodedDate, dbmsInfo, command,
boundData, boundDataLength, boundDate, updateType );
status = DISPATCH_COMMAND_DBX( cmdUpdate, cmd, dbmsInfo );
if( cryptStatusError( status ) )
performErrorQuery( dbmsInfo );
else
{
/* If we're starting or ending an update, record the update state */
if( updateType == DBMS_UPDATE_BEGIN )
dbmsInfo->flags |= DBMS_FLAG_UPDATEACTIVE;
if( updateType == DBMS_UPDATE_COMMIT || \
updateType == DBMS_UPDATE_ABORT )
dbmsInfo->flags &= ~DBMS_FLAG_UPDATEACTIVE;
}
return( status );
}
static int performStaticUpdate( DBMS_INFO *dbmsInfo, const char *command )
{
return( performUpdate( dbmsInfo, command, NULL, 0, 0,
DBMS_UPDATE_NORMAL ) );
}
static int performQuery( DBMS_INFO *dbmsInfo, const char *command,
char *data, int *dataLength, const char *queryData,
const int queryDataLength, const time_t queryDate,
const DBMS_CACHEDQUERY_TYPE queryEntry,
const DBMS_QUERY_TYPE queryType )
{
static const COMMAND_INFO cmdTemplate = \
{ DBX_COMMAND_QUERY, COMMAND_FLAG_NONE, 2, 1 };
COMMAND_INFO cmd;
BYTE encodedDate[ 8 + 8 ];
int argIndex, status;
assert( isWritePtr( dbmsInfo, sizeof( DBMS_INFO ) ) );
assert( ( data == NULL && dataLength == NULL ) || \
isWritePtr( data, 16 ) );
assert( ( queryData == NULL && queryDataLength == 0 ) || \
( queryDataLength > 0 && \
isReadPtr( queryData, queryDataLength ) ) );
assert( queryEntry >= DBMS_CACHEDQUERY_NONE && \
queryEntry < DBMS_CACHEDQUERY_LAST );
assert( queryType > DBMS_QUERY_NONE && queryType < DBMS_QUERY_LAST );
/* Additional state checks: If we're starting a new query or performing
a point query there can't already be one active, and if we're
continuing or cancelling an existing query there has to be one
already active */
if( ( ( queryType == DBMS_QUERY_START || \
queryType == DBMS_QUERY_CHECK || \
queryType == DBMS_QUERY_NORMAL ) && \
( dbmsInfo->flags & DBMS_FLAG_QUERYACTIVE ) ) ||
( ( queryType == DBMS_QUERY_CONTINUE || \
queryType == DBMS_QUERY_CANCEL ) && \
!( dbmsInfo->flags & DBMS_FLAG_QUERYACTIVE ) ) )
retIntError();
/* Clear return value */
if( data != NULL )
{
memset( data, 0, 16 );
*dataLength = 0;
}
/* Dispatch the command */
argIndex = initQueryData( &cmd, &cmdTemplate, encodedDate, dbmsInfo,
command, queryData, queryDataLength,
queryDate, queryType );
cmd.arg[ 1 ] = queryEntry;
cmd.strArg[ argIndex ] = data;
cmd.strArgLen[ argIndex ] = 0;
cmd.noStrArgs = argIndex + 1;
status = DISPATCH_COMMAND_DBX( cmdQuery, cmd, dbmsInfo );
if( cryptStatusError( status ) )
{
performErrorQuery( dbmsInfo );
return( status );
}
/* Update the state information based on the query that we've just
performed */
if( queryType == DBMS_QUERY_START )
dbmsInfo->flags |= DBMS_FLAG_QUERYACTIVE;
if( queryType == DBMS_QUERY_CANCEL )
dbmsInfo->flags &= ~DBMS_FLAG_QUERYACTIVE;
if( dataLength != NULL )
{
*dataLength = cmd.strArgLen[ argIndex ];
if( *dataLength <= 0 || *dataLength > MAX_QUERY_RESULT_SIZE )
{
assert( NOTREACHED );
memset( data, 0, 16 );
*dataLength = 0;
return( CRYPT_ERROR_BADDATA );
}
}
return( CRYPT_OK );
}
static int performStaticQuery( DBMS_INFO *dbmsInfo, const char *command,
const DBMS_CACHEDQUERY_TYPE queryEntry,
const DBMS_QUERY_TYPE queryType )
{
return( performQuery( dbmsInfo, command, NULL, NULL, NULL, 0, 0,
queryEntry, queryType ) );
}
#endif /* USE_DBMS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -