VirtualBox

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

Last change on this file since 59827 was 57372, checked in by vboxsync, 9 years ago

scm: fixes in previous cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.7 KB
Line 
1/* $Id: pkzipvfs.cpp 57372 2015-08-14 22:01:25Z vboxsync $ */
2/** @file
3 * IPRT - PKZIP Virtual Filesystem.
4 */
5
6/*
7 * Copyright (C) 2014-2015 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 if (pLfh->cbFilename >= sizeof(pThis->szName))
409 return VERR_PKZIP_NAME_TOO_LONG;
410
411 *pcbExtra = pLfh->cbFilename + pLfh->cbExtra;
412 return VINF_SUCCESS;
413}
414
415
416/**
417 * Parse the Central Directory Header.
418 */
419static int rtZipPkzipParseCentrDirHeader(PRTZIPPKZIPREADER pThis, PRTZIPPKZIPCENTRDIRHDR pCdh, size_t *pcbExtra)
420{
421 if (pCdh->u32Magic != RTZIPPKZIPCENTRDIRHDR_MAGIC)
422 return VERR_PKZIP_BAD_CDF_HEADER;
423
424 if (pCdh->cbFilename >= sizeof(pThis->szName))
425 return VERR_PKZIP_NAME_TOO_LONG;
426
427 *pcbExtra = pCdh->cbFilename + pCdh->cbExtra + pCdh->cbComment;
428
429 pThis->cdh = *pCdh;
430 pThis->fZip64Ex = false;
431 return VINF_SUCCESS;
432}
433
434
435/**
436 * Return the offset of the Local File Header.
437 */
438static uint64_t rtZipPkzipReaderOffLocalHeader(PRTZIPPKZIPREADER pThis)
439{
440 if (pThis->fZip64Ex && pThis->cdh.offLocalFileHeader == (uint32_t)-1)
441 return pThis->cd64ex.offLocalFileHeader;
442
443 return pThis->cdh.offLocalFileHeader;
444}
445
446
447/**
448 * Return the uncompressed object size.
449 */
450static uint64_t rtZipPkzipReaderUncompressed(PRTZIPPKZIPREADER pThis)
451{
452 if (pThis->fZip64Ex && pThis->cdh.cbUncompressed == (uint32_t)-1)
453 return pThis->cd64ex.cbUncompressed;
454
455 return pThis->cdh.cbUncompressed;
456}
457
458
459/**
460 * Return the compressed object size.
461 */
462static uint64_t rtZipPkzipReaderCompressed(PRTZIPPKZIPREADER pThis)
463{
464 if (pThis->fZip64Ex && pThis->cdh.cbCompressed == (uint32_t)-1)
465 return pThis->cd64ex.cbCompressed;
466
467 return pThis->cdh.cbCompressed;
468}
469
470
471/**
472 * Parse the extra part of the Central Directory Header.
473 */
474static int rtZipPkzipParseCentrDirHeaderExtra(PRTZIPPKZIPREADER pThis, uint8_t *pu8Buf, size_t cb,
475 RTZIPPKZIP_COMP_METHOD *penmCompMethod, uint64_t *pcbCompressed)
476{
477 int rc = RTStrCopyEx(pThis->szName, sizeof(pThis->szName), (const char*)pu8Buf, pThis->cdh.cbFilename);
478 if (RT_SUCCESS(rc))
479 {
480 uint8_t *offStart = pu8Buf;
481 pu8Buf += pThis->cdh.cbFilename;
482 cb = pThis->cdh.cbExtra;
483 while (cb >= 4)
484 {
485 uint16_t idExtra = *(uint16_t*)pu8Buf;
486 pu8Buf += 2;
487 uint16_t cbExtra = *(uint16_t*)pu8Buf;
488 pu8Buf += 2;
489 cb -= 4;
490
491 if (cb >= cbExtra)
492 {
493 switch (idExtra)
494 {
495 case 0x0001:
496 /*
497 * ZIP64 Extended Information Extra Field.
498 */
499 if (!pThis->fZip64Eocd)
500 return VERR_PKZIP_ZIP64EX_IN_ZIP32;
501 /* Not all fields are really used. */
502 RT_ZERO(pThis->cd64ex);
503 memcpy(&pThis->cd64ex, pu8Buf, cbExtra);
504 pThis->fZip64Ex = true;
505 break;
506
507 default:
508 /* unknown, just skip */
509 break;
510 }
511 pu8Buf += cbExtra;
512 cb -= cbExtra;
513 }
514 else
515 {
516 rc = VERR_PKZIP_BAD_CDF_HEADER;
517 break;
518 }
519 }
520
521 *penmCompMethod = (RTZIPPKZIP_COMP_METHOD)pThis->cdh.u16ComprMethod;
522 *pcbCompressed = rtZipPkzipReaderCompressed(pThis);
523 }
524 return VINF_SUCCESS;
525}
526
527
528/**
529 * Translate a PKZip header to an IPRT object info structure.
530 */
531static int rtZipPkzipReaderGetFsObjInfo(PRTZIPPKZIPREADER pThis, PRTFSOBJINFO pObjInfo)
532{
533 /*
534 * Zap the whole structure, this takes care of unused space in the union.
535 */
536 RT_ZERO(*pObjInfo);
537 pObjInfo->cbObject = rtZipPkzipReaderUncompressed(pThis);
538 pObjInfo->cbAllocated = rtZipPkzipReaderUncompressed(pThis); /* XXX */
539 RTTIMESPEC ts;
540 rtZipPkzipReaderDecodeDosTime(&ts, pThis->cdh.u16LastModifiedTime, pThis->cdh.u16LastModifiedDate);
541 pObjInfo->ChangeTime = ts;
542 pObjInfo->ModificationTime = ts;
543 pObjInfo->AccessTime = ts;
544 pObjInfo->BirthTime = ts;
545 const char *pszEnd = strchr(pThis->szName, '\0');
546 if (pszEnd == &pThis->szName[0] || pszEnd[-1] != '/')
547 pObjInfo->Attr.fMode = RTFS_TYPE_FILE \
548 | RTFS_UNIX_IRUSR | RTFS_UNIX_IWUSR \
549 | RTFS_UNIX_IRGRP \
550 | RTFS_UNIX_IROTH;
551 else
552 pObjInfo->Attr.fMode = RTFS_TYPE_DIRECTORY \
553 | RTFS_UNIX_IRWXU \
554 | RTFS_UNIX_IRGRP | RTFS_UNIX_IXGRP \
555 | RTFS_UNIX_IROTH | RTFS_UNIX_IXOTH;
556 pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX;
557 pObjInfo->Attr.u.Unix.cHardlinks = 1;
558
559 return VINF_SUCCESS;
560}
561
562
563/**
564 * Search the magic value of the End Of Central Directory Record.
565 *
566 * @returns true if found, false otherwise.
567 * @param pu8Buf buffer.
568 * @param cb size of buffer.
569 * @param piPos where to store the position of the magic value.
570 */
571static bool rtZipPkzipReaderScanEocd(const uint8_t *pu8Buf, size_t cb, int *piPos)
572{
573 if (cb < 4)
574 return false;
575 ssize_t i;
576 for (i = (ssize_t)cb - 4; i >= 0; --i)
577 if (*(uint32_t*)(pu8Buf + i) == RTZIPPKZIPENDOFCENTRDIRREC_MAGIC)
578 {
579 *piPos = i;
580 return true;
581 }
582 return false;
583}
584
585
586/**
587 * Read the Local File Header. We ignore the content -- we trust the Central
588 * Directory.
589 */
590static int rtZipPkzipFssIosReadLfh(PRTZIPPKZIPFSSTREAM pThis, uint64_t *poffStartData)
591{
592 RTZIPPKZIPLOCALFILEHDR lfh;
593 uint64_t offLocalFileHeader = rtZipPkzipReaderOffLocalHeader(&pThis->PkzipReader);
594 int rc = RTVfsIoStrmReadAt(pThis->hVfsIos, offLocalFileHeader,
595 &lfh, sizeof(lfh) - 1, true /*fBlocking*/, NULL);
596 if (RT_SUCCESS(rc))
597 {
598 if (lfh.u32Magic == RTZIPPKZIPLOCALFILEHDR_MAGIC)
599 {
600 size_t cbExtra = 0;
601 rc = rtZipPkzipParseLocalFileHeader(&pThis->PkzipReader, &lfh, &cbExtra);
602 if (RT_SUCCESS(rc))
603 {
604 /* Just skip the file name and and extra field. We use the data
605 * from the Central Directory Header. */
606 rc = RTVfsIoStrmSkip(pThis->hVfsIos, cbExtra);
607 if (RT_SUCCESS(rc))
608 *poffStartData = offLocalFileHeader + sizeof(lfh) - 1 + cbExtra;
609 }
610 }
611 else
612 rc = VERR_PKZIP_BAD_LF_HEADER;
613 }
614
615 return rc;
616}
617
618
619/**
620 * Scan the current Central Directory Header.
621 */
622static int rtZipPkzipFssIosReadCdh(PRTZIPPKZIPFSSTREAM pThis, uint64_t *poffStartData,
623 RTZIPPKZIP_COMP_METHOD *penmCompMethod, uint64_t *pcbCompressed)
624{
625 int rc;
626
627 uint64_t offCd = pThis->offNextCdh - pThis->offFirstCdh;
628 if ( pThis->iCentrDirEntry < pThis->cCentrDirEntries
629 || offCd + sizeof(RTZIPPKZIPCENTRDIRHDR) - 1 <= pThis->cbCentrDir)
630 {
631 RTZIPPKZIPCENTRDIRHDR cdh;
632 rc = RTVfsIoStrmReadAt(pThis->hVfsIos, pThis->offNextCdh,
633 &cdh, sizeof(cdh) - 1, true /*fBlocking*/, NULL);
634 if (RT_SUCCESS(rc))
635 {
636 pThis->offNextCdh += sizeof(cdh) - 1;
637 pThis->iCentrDirEntry++;
638 size_t cbExtra = 0;
639 rc = rtZipPkzipParseCentrDirHeader(&pThis->PkzipReader, &cdh, &cbExtra);
640 if (RT_SUCCESS(rc))
641 {
642 if (offCd + sizeof(RTZIPPKZIPCENTRDIRHDR) - 1 + cbExtra <= pThis->cbCentrDir)
643 {
644 /* extra data up to 64k */
645 uint8_t *pu8Buf = (uint8_t*)RTMemTmpAlloc(cbExtra);
646 if (pu8Buf)
647 {
648 rc = RTVfsIoStrmRead(pThis->hVfsIos, pu8Buf, cbExtra, true /*fBlocking*/, NULL);
649 if (RT_SUCCESS(rc))
650 {
651 rc = rtZipPkzipParseCentrDirHeaderExtra(&pThis->PkzipReader, pu8Buf, cbExtra,
652 penmCompMethod, pcbCompressed);
653 if (RT_SUCCESS(rc))
654 rc = rtZipPkzipFssIosReadLfh(pThis, poffStartData);
655 }
656 pThis->offNextCdh += cbExtra;
657 RTMemTmpFree(pu8Buf);
658 }
659 else
660 rc = VERR_NO_MEMORY;
661 }
662 else
663 rc = VERR_EOF;
664 }
665 }
666 }
667 else
668 rc = VERR_EOF;
669
670 return rc;
671}
672
673
674/**
675 * Scan for the End of Central Directory Record. Of course this works not if
676 * the stream is non-seekable (i.e. a pipe).
677 */
678static int rtZipPkzipFssIosReadEocb(PRTZIPPKZIPFSSTREAM pThis)
679{
680 RTFSOBJINFO Info;
681 int rc = RTVfsIoStrmQueryInfo(pThis->hVfsIos, &Info, RTFSOBJATTRADD_UNIX);
682 if (RT_FAILURE(rc))
683 return rc;
684
685 uint64_t cbFile = Info.cbObject;
686 if (cbFile < sizeof(RTZIPPKZIPENDOFCENTRDIRREC)-1)
687 return VERR_PKZIP_NO_EOCB;
688
689 /* search for start of the 'end of Central Directory Record' */
690 size_t cbBuf = RT_MIN(_1K, cbFile);
691 uint8_t *pu8Buf = (uint8_t*)RTMemTmpAlloc(cbBuf);
692 if (!pu8Buf)
693 return VERR_NO_MEMORY;
694
695 /* maximum size of EOCD comment 2^16-1 */
696 const size_t cbHdrMax = 0xffff + sizeof(RTZIPPKZIPENDOFCENTRDIRREC) - 1;
697 uint64_t offMin = cbFile >= cbHdrMax ? cbFile - cbHdrMax : 0;
698
699 uint64_t off = cbFile - cbBuf;
700 while (off >= offMin)
701 {
702 rc = RTVfsIoStrmReadAt(pThis->hVfsIos, off, pu8Buf, cbBuf, true /*fBlocking*/, NULL);
703 if (RT_FAILURE(rc))
704 break;
705 int offMagic;
706 if (rtZipPkzipReaderScanEocd(pu8Buf, cbBuf, &offMagic))
707 {
708 off += offMagic;
709 RTZIPPKZIPENDOFCENTRDIRREC eocd;
710 rc = RTVfsIoStrmReadAt(pThis->hVfsIos, off, &eocd, sizeof(eocd) - 1,
711 true /*fBlocking*/, NULL);
712 if (RT_SUCCESS(rc))
713 {
714 /* well, this shouldn't fail if the content didn't change */
715 if (eocd.u32Magic == RTZIPPKZIPENDOFCENTRDIRREC_MAGIC)
716 {
717 /* sanity check */
718 if (off + RT_OFFSETOF(RTZIPPKZIPENDOFCENTRDIRREC, u8Comment) + eocd.cbComment == cbFile)
719 {
720 pThis->offFirstCdh = eocd.offCentrDir;
721 pThis->offNextCdh = eocd.offCentrDir;
722 pThis->iCentrDirEntry = 0;
723 pThis->cCentrDirEntries = eocd.cCentrDirRecords;
724 pThis->cbCentrDir = eocd.cbCentrDir;
725 pThis->PkzipReader.fHaveEocd = true;
726 }
727 else
728 rc = VERR_PKZIP_NO_EOCB;
729 }
730 else
731 rc = VERR_PKZIP_NO_EOCB;
732 }
733 if (rc != VERR_PKZIP_NO_EOCB)
734 break;
735 }
736 else
737 rc = VERR_PKZIP_NO_EOCB;
738 /* overlap the following read */
739 off -= cbBuf - 4;
740 }
741
742 RTMemTmpFree(pu8Buf);
743
744 /*
745 * Now check for the presence of the Zip64 End of Central Directory Locator.
746 */
747 if ( RT_SUCCESS(rc)
748 && off > (unsigned)sizeof(RTZIPPKZIP64ENDOFCENTRDIRLOC))
749 {
750 off -= sizeof(RTZIPPKZIP64ENDOFCENTRDIRLOC);
751
752 RTZIPPKZIP64ENDOFCENTRDIRLOC eocd64loc;
753 rc = RTVfsIoStrmReadAt(pThis->hVfsIos, off, &eocd64loc, sizeof(eocd64loc), true /*fBlocking*/, NULL);
754 if (RT_SUCCESS(rc))
755 {
756 if (eocd64loc.u32Magic == RTZIPPKZIP64ENDOFCENTRDIRLOC_MAGIC)
757 pThis->PkzipReader.fZip64Eocd = true;
758 }
759 }
760 return rc;
761}
762
763
764/**
765 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
766 */
767static DECLCALLBACK(int) rtZipPkzipFssBaseObj_Close(void *pvThis)
768{
769 PRTZIPPKZIPBASEOBJ pThis = (PRTZIPPKZIPBASEOBJ)pvThis;
770
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 RTVFSOBJOPS_VERSION
823};
824
825
826/**
827 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
828 */
829static DECLCALLBACK(int) rtZipPkzipFssIos_Close(void *pvThis)
830{
831 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
832
833 RTVfsIoStrmRelease(pThis->hVfsIos);
834 pThis->hVfsIos = NIL_RTVFSIOSTREAM;
835
836 if (pThis->pZip)
837 {
838 RTZipDecompDestroy(pThis->pZip);
839 pThis->pZip = NULL;
840 }
841
842 return rtZipPkzipFssBaseObj_Close(&pThis->BaseObj);
843}
844
845
846/**
847 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
848 */
849static DECLCALLBACK(int) rtZipPkzipFssIos_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
850{
851 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
852 return rtZipPkzipFssBaseObj_QueryInfo(&pThis->BaseObj, pObjInfo, enmAddAttr);
853}
854
855
856/**
857 * Callback function for rtZipPkzipFssIos_Read. For feeding compressed data
858 * into the decompressor function.
859 */
860static DECLCALLBACK(int) rtZipPkzipFssIosReadHelper(void *pvThis, void *pvBuf, size_t cbToRead, size_t *pcbRead)
861{
862 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
863 int rc = VINF_SUCCESS;
864 if (!cbToRead)
865 return rc;
866 if ( pThis->fPassZipType
867 && cbToRead > 0)
868 {
869 uint8_t *pu8Buf = (uint8_t*)pvBuf;
870 pu8Buf[0] = pThis->enmZipType;
871 pvBuf = &pu8Buf[1];
872 cbToRead--;
873 pThis->fPassZipType = false;
874 }
875 if (cbToRead > 0)
876 {
877 size_t cbRead = 0;
878 const size_t cbAvail = pThis->cbComp;
879 rc = RTVfsIoStrmReadAt(pThis->hVfsIos, pThis->offComp, pvBuf,
880 RT_MIN(cbToRead, cbAvail), true /*fBlocking*/, &cbRead);
881 if ( RT_SUCCESS(rc)
882 && cbToRead > cbAvail)
883 rc = pcbRead ? VINF_EOF : VERR_EOF;
884 if ( rc == VINF_EOF
885 && !pcbRead)
886 rc = VERR_EOF;
887 pThis->offComp += cbRead;
888 if (pcbRead)
889 *pcbRead = cbRead;
890 }
891 return rc;
892}
893
894
895/**
896 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
897 */
898static DECLCALLBACK(int) rtZipPkzipFssIos_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
899{
900 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
901 Assert(pSgBuf->cSegs == 1);
902
903 if (off < 0)
904 off = pThis->offFile;
905 if (off >= (RTFOFF)pThis->cbFile)
906 return pcbRead ? VINF_EOF : VERR_EOF;
907
908 Assert(pThis->cbFile >= pThis->offFile);
909 uint64_t cbLeft = (uint64_t)(pThis->cbFile - pThis->offFile);
910 size_t cbToRead = pSgBuf->paSegs[0].cbSeg;
911 if (cbToRead > cbLeft)
912 {
913 if (!pcbRead)
914 return VERR_EOF;
915 cbToRead = (size_t)cbLeft;
916 }
917
918 /*
919 * Restart decompression at start of stream or on backward seeking.
920 */
921 if ( !pThis->pZip
922 || !off
923 || off < (RTFOFF)pThis->offFile)
924 {
925 switch (pThis->enmCompMethod)
926 {
927 case RTZIPPKZIP_COMP_METHOD_STORED:
928 pThis->enmZipType = RTZIPTYPE_STORE;
929 break;
930
931 case RTZIPPKZIP_COMP_METHOD_DEFLATED:
932 pThis->enmZipType = RTZIPTYPE_ZLIB_NO_HEADER;
933 break;
934
935 default:
936 pThis->enmZipType = RTZIPTYPE_INVALID;
937 break;
938 }
939
940 if (pThis->pZip)
941 {
942 RTZipDecompDestroy(pThis->pZip);
943 pThis->pZip = NULL;
944 }
945 int rc = RTZipDecompCreate(&pThis->pZip, (void*)pThis, rtZipPkzipFssIosReadHelper);
946 if (RT_FAILURE(rc))
947 return rc;
948 }
949
950 /*
951 * Skip bytes if necessary.
952 */
953 if (off > (RTFOFF)pThis->offFile)
954 {
955 uint8_t u8Buf[_1K];
956 while (off > (RTFOFF)pThis->offFile)
957 {
958 size_t cbSkip = off - pThis->offFile;
959 if (cbSkip > sizeof(u8Buf))
960 cbSkip = sizeof(u8Buf);
961 int rc = RTZipDecompress(pThis->pZip, u8Buf, cbSkip, NULL);
962 if (RT_FAILURE(rc))
963 return rc;
964 pThis->offFile += cbSkip;
965 }
966 }
967
968 /*
969 * Do the actual reading.
970 */
971 size_t cbReadStack = 0;
972 if (!pcbRead)
973 pcbRead = &cbReadStack;
974 int rc = RTZipDecompress(pThis->pZip, pSgBuf->paSegs[0].pvSeg, cbToRead, pcbRead);
975 pThis->offFile = off + *pcbRead;
976 if (pThis->offFile >= pThis->cbFile)
977 {
978 Assert(pThis->offFile == pThis->cbFile);
979 pThis->fEndOfStream = true;
980 }
981
982 return rc;
983}
984
985static DECLCALLBACK(int) rtZipPkzipFssIos_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcWritten)
986{
987 return VERR_NOT_IMPLEMENTED;
988}
989
990static DECLCALLBACK(int) rtZipPkzipFssIos_Flush(void *pvThis)
991{
992 return VERR_NOT_IMPLEMENTED;
993}
994
995/**
996 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
997 */
998static DECLCALLBACK(int) rtZipPkzipFssIos_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies,
999 bool fIntr, uint32_t *pfRetEvents)
1000{
1001 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
1002
1003 /* When we've reached the end, read will be set to indicate it. */
1004 if ( (fEvents & RTPOLL_EVT_READ)
1005 && pThis->fEndOfStream)
1006 {
1007 int rc = RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, 0, fIntr, pfRetEvents);
1008 if (RT_SUCCESS(rc))
1009 *pfRetEvents |= RTPOLL_EVT_READ;
1010 else
1011 *pfRetEvents = RTPOLL_EVT_READ;
1012 return VINF_SUCCESS;
1013 }
1014
1015 return RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, cMillies, fIntr, pfRetEvents);
1016}
1017
1018
1019/**
1020 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
1021 */
1022static DECLCALLBACK(int) rtZipPkzipFssIos_Tell(void *pvThis, PRTFOFF poffActual)
1023{
1024 PRTZIPPKZIPIOSTREAM pThis = (PRTZIPPKZIPIOSTREAM)pvThis;
1025 *poffActual = pThis->offFile;
1026 return VINF_SUCCESS;
1027}
1028
1029
1030/**
1031 * Pkzip I/O object stream operations.
1032 */
1033static const RTVFSIOSTREAMOPS g_rtZipPkzipFssIosOps =
1034{
1035 { /* Obj */
1036 RTVFSOBJOPS_VERSION,
1037 RTVFSOBJTYPE_IO_STREAM,
1038 "PkzipFsStream::IoStream",
1039 rtZipPkzipFssIos_Close,
1040 rtZipPkzipFssIos_QueryInfo,
1041 RTVFSOBJOPS_VERSION
1042 },
1043 RTVFSIOSTREAMOPS_VERSION,
1044 RTVFSIOSTREAMOPS_FEAT_NO_SG,
1045 rtZipPkzipFssIos_Read,
1046 rtZipPkzipFssIos_Write,
1047 rtZipPkzipFssIos_Flush,
1048 rtZipPkzipFssIos_PollOne,
1049 rtZipPkzipFssIos_Tell,
1050 NULL /*Skip*/,
1051 NULL /*ZeroFill*/,
1052 RTVFSIOSTREAMOPS_VERSION
1053};
1054
1055/**
1056 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
1057 */
1058static DECLCALLBACK(int) rtZipPkzipFss_Close(void *pvThis)
1059{
1060 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1061
1062 RTVfsObjRelease(pThis->hVfsCurObj);
1063 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1064 pThis->pCurIosData = NULL;
1065
1066 RTVfsIoStrmRelease(pThis->hVfsIos);
1067 pThis->hVfsIos = NIL_RTVFSIOSTREAM;
1068
1069 return VINF_SUCCESS;
1070}
1071
1072
1073/**
1074 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
1075 */
1076static DECLCALLBACK(int) rtZipPkzipFss_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
1077{
1078 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1079 /* Take the lazy approach here, with the sideffect of providing some info
1080 that is actually kind of useful. */
1081 return RTVfsIoStrmQueryInfo(pThis->hVfsIos, pObjInfo, enmAddAttr);
1082}
1083
1084
1085/**
1086 * @interface_method_impl{RTVFSFSSTREAMOPS,pfnNext}
1087 */
1088static DECLCALLBACK(int) rtZipPkzipFss_Next(void *pvThis, char **ppszName, RTVFSOBJTYPE *penmType, PRTVFSOBJ phVfsObj)
1089{
1090 PRTZIPPKZIPFSSTREAM pThis = (PRTZIPPKZIPFSSTREAM)pvThis;
1091
1092 /*
1093 * Dispense with the current object.
1094 */
1095 if (pThis->hVfsCurObj != NIL_RTVFSOBJ)
1096 {
1097 if (pThis->pCurIosData)
1098 {
1099 pThis->pCurIosData->fEndOfStream = true;
1100 pThis->pCurIosData->offFile = pThis->pCurIosData->cbFile;
1101 pThis->pCurIosData = NULL;
1102 }
1103
1104 RTVfsObjRelease(pThis->hVfsCurObj);
1105 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1106 }
1107
1108 /*
1109 * Check if we've already reached the end in some way.
1110 */
1111 if (pThis->fEndOfStream)
1112 return VERR_EOF;
1113 if (pThis->rcFatal != VINF_SUCCESS)
1114 return pThis->rcFatal;
1115
1116 int rc = VINF_SUCCESS;
1117
1118 /*
1119 * Read the end of Central Directory Record once.
1120 */
1121 if (!pThis->PkzipReader.fHaveEocd)
1122 rc = rtZipPkzipFssIosReadEocb(pThis);
1123 uint64_t offData = 0;
1124
1125 /*
1126 * Parse the current Central Directory Header.
1127 */
1128 RTZIPPKZIP_COMP_METHOD enmCompMethod = RTZIPPKZIP_COMP_METHOD_STORED;
1129 uint64_t cbCompressed = 0;
1130 if (RT_SUCCESS(rc))
1131 rc = rtZipPkzipFssIosReadCdh(pThis, &offData, &enmCompMethod, &cbCompressed);
1132 if (RT_FAILURE(rc))
1133 return pThis->rcFatal = rc;
1134
1135 /*
1136 * Fill an object info structure from the current Pkzip state.
1137 */
1138 RTFSOBJINFO Info;
1139 rc = rtZipPkzipReaderGetFsObjInfo(&pThis->PkzipReader, &Info);
1140 if (RT_FAILURE(rc))
1141 return pThis->rcFatal = rc;
1142
1143 /*
1144 * Create an object of the appropriate type.
1145 */
1146 RTVFSOBJTYPE enmType;
1147 RTVFSOBJ hVfsObj;
1148 RTFMODE fType = Info.Attr.fMode & RTFS_TYPE_MASK;
1149 switch (fType)
1150 {
1151 case RTFS_TYPE_FILE:
1152 RTVFSIOSTREAM hVfsIos;
1153 PRTZIPPKZIPIOSTREAM pIosData;
1154 rc = RTVfsNewIoStream(&g_rtZipPkzipFssIosOps,
1155 sizeof(*pIosData),
1156 RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN,
1157 NIL_RTVFS,
1158 NIL_RTVFSLOCK,
1159 &hVfsIos,
1160 (void **)&pIosData);
1161 if (RT_FAILURE(rc))
1162 return pThis->rcFatal = rc;
1163
1164 pIosData->BaseObj.pPkzipReader = &pThis->PkzipReader;
1165 pIosData->BaseObj.ObjInfo = Info;
1166 pIosData->cbFile = Info.cbObject;
1167 pIosData->offFile = 0;
1168 pIosData->offComp = offData;
1169 pIosData->offCompStart = offData;
1170 pIosData->cbComp = cbCompressed;
1171 pIosData->enmCompMethod = enmCompMethod;
1172 pIosData->fPassZipType = true;
1173 pIosData->hVfsIos = pThis->hVfsIos;
1174 RTVfsIoStrmRetain(pThis->hVfsIos);
1175 pThis->pCurIosData = pIosData;
1176 enmType = RTVFSOBJTYPE_IO_STREAM;
1177 hVfsObj = RTVfsObjFromIoStream(hVfsIos);
1178 RTVfsIoStrmRelease(hVfsIos);
1179 break;
1180
1181 case RTFS_TYPE_DIRECTORY:
1182 PRTZIPPKZIPBASEOBJ pBaseObjData;
1183 rc = RTVfsNewBaseObj(&g_rtZipPkzipFssBaseObjOps,
1184 sizeof(*pBaseObjData),
1185 NIL_RTVFS,
1186 NIL_RTVFSLOCK,
1187 &hVfsObj,
1188 (void **)&pBaseObjData);
1189 if (RT_FAILURE(rc))
1190 return pThis->rcFatal = rc;
1191
1192 pBaseObjData->pPkzipReader = &pThis->PkzipReader;
1193 pBaseObjData->ObjInfo = Info;
1194 enmType = RTVFSOBJTYPE_BASE;
1195 break;
1196
1197 default:
1198 return pThis->rcFatal = VERR_PKZIP_UNKNOWN_TYPE_FLAG;
1199 }
1200 pThis->hVfsCurObj = hVfsObj;
1201
1202 if (ppszName)
1203 {
1204 rc = RTStrDupEx(ppszName, pThis->PkzipReader.szName);
1205 if (RT_FAILURE(rc))
1206 return pThis->rcFatal = rc;
1207 }
1208
1209 if (phVfsObj)
1210 {
1211 RTVfsObjRetain(hVfsObj);
1212 *phVfsObj = hVfsObj;
1213 }
1214
1215 if (penmType)
1216 *penmType = enmType;
1217
1218 return VINF_SUCCESS;
1219}
1220
1221
1222/**
1223 * PKZip filesystem stream operations.
1224 */
1225static const RTVFSFSSTREAMOPS rtZipPkzipFssOps =
1226{
1227 { /* Obj */
1228 RTVFSOBJOPS_VERSION,
1229 RTVFSOBJTYPE_FS_STREAM,
1230 "PkzipFsStream",
1231 rtZipPkzipFss_Close,
1232 rtZipPkzipFss_QueryInfo,
1233 RTVFSOBJOPS_VERSION
1234 },
1235 RTVFSFSSTREAMOPS_VERSION,
1236 0,
1237 rtZipPkzipFss_Next,
1238 RTVFSFSSTREAMOPS_VERSION
1239};
1240
1241
1242RTDECL(int) RTZipPkzipFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss)
1243{
1244 /*
1245 * Input validation.
1246 */
1247 AssertPtrReturn(phVfsFss, VERR_INVALID_HANDLE);
1248 *phVfsFss = NIL_RTVFSFSSTREAM;
1249 AssertPtrReturn(hVfsIosIn, VERR_INVALID_HANDLE);
1250 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
1251
1252 uint32_t cRefs = RTVfsIoStrmRetain(hVfsIosIn);
1253 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
1254
1255 /*
1256 * Retain the input stream and create a new filesystem stream handle.
1257 */
1258 PRTZIPPKZIPFSSTREAM pThis;
1259 RTVFSFSSTREAM hVfsFss;
1260 int rc = RTVfsNewFsStream(&rtZipPkzipFssOps, sizeof(*pThis),
1261 NIL_RTVFS, NIL_RTVFSLOCK, &hVfsFss, (void **)&pThis);
1262 if (RT_SUCCESS(rc))
1263 {
1264 pThis->hVfsIos = hVfsIosIn;
1265 pThis->hVfsCurObj = NIL_RTVFSOBJ;
1266 pThis->pCurIosData = NULL;
1267 pThis->fEndOfStream = false;
1268 pThis->rcFatal = VINF_SUCCESS;
1269 pThis->fHaveEocd = false;
1270
1271 *phVfsFss = hVfsFss;
1272 return VINF_SUCCESS;
1273 }
1274
1275 RTVfsIoStrmRelease(hVfsIosIn);
1276 return rc;
1277}
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