VirtualBox

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

Last change on this file since 73516 was 69522, checked in by vboxsync, 7 years ago

pkzip: Fixed memory leak and missing error code in unlikely error path. (parfait/clang-analyzer)

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