VirtualBox

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

Last change on this file since 96120 was 94291, checked in by vboxsync, 3 years ago

IPRT,Storage: Adding RTVfsQueryLabel and internally a generic pfnQueryInfoEx method to the RTVFSOBJOPS function table. Untested implementation of the latter for iso/udf. bugref:9781

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