manifest3.cpp revision f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2e
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * IPRT - Manifest, the bits with the most dependencies.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Copyright (C) 2010 Oracle Corporation
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 * 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 * 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* Header Files *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync*******************************************************************************/
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/*******************************************************************************
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync* Structures and Typedefs *
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync*******************************************************************************/
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Hashes data.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Used when hashing a file, stream or similar.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The desired attribute types.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Only the hashes indicated by this will be calculated. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The size. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The MD5 context. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-1 context. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-256 context. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-512 context. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The MD5 digest. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-1 digest. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-256 digest. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /** The SHA-512 digest. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync/** Pointer to a the hashes for a stream. */
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Creates a hashes structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns Pointer to a hashes structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param fAttrs The desired hashes, RTMANIFEST_ATTR_XXX.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncstatic PRTMANIFESTHASHES rtManifestHashesCreate(uint32_t fAttrs)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PRTMANIFESTHASHES pHashes = (PRTMANIFESTHASHES)RTMemTmpAllocZ(sizeof(*pHashes));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync /*pHashes->cbStream = 0;*/
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Updates the hashes with a block of data.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pHashes The hashes structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pvBuf The data block.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param cbBuf The size of the data block.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncstatic void rtManifestHashesUpdate(PRTMANIFESTHASHES pHashes, void const *pvBuf, size_t cbBuf)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Finalizes all the hashes.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pHashes The hashes structure.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncstatic void rtManifestHashesFinal(PRTMANIFESTHASHES pHashes)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTMd5Final(pHashes->abMd5Digest, &pHashes->Md5Ctx);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha1Final(&pHashes->Sha1Ctx, pHashes->abSha1Digest);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha256Final(&pHashes->Sha256Ctx, pHashes->abSha256Digest);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTSha512Final(&pHashes->Sha512Ctx, pHashes->abSha512Digest);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Adds the hashes to a manifest entry.
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.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncstatic int rtManifestHashesSetAttrs(PRTMANIFESTHASHES pHashes, RTMANIFEST hManifest, const char *pszEntry)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync RTStrPrintf(szValue, sizeof(szValue), "%RU64", (uint64_t)pHashes->cbStream);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTManifestEntrySetAttr(hManifest, pszEntry, "SIZE", szValue, RTMANIFEST_ATTR_SIZE);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTMd5ToString(pHashes->abMd5Digest, szValue, sizeof(szValue));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTManifestEntrySetAttr(hManifest, pszEntry, "MD5", szValue, RTMANIFEST_ATTR_MD5);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTSha1ToString(pHashes->abSha1Digest, szValue, sizeof(szValue));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTManifestEntrySetAttr(hManifest, pszEntry, "SHA1", szValue, RTMANIFEST_ATTR_SHA1);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTSha256ToString(pHashes->abSha256Digest, szValue, sizeof(szValue));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTManifestEntrySetAttr(hManifest, pszEntry, "SHA256", szValue, RTMANIFEST_ATTR_SHA256);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTSha512ToString(pHashes->abSha512Digest, szValue, sizeof(szValue));
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc2 = RTManifestEntrySetAttr(hManifest, pszEntry, "SHA512", szValue, RTMANIFEST_ATTR_SHA512);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Destroys the hashes.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @param pHashes The hashes structure. NULL is ignored.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncstatic void rtManifestHashesDestroy(PRTMANIFESTHASHES pHashes)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Adds an entry for a file with the specified set of attributes.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * @returns IPRT status code.
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.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsyncRTDECL(int) RTManifestEntryAddIoStream(RTMANIFEST hManifest, RTVFSIOSTREAM hVfsIos, const char *pszEntry, uint32_t fAttrs)
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Note! This is a convenicence function, so just use the available public
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * methods to get the job done.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync AssertReturn(fAttrs < RTMANIFEST_ATTR_END, VERR_INVALID_PARAMETER);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Allocate and initialize the hash contexts, hash digests and I/O buffer.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync PRTMANIFESTHASHES pHashes = rtManifestHashesCreate(fAttrs);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Process the stream data.
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync rc = RTVfsIoStrmRead(hVfsIos, pvBuf, cbBuf, true /*fBlocking*/, &cbRead);
f1f5335f9ec8e56fe0e3e27f253e24b10ff20f2evboxsync * Add the entry with the finalized hashes.