VirtualBox

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

Last change on this file since 98103 was 98103, checked in by vboxsync, 23 months 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 98103 2023-01-17 14:15:46Z 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, PCRTSGBUF 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 if (pThis->offFile >= pThis->cbFile)
989 {
990 Assert(pThis->offFile == pThis->cbFile);
991 pThis->fEndOfStream = true;
992 }
993
994 return rc;
995}
996
997static DECLCALLBACK(int) rtZipPkzipFssIos_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
998{
999 RT_NOREF_PV(pvThis); RT_NOREF_PV(off); RT_NOREF_PV(pSgBuf); RT_NOREF_PV(fBlocking); RT_NOREF_PV(pcbWritten);
1000 return VERR_NOT_IMPLEMENTED;
1001}
1002
1003static DECLCALLBACK(int) rtZipPkzipFssIos_Flush(void *pvThis)
1004{
1005 RT_NOREF_PV(pvThis);
1006 return VERR_NOT_IMPLEMENTED;
1007}
1008
1009/**
1010 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
1011 */
1012static DECLCALLBACK(int) rtZipPkzipFssIos_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies,
1013 bool fIntr, uint32_t *pfRetEvents)
1014{
1015 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
1016
1017 /* When we've reached the end, read will be set to indicate it. */
1018 if ( (fEvents & RTPOLL_EVT_READ)
1019 && pThis->fEndOfStream)
1020 {
1021 int rc = RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, 0, fIntr, pfRetEvents);
1022 if (RT_SUCCESS(rc))
1023 *pfRetEvents |= RTPOLL_EVT_READ;
1024 else
1025 *pfRetEvents = RTPOLL_EVT_READ;
1026 return VINF_SUCCESS;
1027 }
1028
1029 return RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, cMillies, fIntr, pfRetEvents);
1030}
1031
1032
1033/**
1034 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
1035 */
1036static DECLCALLBACK(int) rtZipPkzipFssIos_Tell(void *pvThis, PRTFOFF poffActual)
1037{
1038 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
1039 *poffActual = pThis->offFile;
1040 return VINF_SUCCESS;
1041}
1042
1043
1044/**
1045 * Pkzip I/O object stream operations.
1046 */
1047static const RTVFSIOSTREAMOPS g_rtZipPkzipFssIosOps =
1048{
1049 { /* Obj */
1050 RTVFSOBJOPS_VERSION,
1051 RTVFSOBJTYPE_IO_STREAM,
1052 "PkzipFsStream::IoStream",
1053 rtZipPkzipFssIos_Close,
1054 rtZipPkzipFssIos_QueryInfo,
1055 NULL,
1056 RTVFSOBJOPS_VERSION
1057 },
1058 RTVFSIOSTREAMOPS_VERSION,
1059 RTVFSIOSTREAMOPS_FEAT_NO_SG,
1060 rtZipPkzipFssIos_Read,
1061 rtZipPkzipFssIos_Write,
1062 rtZipPkzipFssIos_Flush,
1063 rtZipPkzipFssIos_PollOne,
1064 rtZipPkzipFssIos_Tell,
1065 NULL /*Skip*/,
1066 NULL /*ZeroFill*/,
1067 RTVFSIOSTREAMOPS_VERSION
1068};
1069
1070/**
1071 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
1072 */
1073static DECLCALLBACK(int) rtZipPkzipFss_Close(void *pvThis)
1074{
1075 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1076
1077 RTVfsObjRelease(pThis->hVfsCurObj);
1078 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1079 pThis->pCurIosData = NULL;
1080
1081 RTVfsIoStrmRelease(pThis->hVfsIos);
1082 pThis->hVfsIos = NIL_RTVFSIOSTREAM;
1083
1084 return VINF_SUCCESS;
1085}
1086
1087
1088/**
1089 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
1090 */
1091static DECLCALLBACK(int) rtZipPkzipFss_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
1092{
1093 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1094 /* Take the lazy approach here, with the sideffect of providing some info
1095 that is actually kind of useful. */
1096 return RTVfsIoStrmQueryInfo(pThis->hVfsIos, pObjInfo, enmAddAttr);
1097}
1098
1099
1100/**
1101 * @interface_method_impl{RTVFSFSSTREAMOPS,pfnNext}
1102 */
1103static DECLCALLBACK(int) rtZipPkzipFss_Next(void *pvThis, char **ppszName, RTVFSOBJTYPE *penmType, PRTVFSOBJ phVfsObj)
1104{
1105 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1106
1107 /*
1108 * Dispense with the current object.
1109 */
1110 if (pThis->hVfsCurObj != NIL_RTVFSOBJ)
1111 {
1112 if (pThis->pCurIosData)
1113 {
1114 pThis->pCurIosData->fEndOfStream = true;
1115 pThis->pCurIosData->offFile = pThis->pCurIosData->cbFile;
1116 pThis->pCurIosData = NULL;
1117 }
1118
1119 RTVfsObjRelease(pThis->hVfsCurObj);
1120 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1121 }
1122
1123 /*
1124 * Check if we've already reached the end in some way.
1125 */
1126 if (pThis->fEndOfStream)
1127 return VERR_EOF;
1128 if (pThis->rcFatal != VINF_SUCCESS)
1129 return pThis->rcFatal;
1130
1131 int rc = VINF_SUCCESS;
1132
1133 /*
1134 * Read the end of Central Directory Record once.
1135 */
1136 if (!pThis->PkzipReader.fHaveEocd)
1137 rc = rtZipPkzipFssIosReadEocb(pThis);
1138 uint64_t offData = 0;
1139
1140 /*
1141 * Parse the current Central Directory Header.
1142 */
1143 RTZIPPKZIP_COMP_METHOD enmCompMethod = RTZIPPKZIP_COMP_METHOD_STORED;
1144 uint64_t cbCompressed = 0;
1145 if (RT_SUCCESS(rc))
1146 rc = rtZipPkzipFssIosReadCdh(pThis, &offData, &enmCompMethod, &cbCompressed);
1147 if (RT_FAILURE(rc))
1148 return pThis->rcFatal = rc;
1149
1150 /*
1151 * Fill an object info structure from the current Pkzip state.
1152 */
1153 RTFSOBJINFO Info;
1154 rc = rtZipPkzipReaderGetFsObjInfo(&pThis->PkzipReader, &Info);
1155 if (RT_FAILURE(rc))
1156 return pThis->rcFatal = rc;
1157
1158 /*
1159 * Create an object of the appropriate type.
1160 */
1161 RTVFSOBJTYPE enmType;
1162 RTVFSOBJ hVfsObj;
1163 RTFMODE fType = Info.Attr.fMode & RTFS_TYPE_MASK;
1164 switch (fType)
1165 {
1166 case RTFS_TYPE_FILE:
1167 RTVFSIOSTREAM hVfsIos;
1168 PRTZIPPKZIPIOSTREAM pIosData;
1169 rc = RTVfsNewIoStream(&g_rtZipPkzipFssIosOps,
1170 sizeof(*pIosData),
1171 RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN,
1172 NIL_RTVFS,
1173 NIL_RTVFSLOCK,
1174 &hVfsIos,
1175 (void **)&pIosData);
1176 if (RT_FAILURE(rc))
1177 return pThis->rcFatal = rc;
1178
1179 pIosData->BaseObj.pPkzipReader = &pThis->PkzipReader;
1180 pIosData->BaseObj.ObjInfo = Info;
1181 pIosData->cbFile = Info.cbObject;
1182 pIosData->offFile = 0;
1183 pIosData->offComp = offData;
1184 pIosData->offCompStart = offData;
1185 pIosData->cbComp = cbCompressed;
1186 pIosData->enmCompMethod = enmCompMethod;
1187 pIosData->fPassZipType = true;
1188 pIosData->hVfsIos = pThis->hVfsIos;
1189 RTVfsIoStrmRetain(pThis->hVfsIos);
1190 pThis->pCurIosData = pIosData;
1191 enmType = RTVFSOBJTYPE_IO_STREAM;
1192 hVfsObj = RTVfsObjFromIoStream(hVfsIos);
1193 RTVfsIoStrmRelease(hVfsIos);
1194 break;
1195
1196 case RTFS_TYPE_DIRECTORY:
1197 PRTZIPPKZIPBASEOBJ pBaseObjData;
1198 rc = RTVfsNewBaseObj(&g_rtZipPkzipFssBaseObjOps,
1199 sizeof(*pBaseObjData),
1200 NIL_RTVFS,
1201 NIL_RTVFSLOCK,
1202 &hVfsObj,
1203 (void **)&pBaseObjData);
1204 if (RT_FAILURE(rc))
1205 return pThis->rcFatal = rc;
1206
1207 pBaseObjData->pPkzipReader = &pThis->PkzipReader;
1208 pBaseObjData->ObjInfo = Info;
1209 enmType = RTVFSOBJTYPE_BASE;
1210 break;
1211
1212 default:
1213 return pThis->rcFatal = VERR_PKZIP_UNKNOWN_TYPE_FLAG;
1214 }
1215 pThis->hVfsCurObj = hVfsObj;
1216
1217 if (ppszName)
1218 {
1219 rc = RTStrDupEx(ppszName, pThis->PkzipReader.szName);
1220 if (RT_FAILURE(rc))
1221 return pThis->rcFatal = rc;
1222 }
1223
1224 if (phVfsObj)
1225 {
1226 RTVfsObjRetain(hVfsObj);
1227 *phVfsObj = hVfsObj;
1228 }
1229
1230 if (penmType)
1231 *penmType = enmType;
1232
1233 return VINF_SUCCESS;
1234}
1235
1236
1237/**
1238 * PKZip filesystem stream operations.
1239 */
1240static const RTVFSFSSTREAMOPS rtZipPkzipFssOps =
1241{
1242 { /* Obj */
1243 RTVFSOBJOPS_VERSION,
1244 RTVFSOBJTYPE_FS_STREAM,
1245 "PkzipFsStream",
1246 rtZipPkzipFss_Close,
1247 rtZipPkzipFss_QueryInfo,
1248 NULL,
1249 RTVFSOBJOPS_VERSION
1250 },
1251 RTVFSFSSTREAMOPS_VERSION,
1252 0,
1253 rtZipPkzipFss_Next,
1254 NULL,
1255 NULL,
1256 NULL,
1257 RTVFSFSSTREAMOPS_VERSION
1258};
1259
1260
1261RTDECL(int) RTZipPkzipFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss)
1262{
1263 /*
1264 * Input validation.
1265 */
1266 AssertPtrReturn(phVfsFss, VERR_INVALID_HANDLE);
1267 *phVfsFss = NIL_RTVFSFSSTREAM;
1268 AssertPtrReturn(hVfsIosIn, VERR_INVALID_HANDLE);
1269 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
1270
1271 uint32_t cRefs = RTVfsIoStrmRetain(hVfsIosIn);
1272 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
1273
1274 /*
1275 * Retain the input stream and create a new filesystem stream handle.
1276 */
1277 PRTZIPPKZIPFSSTREAM pThis;
1278 RTVFSFSSTREAM hVfsFss;
1279 int rc = RTVfsNewFsStream(&rtZipPkzipFssOps, sizeof(*pThis), NIL_RTVFS, NIL_RTVFSLOCK, RTFILE_O_READ,
1280 &hVfsFss, (void **)&pThis);
1281 if (RT_SUCCESS(rc))
1282 {
1283 pThis->hVfsIos = hVfsIosIn;
1284 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1285 pThis->pCurIosData = NULL;
1286 pThis->fEndOfStream = false;
1287 pThis->rcFatal = VINF_SUCCESS;
1288 pThis->fHaveEocd = false;
1289
1290 *phVfsFss = hVfsFss;
1291 return VINF_SUCCESS;
1292 }
1293
1294 RTVfsIoStrmRelease(hVfsIosIn);
1295 return rc;
1296}
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