VirtualBox

source: vbox/trunk/include/iprt/zip.h@ 98463

Last change on this file since 98463 was 98463, checked in by vboxsync, 2 years ago

Runtime/commone/zip/tarvfswriter.cpp: Add support to write CPIO archives (only the ascii style without checksum is supported right now), shares a lot with the tar code so it didn't made sense to put it into a new vfs module [build fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.3 KB
Line 
1/** @file
2 * IPRT - Compression.
3 */
4
5/*
6 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.215389.xyz.
10 *
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
14 * License.
15 *
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.
20 *
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>.
23 *
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.
29 *
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.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_zip_h
37#define IPRT_INCLUDED_zip_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/cdefs.h>
43#include <iprt/types.h>
44
45RT_C_DECLS_BEGIN
46
47/** @defgroup grp_rt_zip RTZip - Compression
48 * @ingroup grp_rt
49 * @{
50 */
51
52
53
54/**
55 * Callback function for consuming compressed data during compression.
56 *
57 * @returns iprt status code.
58 * @param pvUser User argument.
59 * @param pvBuf Compressed data.
60 * @param cbBuf Size of the compressed data.
61 */
62typedef DECLCALLBACKTYPE(int, FNRTZIPOUT,(void *pvUser, const void *pvBuf, size_t cbBuf));
63/** Pointer to FNRTZIPOUT() function. */
64typedef FNRTZIPOUT *PFNRTZIPOUT;
65
66/**
67 * Callback function for supplying compressed data during decompression.
68 *
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.
74 */
75typedef DECLCALLBACKTYPE(int, FNRTZIPIN,(void *pvUser, void *pvBuf, size_t cbBuf, size_t *pcbBuf));
76/** Pointer to FNRTZIPIN() function. */
77typedef FNRTZIPIN *PFNRTZIPIN;
78
79/**
80 * Compression type.
81 * (Be careful with these they are stored in files!)
82 */
83typedef enum RTZIPTYPE
84{
85 /** Invalid. */
86 RTZIPTYPE_INVALID = 0,
87 /** Choose best fitting one. */
88 RTZIPTYPE_AUTO,
89 /** Store the data. */
90 RTZIPTYPE_STORE,
91 /** Zlib compression the data. */
92 RTZIPTYPE_ZLIB,
93 /** BZlib compress. */
94 RTZIPTYPE_BZLIB,
95 /** libLZF compress. */
96 RTZIPTYPE_LZF,
97 /** Lempel-Ziv-Jeff-Bonwick compression. */
98 RTZIPTYPE_LZJB,
99 /** Lempel-Ziv-Oberhumer compression. */
100 RTZIPTYPE_LZO,
101 /* Zlib compression the data without zlib header. */
102 RTZIPTYPE_ZLIB_NO_HEADER,
103 /** End of valid the valid compression types. */
104 RTZIPTYPE_END
105} RTZIPTYPE;
106
107/**
108 * Compression level.
109 */
110typedef enum RTZIPLEVEL
111{
112 /** Store, don't compress. */
113 RTZIPLEVEL_STORE = 0,
114 /** Fast compression. */
115 RTZIPLEVEL_FAST,
116 /** Default compression. */
117 RTZIPLEVEL_DEFAULT,
118 /** Maximal compression. */
119 RTZIPLEVEL_MAX
120} RTZIPLEVEL;
121
122
123/**
124 * Create a stream compressor instance.
125 *
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.
132 */
133RTDECL(int) RTZipCompCreate(PRTZIPCOMP *ppZip, void *pvUser, PFNRTZIPOUT pfnOut, RTZIPTYPE enmType, RTZIPLEVEL enmLevel);
134
135/**
136 * Compresses a chunk of memory.
137 *
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.
142 */
143RTDECL(int) RTZipCompress(PRTZIPCOMP pZip, const void *pvBuf, size_t cbBuf);
144
145/**
146 * Finishes the compression.
147 * This will flush all data and terminate the compression data stream.
148 *
149 * @returns iprt status code.
150 * @param pZip The stream compressor instance.
151 */
152RTDECL(int) RTZipCompFinish(PRTZIPCOMP pZip);
153
154/**
155 * Destroys the stream compressor instance.
156 *
157 * @returns iprt status code.
158 * @param pZip The compressor instance.
159 */
160RTDECL(int) RTZipCompDestroy(PRTZIPCOMP pZip);
161
162
163/**
164 * Create a stream decompressor instance.
165 *
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.
170 */
171RTDECL(int) RTZipDecompCreate(PRTZIPDECOMP *ppZip, void *pvUser, PFNRTZIPIN pfnIn);
172
173/**
174 * Decompresses a chunk of memory.
175 *
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.
183 */
184RTDECL(int) RTZipDecompress(PRTZIPDECOMP pZip, void *pvBuf, size_t cbBuf, size_t *pcbWritten);
185
186/**
187 * Destroys the stream decompressor instance.
188 *
189 * @returns iprt status code.
190 * @param pZip The decompressor instance.
191 */
192RTDECL(int) RTZipDecompDestroy(PRTZIPDECOMP pZip);
193
194
195/**
196 * Compress a chunk of memory into a block.
197 *
198 * @returns IPRT status code.
199 *
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.
208 */
209RTDECL(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;
212
213
214/**
215 * Decompress a block.
216 *
217 * @returns IPRT status code.
218 *
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.
227 */
228RTDECL(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;
231
232
233/**
234 * Opens a gzip decompression I/O stream.
235 *
236 * @returns IPRT status code.
237 *
238 * @param hVfsIosIn The compressed input stream (must be readable).
239 * The reference is not consumed, instead another
240 * one is retained.
241 * @param fFlags Flags, MBZ.
242 * @param phVfsIosGunzip Where to return the handle to the gunzipped I/O
243 * stream (read).
244 */
245RTDECL(int) RTZipGzipDecompressIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSIOSTREAM phVfsIosGunzip);
246
247/** @name RTZipGzipDecompressIoStream flags.
248 * @{ */
249/** Allow the smaller ZLIB header as well as the regular GZIP header. */
250#define RTZIPGZIPDECOMP_F_ALLOW_ZLIB_HDR RT_BIT(0)
251/** @} */
252
253
254/**
255 * Opens a gzip decompression I/O stream.
256 *
257 * @returns IPRT status code.
258 *
259 * @param hVfsIosDst The compressed output stream (must be writable).
260 * The reference is not consumed, instead another
261 * one is retained.
262 * @param fFlags Flags, MBZ.
263 * @param uLevel The gzip compression level, 1 thru 9.
264 * @param phVfsIosGzip Where to return the gzip input I/O stream handle
265 * (you write to this).
266 */
267RTDECL(int) RTZipGzipCompressIoStream(RTVFSIOSTREAM hVfsIosDst, uint32_t fFlags, uint8_t uLevel, PRTVFSIOSTREAM phVfsIosGzip);
268
269
270/**
271 * A mini GZIP program.
272 *
273 * @returns Program exit code.
274 *
275 * @param cArgs The number of arguments.
276 * @param papszArgs The argument vector. (Note that this may be
277 * reordered, so the memory must be writable.)
278 */
279RTDECL(RTEXITCODE) RTZipGzipCmd(unsigned cArgs, char **papszArgs);
280
281/**
282 * Opens a TAR filesystem stream.
283 *
284 * This is used to extract, list or check a TAR archive.
285 *
286 * @returns IPRT status code.
287 *
288 * @param hVfsIosIn The input stream. The reference is not
289 * consumed, instead another one is retained.
290 * @param fFlags Flags, MBZ.
291 * @param phVfsFss Where to return the handle to the TAR
292 * filesystem stream.
293 */
294RTDECL(int) RTZipTarFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss);
295
296/** TAR format type. */
297typedef enum RTZIPTARFORMAT
298{
299 /** Customary invalid zero value. */
300 RTZIPTARFORMAT_INVALID = 0,
301 /** Default format (GNU). */
302 RTZIPTARFORMAT_DEFAULT,
303 /** The GNU format. */
304 RTZIPTARFORMAT_GNU,
305 /** USTAR format from POSIX.1-1988. */
306 RTZIPTARFORMAT_USTAR,
307 /** PAX format from POSIX.1-2001. */
308 RTZIPTARFORMAT_PAX,
309 /** CPIO format (portable ASCII foramt as defined by SuSV2). */
310 RTZIPTARFORMAT_CPIO_ASCII_SUSV2,
311 /** CPIO format (New ascii format). */
312 RTZIPTARFORMAT_CPIO_ASCII_NEW,
313 /** CPIO format (New ascii format with checksumming). */
314 RTZIPTARFORMAT_CPIO_ASCII_NEW_CHKSUM,
315 /** End of valid formats. */
316 RTZIPTARFORMAT_END,
317 /** Make sure the type is at least 32 bits wide. */
318 RTZIPTARFORMAT_32BIT_HACK = 0x7fffffff
319} RTZIPTARFORMAT;
320
321/**
322 * Opens a TAR filesystem stream for the purpose of create a new TAR archive.
323 *
324 * @returns IPRT status code.
325 *
326 * @param hVfsIosOut The output stream, i.e. where the tar stuff is
327 * written. The reference is not consumed, instead
328 * another one is retained.
329 * @param enmFormat The desired output format.
330 * @param fFlags RTZIPTAR_C_XXX, except RTZIPTAR_C_UPDATE.
331 * @param phVfsFss Where to return the handle to the TAR
332 * filesystem stream.
333 */
334RTDECL(int) RTZipTarFsStreamToIoStream(RTVFSIOSTREAM hVfsIosOut, RTZIPTARFORMAT enmFormat,
335 uint32_t fFlags, PRTVFSFSSTREAM phVfsFss);
336
337/** @name RTZIPTAR_C_XXX - TAR creation flags (RTZipTarFsStreamToIoStream).
338 * @{ */
339/** Check for sparse files.
340 * @note Only supported when adding file objects. The files will be read
341 * twice. */
342#define RTZIPTAR_C_SPARSE RT_BIT_32(0)
343/** Set if opening for updating. */
344#define RTZIPTAR_C_UPDATE RT_BIT_32(1)
345/** Valid bits. */
346#define RTZIPTAR_C_VALID_MASK UINT32_C(0x00000003)
347/** @} */
348
349/**
350 * Opens a TAR filesystem stream for the purpose of create a new TAR archive or
351 * updating an existing one.
352 *
353 * @returns IPRT status code.
354 *
355 * @param hVfsFile The TAR file handle, i.e. where the tar stuff is
356 * written and optionally read/update. The
357 * reference is not consumed, instead another one
358 * is retained.
359 * @param enmFormat The desired output format.
360 * @param fFlags RTZIPTAR_C_XXX.
361 * @param phVfsFss Where to return the handle to the TAR
362 * filesystem stream.
363 */
364RTDECL(int) RTZipTarFsStreamForFile(RTVFSFILE hVfsFile, RTZIPTARFORMAT enmFormat, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss);
365
366/**
367 * Set the owner to store the archive entries with.
368 *
369 * @returns IPRT status code.
370 * @param hVfsFss The handle to a TAR creator.
371 * @param uid The UID value to set. Passing NIL_RTUID makes
372 * it use the value found in RTFSOBJINFO.
373 * @param pszOwner The owner name to store. Passing NULL makes it
374 * use the value found in RTFSOBJINFO.
375 */
376RTDECL(int) RTZipTarFsStreamSetOwner(RTVFSFSSTREAM hVfsFss, RTUID uid, const char *pszOwner);
377
378/**
379 * Set the group to store the archive entries with.
380 *
381 * @returns IPRT status code.
382 * @param hVfsFss The handle to a TAR creator.
383 * @param gid The GID value to set. Passing NIL_RTUID makes
384 * it use the value found in RTFSOBJINFO.
385 * @param pszGroup The group name to store. Passing NULL makes it
386 * use the value found in RTFSOBJINFO.
387 */
388RTDECL(int) RTZipTarFsStreamSetGroup(RTVFSFSSTREAM hVfsFss, RTGID gid, const char *pszGroup);
389
390/**
391 * Set path prefix to store the archive entries with.
392 *
393 * @returns IPRT status code.
394 * @param hVfsFss The handle to a TAR creator.
395 * @param pszPrefix The path prefix to join the names with. Pass
396 * NULL for no prefix.
397 */
398RTDECL(int) RTZipTarFsStreamSetPrefix(RTVFSFSSTREAM hVfsFss, const char *pszPrefix);
399
400/**
401 * Set the AND and OR masks to apply to file (non-dir) modes in the archive.
402 *
403 * @returns IPRT status code.
404 * @param hVfsFss The handle to a TAR creator.
405 * @param fAndMode The bits to keep
406 * @param fOrMode The bits to set.
407 */
408RTDECL(int) RTZipTarFsStreamSetFileMode(RTVFSFSSTREAM hVfsFss, RTFMODE fAndMode, RTFMODE fOrMode);
409
410/**
411 * Set the AND and OR masks to apply to directory modes in the archive.
412 *
413 * @returns IPRT status code.
414 * @param hVfsFss The handle to a TAR creator.
415 * @param fAndMode The bits to keep
416 * @param fOrMode The bits to set.
417 */
418RTDECL(int) RTZipTarFsStreamSetDirMode(RTVFSFSSTREAM hVfsFss, RTFMODE fAndMode, RTFMODE fOrMode);
419
420/**
421 * Set the modification time to store the archive entires with.
422 *
423 * @returns IPRT status code.
424 * @param hVfsFss The handle to a TAR creator.
425 * @param pModificationTime The modification time to use. Pass NULL to use
426 * the value found in RTFSOBJINFO.
427 */
428RTDECL(int) RTZipTarFsStreamSetMTime(RTVFSFSSTREAM hVfsFss, PCRTTIMESPEC pModificationTime);
429
430/**
431 * Truncates a TAR creator stream in update mode.
432 *
433 * Use RTVfsFsStrmNext to examine the TAR stream and locate the cut-off point.
434 *
435 * After performing this call, the stream will be in write mode and
436 * RTVfsFsStrmNext will stop working (VERR_WRONG_ORDER). The RTVfsFsStrmAdd()
437 * and RTVfsFsStrmPushFile() can be used to add new object to the TAR file,
438 * starting at the trunction point. RTVfsFsStrmEnd() is used to finish the TAR
439 * file (this performs the actual file trunction).
440 *
441 * @returns IPRT status code.
442 * @param hVfsFss The handle to a TAR creator in update mode.
443 * @param hVfsObj Object returned by RTVfsFsStrmNext that the
444 * trunction is relative to. This doesn't have to
445 * be the current stream object, it can be an
446 * earlier one too.
447 * @param fAfter If set, @a hVfsObj will remain in the update TAR
448 * file. If clear, @a hVfsObj will not be
449 * included.
450 */
451RTDECL(int) RTZipTarFsStreamTruncate(RTVFSFSSTREAM hVfsFss, RTVFSOBJ hVfsObj, bool fAfter);
452
453/**
454 * A mini TAR program.
455 *
456 * @returns Program exit code.
457 *
458 * @param cArgs The number of arguments.
459 * @param papszArgs The argument vector. (Note that this may be
460 * reordered, so the memory must be writable.)
461 */
462RTDECL(RTEXITCODE) RTZipTarCmd(unsigned cArgs, char **papszArgs);
463
464/**
465 * Opens a ZIP filesystem stream.
466 *
467 * This is used to extract, list or check a ZIP archive.
468 *
469 * @returns IPRT status code.
470 *
471 * @param hVfsIosIn The compressed input stream. The reference is
472 * not consumed, instead another one is retained.
473 * @param fFlags Flags, MBZ.
474 * @param phVfsFss Where to return the handle to the TAR
475 * filesystem stream.
476 */
477RTDECL(int) RTZipPkzipFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss);
478
479/**
480 * A mini UNZIP program.
481 *
482 * @returns Program exit code.
483 * @
484 * @param cArgs The number of arguments.
485 * @param papszArgs The argument vector. (Note that this may be
486 * reordered, so the memory must be writable.)
487 */
488RTDECL(RTEXITCODE) RTZipUnzipCmd(unsigned cArgs, char **papszArgs);
489
490/**
491 * Helper for decompressing files of a ZIP file located in memory.
492 *
493 * @returns IPRT status code.
494 *
495 * @param ppvDst Where to store the pointer to the allocated
496 * buffer. To be freed with RTMemFree().
497 * @param pcbDst Where to store the pointer to the size of the
498 * allocated buffer.
499 * @param pvSrc Pointer to the buffer containing the .zip file.
500 * @param cbSrc Size of the buffer containing the .zip file.
501 * @param pszObject Name of the object to extract.
502 */
503RTDECL(int) RTZipPkzipMemDecompress(void **ppvDst, size_t *pcbDst, const void *pvSrc, size_t cbSrc, const char *pszObject);
504
505/**
506 * Opens a XAR filesystem stream.
507 *
508 * This is used to extract, list or check a XAR archive.
509 *
510 * @returns IPRT status code.
511 *
512 * @param hVfsIosIn The compressed input stream. The reference is
513 * not consumed, instead another one is retained.
514 * @param fFlags Flags, MBZ.
515 * @param phVfsFss Where to return the handle to the XAR filesystem
516 * stream.
517 */
518RTDECL(int) RTZipXarFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss);
519
520/**
521 * Opens a CPIO filesystem stream.
522 *
523 * This is used to extract, list or check a CPIO archive.
524 *
525 * @returns IPRT status code.
526 *
527 * @param hVfsIosIn The input stream. The reference is not
528 * consumed, instead another one is retained.
529 * @param fFlags Flags, MBZ.
530 * @param phVfsFss Where to return the handle to the CPIO
531 * filesystem stream.
532 */
533RTDECL(int) RTZipCpioFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn, uint32_t fFlags, PRTVFSFSSTREAM phVfsFss);
534
535/** @} */
536
537RT_C_DECLS_END
538
539#endif /* !IPRT_INCLUDED_zip_h */
540
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette