2
* Copyright (c) 2003-2007 Tim Kientzle
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
14
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
#include "archive_platform.h"
27
__FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_cpio.c 201163 2009-12-29 05:50:34Z kientzle $");
32
/* #include <stdint.h> */ /* See archive_platform.h */
41
#include "archive_entry.h"
42
#include "archive_private.h"
43
#include "archive_read_private.h"
45
struct cpio_bin_header {
46
unsigned char c_magic[2];
47
unsigned char c_dev[2];
48
unsigned char c_ino[2];
49
unsigned char c_mode[2];
50
unsigned char c_uid[2];
51
unsigned char c_gid[2];
52
unsigned char c_nlink[2];
53
unsigned char c_rdev[2];
54
unsigned char c_mtime[4];
55
unsigned char c_namesize[2];
56
unsigned char c_filesize[4];
59
struct cpio_odc_header {
73
struct cpio_newc_header {
91
struct links_entry *next;
92
struct links_entry *previous;
99
#define CPIO_MAGIC 0x13141516
102
int (*read_header)(struct archive_read *, struct cpio *,
103
struct archive_entry *, size_t *, size_t *);
104
struct links_entry *links_head;
105
struct archive_string entry_name;
106
struct archive_string entry_linkname;
107
off_t entry_bytes_remaining;
112
static int64_t atol16(const char *, unsigned);
113
static int64_t atol8(const char *, unsigned);
114
static int archive_read_format_cpio_bid(struct archive_read *);
115
static int archive_read_format_cpio_cleanup(struct archive_read *);
116
static int archive_read_format_cpio_read_data(struct archive_read *,
117
const void **, size_t *, off_t *);
118
static int archive_read_format_cpio_read_header(struct archive_read *,
119
struct archive_entry *);
120
static int be4(const unsigned char *);
121
static int find_odc_header(struct archive_read *);
122
static int find_newc_header(struct archive_read *);
123
static int header_bin_be(struct archive_read *, struct cpio *,
124
struct archive_entry *, size_t *, size_t *);
125
static int header_bin_le(struct archive_read *, struct cpio *,
126
struct archive_entry *, size_t *, size_t *);
127
static int header_newc(struct archive_read *, struct cpio *,
128
struct archive_entry *, size_t *, size_t *);
129
static int header_odc(struct archive_read *, struct cpio *,
130
struct archive_entry *, size_t *, size_t *);
131
static int is_octal(const char *, size_t);
132
static int is_hex(const char *, size_t);
133
static int le4(const unsigned char *);
134
static void record_hardlink(struct cpio *cpio, struct archive_entry *entry);
137
archive_read_support_format_cpio(struct archive *_a)
139
struct archive_read *a = (struct archive_read *)_a;
143
cpio = (struct cpio *)malloc(sizeof(*cpio));
145
archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
146
return (ARCHIVE_FATAL);
148
memset(cpio, 0, sizeof(*cpio));
149
cpio->magic = CPIO_MAGIC;
151
r = __archive_read_register_format(a,
154
archive_read_format_cpio_bid,
156
archive_read_format_cpio_read_header,
157
archive_read_format_cpio_read_data,
159
archive_read_format_cpio_cleanup);
168
archive_read_format_cpio_bid(struct archive_read *a)
171
const unsigned char *p;
175
cpio = (struct cpio *)(a->format->data);
177
if ((h = __archive_read_ahead(a, 6, NULL)) == NULL)
180
p = (const unsigned char *)h;
182
if (memcmp(p, "070707", 6) == 0) {
183
/* ASCII cpio archive (odc, POSIX.1) */
184
cpio->read_header = header_odc;
187
* XXX TODO: More verification; Could check that only octal
188
* digits appear in appropriate header locations. XXX
190
} else if (memcmp(p, "070701", 6) == 0) {
191
/* ASCII cpio archive (SVR4 without CRC) */
192
cpio->read_header = header_newc;
195
* XXX TODO: More verification; Could check that only hex
196
* digits appear in appropriate header locations. XXX
198
} else if (memcmp(p, "070702", 6) == 0) {
199
/* ASCII cpio archive (SVR4 with CRC) */
200
/* XXX TODO: Flag that we should check the CRC. XXX */
201
cpio->read_header = header_newc;
204
* XXX TODO: More verification; Could check that only hex
205
* digits appear in appropriate header locations. XXX
207
} else if (p[0] * 256 + p[1] == 070707) {
208
/* big-endian binary cpio archives */
209
cpio->read_header = header_bin_be;
211
/* Is more verification possible here? */
212
} else if (p[0] + p[1] * 256 == 070707) {
213
/* little-endian binary cpio archives */
214
cpio->read_header = header_bin_le;
216
/* Is more verification possible here? */
218
return (ARCHIVE_WARN);
224
archive_read_format_cpio_read_header(struct archive_read *a,
225
struct archive_entry *entry)
233
cpio = (struct cpio *)(a->format->data);
234
r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
236
if (r < ARCHIVE_WARN)
239
/* Read name from buffer. */
240
h = __archive_read_ahead(a, namelength + name_pad, NULL);
242
return (ARCHIVE_FATAL);
243
__archive_read_consume(a, namelength + name_pad);
244
archive_strncpy(&cpio->entry_name, (const char *)h, namelength);
245
archive_entry_set_pathname(entry, cpio->entry_name.s);
246
cpio->entry_offset = 0;
248
/* If this is a symlink, read the link contents. */
249
if (archive_entry_filetype(entry) == AE_IFLNK) {
250
h = __archive_read_ahead(a, cpio->entry_bytes_remaining, NULL);
252
return (ARCHIVE_FATAL);
253
__archive_read_consume(a, cpio->entry_bytes_remaining);
254
archive_strncpy(&cpio->entry_linkname, (const char *)h,
255
cpio->entry_bytes_remaining);
256
archive_entry_set_symlink(entry, cpio->entry_linkname.s);
257
cpio->entry_bytes_remaining = 0;
260
/* XXX TODO: If the full mode is 0160200, then this is a Solaris
261
* ACL description for the following entry. Read this body
262
* and parse it as a Solaris-style ACL, then read the next
265
/* Compare name to "TRAILER!!!" to test for end-of-archive. */
266
if (namelength == 11 && strcmp((const char *)h, "TRAILER!!!") == 0) {
267
/* TODO: Store file location of start of block. */
268
archive_set_error(&a->archive, 0, NULL);
269
return (ARCHIVE_EOF);
272
/* Detect and record hardlinks to previously-extracted entries. */
273
record_hardlink(cpio, entry);
279
archive_read_format_cpio_read_data(struct archive_read *a,
280
const void **buff, size_t *size, off_t *offset)
285
cpio = (struct cpio *)(a->format->data);
286
if (cpio->entry_bytes_remaining > 0) {
287
*buff = __archive_read_ahead(a, 1, &bytes_read);
289
return (ARCHIVE_FATAL);
290
if (bytes_read > cpio->entry_bytes_remaining)
291
bytes_read = cpio->entry_bytes_remaining;
293
*offset = cpio->entry_offset;
294
cpio->entry_offset += bytes_read;
295
cpio->entry_bytes_remaining -= bytes_read;
296
__archive_read_consume(a, bytes_read);
299
while (cpio->entry_padding > 0) {
300
*buff = __archive_read_ahead(a, 1, &bytes_read);
302
return (ARCHIVE_FATAL);
303
if (bytes_read > cpio->entry_padding)
304
bytes_read = cpio->entry_padding;
305
__archive_read_consume(a, bytes_read);
306
cpio->entry_padding -= bytes_read;
310
*offset = cpio->entry_offset;
311
return (ARCHIVE_EOF);
316
* Skip forward to the next cpio newc header by searching for the
317
* 07070[12] string. This should be generalized and merged with
318
* find_odc_header below.
321
is_hex(const char *p, size_t len)
324
if ((*p >= '0' && *p <= '9')
325
|| (*p >= 'a' && *p <= 'f')
326
|| (*p >= 'A' && *p <= 'F'))
335
find_newc_header(struct archive_read *a)
339
size_t skip, skipped = 0;
343
h = __archive_read_ahead(a, sizeof(struct cpio_newc_header), &bytes);
345
return (ARCHIVE_FATAL);
349
/* Try the typical case first, then go into the slow search.*/
350
if (memcmp("07070", p, 5) == 0
351
&& (p[5] == '1' || p[5] == '2')
352
&& is_hex(p, sizeof(struct cpio_newc_header)))
356
* Scan ahead until we find something that looks
357
* like an odc header.
359
while (p + sizeof(struct cpio_newc_header) <= q) {
363
if (memcmp("07070", p, 5) == 0
364
&& is_hex(p, sizeof(struct cpio_newc_header))) {
365
skip = p - (const char *)h;
366
__archive_read_consume(a, skip);
369
archive_set_error(&a->archive,
371
"Skipped %d bytes before "
372
"finding valid header",
374
return (ARCHIVE_WARN);
388
skip = p - (const char *)h;
389
__archive_read_consume(a, skip);
395
header_newc(struct archive_read *a, struct cpio *cpio,
396
struct archive_entry *entry, size_t *namelength, size_t *name_pad)
399
const struct cpio_newc_header *header;
402
r = find_newc_header(a);
403
if (r < ARCHIVE_WARN)
406
/* Read fixed-size portion of header. */
407
h = __archive_read_ahead(a, sizeof(struct cpio_newc_header), NULL);
409
return (ARCHIVE_FATAL);
410
__archive_read_consume(a, sizeof(struct cpio_newc_header));
412
/* Parse out hex fields. */
413
header = (const struct cpio_newc_header *)h;
415
if (memcmp(header->c_magic, "070701", 6) == 0) {
416
a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
417
a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
418
} else if (memcmp(header->c_magic, "070702", 6) == 0) {
419
a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
420
a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
422
/* TODO: Abort here? */
425
archive_entry_set_devmajor(entry, atol16(header->c_devmajor, sizeof(header->c_devmajor)));
426
archive_entry_set_devminor(entry, atol16(header->c_devminor, sizeof(header->c_devminor)));
427
archive_entry_set_ino(entry, atol16(header->c_ino, sizeof(header->c_ino)));
428
archive_entry_set_mode(entry, atol16(header->c_mode, sizeof(header->c_mode)));
429
archive_entry_set_uid(entry, atol16(header->c_uid, sizeof(header->c_uid)));
430
archive_entry_set_gid(entry, atol16(header->c_gid, sizeof(header->c_gid)));
431
archive_entry_set_nlink(entry, atol16(header->c_nlink, sizeof(header->c_nlink)));
432
archive_entry_set_rdevmajor(entry, atol16(header->c_rdevmajor, sizeof(header->c_rdevmajor)));
433
archive_entry_set_rdevminor(entry, atol16(header->c_rdevminor, sizeof(header->c_rdevminor)));
434
archive_entry_set_mtime(entry, atol16(header->c_mtime, sizeof(header->c_mtime)), 0);
435
*namelength = atol16(header->c_namesize, sizeof(header->c_namesize));
436
/* Pad name to 2 more than a multiple of 4. */
437
*name_pad = (2 - *namelength) & 3;
440
* Note: entry_bytes_remaining is at least 64 bits and
441
* therefore guaranteed to be big enough for a 33-bit file
444
cpio->entry_bytes_remaining =
445
atol16(header->c_filesize, sizeof(header->c_filesize));
446
archive_entry_set_size(entry, cpio->entry_bytes_remaining);
447
/* Pad file contents to a multiple of 4. */
448
cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
453
* Skip forward to the next cpio odc header by searching for the
454
* 070707 string. This is a hand-optimized search that could
455
* probably be easily generalized to handle all character-based
459
is_octal(const char *p, size_t len)
462
if (*p < '0' || *p > '7')
470
find_odc_header(struct archive_read *a)
474
size_t skip, skipped = 0;
478
h = __archive_read_ahead(a, sizeof(struct cpio_odc_header), &bytes);
480
return (ARCHIVE_FATAL);
484
/* Try the typical case first, then go into the slow search.*/
485
if (memcmp("070707", p, 6) == 0
486
&& is_octal(p, sizeof(struct cpio_odc_header)))
490
* Scan ahead until we find something that looks
491
* like an odc header.
493
while (p + sizeof(struct cpio_odc_header) <= q) {
496
if (memcmp("070707", p, 6) == 0
497
&& is_octal(p, sizeof(struct cpio_odc_header))) {
498
skip = p - (const char *)h;
499
__archive_read_consume(a, skip);
502
archive_set_error(&a->archive,
504
"Skipped %d bytes before "
505
"finding valid header",
507
return (ARCHIVE_WARN);
521
skip = p - (const char *)h;
522
__archive_read_consume(a, skip);
528
header_odc(struct archive_read *a, struct cpio *cpio,
529
struct archive_entry *entry, size_t *namelength, size_t *name_pad)
533
const struct cpio_odc_header *header;
535
a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
536
a->archive.archive_format_name = "POSIX octet-oriented cpio";
538
/* Find the start of the next header. */
539
r = find_odc_header(a);
540
if (r < ARCHIVE_WARN)
543
/* Read fixed-size portion of header. */
544
h = __archive_read_ahead(a, sizeof(struct cpio_odc_header), NULL);
546
return (ARCHIVE_FATAL);
547
__archive_read_consume(a, sizeof(struct cpio_odc_header));
549
/* Parse out octal fields. */
550
header = (const struct cpio_odc_header *)h;
552
archive_entry_set_dev(entry, atol8(header->c_dev, sizeof(header->c_dev)));
553
archive_entry_set_ino(entry, atol8(header->c_ino, sizeof(header->c_ino)));
554
archive_entry_set_mode(entry, atol8(header->c_mode, sizeof(header->c_mode)));
555
archive_entry_set_uid(entry, atol8(header->c_uid, sizeof(header->c_uid)));
556
archive_entry_set_gid(entry, atol8(header->c_gid, sizeof(header->c_gid)));
557
archive_entry_set_nlink(entry, atol8(header->c_nlink, sizeof(header->c_nlink)));
558
archive_entry_set_rdev(entry, atol8(header->c_rdev, sizeof(header->c_rdev)));
559
archive_entry_set_mtime(entry, atol8(header->c_mtime, sizeof(header->c_mtime)), 0);
560
*namelength = atol8(header->c_namesize, sizeof(header->c_namesize));
561
*name_pad = 0; /* No padding of filename. */
564
* Note: entry_bytes_remaining is at least 64 bits and
565
* therefore guaranteed to be big enough for a 33-bit file
568
cpio->entry_bytes_remaining =
569
atol8(header->c_filesize, sizeof(header->c_filesize));
570
archive_entry_set_size(entry, cpio->entry_bytes_remaining);
571
cpio->entry_padding = 0;
576
header_bin_le(struct archive_read *a, struct cpio *cpio,
577
struct archive_entry *entry, size_t *namelength, size_t *name_pad)
580
const struct cpio_bin_header *header;
582
a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
583
a->archive.archive_format_name = "cpio (little-endian binary)";
585
/* Read fixed-size portion of header. */
586
h = __archive_read_ahead(a, sizeof(struct cpio_bin_header), NULL);
588
return (ARCHIVE_FATAL);
589
__archive_read_consume(a, sizeof(struct cpio_bin_header));
591
/* Parse out binary fields. */
592
header = (const struct cpio_bin_header *)h;
594
archive_entry_set_dev(entry, header->c_dev[0] + header->c_dev[1] * 256);
595
archive_entry_set_ino(entry, header->c_ino[0] + header->c_ino[1] * 256);
596
archive_entry_set_mode(entry, header->c_mode[0] + header->c_mode[1] * 256);
597
archive_entry_set_uid(entry, header->c_uid[0] + header->c_uid[1] * 256);
598
archive_entry_set_gid(entry, header->c_gid[0] + header->c_gid[1] * 256);
599
archive_entry_set_nlink(entry, header->c_nlink[0] + header->c_nlink[1] * 256);
600
archive_entry_set_rdev(entry, header->c_rdev[0] + header->c_rdev[1] * 256);
601
archive_entry_set_mtime(entry, le4(header->c_mtime), 0);
602
*namelength = header->c_namesize[0] + header->c_namesize[1] * 256;
603
*name_pad = *namelength & 1; /* Pad to even. */
605
cpio->entry_bytes_remaining = le4(header->c_filesize);
606
archive_entry_set_size(entry, cpio->entry_bytes_remaining);
607
cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
612
header_bin_be(struct archive_read *a, struct cpio *cpio,
613
struct archive_entry *entry, size_t *namelength, size_t *name_pad)
616
const struct cpio_bin_header *header;
618
a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
619
a->archive.archive_format_name = "cpio (big-endian binary)";
621
/* Read fixed-size portion of header. */
622
h = __archive_read_ahead(a, sizeof(struct cpio_bin_header), NULL);
624
return (ARCHIVE_FATAL);
625
__archive_read_consume(a, sizeof(struct cpio_bin_header));
627
/* Parse out binary fields. */
628
header = (const struct cpio_bin_header *)h;
629
archive_entry_set_dev(entry, header->c_dev[0] * 256 + header->c_dev[1]);
630
archive_entry_set_ino(entry, header->c_ino[0] * 256 + header->c_ino[1]);
631
archive_entry_set_mode(entry, header->c_mode[0] * 256 + header->c_mode[1]);
632
archive_entry_set_uid(entry, header->c_uid[0] * 256 + header->c_uid[1]);
633
archive_entry_set_gid(entry, header->c_gid[0] * 256 + header->c_gid[1]);
634
archive_entry_set_nlink(entry, header->c_nlink[0] * 256 + header->c_nlink[1]);
635
archive_entry_set_rdev(entry, header->c_rdev[0] * 256 + header->c_rdev[1]);
636
archive_entry_set_mtime(entry, be4(header->c_mtime), 0);
637
*namelength = header->c_namesize[0] * 256 + header->c_namesize[1];
638
*name_pad = *namelength & 1; /* Pad to even. */
640
cpio->entry_bytes_remaining = be4(header->c_filesize);
641
archive_entry_set_size(entry, cpio->entry_bytes_remaining);
642
cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
647
archive_read_format_cpio_cleanup(struct archive_read *a)
651
cpio = (struct cpio *)(a->format->data);
652
/* Free inode->name map */
653
while (cpio->links_head != NULL) {
654
struct links_entry *lp = cpio->links_head->next;
656
if (cpio->links_head->name)
657
free(cpio->links_head->name);
658
free(cpio->links_head);
659
cpio->links_head = lp;
661
archive_string_free(&cpio->entry_name);
663
(a->format->data) = NULL;
668
le4(const unsigned char *p)
670
return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
675
be4(const unsigned char *p)
677
return ((p[0]<<24) + (p[1]<<16) + (p[2]<<8) + (p[3]));
681
* Note that this implementation does not (and should not!) obey
682
* locale settings; you cannot simply substitute strtol here, since
683
* it does obey locale.
686
atol8(const char *p, unsigned char_cnt)
692
while (char_cnt-- > 0) {
693
if (*p >= '0' && *p <= '7')
705
atol16(const char *p, unsigned char_cnt)
711
while (char_cnt-- > 0) {
712
if (*p >= 'a' && *p <= 'f')
713
digit = *p - 'a' + 10;
714
else if (*p >= 'A' && *p <= 'F')
715
digit = *p - 'A' + 10;
716
else if (*p >= '0' && *p <= '9')
728
record_hardlink(struct cpio *cpio, struct archive_entry *entry)
730
struct links_entry *le;
734
if (archive_entry_nlink(entry) <= 1)
737
dev = archive_entry_dev(entry);
738
ino = archive_entry_ino64(entry);
741
* First look in the list of multiply-linked files. If we've
742
* already dumped it, convert this entry to a hard link entry.
744
for (le = cpio->links_head; le; le = le->next) {
745
if (le->dev == dev && le->ino == ino) {
746
archive_entry_copy_hardlink(entry, le->name);
748
if (--le->links <= 0) {
749
if (le->previous != NULL)
750
le->previous->next = le->next;
751
if (le->next != NULL)
752
le->next->previous = le->previous;
753
if (cpio->links_head == le)
754
cpio->links_head = le->next;
763
le = (struct links_entry *)malloc(sizeof(struct links_entry));
765
__archive_errx(1, "Out of memory adding file to list");
766
if (cpio->links_head != NULL)
767
cpio->links_head->previous = le;
768
le->next = cpio->links_head;
770
cpio->links_head = le;
773
le->links = archive_entry_nlink(entry) - 1;
774
le->name = strdup(archive_entry_pathname(entry));
775
if (le->name == NULL)
776
__archive_errx(1, "Out of memory adding file to list");