manifest.cpp revision 25479956e51de7af723c8958182331275d25542d
/* $Id$ */
/** @file
* IPRT - Manifest file handling.
*/
/*
* Copyright (C) 2009-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/manifest.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Internal per file structure used by RTManifestVerify
*/
typedef struct RTMANIFESTFILEENTRY
{
char *pszManifestFile;
char *pszManifestDigest;
typedef RTMANIFESTFILEENTRY* PRTMANIFESTFILEENTRY;
/**
* Internal structure used for the progress callback
*/
typedef struct RTMANIFESTCALLBACKDATA
{
void *pvUser;
/*******************************************************************************
* Private functions
*******************************************************************************/
{
if (RT_UNLIKELY(*pb == c))
return pb;
return NULL;
}
{
if (RT_UNLIKELY(*pb == c))
return i;
return cb;
}
{
}
/*******************************************************************************
* Public functions
*******************************************************************************/
RTR3DECL(int) RTManifestVerify(const char *pszManifestFile, PRTMANIFESTTEST paTests, size_t cTests, size_t *piFailed)
{
/* Validate input */
/* Open the manifest file */
if (RT_FAILURE(rc))
return rc;
void *pvBuf = 0;
do
{
if (RT_FAILURE(rc))
break;
/* Cast down for the case size_t < uint64_t. This isn't really correct,
but we consider manifest files bigger than size_t as not supported
by now. */
if (!pvBuf)
{
rc = VERR_NO_MEMORY;
break;
}
if (RT_FAILURE(rc))
break;
}while (0);
/* Cleanup */
if (pvBuf)
return rc;
}
RTR3DECL(int) RTManifestVerifyFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, size_t *piFailed,
{
/* Validate input */
int rc = VINF_SUCCESS;
/* Create our compare list */
if (!paFiles)
return VERR_NO_MEMORY;
/* Fill our compare list */
{
char *pszDigest;
if (pfnProgressCallback)
{
callback.cCurrentFile = i;
}
else
if (RT_FAILURE(rc))
break;
}
/* Do the verification */
if (RT_SUCCESS(rc))
/* Cleanup */
{
if (paFiles[i].pszTestDigest)
}
return rc;
}
{
/* Validate input */
if (RT_FAILURE(rc))
return rc;
PRTMANIFESTTEST paFiles = 0;
void *pvBuf = 0;
do
{
if (!paFiles)
{
rc = VERR_NO_MEMORY;
break;
}
{
/* Calculate the SHA1 digest of every file */
if (pfnProgressCallback)
{
callback.cCurrentFile = i;
rc = RTSha1DigestFromFile(paFiles[i].pszTestFile, (char**)&paFiles[i].pszTestDigest, rtSHAProgressCallback, &callback);
}
else
if (RT_FAILURE(rc))
break;
}
if (RT_SUCCESS(rc))
{
if (RT_FAILURE(rc))
break;
}
}while (0);
/* Cleanup */
if (pvBuf)
if (paFiles)
{
if (paFiles[i].pszTestDigest)
}
/* Delete the manifest file on failure */
if (RT_FAILURE(rc))
return rc;
}
{
/* Validate input */
int rc = VINF_SUCCESS;
/* Parse the manifest file line by line */
for (;;)
{
{
break;
}
if ( ( cch == 1
&& pcBuf[0] == '\n')
|| ( cch == 2
&& pcBuf[0] == '\r'
{
continue;
}
/* Check for the digest algorithm */
if (pcBuf[0] == 'S'
{
break;
}
else if (pcBuf[0] == 'S'
{
break;
}
}
return rc;
}
RTR3DECL(int) RTManifestVerifyFilesBuf(void *pvBuf, size_t cbSize, PRTMANIFESTTEST paTests, size_t cTests, size_t *piFailed)
{
/* Validate input */
int rc = VINF_SUCCESS;
PRTMANIFESTFILEENTRY paFiles = (PRTMANIFESTFILEENTRY)RTMemTmpAllocZ(sizeof(RTMANIFESTFILEENTRY) * cTests);
if (!paFiles)
return VERR_NO_MEMORY;
/* Fill our compare list */
/* Parse the manifest file line by line */
for (;;)
{
break;
if ( ( cch == 1
&& pcBuf[0] == '\n')
|| ( cch == 2
&& pcBuf[0] == '\r'
{
continue;
}
/** @todo r=bird:
* -# Better deal with this EOF line platform dependency
* -# The SHA1 test should probably include a blank space check.
* -# If there is a specific order to the elements in the string, it would be
* good if the delimiter searching checked for it.
* -# Deal with filenames containing delimiter characters.
*/
/* Check for the digest algorithm */
if ( cch < 4 ||
(!( pcBuf[0] == 'S'
&&
!( pcBuf[0] == 'S'
)
{
/* Digest unsupported */
break;
}
/* Try to find the filename */
if (!pszNameStart)
{
break;
}
if (!pszNameEnd)
{
break;
}
/* Copy the filename part */
if (!pszName)
{
rc = VERR_NO_MEMORY;
break;
}
/* Try to find the digest sum */
if (!pszDigestStart)
{
break;
}
if (!pszDigestEnd)
if (!pszDigestEnd)
{
break;
}
/* Copy the digest part */
if (!pszDigest)
{
rc = VERR_NO_MEMORY;
break;
}
/* Check our file list against the extracted data */
bool fFound = false;
{
{
/* Add the data of the manifest file to the file list */
fFound = true;
break;
}
}
if (!fFound)
{
/* There have to be an entry in the file list */
break;
}
}
if ( rc == VINF_SUCCESS
{
rc = VINF_SUCCESS;
{
/* If there is an entry in the file list, which hasn't an
* equivalent in the manifest file, its an error. */
if ( !paFiles[i].pszManifestFile
|| !paFiles[i].pszManifestDigest)
{
break;
}
/* Do the manifest SHA digest match against the actual digest? */
{
if (piFailed)
*piFailed = i;
break;
}
}
}
/* Cleanup */
{
if (paFiles[i].pszManifestFile)
if (paFiles[i].pszManifestDigest)
}
return rc;
}
RTR3DECL(int) RTManifestWriteFilesBuf(void **ppvBuf, size_t *pcbSize, RTDIGESTTYPE enmDigestType, PRTMANIFESTTEST paFiles, size_t cFiles)
{
/* Validate input */
const char *pcszDigestType;
switch (enmDigestType)
{
default: return VERR_INVALID_PARAMETER;
}
/* Calculate the size necessary for the memory buffer. */
{
+ 6;
}
/* Create the memory buffer */
if (!pvBuf)
return VERR_NO_MEMORY;
/* Allocate a temporary string buffer. */
if (!pszTmp)
{
return VERR_NO_MEMORY;
}
{
size_t cch = RTStrPrintf(pszTmp, cbMaxSize + 1, "%s (%s)= %s\n", pcszDigestType, RTPathFilename(paFiles[i].pszTestFile), paFiles[i].pszTestDigest);
}
/* Results */
return VINF_SUCCESS;
}