VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/RTFileReadAllByHandleEx-generic.cpp@ 8925

Last change on this file since 8925 was 8925, checked in by vboxsync, 16 years ago

New file APIs for reading (or mapping) a file into memory: RTFileReadAll, RTFileReadAllEx, RTFileReadAllByHandle, RTFileReadAllByHandleEx and RTFileReadAllFree.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1/* $Id: RTFileReadAllByHandleEx-generic.cpp 8925 2008-05-19 16:59:11Z vboxsync $ */
2/** @file
3 * IPRT - RTFileReadAllByHandleEx, generic implementation.
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#include <iprt/file.h>
37#include <iprt/mem.h>
38#include <iprt/assert.h>
39#include <iprt/string.h>
40#include <iprt/err.h>
41
42
43RTDECL(int) RTFileReadAllByHandleEx(RTFILE File, RTFOFF off, RTFOFF cbMax, uint32_t fFlags, void **ppvFile, size_t *pcbFile)
44{
45 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
46
47 /*
48 * Save the current offset first.
49 */
50 RTFOFF offOrg;
51 int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, (uint64_t *)&offOrg);
52 if (RT_SUCCESS(rc))
53 {
54 /*
55 * Get the file size, adjust it and check that it might fit into memory.
56 */
57 RTFOFF cbFile;
58 rc = RTFileSeek(File, 0,RTFILE_SEEK_END, (uint64_t *)&cbFile);
59 if (RT_SUCCESS(rc))
60 {
61 RTFOFF cbAllocFile = cbFile > off ? cbFile - off : 0;
62 if (cbAllocFile > cbMax)
63 cbAllocFile = cbMax;
64 size_t cbAllocMem = (size_t)cbAllocFile;
65 if ( (RTFOFF)cbAllocMem == cbAllocFile
66 && cbAllocMem)
67 {
68 /*
69 * Try allocate the required memory and initialize the header (hardcoded fun).
70 */
71 void *pvHdr = RTMemAlloc(cbAllocMem + 32);
72 if (pvHdr)
73 {
74 memset(pvHdr, 0xff, 32);
75 *(size_t *)pvHdr = cbAllocMem;
76
77 /*
78 * Seek and read.
79 */
80 rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
81 if (RT_SUCCESS(rc))
82 {
83 void *pvFile = (uint8_t *)pvHdr + 32;
84 rc = RTFileRead(File, pvFile, cbAllocMem, NULL);
85 if (RT_SUCCESS(rc))
86 {
87 /*
88 * Success - fill in the return values.
89 */
90 *ppvFile = pvFile;
91 *pcbFile = cbAllocFile;
92 }
93 }
94
95 if (RT_FAILURE(rc))
96 RTMemFree(pvHdr);
97 }
98 else
99 rc = VERR_NO_MEMORY;
100 }
101 else if (!cbAllocMem)
102 rc = VERR_EOF;
103 else
104 rc = VERR_TOO_MUCH_DATA;
105 }
106 /* restore the position. */
107 RTFileSeek(File, offOrg, RTFILE_SEEK_BEGIN, NULL);
108 }
109 return rc;
110}
111
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette