isl_mparse.c

来自「Next BIOS Source code : Extensible Firmw」· C语言 代码 · 共 1,329 行 · 第 1/3 页

C
1,329
字号
	
		/* calculate the length of token */
		oldlen = (ptr) ? (uint32)(ptr-oldptr) : len;

		/* initialize AlgName */
        AlgInfoPtr->AlgName.Length = oldlen;
        AlgInfoPtr->AlgName.Data = isl_CopyMemory(ArchivePtr->Memory, oldptr, oldlen);
        if (AlgInfoPtr->AlgName.Data == NULL) {
			return ISL_FAIL;
        }
        methods = ArchiveConfigMethods.FindAlgorithm(ArchivePtr->Config, AlgInfoPtr->AlgName);
		if (methods) {
			AlgInfoPtr->Methods = (ISL_DIGEST_METHODS *) methods;
		}
		/* update length of ptr*/
		len -= oldlen;
	}

	return ISL_OK;
}

/*-----------------------------------------------------------------------------
 * Name: ParseAttribute
 *
 * Description:
 * This function will accept a buffer and produce a name/value pair. The output
 * ptrs point to portions of the buffer!
 *
 * Parameters: 
 * Input (input)			: buffer to be parsed
 * UpdatedInput (output)	: ptr to remaining buffer
 * NameImage (output)		: ptr to name
 * ValueImage (output)		: ptr to value
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
ISL_STATUS
ParseAttribute(
	ISL_CONST_DATA Input,
	ISL_CONST_DATA_PTR UpdatedInput,
	ISL_CONST_DATA_PTR NameImage,
	ISL_CONST_DATA_PTR ValueImage)
{
    const unsigned char *endptr;		/* pointer to the end of a token */

	if (NameImage == NULL) return ISL_FAIL;
	if (ValueImage == NULL) return ISL_FAIL;

	endptr = Input.Data + Input.Length;

    /* Find Name (oldptr, oldlen) */
	NameImage->Data = Input.Data;
	UpdatedInput->Data = isl_memchr(Input.Data, ':', Input.Length);		
	if (UpdatedInput->Data == NULL) return ISL_FAIL;
	NameImage->Length = (uint32)(UpdatedInput->Data - Input.Data);
	if (NameImage->Length > 70) return ISL_FAIL;

	/* update input pointers (ptr, maxlen) */
	UpdatedInput->Data++;									/* skip ':' */
	UpdatedInput->Data++;									/* skip SPACE */
	if (UpdatedInput->Data > endptr) return ISL_FAIL;		/* check to see if in la-la land */
	UpdatedInput->Length = (uint32)(endptr - UpdatedInput->Data);		/* update length */	
	
	/* Initialize Value */
	ValueImage->Data = UpdatedInput->Data;

	while (endptr > UpdatedInput->Data)
	{
		Input = *UpdatedInput;
		if (ISL_OK != ParseToNewline(Input, UpdatedInput)) return ISL_FAIL;
		if (UpdatedInput->Length == 0)
		{
			ValueImage->Length = (uint32)(UpdatedInput->Data - ValueImage->Data);
			return ISL_OK;
		}
		if (*(UpdatedInput->Data) != ' ') break;			/* no continuation exit loop */
		UpdatedInput->Data++;
		UpdatedInput->Length--;
	}

	ValueImage->Length = (uint32)(UpdatedInput->Data - ValueImage->Data);

	return ISL_OK;
}

