isl_mparse.c

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

C
1,329
字号
		{
			SectionPtr->SectionName = Value;
		} else 
		{
			ISL_HASH_INFO_PTR HashNodePtr;

			HashInfoPtr = MatchDigest(Name, DigestPtr);
			if (HashInfoPtr == NULL) continue;

			/* Allocating a HashNode */
			HashNodePtr = (ISL_HASH_INFO_PTR) isl_AllocateMemory(
												MemoryPtr,
												sizeof(ISL_HASH_INFO));
			if(HashNodePtr == NULL) {
				return ISL_FAIL;
			}
			/* Initializing HashNode */
			HashNodePtr->Name = Name;
			HashNodePtr->Value.Length = IslUtil_Base64DecodeSize((ISL_DATA *)&Value);
			if (HashNodePtr->Value.Length == 0)
			{
				return ISL_FAIL;
			}
			HashNodePtr->Value.Data = isl_AllocateMemory(MemoryPtr, HashNodePtr->Value.Length);
			if (HashNodePtr->Value.Data == NULL)
			{
				return ISL_FAIL;
			}

			if (ISL_OK != IslUtil_Base64Decode((ISL_DATA *)&Value, (ISL_DATA *)&HashNodePtr->Value))
			{
				return ISL_FAIL;
			}
			HashNodePtr->Next = HashInfoPtr->Hash;
			/* Inserting at the head of the list */
			HashInfoPtr->Hash = HashNodePtr;
			HashInfoPtr->HashCount++;
		}
	}
	return ISL_OK;
}

/*-----------------------------------------------------------------------------
 * Name: isl_GetProtocolAndParameters
 *
 * Description:
 *
 * Parameters:
 * Name (input)
 * Protocol (output)
 * Parameters (output)
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
ISL_STATUS
isl_GetProtocolAndParameters(
	ISL_CONST_DATA Name, 
	ISL_CONST_DATA_PTR Protocol, 
	ISL_CONST_DATA_PTR Parameters)
{
	if (Protocol == NULL) return ISL_FAIL;
	if (Parameters == NULL) return ISL_FAIL;
	Parameters->Data = isl_memchr(Name.Data,':',Name.Length);
	if (Parameters->Data == NULL) return ISL_FAIL;
	Parameters->Data++;    /* skip over ':' */
	Parameters->Length = Name.Length - (uint32)(Parameters->Data - Name.Data);
	if (Parameters->Length > Name.Length) return ISL_FAIL;
	Protocol->Data = Name.Data;
	Protocol->Length = Name.Length - (Parameters->Length + 1);
	if (Protocol->Length > Name.Length) return ISL_FAIL;
	return ISL_OK;
}

