VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/zip/pkzipvfs.cpp

Last change on this file was 100908, checked in by vboxsync, 9 months ago

IPRT,Storage,Puel: Changed the pfnRead and pfnWrite VFS methods and the RTVfsIoStrmSgRead, RTVfsIoStrmSgWrite, RTVfsFileSgRead and RTVfsFileSgWrite APIs to advance pSgBuf and respect the incoming position just like RTFileSgRead & RTFileSgWrite.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 41.2 KB
Line 
1/* $Id: pkzipvfs.cpp 100908 2023-08-19 02:57:05Z vboxsync $ */
2/** @file
3 * IPRT - PKZIP Virtual Filesystem.
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#include <iprt/assert.h>
43#include <iprt/err.h>
44#include <iprt/file.h>
45#include <iprt/mem.h>
46#include <iprt/poll.h>
47#include <iprt/string.h>
48#include <iprt/vfs.h>
49#include <iprt/vfslowlevel.h>
50#include <iprt/stream.h>
51
52
53/*********************************************************************************************************************************
54* Structures and Typedefs *
55*********************************************************************************************************************************/
56/* See http://www.pkware.com/documents/casestudies/APPNOTE.TXT */
57
58/**
59 * PKZip Local File Header.
60 */
61#pragma pack(1)
62typedef struct RTZIPPKZIPLOCALFILEHDR
63{
64 /** Magic value, see RTZIPPKZIPLOCALFILEHDR_MAGIC. */
65 uint32_t u32Magic;
66 /** Minimum version needed to extract. */
67 uint16_t u16Version;
68 /** General purpose bit flag. */
69 uint16_t fFlags;
70 /** Compression method. See RTZIPPKZIP_COMP_METHOD_XXX. */
71 uint16_t u16ComprMethod;
72 /** Last modified time, MS-DOS format: HHHHHMMM MMMSSSSS, multiply seconds by 2 */
73 uint16_t u16LastModifiedTime;
74 /** Last modified date, MS-DOS format: YYYYYYYM MMMDDDDD, year starts at 1980 */
75 uint16_t u16LastModifiedDate;
76 /** Checksum. */
77 uint32_t u32Crc;
78 /** Compressed size. */
79 uint32_t cbCompressed;
80 /** Uncompressed size. */
81 uint32_t cbUncompressed;
82 /** Length of the file name. */
83 uint16_t cbFilename;
84 /** Length of the extra field. */
85 uint16_t cbExtra;
86 /** Start of the file name. */
87 uint8_t u8Filename;
88} RTZIPPKZIPLOCALFILEHDR;
89#pragma pack()
90AssertCompileSize(RTZIPPKZIPLOCALFILEHDR, 30+1);
91/** Pointer to PKZip Local File Header. */
92typedef RTZIPPKZIPLOCALFILEHDR *PRTZIPPKZIPLOCALFILEHDR;
93
94#define RTZIPPKZIPLOCALFILEHDR_MAGIC RT_MAKE_U32_FROM_U8('P','K','\003','\004')
95
96/**
97 * PKZip compression method.
98 */
99typedef enum RTZIPPKZIP_COMP_METHOD
100{
101 /** No compression */
102 RTZIPPKZIP_COMP_METHOD_STORED = 0,
103 /** Shrunk */
104 RTZIPPKZIP_COMP_METHOD_SHRUNK = 1,
105 /** Reduced with compression factor 1 */
106 RTZIPPKZIP_COMP_METHOD_REDUCED1 = 2,
107 /** Reduced with compression factor 2 */
108 RTZIPPKZIP_COMP_METHOD_REDUCED2 = 3,
109 /** Reduced with compression factor 3 */
110 RTZIPPKZIP_COMP_METHOD_REDUCED3 = 4,
111 /** Reduced with compression factor 4 */
112 RTZIPPKZIP_COMP_METHOD_REDUCED4 = 5,
113 /** Imploded */
114 RTZIPPKZIP_COMP_METHOD_IMPLODED = 6,
115 /** Deflated */
116 RTZIPPKZIP_COMP_METHOD_DEFLATED = 8,
117 /** Deflated64 */
118 RTZIPPKZIP_COMP_METHOD_DEFLATED64 = 9,
119 /* Compressed using bzip2 */
120 RTZIPPKZIP_COMP_METHOD_BZIP2 = 12,
121 /** Compressed using LZMA */
122 RTZIPPKZIP_COMP_METHOD_LZMA = 14
123} RTZIPPKZIP_COMP_METHOD;
124
125/**
126 * PKZip Central Directory Header.
127 */
128#pragma pack(1)
129typedef struct RTZIPPKZIPCENTRDIRHDR
130{
131 /** The magic value. See RTZIPPKZIPCENTRDIRHDR_MAGIC. */
132 uint32_t u32Magic;
133 /** The version used for creating the item. */
134 uint16_t u16VerMade;
135 /** The minimum version required for extracting the item. */
136 uint16_t u16VerRequired;
137 /** General purpose flags. */
138 uint16_t fFlags;
139 /** Compresstion method. See RTZIPPKZIP_COMP_METHOD_XXX */
140 uint16_t u16ComprMethod;
141 /** Last modified time, MS-DOS format: HHHHHMMM MMMSSSSS, multiply seconds by 2 */
142 uint16_t u16LastModifiedTime;
143 /** Last modified date, MS-DOS format: YYYYYYYM MMMDDDDD, year starts at 1980 */
144 uint16_t u16LastModifiedDate;
145 /** Checksum. */
146 uint32_t u32Crc;
147 /** Compressed size. */
148 uint32_t cbCompressed;
149 /** Uncompressed size. */
150 uint32_t cbUncompressed;
151 /** Length of the object file name. */
152 uint16_t cbFilename;
153 /** Length of the extra field. */
154 uint16_t cbExtra;
155 /** Length of the object comment. */
156 uint16_t cbComment;
157 /** The number of the disk on which this file begins. */
158 uint16_t iDiskStart;
159 /** Internal attributes. */
160 uint16_t u16IntAttrib;
161 /** External attributes. */
162 uint32_t u32ExtAttrib;
163 /** Offset from the start of the first disk on which this file appears to
164 * where the local file header should be found. */
165 uint32_t offLocalFileHeader;
166 /** Start of the file name. */
167 uint8_t u8Filename;
168} RTZIPPKZIPCENTRDIRHDR;
169#pragma pack()
170AssertCompileSize(RTZIPPKZIPCENTRDIRHDR, 46+1);
171/** Pointer to the PKZip Central Directory Header. */
172typedef RTZIPPKZIPCENTRDIRHDR *PRTZIPPKZIPCENTRDIRHDR;
173
174#define RTZIPPKZIPCENTRDIRHDR_MAGIC RT_MAKE_U32_FROM_U8('P','K','\001','\002')
175
176/**
177 * PKZip End of Central Directory Record.
178 */
179#pragma pack(1)
180typedef struct RTZIPPKZIPENDOFCENTRDIRREC
181{
182 /** The magic value. See RTZIPPKZIPENDOFCENTRDIRREC_MAGIC. */
183 uint32_t u32Magic;
184 /** Number of this disk. */
185 uint16_t iThisDisk;
186 /** Number of the disk with the start of the Central Directory. */
187 uint16_t iDiskStartCentrDirectory;
188 /** Number of Central Directory entries on this disk. */
189 uint16_t cCentrDirRecordsThisDisk;
190 /** Number of Central Directory records. */
191 uint16_t cCentrDirRecords;
192 /** Size of the Central Directory in bytes. */
193 uint32_t cbCentrDir;
194 /** Offset of the Central Directory. */
195 uint32_t offCentrDir;
196 /** Size of the comment in bytes. */
197 uint16_t cbComment;
198 /** Start of the comment. */
199 uint8_t u8Comment;
200} RTZIPPKZIPENDOFCENTRDIRREC;
201#pragma pack()
202AssertCompileSize(RTZIPPKZIPENDOFCENTRDIRREC, 22+1);
203/** Pointer to the PKZip End of Central Directory Record. */
204typedef RTZIPPKZIPENDOFCENTRDIRREC const *PCRTZIPPKZIPENDOFCENTRDIRREC;
205
206#define RTZIPPKZIPENDOFCENTRDIRREC_MAGIC RT_MAKE_U32_FROM_U8('P','K','\005','\006')
207
208/**
209 * PKZip ZIP64 End of Central Directory Record.
210 */
211#pragma pack(1)
212typedef struct RTZIPPKZIP64ENDOFCENTRDIRREC
213{
214 /** The magic value. See RTZIPPKZIP64ENDOFCENTRDIRREC_MAGIC. */
215 uint32_t u32Magic;
216 /** Size of Zip64 end of Central Directory Record. */
217 uint64_t cbSizeEocdr;
218 /** The version used for creating the item. */
219 uint16_t u16VerMade;
220 /** The minimum version required for extracting the item. */
221 uint16_t u16VerRequired;
222 /** Number of this disk. */
223 uint32_t iThisDisk;
224 /** Number of the disk with the start of the Central Directory. */
225 uint32_t iDiskStartCentrDirectory;
226 /** Number of Central Directory entries on this disk. */
227 uint64_t cCentrDirRecordsThisDisk;
228 /** Number of Central Directory records. */
229 uint64_t cCentrDirRecords;
230 /** Size of the Central Directory in bytes. */
231 uint64_t cbCentrDir;
232 /** Offset of the Central Directory. */
233 uint64_t offCentrDir;
234} RTZIPPKZIP64ENDOFCENTRDIRREC;
235#pragma pack()
236AssertCompileSize(RTZIPPKZIP64ENDOFCENTRDIRREC, 56);
237/** Pointer to the 64-bit PKZip End of Central Directory Record. */
238typedef RTZIPPKZIP64ENDOFCENTRDIRREC *PRTZIPPKZIP64ENDOFCENTRDIRREC;
239
240#define RTZIPPKZIP64ENDOFCENTRDIRREC_MAGIC RT_MAKE_U32_FROM_U8('P','K','\006','\006')
241
242/**
243 * PKZip ZIP64 End of Central Directory Locator.
244 */
245#pragma pack(1)
246typedef struct RTZIPPKZIP64ENDOFCENTRDIRLOC
247{
248 /** The magic value. See RTZIPPKZIP64ENDOFCENTRDIRLOC_MAGIC. */
249 uint32_t u32Magic;
250 /** Number of the disk with the start of the ZIP64 End of Central Directory. */
251 uint32_t iDiskStartCentrDir;
252 /** Relative offset of the ZIP64 End of Central Directory Record. */
253 uint64_t offEndOfCentrDirRec;
254 /** Total number of disks. */
255 uint32_t cDisks;
256} RTZIPPKZIP64ENDOFCENTRDIRLOC;
257#pragma pack()
258AssertCompileSize(RTZIPPKZIP64ENDOFCENTRDIRLOC, 20);
259
260#define RTZIPPKZIP64ENDOFCENTRDIRLOC_MAGIC RT_MAKE_U32_FROM_U8('P','K','\006','\007')
261
262/**
263 * PKZip ZIP64 Extended Information Extra Field.
264 */
265#pragma pack(1)
266typedef struct RTZIPPKZIP64EXTRAFIELD
267{
268 /** Uncompressed size. */
269 uint64_t cbUncompressed;
270 /** Compressed size. */
271 uint64_t cbCompressed;
272 /** Offset from the start of the first disk on which this file appears to
273 * where the local file header should be found. */
274 uint64_t offLocalFileHeader;
275 /** The number of the disk on which this file begins. */
276 uint32_t iDiskStart;
277} RTZIPPKZIP64EXTRAFIELD;
278#pragma pack()
279/** Pointer to the ZIP64 Extended Information Extra Field. */
280typedef RTZIPPKZIP64EXTRAFIELD *PRTZIPPKZIP64EXTRAFIELD;
281AssertCompileSize(RTZIPPKZIP64EXTRAFIELD, 28);
282
283/**
284 * PKZip reader instance data.
285 */
286typedef struct RTZIPPKZIPREADER
287{
288 /** Set if we have the End of Central Directory record. */
289 bool fHaveEocd;
290 /** The Central Directory header. */
291 RTZIPPKZIPCENTRDIRHDR cdh;
292 /** ZIP64 extended information. */
293 RTZIPPKZIP64EXTRAFIELD cd64ex;
294 /** Set if ZIP64 End of Central Directory Locator is present (archive setting). */
295 bool fZip64Eocd;
296 /** Set if cd64ex is valid for the current file header (object setting). */
297 bool fZip64Ex;
298 /* The name of the current object. */
299 char szName[RTPATH_MAX];
300} RTZIPPKZIPREADER;
301/** Pointer to the PKZip reader instance data. */
302typedef RTZIPPKZIPREADER *PRTZIPPKZIPREADER;
303
304/**
305 * Pkzip object (directory).
306 */
307typedef struct RTZIPPKZIPBASEOBJ
308{
309 /** Pointer to the reader instance data (resides in the filesystem
310 * stream). */
311 PRTZIPPKZIPREADER pPkzipReader;
312 /** The object info with unix attributes. */
313 RTFSOBJINFO ObjInfo;
314} RTZIPPKZIPBASEOBJ;
315/** Pointer to a PKZIP filesystem stream base object. */
316typedef RTZIPPKZIPBASEOBJ *PRTZIPPKZIPBASEOBJ;
317
318/**
319 * Pkzip object (file) represented as a VFS I/O stream.
320 */
321typedef struct RTZIPPKZIPIOSTREAM
322{
323 /** The basic PKZIP object data. */
324 RTZIPPKZIPBASEOBJ BaseObj;
325 /** The number of (uncompressed) bytes in the file. */
326 uint64_t cbFile;
327 /** The current file position at uncompressed file data. */
328 uint64_t offFile;
329 /** The start position of the compressed data in the hVfsIos. */
330 uint64_t offCompStart;
331 /** The current position for decompressing bytes in the hVfsIos. */
332 uint64_t offComp;
333 /** The number of compressed bytes starting at offCompStart. */
334 uint64_t cbComp;
335 /** Set if we have to pass the type function the next time the input
336 * function is called. */
337 bool fPassZipType;
338 /** Set if we've reached the end of the file. */
339 bool fEndOfStream;
340 /** Pkzip compression method for this object. */
341 RTZIPPKZIP_COMP_METHOD enmCompMethod;
342 /** Zip compression method. */
343 RTZIPTYPE enmZipType;
344 /** The decompressor instance. */
345 PRTZIPDECOMP pZip;
346 /** The input I/O stream. */
347 RTVFSIOSTREAM hVfsIos;
348} RTZIPPKZIPIOSTREAM;
349/** Pointer to a the private data of a PKZIP file I/O stream. */
350typedef RTZIPPKZIPIOSTREAM *PRTZIPPKZIPIOSTREAM;
351
352
353/**
354 * PKZip filesystem stream private data. The stream must be seekable!
355 */
356typedef struct RTZIPPKZIPFSSTREAM
357{
358 /** The input I/O stream. */
359 RTVFSIOSTREAM hVfsIos;
360
361 /** The current object (referenced). */
362 RTVFSOBJ hVfsCurObj;
363 /** Pointer to the private data if hVfsCurObj is representing a file. */
364 PRTZIPPKZIPIOSTREAM pCurIosData;
365
366 /** The offset of the first Central Directory header. */
367 uint64_t offFirstCdh;
368 /** The offset of the next Central Directory header. */
369 uint64_t offNextCdh;
370
371 /** Size of the central directory. */
372 uint64_t cbCentrDir;
373 /** Current central directory entry. */
374 uint64_t iCentrDirEntry;
375 /** Number of central directory entries. */
376 uint64_t cCentrDirEntries;
377
378 /** Set if we have the End of Central Directory Record. */
379 bool fHaveEocd;
380 /** Set if we've reached the end of the stream. */
381 bool fEndOfStream;
382 /** Set if we've encountered a fatal error. */
383 int rcFatal;
384
385 /** The PKZIP reader instance data. */
386 RTZIPPKZIPREADER PkzipReader;
387} RTZIPPKZIPFSSTREAM;
388/** Pointer to a the private data of a PKZIP filesystem stream. */
389typedef RTZIPPKZIPFSSTREAM *PRTZIPPKZIPFSSTREAM;
390
391
392
393/**
394 * Decode date/time from DOS format as used in PKZip.
395 */
396static int rtZipPkzipReaderDecodeDosTime(PRTTIMESPEC pTimeSpec, uint16_t u16Time, uint16_t u16Date)
397{
398 RTTIME time;
399 RT_ZERO(time);
400 time.i32Year = ((u16Date & 0xfe00) >> 9) + 1980;
401 time.u8Month = (u16Date & 0x01e0) >> 5;
402 time.u8MonthDay = u16Date & 0x001f;
403 time.u8Hour = (u16Time & 0xf800) >> 11;
404 time.u8Minute = (u16Time & 0x07e0) >> 5;
405 time.u8Second = u16Time & 0x001f;
406 RTTimeNormalize(&time);
407 RTTimeImplode(pTimeSpec, &time);
408 return VINF_SUCCESS;
409}
410
411
412/**
413 * Parse the Local File Header.
414 * Just skip the data as we trust the Central Directory.
415 */
416static int rtZipPkzipParseLocalFileHeader(PRTZIPPKZIPREADER pThis, PRTZIPPKZIPLOCALFILEHDR pLfh, size_t *pcbExtra)
417{
418 RT_NOREF_PV(pThis);
419
420 if (pLfh->cbFilename >= sizeof(pThis->szName))
421 return VERR_PKZIP_NAME_TOO_LONG;
422
423 *pcbExtra = pLfh->cbFilename + pLfh->cbExtra;
424 return VINF_SUCCESS;
425}
426
427
428/**
429 * Parse the Central Directory Header.
430 */
431static int rtZipPkzipParseCentrDirHeader(PRTZIPPKZIPREADER pThis, PRTZIPPKZIPCENTRDIRHDR pCdh, size_t *pcbExtra)
432{
433 if (pCdh->u32Magic != RTZIPPKZIPCENTRDIRHDR_MAGIC)
434 return VERR_PKZIP_BAD_CDF_HEADER;
435
436 if (pCdh->cbFilename >= sizeof(pThis->szName))
437 return VERR_PKZIP_NAME_TOO_LONG;
438
439 *pcbExtra = pCdh->cbFilename + pCdh->cbExtra + pCdh->cbComment;
440
441 pThis->cdh = *pCdh;
442 pThis->fZip64Ex = false;
443 return VINF_SUCCESS;
444}
445
446
447/**
448 * Return the offset of the Local File Header.
449 */
450static uint64_t rtZipPkzipReaderOffLocalHeader(PRTZIPPKZIPREADER pThis)
451{
452 if (pThis->fZip64Ex && pThis->cdh.offLocalFileHeader == (uint32_t)-1)
453 return pThis->cd64ex.offLocalFileHeader;
454
455 return pThis->cdh.offLocalFileHeader;
456}
457
458
459/**
460 * Return the uncompressed object size.
461 */
462static uint64_t rtZipPkzipReaderUncompressed(PRTZIPPKZIPREADER pThis)
463{
464 if (pThis->fZip64Ex && pThis->cdh.cbUncompressed == (uint32_t)-1)
465 return pThis->cd64ex.cbUncompressed;
466
467 return pThis->cdh.cbUncompressed;
468}
469
470
471/**
472 * Return the compressed object size.
473 */
474static uint64_t rtZipPkzipReaderCompressed(PRTZIPPKZIPREADER pThis)
475{
476 if (pThis->fZip64Ex && pThis->cdh.cbCompressed == (uint32_t)-1)
477 return pThis->cd64ex.cbCompressed;
478
479 return pThis->cdh.cbCompressed;
480}
481
482
483/**
484 * Parse the extra part of the Central Directory Header.
485 */
486static int rtZipPkzipParseCentrDirHeaderExtra(PRTZIPPKZIPREADER pThis, uint8_t *pu8Buf, size_t cb,
487 RTZIPPKZIP_COMP_METHOD *penmCompMethod, uint64_t *pcbCompressed)
488{
489 int rc = RTStrCopyEx(pThis->szName, sizeof(pThis->szName), (const char*)pu8Buf, pThis->cdh.cbFilename);
490 if (RT_SUCCESS(rc))
491 {
492 pu8Buf += pThis->cdh.cbFilename;
493 cb = pThis->cdh.cbExtra;
494 while (cb >= 4)
495 {
496 uint16_t idExtra = *(uint16_t*)pu8Buf;
497 pu8Buf += 2;
498 uint16_t cbExtra = *(uint16_t*)pu8Buf;
499 pu8Buf += 2;
500 cb -= 4;
501
502 if (cb >= cbExtra)
503 {
504 switch (idExtra)
505 {
506 case 0x0001:
507 /*
508 * ZIP64 Extended Information Extra Field.
509 */
510 if (!pThis->fZip64Eocd)
511 return VERR_PKZIP_ZIP64EX_IN_ZIP32;
512 /* Not all fields are really used. */
513 RT_ZERO(pThis->cd64ex);
514 memcpy(&pThis->cd64ex, pu8Buf, cbExtra);
515 pThis->fZip64Ex = true;
516 break;
517
518 default:
519 /* unknown, just skip */
520 break;
521 }
522 pu8Buf += cbExtra;
523 cb -= cbExtra;
524 }
525 else
526 {
527 rc = VERR_PKZIP_BAD_CDF_HEADER;
528 break;
529 }
530 }
531
532 *penmCompMethod = (RTZIPPKZIP_COMP_METHOD)pThis->cdh.u16ComprMethod;
533 *pcbCompressed = rtZipPkzipReaderCompressed(pThis);
534 }
535 return VINF_SUCCESS;
536}
537
538
539/**
540 * Translate a PKZip header to an IPRT object info structure.
541 */
542static int rtZipPkzipReaderGetFsObjInfo(PRTZIPPKZIPREADER pThis, PRTFSOBJINFO pObjInfo)
543{
544 /*
545 * Zap the whole structure, this takes care of unused space in the union.
546 */
547 RT_ZERO(*pObjInfo);
548 pObjInfo->cbObject = rtZipPkzipReaderUncompressed(pThis);
549 pObjInfo->cbAllocated = rtZipPkzipReaderUncompressed(pThis); /* XXX */
550 RTTIMESPEC ts;
551 rtZipPkzipReaderDecodeDosTime(&ts, pThis->cdh.u16LastModifiedTime, pThis->cdh.u16LastModifiedDate);
552 pObjInfo->ChangeTime = ts;
553 pObjInfo->ModificationTime = ts;
554 pObjInfo->AccessTime = ts;
555 pObjInfo->BirthTime = ts;
556 const char *pszEnd = strchr(pThis->szName, '\0');
557 if (pszEnd == &pThis->szName[0] || pszEnd[-1] != '/')
558 pObjInfo->Attr.fMode = RTFS_TYPE_FILE \
559 | RTFS_UNIX_IRUSR | RTFS_UNIX_IWUSR \
560 | RTFS_UNIX_IRGRP \
561 | RTFS_UNIX_IROTH;
562 else
563 pObjInfo->Attr.fMode = RTFS_TYPE_DIRECTORY \
564 | RTFS_UNIX_IRWXU \
565 | RTFS_UNIX_IRGRP | RTFS_UNIX_IXGRP \
566 | RTFS_UNIX_IROTH | RTFS_UNIX_IXOTH;
567 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
568 pObjInfo->Attr.u.Unix.cHardlinks = 1;
569
570 return VINF_SUCCESS;
571}
572
573
574/**
575 * Search the magic value of the End Of Central Directory Record.
576 *
577 * @returns true if found, false otherwise.
578 * @param pu8Buf buffer.
579 * @param cb size of buffer.
580 * @param piPos where to store the position of the magic value.
581 */
582static bool rtZipPkzipReaderScanEocd(const uint8_t *pu8Buf, size_t cb, int *piPos)
583{
584 if (cb < 4)
585 return false;
586 ssize_t i;
587 for (i = (ssize_t)cb - 4; i >= 0; --i)
588 if (*(uint32_t*)(pu8Buf + i) == RTZIPPKZIPENDOFCENTRDIRREC_MAGIC)
589 {
590 *piPos = i;
591 return true;
592 }
593 return false;
594}
595
596
597/**
598 * Read the Local File Header. We ignore the content -- we trust the Central
599 * Directory.
600 */
601static int rtZipPkzipFssIosReadLfh(PRTZIPPKZIPFSSTREAM pThis, uint64_t *poffStartData)
602{
603 RTZIPPKZIPLOCALFILEHDR lfh;
604 uint64_t offLocalFileHeader = rtZipPkzipReaderOffLocalHeader(&pThis->PkzipReader);
605 int rc = RTVfsIoStrmReadAt(pThis->hVfsIos, offLocalFileHeader,
606 &lfh, sizeof(lfh) - 1, true /*fBlocking*/, NULL);
607 if (RT_SUCCESS(rc))
608 {
609 if (lfh.u32Magic == RTZIPPKZIPLOCALFILEHDR_MAGIC)
610 {
611 size_t cbExtra = 0;
612 rc = rtZipPkzipParseLocalFileHeader(&pThis->PkzipReader, &lfh, &cbExtra);
613 if (RT_SUCCESS(rc))
614 {
615 /* Just skip the file name and and extra field. We use the data
616 * from the Central Directory Header. */
617 rc = RTVfsIoStrmSkip(pThis->hVfsIos, cbExtra);
618 if (RT_SUCCESS(rc))
619 *poffStartData = offLocalFileHeader + sizeof(lfh) - 1 + cbExtra;
620 }
621 }
622 else
623 rc = VERR_PKZIP_BAD_LF_HEADER;
624 }
625
626 return rc;
627}
628
629
630/**
631 * Scan the current Central Directory Header.
632 */
633static int rtZipPkzipFssIosReadCdh(PRTZIPPKZIPFSSTREAM pThis, uint64_t *poffStartData,
634 RTZIPPKZIP_COMP_METHOD *penmCompMethod, uint64_t *pcbCompressed)
635{
636 int rc;
637
638 uint64_t offCd = pThis->offNextCdh - pThis->offFirstCdh;
639 if ( pThis->iCentrDirEntry < pThis->cCentrDirEntries
640 || offCd + sizeof(RTZIPPKZIPCENTRDIRHDR) - 1 <= pThis->cbCentrDir)
641 {
642 RTZIPPKZIPCENTRDIRHDR cdh;
643 rc = RTVfsIoStrmReadAt(pThis->hVfsIos, pThis->offNextCdh,
644 &cdh, sizeof(cdh) - 1, true /*fBlocking*/, NULL);
645 if (RT_SUCCESS(rc))
646 {
647 pThis->offNextCdh += sizeof(cdh) - 1;
648 pThis->iCentrDirEntry++;
649 size_t cbExtra = 0;
650 rc = rtZipPkzipParseCentrDirHeader(&pThis->PkzipReader, &cdh, &cbExtra);
651 if (RT_SUCCESS(rc))
652 {
653 if (offCd + sizeof(RTZIPPKZIPCENTRDIRHDR) - 1 + cbExtra <= pThis->cbCentrDir)
654 {
655 /* extra data up to 64k */
656 uint8_t *pu8Buf = (uint8_t*)RTMemTmpAlloc(cbExtra);
657 if (pu8Buf)
658 {
659 rc = RTVfsIoStrmRead(pThis->hVfsIos, pu8Buf, cbExtra, true /*fBlocking*/, NULL);
660 if (RT_SUCCESS(rc))
661 {
662 rc = rtZipPkzipParseCentrDirHeaderExtra(&pThis->PkzipReader, pu8Buf, cbExtra,
663 penmCompMethod, pcbCompressed);
664 if (RT_SUCCESS(rc))
665 rc = rtZipPkzipFssIosReadLfh(pThis, poffStartData);
666 }
667 pThis->offNextCdh += cbExtra;
668 RTMemTmpFree(pu8Buf);
669 }
670 else
671 rc = VERR_NO_MEMORY;
672 }
673 else
674 rc = VERR_EOF;
675 }
676 }
677 }
678 else
679 rc = VERR_EOF;
680
681 return rc;
682}
683
684
685/**
686 * Scan for the End of Central Directory Record. Of course this works not if
687 * the stream is non-seekable (i.e. a pipe).
688 */
689static int rtZipPkzipFssIosReadEocb(PRTZIPPKZIPFSSTREAM pThis)
690{
691 RTFSOBJINFO Info;
692 int rc = RTVfsIoStrmQueryInfo(pThis->hVfsIos, &Info, RTFSOBJATTRADD_UNIX);
693 if (RT_FAILURE(rc))
694 return rc;
695
696 uint64_t cbFile = Info.cbObject;
697 if (cbFile < sizeof(RTZIPPKZIPENDOFCENTRDIRREC)-1)
698 return VERR_PKZIP_NO_EOCB;
699
700 /* search for start of the 'end of Central Directory Record' */
701 size_t cbBuf = RT_MIN(_1K, cbFile);
702 uint8_t *pu8Buf = (uint8_t*)RTMemTmpAlloc(cbBuf);
703 if (!pu8Buf)
704 return VERR_NO_MEMORY;
705
706 /* maximum size of EOCD comment 2^16-1 */
707 const size_t cbHdrMax = 0xffff + sizeof(RTZIPPKZIPENDOFCENTRDIRREC) - 1;
708 uint64_t offMin = cbFile >= cbHdrMax ? cbFile - cbHdrMax : 0;
709
710 uint64_t off = cbFile - cbBuf;
711 while (off >= offMin)
712 {
713 rc = RTVfsIoStrmReadAt(pThis->hVfsIos, off, pu8Buf, cbBuf, true /*fBlocking*/, NULL);
714 if (RT_FAILURE(rc))
715 break;
716 int offMagic;
717 if (rtZipPkzipReaderScanEocd(pu8Buf, cbBuf, &offMagic))
718 {
719 off += offMagic;
720 RTZIPPKZIPENDOFCENTRDIRREC eocd;
721 rc = RTVfsIoStrmReadAt(pThis->hVfsIos, off, &eocd, sizeof(eocd) - 1,
722 true /*fBlocking*/, NULL);
723 if (RT_SUCCESS(rc))
724 {
725 /* well, this shouldn't fail if the content didn't change */
726 if (eocd.u32Magic == RTZIPPKZIPENDOFCENTRDIRREC_MAGIC)
727 {
728 /* sanity check */
729 if (off + RT_UOFFSETOF(RTZIPPKZIPENDOFCENTRDIRREC, u8Comment) + eocd.cbComment == cbFile)
730 {
731 pThis->offFirstCdh = eocd.offCentrDir;
732 pThis->offNextCdh = eocd.offCentrDir;
733 pThis->iCentrDirEntry = 0;
734 pThis->cCentrDirEntries = eocd.cCentrDirRecords;
735 pThis->cbCentrDir = eocd.cbCentrDir;
736 pThis->PkzipReader.fHaveEocd = true;
737 }
738 else
739 rc = VERR_PKZIP_NO_EOCB;
740 }
741 else
742 rc = VERR_PKZIP_NO_EOCB;
743 }
744 if (rc != VERR_PKZIP_NO_EOCB)
745 break;
746 }
747 else
748 rc = VERR_PKZIP_NO_EOCB;
749 /* overlap the following read */
750 off -= cbBuf - 4;
751 }
752
753 RTMemTmpFree(pu8Buf);
754
755 /*
756 * Now check for the presence of the Zip64 End of Central Directory Locator.
757 */
758 if ( RT_SUCCESS(rc)
759 && off > (unsigned)sizeof(RTZIPPKZIP64ENDOFCENTRDIRLOC))
760 {
761 off -= sizeof(RTZIPPKZIP64ENDOFCENTRDIRLOC);
762
763 RTZIPPKZIP64ENDOFCENTRDIRLOC eocd64loc;
764 rc = RTVfsIoStrmReadAt(pThis->hVfsIos, off, &eocd64loc, sizeof(eocd64loc), true /*fBlocking*/, NULL);
765 if (RT_SUCCESS(rc))
766 {
767 if (eocd64loc.u32Magic == RTZIPPKZIP64ENDOFCENTRDIRLOC_MAGIC)
768 pThis->PkzipReader.fZip64Eocd = true;
769 }
770 }
771 return rc;
772}
773
774
775/**
776 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
777 */
778static DECLCALLBACK(int) rtZipPkzipFssBaseObj_Close(void *pvThis)
779{
780 NOREF(pvThis);
781 return VINF_SUCCESS;
782}
783
784
785/**
786 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
787 */
788static DECLCALLBACK(int) rtZipPkzipFssBaseObj_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
789{
790 PRTZIPPKZIPBASEOBJ pThis = (PRTZIPPKZIPBASEOBJ)pvThis;
791
792 /*
793 * Copy the desired data.
794 */
795 switch (enmAddAttr)
796 {
797 case RTFSOBJATTRADD_NOTHING:
798 case RTFSOBJATTRADD_UNIX:
799 *pObjInfo = pThis->ObjInfo;
800 break;
801
802 case RTFSOBJATTRADD_UNIX_OWNER:
803 *pObjInfo = pThis->ObjInfo;
804 break;
805
806 case RTFSOBJATTRADD_UNIX_GROUP:
807 *pObjInfo = pThis->ObjInfo;
808 break;
809
810 case RTFSOBJATTRADD_EASIZE:
811 *pObjInfo = pThis->ObjInfo;
812 break;
813
814 default:
815 return VERR_NOT_SUPPORTED;
816 }
817
818 return VINF_SUCCESS;
819}
820
821
822/**
823 * PKZip filesystem base object operations (directory objects).
824 */
825static const RTVFSOBJOPS g_rtZipPkzipFssBaseObjOps =
826{
827 RTVFSOBJOPS_VERSION,
828 RTVFSOBJTYPE_BASE,
829 "PkzipFsStream::Obj",
830 rtZipPkzipFssBaseObj_Close,
831 rtZipPkzipFssBaseObj_QueryInfo,
832 NULL,
833 RTVFSOBJOPS_VERSION
834};
835
836
837/**
838 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
839 */
840static DECLCALLBACK(int) rtZipPkzipFssIos_Close(void *pvThis)
841{
842 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
843
844 RTVfsIoStrmRelease(pThis->hVfsIos);
845 pThis->hVfsIos = NIL_RTVFSIOSTREAM;
846
847 if (pThis->pZip)
848 {
849 RTZipDecompDestroy(pThis->pZip);
850 pThis->pZip = NULL;
851 }
852
853 return rtZipPkzipFssBaseObj_Close(&pThis->BaseObj);
854}
855
856
857/**
858 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
859 */
860static DECLCALLBACK(int) rtZipPkzipFssIos_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
861{
862 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
863 return rtZipPkzipFssBaseObj_QueryInfo(&pThis->BaseObj, pObjInfo, enmAddAttr);
864}
865
866
867/**
868 * Callback function for rtZipPkzipFssIos_Read. For feeding compressed data
869 * into the decompressor function.
870 */
871static DECLCALLBACK(int) rtZipPkzipFssIosReadHelper(void *pvThis, void *pvBuf, size_t cbToRead, size_t *pcbRead)
872{
873 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
874 int rc = VINF_SUCCESS;
875 if (!cbToRead)
876 return rc;
877 if ( pThis->fPassZipType
878 && cbToRead > 0)
879 {
880 uint8_t *pu8Buf = (uint8_t*)pvBuf;
881 pu8Buf[0] = pThis->enmZipType;
882 pvBuf = &pu8Buf[1];
883 cbToRead--;
884 pThis->fPassZipType = false;
885 }
886 if (cbToRead > 0)
887 {
888 size_t cbRead = 0;
889 const size_t cbAvail = pThis->cbComp;
890 rc = RTVfsIoStrmReadAt(pThis->hVfsIos, pThis->offComp, pvBuf,
891 RT_MIN(cbToRead, cbAvail), true /*fBlocking*/, &cbRead);
892 if ( RT_SUCCESS(rc)
893 && cbToRead > cbAvail)
894 rc = pcbRead ? VINF_EOF : VERR_EOF;
895 if ( rc == VINF_EOF
896 && !pcbRead)
897 rc = VERR_EOF;
898 pThis->offComp += cbRead;
899 if (pcbRead)
900 *pcbRead = cbRead;
901 }
902 return rc;
903}
904
905
906/**
907 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
908 */
909static DECLCALLBACK(int) rtZipPkzipFssIos_Read(void *pvThis, RTFOFF off, PRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
910{
911 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
912 Assert(pSgBuf->cSegs == 1);
913 RT_NOREF_PV(fBlocking);
914
915 if (off < 0)
916 off = pThis->offFile;
917 if (off >= (RTFOFF)pThis->cbFile)
918 return pcbRead ? VINF_EOF : VERR_EOF;
919
920 Assert(pThis->cbFile >= pThis->offFile);
921 uint64_t cbLeft = (uint64_t)(pThis->cbFile - pThis->offFile);
922 size_t cbToRead = pSgBuf->paSegs[0].cbSeg;
923 if (cbToRead > cbLeft)
924 {
925 if (!pcbRead)
926 return VERR_EOF;
927 cbToRead = (size_t)cbLeft;
928 }
929
930 /*
931 * Restart decompression at start of stream or on backward seeking.
932 */
933 if ( !pThis->pZip
934 || !off
935 || off < (RTFOFF)pThis->offFile)
936 {
937 switch (pThis->enmCompMethod)
938 {
939 case RTZIPPKZIP_COMP_METHOD_STORED:
940 pThis->enmZipType = RTZIPTYPE_STORE;
941 break;
942
943 case RTZIPPKZIP_COMP_METHOD_DEFLATED:
944 pThis->enmZipType = RTZIPTYPE_ZLIB_NO_HEADER;
945 break;
946
947 default:
948 pThis->enmZipType = RTZIPTYPE_INVALID;
949 break;
950 }
951
952 if (pThis->pZip)
953 {
954 RTZipDecompDestroy(pThis->pZip);
955 pThis->pZip = NULL;
956 }
957 int rc = RTZipDecompCreate(&pThis->pZip, (void*)pThis, rtZipPkzipFssIosReadHelper);
958 if (RT_FAILURE(rc))
959 return rc;
960 }
961
962 /*
963 * Skip bytes if necessary.
964 */
965 if (off > (RTFOFF)pThis->offFile)
966 {
967 uint8_t u8Buf[_1K];
968 while (off > (RTFOFF)pThis->offFile)
969 {
970 size_t cbSkip = off - pThis->offFile;
971 if (cbSkip > sizeof(u8Buf))
972 cbSkip = sizeof(u8Buf);
973 int rc = RTZipDecompress(pThis->pZip, u8Buf, cbSkip, NULL);
974 if (RT_FAILURE(rc))
975 return rc;
976 pThis->offFile += cbSkip;
977 }
978 }
979
980 /*
981 * Do the actual reading.
982 */
983 size_t cbReadStack = 0;
984 if (!pcbRead)
985 pcbRead = &cbReadStack;
986 int rc = RTZipDecompress(pThis->pZip, pSgBuf->paSegs[0].pvSeg, cbToRead, pcbRead);
987 pThis->offFile = off + *pcbRead;
988 RTSgBufAdvance(pSgBuf, *pcbRead);
989
990 if (pThis->offFile >= pThis->cbFile)
991 {
992 Assert(pThis->offFile == pThis->cbFile);
993 pThis->fEndOfStream = true;
994 }
995
996 return rc;
997}
998
999static DECLCALLBACK(int) rtZipPkzipFssIos_Write(void *pvThis, RTFOFF off, PRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
1000{
1001 RT_NOREF_PV(pvThis); RT_NOREF_PV(off); RT_NOREF_PV(pSgBuf); RT_NOREF_PV(fBlocking); RT_NOREF_PV(pcbWritten);
1002 return VERR_NOT_IMPLEMENTED;
1003}
1004
1005static DECLCALLBACK(int) rtZipPkzipFssIos_Flush(void *pvThis)
1006{
1007 RT_NOREF_PV(pvThis);
1008 return VERR_NOT_IMPLEMENTED;
1009}
1010
1011/**
1012 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
1013 */
1014static DECLCALLBACK(int) rtZipPkzipFssIos_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies,
1015 bool fIntr, uint32_t *pfRetEvents)
1016{
1017 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
1018
1019 /* When we've reached the end, read will be set to indicate it. */
1020 if ( (fEvents & RTPOLL_EVT_READ)
1021 && pThis->fEndOfStream)
1022 {
1023 int rc = RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, 0, fIntr, pfRetEvents);
1024 if (RT_SUCCESS(rc))
1025 *pfRetEvents |= RTPOLL_EVT_READ;
1026 else
1027 *pfRetEvents = RTPOLL_EVT_READ;
1028 return VINF_SUCCESS;
1029 }
1030
1031 return RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, cMillies, fIntr, pfRetEvents);
1032}
1033
1034
1035/**
1036 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
1037 */
1038static DECLCALLBACK(int) rtZipPkzipFssIos_Tell(void *pvThis, PRTFOFF poffActual)
1039{
1040 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
1041 *poffActual = pThis->offFile;
1042 return VINF_SUCCESS;
1043}
1044
1045
1046/**
1047 * Pkzip I/O object stream operations.
1048 */
1049static const RTVFSIOSTREAMOPS g_rtZipPkzipFssIosOps =
1050{
1051 { /* Obj */
1052 RTVFSOBJOPS_VERSION,
1053 RTVFSOBJTYPE_IO_STREAM,
1054 "PkzipFsStream::IoStream",
1055 rtZipPkzipFssIos_Close,
1056 rtZipPkzipFssIos_QueryInfo,
1057 NULL,
1058 RTVFSOBJOPS_VERSION
1059 },
1060 RTVFSIOSTREAMOPS_VERSION,
1061 RTVFSIOSTREAMOPS_FEAT_NO_SG,
1062 rtZipPkzipFssIos_Read,
1063 rtZipPkzipFssIos_Write,
1064 rtZipPkzipFssIos_Flush,
1065 rtZipPkzipFssIos_PollOne,
1066 rtZipPkzipFssIos_Tell,
1067 NULL /*Skip*/,
1068 NULL /*ZeroFill*/,
1069 RTVFSIOSTREAMOPS_VERSION
1070};
1071
1072/**
1073 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
1074 */
1075static DECLCALLBACK(int) rtZipPkzipFss_Close(void *pvThis)
1076{
1077 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1078
1079 RTVfsObjRelease(pThis->hVfsCurObj);
1080 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1081 pThis->pCurIosData = NULL;
1082
1083 RTVfsIoStrmRelease(pThis->hVfsIos);
1084 pThis->hVfsIos = NIL_RTVFSIOSTREAM;
1085
1086 return VINF_SUCCESS;
1087}
1088
1089
1090/**
1091 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
1092 */
1093static DECLCALLBACK(int) rtZipPkzipFss_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
1094{
1095 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1096 /* Take the lazy approach here, with the sideffect of providing some info
1097 that is actually kind of useful. */
1098 return RTVfsIoStrmQueryInfo(pThis->hVfsIos, pObjInfo, enmAddAttr);
1099}
1100
1101
1102/**
1103 * @interface_method_impl{RTVFSFSSTREAMOPS,pfnNext}
1104 */
1105static DECLCALLBACK(int) rtZipPkzipFss_Next(void *pvThis, char **ppszName, RTVFSOBJTYPE *penmType, PRTVFSOBJ phVfsObj)
1106{
1107 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1108
1109 /*
1110 * Dispense with the current object.
1111 */
1112 if (pThis->hVfsCurObj != NIL_RTVFSOBJ)
1113 {
1114 if (pThis->pCurIosData)
1115 {
1116 pThis->pCurIosData->fEndOfStream = true;
1117 pThis->pCurIosData->offFile = pThis->pCurIosData->cbFile;
1118 pThis->pCurIosData = NULL;
1119 }
1120
1121 RTVfsObjRelease(pThis->hVfsCurObj);
1122 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1123 }
1124
1125 /*
1126 * Check if we've already reached the end in some way.
1127 */
1128 if (pThis->fEndOfStream)
1129 return VERR_EOF;
1130 if (pThis->rcFatal != VINF_SUCCESS)
1131 return pThis->rcFatal;
1132
1133 int rc = VINF_SUCCESS;
1134
1135 /*
1136 * Read the end of Central Directory Record once.
1137 */
1138 if (!pThis->PkzipReader.fHaveEocd)
1139 rc = rtZipPkzipFssIosReadEocb(pThis);
1140 uint64_t offData = 0;
1141
1142 /*
1143 * Parse the current Central Directory Header.
1144 */
1145 RTZIPPKZIP_COMP_METHOD enmCompMethod = RTZIPPKZIP_COMP_METHOD_STORED;
1146 uint64_t cbCompressed = 0;
1147 if (RT_SUCCESS(rc))
1148 rc = rtZipPkzipFssIosReadCdh(pThis, &offData, &enmCompMethod, &cbCompressed);
1149 if (RT_FAILURE(rc))
1150 return pThis->rcFatal = rc;
1151
1152 /*
1153 * Fill an object info structure from the current Pkzip state.
1154 */
1155 RTFSOBJINFO Info;
1156 rc = rtZipPkzipReaderGetFsObjInfo(&pThis->PkzipReader, &Info);
1157 if (RT_FAILURE(rc))
1158 return pThis->rcFatal = rc;
1159
1160 /*
1161 * Create an object of the appropriate type.
1162 */
1163 RTVFSOBJTYPE enmType;
1164 RTVFSOBJ hVfsObj;
1165 RTFMODE fType = Info.Attr.fMode & RTFS_TYPE_MASK;
1166 switch (fType)
1167 {
1168 case RTFS_TYPE_FILE:
1169 RTVFSIOSTREAM hVfsIos;
1170 PRTZIPPKZIPIOSTREAM pIosData;
1171 rc = RTVfsNewIoStream(&g_rtZipPkzipFssIosOps,
1172 sizeof(*pIosData),
1173 RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN,
1174 NIL_RTVFS,
1175 NIL_RTVFSLOCK,
1176 &hVfsIos,
1177 (void **)&pIosData);
1178 if (RT_FAILURE(rc))
1179 return pThis->rcFatal = rc;
1180
1181 pIosData->BaseObj.pPkzipReader = &pThis->PkzipReader;
1182 pIosData->BaseObj.ObjInfo = Info;
1183 pIosData->cbFile = Info.cbObject;
1184 pIosData->offFile = 0;
1185 pIosData->offComp = offData;
1186 pIosData->offCompStart = offData;
1187 pIosData->cbComp = cbCompressed;
1188 pIosData->enmCompMethod = enmCompMethod;
1189 pIosData->fPassZipType = true;
1190 pIosData->hVfsIos = pThis->hVfsIos;
1191 RTVfsIoStrmRetain(pThis->hVfsIos);
1192 pThis->pCurIosData = pIosData;
1193 enmType = RTVFSOBJTYPE_IO_STREAM;
1194 hVfsObj = RTVfsObjFromIoStream(hVfsIos);
1195 RTVfsIoStrmRelease(hVfsIos);
1196 break;
1197
1198 case RTFS_TYPE_DIRECTORY:
1199 PRTZIPPKZIPBASEOBJ pBaseObjData;
1200 rc = RTVfsNewBaseObj(&g_rtZipPkzipFssBaseObjOps,
1201 sizeof(*pBaseObjData),
1202 NIL_RTVFS,
1203 NIL_RTVFSLOCK,
1204 &hVfsObj,
1205 (void **)&pBaseObjData);
1206 if (RT_FAILURE(rc))
1207 return pThis->rcFatal = rc;
1208
1209 pBaseObjData->pPkzipReader = &pThis->PkzipReader;
1210 pBaseObjData->ObjInfo = Info;
1211 enmType = RTVFSOBJTYPE_BASE;
1212 break;
1213
1214 default:
1215 return pThis->rcFatal = VERR_PKZIP_UNKNOWN_TYPE_FLAG;
1216 }
1217 pThis->hVfsCurObj = hVfsObj;
1218
1219 if (ppszName)
1220 {
1221 rc = RTStrDupEx(ppszName, pThis->PkzipReader.szName);
1222 if (RT_FAILURE(rc))
1223 return pThis->rcFatal = rc;
1224 }
1225
1226 if (phVfsObj)
1227 {
1228 RTVfsObjRetain(hVfsObj);
1229 *phVfsObj = hVfsObj;
1230 }
1231
1232 if (penmType)
1233 *penmType = enmType;
1234
1235 return VINF_SUCCESS;
1236}
1237
1238
1239/**
1240 * PKZip filesystem stream operations.
1241 */
1242static const RTVFSFSSTREAMOPS rtZipPkzipFssOps =
1243{
1244 { /* Obj */
1245 RTVFSOBJOPS_VERSION,
1246 RTVFSOBJTYPE_FS_STREAM,
1247 "PkzipFsStream",
1248 rtZipPkzipFss_Close,
1249 rtZipPkzipFss_QueryInfo,
1250 NULL,
1251 RTVFSOBJOPS_VERSION
1252 },
1253 RTVFSFSSTREAMOPS_VERSION,
1254 0,
1255 rtZipPkzipFss_Next,
1256 NULL,
1257 NULL,
1258 NULL,
1259 RTVFSFSSTREAMOPS_VERSION
1260};
1261
1262
1263RTDECL(int) RTZipPkzipFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss)
1264{
1265 /*
1266 * Input validation.
1267 */
1268 AssertPtrReturn(phVfsFss, VERR_INVALID_HANDLE);
1269 *phVfsFss = NIL_RTVFSFSSTREAM;
1270 AssertPtrReturn(hVfsIosIn, VERR_INVALID_HANDLE);
1271 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
1272
1273 uint32_t cRefs = RTVfsIoStrmRetain(hVfsIosIn);
1274 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
1275
1276 /*
1277 * Retain the input stream and create a new filesystem stream handle.
1278 */
1279 PRTZIPPKZIPFSSTREAM pThis;
1280 RTVFSFSSTREAM hVfsFss;
1281 int rc = RTVfsNewFsStream(&rtZipPkzipFssOps, sizeof(*pThis), NIL_RTVFS, NIL_RTVFSLOCK, RTFILE_O_READ,
1282 &hVfsFss, (void **)&pThis);
1283 if (RT_SUCCESS(rc))
1284 {
1285 pThis->hVfsIos = hVfsIosIn;
1286 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1287 pThis->pCurIosData = NULL;
1288 pThis->fEndOfStream = false;
1289 pThis->rcFatal = VINF_SUCCESS;
1290 pThis->fHaveEocd = false;
1291
1292 *phVfsFss = hVfsFss;
1293 return VINF_SUCCESS;
1294 }
1295
1296 RTVfsIoStrmRelease(hVfsIosIn);
1297 return rc;
1298}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use