/*-----------------------------------------------------------------------------
 * Name: CountAttributes
 *
 * Description:
 * This functions counts the number of attributes in a buffer containing a
 * section
 *
 * Parameters:
 * Input (input)		: a buffer containing a section
 *
 * Return value:
 * The number of attributes in the buffer containing a section
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
uint32
CountAttributes(
	ISL_CONST_DATA Input)
{
	uint32 count = 0;
	ISL_CONST_DATA UpdatedInput;
	ISL_CONST_DATA NameImage;
	ISL_CONST_DATA ValueImage;

	while(	Input.Length > 0 &&
			ParseAttribute(Input, &UpdatedInput, &NameImage, &ValueImage) == ISL_OK )
	{
		Input = UpdatedInput;
		count++;
	}
	return count;
}

/*-----------------------------------------------------------------------------
 * Name: CountSections
 *
 * Description:
 * This function will count the number of sections in a buffer
 *
 * Parameters:
 * Input (input)		: buffer to be inspected for sections
 *
 * Return value:
 * Number of sections in the buffer
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
uint32
CountSections(
	ISL_CONST_DATA Input)
{
	uint32 count = 0;
	ISL_CONST_DATA UpdatedInput;
	ISL_CONST_DATA SectionImage;

	while(	Input.Length > 0 &&
			ParseToEndOfSection(Input, &UpdatedInput, &SectionImage) == ISL_OK )
	{
		Input = UpdatedInput;
		count++;
	}
	return count;
}
/*-----------------------------------------------------------------------------
 * Name: StripAttributeValue
 *
 * Description:
 * This function will strip the attribute value of newline characters and
 * continutation characters. This process happens in place with the buffer
 * containing the attribute value being modified.
 *
 * Parameters:
 * AttributeInfoPtr (input/ouput)
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
ISL_STATUS StripAttributeValue(
	ISL_ATTRIBUTE_INFO_PTR AttributeInfoPtr)
{
	uint32 oldlen;
	uint8 *oldptr;
	uint8 *endptr;
	uint8 *DataPtr;

	if (AttributeInfoPtr == NULL) return ISL_FAIL;

	oldptr = AttributeInfoPtr->Value.Data;
	oldlen = AttributeInfoPtr->Value.Length;
	endptr = AttributeInfoPtr->Value.Data + AttributeInfoPtr->Value.Length;
	DataPtr = AttributeInfoPtr->Value.Data;
	while (oldptr < endptr) 
	{
		uint8 *CR;
		uint8 *LF;
		uint8 *ptr;

		CR = isl_memchr(oldptr, '\r', oldlen);
		LF = isl_memchr(oldptr, '\n', oldlen);
		ptr = (CR) ? CR : LF;
        if (ptr == NULL)							/* testing for newline */
		{
			cssm_memmove(DataPtr, oldptr, oldlen);	/* moving in-place */
			DataPtr += oldlen;						/* increment counter */
			break;									/* exit */
		}
		else
		{
			oldlen = (uint32)(ptr - oldptr);		/* calculating length */
			cssm_memmove(DataPtr, oldptr, oldlen);	/* moving in-place */
			DataPtr += oldlen;						/* incrementing length */
		}
		ptr = (LF) ? LF : CR;						/* finding newline */
		ptr++;										/* skipping newline */
        if (ptr < endptr && *ptr == ' ')			/* testing for continuation */
            ptr++;									/* skipping space */
        oldptr = ptr;
		oldlen = (uint32)(endptr - oldptr);
	}
	AttributeInfoPtr->Value.Length = (uint32)(DataPtr - AttributeInfoPtr->Value.Data);
	return ISL_OK;
}