/*-----------------------------------------------------------------------------
 * Name: isl_BuildHeaderSection
 *
 * Description:
 *
 * Parameters:
 * HeaderPtr (output)
 * MemoryPtr (input)
 * SectionInfoPtr (input)
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
ISL_STATUS
isl_BuildHeaderSection(
	ISL_HEADER_SECTION_PTR HeaderPtr,
	ISL_MEMORY_CONTEXT_PTR MemoryPtr,
	ISL_SECTION_INFO_PTR SectionInfoPtr)
{
    ISL_NAME_VALUE_PTR AttributeNodePtr;
	ISL_NAME_VALUE_PTR NameValuePtr;
	uint32 i;

	if (SectionInfoPtr->Attributes.NumberOfAttributes == 0) return ISL_FAIL;

	if (ISL_OK != ParseVersion(
					HeaderPtr, 
					SectionInfoPtr->Attributes.Attributes[0].Value))
		return ISL_FAIL;

	HeaderPtr->AttributeCount = SectionInfoPtr->Attributes.NumberOfAttributes;
	HeaderPtr->Attribute = isl_AllocateMemory(
        MemoryPtr,
        sizeof(ISL_LIST) * HeaderPtr->AttributeCount);
	if (HeaderPtr->Attribute == NULL)
	{
		return ISL_FAIL;
	}

    AttributeNodePtr = isl_AllocateMemory(
	    MemoryPtr,
		sizeof(ISL_NAME_VALUE)*HeaderPtr->AttributeCount);
    if (AttributeNodePtr == NULL)
    {
        return ISL_FAIL;
    }

	for(i=0;
		i < SectionInfoPtr->Attributes.NumberOfAttributes; 
		i++)
	{
		ISL_ATTRIBUTE_INFO_PTR AttributePtr;

		AttributePtr = SectionInfoPtr->Attributes.Attributes + i;
		if (AttributePtr->Info.AttributeNameFormat != 
            CSSM_DB_ATTRIBUTE_NAME_AS_BLOB)
		{
			return ISL_FAIL;
		}
		
        NameValuePtr = AttributeNodePtr + i;
        NameValuePtr->Name.Length = AttributePtr->Info.Label.Name.Length;
		NameValuePtr->Name.Data = AttributePtr->Info.Label.Name.Data;
		NameValuePtr->Value.Length = AttributePtr->Value.Length;
		NameValuePtr->Value.Data = AttributePtr->Value.Data;

        HeaderPtr->Attribute[i].Node = NameValuePtr;
        HeaderPtr->Attribute[i].Next = &(HeaderPtr->Attribute[i + 1]);
	}
	HeaderPtr->Attribute[HeaderPtr->AttributeCount - 1].Next = NULL;
    return ISL_OK;
}

/*-----------------------------------------------------------------------------
 * Name: isl_BuildManifestSection
 *
 * Description:
 *
 * Parameters:
 * ManifestSectionPtr (output)
 * SectionInfoPtr (input)
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
ISL_STATUS
isl_BuildManifestSection(
	ISL_MANIFEST_SECTION_PTR ManifestSectionPtr,
	ISL_SECTION_INFO_PTR SectionInfoPtr)
{
    ISL_SERVICE_CLASS_METHODS *methods;
	ISL_ARCHIVE_CONTEXT_PTR pArchive;
	ISL_SECTION_PTR SectionPtr;

	if (SectionInfoPtr == NULL) return ISL_FAIL;
	if (ManifestSectionPtr == NULL) return ISL_FAIL;
	pArchive = ManifestSectionPtr->Parent;
	if (pArchive == NULL) return ISL_FAIL;

	SectionPtr = &(ManifestSectionPtr->SectionInfo);
	
	if (InitializeSection(SectionPtr, SectionInfoPtr, pArchive) != ISL_OK)
	{
		return ISL_FAIL;
	}

	if (ManifestSectionPtr->SectionInfo.Name.Data == NULL) return ISL_FAIL;
	if (ManifestSectionPtr->SectionInfo.Name.Length != 13 ||
		cssm_memcmp(
			ManifestSectionPtr->SectionInfo.Name.Data,
			"00LocationMap",
			ManifestSectionPtr->SectionInfo.Name.Length) != 0)
	{
		if (isl_GetProtocolAndParameters(
				ManifestSectionPtr->SectionInfo.Name, 
				&ManifestSectionPtr->Protocol, 
				&ManifestSectionPtr->Parameters) != ISL_OK)
		{
			return ISL_FAIL;
		}
		methods = ArchiveConfigMethods.FindAlgorithm(
			pArchive->Config, 
			ManifestSectionPtr->Protocol);
		
		ManifestSectionPtr->GetDataMethods = (ISL_GET_DATA_METHODS *)methods;
	}
	else
	{
		/* special section */
	}
	return ISL_OK;
}

/*-----------------------------------------------------------------------------
 * Name: isl_BuildSigSection
 *
 * Description:
 *
 * Parameters:
 * ArchivePtr (input)
 * SigSectionPtr (output)
 * SectionInfoPtr (input)
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
ISL_STATUS
isl_BuildSigSection(
    ISL_SIG_SECTION_LIST_PTR SignedObjectListPtr,
    ISL_ARCHIVE_CONTEXT_PTR ArchivePtr,
	ISL_SIG_SECTION_PTR SignedObjectPtr,
	ISL_SECTION_INFO_PTR SectionInfoPtr)
{	
    ISL_LIST_PTR ManSectListPtr;
	ISL_SECTION_PTR	SectionPtr;

	if (SignedObjectPtr == NULL ||
	    ArchivePtr == NULL) 
        return ISL_FAIL;

    SignedObjectPtr->Parent = SignedObjectListPtr;
 	SectionPtr = &(SignedObjectPtr->SectionInfo);
	
	if (InitializeSection(SectionPtr, SectionInfoPtr, ArchivePtr) != ISL_OK)
	{
		return ISL_FAIL;
	}
 
	for (ManSectListPtr = ArchivePtr->Manifest.Section;
		 ManSectListPtr != NULL;
		 ManSectListPtr = ManSectListPtr->Next)
	{
	    ISL_MANIFEST_SECTION_PTR	ManifestSectionPtr;

        ManifestSectionPtr = ManSectListPtr->Node;
		if (IS_EQUAL(
            ManifestSectionPtr->SectionInfo.Name, 
            SignedObjectPtr->SectionInfo.Name))
		{
			SignedObjectPtr->ManSect = ManifestSectionPtr;
		}
	}
	
	if (SignedObjectPtr->ManSect == NULL) return ISL_FAIL;

	return ISL_OK;
}

/*-----------------------------------------------------------------------------
 * Name: MatchDigest
 *
 * Description:
 *
 * Parameters:
 * Name (input)
 * AlgList (input)
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
static ISL_ALG_INFO_PTR 
MatchDigest(
	ISL_CONST_DATA Name, 
	ISL_ALG_INFO_PTR AlgList)
{
	ISL_CONST_DATA AlgName;		/* digest name e.g. md5, sha-1 */
	uint32 i =0;

	if (Name.Length <= HASH_STR_LENGTH) return NULL;

	AlgName.Length = HASH_STR_LENGTH;

	for(i=0; i <= Name.Length - HASH_STR_LENGTH; i++)
	{
		AlgName.Data = Name.Data + i;

		if (IsHash(AlgName))
		{
			for(AlgName.Data = Name.Data, 	AlgName.Length = i;
				AlgList != NULL;
				AlgList = AlgList->Next)
			{
				if (IS_EQUAL(AlgName, AlgList->AlgName))
					return (AlgList);
			}

			return NULL;
		}
	}

	return NULL;	/* no match found */

}

/*-----------------------------------------------------------------------------
 * Name: AllocAlgList
 *
 * Description:
 *
 * Parameters:
 * MemoryPtr (input)
 * count (input)
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
static ISL_ALG_INFO_PTR
AllocAlgList(
	ISL_MEMORY_CONTEXT_PTR MemoryPtr,
	unsigned long count)				/* length of list */
{
	unsigned long			i = 0;			/* loop counter */
	ISL_ALG_INFO_PTR		list = NULL;	/* list to be allocated */

	/* allocating list of ISL_ALG_INFO */
	list = (ISL_ALG_INFO_PTR) isl_AllocateMemory(MemoryPtr, count*sizeof(ISL_ALG_INFO));
	if (!list) {
		return NULL;
	}

	/* initializing each item in the list */
	for(i=0; i < count; i++)
	{
		list[i].Parent = NULL;
		list[i].Next = list + (i+1);
	}

    list[count-1].Next = NULL;
	return list;
}

/*-----------------------------------------------------------------------------
 * Name: CountItemsInList
 *
 * Description:
 * Counts the number of items in a space separated list
 *
 * Parameters: 
 * ptr (input)
 * len (input)
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
static unsigned long
CountItemsInList(
	const unsigned char *ptr, 
	unsigned int len)
{
	unsigned long count = 0;

	while (len > 0)
	{
		while (len > 0 && *ptr == ' ' && !IS_NEWLINE(*ptr))
		{
			ptr++; len--;
		}

		if (len == 0 || IS_NEWLINE(*ptr))
			return count;

		count++;

		while (len > 0 && IS_HEADERCHAR(*ptr))
		{
			ptr++; len--;
		}
	}

	return count;
}

/*-----------------------------------------------------------------------------
 * Name: InitAlgList
 *
 * Description:
 * This function will initialize algorithm information list using the value
 * of an attribute called Digest-Algorithms
 *
 * Parameters:
 * list (input/output)			: list of algorithm infos
 * Value (input)				: space separated list of digest algorithms
 * pArchive (input)				: used for list of supported algorithms
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
static ISL_STATUS
InitAlgList(
	ISL_ALG_INFO_PTR AlgInfoPtr, 
	ISL_CONST_DATA Value,
	ISL_ARCHIVE_CONTEXT_PTR ArchivePtr)
{
	const uint8	        *oldptr = NULL;
    const uint8         *ptr = NULL;
	uint32	            oldlen = 0;
    uint32              len = 0;

	for(ptr = Value.Data, len = Value.Length;
		AlgInfoPtr != NULL;
		AlgInfoPtr = AlgInfoPtr->Next)
	{
        ISL_SERVICE_CLASS_METHODS *methods;

		if (!ptr)
			return ISL_FAIL;

		/* looking for beginning of token */
		while (len > 0 && !IS_HEADERCHAR(*ptr) )
		{
			ptr++;
			len--;
		}

		/* testing for valid length */
		if (!(len > 0))
			return ISL_FAIL;

		/* looking for end of token */
		oldptr = ptr;
		ptr = isl_memchr(oldptr, ' ', len);

⌨️ 快捷键说明

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