VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/zip/gzipvfs.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: 34.2 KB
Line 
1/* $Id: gzipvfs.cpp 94291 2022-03-17 13:29:52Z vboxsync $ */
2/** @file
3 * IPRT - GZIP Compressor and Decompressor I/O Stream.
4 */
5
6/*
7 * Copyright (C) 2010-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* Defined Constants And Macros *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/zip.h>
33
34#include <iprt/assert.h>
35#include <iprt/ctype.h>
36#include <iprt/file.h>
37#include <iprt/err.h>
38#include <iprt/poll.h>
39#include <iprt/string.h>
40#include <iprt/vfslowlevel.h>
41
42#include <zlib.h>
43
44#if defined(RT_OS_OS2) || defined(RT_OS_SOLARIS) || defined(RT_OS_WINDOWS)
45/**
46 * Drag in the missing zlib symbols.
47 */
48PFNRT g_apfnRTZlibDeps[] =
49{
50 (PFNRT)gzrewind,
51 (PFNRT)gzread,
52 (PFNRT)gzopen,
53 (PFNRT)gzwrite,
54 (PFNRT)gzclose,
55 (PFNRT)gzdopen,
56 NULL
57};
58#endif /* RT_OS_OS2 || RT_OS_SOLARIS || RT_OS_WINDOWS */
59
60
61/*********************************************************************************************************************************
62* Structures and Typedefs *
63*********************************************************************************************************************************/
64#pragma pack(1)
65typedef struct RTZIPGZIPHDR
66{
67 /** RTZIPGZIPHDR_ID1. */
68 uint8_t bId1;
69 /** RTZIPGZIPHDR_ID2. */
70 uint8_t bId2;
71 /** CM - The compression method. */
72 uint8_t bCompressionMethod;
73 /** FLG - Flags. */
74 uint8_t fFlags;
75 /** Modification time of the source file or the timestamp at the time the
76 * compression took place. Can also be zero. Is the number of seconds since
77 * unix epoch. */
78 uint32_t u32ModTime;
79 /** Flags specific to the compression method. */
80 uint8_t bXtraFlags;
81 /** An ID indicating which OS or FS gzip ran on. */
82 uint8_t bOS;
83} RTZIPGZIPHDR;
84#pragma pack()
85AssertCompileSize(RTZIPGZIPHDR, 10);
86/** Pointer to a const gzip header. */
87typedef RTZIPGZIPHDR const *PCRTZIPGZIPHDR;
88
89/** gzip header identification no 1. */
90#define RTZIPGZIPHDR_ID1 0x1f
91/** gzip header identification no 2. */
92#define RTZIPGZIPHDR_ID2 0x8b
93/** gzip deflate compression method. */
94#define RTZIPGZIPHDR_CM_DEFLATE 8
95
96/** @name gzip header flags
97 * @{ */
98/** Probably a text file */
99#define RTZIPGZIPHDR_FLG_TEXT UINT8_C(0x01)
100/** Header CRC present (crc32 of header cast to uint16_t). */
101#define RTZIPGZIPHDR_FLG_HDR_CRC UINT8_C(0x02)
102/** Length prefixed xtra field is present. */
103#define RTZIPGZIPHDR_FLG_EXTRA UINT8_C(0x04)
104/** A name field is present (latin-1). */
105#define RTZIPGZIPHDR_FLG_NAME UINT8_C(0x08)
106/** A comment field is present (latin-1). */
107#define RTZIPGZIPHDR_FLG_COMMENT UINT8_C(0x10)
108/** Mask of valid flags. */
109#define RTZIPGZIPHDR_FLG_VALID_MASK UINT8_C(0x1f)
110/** @} */
111
112/** @name gzip default xtra flag values
113 * @{ */
114#define RTZIPGZIPHDR_XFL_DEFLATE_MAX UINT8_C(0x02)
115#define RTZIPGZIPHDR_XFL_DEFLATE_FASTEST UINT8_C(0x04)
116/** @} */
117
118/** @name Operating system / Filesystem IDs
119 * @{ */
120#define RTZIPGZIPHDR_OS_FAT UINT8_C(0x00)
121#define RTZIPGZIPHDR_OS_AMIGA UINT8_C(0x01)
122#define RTZIPGZIPHDR_OS_VMS UINT8_C(0x02)
123#define RTZIPGZIPHDR_OS_UNIX UINT8_C(0x03)
124#define RTZIPGZIPHDR_OS_VM_CMS UINT8_C(0x04)
125#define RTZIPGZIPHDR_OS_ATARIS_TOS UINT8_C(0x05)
126#define RTZIPGZIPHDR_OS_HPFS UINT8_C(0x06)
127#define RTZIPGZIPHDR_OS_MACINTOSH UINT8_C(0x07)
128#define RTZIPGZIPHDR_OS_Z_SYSTEM UINT8_C(0x08)
129#define RTZIPGZIPHDR_OS_CPM UINT8_C(0x09)
130#define RTZIPGZIPHDR_OS_TOPS_20 UINT8_C(0x0a)
131#define RTZIPGZIPHDR_OS_NTFS UINT8_C(0x0b)
132#define RTZIPGZIPHDR_OS_QDOS UINT8_C(0x0c)
133#define RTZIPGZIPHDR_OS_ACORN_RISCOS UINT8_C(0x0d)
134#define RTZIPGZIPHDR_OS_UNKNOWN UINT8_C(0xff)
135/** @} */
136
137
138/**
139 * The internal data of a GZIP I/O stream.
140 */
141typedef struct RTZIPGZIPSTREAM
142{
143 /** The stream we're reading or writing the compressed data from or to. */
144 RTVFSIOSTREAM hVfsIos;
145 /** Set if it's a decompressor, clear if it's a compressor. */
146 bool fDecompress;
147 /** Set if zlib reported a fatal error. */
148 bool fFatalError;
149 /** Set if we've reached the end of the zlib stream. */
150 bool fEndOfStream;
151 /** The stream offset for pfnTell, always the uncompressed data. */
152 RTFOFF offStream;
153 /** The zlib stream. */
154 z_stream Zlib;
155 /** The data buffer. */
156 uint8_t abBuffer[_64K];
157 /** Scatter gather segment describing abBuffer. */
158 RTSGSEG SgSeg;
159 /** Scatter gather buffer describing abBuffer. */
160 RTSGBUF SgBuf;
161 /** The original file name (decompressor only). */
162 char *pszOrgName;
163 /** The comment (decompressor only). */
164 char *pszComment;
165 /** The gzip header. */
166 RTZIPGZIPHDR Hdr;
167} RTZIPGZIPSTREAM;
168/** Pointer to a the internal data of a GZIP I/O stream. */
169typedef RTZIPGZIPSTREAM *PRTZIPGZIPSTREAM;
170
171
172/*********************************************************************************************************************************
173* Internal Functions *
174*********************************************************************************************************************************/
175static int rtZipGzip_FlushIt(PRTZIPGZIPSTREAM pThis, uint8_t fFlushType);
176
177
178/**
179 * Convert from zlib to IPRT status codes.
180 *
181 * This will also set the fFatalError flag when appropriate.
182 *
183 * @returns IPRT status code.
184 * @param pThis The gzip I/O stream instance data.
185 * @param rc Zlib error code.
186 */
187static int rtZipGzipConvertErrFromZlib(PRTZIPGZIPSTREAM pThis, int rc)
188{
189 switch (rc)
190 {
191 case Z_OK:
192 return VINF_SUCCESS;
193
194 case Z_BUF_ERROR:
195 /* This isn't fatal. */
196 return VINF_SUCCESS; /** @todo The code in zip.cpp treats Z_BUF_ERROR as fatal... */
197
198 case Z_STREAM_ERROR:
199 pThis->fFatalError = true;
200 return VERR_ZIP_CORRUPTED;
201
202 case Z_DATA_ERROR:
203 pThis->fFatalError = true;
204 return pThis->fDecompress ? VERR_ZIP_CORRUPTED : VERR_ZIP_ERROR;
205
206 case Z_MEM_ERROR:
207 pThis->fFatalError = true;
208 return VERR_ZIP_NO_MEMORY;
209
210 case Z_VERSION_ERROR:
211 pThis->fFatalError = true;
212 return VERR_ZIP_UNSUPPORTED_VERSION;
213
214 case Z_ERRNO: /* We shouldn't see this status! */
215 default:
216 AssertMsgFailed(("%d\n", rc));
217 if (rc >= 0)
218 return VINF_SUCCESS;
219 pThis->fFatalError = true;
220 return VERR_ZIP_ERROR;
221 }
222}
223
224
225/**
226 * @interface_method_impl{RTVFSOBJOPS,pfnClose}
227 */
228static DECLCALLBACK(int) rtZipGzip_Close(void *pvThis)
229{
230 PRTZIPGZIPSTREAM pThis = (PRTZIPGZIPSTREAM)pvThis;
231
232 int rc;
233 if (pThis->fDecompress)
234 {
235 rc = inflateEnd(&pThis->Zlib);
236 if (rc != Z_OK)
237 rc = rtZipGzipConvertErrFromZlib(pThis, rc);
238 }
239 else
240 {
241 /* Flush the compression stream before terminating it. */
242 rc = VINF_SUCCESS;
243 if (!pThis->fFatalError)
244 rc = rtZipGzip_FlushIt(pThis, Z_FINISH);
245
246 int rc2 = deflateEnd(&pThis->Zlib);
247 if (RT_SUCCESS(rc) && rc2 != Z_OK)
248 rc = rtZipGzipConvertErrFromZlib(pThis, rc);
249 }
250
251 RTVfsIoStrmRelease(pThis->hVfsIos);
252 pThis->hVfsIos = NIL_RTVFSIOSTREAM;
253 RTStrFree(pThis->pszOrgName);
254 pThis->pszOrgName = NULL;
255 RTStrFree(pThis->pszComment);
256 pThis->pszComment = NULL;
257
258 return rc;
259}
260
261
262/**
263 * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
264 */
265static DECLCALLBACK(int) rtZipGzip_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
266{
267 PRTZIPGZIPSTREAM pThis = (PRTZIPGZIPSTREAM)pvThis;
268 return RTVfsIoStrmQueryInfo(pThis->hVfsIos, pObjInfo, enmAddAttr);
269}
270
271
272/**
273 * Reads one segment.
274 *
275 * @returns IPRT status code.
276 * @param pThis The gzip I/O stream instance data.
277 * @param pvBuf Where to put the read bytes.
278 * @param cbToRead The number of bytes to read.
279 * @param fBlocking Whether to block or not.
280 * @param pcbRead Where to store the number of bytes actually read.
281 */
282static int rtZipGzip_ReadOneSeg(PRTZIPGZIPSTREAM pThis, void *pvBuf, size_t cbToRead, bool fBlocking, size_t *pcbRead)
283{
284 /*
285 * This simplifies life a wee bit below.
286 */
287 if (pThis->fEndOfStream)
288 return pcbRead ? VINF_EOF : VERR_EOF;
289
290 /*
291 * Set up the output buffer.
292 */
293 pThis->Zlib.next_out = (Bytef *)pvBuf;
294 pThis->Zlib.avail_out = (uInt)cbToRead;
295 AssertReturn(pThis->Zlib.avail_out == cbToRead, VERR_OUT_OF_RANGE);
296
297 /*
298 * Be greedy reading input, even if no output buffer is left. It's possible
299 * that it's just the end of stream marker which needs to be read. Happens
300 * for incompressible blocks just larger than the input buffer size.
301 */
302 int rc = VINF_SUCCESS;
303 while ( pThis->Zlib.avail_out > 0
304 || pThis->Zlib.avail_in == 0 /* greedy */)
305 {
306 /*
307 * Read more input?
308 *
309 * N.B. The assertions here validate the RTVfsIoStrmSgRead behavior
310 * since the API is new and untested. They could be removed later
311 * but, better leaving them in.
312 */
313 if (pThis->Zlib.avail_in == 0)
314 {
315 size_t cbReadIn = ~(size_t)0;
316 rc = RTVfsIoStrmSgRead(pThis->hVfsIos, -1 /*off*/, &pThis->SgBuf, fBlocking, &cbReadIn);
317 if (rc != VINF_SUCCESS)
318 {
319 AssertMsg(RT_FAILURE(rc) || rc == VINF_TRY_AGAIN || rc == VINF_EOF, ("%Rrc\n", rc));
320 if (rc == VERR_INTERRUPTED)
321 {
322 Assert(cbReadIn == 0);
323 continue;
324 }
325 if (RT_FAILURE(rc) || rc == VINF_TRY_AGAIN || cbReadIn == 0)
326 {
327 Assert(cbReadIn == 0);
328 break;
329 }
330 AssertMsg(rc == VINF_EOF, ("%Rrc\n", rc));
331 }
332 AssertMsgBreakStmt(cbReadIn > 0 && cbReadIn <= sizeof(pThis->abBuffer), ("%zu %Rrc\n", cbReadIn, rc),
333 rc = VERR_INTERNAL_ERROR_4);
334
335 pThis->Zlib.avail_in = (uInt)cbReadIn;
336 pThis->Zlib.next_in = &pThis->abBuffer[0];
337 }
338
339 /*
340 * Pass it on to zlib.
341 */
342 rc = inflate(&pThis->Zlib, Z_NO_FLUSH);
343 if (rc != Z_OK && rc != Z_BUF_ERROR)
344 {
345 if (rc == Z_STREAM_END)
346 {
347 pThis->fEndOfStream = true;
348 if (pThis->Zlib.avail_out == 0)
349 rc = VINF_SUCCESS;
350 else
351 rc = pcbRead ? VINF_EOF : VERR_EOF;
352 }
353 else
354 rc = rtZipGzipConvertErrFromZlib(pThis, rc);
355 break;
356 }
357 rc = VINF_SUCCESS;
358 }
359
360 /*
361 * Update the read counters before returning.
362 */
363 size_t const cbRead = cbToRead - pThis->Zlib.avail_out;
364 pThis->offStream += cbRead;
365 if (pcbRead)
366 *pcbRead = cbRead;
367
368 return rc;
369}
370
371
372/**
373 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
374 */
375static DECLCALLBACK(int) rtZipGzip_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
376{
377 PRTZIPGZIPSTREAM pThis = (PRTZIPGZIPSTREAM)pvThis;
378
379 Assert(pSgBuf->cSegs == 1);
380 if (!pThis->fDecompress)
381 return VERR_ACCESS_DENIED;
382 AssertReturn(off == -1 || off == pThis->offStream , VERR_INVALID_PARAMETER);
383
384 return rtZipGzip_ReadOneSeg(pThis, pSgBuf->paSegs[0].pvSeg, pSgBuf->paSegs[0].cbSeg, fBlocking, pcbRead);
385}
386
387
388/**
389 * Internal helper for rtZipGzip_Write, rtZipGzip_Flush and rtZipGzip_Close.
390 *
391 * @returns IPRT status code.
392 * @retval VINF_SUCCESS
393 * @retval VINF_TRY_AGAIN - the only informational status.
394 * @retval VERR_INTERRUPTED - call again.
395 *
396 * @param pThis The gzip I/O stream instance data.
397 * @param fBlocking Whether to block or not.
398 */
399static int rtZipGzip_WriteOutputBuffer(PRTZIPGZIPSTREAM pThis, bool fBlocking)
400{
401 /*
402 * Anything to write? No, then just return immediately.
403 */
404 size_t cbToWrite = sizeof(pThis->abBuffer) - pThis->Zlib.avail_out;
405 if (cbToWrite == 0)
406 {
407 Assert(pThis->Zlib.next_out == &pThis->abBuffer[0]);
408 return VINF_SUCCESS;
409 }
410 Assert(cbToWrite <= sizeof(pThis->abBuffer));
411
412 /*
413 * Loop write on VERR_INTERRUPTED.
414 *
415 * Note! Asserting a bit extra here to make sure the
416 * RTVfsIoStrmSgWrite works correctly.
417 */
418 int rc;
419 size_t cbWrittenOut;
420 for (;;)
421 {
422 /* Set up the buffer. */
423 pThis->SgSeg.cbSeg = cbToWrite;
424 Assert(pThis->SgSeg.pvSeg == &pThis->abBuffer[0]);
425 RTSgBufReset(&pThis->SgBuf);
426
427 cbWrittenOut = ~(size_t)0;
428 rc = RTVfsIoStrmSgWrite(pThis->hVfsIos, -1 /*off*/, &pThis->SgBuf, fBlocking, &cbWrittenOut);
429 if (rc != VINF_SUCCESS)
430 {
431 AssertMsg(RT_FAILURE(rc) || rc == VINF_TRY_AGAIN, ("%Rrc\n", rc));
432 if (rc == VERR_INTERRUPTED)
433 {
434 Assert(cbWrittenOut == 0);
435 continue;
436 }
437 if (RT_FAILURE(rc) || rc == VINF_TRY_AGAIN || cbWrittenOut == 0)
438 {
439 AssertReturn(cbWrittenOut == 0, VERR_INTERNAL_ERROR_3);
440 AssertReturn(rc != VINF_SUCCESS, VERR_IPE_UNEXPECTED_INFO_STATUS);
441 return rc;
442 }
443 }
444 break;
445 }
446 AssertMsgReturn(cbWrittenOut > 0 && cbWrittenOut <= sizeof(pThis->abBuffer),
447 ("%zu %Rrc\n", cbWrittenOut, rc),
448 VERR_INTERNAL_ERROR_4);
449
450 /*
451 * Adjust the Zlib output buffer members.
452 */
453 if (cbWrittenOut == pThis->SgBuf.paSegs[0].cbSeg)
454 {
455 pThis->Zlib.avail_out = sizeof(pThis->abBuffer);
456 pThis->Zlib.next_out = &pThis->abBuffer[0];
457 }
458 else
459 {
460 Assert(cbWrittenOut <= pThis->SgBuf.paSegs[0].cbSeg);
461 size_t cbLeft = pThis->SgBuf.paSegs[0].cbSeg - cbWrittenOut;
462 memmove(&pThis->abBuffer[0], &pThis->abBuffer[cbWrittenOut], cbLeft);
463 pThis->Zlib.avail_out += (uInt)cbWrittenOut;
464 pThis->Zlib.next_out = &pThis->abBuffer[cbWrittenOut];
465 }
466
467 return VINF_SUCCESS;
468}
469
470
471/**
472 * Processes all available input.
473 *
474 * @returns IPRT status code.
475 *
476 * @param pThis The gzip I/O stream instance data.
477 * @param fBlocking Whether to block or not.
478 */
479static int rtZipGzip_CompressIt(PRTZIPGZIPSTREAM pThis, bool fBlocking)
480{
481 /*
482 * Processes all the intput currently lined up for us.
483 */
484 while (pThis->Zlib.avail_in > 0)
485 {
486 /* Make sure there is some space in the output buffer before calling
487 deflate() so we don't waste time filling up the corners. */
488 static const size_t s_cbFlushThreshold = 4096;
489 AssertCompile(sizeof(pThis->abBuffer) >= s_cbFlushThreshold * 4);
490 if (pThis->Zlib.avail_out < s_cbFlushThreshold)
491 {
492 int rc = rtZipGzip_WriteOutputBuffer(pThis, fBlocking);
493 if (rc != VINF_SUCCESS)
494 return rc;
495 Assert(pThis->Zlib.avail_out >= s_cbFlushThreshold);
496 }
497
498 int rcZlib = deflate(&pThis->Zlib, Z_NO_FLUSH);
499 if (rcZlib != Z_OK)
500 return rtZipGzipConvertErrFromZlib(pThis, rcZlib);
501 }
502 return VINF_SUCCESS;
503}
504
505
506/**
507 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
508 */
509static DECLCALLBACK(int) rtZipGzip_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
510{
511 PRTZIPGZIPSTREAM pThis = (PRTZIPGZIPSTREAM)pvThis;
512
513 Assert(pSgBuf->cSegs == 1); NOREF(fBlocking);
514 if (pThis->fDecompress)
515 return VERR_ACCESS_DENIED;
516 AssertReturn(off == -1 || off == pThis->offStream , VERR_INVALID_PARAMETER);
517
518 /*
519 * Write out the input buffer. Using a loop here because of potential
520 * integer type overflow since avail_in is uInt and cbSeg is size_t.
521 */
522 int rc = VINF_SUCCESS;
523 size_t cbWritten = 0;
524 uint8_t const *pbSrc = (uint8_t const *)pSgBuf->paSegs[0].pvSeg;
525 size_t cbLeft = pSgBuf->paSegs[0].cbSeg;
526 if (cbLeft > 0)
527 for (;;)
528 {
529 size_t cbThis = cbLeft < ~(uInt)0 ? cbLeft : ~(uInt)0 / 2;
530 pThis->Zlib.next_in = (Bytef * )pbSrc;
531 pThis->Zlib.avail_in = (uInt)cbThis;
532 rc = rtZipGzip_CompressIt(pThis, fBlocking);
533
534 Assert(cbThis >= pThis->Zlib.avail_in);
535 cbThis -= pThis->Zlib.avail_in;
536 cbWritten += cbThis;
537 if (cbLeft == cbThis || rc != VINF_SUCCESS)
538 break;
539 pbSrc += cbThis;
540 cbLeft -= cbThis;
541 }
542
543 pThis->offStream += cbWritten;
544 if (pcbWritten)
545 *pcbWritten = cbWritten;
546 return rc;
547}
548
549
550/**
551 * Processes all available input.
552 *
553 * @returns IPRT status code.
554 *
555 * @param pThis The gzip I/O stream instance data.
556 * @param fFlushType The flush type to pass to deflate().
557 */
558static int rtZipGzip_FlushIt(PRTZIPGZIPSTREAM pThis, uint8_t fFlushType)
559{
560 /*
561 * Tell Zlib to flush until it stops producing more output.
562 */
563 int rc;
564 bool fMaybeMore = true;
565 for (;;)
566 {
567 /* Write the entire output buffer. */
568 do
569 {
570 rc = rtZipGzip_WriteOutputBuffer(pThis, true /*fBlocking*/);
571 if (RT_FAILURE(rc))
572 return rc;
573 Assert(rc == VINF_SUCCESS);
574 } while (pThis->Zlib.avail_out < sizeof(pThis->abBuffer));
575
576 if (!fMaybeMore)
577 return VINF_SUCCESS;
578
579 /* Do the flushing. */
580 pThis->Zlib.next_in = NULL;
581 pThis->Zlib.avail_in = 0;
582 int rcZlib = deflate(&pThis->Zlib, fFlushType);
583 if (rcZlib == Z_OK)
584 fMaybeMore = pThis->Zlib.avail_out < 64 || fFlushType == Z_FINISH;
585 else if (rcZlib == Z_STREAM_END)
586 fMaybeMore = false;
587 else
588 {
589 rtZipGzip_WriteOutputBuffer(pThis, true /*fBlocking*/);
590 return rtZipGzipConvertErrFromZlib(pThis, rcZlib);
591 }
592 }
593}
594
595
596/**
597 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
598 */
599static DECLCALLBACK(int) rtZipGzip_Flush(void *pvThis)
600{
601 PRTZIPGZIPSTREAM pThis = (PRTZIPGZIPSTREAM)pvThis;
602 if (!pThis->fDecompress)
603 {
604 int rc = rtZipGzip_FlushIt(pThis, Z_SYNC_FLUSH);
605 if (RT_FAILURE(rc))
606 return rc;
607 }
608
609 return RTVfsIoStrmFlush(pThis->hVfsIos);
610}
611
612
613/**
614 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
615 */
616static DECLCALLBACK(int) rtZipGzip_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
617 uint32_t *pfRetEvents)
618{
619 PRTZIPGZIPSTREAM pThis = (PRTZIPGZIPSTREAM)pvThis;
620
621 /*
622 * Collect our own events first and see if that satisfies the request. If
623 * not forward the call to the compressed stream.
624 */
625 uint32_t fRetEvents = 0;
626 if (pThis->fFatalError)
627 fRetEvents |= RTPOLL_EVT_ERROR;
628 if (pThis->fDecompress)
629 {
630 fEvents &= ~RTPOLL_EVT_WRITE;
631 if (pThis->Zlib.avail_in > 0)
632 fRetEvents = RTPOLL_EVT_READ;
633 }
634 else
635 {
636 fEvents &= ~RTPOLL_EVT_READ;
637 if (pThis->Zlib.avail_out > 0)
638 fRetEvents = RTPOLL_EVT_WRITE;
639 }
640
641 int rc = VINF_SUCCESS;
642 fRetEvents &= fEvents;
643 if (!fRetEvents)
644 rc = RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, cMillies, fIntr, pfRetEvents);
645 return rc;
646}
647
648
649/**
650 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
651 */
652static DECLCALLBACK(int) rtZipGzip_Tell(void *pvThis, PRTFOFF poffActual)
653{
654 PRTZIPGZIPSTREAM pThis = (PRTZIPGZIPSTREAM)pvThis;
655 *poffActual = pThis->offStream;
656 return VINF_SUCCESS;
657}
658
659
660/**
661 * The GZIP I/O stream vtable.
662 */
663static RTVFSIOSTREAMOPS g_rtZipGzipOps =
664{
665 { /* Obj */
666 RTVFSOBJOPS_VERSION,
667 RTVFSOBJTYPE_IO_STREAM,
668 "gzip",
669 rtZipGzip_Close,
670 rtZipGzip_QueryInfo,
671 NULL,
672 RTVFSOBJOPS_VERSION
673 },
674 RTVFSIOSTREAMOPS_VERSION,
675 RTVFSIOSTREAMOPS_FEAT_NO_SG,
676 rtZipGzip_Read,
677 rtZipGzip_Write,
678 rtZipGzip_Flush,
679 rtZipGzip_PollOne,
680 rtZipGzip_Tell,
681 NULL /* Skip */,
682 NULL /*ZeroFill*/,
683 RTVFSIOSTREAMOPS_VERSION,
684};
685
686
687RTDECL(int) RTZipGzipDecompressIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSIOSTREAM phVfsIosOut)
688{
689 AssertPtrReturn(hVfsIosIn, VERR_INVALID_HANDLE);
690 AssertReturn(!(fFlags & ~RTZIPGZIPDECOMP_F_ALLOW_ZLIB_HDR), VERR_INVALID_PARAMETER);
691 AssertPtrReturn(phVfsIosOut, VERR_INVALID_POINTER);
692
693 uint32_t cRefs = RTVfsIoStrmRetain(hVfsIosIn);
694 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
695
696 /*
697 * Create the decompression I/O stream.
698 */
699 RTVFSIOSTREAM hVfsIos;
700 PRTZIPGZIPSTREAM pThis;
701 int rc = RTVfsNewIoStream(&g_rtZipGzipOps, sizeof(RTZIPGZIPSTREAM), RTFILE_O_READ, NIL_RTVFS, NIL_RTVFSLOCK,
702 &hVfsIos, (void **)&pThis);
703 if (RT_SUCCESS(rc))
704 {
705 pThis->hVfsIos = hVfsIosIn;
706 pThis->offStream = 0;
707 pThis->fDecompress = true;
708 pThis->SgSeg.pvSeg = &pThis->abBuffer[0];
709 pThis->SgSeg.cbSeg = sizeof(pThis->abBuffer);
710 RTSgBufInit(&pThis->SgBuf, &pThis->SgSeg, 1);
711
712 memset(&pThis->Zlib, 0, sizeof(pThis->Zlib));
713 pThis->Zlib.opaque = pThis;
714 rc = inflateInit2(&pThis->Zlib, MAX_WBITS | RT_BIT(5) /* autodetect gzip header */);
715 if (rc >= 0)
716 {
717 /*
718 * Read the gzip header from the input stream to check that it's
719 * a gzip stream as specified by the user.
720 *
721 * Note! Since we've told zlib to check for the gzip header, we
722 * prebuffer what we read in the input buffer so it can
723 * be handed on to zlib later on.
724 */
725 rc = RTVfsIoStrmRead(pThis->hVfsIos, pThis->abBuffer, sizeof(RTZIPGZIPHDR), true /*fBlocking*/, NULL /*pcbRead*/);
726 if (RT_SUCCESS(rc))
727 {
728 /* Validate the header and make a copy of it. */
729 PCRTZIPGZIPHDR pHdr = (PCRTZIPGZIPHDR)pThis->abBuffer;
730 if ( pHdr->bId1 == RTZIPGZIPHDR_ID1
731 && pHdr->bId2 == RTZIPGZIPHDR_ID2
732 && !(pHdr->fFlags & ~RTZIPGZIPHDR_FLG_VALID_MASK))
733 {
734 if (pHdr->bCompressionMethod == RTZIPGZIPHDR_CM_DEFLATE)
735 rc = VINF_SUCCESS;
736 else
737 rc = VERR_ZIP_UNSUPPORTED_METHOD;
738 }
739 else if ( (fFlags & RTZIPGZIPDECOMP_F_ALLOW_ZLIB_HDR)
740 && (RT_MAKE_U16(pHdr->bId2, pHdr->bId1) % 31) == 0
741 && (pHdr->bId1 & 0xf) == RTZIPGZIPHDR_CM_DEFLATE )
742 {
743 pHdr = NULL;
744 rc = VINF_SUCCESS;
745 }
746 else
747 rc = VERR_ZIP_BAD_HEADER;
748 if (RT_SUCCESS(rc))
749 {
750 pThis->Zlib.avail_in = sizeof(RTZIPGZIPHDR);
751 pThis->Zlib.next_in = &pThis->abBuffer[0];
752 if (pHdr)
753 {
754 pThis->Hdr = *pHdr;
755 /* Parse on if there are names or comments. */
756 if (pHdr->fFlags & (RTZIPGZIPHDR_FLG_NAME | RTZIPGZIPHDR_FLG_COMMENT))
757 {
758 /** @todo Can implement this when someone needs the
759 * name or comment for something useful. */
760 }
761 }
762 if (RT_SUCCESS(rc))
763 {
764 *phVfsIosOut = hVfsIos;
765 return VINF_SUCCESS;
766 }
767 }
768 }
769 }
770 else
771 rc = rtZipGzipConvertErrFromZlib(pThis, rc); /** @todo cleaning up in this situation is going to go wrong. */
772 RTVfsIoStrmRelease(hVfsIos);
773 }
774 else
775 RTVfsIoStrmRelease(hVfsIosIn);
776 return rc;
777}
778
779
780RTDECL(int) RTZipGzipCompressIoStream(RTVFSIOSTREAM hVfsIosDst, uint32_t fFlags, uint8_t uLevel, PRTVFSIOSTREAM phVfsIosZip)
781{
782 AssertPtrReturn(hVfsIosDst, VERR_INVALID_HANDLE);
783 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
784 AssertPtrReturn(phVfsIosZip, VERR_INVALID_POINTER);
785 AssertReturn(uLevel > 0 && uLevel <= 9, VERR_INVALID_PARAMETER);
786
787 uint32_t cRefs = RTVfsIoStrmRetain(hVfsIosDst);
788 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
789
790 /*
791 * Create the compression I/O stream.
792 */
793 RTVFSIOSTREAM hVfsIos;
794 PRTZIPGZIPSTREAM pThis;
795 int rc = RTVfsNewIoStream(&g_rtZipGzipOps, sizeof(RTZIPGZIPSTREAM), RTFILE_O_WRITE, NIL_RTVFS, NIL_RTVFSLOCK,
796 &hVfsIos, (void **)&pThis);
797 if (RT_SUCCESS(rc))
798 {
799 pThis->hVfsIos = hVfsIosDst;
800 pThis->offStream = 0;
801 pThis->fDecompress = false;
802 pThis->SgSeg.pvSeg = &pThis->abBuffer[0];
803 pThis->SgSeg.cbSeg = sizeof(pThis->abBuffer);
804 RTSgBufInit(&pThis->SgBuf, &pThis->SgSeg, 1);
805
806 RT_ZERO(pThis->Zlib);
807 pThis->Zlib.opaque = pThis;
808 pThis->Zlib.next_out = &pThis->abBuffer[0];
809 pThis->Zlib.avail_out = sizeof(pThis->abBuffer);
810
811 rc = deflateInit2(&pThis->Zlib,
812 uLevel,
813 Z_DEFLATED,
814 15 /* Windows Size */ + 16 /* GZIP header */,
815 9 /* Max memory level for optimal speed */,
816 Z_DEFAULT_STRATEGY);
817
818 if (rc >= 0)
819 {
820 *phVfsIosZip = hVfsIos;
821 return VINF_SUCCESS;
822 }
823
824 rc = rtZipGzipConvertErrFromZlib(pThis, rc); /** @todo cleaning up in this situation is going to go wrong. */
825 RTVfsIoStrmRelease(hVfsIos);
826 }
827 else
828 RTVfsIoStrmRelease(hVfsIosDst);
829 return rc;
830}
831
832
833
834/**
835 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnValidate}
836 */
837static DECLCALLBACK(int) rtVfsChainGunzip_Validate(PCRTVFSCHAINELEMENTREG pProviderReg, PRTVFSCHAINSPEC pSpec,
838 PRTVFSCHAINELEMSPEC pElement, uint32_t *poffError, PRTERRINFO pErrInfo)
839{
840 RT_NOREF(pProviderReg, poffError, pErrInfo);
841
842 if (pElement->enmType != RTVFSOBJTYPE_IO_STREAM)
843 return VERR_VFS_CHAIN_ONLY_IOS;
844 if (pElement->enmTypeIn == RTVFSOBJTYPE_INVALID)
845 return VERR_VFS_CHAIN_CANNOT_BE_FIRST_ELEMENT;
846 if ( pElement->enmTypeIn != RTVFSOBJTYPE_FILE
847 && pElement->enmTypeIn != RTVFSOBJTYPE_IO_STREAM)
848 return VERR_VFS_CHAIN_TAKES_FILE_OR_IOS;
849 if (pSpec->fOpenFile & RTFILE_O_WRITE)
850 return VERR_VFS_CHAIN_READ_ONLY_IOS;
851 if (pElement->cArgs != 0)
852 return VERR_VFS_CHAIN_NO_ARGS;
853
854 return VINF_SUCCESS;
855}
856
857
858/**
859 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnInstantiate}
860 */
861static DECLCALLBACK(int) rtVfsChainGunzip_Instantiate(PCRTVFSCHAINELEMENTREG pProviderReg, PCRTVFSCHAINSPEC pSpec,
862 PCRTVFSCHAINELEMSPEC pElement, RTVFSOBJ hPrevVfsObj,
863 PRTVFSOBJ phVfsObj, uint32_t *poffError, PRTERRINFO pErrInfo)
864{
865 RT_NOREF(pProviderReg, pSpec, pElement, poffError, pErrInfo);
866 AssertReturn(hPrevVfsObj != NIL_RTVFSOBJ, VERR_VFS_CHAIN_IPE);
867
868 RTVFSIOSTREAM hVfsIosIn = RTVfsObjToIoStream(hPrevVfsObj);
869 if (hVfsIosIn == NIL_RTVFSIOSTREAM)
870 return VERR_VFS_CHAIN_CAST_FAILED;
871
872 RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
873 int rc = RTZipGzipDecompressIoStream(hVfsIosIn, 0 /*fFlags*/, &hVfsIos);
874 RTVfsObjFromIoStream(hVfsIosIn);
875 if (RT_SUCCESS(rc))
876 {
877 *phVfsObj = RTVfsObjFromIoStream(hVfsIos);
878 RTVfsIoStrmRelease(hVfsIos);
879 if (*phVfsObj != NIL_RTVFSOBJ)
880 return VINF_SUCCESS;
881 rc = VERR_VFS_CHAIN_CAST_FAILED;
882 }
883 return rc;
884}
885
886
887/**
888 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnCanReuseElement}
889 */
890static DECLCALLBACK(bool) rtVfsChainGunzip_CanReuseElement(PCRTVFSCHAINELEMENTREG pProviderReg,
891 PCRTVFSCHAINSPEC pSpec, PCRTVFSCHAINELEMSPEC pElement,
892 PCRTVFSCHAINSPEC pReuseSpec, PCRTVFSCHAINELEMSPEC pReuseElement)
893{
894 RT_NOREF(pProviderReg, pSpec, pElement, pReuseSpec, pReuseElement);
895 return false;
896}
897
898
899/** VFS chain element 'gunzip'. */
900static RTVFSCHAINELEMENTREG g_rtVfsChainGunzipReg =
901{
902 /* uVersion = */ RTVFSCHAINELEMENTREG_VERSION,
903 /* fReserved = */ 0,
904 /* pszName = */ "gunzip",
905 /* ListEntry = */ { NULL, NULL },
906 /* pszHelp = */ "Takes an I/O stream and gunzips it. No arguments.",
907 /* pfnValidate = */ rtVfsChainGunzip_Validate,
908 /* pfnInstantiate = */ rtVfsChainGunzip_Instantiate,
909 /* pfnCanReuseElement = */ rtVfsChainGunzip_CanReuseElement,
910 /* uEndMarker = */ RTVFSCHAINELEMENTREG_VERSION
911};
912
913RTVFSCHAIN_AUTO_REGISTER_ELEMENT_PROVIDER(&g_rtVfsChainGunzipReg, rtVfsChainGunzipReg);
914
915
916
917/**
918 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnValidate}
919 */
920static DECLCALLBACK(int) rtVfsChainGzip_Validate(PCRTVFSCHAINELEMENTREG pProviderReg, PRTVFSCHAINSPEC pSpec,
921 PRTVFSCHAINELEMSPEC pElement, uint32_t *poffError, PRTERRINFO pErrInfo)
922{
923 RT_NOREF(pProviderReg);
924
925 /*
926 * Basics.
927 */
928 if (pElement->enmType != RTVFSOBJTYPE_IO_STREAM)
929 return VERR_VFS_CHAIN_ONLY_IOS;
930 if (pElement->enmTypeIn == RTVFSOBJTYPE_INVALID)
931 return VERR_VFS_CHAIN_CANNOT_BE_FIRST_ELEMENT;
932 if ( pElement->enmTypeIn != RTVFSOBJTYPE_FILE
933 && pElement->enmTypeIn != RTVFSOBJTYPE_IO_STREAM)
934 return VERR_VFS_CHAIN_TAKES_FILE_OR_IOS;
935 if (pSpec->fOpenFile & RTFILE_O_READ)
936 return VERR_VFS_CHAIN_WRITE_ONLY_IOS;
937 if (pElement->cArgs > 1)
938 return VERR_VFS_CHAIN_AT_MOST_ONE_ARG;
939
940 /*
941 * Optional argument 1..9 indicating the compression level.
942 * We store it in pSpec->uProvider.
943 */
944 if (pElement->cArgs > 0)
945 {
946 const char *psz = pElement->paArgs[0].psz;
947 if (!*psz || !strcmp(psz, "default"))
948 pElement->uProvider = 6;
949 else if (!strcmp(psz, "fast"))
950 pElement->uProvider = 3;
951 else if ( RT_C_IS_DIGIT(*psz)
952 && *psz != '0'
953 && *RTStrStripL(psz + 1) == '\0')
954 pElement->uProvider = *psz - '0';
955 else
956 {
957 *poffError = pElement->paArgs[0].offSpec;
958 return RTErrInfoSet(pErrInfo, VERR_VFS_CHAIN_INVALID_ARGUMENT, "Expected compression level: 1-9, default, or fast");
959 }
960 }
961 else
962 pElement->uProvider = 6;
963
964 return VINF_SUCCESS;
965}
966
967
968/**
969 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnInstantiate}
970 */
971static DECLCALLBACK(int) rtVfsChainGzip_Instantiate(PCRTVFSCHAINELEMENTREG pProviderReg, PCRTVFSCHAINSPEC pSpec,
972 PCRTVFSCHAINELEMSPEC pElement, RTVFSOBJ hPrevVfsObj,
973 PRTVFSOBJ phVfsObj, uint32_t *poffError, PRTERRINFO pErrInfo)
974{
975 RT_NOREF(pProviderReg, pSpec, pElement, poffError, pErrInfo);
976 AssertReturn(hPrevVfsObj != NIL_RTVFSOBJ, VERR_VFS_CHAIN_IPE);
977
978 RTVFSIOSTREAM hVfsIosOut = RTVfsObjToIoStream(hPrevVfsObj);
979 if (hVfsIosOut == NIL_RTVFSIOSTREAM)
980 return VERR_VFS_CHAIN_CAST_FAILED;
981
982 RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
983 int rc = RTZipGzipCompressIoStream(hVfsIosOut, 0 /*fFlags*/, pElement->uProvider, &hVfsIos);
984 RTVfsObjFromIoStream(hVfsIosOut);
985 if (RT_SUCCESS(rc))
986 {
987 *phVfsObj = RTVfsObjFromIoStream(hVfsIos);
988 RTVfsIoStrmRelease(hVfsIos);
989 if (*phVfsObj != NIL_RTVFSOBJ)
990 return VINF_SUCCESS;
991 rc = VERR_VFS_CHAIN_CAST_FAILED;
992 }
993 return rc;
994}
995
996
997/**
998 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnCanReuseElement}
999 */
1000static DECLCALLBACK(bool) rtVfsChainGzip_CanReuseElement(PCRTVFSCHAINELEMENTREG pProviderReg,
1001 PCRTVFSCHAINSPEC pSpec, PCRTVFSCHAINELEMSPEC pElement,
1002 PCRTVFSCHAINSPEC pReuseSpec, PCRTVFSCHAINELEMSPEC pReuseElement)
1003{
1004 RT_NOREF(pProviderReg, pSpec, pElement, pReuseSpec, pReuseElement);
1005 return false;
1006}
1007
1008
1009/** VFS chain element 'gzip'. */
1010static RTVFSCHAINELEMENTREG g_rtVfsChainGzipReg =
1011{
1012 /* uVersion = */ RTVFSCHAINELEMENTREG_VERSION,
1013 /* fReserved = */ 0,
1014 /* pszName = */ "gzip",
1015 /* ListEntry = */ { NULL, NULL },
1016 /* pszHelp = */ "Takes an I/O stream and gzips it.\n"
1017 "Optional argument specifying compression level: 1-9, default, fast",
1018 /* pfnValidate = */ rtVfsChainGzip_Validate,
1019 /* pfnInstantiate = */ rtVfsChainGzip_Instantiate,
1020 /* pfnCanReuseElement = */ rtVfsChainGzip_CanReuseElement,
1021 /* uEndMarker = */ RTVFSCHAINELEMENTREG_VERSION
1022};
1023
1024RTVFSCHAIN_AUTO_REGISTER_ELEMENT_PROVIDER(&g_rtVfsChainGzipReg, rtVfsChainGzipReg);
1025
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