⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 int_attr.c

📁 cryptlib安全工具包
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*																			*
*				cryptlib Internal Attribute-list Manipulation API			*
*						Copyright Peter Gutmann 1992-2008					*
*																			*
****************************************************************************/

#if defined( INC_ALL )
  #include "crypt.h"
#else
  #include "crypt.h"
#endif /* Compiler-specific includes */

/* The minimum size of an attribute-list element (in this case for 
   sessions), used for error checking in debug mode.  The values are various 
   ints and pointers, and the 'previous' and 'next' pointer for the list 
   itself */

#define MIN_ATTRLIST_SIZE	( ( 7 * sizeof( int ) ) + \
							  ( 2 * sizeof( void * ) ) )

/* Movement codes for the attribute cursor */

typedef enum {
	CURSOR_MOVE_NONE,		/* No moveme type */
	CURSOR_MOVE_START,		/* Move to first attribute */
	CURSOR_MOVE_PREV,		/* Move to previous attribute */
	CURSOR_MOVE_NEXT,		/* Move to next attribute */
	CURSOR_MOVE_END,		/* Move to last attribute */
	CURSOR_MOVE_LAST		/* Last possible move type */
	} CURSOR_MOVE_TYPE;

/****************************************************************************
*																			*
*							Attribute Location Routines						*
*																			*
****************************************************************************/

/* Find the start and end of an attribute group from an attribute within
   the group */

CHECK_RETVAL_PTR STDC_NONNULL_ARG( ( 2 ) ) \
void *attributeFindStart( IN_OPT const void *attributePtr,
						  IN GETATTRFUNCTION getAttrFunction )
	{
	CRYPT_ATTRIBUTE_TYPE groupID;
	int iterationCount;

	assert( attributePtr == NULL || \
			isReadPtr( attributePtr, MIN_ATTRLIST_SIZE ) );
	
	REQUIRES_N( getAttrFunction != NULL );

	if( attributePtr == NULL )
		return( NULL );

	/* Move backwards until we find the start of the attribute */
	if( getAttrFunction( attributePtr, &groupID, NULL, NULL, 
						 ATTR_CURRENT ) == NULL )
		return( NULL );
	ENSURES_N( groupID != CRYPT_ATTRIBUTE_NONE );
	for( iterationCount = 0; iterationCount < FAILSAFE_ITERATIONS_MAX; 
		 iterationCount++ )
		{
		CRYPT_ATTRIBUTE_TYPE prevGroupID;
		const void *prevPtr;

		prevPtr = getAttrFunction( attributePtr, &prevGroupID, NULL, NULL,
								   ATTR_PREV );
		if( prevPtr == NULL || prevGroupID != groupID )
			{
			/* We've reached the start of the list or a different attribute
			   group, this is the start of the current group */
			break;
			}
		attributePtr = prevPtr;
		}
	ENSURES_N( iterationCount < FAILSAFE_ITERATIONS_MAX );

	return( ( void * ) attributePtr );
	}

CHECK_RETVAL_PTR STDC_NONNULL_ARG( ( 2 ) ) \
void *attributeFindEnd( IN_OPT const void *attributePtr,
						IN GETATTRFUNCTION getAttrFunction )
	{
	CRYPT_ATTRIBUTE_TYPE groupID;
	int iterationCount;

	assert( attributePtr == NULL || \
			isReadPtr( attributePtr, MIN_ATTRLIST_SIZE ) );

	REQUIRES_N( getAttrFunction != NULL );

	if( attributePtr == NULL )
		return( NULL );

	/* Move forwards until we're just before the start of the next
	   attribute */
	if( getAttrFunction( attributePtr, &groupID, NULL, NULL, 
						 ATTR_CURRENT ) == NULL )
		return( NULL );
	ENSURES_N( groupID != CRYPT_ATTRIBUTE_NONE );
	for( iterationCount = 0; iterationCount < FAILSAFE_ITERATIONS_MAX; 
		 iterationCount++ )
		{
		CRYPT_ATTRIBUTE_TYPE nextGroupID;
		const void *nextPtr;

		nextPtr = getAttrFunction( attributePtr, &nextGroupID, NULL, NULL,
								   ATTR_NEXT );
		if( nextPtr == NULL || nextGroupID != groupID )
			{
			/* We've reached the end of the list or a different attribute
			   group, this is the end of the current group */
			break;
			}
		attributePtr = nextPtr;
		}
	ENSURES_N( iterationCount < FAILSAFE_ITERATIONS_MAX );

	return( ( void * ) attributePtr );
	}

/* Find an attribute in a list of attributes */

CHECK_RETVAL_PTR STDC_NONNULL_ARG( ( 2 ) ) \
void *attributeFind( IN_OPT const void *attributePtr,
					 IN GETATTRFUNCTION getAttrFunction,
					 IN_ATTRIBUTE const CRYPT_ATTRIBUTE_TYPE attributeID,
					 IN_ENUM_OPT( CRYPT_ATTRIBUTE ) \
						const CRYPT_ATTRIBUTE_TYPE instanceID )
	{
	CRYPT_ATTRIBUTE_TYPE currAttributeID, currInstanceID;
	int iterationCount;

	assert( attributePtr == NULL || \
			isReadPtr( attributePtr, MIN_ATTRLIST_SIZE ) );

	REQUIRES_N( getAttrFunction != NULL );
	REQUIRES_N( isAttribute( attributeID ) || \
				isInternalAttribute( attributeID ) );
	REQUIRES_N( instanceID == CRYPT_ATTRIBUTE_NONE || \
				isAttribute( instanceID ) || \
				isInternalAttribute( instanceID ) );

	if( attributePtr == NULL )
		return( NULL );

	/* Find the attribute in the list */
	if( getAttrFunction( attributePtr, NULL, &currAttributeID, NULL, 
						 ATTR_CURRENT ) == NULL )
		return( NULL );
	ENSURES_N( currAttributeID != CRYPT_ATTRIBUTE_NONE );
	for( iterationCount = 0; 
		 attributePtr != NULL && currAttributeID != attributeID && \
			iterationCount < FAILSAFE_ITERATIONS_MAX;
		 iterationCount++ )
		{
		attributePtr = getAttrFunction( attributePtr, NULL,
										&currAttributeID, NULL,
										ATTR_NEXT );
		}
	ENSURES_N( iterationCount < FAILSAFE_ITERATIONS_MAX );
	if( attributePtr == NULL || instanceID == CRYPT_ATTRIBUTE_NONE )
		{
		/* If the attribute isn't present or we're not looking for a 
		   particular instance, we're done */
		return( ( void * ) attributePtr );
		}

	/* Find the attribute instance */
	if( getAttrFunction( attributePtr, NULL, &currAttributeID, 
						 &currInstanceID, ATTR_CURRENT ) == NULL )
		return( NULL );
	ENSURES_N( currAttributeID != CRYPT_ATTRIBUTE_NONE );
	for( iterationCount = 0; 
		 attributePtr != NULL && currAttributeID == attributeID && \
			iterationCount < FAILSAFE_ITERATIONS_MAX; 
		 iterationCount++ )
		{
		if( currInstanceID == instanceID )
			return( ( void * ) attributePtr );
		attributePtr = getAttrFunction( attributePtr, NULL,
										&currAttributeID, &currInstanceID,
										ATTR_NEXT );
		}
	ENSURES_N( iterationCount < FAILSAFE_ITERATIONS_MAX );

	return( NULL );
	}

/* Find the next instance of an attribute in an attribute group.  This is
   used to step through multiple instances of an attribute, for example in
   a cert extension containing a SEQUENCE OF <attribute> */

CHECK_RETVAL_PTR STDC_NONNULL_ARG( ( 2 ) ) \
void *attributeFindNextInstance( IN_OPT const void *attributePtr,
								 IN GETATTRFUNCTION getAttrFunction )
	{
	CRYPT_ATTRIBUTE_TYPE groupID, attributeID;
	CRYPT_ATTRIBUTE_TYPE currGroupID, currAttributeID;
	int iterationCount;

	assert( attributePtr == NULL || \
			isReadPtr( attributePtr, MIN_ATTRLIST_SIZE ) );

	REQUIRES_N( getAttrFunction != NULL );

	if( attributePtr == NULL )
		return( NULL );

	/* Skip the current field */
	if( getAttrFunction( attributePtr, &groupID, &attributeID, NULL, 
						 ATTR_CURRENT ) == NULL )
		return( NULL );
	ENSURES_N( groupID != CRYPT_ATTRIBUTE_NONE && \
			   attributeID != CRYPT_ATTRIBUTE_NONE );
	attributePtr = getAttrFunction( attributePtr, &currGroupID, 
									&currAttributeID, NULL, ATTR_NEXT );
	if( attributePtr == NULL )
		{
		/* No next attribute, we're done */
		return( NULL );
		}
	ENSURES_N( currGroupID != CRYPT_ATTRIBUTE_NONE );

	/* Step through the remaining attributes in the group looking for
	   another occurrence of the current attribute */
	for( iterationCount = 0; \
		 attributePtr != NULL && currGroupID == groupID && \
			iterationCount < FAILSAFE_ITERATIONS_MAX;
		 iterationCount++ )
		{
		if( currAttributeID == attributeID )
			return( ( void * ) attributePtr );
		attributePtr = getAttrFunction( attributePtr, &currGroupID,
										&currAttributeID, NULL,
										ATTR_NEXT );
		}
	ENSURES_N( iterationCount < FAILSAFE_ITERATIONS_MAX );

	/* We couldn't find another instance of the attribute in this group */
	return( NULL );
	}

/****************************************************************************
*																			*
*						Attribute Cursor Movement Routines					*
*																			*
****************************************************************************/

/* Moving the cursor by attribute group is a bit more complex than just 
   stepping forwards or backwards along the attribute list.  First we have 
   to find the start or end of the current group.  Then we move to the start 
   of the previous (via ATTR_PREV and attributeFindStart()), or start of the
   next (via ATTR_NEXT) group beyond that.  This has the effect of moving us 
   from anywhere in the current group to the start of the preceding or 
   following group.  Finally, we repeat this as required */

CHECK_RETVAL_PTR STDC_NONNULL_ARG( ( 1, 2 ) ) \
static const void *moveCursorByGroup( const void *currentCursor,
									  IN GETATTRFUNCTION getAttrFunction,
									  IN_ENUM( CURSOR_MOVE ) \
										const CURSOR_MOVE_TYPE cursorMoveType, 
									  IN_INT int count, 
									  const BOOLEAN absMove )
	{
	const void *newCursor = currentCursor, *lastCursor = NULL;
	int iterationCount;

	assert( isReadPtr( currentCursor, MIN_ATTRLIST_SIZE ) );

	REQUIRES_N( getAttrFunction != NULL );
	REQUIRES_N( cursorMoveType > CURSOR_MOVE_NONE && \
				cursorMoveType < CURSOR_MOVE_LAST );
	REQUIRES_N( count > 0 && count <= MAX_INTLENGTH );

	for( iterationCount = 0; \
		 count-- > 0 && newCursor != NULL && \
			iterationCount < FAILSAFE_ITERATIONS_MAX;
		 iterationCount++ )
		{
		lastCursor = newCursor;
		if( cursorMoveType == CURSOR_MOVE_START || \

⌨️ 快捷键说明

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