VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/zip/pkzip.cpp@ 98962

Last change on this file since 98962 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1/* $Id: pkzip.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - PKZIP archive I/O.
4 */
5
6/*
7 * Copyright (C) 2014-2023 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/zip.h>
42
43#include <iprt/file.h>
44#include <iprt/err.h>
45#include <iprt/fs.h>
46#include <iprt/mem.h>
47#include <iprt/string.h>
48#include <iprt/vfs.h>
49#include <iprt/vfslowlevel.h>
50
51
52/*********************************************************************************************************************************
53* Structures and Typedefs *
54*********************************************************************************************************************************/
55/**
56 * Memory stream private data.
57 */
58typedef struct MEMIOSTREAM
59{
60 /** Size of the memory buffer. */
61 size_t cbBuf;
62 /** Pointer to the memory buffer. */
63 uint8_t *pu8Buf;
64 /** Current offset. */
65 size_t off;
66} MEMIOSTREAM;
67typedef MEMIOSTREAM *PMEMIOSTREAM;
68
69
70/**
71 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
72 */
73static DECLCALLBACK(int) memFssIos_Close(void *pvThis)
74{
75 NOREF(pvThis);
76 return VINF_SUCCESS;
77}
78
79/**
80 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
81 */
82static DECLCALLBACK(int) memFssIos_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
83{
84 PMEMIOSTREAM pThis = (PMEMIOSTREAM)pvThis;
85 switch (enmAddAttr)
86 {
87 case RTFSOBJATTRADD_NOTHING:
88 case RTFSOBJATTRADD_UNIX:
89 RT_ZERO(*pObjInfo);
90 pObjInfo->cbObject = pThis->cbBuf;
91 break;
92 default:
93 return VERR_NOT_SUPPORTED;
94 }
95 return VINF_SUCCESS;
96}
97
98/**
99 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
100 */
101static DECLCALLBACK(int) memFssIos_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
102{
103 PMEMIOSTREAM pThis = (PMEMIOSTREAM)pvThis;
104 Assert(pSgBuf->cSegs == 1);
105 RT_NOREF_PV(fBlocking);
106
107 if (off < 0)
108 off = pThis->off;
109 if (off >= (RTFOFF)pThis->cbBuf)
110 return pcbRead ? VINF_EOF : VERR_EOF;
111
112 size_t cbLeft = pThis->cbBuf - off;
113 size_t cbToRead = pSgBuf->paSegs[0].cbSeg;
114 if (cbToRead > cbLeft)
115 {
116 if (!pcbRead)
117 return VERR_EOF;
118 cbToRead = (size_t)cbLeft;
119 }
120
121 memcpy(pSgBuf->paSegs[0].pvSeg, pThis->pu8Buf + off, cbToRead);
122 pThis->off = off + cbToRead;
123 if (pcbRead)
124 *pcbRead = cbToRead;
125
126 return VINF_SUCCESS;
127}
128
129/**
130 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
131 */
132static DECLCALLBACK(int) memFssIos_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
133{
134 RT_NOREF_PV(pvThis); RT_NOREF_PV(off); RT_NOREF_PV(pSgBuf); RT_NOREF_PV(fBlocking); RT_NOREF_PV(pcbWritten);
135 return VERR_NOT_IMPLEMENTED;
136}
137
138/**
139 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
140 */
141static DECLCALLBACK(int) memFssIos_Flush(void *pvThis)
142{
143 RT_NOREF_PV(pvThis);
144 return VERR_NOT_IMPLEMENTED;
145}
146
147/**
148 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
149 */
150static DECLCALLBACK(int) memFssIos_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr, uint32_t *pfRetEvents)
151{
152 RT_NOREF_PV(pvThis); RT_NOREF_PV(fEvents); RT_NOREF_PV(cMillies); RT_NOREF_PV(fIntr); RT_NOREF_PV(pfRetEvents);
153 return VERR_NOT_IMPLEMENTED;
154}
155
156/**
157 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
158 */
159static DECLCALLBACK(int) memFssIos_Tell(void *pvThis, PRTFOFF poffActual)
160{
161 PMEMIOSTREAM pThis = (PMEMIOSTREAM)pvThis;
162 *poffActual = pThis->off;
163 return VINF_SUCCESS;
164}
165
166/**
167 * Memory I/O object stream operations.
168 */
169static const RTVFSIOSTREAMOPS g_memFssIosOps =
170{
171 { /* Obj */
172 RTVFSOBJOPS_VERSION,
173 RTVFSOBJTYPE_IO_STREAM,
174 "MemFsStream::IoStream",
175 memFssIos_Close,
176 memFssIos_QueryInfo,
177 NULL,
178 RTVFSOBJOPS_VERSION
179 },
180 RTVFSIOSTREAMOPS_VERSION,
181 RTVFSIOSTREAMOPS_FEAT_NO_SG,
182 memFssIos_Read,
183 memFssIos_Write,
184 memFssIos_Flush,
185 memFssIos_PollOne,
186 memFssIos_Tell,
187 NULL /*Skip*/,
188 NULL /*ZeroFill*/,
189 RTVFSIOSTREAMOPS_VERSION
190};
191
192RTDECL(int) RTZipPkzipMemDecompress(void **ppvDst, size_t *pcbDst, const void *pvSrc, size_t cbSrc, const char *pszObject)
193{
194 PMEMIOSTREAM pIosData;
195 RTVFSIOSTREAM hVfsIos;
196 int rc = RTVfsNewIoStream(&g_memFssIosOps,
197 sizeof(*pIosData),
198 RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN,
199 NIL_RTVFS,
200 NIL_RTVFSLOCK,
201 &hVfsIos,
202 (void **)&pIosData);
203 if (RT_SUCCESS(rc))
204 {
205 pIosData->pu8Buf = (uint8_t*)pvSrc;
206 pIosData->cbBuf = cbSrc;
207 pIosData->off = 0;
208 RTVFSFSSTREAM hVfsFss;
209 rc = RTZipPkzipFsStreamFromIoStream(hVfsIos, 0 /*fFlags*/, &hVfsFss);
210 RTVfsIoStrmRelease(hVfsIos);
211 if (RT_SUCCESS(rc))
212 {
213 /*
214 * Loop through all objects. Actually this wouldn't be required
215 * for .zip files but we opened it as I/O stream.
216 */
217 for (bool fFound = false; !fFound;)
218 {
219 char *pszName;
220 RTVFSOBJ hVfsObj;
221 rc = RTVfsFsStrmNext(hVfsFss, &pszName, NULL /*penmType*/, &hVfsObj);
222 if (RT_FAILURE(rc))
223 break;
224 fFound = !strcmp(pszName, pszObject);
225 if (fFound)
226 {
227 RTFSOBJINFO UnixInfo;
228 rc = RTVfsObjQueryInfo(hVfsObj, &UnixInfo, RTFSOBJATTRADD_UNIX);
229 if (RT_SUCCESS(rc))
230 {
231 size_t cb = UnixInfo.cbObject;
232 void *pv = RTMemAlloc(cb);
233 if (pv)
234 {
235 RTVFSIOSTREAM hVfsIosObj = RTVfsObjToIoStream(hVfsObj);
236 if (hVfsIos != NIL_RTVFSIOSTREAM)
237 {
238 rc = RTVfsIoStrmRead(hVfsIosObj, pv, cb, true /*fBlocking*/, NULL);
239 if (RT_SUCCESS(rc))
240 {
241 *ppvDst = pv;
242 *pcbDst = cb;
243 }
244 }
245 else
246 rc = VERR_INTERNAL_ERROR_4;
247 if (RT_FAILURE(rc))
248 RTMemFree(pv);
249 }
250 }
251 }
252 RTVfsObjRelease(hVfsObj);
253 RTStrFree(pszName);
254 }
255 RTVfsFsStrmRelease(hVfsFss);
256 }
257 }
258 return rc;
259}
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