/*-----------------------------------------------------------------------------
 * Name: ParseSectionAttributes
 *
 * Description:
 * Given a pointer a section image, this function will parse and return the
 * ptrs to all attributes in the section. It expects that the number of 
 * attributes has been pre-calculated with CountAttributes.
 *
 * Parameters: 
 * ArchivePtr (input)			: archive context for memory funcs
 * SectionInfoPtr (output)		: struct to be filled with attributes
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
ISL_STATUS
ParseSectionAttributes(
	ISL_MEMORY_CONTEXT_PTR MemoryPtr,
	ISL_SECTION_INFO_PTR SectionInfoPtr)
{
	uint32 i = 0;
	ISL_CONST_DATA Input;
	ISL_CONST_DATA UpdatedInput;
	ISL_CONST_DATA Name;
	ISL_CONST_DATA Value;
	ISL_ATTRIBUTE_INFO_PTR AttributeInfoPtr;

	if (SectionInfoPtr == NULL) return ISL_FAIL;

	Input.Length = SectionInfoPtr->Image.Length;
	Input.Data = isl_CopyMemory(
        MemoryPtr, 
        SectionInfoPtr->Image.Data, 
        Input.Length);
	if (Input.Data == NULL)
		return ISL_FAIL;

	for(i = 0; i < SectionInfoPtr->Attributes.NumberOfAttributes; i++)
	{
		if (ParseAttribute(Input, &UpdatedInput, &Name, &Value) != ISL_OK) 
			return ISL_FAIL;

		AttributeInfoPtr = SectionInfoPtr->Attributes.Attributes + i;

		AttributeInfoPtr->Info.AttributeNameFormat = 
            CSSM_DB_ATTRIBUTE_NAME_AS_BLOB;

		AttributeInfoPtr->Info.Label.Name.Data = (uint8 *)Name.Data;
		AttributeInfoPtr->Info.Label.Name.Length = Name.Length;

		AttributeInfoPtr->Info.AttributeFormat = CSSM_DB_ATTRIBUTE_FORMAT_BLOB;
		AttributeInfoPtr->Value.Data = (uint8 *)Value.Data;
		AttributeInfoPtr->Value.Length = Value.Length;
		if (ISL_OK != StripAttributeValue(AttributeInfoPtr))
			return ISL_FAIL;
		Input = UpdatedInput;
	}
	return ISL_OK;
}

/*-----------------------------------------------------------------------------
 * Name: isl_JarFileDecode
 *
 * Description:
 * This function parse an input buffer into a SectionInfoGroup -- sections &
 * attributes for each section
 *
 * Parameters: 
 * ArchivePtr (input)				: archive context for memory funcs
 * Input (input)					: buffer to be parse
 * SectionInfoGroupPtr (output)		: struct to be filled from the parse
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
ISL_STATUS
isl_JarFileDecode(
	ISL_MEMORY_CONTEXT_PTR MemoryPtr,
	ISL_CONST_DATA Input,
	ISL_SECTION_INFO_GROUP_PTR SectionInfoGroupPtr)
{
	uint32 TotalSize = 0;
	uint32 NumberOfSections = 0;
	uint32 NumberOfAttributes = 0;
	uint32 i;
	ISL_CONST_DATA UpdatedInput;
	ISL_CONST_DATA SectionImage;
	ISL_SECTION_INFO_PTR SectionInfoPtrs;
	ISL_ATTRIBUTE_INFO_PTR AttributeInfoPtrs;


	if (SectionInfoGroupPtr == NULL) return ISL_FAIL;

	NumberOfSections = CountSections(Input);
	if (NumberOfSections == 0) return ISL_FAIL;
	TotalSize = NumberOfSections * sizeof(ISL_SECTION_INFO);

	SectionInfoPtrs = isl_AllocateMemory(
						MemoryPtr,
						NumberOfSections * sizeof(ISL_SECTION_INFO));
	if (SectionInfoPtrs == NULL) return ISL_FAIL;

	SectionInfoGroupPtr->NumberOfSections = NumberOfSections;
	SectionInfoGroupPtr->Sections = SectionInfoPtrs;

	for(i = 0; i < NumberOfSections; i++)
	{
		if (ParseToEndOfSection(Input, &UpdatedInput, &SectionImage) != ISL_OK)
        {
            return ISL_FAIL;
        }

		SectionInfoPtrs[i].Image = SectionImage;
		Input = UpdatedInput;
	}

	for(i=0; i < NumberOfSections; i++)
	{
		NumberOfAttributes = CountAttributes(SectionInfoPtrs[i].Image);
		TotalSize += NumberOfAttributes*sizeof(ISL_ATTRIBUTE_INFO);

		AttributeInfoPtrs = isl_AllocateMemory(
		    MemoryPtr,
		    NumberOfAttributes * sizeof(ISL_ATTRIBUTE_INFO));
		if (AttributeInfoPtrs == NULL) return ISL_FAIL;

		SectionInfoPtrs[i].Attributes.NumberOfAttributes = NumberOfAttributes;
		SectionInfoPtrs[i].Attributes.Attributes = AttributeInfoPtrs;
	}

	for(i=0; i < NumberOfSections; i++)
	{
		if (ISL_OK != ParseSectionAttributes(MemoryPtr, SectionInfoPtrs +i))
        {
            return ISL_FAIL;
        }
	}

	return ISL_OK;
}

/*-----------------------------------------------------------------------------
 * Name: ParseToEndOfSection
 *
 * Description:
 * This function will scan a buffer for a section
 *
 * Parameters: 
 * Input (input)				: buffer to be parsed 
 * UpdatedInput (output)	    : ptr to the end of section 
 * SectionImage (output)	    : ptr to the section
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
ISL_STATUS
ParseToEndOfSection(
	ISL_CONST_DATA Input,				
	ISL_CONST_DATA_PTR UpdatedInput,	
	ISL_CONST_DATA_PTR SectionImage)	
{
	const uint8 *endptr;

	if (UpdatedInput == NULL) return ISL_FAIL;
	if (SectionImage == NULL) return ISL_FAIL;
	if (Input.Data == NULL) return ISL_FAIL;

	SectionImage->Data = Input.Data;			/* pointer to start of token */
	endptr = Input.Data + Input.Length;			/* pointer to end of buffer */
	do {
		if (ISL_OK != ParseToNewline(Input, UpdatedInput)) return ISL_FAIL;
		if (UpdatedInput->Length == 0) 
		{
			/* corner case when no trailing CR/LF */
			SectionImage->Length = (uint32)(UpdatedInput->Data - SectionImage->Data);
			return ISL_OK;
		}
		Input = *UpdatedInput;
	}
	while(*(UpdatedInput->Data) != '\r' && *(UpdatedInput->Data) != '\n');

	if ( (UpdatedInput->Length > 1) &&
		*(UpdatedInput->Data) == '\r' &&
		*(UpdatedInput->Data + 1) == '\n' )
	{
		UpdatedInput->Data++;
		UpdatedInput->Length --;
	}
	UpdatedInput->Data++;
	UpdatedInput->Length--;
	SectionImage->Length = (uint32)(UpdatedInput->Data - SectionImage->Data);
	return ISL_OK;
}

/*-----------------------------------------------------------------------------
 * Name: ParseToNewline
 *
 * Description:
 * This function will scan a buffer for a newline
 *
 * Parameters: 
 * Input (input)				: buffer to be parsed 
 * UpdatedInput (output)		: pointer to new line
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
ISL_STATUS
ParseToNewline(
	ISL_CONST_DATA Input,				/* buffer to be parsed */
	ISL_CONST_DATA_PTR UpdatedInput)
{
    const uint8 *pCR;
    const uint8 *pLF;

	pCR = isl_memchr(Input.Data, '\r',Input.Length);
    pLF = isl_memchr(Input.Data, '\n',Input.Length);	

    UpdatedInput->Data = (pLF) ? pLF : pCR;		    /* (CR/LF or CR) or LF */
	if (UpdatedInput->Data == NULL)					/* test for newline */
		return ISL_FAIL;							/* test failed */
	UpdatedInput->Data++;							/* skip over newline */
	UpdatedInput->Length = Input.Length - (uint32)(UpdatedInput->Data - Input.Data);
	return ISL_OK;
}


⌨️ 快捷键说明

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