isl_util.c

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

C
595
字号
/*-----------------------------------------------------------------------
 *      File:   util.c
 *
Copyright (c)  1999 - 2002 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.

 *---------------------------------------------------------------------
 */
/* 
 * INTEL CONFIDENTIAL 
 * This file, software, or program is supplied under the terms of a 
 * license agreement or nondisclosure agreement with Intel Corporation 
 * and may not be copied or disclosed except in accordance with the 
 * terms of that agreement. This file, software, or program contains 
 * copyrighted material and/or trade secret information of Intel 
 * Corporation, and must be treated as such. Intel reserves all rights 
 * in this material, except as the license agreement or nondisclosure 
 * agreement specifically indicate. 
 */ 
/* 
 * WARNING: EXPORT RESTRICTED. 
 * This software is subject to the U.S. Export Administration Regulations 
 * and other U.S. law, and may not be exported or re-exported to certain 
 * countries (currently Afghanistan (Taliban-controlled areas), Cuba, Iran, 
 * Iraq, Libya, North Korea, Serbia (except Kosovo), Sudan and Syria) or to 
 * persons or entities prohibited from receiving U.S. exports (including Denied 
 * Parties, Specially Designated Nationals, and entities on the Bureau of 
 * Export Administration Entity List or involved with missile technology or 
 * nuclear, chemical or biological weapons).
 */ 

//#include <stdio.h>
//#include <sys/stat.h>
#include "isl_internal.h"
#include "islutil.h"

static uint32 gErrorCode = 0;

/*-----------------------------------------------------------------------------
 * Name: isl_ParseFilePath
 *
 * Description:
 *
 * Parameters: 
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
ISL_STATUS
isl_ParseFilePath(
    ISL_CONST_DATA      FilePath,
    ISL_CONST_DATA_PTR  FileDir,
    ISL_CONST_DATA_PTR  FileName,
    ISL_CONST_DATA_PTR  FileTitle,
    ISL_CONST_DATA_PTR  FileExt)
{
	const uint8	*ptr;
	const uint8	*oldptr;
	const uint8 *endptr;
	const uint8	*pBS;
	const uint8	*pFS;
	uint32	ptrlen;

	if (!FilePath.Data) return ISL_FAIL;

	ptr = FilePath.Data;
	oldptr = FilePath.Data;
	endptr = FilePath.Data + FilePath.Length;
	while (ptr)	
	{
		oldptr = ++ptr;
		ptrlen = (uint32)(endptr - ptr);
		pBS = isl_memchr(oldptr, '\\', ptrlen);
		pFS = isl_memchr(oldptr, '/', ptrlen);
		ptr = (pBS > pFS) ? pBS : pFS;
	}

	ptr = isl_memchr(oldptr, '.', (uint32)(endptr - oldptr));
	ptr = (ptr)? ptr : endptr;
	if (FileExt)
	{
	   FileExt->Length = (uint32)(endptr - ptr) - 1;
	   FileExt->Data = ptr + 1;
	}
	if (FileTitle)
	{
	   FileTitle->Length = (uint32)(ptr - oldptr);
	   FileTitle->Data = oldptr;
	}
	if (FileName)
	{
	   FileName->Length = (uint32)(endptr - oldptr);
	   FileName->Data = oldptr;
	}
	if (FileDir)
	{
	   FileDir->Length = (uint32)(oldptr - FilePath.Data); 
	   FileDir->Data = FilePath.Data;
	}
	return ISL_OK;
}


#pragma warning( disable : 4035)
/*-----------------------------------------------------------------------------
 * Name: isl_memchr
 *
 * Description:
 *
 * Parameters: 
 *
 * Return value:
 * 
 * Error Codes:
 *---------------------------------------------------------------------------*/
void *isl_memchr(const void *s, char c, unsigned int n)
{
    unsigned int    Index = 0;
    char *          ret;
    int             Found = 0;

    ret = (void*)s;
    
    while (Index < n)
    {
        if (*ret == c)
        {
            Found = 1;
            break;
        }
        Index++;
        ret++;
    }
    
    if (!Found)
        ret = NULL;

    return ret;
}
#pragma warning( default : 4035)

void ISL_SetError(uint32 ErrCode)
{
	gErrorCode = ErrCode;
}

uint32 ISL_GetError()
{
	uint32 ErrCode;

	ErrCode = gErrorCode;
	gErrorCode = 0;
	return ErrCode;
}

