RTFileReadAllByHandleEx-generic.cpp revision a55e9030a4645802dc4c16eb1fa66378e3642850
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync/* $Id$ */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - RTFileReadAllByHandleEx, generic implementation.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2008 Sun Microsystems, Inc.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync *
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * available from http://www.virtualbox.org. This file is free software;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * The contents of this file may alternatively be used under the terms
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * of the Common Development and Distribution License Version 1.0
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution, in which case the provisions of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * CDDL are applicable instead of those of the GPL.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * You may elect to license modified versions of this file under the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * terms and conditions of either the GPL or the CDDL or both.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync *
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * additional information or have any questions.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
9c11b89c71ca727d975c39f2719063501ddcd03dvboxsync/*******************************************************************************
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync* Header Files *
9c11b89c71ca727d975c39f2719063501ddcd03dvboxsync*******************************************************************************/
9c11b89c71ca727d975c39f2719063501ddcd03dvboxsync#include <iprt/file.h>
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync#include "internal/iprt.h"
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
9c11b89c71ca727d975c39f2719063501ddcd03dvboxsync#include <iprt/mem.h>
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync#include <iprt/assert.h>
a7aa94e0115a73841f34ebbfa00f63fa1904e51fvboxsync#include <iprt/string.h>
8e034cd2a6b0c86697554278e970163287003bb9vboxsync#include <iprt/err.h>
8e034cd2a6b0c86697554278e970163287003bb9vboxsync
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsyncRTDECL(int) RTFileReadAllByHandleEx(RTFILE File, RTFOFF off, RTFOFF cbMax, uint32_t fFlags, void **ppvFile, size_t *pcbFile)
089c21a647af46044cad04a78cfdcfae814d2105vboxsync{
089c21a647af46044cad04a78cfdcfae814d2105vboxsync AssertReturn(!(fFlags & ~RTFILE_RDALL_VALID_MASK), VERR_INVALID_PARAMETER);
089c21a647af46044cad04a78cfdcfae814d2105vboxsync
089c21a647af46044cad04a78cfdcfae814d2105vboxsync /*
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Save the current offset first.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync */
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync RTFOFF offOrg;
5eda82e218d35ae0691febd531e1bfc0324cc4a6vboxsync int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, (uint64_t *)&offOrg);
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync if (RT_SUCCESS(rc))
4b37aac42309dbacba7ff11f16ed8d72a06b71e4vboxsync {
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /*
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync * Get the file size, adjust it and check that it might fit into memory.
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync */
089c21a647af46044cad04a78cfdcfae814d2105vboxsync RTFOFF cbFile;
0e5731ab59b4ecead38375f26eeea698f00b19fdvboxsync rc = RTFileSeek(File, 0,RTFILE_SEEK_END, (uint64_t *)&cbFile);
0e5731ab59b4ecead38375f26eeea698f00b19fdvboxsync if (RT_SUCCESS(rc))
8e034cd2a6b0c86697554278e970163287003bb9vboxsync {
8e034cd2a6b0c86697554278e970163287003bb9vboxsync RTFOFF cbAllocFile = cbFile > off ? cbFile - off : 0;
8e034cd2a6b0c86697554278e970163287003bb9vboxsync if (cbAllocFile > cbMax)
8e034cd2a6b0c86697554278e970163287003bb9vboxsync cbAllocFile = cbMax;
8e034cd2a6b0c86697554278e970163287003bb9vboxsync size_t cbAllocMem = (size_t)cbAllocFile;
8e034cd2a6b0c86697554278e970163287003bb9vboxsync if ((RTFOFF)cbAllocMem == cbAllocFile)
8e034cd2a6b0c86697554278e970163287003bb9vboxsync {
8e034cd2a6b0c86697554278e970163287003bb9vboxsync /*
8e034cd2a6b0c86697554278e970163287003bb9vboxsync * Try allocate the required memory and initialize the header (hardcoded fun).
8e034cd2a6b0c86697554278e970163287003bb9vboxsync */
26160a583507a31755f24f87663ce91a2b327036vboxsync void *pvHdr = RTMemAlloc(cbAllocMem + 32);
8e034cd2a6b0c86697554278e970163287003bb9vboxsync if (pvHdr)
8e034cd2a6b0c86697554278e970163287003bb9vboxsync {
8e034cd2a6b0c86697554278e970163287003bb9vboxsync memset(pvHdr, 0xff, 32);
2c203bb219df5391ed7f3888c85e12a4e47817davboxsync *(size_t *)pvHdr = cbAllocMem;
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync
29bdc01040c07a3dd482a94a2cb8f0a90f8587a7vboxsync /*
* Seek and read.
*/
rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
if (RT_SUCCESS(rc))
{
void *pvFile = (uint8_t *)pvHdr + 32;
rc = RTFileRead(File, pvFile, cbAllocMem, NULL);
if (RT_SUCCESS(rc))
{
/*
* Success - fill in the return values.
*/
*ppvFile = pvFile;
*pcbFile = cbAllocMem;
}
}
if (RT_FAILURE(rc))
RTMemFree(pvHdr);
}
else
rc = VERR_NO_MEMORY;
}
else
rc = VERR_TOO_MUCH_DATA;
}
/* restore the position. */
RTFileSeek(File, offOrg, RTFILE_SEEK_BEGIN, NULL);
}
return rc;
}
RT_EXPORT_SYMBOL(RTFileReadAllByHandleEx);