6 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
36 #ifndef IPRT_INCLUDED_zip_h
37 #define IPRT_INCLUDED_zip_h
38 #ifndef RT_WITHOUT_PRAGMA_ONCE
42 #include <iprt/cdefs.h>
43 #include <iprt/types.h>
47 /** @defgroup grp_rt_zip RTZip - Compression
55 * Callback function for consuming compressed data during compression.
57 * @returns iprt status code.
58 * @param pvUser User argument.
59 * @param pvBuf Compressed data.
60 * @param cbBuf Size of the compressed data.
62 typedef DECLCALLBACKTYPE(int, FNRTZIPOUT
,(void *pvUser
, const void *pvBuf
, size_t cbBuf
));
63 /** Pointer to FNRTZIPOUT() function. */
64 typedef FNRTZIPOUT
*PFNRTZIPOUT
;
67 * Callback function for supplying compressed data during decompression.
69 * @returns iprt status code.
70 * @param pvUser User argument.
71 * @param pvBuf Where to store the compressed data.
72 * @param cbBuf Size of the buffer.
73 * @param pcbBuf Number of bytes actually stored in the buffer.
75 typedef DECLCALLBACKTYPE(int, FNRTZIPIN
,(void *pvUser
, void *pvBuf
, size_t cbBuf
, size_t *pcbBuf
));
76 /** Pointer to FNRTZIPIN() function. */
77 typedef FNRTZIPIN
*PFNRTZIPIN
;
81 * (Be careful with these they are stored in files!)
83 typedef enum RTZIPTYPE
86 RTZIPTYPE_INVALID
= 0,
87 /** Choose best fitting one. */
89 /** Store the data. */
91 /** Zlib compression the data. */
93 /** BZlib compress. */
95 /** libLZF compress. */
97 /** Lempel-Ziv-Jeff-Bonwick compression. */
99 /** Lempel-Ziv-Oberhumer compression. */
101 /* Zlib compression the data without zlib header. */
102 RTZIPTYPE_ZLIB_NO_HEADER
,
103 /** End of valid the valid compression types. */
110 typedef enum RTZIPLEVEL
112 /** Store, don't compress. */
113 RTZIPLEVEL_STORE
= 0,
114 /** Fast compression. */
116 /** Default compression. */
118 /** Maximal compression. */
124 * Create a stream compressor instance.
126 * @returns iprt status code.
127 * @param ppZip Where to store the instance handle.
128 * @param pvUser User argument which will be passed on to pfnOut and pfnIn.
129 * @param pfnOut Callback for consuming output of compression.
130 * @param enmType Type of compressor to create.
131 * @param enmLevel Compression level.
133 RTDECL(int) RTZipCompCreate(PRTZIPCOMP
*ppZip
, void *pvUser
, PFNRTZIPOUT pfnOut
, RTZIPTYPE enmType
, RTZIPLEVEL enmLevel
);
136 * Compresses a chunk of memory.
138 * @returns iprt status code.
139 * @param pZip The compressor instance.
140 * @param pvBuf Pointer to buffer containing the bits to compress.
141 * @param cbBuf Number of bytes to compress.
143 RTDECL(int) RTZipCompress(PRTZIPCOMP pZip
, const void *pvBuf
, size_t cbBuf
);
146 * Finishes the compression.
147 * This will flush all data and terminate the compression data stream.
149 * @returns iprt status code.
150 * @param pZip The stream compressor instance.
152 RTDECL(int) RTZipCompFinish(PRTZIPCOMP pZip
);
155 * Destroys the stream compressor instance.
157 * @returns iprt status code.
158 * @param pZip The compressor instance.
160 RTDECL(int) RTZipCompDestroy(PRTZIPCOMP pZip
);
164 * Create a stream decompressor instance.
166 * @returns iprt status code.
167 * @param ppZip Where to store the instance handle.
168 * @param pvUser User argument which will be passed on to pfnOut and pfnIn.
169 * @param pfnIn Callback for producing input for decompression.
171 RTDECL(int) RTZipDecompCreate(PRTZIPDECOMP
*ppZip
, void *pvUser
, PFNRTZIPIN pfnIn
);
174 * Decompresses a chunk of memory.
176 * @returns iprt status code.
177 * @param pZip The stream decompressor instance.
178 * @param pvBuf Where to store the decompressed data.
179 * @param cbBuf Number of bytes to produce. If pcbWritten is set
180 * any number of bytes up to cbBuf might be returned.
181 * @param pcbWritten Number of bytes actually written to the buffer. If NULL
182 * cbBuf number of bytes must be written.
184 RTDECL(int) RTZipDecompress(PRTZIPDECOMP pZip
, void *pvBuf
, size_t cbBuf
, size_t *pcbWritten
);
187 * Destroys the stream decompressor instance.
189 * @returns iprt status code.
190 * @param pZip The decompressor instance.
192 RTDECL(int) RTZipDecompDestroy(PRTZIPDECOMP pZip
);
196 * Compress a chunk of memory into a block.
198 * @returns IPRT status code.
200 * @param enmType The compression type.
201 * @param enmLevel The compression level.
202 * @param fFlags Flags reserved for future extensions, MBZ.
203 * @param pvSrc Pointer to the input block.
204 * @param cbSrc Size of the input block.
205 * @param pvDst Pointer to the output buffer.
206 * @param cbDst The size of the output buffer.
207 * @param pcbDstActual Where to return the compressed size.
209 RTDECL(int) RTZipBlockCompress(RTZIPTYPE enmType
, RTZIPLEVEL enmLevel
, uint32_t fFlags
,
210 void const *pvSrc
, size_t cbSrc
,
211 void *pvDst
, size_t cbDst
, size_t *pcbDstActual
) RT_NO_THROW_PROTO
;
215 * Decompress a block.
217 * @returns IPRT status code.
219 * @param enmType The compression type.
220 * @param fFlags Flags reserved for future extensions, MBZ.
221 * @param pvSrc Pointer to the input block.
222 * @param cbSrc Size of the input block.
223 * @param pcbSrcActual Where to return the compressed size.
224 * @param pvDst Pointer to the output buffer.
225 * @param cbDst The size of the output buffer.
226 * @param pcbDstActual Where to return the decompressed size.
228 RTDECL(int) RTZipBlockDecompress(RTZIPTYPE enmType
, uint32_t fFlags
,
229 void const *pvSrc
, size_t cbSrc
, size_t *pcbSrcActual
,
230 void *pvDst
, size_t cbDst
, size_t *pcbDstActual
) RT_NO_THROW_PROTO
;
234 * Opens a gzip decompression I/O stream.
236 * @returns IPRT status code.
238 * @param hVfsIosIn The compressed input stream (must be readable).
239 * The reference is not consumed, instead another
241 * @param fFlags Flags, MBZ.
242 * @param phVfsIosGunzip Where to return the handle to the gunzipped I/O
245 RTDECL(int) RTZipGzipDecompressIoStream(RTVFSIOSTREAM hVfsIosIn
, uint32_t fFlags
, PRTVFSIOSTREAM phVfsIosGunzip
);
247 /** @name RTZipGzipDecompressIoStream flags.
249 /** Allow the smaller ZLIB header as well as the regular GZIP header. */
250 #define RTZIPGZIPDECOMP_F_ALLOW_ZLIB_HDR RT_BIT(0)
255 * Opens a xz decompression I/O stream.
257 * @returns IPRT status code.
259 * @param hVfsIosIn The compressed input stream (must be readable).
260 * The reference is not consumed, instead another
262 * @param fFlags Flags, MBZ.
263 * @param phVfsIosXz Where to return the handle to the decompressed I/O
266 RTDECL(int) RTZipXzDecompressIoStream(RTVFSIOSTREAM hVfsIosIn
, uint32_t fFlags
, PRTVFSIOSTREAM phVfsIosXz
);
270 * Opens a gzip decompression I/O stream.
272 * @returns IPRT status code.
274 * @param hVfsIosDst The compressed output stream (must be writable).
275 * The reference is not consumed, instead another
277 * @param fFlags Flags, MBZ.
278 * @param uLevel The gzip compression level, 1 thru 9.
279 * @param phVfsIosGzip Where to return the gzip input I/O stream handle
280 * (you write to this).
282 RTDECL(int) RTZipGzipCompressIoStream(RTVFSIOSTREAM hVfsIosDst
, uint32_t fFlags
, uint8_t uLevel
, PRTVFSIOSTREAM phVfsIosGzip
);
286 * Opens a xz decompression I/O stream.
288 * @returns IPRT status code.
290 * @param hVfsIosDst The compressed output stream (must be writable).
291 * The reference is not consumed, instead another
293 * @param fFlags Flags, MBZ.
294 * @param uLevel The xz compression level, 1 thru 9.
295 * @param phVfsIosXz Where to return the xz input I/O stream handle
296 * (you write to this).
298 RTDECL(int) RTZipXzCompressIoStream(RTVFSIOSTREAM hVfsIosDst
, uint32_t fFlags
, uint8_t uLevel
, PRTVFSIOSTREAM phVfsIosXz
);
302 * A mini GZIP program.
304 * @returns Program exit code.
306 * @param cArgs The number of arguments.
307 * @param papszArgs The argument vector. (Note that this may be
308 * reordered, so the memory must be writable.)
310 RTDECL(RTEXITCODE
) RTZipGzipCmd(unsigned cArgs
, char **papszArgs
);
313 * Opens a TAR filesystem stream.
315 * This is used to extract, list or check a TAR archive.
317 * @returns IPRT status code.
319 * @param hVfsIosIn The input stream. The reference is not
320 * consumed, instead another one is retained.
321 * @param fFlags Flags, MBZ.
322 * @param phVfsFss Where to return the handle to the TAR
325 RTDECL(int) RTZipTarFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn
, uint32_t fFlags
, PRTVFSFSSTREAM phVfsFss
);
327 /** TAR format type. */
328 typedef enum RTZIPTARFORMAT
330 /** Customary invalid zero value. */
331 RTZIPTARFORMAT_INVALID
= 0,
332 /** Default format (GNU). */
333 RTZIPTARFORMAT_DEFAULT
,
334 /** The GNU format. */
336 /** USTAR format from POSIX.1-1988. */
337 RTZIPTARFORMAT_USTAR
,
338 /** PAX format from POSIX.1-2001. */
340 /** CPIO format (portable ASCII foramt as defined by SuSV2). */
341 RTZIPTARFORMAT_CPIO_ASCII_SUSV2
,
342 /** CPIO format (New ascii format). */
343 RTZIPTARFORMAT_CPIO_ASCII_NEW
,
344 /** CPIO format (New ascii format with checksumming). */
345 RTZIPTARFORMAT_CPIO_ASCII_NEW_CHKSUM
,
346 /** End of valid formats. */
348 /** Make sure the type is at least 32 bits wide. */
349 RTZIPTARFORMAT_32BIT_HACK
= 0x7fffffff
353 * Opens a TAR filesystem stream for the purpose of create a new TAR archive.
355 * @returns IPRT status code.
357 * @param hVfsIosOut The output stream, i.e. where the tar stuff is
358 * written. The reference is not consumed, instead
359 * another one is retained.
360 * @param enmFormat The desired output format.
361 * @param fFlags RTZIPTAR_C_XXX, except RTZIPTAR_C_UPDATE.
362 * @param phVfsFss Where to return the handle to the TAR
365 RTDECL(int) RTZipTarFsStreamToIoStream(RTVFSIOSTREAM hVfsIosOut
, RTZIPTARFORMAT enmFormat
,
366 uint32_t fFlags
, PRTVFSFSSTREAM phVfsFss
);
368 /** @name RTZIPTAR_C_XXX - TAR creation flags (RTZipTarFsStreamToIoStream).
370 /** Check for sparse files.
371 * @note Only supported when adding file objects. The files will be read
373 #define RTZIPTAR_C_SPARSE RT_BIT_32(0)
374 /** Set if opening for updating. */
375 #define RTZIPTAR_C_UPDATE RT_BIT_32(1)
377 #define RTZIPTAR_C_VALID_MASK UINT32_C(0x00000003)
381 * Opens a TAR filesystem stream for the purpose of create a new TAR archive or
382 * updating an existing one.
384 * @returns IPRT status code.
386 * @param hVfsFile The TAR file handle, i.e. where the tar stuff is
387 * written and optionally read/update. The
388 * reference is not consumed, instead another one
390 * @param enmFormat The desired output format.
391 * @param fFlags RTZIPTAR_C_XXX.
392 * @param phVfsFss Where to return the handle to the TAR
395 RTDECL(int) RTZipTarFsStreamForFile(RTVFSFILE hVfsFile
, RTZIPTARFORMAT enmFormat
, uint32_t fFlags
, PRTVFSFSSTREAM phVfsFss
);
398 * Set the owner to store the archive entries with.
400 * @returns IPRT status code.
401 * @param hVfsFss The handle to a TAR creator.
402 * @param uid The UID value to set. Passing NIL_RTUID makes
403 * it use the value found in RTFSOBJINFO.
404 * @param pszOwner The owner name to store. Passing NULL makes it
405 * use the value found in RTFSOBJINFO.
407 RTDECL(int) RTZipTarFsStreamSetOwner(RTVFSFSSTREAM hVfsFss
, RTUID uid
, const char *pszOwner
);
410 * Set the group to store the archive entries with.
412 * @returns IPRT status code.
413 * @param hVfsFss The handle to a TAR creator.
414 * @param gid The GID value to set. Passing NIL_RTUID makes
415 * it use the value found in RTFSOBJINFO.
416 * @param pszGroup The group name to store. Passing NULL makes it
417 * use the value found in RTFSOBJINFO.
419 RTDECL(int) RTZipTarFsStreamSetGroup(RTVFSFSSTREAM hVfsFss
, RTGID gid
, const char *pszGroup
);
422 * Set path prefix to store the archive entries with.
424 * @returns IPRT status code.
425 * @param hVfsFss The handle to a TAR creator.
426 * @param pszPrefix The path prefix to join the names with. Pass
427 * NULL for no prefix.
429 RTDECL(int) RTZipTarFsStreamSetPrefix(RTVFSFSSTREAM hVfsFss
, const char *pszPrefix
);
432 * Set the AND and OR masks to apply to file (non-dir) modes in the archive.
434 * @returns IPRT status code.
435 * @param hVfsFss The handle to a TAR creator.
436 * @param fAndMode The bits to keep
437 * @param fOrMode The bits to set.
439 RTDECL(int) RTZipTarFsStreamSetFileMode(RTVFSFSSTREAM hVfsFss
, RTFMODE fAndMode
, RTFMODE fOrMode
);
442 * Set the AND and OR masks to apply to directory modes in the archive.
444 * @returns IPRT status code.
445 * @param hVfsFss The handle to a TAR creator.
446 * @param fAndMode The bits to keep
447 * @param fOrMode The bits to set.
449 RTDECL(int) RTZipTarFsStreamSetDirMode(RTVFSFSSTREAM hVfsFss
, RTFMODE fAndMode
, RTFMODE fOrMode
);
452 * Set the modification time to store the archive entires with.
454 * @returns IPRT status code.
455 * @param hVfsFss The handle to a TAR creator.
456 * @param pModificationTime The modification time to use. Pass NULL to use
457 * the value found in RTFSOBJINFO.
459 RTDECL(int) RTZipTarFsStreamSetMTime(RTVFSFSSTREAM hVfsFss
, PCRTTIMESPEC pModificationTime
);
462 * Truncates a TAR creator stream in update mode.
464 * Use RTVfsFsStrmNext to examine the TAR stream and locate the cut-off point.
466 * After performing this call, the stream will be in write mode and
467 * RTVfsFsStrmNext will stop working (VERR_WRONG_ORDER). The RTVfsFsStrmAdd()
468 * and RTVfsFsStrmPushFile() can be used to add new object to the TAR file,
469 * starting at the trunction point. RTVfsFsStrmEnd() is used to finish the TAR
470 * file (this performs the actual file trunction).
472 * @returns IPRT status code.
473 * @param hVfsFss The handle to a TAR creator in update mode.
474 * @param hVfsObj Object returned by RTVfsFsStrmNext that the
475 * trunction is relative to. This doesn't have to
476 * be the current stream object, it can be an
478 * @param fAfter If set, @a hVfsObj will remain in the update TAR
479 * file. If clear, @a hVfsObj will not be
482 RTDECL(int) RTZipTarFsStreamTruncate(RTVFSFSSTREAM hVfsFss
, RTVFSOBJ hVfsObj
, bool fAfter
);
485 * A mini TAR program.
487 * @returns Program exit code.
489 * @param cArgs The number of arguments.
490 * @param papszArgs The argument vector. (Note that this may be
491 * reordered, so the memory must be writable.)
493 RTDECL(RTEXITCODE
) RTZipTarCmd(unsigned cArgs
, char **papszArgs
);
496 * Opens a ZIP filesystem stream.
498 * This is used to extract, list or check a ZIP archive.
500 * @returns IPRT status code.
502 * @param hVfsIosIn The compressed input stream. The reference is
503 * not consumed, instead another one is retained.
504 * @param fFlags Flags, MBZ.
505 * @param phVfsFss Where to return the handle to the TAR
508 RTDECL(int) RTZipPkzipFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn
, uint32_t fFlags
, PRTVFSFSSTREAM phVfsFss
);
511 * A mini UNZIP program.
513 * @returns Program exit code.
515 * @param cArgs The number of arguments.
516 * @param papszArgs The argument vector. (Note that this may be
517 * reordered, so the memory must be writable.)
519 RTDECL(RTEXITCODE
) RTZipUnzipCmd(unsigned cArgs
, char **papszArgs
);
522 * Helper for decompressing files of a ZIP file located in memory.
524 * @returns IPRT status code.
526 * @param ppvDst Where to store the pointer to the allocated
527 * buffer. To be freed with RTMemFree().
528 * @param pcbDst Where to store the pointer to the size of the
530 * @param pvSrc Pointer to the buffer containing the .zip file.
531 * @param cbSrc Size of the buffer containing the .zip file.
532 * @param pszObject Name of the object to extract.
534 RTDECL(int) RTZipPkzipMemDecompress(void **ppvDst
, size_t *pcbDst
, const void *pvSrc
, size_t cbSrc
, const char *pszObject
);
537 * Opens a XAR filesystem stream.
539 * This is used to extract, list or check a XAR archive.
541 * @returns IPRT status code.
543 * @param hVfsIosIn The compressed input stream. The reference is
544 * not consumed, instead another one is retained.
545 * @param fFlags Flags, MBZ.
546 * @param phVfsFss Where to return the handle to the XAR filesystem
549 RTDECL(int) RTZipXarFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn
, uint32_t fFlags
, PRTVFSFSSTREAM phVfsFss
);
552 * Opens a CPIO filesystem stream.
554 * This is used to extract, list or check a CPIO archive.
556 * @returns IPRT status code.
558 * @param hVfsIosIn The input stream. The reference is not
559 * consumed, instead another one is retained.
560 * @param fFlags Flags, MBZ.
561 * @param phVfsFss Where to return the handle to the CPIO
564 RTDECL(int) RTZipCpioFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn
, uint32_t fFlags
, PRTVFSFSSTREAM phVfsFss
);
570 #endif /* !IPRT_INCLUDED_zip_h */