/*-----------------------------------------------------------------------------
 * Name:  isl_VerifyDigestValues
 *
 * Description:
 *
 * Parameters:
 * Context (input) - Context used for GetDataMethods
 * AlgInfoPtr (input) - Digests to verify against
 * GetDataMethods (input) - GetDataMethods for retrieving data
 * GetParameters (input) - Parameters for GetDataMethods
 * OutputContext (input) - User context for callback
 * Update (input/output) - Callback for reporting data as being retrieved
 * for verification
 *
 * Return value:
 * ISL_OK if Verified
 * ISL_FAIL, otherwise
 *
 * Error Codes:
 *
 *---------------------------------------------------------------------------*/
ISL_STATUS isl_VerifyDigestValues(
	ISL_MANIFEST_SECTION_PTR		Context,
	ISL_ALG_INFO_PTR				AlgInfoPtr,
	ISL_GET_DATA_METHODS *			GetDataMethods,
 	ISL_CONST_DATA                  GetParameters,
    void *OutputContext,					/* object to update */
	ISL_STATUS (*Update)(					/* object's update method */
		void *UpdateContext,				/* (place OutputContext here) */
		ISL_CONST_DATA Buffer)) 			/* Length and Value of update */  
{
	/* variables to be allocated and freed */
  	CSSM_CC_HANDLE					*DigestHandlesPtr = NULL;
	ISL_GET_DATA_SERVICE_CONTEXT    *GetDataContext = NULL;
	ISL_DATA_PTR					DigestValues = NULL;
	uint32                          NumberOfDigests = 0;

	/* variables to be returned */
	ISL_STATUS retval = ISL_FAIL;

	/* intermediate variables */
    ISL_MEMORY_CONTEXT_PTR  MemoryPtr;
	ISL_CONST_DATA                  block;
    ISL_STATUS                      status;
	ISL_ERROR_CODE					error;
	ISL_ALG_INFO_PTR				digest;
	ISL_HASH_INFO_PTR hash;
    uint32                          ContextSize;
    uint32                          i;
	uint32							count;


	/* setup the get data routine */

    if (Context == NULL ||
		Context->Parent == NULL ||
		GetDataMethods == NULL ||
		GetDataMethods->SizeofObject == NULL ||
		GetDataMethods->InitializeWithClass == NULL ||
		GetDataMethods->Update == NULL ||
		GetDataMethods->Recycle == NULL)
	{
		return ISL_FAIL;
	}

	MemoryPtr = Context->Parent->Memory;
	ContextSize = (uint32)GetDataMethods->SizeofObject();
	GetDataContext = isl_AllocateMemory(MemoryPtr, ContextSize);
	if (GetDataContext == NULL) goto FAIL;

	status = GetDataMethods->InitializeWithClass(
		GetDataContext,
		GetParameters, 
		GetDataMethods->ServiceMethods.Class, 
		Context);
	if (status != ISL_OK) goto FAIL;

    NumberOfDigests = Context->SectionInfo.AlgCount;
    DigestHandlesPtr = isl_AllocateMemory(
        MemoryPtr, 
        NumberOfDigests * sizeof(CSSM_CC_HANDLE));
	if (DigestHandlesPtr == NULL) goto FAIL;
	DigestValues = isl_AllocateMemory(
        MemoryPtr, 
        NumberOfDigests * sizeof(ISL_DATA));
	if (DigestValues == NULL) goto FAIL;
/* Begin Hack */
/* The CSP is current unable to allocate digest data */
	for(i = 0; i < NumberOfDigests; i++)
	{
		DigestValues[i].Length = 20;
		DigestValues[i].Data = isl_AllocateMemory(MemoryPtr, DigestValues[i].Length);
		if (DigestValues[i].Data == NULL) goto FAIL;
	}
/* End Hack */

    /* setup all the digest algorithms */
	for (i = 0, digest = AlgInfoPtr; 
        digest; 
        i++, digest = digest->Next) 
    {
		if (digest->Methods != NULL)
		{
			DigestHandlesPtr[i] = CSSM_CSP_CreateDigestContext(
									digest->Methods->CSPHandle,
									digest->Methods->AlgorithmID);
		}
    }

	/* run all the data through all the digests */
	for (count = 0;;count++) 
	{
		error = GetDataMethods->Update(GetDataContext, &block);
		if (error != ISL_NO_ERROR) {
			goto FAIL;
		}

		/* need to handle corner case of an empty thing */
		/* so check for break only after first iteration */
		if (count != 0)
		{
			if (block.Length == 0)
			{
				break;
			}
			else
			{
				/* don't have staged hashes */
				goto FAIL;
			}
		}

        if (Update)
        {
            if (ISL_OK != (*Update)(OutputContext, block)) return ISL_FAIL;
        }

⌨️ 快捷键说明

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