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