1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "config.h"
#include "internal.h"
static bool is_zero_block(const tar_header_t *hdr)
{
const unsigned char *ptr = (const unsigned char *)hdr;
return ptr[0] == '\0' && memcmp(ptr, ptr + 1, sizeof(*hdr) - 1) == 0;
}
static int check_version(const tar_header_t *hdr)
{
char buffer[sizeof(hdr->magic) + sizeof(hdr->version)];
memset(buffer, '\0', sizeof(buffer));
if (memcmp(hdr->magic, buffer, sizeof(hdr->magic)) == 0 &&
memcmp(hdr->version, buffer, sizeof(hdr->version)) == 0)
return ETV_V7_UNIX;
if (memcmp(hdr->magic, TAR_MAGIC, sizeof(hdr->magic)) == 0 &&
memcmp(hdr->version, TAR_VERSION, sizeof(hdr->version)) == 0)
return ETV_POSIX;
if (memcmp(hdr->magic, TAR_MAGIC_OLD, sizeof(hdr->magic)) == 0 &&
memcmp(hdr->version, TAR_VERSION_OLD, sizeof(hdr->version)) == 0)
return ETV_PRE_POSIX;
return ETV_UNKNOWN;
}
static char *record_to_memory(int fd, uint64_t size)
{
char *buffer = malloc(size + 1);
if (buffer == NULL)
goto fail_errno;
if (read_data("reading tar record", fd, buffer, size))
goto fail;
if (skip_padding(fd, size))
goto fail;
buffer[size] = '\0';
return buffer;
fail_errno:
perror("reading tar record");
goto fail;
fail:
free(buffer);
return NULL;
}
static tar_xattr_t *mkxattr(const char *key, size_t keylen,
const char *value, size_t valuelen)
{
tar_xattr_t *xattr;
xattr = calloc(1, sizeof(*xattr) + keylen + 1 + valuelen + 1);
if (xattr == NULL)
return NULL;
xattr->key = xattr->data;
xattr->value = xattr->data + keylen + 1;
memcpy(xattr->key, key, keylen);
memcpy(xattr->value, value, valuelen);
return xattr;
}
static int read_pax_header(int fd, uint64_t entsize, unsigned int *set_by_pax,
tar_header_decoded_t *out)
{
sparse_map_t *sparse_last = NULL, *sparse;
uint64_t field, offset = 0, num_bytes = 0;
char *buffer, *line, *key, *ptr, *value;
tar_xattr_t *xattr;
uint64_t i;
buffer = record_to_memory(fd, entsize);
if (buffer == NULL)
return -1;
for (i = 0; i < entsize; ++i) {
while (i < entsize && isspace(buffer[i]))
++i;
while (i < entsize && isdigit(buffer[i]))
++i;
while (i < entsize && isspace(buffer[i]))
++i;
if (i >= entsize)
break;
line = buffer + i;
while (i < entsize && buffer[i] != '\n')
++i;
buffer[i] = '\0';
if (!strncmp(line, "uid=", 4)) {
if (pax_read_decimal(line + 4, &field))
goto fail;
out->sb.st_uid = field;
*set_by_pax |= PAX_UID;
} else if (!strncmp(line, "gid=", 4)) {
if (pax_read_decimal(line + 4, &field))
goto fail;
out->sb.st_gid = field;
*set_by_pax |= PAX_GID;
} else if (!strncmp(line, "path=", 5)) {
free(out->name);
out->name = strdup(line + 5);
if (out->name == NULL)
goto fail_errno;
*set_by_pax |= PAX_NAME;
} else if (!strncmp(line, "size=", 5)) {
if (pax_read_decimal(line + 5, &out->record_size))
goto fail;
*set_by_pax |= PAX_SIZE;
} else if (!strncmp(line, "linkpath=", 9)) {
free(out->link_target);
out->link_target = strdup(line + 9);
if (out->link_target == NULL)
goto fail_errno;
*set_by_pax |= PAX_SLINK_TARGET;
} else if (!strncmp(line, "atime=", 6)) {
if (line[6] == '-') {
if (pax_read_decimal(line + 7, &field))
goto fail;
out->sb.st_atime = -((int64_t)field);
} else {
if (pax_read_decimal(line + 6, &field))
goto fail;
out->sb.st_atime = field;
}
*set_by_pax |= PAX_ATIME;
} else if (!strncmp(line, "mtime=", 6)) {
if (line[6] == '-') {
if (pax_read_decimal(line + 7, &field))
goto fail;
out->sb.st_mtime = -((int64_t)field);
} else {
if (pax_read_decimal(line + 6, &field))
goto fail;
out->sb.st_mtime = field;
}
*set_by_pax |= PAX_MTIME;
} else if (!strncmp(line, "ctime=", 6)) {
if (line[6] == '-') {
if (pax_read_decimal(line + 7, &field))
goto fail;
out->sb.st_ctime = -((int64_t)field);
} else {
if (pax_read_decimal(line + 6, &field))
goto fail;
out->sb.st_ctime = field;
}
*set_by_pax |= PAX_CTIME;
} else if (!strncmp(line, "GNU.sparse.name=", 16)) {
free(out->name);
out->name = strdup(line + 5);
if (out->name == NULL)
goto fail_errno;
*set_by_pax |= PAX_NAME;
} else if (!strncmp(line, "GNU.sparse.map=", 15)) {
free_sparse_list(out->sparse);
sparse_last = NULL;
out->sparse = read_sparse_map(line + 15);
if (out->sparse == NULL)
goto fail;
} else if (!strncmp(line, "GNU.sparse.size=", 16)) {
if (pax_read_decimal(line + 16, &out->actual_size))
goto fail;
*set_by_pax |= PAX_SPARSE_SIZE;
} else if (!strncmp(line, "GNU.sparse.offset=", 18)) {
if (pax_read_decimal(line + 18, &offset))
goto fail;
} else if (!strncmp(line, "GNU.sparse.numbytes=", 20)) {
if (pax_read_decimal(line + 20, &num_bytes))
goto fail;
sparse = calloc(1, sizeof(*sparse));
if (sparse == NULL)
goto fail_errno;
sparse->offset = offset;
sparse->count = num_bytes;
if (sparse_last == NULL) {
free_sparse_list(out->sparse);
out->sparse = sparse_last = sparse;
} else {
sparse_last->next = sparse;
sparse_last = sparse;
}
} else if (!strncmp(line, "SCHILY.xattr.", 13)) {
key = line + 13;
ptr = strrchr(key, '=');
if (ptr == NULL || ptr == key)
continue;
value = ptr + 1;
xattr = mkxattr(key, ptr - key, value, strlen(value));
if (xattr == NULL)
goto fail_errno;
xattr->next = out->xattr;
out->xattr = xattr;
} else if (!strncmp(line, "LIBARCHIVE.xattr.", 17)) {
key = line + 17;
ptr = strrchr(key, '=');
if (ptr == NULL || ptr == key)
continue;
value = ptr + 1;
xattr = mkxattr(key, ptr - key, value, strlen(value));
if (xattr == NULL)
goto fail_errno;
urldecode(xattr->key);
base64_decode((uint8_t *)xattr->value, value);
xattr->next = out->xattr;
out->xattr = xattr;
}
}
free(buffer);
return 0;
fail_errno:
perror("reading pax header");
goto fail;
fail:
free(buffer);
return -1;
}
static int decode_header(const tar_header_t *hdr, unsigned int set_by_pax,
tar_header_decoded_t *out, int version)
{
size_t len1, len2;
uint64_t field;
if (!(set_by_pax & PAX_NAME)) {
if (hdr->tail.posix.prefix[0] != '\0' &&
version == ETV_POSIX) {
len1 = strnlen(hdr->name, sizeof(hdr->name));
len2 = strnlen(hdr->tail.posix.prefix,
sizeof(hdr->tail.posix.prefix));
out->name = malloc(len1 + 1 + len2 + 1);
if (out->name != NULL) {
memcpy(out->name, hdr->tail.posix.prefix, len2);
out->name[len2] = '/';
memcpy(out->name + len2 + 1, hdr->name, len1);
out->name[len1 + 1 + len2] = '\0';
}
} else {
out->name = strndup(hdr->name, sizeof(hdr->name));
}
if (out->name == NULL) {
perror("decoding filename");
return -1;
}
}
if (!(set_by_pax & PAX_SIZE)) {
if (read_number(hdr->size, sizeof(hdr->size), &out->record_size))
return -1;
}
if (!(set_by_pax & PAX_UID)) {
if (read_number(hdr->uid, sizeof(hdr->uid), &field))
return -1;
out->sb.st_uid = field;
}
if (!(set_by_pax & PAX_GID)) {
if (read_number(hdr->gid, sizeof(hdr->gid), &field))
return -1;
out->sb.st_gid = field;
}
if (!(set_by_pax & PAX_DEV_MAJ)) {
if (read_number(hdr->devmajor, sizeof(hdr->devmajor), &field))
return -1;
out->sb.st_rdev = makedev(field, minor(out->sb.st_rdev));
}
if (!(set_by_pax & PAX_DEV_MIN)) {
if (read_number(hdr->devminor, sizeof(hdr->devminor), &field))
return -1;
out->sb.st_rdev = makedev(major(out->sb.st_rdev), field);
}
if (!(set_by_pax & PAX_MTIME)) {
if (read_number(hdr->mtime, sizeof(hdr->mtime), &field))
return -1;
if (field & 0x8000000000000000UL) {
field = ~field + 1;
out->sb.st_mtime = -((int64_t)field);
} else {
out->sb.st_mtime = field;
}
}
if (!(set_by_pax & PAX_ATIME)) {
field = out->sb.st_mtime;
if (version == ETV_PRE_POSIX &&
((uint8_t)hdr->tail.gnu.atime[0] == 0x80 ||
(uint8_t)hdr->tail.gnu.atime[0] == 0xFF ||
isdigit(hdr->tail.gnu.atime[0]))) {
if (read_number(hdr->tail.gnu.atime,
sizeof(hdr->tail.gnu.atime), &field))
return -1;
}
if (field & 0x8000000000000000UL) {
field = ~field + 1;
out->sb.st_atime = -((int64_t)field);
} else {
out->sb.st_atime = field;
}
}
if (!(set_by_pax & PAX_CTIME)) {
field = out->sb.st_mtime;
if (version == ETV_PRE_POSIX &&
((uint8_t)hdr->tail.gnu.ctime[0] == 0x80 ||
(uint8_t)hdr->tail.gnu.ctime[0] == 0xFF ||
isdigit(hdr->tail.gnu.ctime[0]))) {
if (read_number(hdr->tail.gnu.ctime,
sizeof(hdr->tail.gnu.atime), &field))
return -1;
}
if (field & 0x8000000000000000UL) {
field = ~field + 1;
out->sb.st_ctime = -((int64_t)field);
} else {
out->sb.st_ctime = field;
}
}
if (read_octal(hdr->mode, sizeof(hdr->mode), &field))
return -1;
out->sb.st_mode = field & 07777;
if (hdr->typeflag == TAR_TYPE_LINK ||
hdr->typeflag == TAR_TYPE_SLINK) {
if (!(set_by_pax & PAX_SLINK_TARGET)) {
out->link_target = strdup(hdr->linkname);
if (out->link_target == NULL) {
perror("decoding symlink target");
return -1;
}
}
}
out->unknown_record = false;
switch (hdr->typeflag) {
case '\0':
case TAR_TYPE_FILE:
case TAR_TYPE_GNU_SPARSE:
out->sb.st_mode |= S_IFREG;
break;
case TAR_TYPE_LINK:
/* XXX: hard links are not support yet */
out->sb.st_mode = S_IFLNK | 0777;
break;
case TAR_TYPE_SLINK:
out->sb.st_mode = S_IFLNK | 0777;
break;
case TAR_TYPE_CHARDEV:
out->sb.st_mode |= S_IFCHR;
break;
case TAR_TYPE_BLOCKDEV:
out->sb.st_mode |= S_IFBLK;
break;
case TAR_TYPE_DIR:
out->sb.st_mode |= S_IFDIR;
break;
case TAR_TYPE_FIFO:
out->sb.st_mode |= S_IFIFO;
break;
default:
out->unknown_record = true;
break;
}
return 0;
}
int read_header(int fd, tar_header_decoded_t *out)
{
unsigned int set_by_pax = 0;
bool prev_was_zero = false;
uint64_t pax_size;
tar_header_t hdr;
int version;
memset(out, 0, sizeof(*out));
for (;;) {
if (read_data("reading tar header", fd, &hdr, sizeof(hdr)))
goto fail;
if (is_zero_block(&hdr)) {
if (prev_was_zero)
goto out_eof;
prev_was_zero = true;
continue;
}
prev_was_zero = false;
version = check_version(&hdr);
if (version == ETV_UNKNOWN)
goto fail_magic;
if (!is_checksum_valid(&hdr))
goto fail_chksum;
switch (hdr.typeflag) {
case TAR_TYPE_GNU_SLINK:
if (read_number(hdr.size, sizeof(hdr.size), &pax_size))
goto fail;
free(out->link_target);
out->link_target = record_to_memory(fd, pax_size);
if (out->link_target == NULL)
goto fail;
set_by_pax |= PAX_SLINK_TARGET;
continue;
case TAR_TYPE_GNU_PATH:
if (read_number(hdr.size, sizeof(hdr.size), &pax_size))
goto fail;
free(out->name);
out->name = record_to_memory(fd, pax_size);
if (out->name == NULL)
goto fail;
set_by_pax |= PAX_NAME;
continue;
case TAR_TYPE_PAX:
clear_header(out);
if (read_number(hdr.size, sizeof(hdr.size), &pax_size))
goto fail;
set_by_pax = 0;
if (read_pax_header(fd, pax_size, &set_by_pax, out))
goto fail;
continue;
case TAR_TYPE_GNU_SPARSE:
free_sparse_list(out->sparse);
out->sparse = read_gnu_old_sparse(fd, &hdr);
if (out->sparse == NULL)
goto fail;
if (read_number(hdr.tail.gnu.realsize,
sizeof(hdr.tail.gnu.realsize),
&out->actual_size))
goto fail;
break;
}
break;
}
if (decode_header(&hdr, set_by_pax, out, version))
goto fail;
if (out->sparse != NULL) {
out->sb.st_size = out->actual_size;
} else {
out->sb.st_size = out->record_size;
out->actual_size = out->record_size;
}
return 0;
out_eof:
clear_header(out);
return 1;
fail_magic:
fputs("input is not a ustar tar archive!\n", stderr);
goto fail;
fail_chksum:
fputs("invalid tar header checksum!\n", stderr);
goto fail;
fail:
clear_header(out);
return -1;
}
|