VirtualBox

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

Last change on this file since 62508 was 62477, checked in by vboxsync, 9 years ago

(C) 2016

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