2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2010-2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_cpio.c 201163 2009-12-29 05:50:34Z kientzle $");
33 /* #include <stdint.h> */ /* See archive_platform.h */
42 #include "archive_entry.h"
43 #include "archive_entry_locale.h"
44 #include "archive_private.h"
45 #include "archive_read_private.h"
47 #define bin_magic_offset 0
48 #define bin_magic_size 2
49 #define bin_dev_offset 2
50 #define bin_dev_size 2
51 #define bin_ino_offset 4
52 #define bin_ino_size 2
53 #define bin_mode_offset 6
54 #define bin_mode_size 2
55 #define bin_uid_offset 8
56 #define bin_uid_size 2
57 #define bin_gid_offset 10
58 #define bin_gid_size 2
59 #define bin_nlink_offset 12
60 #define bin_nlink_size 2
61 #define bin_rdev_offset 14
62 #define bin_rdev_size 2
63 #define bin_mtime_offset 16
64 #define bin_mtime_size 4
65 #define bin_namesize_offset 20
66 #define bin_namesize_size 2
67 #define bin_filesize_offset 22
68 #define bin_filesize_size 4
69 #define bin_header_size 26
71 #define odc_magic_offset 0
72 #define odc_magic_size 6
73 #define odc_dev_offset 6
74 #define odc_dev_size 6
75 #define odc_ino_offset 12
76 #define odc_ino_size 6
77 #define odc_mode_offset 18
78 #define odc_mode_size 6
79 #define odc_uid_offset 24
80 #define odc_uid_size 6
81 #define odc_gid_offset 30
82 #define odc_gid_size 6
83 #define odc_nlink_offset 36
84 #define odc_nlink_size 6
85 #define odc_rdev_offset 42
86 #define odc_rdev_size 6
87 #define odc_mtime_offset 48
88 #define odc_mtime_size 11
89 #define odc_namesize_offset 59
90 #define odc_namesize_size 6
91 #define odc_filesize_offset 65
92 #define odc_filesize_size 11
93 #define odc_header_size 76
95 #define newc_magic_offset 0
96 #define newc_magic_size 6
97 #define newc_ino_offset 6
98 #define newc_ino_size 8
99 #define newc_mode_offset 14
100 #define newc_mode_size 8
101 #define newc_uid_offset 22
102 #define newc_uid_size 8
103 #define newc_gid_offset 30
104 #define newc_gid_size 8
105 #define newc_nlink_offset 38
106 #define newc_nlink_size 8
107 #define newc_mtime_offset 46
108 #define newc_mtime_size 8
109 #define newc_filesize_offset 54
110 #define newc_filesize_size 8
111 #define newc_devmajor_offset 62
112 #define newc_devmajor_size 8
113 #define newc_devminor_offset 70
114 #define newc_devminor_size 8
115 #define newc_rdevmajor_offset 78
116 #define newc_rdevmajor_size 8
117 #define newc_rdevminor_offset 86
118 #define newc_rdevminor_size 8
119 #define newc_namesize_offset 94
120 #define newc_namesize_size 8
121 #define newc_checksum_offset 102
122 #define newc_checksum_size 8
123 #define newc_header_size 110
126 * An afio large ASCII header, which they named itself.
127 * afio utility uses this header, if a file size is larger than 2G bytes
128 * or inode/uid/gid is bigger than 65535(0xFFFF) or mtime is bigger than
129 * 0x7fffffff, which we cannot record to odc header because of its limit.
130 * If not, uses odc header.
132 #define afiol_magic_offset 0
133 #define afiol_magic_size 6
134 #define afiol_dev_offset 6
135 #define afiol_dev_size 8 /* hex */
136 #define afiol_ino_offset 14
137 #define afiol_ino_size 16 /* hex */
138 #define afiol_ino_m_offset 30 /* 'm' */
139 #define afiol_mode_offset 31
140 #define afiol_mode_size 6 /* oct */
141 #define afiol_uid_offset 37
142 #define afiol_uid_size 8 /* hex */
143 #define afiol_gid_offset 45
144 #define afiol_gid_size 8 /* hex */
145 #define afiol_nlink_offset 53
146 #define afiol_nlink_size 8 /* hex */
147 #define afiol_rdev_offset 61
148 #define afiol_rdev_size 8 /* hex */
149 #define afiol_mtime_offset 69
150 #define afiol_mtime_size 16 /* hex */
151 #define afiol_mtime_n_offset 85 /* 'n' */
152 #define afiol_namesize_offset 86
153 #define afiol_namesize_size 4 /* hex */
154 #define afiol_flag_offset 90
155 #define afiol_flag_size 4 /* hex */
156 #define afiol_xsize_offset 94
157 #define afiol_xsize_size 4 /* hex */
158 #define afiol_xsize_s_offset 98 /* 's' */
159 #define afiol_filesize_offset 99
160 #define afiol_filesize_size 16 /* hex */
161 #define afiol_filesize_c_offset 115 /* ':' */
162 #define afiol_header_size 116
166 struct links_entry *next;
167 struct links_entry *previous;
174 #define CPIO_MAGIC 0x13141516
177 int (*read_header)(struct archive_read *, struct cpio *,
178 struct archive_entry *, size_t *, size_t *);
179 struct links_entry *links_head;
180 int64_t entry_bytes_remaining;
181 int64_t entry_bytes_unconsumed;
182 int64_t entry_offset;
183 int64_t entry_padding;
185 struct archive_string_conv *opt_sconv;
186 struct archive_string_conv *sconv_default;
187 int init_default_conversion;
192 static int64_t atol16(const char *, unsigned);
193 static int64_t atol8(const char *, unsigned);
194 static int archive_read_format_cpio_bid(struct archive_read *, int);
195 static int archive_read_format_cpio_options(struct archive_read *,
196 const char *, const char *);
197 static int archive_read_format_cpio_cleanup(struct archive_read *);
198 static int archive_read_format_cpio_read_data(struct archive_read *,
199 const void **, size_t *, int64_t *);
200 static int archive_read_format_cpio_read_header(struct archive_read *,
201 struct archive_entry *);
202 static int archive_read_format_cpio_skip(struct archive_read *);
203 static int64_t be4(const unsigned char *);
204 static int find_odc_header(struct archive_read *);
205 static int find_newc_header(struct archive_read *);
206 static int header_bin_be(struct archive_read *, struct cpio *,
207 struct archive_entry *, size_t *, size_t *);
208 static int header_bin_le(struct archive_read *, struct cpio *,
209 struct archive_entry *, size_t *, size_t *);
210 static int header_newc(struct archive_read *, struct cpio *,
211 struct archive_entry *, size_t *, size_t *);
212 static int header_odc(struct archive_read *, struct cpio *,
213 struct archive_entry *, size_t *, size_t *);
214 static int header_afiol(struct archive_read *, struct cpio *,
215 struct archive_entry *, size_t *, size_t *);
216 static int is_octal(const char *, size_t);
217 static int is_hex(const char *, size_t);
218 static int64_t le4(const unsigned char *);
219 static int record_hardlink(struct archive_read *a,
220 struct cpio *cpio, struct archive_entry *entry);
223 archive_read_support_format_cpio(struct archive *_a)
225 struct archive_read *a = (struct archive_read *)_a;
229 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
230 ARCHIVE_STATE_NEW, "archive_read_support_format_cpio");
232 cpio = (struct cpio *)calloc(1, sizeof(*cpio));
234 archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
235 return (ARCHIVE_FATAL);
237 cpio->magic = CPIO_MAGIC;
239 r = __archive_read_register_format(a,
242 archive_read_format_cpio_bid,
243 archive_read_format_cpio_options,
244 archive_read_format_cpio_read_header,
245 archive_read_format_cpio_read_data,
246 archive_read_format_cpio_skip,
248 archive_read_format_cpio_cleanup,
259 archive_read_format_cpio_bid(struct archive_read *a, int best_bid)
261 const unsigned char *p;
265 (void)best_bid; /* UNUSED */
267 cpio = (struct cpio *)(a->format->data);
269 if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
273 if (memcmp(p, "070707", 6) == 0) {
274 /* ASCII cpio archive (odc, POSIX.1) */
275 cpio->read_header = header_odc;
278 * XXX TODO: More verification; Could check that only octal
279 * digits appear in appropriate header locations. XXX
281 } else if (memcmp(p, "070727", 6) == 0) {
282 /* afio large ASCII cpio archive */
283 cpio->read_header = header_odc;
286 * XXX TODO: More verification; Could check that almost hex
287 * digits appear in appropriate header locations. XXX
289 } else if (memcmp(p, "070701", 6) == 0) {
290 /* ASCII cpio archive (SVR4 without CRC) */
291 cpio->read_header = header_newc;
294 * XXX TODO: More verification; Could check that only hex
295 * digits appear in appropriate header locations. XXX
297 } else if (memcmp(p, "070702", 6) == 0) {
298 /* ASCII cpio archive (SVR4 with CRC) */
299 /* XXX TODO: Flag that we should check the CRC. XXX */
300 cpio->read_header = header_newc;
303 * XXX TODO: More verification; Could check that only hex
304 * digits appear in appropriate header locations. XXX
306 } else if (p[0] * 256 + p[1] == 070707) {
307 /* big-endian binary cpio archives */
308 cpio->read_header = header_bin_be;
310 /* Is more verification possible here? */
311 } else if (p[0] + p[1] * 256 == 070707) {
312 /* little-endian binary cpio archives */
313 cpio->read_header = header_bin_le;
315 /* Is more verification possible here? */
317 return (ARCHIVE_WARN);
323 archive_read_format_cpio_options(struct archive_read *a,
324 const char *key, const char *val)
327 int ret = ARCHIVE_FAILED;
329 cpio = (struct cpio *)(a->format->data);
330 if (strcmp(key, "compat-2x") == 0) {
331 /* Handle filenames as libarchive 2.x */
332 cpio->init_default_conversion = (val != NULL)?1:0;
334 } else if (strcmp(key, "hdrcharset") == 0) {
335 if (val == NULL || val[0] == 0)
336 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
337 "cpio: hdrcharset option needs a character-set name");
340 archive_string_conversion_from_charset(
341 &a->archive, val, 0);
342 if (cpio->opt_sconv != NULL)
348 } else if (strcmp(key, "pwb") == 0) {
349 if (val != NULL && val[0] != 0)
350 cpio->option_pwb = 1;
354 /* Note: The "warn" return is just to inform the options
355 * supervisor that we didn't handle it. It will generate
356 * a suitable error if no one used this option. */
357 return (ARCHIVE_WARN);
361 archive_read_format_cpio_read_header(struct archive_read *a,
362 struct archive_entry *entry)
366 struct archive_string_conv *sconv;
371 cpio = (struct cpio *)(a->format->data);
372 sconv = cpio->opt_sconv;
374 if (!cpio->init_default_conversion) {
375 cpio->sconv_default =
376 archive_string_default_conversion_for_read(
378 cpio->init_default_conversion = 1;
380 sconv = cpio->sconv_default;
383 r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
385 if (r < ARCHIVE_WARN)
388 /* Read name from buffer. */
389 h = __archive_read_ahead(a, namelength + name_pad, NULL);
391 return (ARCHIVE_FATAL);
392 if (archive_entry_copy_pathname_l(entry,
393 (const char *)h, namelength, sconv) != 0) {
394 if (errno == ENOMEM) {
395 archive_set_error(&a->archive, ENOMEM,
396 "Can't allocate memory for Pathname");
397 return (ARCHIVE_FATAL);
399 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
400 "Pathname can't be converted from %s to current locale.",
401 archive_string_conversion_charset_name(sconv));
404 cpio->entry_offset = 0;
406 __archive_read_consume(a, namelength + name_pad);
408 /* If this is a symlink, read the link contents. */
409 if (archive_entry_filetype(entry) == AE_IFLNK) {
410 if (cpio->entry_bytes_remaining > 1024 * 1024) {
411 archive_set_error(&a->archive, ENOMEM,
412 "Rejecting malformed cpio archive: symlink contents exceed 1 megabyte");
413 return (ARCHIVE_FATAL);
415 hl = __archive_read_ahead(a,
416 (size_t)cpio->entry_bytes_remaining, NULL);
418 return (ARCHIVE_FATAL);
419 if (archive_entry_copy_symlink_l(entry, (const char *)hl,
420 (size_t)cpio->entry_bytes_remaining, sconv) != 0) {
421 if (errno == ENOMEM) {
422 archive_set_error(&a->archive, ENOMEM,
423 "Can't allocate memory for Linkname");
424 return (ARCHIVE_FATAL);
426 archive_set_error(&a->archive,
427 ARCHIVE_ERRNO_FILE_FORMAT,
428 "Linkname can't be converted from %s to "
430 archive_string_conversion_charset_name(sconv));
433 __archive_read_consume(a, cpio->entry_bytes_remaining);
434 cpio->entry_bytes_remaining = 0;
437 /* XXX TODO: If the full mode is 0160200, then this is a Solaris
438 * ACL description for the following entry. Read this body
439 * and parse it as a Solaris-style ACL, then read the next
442 /* Compare name to "TRAILER!!!" to test for end-of-archive. */
443 if (namelength == 11 && strncmp((const char *)h, "TRAILER!!!",
445 /* TODO: Store file location of start of block. */
446 archive_clear_error(&a->archive);
447 return (ARCHIVE_EOF);
450 /* Detect and record hardlinks to previously-extracted entries. */
451 if (record_hardlink(a, cpio, entry) != ARCHIVE_OK) {
452 return (ARCHIVE_FATAL);
459 archive_read_format_cpio_read_data(struct archive_read *a,
460 const void **buff, size_t *size, int64_t *offset)
465 cpio = (struct cpio *)(a->format->data);
467 if (cpio->entry_bytes_unconsumed) {
468 __archive_read_consume(a, cpio->entry_bytes_unconsumed);
469 cpio->entry_bytes_unconsumed = 0;
472 if (cpio->entry_bytes_remaining > 0) {
473 *buff = __archive_read_ahead(a, 1, &bytes_read);
475 return (ARCHIVE_FATAL);
476 if (bytes_read > cpio->entry_bytes_remaining)
477 bytes_read = (ssize_t)cpio->entry_bytes_remaining;
479 cpio->entry_bytes_unconsumed = bytes_read;
480 *offset = cpio->entry_offset;
481 cpio->entry_offset += bytes_read;
482 cpio->entry_bytes_remaining -= bytes_read;
485 if (cpio->entry_padding !=
486 __archive_read_consume(a, cpio->entry_padding)) {
487 return (ARCHIVE_FATAL);
489 cpio->entry_padding = 0;
492 *offset = cpio->entry_offset;
493 return (ARCHIVE_EOF);
498 archive_read_format_cpio_skip(struct archive_read *a)
500 struct cpio *cpio = (struct cpio *)(a->format->data);
501 int64_t to_skip = cpio->entry_bytes_remaining + cpio->entry_padding +
502 cpio->entry_bytes_unconsumed;
504 if (to_skip != __archive_read_consume(a, to_skip)) {
505 return (ARCHIVE_FATAL);
507 cpio->entry_bytes_remaining = 0;
508 cpio->entry_padding = 0;
509 cpio->entry_bytes_unconsumed = 0;
514 * Skip forward to the next cpio newc header by searching for the
515 * 07070[12] string. This should be generalized and merged with
516 * find_odc_header below.
519 is_hex(const char *p, size_t len)
522 if ((*p >= '0' && *p <= '9')
523 || (*p >= 'a' && *p <= 'f')
524 || (*p >= 'A' && *p <= 'F'))
533 find_newc_header(struct archive_read *a)
537 size_t skip, skipped = 0;
541 h = __archive_read_ahead(a, newc_header_size, &bytes);
543 return (ARCHIVE_FATAL);
547 /* Try the typical case first, then go into the slow search.*/
548 if (memcmp("07070", p, 5) == 0
549 && (p[5] == '1' || p[5] == '2')
550 && is_hex(p, newc_header_size))
554 * Scan ahead until we find something that looks
555 * like a newc header.
557 while (p + newc_header_size <= q) {
561 if (memcmp("07070", p, 5) == 0
562 && is_hex(p, newc_header_size)) {
563 skip = p - (const char *)h;
564 __archive_read_consume(a, skip);
567 archive_set_error(&a->archive,
569 "Skipped %d bytes before "
570 "finding valid header",
572 return (ARCHIVE_WARN);
586 skip = p - (const char *)h;
587 __archive_read_consume(a, skip);
593 header_newc(struct archive_read *a, struct cpio *cpio,
594 struct archive_entry *entry, size_t *namelength, size_t *name_pad)
600 r = find_newc_header(a);
601 if (r < ARCHIVE_WARN)
604 /* Read fixed-size portion of header. */
605 h = __archive_read_ahead(a, newc_header_size, NULL);
607 return (ARCHIVE_FATAL);
609 /* Parse out hex fields. */
610 header = (const char *)h;
612 if (memcmp(header + newc_magic_offset, "070701", 6) == 0) {
613 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
614 a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
615 } else if (memcmp(header + newc_magic_offset, "070702", 6) == 0) {
616 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
617 a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
619 /* TODO: Abort here? */
622 archive_entry_set_devmajor(entry,
623 (dev_t)atol16(header + newc_devmajor_offset, newc_devmajor_size));
624 archive_entry_set_devminor(entry,
625 (dev_t)atol16(header + newc_devminor_offset, newc_devminor_size));
626 archive_entry_set_ino(entry, atol16(header + newc_ino_offset, newc_ino_size));
627 archive_entry_set_mode(entry,
628 (mode_t)atol16(header + newc_mode_offset, newc_mode_size));
629 archive_entry_set_uid(entry, atol16(header + newc_uid_offset, newc_uid_size));
630 archive_entry_set_gid(entry, atol16(header + newc_gid_offset, newc_gid_size));
631 archive_entry_set_nlink(entry,
632 (unsigned int)atol16(header + newc_nlink_offset, newc_nlink_size));
633 archive_entry_set_rdevmajor(entry,
634 (dev_t)atol16(header + newc_rdevmajor_offset, newc_rdevmajor_size));
635 archive_entry_set_rdevminor(entry,
636 (dev_t)atol16(header + newc_rdevminor_offset, newc_rdevminor_size));
637 archive_entry_set_mtime(entry, atol16(header + newc_mtime_offset, newc_mtime_size), 0);
638 *namelength = (size_t)atol16(header + newc_namesize_offset, newc_namesize_size);
639 /* Pad name to 2 more than a multiple of 4. */
640 *name_pad = (2 - *namelength) & 3;
642 /* Make sure that the padded name length fits into size_t. */
643 if (*name_pad > SIZE_MAX - *namelength) {
644 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
645 "cpio archive has invalid namelength");
646 return (ARCHIVE_FATAL);
650 * Note: entry_bytes_remaining is at least 64 bits and
651 * therefore guaranteed to be big enough for a 33-bit file
654 cpio->entry_bytes_remaining =
655 atol16(header + newc_filesize_offset, newc_filesize_size);
656 archive_entry_set_size(entry, cpio->entry_bytes_remaining);
657 /* Pad file contents to a multiple of 4. */
658 cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
659 __archive_read_consume(a, newc_header_size);
664 * Skip forward to the next cpio odc header by searching for the
665 * 070707 string. This is a hand-optimized search that could
666 * probably be easily generalized to handle all character-based
670 is_octal(const char *p, size_t len)
673 if (*p < '0' || *p > '7')
681 is_afio_large(const char *h, size_t len)
683 if (len < afiol_header_size)
685 if (h[afiol_ino_m_offset] != 'm'
686 || h[afiol_mtime_n_offset] != 'n'
687 || h[afiol_xsize_s_offset] != 's'
688 || h[afiol_filesize_c_offset] != ':')
690 if (!is_hex(h + afiol_dev_offset, afiol_ino_m_offset - afiol_dev_offset))
692 if (!is_hex(h + afiol_mode_offset, afiol_mtime_n_offset - afiol_mode_offset))
694 if (!is_hex(h + afiol_namesize_offset, afiol_xsize_s_offset - afiol_namesize_offset))
696 if (!is_hex(h + afiol_filesize_offset, afiol_filesize_size))
702 find_odc_header(struct archive_read *a)
706 size_t skip, skipped = 0;
710 h = __archive_read_ahead(a, odc_header_size, &bytes);
712 return (ARCHIVE_FATAL);
716 /* Try the typical case first, then go into the slow search.*/
717 if (memcmp("070707", p, 6) == 0 && is_octal(p, odc_header_size))
719 if (memcmp("070727", p, 6) == 0 && is_afio_large(p, bytes)) {
720 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
725 * Scan ahead until we find something that looks
726 * like an odc header.
728 while (p + odc_header_size <= q) {
731 if ((memcmp("070707", p, 6) == 0
732 && is_octal(p, odc_header_size))
733 || (memcmp("070727", p, 6) == 0
734 && is_afio_large(p, q - p))) {
735 skip = p - (const char *)h;
736 __archive_read_consume(a, skip);
739 a->archive.archive_format =
740 ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
742 archive_set_error(&a->archive,
744 "Skipped %d bytes before "
745 "finding valid header",
747 return (ARCHIVE_WARN);
761 skip = p - (const char *)h;
762 __archive_read_consume(a, skip);
768 header_odc(struct archive_read *a, struct cpio *cpio,
769 struct archive_entry *entry, size_t *namelength, size_t *name_pad)
775 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
776 a->archive.archive_format_name = "POSIX octet-oriented cpio";
778 /* Find the start of the next header. */
779 r = find_odc_header(a);
780 if (r < ARCHIVE_WARN)
783 if (a->archive.archive_format == ARCHIVE_FORMAT_CPIO_AFIO_LARGE) {
784 int r2 = (header_afiol(a, cpio, entry, namelength, name_pad));
785 if (r2 == ARCHIVE_OK)
791 /* Read fixed-size portion of header. */
792 h = __archive_read_ahead(a, odc_header_size, NULL);
794 return (ARCHIVE_FATAL);
796 /* Parse out octal fields. */
797 header = (const char *)h;
799 archive_entry_set_dev(entry,
800 (dev_t)atol8(header + odc_dev_offset, odc_dev_size));
801 archive_entry_set_ino(entry, atol8(header + odc_ino_offset, odc_ino_size));
802 archive_entry_set_mode(entry,
803 (mode_t)atol8(header + odc_mode_offset, odc_mode_size));
804 archive_entry_set_uid(entry, atol8(header + odc_uid_offset, odc_uid_size));
805 archive_entry_set_gid(entry, atol8(header + odc_gid_offset, odc_gid_size));
806 archive_entry_set_nlink(entry,
807 (unsigned int)atol8(header + odc_nlink_offset, odc_nlink_size));
808 archive_entry_set_rdev(entry,
809 (dev_t)atol8(header + odc_rdev_offset, odc_rdev_size));
810 archive_entry_set_mtime(entry, atol8(header + odc_mtime_offset, odc_mtime_size), 0);
811 *namelength = (size_t)atol8(header + odc_namesize_offset, odc_namesize_size);
812 *name_pad = 0; /* No padding of filename. */
815 * Note: entry_bytes_remaining is at least 64 bits and
816 * therefore guaranteed to be big enough for a 33-bit file
819 cpio->entry_bytes_remaining =
820 atol8(header + odc_filesize_offset, odc_filesize_size);
821 archive_entry_set_size(entry, cpio->entry_bytes_remaining);
822 cpio->entry_padding = 0;
823 __archive_read_consume(a, odc_header_size);
828 * NOTE: if a filename suffix is ".z", it is the file gziped by afio.
829 * it would be nice that we can show uncompressed file size and we can
830 * uncompressed file contents automatically, unfortunately we have nothing
831 * to get a uncompressed file size while reading each header. It means
832 * we also cannot uncompress file contents under our framework.
835 header_afiol(struct archive_read *a, struct cpio *cpio,
836 struct archive_entry *entry, size_t *namelength, size_t *name_pad)
841 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
842 a->archive.archive_format_name = "afio large ASCII";
844 /* Read fixed-size portion of header. */
845 h = __archive_read_ahead(a, afiol_header_size, NULL);
847 return (ARCHIVE_FATAL);
849 /* Parse out octal fields. */
850 header = (const char *)h;
852 archive_entry_set_dev(entry,
853 (dev_t)atol16(header + afiol_dev_offset, afiol_dev_size));
854 archive_entry_set_ino(entry, atol16(header + afiol_ino_offset, afiol_ino_size));
855 archive_entry_set_mode(entry,
856 (mode_t)atol8(header + afiol_mode_offset, afiol_mode_size));
857 archive_entry_set_uid(entry, atol16(header + afiol_uid_offset, afiol_uid_size));
858 archive_entry_set_gid(entry, atol16(header + afiol_gid_offset, afiol_gid_size));
859 archive_entry_set_nlink(entry,
860 (unsigned int)atol16(header + afiol_nlink_offset, afiol_nlink_size));
861 archive_entry_set_rdev(entry,
862 (dev_t)atol16(header + afiol_rdev_offset, afiol_rdev_size));
863 archive_entry_set_mtime(entry, atol16(header + afiol_mtime_offset, afiol_mtime_size), 0);
864 *namelength = (size_t)atol16(header + afiol_namesize_offset, afiol_namesize_size);
865 *name_pad = 0; /* No padding of filename. */
867 cpio->entry_bytes_remaining =
868 atol16(header + afiol_filesize_offset, afiol_filesize_size);
869 archive_entry_set_size(entry, cpio->entry_bytes_remaining);
870 cpio->entry_padding = 0;
871 __archive_read_consume(a, afiol_header_size);
877 header_bin_le(struct archive_read *a, struct cpio *cpio,
878 struct archive_entry *entry, size_t *namelength, size_t *name_pad)
881 const unsigned char *header;
883 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
884 a->archive.archive_format_name = "cpio (little-endian binary)";
886 /* Read fixed-size portion of header. */
887 h = __archive_read_ahead(a, bin_header_size, NULL);
889 archive_set_error(&a->archive, 0,
890 "End of file trying to read next cpio header");
891 return (ARCHIVE_FATAL);
894 /* Parse out binary fields. */
895 header = (const unsigned char *)h;
897 archive_entry_set_dev(entry, header[bin_dev_offset] + header[bin_dev_offset + 1] * 256);
898 archive_entry_set_ino(entry, header[bin_ino_offset] + header[bin_ino_offset + 1] * 256);
899 archive_entry_set_mode(entry, header[bin_mode_offset] + header[bin_mode_offset + 1] * 256);
900 if (cpio->option_pwb) {
901 /* turn off random bits left over from V6 inode */
902 archive_entry_set_mode(entry, archive_entry_mode(entry) & 067777);
903 if ((archive_entry_mode(entry) & AE_IFMT) == 0)
904 archive_entry_set_mode(entry, archive_entry_mode(entry) | AE_IFREG);
906 archive_entry_set_uid(entry, header[bin_uid_offset] + header[bin_uid_offset + 1] * 256);
907 archive_entry_set_gid(entry, header[bin_gid_offset] + header[bin_gid_offset + 1] * 256);
908 archive_entry_set_nlink(entry, header[bin_nlink_offset] + header[bin_nlink_offset + 1] * 256);
909 archive_entry_set_rdev(entry, header[bin_rdev_offset] + header[bin_rdev_offset + 1] * 256);
910 archive_entry_set_mtime(entry, le4(header + bin_mtime_offset), 0);
911 *namelength = header[bin_namesize_offset] + header[bin_namesize_offset + 1] * 256;
912 *name_pad = *namelength & 1; /* Pad to even. */
914 cpio->entry_bytes_remaining = le4(header + bin_filesize_offset);
915 archive_entry_set_size(entry, cpio->entry_bytes_remaining);
916 cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
917 __archive_read_consume(a, bin_header_size);
922 header_bin_be(struct archive_read *a, struct cpio *cpio,
923 struct archive_entry *entry, size_t *namelength, size_t *name_pad)
926 const unsigned char *header;
928 a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
929 a->archive.archive_format_name = "cpio (big-endian binary)";
931 /* Read fixed-size portion of header. */
932 h = __archive_read_ahead(a, bin_header_size, NULL);
934 archive_set_error(&a->archive, 0,
935 "End of file trying to read next cpio header");
936 return (ARCHIVE_FATAL);
939 /* Parse out binary fields. */
940 header = (const unsigned char *)h;
942 archive_entry_set_dev(entry, header[bin_dev_offset] * 256 + header[bin_dev_offset + 1]);
943 archive_entry_set_ino(entry, header[bin_ino_offset] * 256 + header[bin_ino_offset + 1]);
944 archive_entry_set_mode(entry, header[bin_mode_offset] * 256 + header[bin_mode_offset + 1]);
945 if (cpio->option_pwb) {
946 /* turn off random bits left over from V6 inode */
947 archive_entry_set_mode(entry, archive_entry_mode(entry) & 067777);
948 if ((archive_entry_mode(entry) & AE_IFMT) == 0)
949 archive_entry_set_mode(entry, archive_entry_mode(entry) | AE_IFREG);
951 archive_entry_set_uid(entry, header[bin_uid_offset] * 256 + header[bin_uid_offset + 1]);
952 archive_entry_set_gid(entry, header[bin_gid_offset] * 256 + header[bin_gid_offset + 1]);
953 archive_entry_set_nlink(entry, header[bin_nlink_offset] * 256 + header[bin_nlink_offset + 1]);
954 archive_entry_set_rdev(entry, header[bin_rdev_offset] * 256 + header[bin_rdev_offset + 1]);
955 archive_entry_set_mtime(entry, be4(header + bin_mtime_offset), 0);
956 *namelength = header[bin_namesize_offset] * 256 + header[bin_namesize_offset + 1];
957 *name_pad = *namelength & 1; /* Pad to even. */
959 cpio->entry_bytes_remaining = be4(header + bin_filesize_offset);
960 archive_entry_set_size(entry, cpio->entry_bytes_remaining);
961 cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
962 __archive_read_consume(a, bin_header_size);
967 archive_read_format_cpio_cleanup(struct archive_read *a)
971 cpio = (struct cpio *)(a->format->data);
972 /* Free inode->name map */
973 while (cpio->links_head != NULL) {
974 struct links_entry *lp = cpio->links_head->next;
976 free(cpio->links_head->name);
977 free(cpio->links_head);
978 cpio->links_head = lp;
981 (a->format->data) = NULL;
986 le4(const unsigned char *p)
988 return ((p[0] << 16) + (((int64_t)p[1]) << 24) + (p[2] << 0) + (p[3] << 8));
993 be4(const unsigned char *p)
995 return ((((int64_t)p[0]) << 24) + (p[1] << 16) + (p[2] << 8) + (p[3]));
999 * Note that this implementation does not (and should not!) obey
1000 * locale settings; you cannot simply substitute strtol here, since
1001 * it does obey locale.
1004 atol8(const char *p, unsigned char_cnt)
1010 while (char_cnt-- > 0) {
1011 if (*p >= '0' && *p <= '7')
1023 atol16(const char *p, unsigned char_cnt)
1029 while (char_cnt-- > 0) {
1030 if (*p >= 'a' && *p <= 'f')
1031 digit = *p - 'a' + 10;
1032 else if (*p >= 'A' && *p <= 'F')
1033 digit = *p - 'A' + 10;
1034 else if (*p >= '0' && *p <= '9')
1046 record_hardlink(struct archive_read *a,
1047 struct cpio *cpio, struct archive_entry *entry)
1049 struct links_entry *le;
1053 if (archive_entry_nlink(entry) <= 1)
1054 return (ARCHIVE_OK);
1056 dev = archive_entry_dev(entry);
1057 ino = archive_entry_ino64(entry);
1060 * First look in the list of multiply-linked files. If we've
1061 * already dumped it, convert this entry to a hard link entry.
1063 for (le = cpio->links_head; le; le = le->next) {
1064 if (le->dev == dev && le->ino == ino) {
1065 archive_entry_copy_hardlink(entry, le->name);
1067 if (--le->links <= 0) {
1068 if (le->previous != NULL)
1069 le->previous->next = le->next;
1070 if (le->next != NULL)
1071 le->next->previous = le->previous;
1072 if (cpio->links_head == le)
1073 cpio->links_head = le->next;
1078 return (ARCHIVE_OK);
1082 le = (struct links_entry *)malloc(sizeof(struct links_entry));
1084 archive_set_error(&a->archive,
1085 ENOMEM, "Out of memory adding file to list");
1086 return (ARCHIVE_FATAL);
1088 if (cpio->links_head != NULL)
1089 cpio->links_head->previous = le;
1090 le->next = cpio->links_head;
1091 le->previous = NULL;
1092 cpio->links_head = le;
1095 le->links = archive_entry_nlink(entry) - 1;
1096 le->name = strdup(archive_entry_pathname(entry));
1097 if (le->name == NULL) {
1098 archive_set_error(&a->archive,
1099 ENOMEM, "Out of memory adding file to list");
1100 return (ARCHIVE_FATAL);
1103 return (ARCHIVE_OK);