VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/zip/gzipvfs.cpp@ 81106

Last change on this file since 81106 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • 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 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - GZIP Compressor and Decompressor I/O Stream.
4 */
5
6/*
7 * Copyright (C) 2010-2019 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 RTVFSOBJOPS_VERSION
672 },
673 RTVFSIOSTREAMOPS_VERSION,
674 RTVFSIOSTREAMOPS_FEAT_NO_SG,
675 rtZipGzip_Read,
676 rtZipGzip_Write,
677 rtZipGzip_Flush,
678 rtZipGzip_PollOne,
679 rtZipGzip_Tell,
680 NULL /* Skip */,
681 NULL /*ZeroFill*/,
682 RTVFSIOSTREAMOPS_VERSION,
683};
684
685
686RTDECL(int) RTZipGzipDecompressIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSIOSTREAM phVfsIosOut)
687{
688 AssertPtrReturn(hVfsIosIn, VERR_INVALID_HANDLE);
689 AssertReturn(!(fFlags & ~RTZIPGZIPDECOMP_F_ALLOW_ZLIB_HDR), VERR_INVALID_PARAMETER);
690 AssertPtrReturn(phVfsIosOut, VERR_INVALID_POINTER);
691
692 uint32_t cRefs = RTVfsIoStrmRetain(hVfsIosIn);
693 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
694
695 /*
696 * Create the decompression I/O stream.
697 */
698 RTVFSIOSTREAM hVfsIos;
699 PRTZIPGZIPSTREAM pThis;
700 int rc = RTVfsNewIoStream(&g_rtZipGzipOps, sizeof(RTZIPGZIPSTREAM), RTFILE_O_READ, NIL_RTVFS, NIL_RTVFSLOCK,
701 &hVfsIos, (void **)&pThis);
702 if (RT_SUCCESS(rc))
703 {
704 pThis->hVfsIos = hVfsIosIn;
705 pThis->offStream = 0;
706 pThis->fDecompress = true;
707 pThis->SgSeg.pvSeg = &pThis->abBuffer[0];
708 pThis->SgSeg.cbSeg = sizeof(pThis->abBuffer);
709 RTSgBufInit(&pThis->SgBuf, &pThis->SgSeg, 1);
710
711 memset(&pThis->Zlib, 0, sizeof(pThis->Zlib));
712 pThis->Zlib.opaque = pThis;
713 rc = inflateInit2(&pThis->Zlib, MAX_WBITS | RT_BIT(5) /* autodetect gzip header */);
714 if (rc >= 0)
715 {
716 /*
717 * Read the gzip header from the input stream to check that it's
718 * a gzip stream as specified by the user.
719 *
720 * Note! Since we've told zlib to check for the gzip header, we
721 * prebuffer what we read in the input buffer so it can
722 * be handed on to zlib later on.
723 */
724 rc = RTVfsIoStrmRead(pThis->hVfsIos, pThis->abBuffer, sizeof(RTZIPGZIPHDR), true /*fBlocking*/, NULL /*pcbRead*/);
725 if (RT_SUCCESS(rc))
726 {
727 /* Validate the header and make a copy of it. */
728 PCRTZIPGZIPHDR pHdr = (PCRTZIPGZIPHDR)pThis->abBuffer;
729 if ( pHdr->bId1 == RTZIPGZIPHDR_ID1
730 && pHdr->bId2 == RTZIPGZIPHDR_ID2
731 && !(pHdr->fFlags & ~RTZIPGZIPHDR_FLG_VALID_MASK))
732 {
733 if (pHdr->bCompressionMethod == RTZIPGZIPHDR_CM_DEFLATE)
734 rc = VINF_SUCCESS;
735 else
736 rc = VERR_ZIP_UNSUPPORTED_METHOD;
737 }
738 else if ( (fFlags & RTZIPGZIPDECOMP_F_ALLOW_ZLIB_HDR)
739 && (RT_MAKE_U16(pHdr->bId2, pHdr->bId1) % 31) == 0
740 && (pHdr->bId1 & 0xf) == RTZIPGZIPHDR_CM_DEFLATE )
741 {
742 pHdr = NULL;
743 rc = VINF_SUCCESS;
744 }
745 else
746 rc = VERR_ZIP_BAD_HEADER;
747 if (RT_SUCCESS(rc))
748 {
749 pThis->Zlib.avail_in = sizeof(RTZIPGZIPHDR);
750 pThis->Zlib.next_in = &pThis->abBuffer[0];
751 if (pHdr)
752 {
753 pThis->Hdr = *pHdr;
754 /* Parse on if there are names or comments. */
755 if (pHdr->fFlags & (RTZIPGZIPHDR_FLG_NAME | RTZIPGZIPHDR_FLG_COMMENT))
756 {
757 /** @todo Can implement this when someone needs the
758 * name or comment for something useful. */
759 }
760 }
761 if (RT_SUCCESS(rc))
762 {
763 *phVfsIosOut = hVfsIos;
764 return VINF_SUCCESS;
765 }
766 }
767 }
768 }
769 else
770 rc = rtZipGzipConvertErrFromZlib(pThis, rc); /** @todo cleaning up in this situation is going to go wrong. */
771 RTVfsIoStrmRelease(hVfsIos);
772 }
773 else
774 RTVfsIoStrmRelease(hVfsIosIn);
775 return rc;
776}
777
778
779RTDECL(int) RTZipGzipCompressIoStream(RTVFSIOSTREAM hVfsIosDst, uint32_t fFlags, uint8_t uLevel, PRTVFSIOSTREAM phVfsIosZip)
780{
781 AssertPtrReturn(hVfsIosDst, VERR_INVALID_HANDLE);
782 AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
783 AssertPtrReturn(phVfsIosZip, VERR_INVALID_POINTER);
784 AssertReturn(uLevel > 0 && uLevel <= 9, VERR_INVALID_PARAMETER);
785
786 uint32_t cRefs = RTVfsIoStrmRetain(hVfsIosDst);
787 AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
788
789 /*
790 * Create the compression I/O stream.
791 */
792 RTVFSIOSTREAM hVfsIos;
793 PRTZIPGZIPSTREAM pThis;
794 int rc = RTVfsNewIoStream(&g_rtZipGzipOps, sizeof(RTZIPGZIPSTREAM), RTFILE_O_WRITE, NIL_RTVFS, NIL_RTVFSLOCK,
795 &hVfsIos, (void **)&pThis);
796 if (RT_SUCCESS(rc))
797 {
798 pThis->hVfsIos = hVfsIosDst;
799 pThis->offStream = 0;
800 pThis->fDecompress = false;
801 pThis->SgSeg.pvSeg = &pThis->abBuffer[0];
802 pThis->SgSeg.cbSeg = sizeof(pThis->abBuffer);
803 RTSgBufInit(&pThis->SgBuf, &pThis->SgSeg, 1);
804
805 RT_ZERO(pThis->Zlib);
806 pThis->Zlib.opaque = pThis;
807 pThis->Zlib.next_out = &pThis->abBuffer[0];
808 pThis->Zlib.avail_out = sizeof(pThis->abBuffer);
809
810 rc = deflateInit2(&pThis->Zlib,
811 uLevel,
812 Z_DEFLATED,
813 15 /* Windows Size */ + 16 /* GZIP header */,
814 9 /* Max memory level for optimal speed */,
815 Z_DEFAULT_STRATEGY);
816
817 if (rc >= 0)
818 {
819 *phVfsIosZip = hVfsIos;
820 return VINF_SUCCESS;
821 }
822
823 rc = rtZipGzipConvertErrFromZlib(pThis, rc); /** @todo cleaning up in this situation is going to go wrong. */
824 RTVfsIoStrmRelease(hVfsIos);
825 }
826 else
827 RTVfsIoStrmRelease(hVfsIosDst);
828 return rc;
829}
830
831
832
833/**
834 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnValidate}
835 */
836static DECLCALLBACK(int) rtVfsChainGunzip_Validate(PCRTVFSCHAINELEMENTREG pProviderReg, PRTVFSCHAINSPEC pSpec,
837 PRTVFSCHAINELEMSPEC pElement, uint32_t *poffError, PRTERRINFO pErrInfo)
838{
839 RT_NOREF(pProviderReg, poffError, pErrInfo);
840
841 if (pElement->enmType != RTVFSOBJTYPE_IO_STREAM)
842 return VERR_VFS_CHAIN_ONLY_IOS;
843 if (pElement->enmTypeIn == RTVFSOBJTYPE_INVALID)
844 return VERR_VFS_CHAIN_CANNOT_BE_FIRST_ELEMENT;
845 if ( pElement->enmTypeIn != RTVFSOBJTYPE_FILE
846 && pElement->enmTypeIn != RTVFSOBJTYPE_IO_STREAM)
847 return VERR_VFS_CHAIN_TAKES_FILE_OR_IOS;
848 if (pSpec->fOpenFile & RTFILE_O_WRITE)
849 return VERR_VFS_CHAIN_READ_ONLY_IOS;
850 if (pElement->cArgs != 0)
851 return VERR_VFS_CHAIN_NO_ARGS;
852
853 return VINF_SUCCESS;
854}
855
856
857/**
858 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnInstantiate}
859 */
860static DECLCALLBACK(int) rtVfsChainGunzip_Instantiate(PCRTVFSCHAINELEMENTREG pProviderReg, PCRTVFSCHAINSPEC pSpec,
861 PCRTVFSCHAINELEMSPEC pElement, RTVFSOBJ hPrevVfsObj,
862 PRTVFSOBJ phVfsObj, uint32_t *poffError, PRTERRINFO pErrInfo)
863{
864 RT_NOREF(pProviderReg, pSpec, pElement, poffError, pErrInfo);
865 AssertReturn(hPrevVfsObj != NIL_RTVFSOBJ, VERR_VFS_CHAIN_IPE);
866
867 RTVFSIOSTREAM hVfsIosIn = RTVfsObjToIoStream(hPrevVfsObj);
868 if (hVfsIosIn == NIL_RTVFSIOSTREAM)
869 return VERR_VFS_CHAIN_CAST_FAILED;
870
871 RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
872 int rc = RTZipGzipDecompressIoStream(hVfsIosIn, 0 /*fFlags*/, &hVfsIos);
873 RTVfsObjFromIoStream(hVfsIosIn);
874 if (RT_SUCCESS(rc))
875 {
876 *phVfsObj = RTVfsObjFromIoStream(hVfsIos);
877 RTVfsIoStrmRelease(hVfsIos);
878 if (*phVfsObj != NIL_RTVFSOBJ)
879 return VINF_SUCCESS;
880 rc = VERR_VFS_CHAIN_CAST_FAILED;
881 }
882 return rc;
883}
884
885
886/**
887 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnCanReuseElement}
888 */
889static DECLCALLBACK(bool) rtVfsChainGunzip_CanReuseElement(PCRTVFSCHAINELEMENTREG pProviderReg,
890 PCRTVFSCHAINSPEC pSpec, PCRTVFSCHAINELEMSPEC pElement,
891 PCRTVFSCHAINSPEC pReuseSpec, PCRTVFSCHAINELEMSPEC pReuseElement)
892{
893 RT_NOREF(pProviderReg, pSpec, pElement, pReuseSpec, pReuseElement);
894 return false;
895}
896
897
898/** VFS chain element 'gunzip'. */
899static RTVFSCHAINELEMENTREG g_rtVfsChainGunzipReg =
900{
901 /* uVersion = */ RTVFSCHAINELEMENTREG_VERSION,
902 /* fReserved = */ 0,
903 /* pszName = */ "gunzip",
904 /* ListEntry = */ { NULL, NULL },
905 /* pszHelp = */ "Takes an I/O stream and gunzips it. No arguments.",
906 /* pfnValidate = */ rtVfsChainGunzip_Validate,
907 /* pfnInstantiate = */ rtVfsChainGunzip_Instantiate,
908 /* pfnCanReuseElement = */ rtVfsChainGunzip_CanReuseElement,
909 /* uEndMarker = */ RTVFSCHAINELEMENTREG_VERSION
910};
911
912RTVFSCHAIN_AUTO_REGISTER_ELEMENT_PROVIDER(&g_rtVfsChainGunzipReg, rtVfsChainGunzipReg);
913
914
915
916/**
917 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnValidate}
918 */
919static DECLCALLBACK(int) rtVfsChainGzip_Validate(PCRTVFSCHAINELEMENTREG pProviderReg, PRTVFSCHAINSPEC pSpec,
920 PRTVFSCHAINELEMSPEC pElement, uint32_t *poffError, PRTERRINFO pErrInfo)
921{
922 RT_NOREF(pProviderReg);
923
924 /*
925 * Basics.
926 */
927 if (pElement->enmType != RTVFSOBJTYPE_IO_STREAM)
928 return VERR_VFS_CHAIN_ONLY_IOS;
929 if (pElement->enmTypeIn == RTVFSOBJTYPE_INVALID)
930 return VERR_VFS_CHAIN_CANNOT_BE_FIRST_ELEMENT;
931 if ( pElement->enmTypeIn != RTVFSOBJTYPE_FILE
932 && pElement->enmTypeIn != RTVFSOBJTYPE_IO_STREAM)
933 return VERR_VFS_CHAIN_TAKES_FILE_OR_IOS;
934 if (pSpec->fOpenFile & RTFILE_O_READ)
935 return VERR_VFS_CHAIN_WRITE_ONLY_IOS;
936 if (pElement->cArgs > 1)
937 return VERR_VFS_CHAIN_AT_MOST_ONE_ARG;
938
939 /*
940 * Optional argument 1..9 indicating the compression level.
941 * We store it in pSpec->uProvider.
942 */
943 if (pElement->cArgs > 0)
944 {
945 const char *psz = pElement->paArgs[0].psz;
946 if (!*psz || !strcmp(psz, "default"))
947 pElement->uProvider = 6;
948 else if (!strcmp(psz, "fast"))
949 pElement->uProvider = 3;
950 else if ( RT_C_IS_DIGIT(*psz)
951 && *psz != '0'
952 && *RTStrStripL(psz + 1) == '\0')
953 pElement->uProvider = *psz - '0';
954 else
955 {
956 *poffError = pElement->paArgs[0].offSpec;
957 return RTErrInfoSet(pErrInfo, VERR_VFS_CHAIN_INVALID_ARGUMENT, "Expected compression level: 1-9, default, or fast");
958 }
959 }
960 else
961 pElement->uProvider = 6;
962
963 return VINF_SUCCESS;
964}
965
966
967/**
968 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnInstantiate}
969 */
970static DECLCALLBACK(int) rtVfsChainGzip_Instantiate(PCRTVFSCHAINELEMENTREG pProviderReg, PCRTVFSCHAINSPEC pSpec,
971 PCRTVFSCHAINELEMSPEC pElement, RTVFSOBJ hPrevVfsObj,
972 PRTVFSOBJ phVfsObj, uint32_t *poffError, PRTERRINFO pErrInfo)
973{
974 RT_NOREF(pProviderReg, pSpec, pElement, poffError, pErrInfo);
975 AssertReturn(hPrevVfsObj != NIL_RTVFSOBJ, VERR_VFS_CHAIN_IPE);
976
977 RTVFSIOSTREAM hVfsIosOut = RTVfsObjToIoStream(hPrevVfsObj);
978 if (hVfsIosOut == NIL_RTVFSIOSTREAM)
979 return VERR_VFS_CHAIN_CAST_FAILED;
980
981 RTVFSIOSTREAM hVfsIos = NIL_RTVFSIOSTREAM;
982 int rc = RTZipGzipCompressIoStream(hVfsIosOut, 0 /*fFlags*/, pElement->uProvider, &hVfsIos);
983 RTVfsObjFromIoStream(hVfsIosOut);
984 if (RT_SUCCESS(rc))
985 {
986 *phVfsObj = RTVfsObjFromIoStream(hVfsIos);
987 RTVfsIoStrmRelease(hVfsIos);
988 if (*phVfsObj != NIL_RTVFSOBJ)
989 return VINF_SUCCESS;
990 rc = VERR_VFS_CHAIN_CAST_FAILED;
991 }
992 return rc;
993}
994
995
996/**
997 * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnCanReuseElement}
998 */
999static DECLCALLBACK(bool) rtVfsChainGzip_CanReuseElement(PCRTVFSCHAINELEMENTREG pProviderReg,
1000 PCRTVFSCHAINSPEC pSpec, PCRTVFSCHAINELEMSPEC pElement,
1001 PCRTVFSCHAINSPEC pReuseSpec, PCRTVFSCHAINELEMSPEC pReuseElement)
1002{
1003 RT_NOREF(pProviderReg, pSpec, pElement, pReuseSpec, pReuseElement);
1004 return false;
1005}
1006
1007
1008/** VFS chain element 'gzip'. */
1009static RTVFSCHAINELEMENTREG g_rtVfsChainGzipReg =
1010{
1011 /* uVersion = */ RTVFSCHAINELEMENTREG_VERSION,
1012 /* fReserved = */ 0,
1013 /* pszName = */ "gzip",
1014 /* ListEntry = */ { NULL, NULL },
1015 /* pszHelp = */ "Takes an I/O stream and gzips it.\n"
1016 "Optional argument specifying compression level: 1-9, default, fast",
1017 /* pfnValidate = */ rtVfsChainGzip_Validate,
1018 /* pfnInstantiate = */ rtVfsChainGzip_Instantiate,
1019 /* pfnCanReuseElement = */ rtVfsChainGzip_CanReuseElement,
1020 /* uEndMarker = */ RTVFSCHAINELEMENTREG_VERSION
1021};
1022
1023RTVFSCHAIN_AUTO_REGISTER_ELEMENT_PROVIDER(&g_rtVfsChainGzipReg, rtVfsChainGzipReg);
1024
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