VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 4 days ago

Copyright year updates by scm.

  • 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 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - PKZIP Virtual Filesystem.
4 */
5
6/*
7 * Copyright (C) 2014-2024 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 rc;
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 {
879 Assert(cbToRead);
880
881 uint8_t *pu8Buf = (uint8_t*)pvBuf;
882 pu8Buf[0] = pThis->enmZipType;
883 pvBuf = &pu8Buf[1];
884 cbToRead--;
885 pThis->fPassZipType = false;
886 }
887 if (cbToRead > 0)
888 {
889 size_t cbRead = 0;
890 const size_t cbAvail = pThis->cbComp;
891 rc = RTVfsIoStrmReadAt(pThis->hVfsIos, pThis->offComp, pvBuf,
892 RT_MIN(cbToRead, cbAvail), true /*fBlocking*/, &cbRead);
893 if ( RT_SUCCESS(rc)
894 && cbToRead > cbAvail)
895 rc = pcbRead ? VINF_EOF : VERR_EOF;
896 if ( rc == VINF_EOF
897 && !pcbRead)
898 rc = VERR_EOF;
899 pThis->offComp += cbRead;
900 if (pcbRead)
901 *pcbRead = cbRead;
902 }
903 return rc;
904}
905
906
907/**
908 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
909 */
910static DECLCALLBACK(int) rtZipPkzipFssIos_Read(void *pvThis, RTFOFF off, PRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
911{
912 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
913 Assert(pSgBuf->cSegs == 1);
914 RT_NOREF_PV(fBlocking);
915
916 if (off < 0)
917 off = pThis->offFile;
918 if (off >= (RTFOFF)pThis->cbFile)
919 return pcbRead ? VINF_EOF : VERR_EOF;
920
921 Assert(pThis->cbFile >= pThis->offFile);
922 uint64_t cbLeft = (uint64_t)(pThis->cbFile - pThis->offFile);
923 size_t cbToRead = pSgBuf->paSegs[0].cbSeg;
924 if (cbToRead > cbLeft)
925 {
926 if (!pcbRead)
927 return VERR_EOF;
928 cbToRead = (size_t)cbLeft;
929 }
930
931 /*
932 * Restart decompression at start of stream or on backward seeking.
933 */
934 if ( !pThis->pZip
935 || !off
936 || off < (RTFOFF)pThis->offFile)
937 {
938 switch (pThis->enmCompMethod)
939 {
940 case RTZIPPKZIP_COMP_METHOD_STORED:
941 pThis->enmZipType = RTZIPTYPE_STORE;
942 break;
943
944 case RTZIPPKZIP_COMP_METHOD_DEFLATED:
945 pThis->enmZipType = RTZIPTYPE_ZLIB_NO_HEADER;
946 break;
947
948 default:
949 pThis->enmZipType = RTZIPTYPE_INVALID;
950 break;
951 }
952
953 if (pThis->pZip)
954 {
955 RTZipDecompDestroy(pThis->pZip);
956 pThis->pZip = NULL;
957 }
958 int rc = RTZipDecompCreate(&pThis->pZip, (void*)pThis, rtZipPkzipFssIosReadHelper);
959 if (RT_FAILURE(rc))
960 return rc;
961 }
962
963 /*
964 * Skip bytes if necessary.
965 */
966 if (off > (RTFOFF)pThis->offFile)
967 {
968 uint8_t u8Buf[_1K];
969 while (off > (RTFOFF)pThis->offFile)
970 {
971 size_t cbSkip = off - pThis->offFile;
972 if (cbSkip > sizeof(u8Buf))
973 cbSkip = sizeof(u8Buf);
974 int rc = RTZipDecompress(pThis->pZip, u8Buf, cbSkip, NULL);
975 if (RT_FAILURE(rc))
976 return rc;
977 pThis->offFile += cbSkip;
978 }
979 }
980
981 /*
982 * Do the actual reading.
983 */
984 size_t cbReadStack = 0;
985 if (!pcbRead)
986 pcbRead = &cbReadStack;
987 int rc = RTZipDecompress(pThis->pZip, pSgBuf->paSegs[0].pvSeg, cbToRead, pcbRead);
988 pThis->offFile = off + *pcbRead;
989 RTSgBufAdvance(pSgBuf, *pcbRead);
990
991 if (pThis->offFile >= pThis->cbFile)
992 {
993 Assert(pThis->offFile == pThis->cbFile);
994 pThis->fEndOfStream = true;
995 }
996
997 return rc;
998}
999
1000static DECLCALLBACK(int) rtZipPkzipFssIos_Write(void *pvThis, RTFOFF off, PRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
1001{
1002 RT_NOREF_PV(pvThis); RT_NOREF_PV(off); RT_NOREF_PV(pSgBuf); RT_NOREF_PV(fBlocking); RT_NOREF_PV(pcbWritten);
1003 return VERR_NOT_IMPLEMENTED;
1004}
1005
1006static DECLCALLBACK(int) rtZipPkzipFssIos_Flush(void *pvThis)
1007{
1008 RT_NOREF_PV(pvThis);
1009 return VERR_NOT_IMPLEMENTED;
1010}
1011
1012/**
1013 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
1014 */
1015static DECLCALLBACK(int) rtZipPkzipFssIos_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies,
1016 bool fIntr, uint32_t *pfRetEvents)
1017{
1018 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
1019
1020 /* When we've reached the end, read will be set to indicate it. */
1021 if ( (fEvents & RTPOLL_EVT_READ)
1022 && pThis->fEndOfStream)
1023 {
1024 int rc = RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, 0, fIntr, pfRetEvents);
1025 if (RT_SUCCESS(rc))
1026 *pfRetEvents |= RTPOLL_EVT_READ;
1027 else
1028 *pfRetEvents = RTPOLL_EVT_READ;
1029 return VINF_SUCCESS;
1030 }
1031
1032 return RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, cMillies, fIntr, pfRetEvents);
1033}
1034
1035
1036/**
1037 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
1038 */
1039static DECLCALLBACK(int) rtZipPkzipFssIos_Tell(void *pvThis, PRTFOFF poffActual)
1040{
1041 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
1042 *poffActual = pThis->offFile;
1043 return VINF_SUCCESS;
1044}
1045
1046
1047/**
1048 * Pkzip I/O object stream operations.
1049 */
1050static const RTVFSIOSTREAMOPS g_rtZipPkzipFssIosOps =
1051{
1052 { /* Obj */
1053 RTVFSOBJOPS_VERSION,
1054 RTVFSOBJTYPE_IO_STREAM,
1055 "PkzipFsStream::IoStream",
1056 rtZipPkzipFssIos_Close,
1057 rtZipPkzipFssIos_QueryInfo,
1058 NULL,
1059 RTVFSOBJOPS_VERSION
1060 },
1061 RTVFSIOSTREAMOPS_VERSION,
1062 RTVFSIOSTREAMOPS_FEAT_NO_SG,
1063 rtZipPkzipFssIos_Read,
1064 rtZipPkzipFssIos_Write,
1065 rtZipPkzipFssIos_Flush,
1066 rtZipPkzipFssIos_PollOne,
1067 rtZipPkzipFssIos_Tell,
1068 NULL /*Skip*/,
1069 NULL /*ZeroFill*/,
1070 RTVFSIOSTREAMOPS_VERSION
1071};
1072
1073/**
1074 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
1075 */
1076static DECLCALLBACK(int) rtZipPkzipFss_Close(void *pvThis)
1077{
1078 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1079
1080 RTVfsObjRelease(pThis->hVfsCurObj);
1081 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1082 pThis->pCurIosData = NULL;
1083
1084 RTVfsIoStrmRelease(pThis->hVfsIos);
1085 pThis->hVfsIos = NIL_RTVFSIOSTREAM;
1086
1087 return VINF_SUCCESS;
1088}
1089
1090
1091/**
1092 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
1093 */
1094static DECLCALLBACK(int) rtZipPkzipFss_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
1095{
1096 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1097 /* Take the lazy approach here, with the sideffect of providing some info
1098 that is actually kind of useful. */
1099 return RTVfsIoStrmQueryInfo(pThis->hVfsIos, pObjInfo, enmAddAttr);
1100}
1101
1102
1103/**
1104 * @interface_method_impl{RTVFSFSSTREAMOPS,pfnNext}
1105 */
1106static DECLCALLBACK(int) rtZipPkzipFss_Next(void *pvThis, char **ppszName, RTVFSOBJTYPE *penmType, PRTVFSOBJ phVfsObj)
1107{
1108 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1109
1110 /*
1111 * Dispense with the current object.
1112 */
1113 if (pThis->hVfsCurObj != NIL_RTVFSOBJ)
1114 {
1115 if (pThis->pCurIosData)
1116 {
1117 pThis->pCurIosData->fEndOfStream = true;
1118 pThis->pCurIosData->offFile = pThis->pCurIosData->cbFile;
1119 pThis->pCurIosData = NULL;
1120 }
1121
1122 RTVfsObjRelease(pThis->hVfsCurObj);
1123 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1124 }
1125
1126 /*
1127 * Check if we've already reached the end in some way.
1128 */
1129 if (pThis->fEndOfStream)
1130 return VERR_EOF;
1131 if (pThis->rcFatal != VINF_SUCCESS)
1132 return pThis->rcFatal;
1133
1134 int rc = VINF_SUCCESS;
1135
1136 /*
1137 * Read the end of Central Directory Record once.
1138 */
1139 if (!pThis->PkzipReader.fHaveEocd)
1140 rc = rtZipPkzipFssIosReadEocb(pThis);
1141 uint64_t offData = 0;
1142
1143 /*
1144 * Parse the current Central Directory Header.
1145 */
1146 RTZIPPKZIP_COMP_METHOD enmCompMethod = RTZIPPKZIP_COMP_METHOD_STORED;
1147 uint64_t cbCompressed = 0;
1148 if (RT_SUCCESS(rc))
1149 rc = rtZipPkzipFssIosReadCdh(pThis, &offData, &enmCompMethod, &cbCompressed);
1150 if (RT_FAILURE(rc))
1151 return pThis->rcFatal = rc;
1152
1153 /*
1154 * Fill an object info structure from the current Pkzip state.
1155 */
1156 RTFSOBJINFO Info;
1157 rc = rtZipPkzipReaderGetFsObjInfo(&pThis->PkzipReader, &Info);
1158 if (RT_FAILURE(rc))
1159 return pThis->rcFatal = rc;
1160
1161 /*
1162 * Create an object of the appropriate type.
1163 */
1164 RTVFSOBJTYPE enmType;
1165 RTVFSOBJ hVfsObj;
1166 RTFMODE fType = Info.Attr.fMode & RTFS_TYPE_MASK;
1167 switch (fType)
1168 {
1169 case RTFS_TYPE_FILE:
1170 RTVFSIOSTREAM hVfsIos;
1171 PRTZIPPKZIPIOSTREAM pIosData;
1172 rc = RTVfsNewIoStream(&g_rtZipPkzipFssIosOps,
1173 sizeof(*pIosData),
1174 RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN,
1175 NIL_RTVFS,
1176 NIL_RTVFSLOCK,
1177 &hVfsIos,
1178 (void **)&pIosData);
1179 if (RT_FAILURE(rc))
1180 return pThis->rcFatal = rc;
1181
1182 pIosData->BaseObj.pPkzipReader = &pThis->PkzipReader;
1183 pIosData->BaseObj.ObjInfo = Info;
1184 pIosData->cbFile = Info.cbObject;
1185 pIosData->offFile = 0;
1186 pIosData->offComp = offData;
1187 pIosData->offCompStart = offData;
1188 pIosData->cbComp = cbCompressed;
1189 pIosData->enmCompMethod = enmCompMethod;
1190 pIosData->fPassZipType = true;
1191 pIosData->hVfsIos = pThis->hVfsIos;
1192 RTVfsIoStrmRetain(pThis->hVfsIos);
1193 pThis->pCurIosData = pIosData;
1194 enmType = RTVFSOBJTYPE_IO_STREAM;
1195 hVfsObj = RTVfsObjFromIoStream(hVfsIos);
1196 RTVfsIoStrmRelease(hVfsIos);
1197 break;
1198
1199 case RTFS_TYPE_DIRECTORY:
1200 PRTZIPPKZIPBASEOBJ pBaseObjData;
1201 rc = RTVfsNewBaseObj(&g_rtZipPkzipFssBaseObjOps,
1202 sizeof(*pBaseObjData),
1203 NIL_RTVFS,
1204 NIL_RTVFSLOCK,
1205 &hVfsObj,
1206 (void **)&pBaseObjData);
1207 if (RT_FAILURE(rc))
1208 return pThis->rcFatal = rc;
1209
1210 pBaseObjData->pPkzipReader = &pThis->PkzipReader;
1211 pBaseObjData->ObjInfo = Info;
1212 enmType = RTVFSOBJTYPE_BASE;
1213 break;
1214
1215 default:
1216 return pThis->rcFatal = VERR_PKZIP_UNKNOWN_TYPE_FLAG;
1217 }
1218 pThis->hVfsCurObj = hVfsObj;
1219
1220 if (ppszName)
1221 {
1222 rc = RTStrDupEx(ppszName, pThis->PkzipReader.szName);
1223 if (RT_FAILURE(rc))
1224 return pThis->rcFatal = rc;
1225 }
1226
1227 if (phVfsObj)
1228 {
1229 RTVfsObjRetain(hVfsObj);
1230 *phVfsObj = hVfsObj;
1231 }
1232
1233 if (penmType)
1234 *penmType = enmType;
1235
1236 return VINF_SUCCESS;
1237}
1238
1239
1240/**
1241 * PKZip filesystem stream operations.
1242 */
1243static const RTVFSFSSTREAMOPS rtZipPkzipFssOps =
1244{
1245 { /* Obj */
1246 RTVFSOBJOPS_VERSION,
1247 RTVFSOBJTYPE_FS_STREAM,
1248 "PkzipFsStream",
1249 rtZipPkzipFss_Close,
1250 rtZipPkzipFss_QueryInfo,
1251 NULL,
1252 RTVFSOBJOPS_VERSION
1253 },
1254 RTVFSFSSTREAMOPS_VERSION,
1255 0,
1256 rtZipPkzipFss_Next,
1257 NULL,
1258 NULL,
1259 NULL,
1260 RTVFSFSSTREAMOPS_VERSION
1261};
1262
1263
1264RTDECL(int) RTZipPkzipFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss)
1265{
1266 /*
1267 * Input validation.
1268 */
1269 AssertPtrReturn(phVfsFss, VERR_INVALID_HANDLE);
1270 *phVfsFss = NIL_RTVFSFSSTREAM;
1271 AssertPtrReturn(hVfsIosIn, VERR_INVALID_HANDLE);
1272 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
1273
1274 uint32_t cRefs = RTVfsIoStrmRetain(hVfsIosIn);
1275 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
1276
1277 /*
1278 * Retain the input stream and create a new filesystem stream handle.
1279 */
1280 PRTZIPPKZIPFSSTREAM pThis;
1281 RTVFSFSSTREAM hVfsFss;
1282 int rc = RTVfsNewFsStream(&rtZipPkzipFssOps, sizeof(*pThis), NIL_RTVFS, NIL_RTVFSLOCK, RTFILE_O_READ,
1283 &hVfsFss, (void **)&pThis);
1284 if (RT_SUCCESS(rc))
1285 {
1286 pThis->hVfsIos = hVfsIosIn;
1287 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1288 pThis->pCurIosData = NULL;
1289 pThis->fEndOfStream = false;
1290 pThis->rcFatal = VINF_SUCCESS;
1291 pThis->fHaveEocd = false;
1292
1293 *phVfsFss = hVfsFss;
1294 return VINF_SUCCESS;
1295 }
1296
1297 RTVfsIoStrmRelease(hVfsIosIn);
1298 return rc;
1299}
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