1 | /* $Id: RTFileReadAllByHandleEx-generic.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTFileReadAllByHandleEx, generic implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/file.h>
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #include <iprt/mem.h>
|
---|
45 | #include <iprt/assert.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 | #include <iprt/errcore.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | RTDECL(int) RTFileReadAllByHandleEx(RTFILE File, RTFOFF off, RTFOFF cbMax, uint32_t fFlags, void **ppvFile, size_t *pcbFile)
|
---|
51 | {
|
---|
52 | AssertReturn(!(fFlags & ~RTFILE_RDALL_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * Save the current offset first.
|
---|
56 | */
|
---|
57 | RTFOFF offOrg;
|
---|
58 | int rc = RTFileSeek(File, 0, RTFILE_SEEK_CURRENT, (uint64_t *)&offOrg);
|
---|
59 | if (RT_SUCCESS(rc))
|
---|
60 | {
|
---|
61 | /*
|
---|
62 | * Get the file size, adjust it and check that it might fit into memory.
|
---|
63 | */
|
---|
64 | RTFOFF cbFile;
|
---|
65 | AssertCompile(sizeof(cbFile) == sizeof(uint64_t));
|
---|
66 | rc = RTFileSeek(File, 0, RTFILE_SEEK_END, (uint64_t *)&cbFile);
|
---|
67 | if (RT_SUCCESS(rc))
|
---|
68 | {
|
---|
69 | RTFOFF cbAllocFile = cbFile > off ? cbFile - off : 0;
|
---|
70 | if (cbAllocFile <= cbMax)
|
---|
71 | { /* likely */ }
|
---|
72 | else if (!(fFlags & RTFILE_RDALL_F_FAIL_ON_MAX_SIZE))
|
---|
73 | cbAllocFile = cbMax;
|
---|
74 | else
|
---|
75 | rc = VERR_OUT_OF_RANGE;
|
---|
76 | if (RT_SUCCESS(rc))
|
---|
77 | {
|
---|
78 | size_t cbAllocMem = (size_t)cbAllocFile;
|
---|
79 | if ((RTFOFF)cbAllocMem == cbAllocFile)
|
---|
80 | {
|
---|
81 | /*
|
---|
82 | * Try allocate the required memory and initialize the header (hardcoded fun).
|
---|
83 | */
|
---|
84 | void *pvHdr = RTMemAlloc(cbAllocMem + 32 + (fFlags & RTFILE_RDALL_F_TRAILING_ZERO_BYTE ? 1 : 0));
|
---|
85 | if (pvHdr)
|
---|
86 | {
|
---|
87 | memset(pvHdr, 0xff, 32);
|
---|
88 | *(size_t *)pvHdr = cbAllocMem;
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Seek and read.
|
---|
92 | */
|
---|
93 | rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, NULL);
|
---|
94 | if (RT_SUCCESS(rc))
|
---|
95 | {
|
---|
96 | void *pvFile = (uint8_t *)pvHdr + 32;
|
---|
97 | rc = RTFileRead(File, pvFile, cbAllocMem, NULL);
|
---|
98 | if (RT_SUCCESS(rc))
|
---|
99 | {
|
---|
100 | if (fFlags & RTFILE_RDALL_F_TRAILING_ZERO_BYTE)
|
---|
101 | ((uint8_t *)pvFile)[cbAllocFile] = '\0';
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Success - fill in the return values.
|
---|
105 | */
|
---|
106 | *ppvFile = pvFile;
|
---|
107 | *pcbFile = cbAllocMem;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (RT_FAILURE(rc))
|
---|
112 | RTMemFree(pvHdr);
|
---|
113 | }
|
---|
114 | else
|
---|
115 | rc = VERR_NO_MEMORY;
|
---|
116 | }
|
---|
117 | else
|
---|
118 | rc = VERR_TOO_MUCH_DATA;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | /* restore the position. */
|
---|
122 | RTFileSeek(File, offOrg, RTFILE_SEEK_BEGIN, NULL);
|
---|
123 | }
|
---|
124 | return rc;
|
---|
125 | }
|
---|
126 | RT_EXPORT_SYMBOL(RTFileReadAllByHandleEx);
|
---|
127 |
|
---|