VirtualBox

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

Last change on this file since 106580 was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

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