manifest3.cpp revision f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2e
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/* $Id$ */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/** @file
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * IPRT - Manifest, the bits with the most dependencies.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Copyright (C) 2010 Oracle Corporation
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * available from http://www.virtualbox.org. This file is free software;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * you can redistribute it and/or modify it under the terms of the GNU
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * General Public License (GPL) as published by the Free Software
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * The contents of this file may alternatively be used under the terms
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * of the Common Development and Distribution License Version 1.0
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * VirtualBox OSE distribution, in which case the provisions of the
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * CDDL are applicable instead of those of the GPL.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * You may elect to license modified versions of this file under the
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * terms and conditions of either the GPL or the CDDL or both.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/*******************************************************************************
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync* Header Files *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync*******************************************************************************/
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include "internal/iprt.h"
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/manifest.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/asm.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/assert.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/err.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/md5.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/mem.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/sha.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/string.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync#include <iprt/vfs.h>
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/*******************************************************************************
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync* Structures and Typedefs *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync*******************************************************************************/
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Hashes data.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Used when hashing a file, stream or similar.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsynctypedef struct RTMANIFESTHASHES
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The desired attribute types.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Only the hashes indicated by this will be calculated. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync uint32_t fAttrs;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The size. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTFOFF cbStream;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The MD5 context. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTMD5CONTEXT Md5Ctx;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-1 context. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSHA1CONTEXT Sha1Ctx;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-256 context. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSHA256CONTEXT Sha256Ctx;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-512 context. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSHA512CONTEXT Sha512Ctx;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The MD5 digest. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync uint8_t abMd5Digest[RTMD5_HASH_SIZE];
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-1 digest. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync uint8_t abSha1Digest[RTSHA1_HASH_SIZE];
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-256 digest. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync uint8_t abSha256Digest[RTSHA256_HASH_SIZE];
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-512 digest. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync uint8_t abSha512Digest[RTSHA512_HASH_SIZE];
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync} RTMANIFESTHASHES;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/** Pointer to a the hashes for a stream. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsynctypedef RTMANIFESTHASHES *PRTMANIFESTHASHES;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Creates a hashes structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns Pointer to a hashes structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param fAttrs The desired hashes, RTMANIFEST_ATTR_XXX.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncstatic PRTMANIFESTHASHES rtManifestHashesCreate(uint32_t fAttrs)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PRTMANIFESTHASHES pHashes = (PRTMANIFESTHASHES)RTMemTmpAllocZ(sizeof(*pHashes));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pHashes->fAttrs = fAttrs;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*pHashes->cbStream = 0;*/
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (fAttrs & RTMANIFEST_ATTR_MD5)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTMd5Init(&pHashes->Md5Ctx);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (fAttrs & RTMANIFEST_ATTR_SHA1)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha1Init(&pHashes->Sha1Ctx);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (fAttrs & RTMANIFEST_ATTR_SHA256)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha256Init(&pHashes->Sha256Ctx);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (fAttrs & RTMANIFEST_ATTR_SHA512)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha512Init(&pHashes->Sha512Ctx);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return pHashes;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Updates the hashes with a block of data.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pHashes The hashes structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pvBuf The data block.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param cbBuf The size of the data block.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncstatic void rtManifestHashesUpdate(PRTMANIFESTHASHES pHashes, void const *pvBuf, size_t cbBuf)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pHashes->cbStream += cbBuf;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_MD5)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTMd5Update(&pHashes->Md5Ctx, pvBuf, cbBuf);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_SHA1)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha1Update(&pHashes->Sha1Ctx, pvBuf, cbBuf);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_SHA256)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha256Update(&pHashes->Sha256Ctx, pvBuf, cbBuf);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_SHA512)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha512Update(&pHashes->Sha512Ctx, pvBuf, cbBuf);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Finalizes all the hashes.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pHashes The hashes structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncstatic void rtManifestHashesFinal(PRTMANIFESTHASHES pHashes)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_MD5)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTMd5Final(pHashes->abMd5Digest, &pHashes->Md5Ctx);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_SHA1)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha1Final(&pHashes->Sha1Ctx, pHashes->abSha1Digest);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_SHA256)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha256Final(&pHashes->Sha256Ctx, pHashes->abSha256Digest);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_SHA512)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha512Final(&pHashes->Sha512Ctx, pHashes->abSha512Digest);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Adds the hashes to a manifest entry.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns IPRT status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pHashes The hashes structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param hManifest The manifest to add them to.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pszEntry The entry name.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncstatic int rtManifestHashesSetAttrs(PRTMANIFESTHASHES pHashes, RTMANIFEST hManifest, const char *pszEntry)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync char szValue[RTSHA512_DIGEST_LEN + 8];
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc = VINF_SUCCESS;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc2;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_SIZE)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTStrPrintf(szValue, sizeof(szValue), "%RU64", (uint64_t)pHashes->cbStream);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTManifestEntrySetAttr(hManifest, pszEntry, "SIZE", szValue, RTMANIFEST_ATTR_SIZE);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = rc2;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_MD5)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTMd5ToString(pHashes->abMd5Digest, szValue, sizeof(szValue));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc2))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTManifestEntrySetAttr(hManifest, pszEntry, "MD5", szValue, RTMANIFEST_ATTR_MD5);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = rc2;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_SHA1)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTSha1ToString(pHashes->abSha1Digest, szValue, sizeof(szValue));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc2))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTManifestEntrySetAttr(hManifest, pszEntry, "SHA1", szValue, RTMANIFEST_ATTR_SHA1);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = rc2;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_SHA256)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTSha256ToString(pHashes->abSha256Digest, szValue, sizeof(szValue));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc2))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTManifestEntrySetAttr(hManifest, pszEntry, "SHA256", szValue, RTMANIFEST_ATTR_SHA256);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = rc2;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (pHashes->fAttrs & RTMANIFEST_ATTR_SHA512)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTSha512ToString(pHashes->abSha512Digest, szValue, sizeof(szValue));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc2))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTManifestEntrySetAttr(hManifest, pszEntry, "SHA512", szValue, RTMANIFEST_ATTR_SHA512);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = rc2;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Destroys the hashes.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pHashes The hashes structure. NULL is ignored.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncstatic void rtManifestHashesDestroy(PRTMANIFESTHASHES pHashes)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTMemTmpFree(pHashes);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/**
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Adds an entry for a file with the specified set of attributes.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns IPRT status code.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param hManifest The manifest handle.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param hVfsIos The I/O stream handle of the entry. This will
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * be processed to its end on successful return.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * (Must be positioned at the start to get
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * the expected results.)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pszEntry The entry name.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param fAttrs The attributes to create for this stream.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncRTDECL(int) RTManifestEntryAddIoStream(RTMANIFEST hManifest, RTVFSIOSTREAM hVfsIos, const char *pszEntry, uint32_t fAttrs)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync{
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Note! This is a convenicence function, so just use the available public
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * methods to get the job done.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertReturn(fAttrs < RTMANIFEST_ATTR_END, VERR_INVALID_PARAMETER);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertPtr(pszEntry);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Allocate and initialize the hash contexts, hash digests and I/O buffer.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PRTMANIFESTHASHES pHashes = rtManifestHashesCreate(fAttrs);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (!pHashes)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return VERR_NO_TMP_MEMORY;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync int rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync size_t cbBuf = _1M;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync void *pvBuf = RTMemTmpAlloc(cbBuf);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_UNLIKELY(!pvBuf))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync cbBuf = _4K;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync pvBuf = RTMemTmpAlloc(cbBuf);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_LIKELY(pvBuf))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Process the stream data.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync for (;;)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync size_t cbRead;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = RTVfsIoStrmRead(hVfsIos, pvBuf, cbBuf, true /*fBlocking*/, &cbRead);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if ( (rc == VINF_EOF && cbRead == 0)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync || RT_FAILURE(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync break;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rtManifestHashesUpdate(pHashes, pvBuf, cbBuf);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Add the entry with the finalized hashes.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rtManifestHashesFinal(pHashes);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = RTManifestEntryAdd(hManifest, pszEntry);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync if (RT_SUCCESS(rc))
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = rtManifestHashesSetAttrs(pHashes, hManifest, pszEntry);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync else
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync {
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rtManifestHashesDestroy(pHashes);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = VERR_NO_TMP_MEMORY;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync }
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync return rc;
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync}
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync