VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/zip/tar.h@ 73041

Last change on this file since 73041 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 KB
Line 
1/* $Id: tar.h 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * IPRT - TAR Virtual Filesystem.
4 */
5
6/*
7 * Copyright (C) 2010-2017 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#ifndef __common_zip_tar_h
28#define __common_zip_tar_h
29
30#include <iprt/assert.h>
31
32/** @name RTZIPTARHDRPOSIX::typeflag
33 * @{ */
34#define RTZIPTAR_TF_OLDNORMAL '\0' /**< Normal disk file, Unix compatible */
35#define RTZIPTAR_TF_NORMAL '0' /**< Normal disk file */
36#define RTZIPTAR_TF_LINK '1' /**< Link to previously dumped file */
37#define RTZIPTAR_TF_SYMLINK '2' /**< Symbolic link */
38#define RTZIPTAR_TF_CHR '3' /**< Character special file */
39#define RTZIPTAR_TF_BLK '4' /**< Block special file */
40#define RTZIPTAR_TF_DIR '5' /**< Directory */
41#define RTZIPTAR_TF_FIFO '6' /**< FIFO special file */
42#define RTZIPTAR_TF_CONTIG '7' /**< Contiguous file */
43
44#define RTZIPTAR_TF_X_HDR 'x' /**< Extended header. */
45#define RTZIPTAR_TF_X_GLOBAL 'g' /**< Global extended header. */
46
47#define RTZIPTAR_TF_SOLARIS_XHDR 'X'
48
49#define RTZIPTAR_TF_GNU_DUMPDIR 'D'
50#define RTZIPTAR_TF_GNU_LONGLINK 'K' /**< GNU long link header. */
51#define RTZIPTAR_TF_GNU_LONGNAME 'L' /**< GNU long name header. */
52#define RTZIPTAR_TF_GNU_MULTIVOL 'M'
53#define RTZIPTAR_TF_GNU_SPARSE 'S'
54#define RTZIPTAR_TF_GNU_VOLDHR 'V'
55/** @} */
56
57
58/**
59 * The ancient tar header.
60 *
61 * The posix and gnu headers are compatible with the members up to and including
62 * link name, from there on they differ.
63 */
64typedef struct RTZIPTARHDRANCIENT
65{
66 char name[100];
67 char mode[8];
68 char uid[8];
69 char gid[8];
70 char size[12];
71 char mtime[12];
72 char chksum[8];
73 char typeflag;
74 char linkname[100]; /**< Was called linkflag. */
75 char unused[8+64+16+155+12];
76} RTZIPTARHDRANCIENT;
77AssertCompileSize(RTZIPTARHDRANCIENT, 512);
78AssertCompileMemberOffset(RTZIPTARHDRANCIENT, name, 0);
79AssertCompileMemberOffset(RTZIPTARHDRANCIENT, mode, 100);
80AssertCompileMemberOffset(RTZIPTARHDRANCIENT, uid, 108);
81AssertCompileMemberOffset(RTZIPTARHDRANCIENT, gid, 116);
82AssertCompileMemberOffset(RTZIPTARHDRANCIENT, size, 124);
83AssertCompileMemberOffset(RTZIPTARHDRANCIENT, mtime, 136);
84AssertCompileMemberOffset(RTZIPTARHDRANCIENT, chksum, 148);
85AssertCompileMemberOffset(RTZIPTARHDRANCIENT, typeflag, 156);
86AssertCompileMemberOffset(RTZIPTARHDRANCIENT, linkname, 157);
87AssertCompileMemberOffset(RTZIPTARHDRANCIENT, unused, 257);
88
89
90/** The uniform standard tape archive format magic value. */
91#define RTZIPTAR_USTAR_MAGIC "ustar"
92/** The ustar version string.
93 * @remarks The terminator character is not part of the field. */
94#define RTZIPTAR_USTAR_VERSION "00"
95
96/** The GNU magic + version value. */
97#define RTZIPTAR_GNU_MAGIC "ustar "
98
99
100/**
101 * The posix header (according to SuS).
102 */
103typedef struct RTZIPTARHDRPOSIX
104{
105 char name[100];
106 char mode[8];
107 char uid[8];
108 char gid[8];
109 char size[12];
110 char mtime[12];
111 char chksum[8];
112 char typeflag;
113 char linkname[100];
114 char magic[6];
115 char version[2];
116 char uname[32];
117 char gname[32];
118 char devmajor[8];
119 char devminor[8];
120 char prefix[155];
121 char unused[12];
122} RTZIPTARHDRPOSIX;
123AssertCompileSize(RTZIPTARHDRPOSIX, 512);
124AssertCompileMemberOffset(RTZIPTARHDRPOSIX, name, 0);
125AssertCompileMemberOffset(RTZIPTARHDRPOSIX, mode, 100);
126AssertCompileMemberOffset(RTZIPTARHDRPOSIX, uid, 108);
127AssertCompileMemberOffset(RTZIPTARHDRPOSIX, gid, 116);
128AssertCompileMemberOffset(RTZIPTARHDRPOSIX, size, 124);
129AssertCompileMemberOffset(RTZIPTARHDRPOSIX, mtime, 136);
130AssertCompileMemberOffset(RTZIPTARHDRPOSIX, chksum, 148);
131AssertCompileMemberOffset(RTZIPTARHDRPOSIX, typeflag, 156);
132AssertCompileMemberOffset(RTZIPTARHDRPOSIX, linkname, 157);
133AssertCompileMemberOffset(RTZIPTARHDRPOSIX, magic, 257);
134AssertCompileMemberOffset(RTZIPTARHDRPOSIX, version, 263);
135AssertCompileMemberOffset(RTZIPTARHDRPOSIX, uname, 265);
136AssertCompileMemberOffset(RTZIPTARHDRPOSIX, gname, 297);
137AssertCompileMemberOffset(RTZIPTARHDRPOSIX, devmajor, 329);
138AssertCompileMemberOffset(RTZIPTARHDRPOSIX, devminor, 337);
139AssertCompileMemberOffset(RTZIPTARHDRPOSIX, prefix, 345);
140
141/**
142 * GNU sparse data segment descriptor.
143 */
144typedef struct RTZIPTARGNUSPARSE
145{
146 char offset[12]; /**< Absolute offset relative to the start of the file. */
147 char numbytes[12];
148} RTZIPTARGNUSPARSE;
149AssertCompileSize(RTZIPTARGNUSPARSE, 24);
150AssertCompileMemberOffset(RTZIPTARGNUSPARSE, offset, 0);
151AssertCompileMemberOffset(RTZIPTARGNUSPARSE, numbytes, 12);
152/** Pointer to a GNU sparse data segment descriptor. */
153typedef RTZIPTARGNUSPARSE *PRTZIPTARGNUSPARSE;
154/** Pointer to a const GNU sparse data segment descriptor. */
155typedef RTZIPTARGNUSPARSE *PCRTZIPTARGNUSPARSE;
156
157/**
158 * The GNU header.
159 */
160typedef struct RTZIPTARHDRGNU
161{
162 char name[100];
163 char mode[8];
164 char uid[8];
165 char gid[8];
166 char size[12];
167 char mtime[12];
168 char chksum[8];
169 char typeflag;
170 char linkname[100];
171 char magic[8];
172 char uname[32];
173 char gname[32];
174 char devmajor[8];
175 char devminor[8];
176 char atime[12];
177 char ctime[12];
178 char offset[12]; /**< for multi-volume? */
179 char longnames[4]; /**< Seems to be unused. */
180 char unused[1];
181 RTZIPTARGNUSPARSE sparse[4];
182 char isextended; /**< More headers about sparse stuff if binary value 1. */
183 char realsize[12];
184 char unused2[17];
185} RTZIPTARHDRGNU;
186AssertCompileSize(RTZIPTARHDRGNU, 512);
187AssertCompileMemberOffset(RTZIPTARHDRGNU, name, 0);
188AssertCompileMemberOffset(RTZIPTARHDRGNU, mode, 100);
189AssertCompileMemberOffset(RTZIPTARHDRGNU, uid, 108);
190AssertCompileMemberOffset(RTZIPTARHDRGNU, gid, 116);
191AssertCompileMemberOffset(RTZIPTARHDRGNU, size, 124);
192AssertCompileMemberOffset(RTZIPTARHDRGNU, mtime, 136);
193AssertCompileMemberOffset(RTZIPTARHDRGNU, chksum, 148);
194AssertCompileMemberOffset(RTZIPTARHDRGNU, typeflag, 156);
195AssertCompileMemberOffset(RTZIPTARHDRGNU, linkname, 157);
196AssertCompileMemberOffset(RTZIPTARHDRGNU, magic, 257);
197AssertCompileMemberOffset(RTZIPTARHDRGNU, uname, 265);
198AssertCompileMemberOffset(RTZIPTARHDRGNU, gname, 297);
199AssertCompileMemberOffset(RTZIPTARHDRGNU, devmajor, 329);
200AssertCompileMemberOffset(RTZIPTARHDRGNU, devminor, 337);
201AssertCompileMemberOffset(RTZIPTARHDRGNU, atime, 345);
202AssertCompileMemberOffset(RTZIPTARHDRGNU, ctime, 357);
203AssertCompileMemberOffset(RTZIPTARHDRGNU, offset, 369);
204AssertCompileMemberOffset(RTZIPTARHDRGNU, longnames, 381);
205AssertCompileMemberOffset(RTZIPTARHDRGNU, unused, 385);
206AssertCompileMemberOffset(RTZIPTARHDRGNU, sparse, 386);
207AssertCompileMemberOffset(RTZIPTARHDRGNU, isextended,482);
208AssertCompileMemberOffset(RTZIPTARHDRGNU, realsize, 483);
209AssertCompileMemberOffset(RTZIPTARHDRGNU, unused2, 495);
210
211
212/**
213 * GNU sparse header.
214 */
215typedef struct RTZIPTARHDRGNUSPARSE
216{
217 RTZIPTARGNUSPARSE sp[21];
218 char isextended;
219 char unused[7];
220} RTZIPTARHDRGNUSPARSE;
221AssertCompileSize(RTZIPTARHDRGNUSPARSE, 512);
222AssertCompileMemberOffset(RTZIPTARHDRGNUSPARSE, sp, 0);
223AssertCompileMemberOffset(RTZIPTARHDRGNUSPARSE, isextended, 504);
224AssertCompileMemberOffset(RTZIPTARHDRGNUSPARSE, unused, 505);
225
226
227/**
228 * The bits common to posix and GNU.
229 */
230typedef struct RTZIPTARHDRCOMMON
231{
232 char name[100];
233 char mode[8];
234 char uid[8];
235 char gid[8];
236 char size[12];
237 char mtime[12];
238 char chksum[8];
239 char typeflag;
240 char linkname[100];
241 char magic[6];
242 char version[2];
243 char uname[32];
244 char gname[32];
245 char devmajor[8];
246 char devminor[8];
247 char not_common[155+12];
248} RTZIPTARHDRCOMMON;
249AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, name, RTZIPTARHDRPOSIX, name);
250AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, mode, RTZIPTARHDRPOSIX, mode);
251AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, uid, RTZIPTARHDRPOSIX, uid);
252AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, gid, RTZIPTARHDRPOSIX, gid);
253AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, size, RTZIPTARHDRPOSIX, size);
254AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, mtime, RTZIPTARHDRPOSIX, mtime);
255AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, chksum, RTZIPTARHDRPOSIX, chksum);
256AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, typeflag, RTZIPTARHDRPOSIX, typeflag);
257AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, linkname, RTZIPTARHDRPOSIX, linkname);
258AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, magic, RTZIPTARHDRPOSIX, magic);
259AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, version, RTZIPTARHDRPOSIX, version);
260AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, uname, RTZIPTARHDRPOSIX, uname);
261AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, gname, RTZIPTARHDRPOSIX, gname);
262AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, devmajor, RTZIPTARHDRPOSIX, devmajor);
263AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, devminor, RTZIPTARHDRPOSIX, devminor);
264
265AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, name, RTZIPTARHDRGNU, name);
266AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, mode, RTZIPTARHDRGNU, mode);
267AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, uid, RTZIPTARHDRGNU, uid);
268AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, gid, RTZIPTARHDRGNU, gid);
269AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, size, RTZIPTARHDRGNU, size);
270AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, mtime, RTZIPTARHDRGNU, mtime);
271AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, chksum, RTZIPTARHDRGNU, chksum);
272AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, typeflag, RTZIPTARHDRGNU, typeflag);
273AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, linkname, RTZIPTARHDRGNU, linkname);
274AssertCompileMembersAtSameOffset( RTZIPTARHDRCOMMON, magic, RTZIPTARHDRGNU, magic);
275AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, uname, RTZIPTARHDRGNU, uname);
276AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, gname, RTZIPTARHDRGNU, gname);
277AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, devmajor, RTZIPTARHDRGNU, devmajor);
278AssertCompileMembersSameSizeAndOffset(RTZIPTARHDRCOMMON, devminor, RTZIPTARHDRGNU, devminor);
279
280
281
282/**
283 * Tar header union.
284 */
285typedef union RTZIPTARHDR
286{
287 /** Byte view. */
288 char ab[512];
289 /** The standard header. */
290 RTZIPTARHDRANCIENT Ancient;
291 /** The standard header. */
292 RTZIPTARHDRPOSIX Posix;
293 /** The GNU header. */
294 RTZIPTARHDRGNU Gnu;
295 /** The bits common to both GNU and the standard header. */
296 RTZIPTARHDRCOMMON Common;
297 /** GNU sparse header. */
298 RTZIPTARHDRGNUSPARSE GnuSparse;
299} RTZIPTARHDR;
300AssertCompileSize(RTZIPTARHDR, 512);
301/** Pointer to a tar file header. */
302typedef RTZIPTARHDR *PRTZIPTARHDR;
303/** Pointer to a const tar file header. */
304typedef RTZIPTARHDR const *PCRTZIPTARHDR;
305
306
307/**
308 * Tar header type.
309 */
310typedef enum RTZIPTARTYPE
311{
312 /** Invalid type value. */
313 RTZIPTARTYPE_INVALID = 0,
314 /** Posix header. */
315 RTZIPTARTYPE_POSIX,
316 /** The old GNU header, has layout conflicting with posix. */
317 RTZIPTARTYPE_GNU,
318 /** Ancient tar header which does not use anything beyond the magic. */
319 RTZIPTARTYPE_ANCIENT,
320 /** End of the valid type values (this is not valid). */
321 RTZIPTARTYPE_END,
322 /** The usual type blow up. */
323 RTZIPTARTYPE_32BIT_HACK = 0x7fffffff
324} RTZIPTARTYPE;
325typedef RTZIPTARTYPE *PRTZIPTARTYPE;
326
327
328/**
329 * Calculates the TAR header checksums and detects if it's all zeros.
330 *
331 * @returns true if all zeros, false if not.
332 * @param pHdr The header to checksum.
333 * @param pi32Unsigned Where to store the checksum calculated using
334 * unsigned chars. This is the one POSIX specifies.
335 * @param pi32Signed Where to store the checksum calculated using
336 * signed chars.
337 *
338 * @remarks The reason why we calculate the checksum as both signed and unsigned
339 * has to do with various the char C type being signed on some hosts
340 * and unsigned on others.
341 */
342DECLINLINE(bool) rtZipTarCalcChkSum(PCRTZIPTARHDR pHdr, int32_t *pi32Unsigned, int32_t *pi32Signed)
343{
344 int32_t i32Unsigned = 0;
345 int32_t i32Signed = 0;
346
347 /*
348 * Sum up the entire header.
349 */
350 const char *pch = (const char *)pHdr;
351 const char *pchEnd = pch + sizeof(*pHdr);
352 do
353 {
354 i32Unsigned += *(unsigned char *)pch;
355 i32Signed += *(signed char *)pch;
356 } while (++pch != pchEnd);
357
358 /*
359 * Check if it's all zeros and replace the chksum field with spaces.
360 */
361 bool const fZeroHdr = i32Unsigned == 0;
362
363 pch = pHdr->Common.chksum;
364 pchEnd = pch + sizeof(pHdr->Common.chksum);
365 do
366 {
367 i32Unsigned -= *(unsigned char *)pch;
368 i32Signed -= *(signed char *)pch;
369 } while (++pch != pchEnd);
370
371 i32Unsigned += (unsigned char)' ' * sizeof(pHdr->Common.chksum);
372 i32Signed += (signed char)' ' * sizeof(pHdr->Common.chksum);
373
374 *pi32Unsigned = i32Unsigned;
375 if (pi32Signed)
376 *pi32Signed = i32Signed;
377 return fZeroHdr;
378}
379
380
381#endif
382
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