1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2010-2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
25  */
26 
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 $");
29 
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 /* #include <stdint.h> */ /* See archive_platform.h */
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 
41 #include "archive.h"
42 #include "archive_entry.h"
43 #include "archive_entry_locale.h"
44 #include "archive_private.h"
45 #include "archive_read_private.h"
46 
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
70 
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
94 
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
124 
125 /*
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.
131  */
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
163 
164 
165 struct links_entry {
166         struct links_entry      *next;
167         struct links_entry      *previous;
168         unsigned int             links;
169         dev_t                    dev;
170         int64_t                  ino;
171         char                    *name;
172 };
173 
174 #define	CPIO_MAGIC   0x13141516
175 struct cpio {
176 	int			  magic;
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;
184 
185 	struct archive_string_conv *opt_sconv;
186 	struct archive_string_conv *sconv_default;
187 	int			  init_default_conversion;
188 
189 	int			  option_pwb;
190 };
191 
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);
221 
222 int
223 archive_read_support_format_cpio(struct archive *_a)
224 {
225 	struct archive_read *a = (struct archive_read *)_a;
226 	struct cpio *cpio;
227 	int r;
228 
229 	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
230 	    ARCHIVE_STATE_NEW, "archive_read_support_format_cpio");
231 
232 	cpio = (struct cpio *)calloc(1, sizeof(*cpio));
233 	if (cpio == NULL) {
234 		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
235 		return (ARCHIVE_FATAL);
236 	}
237 	cpio->magic = CPIO_MAGIC;
238 
239 	r = __archive_read_register_format(a,
240 	    cpio,
241 	    "cpio",
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,
247 	    NULL,
248 	    archive_read_format_cpio_cleanup,
249 	    NULL,
250 	    NULL);
251 
252 	if (r != ARCHIVE_OK)
253 		free(cpio);
254 	return (ARCHIVE_OK);
255 }
256 
257 
258 static int
259 archive_read_format_cpio_bid(struct archive_read *a, int best_bid)
260 {
261 	const unsigned char *p;
262 	struct cpio *cpio;
263 	int bid;
264 
265 	(void)best_bid; /* UNUSED */
266 
267 	cpio = (struct cpio *)(a->format->data);
268 
269 	if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
270 		return (-1);
271 
272 	bid = 0;
273 	if (memcmp(p, "070707", 6) == 0) {
274 		/* ASCII cpio archive (odc, POSIX.1) */
275 		cpio->read_header = header_odc;
276 		bid += 48;
277 		/*
278 		 * XXX TODO:  More verification; Could check that only octal
279 		 * digits appear in appropriate header locations. XXX
280 		 */
281 	} else if (memcmp(p, "070727", 6) == 0) {
282 		/* afio large ASCII cpio archive */
283 		cpio->read_header = header_odc;
284 		bid += 48;
285 		/*
286 		 * XXX TODO:  More verification; Could check that almost hex
287 		 * digits appear in appropriate header locations. XXX
288 		 */
289 	} else if (memcmp(p, "070701", 6) == 0) {
290 		/* ASCII cpio archive (SVR4 without CRC) */
291 		cpio->read_header = header_newc;
292 		bid += 48;
293 		/*
294 		 * XXX TODO:  More verification; Could check that only hex
295 		 * digits appear in appropriate header locations. XXX
296 		 */
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;
301 		bid += 48;
302 		/*
303 		 * XXX TODO:  More verification; Could check that only hex
304 		 * digits appear in appropriate header locations. XXX
305 		 */
306 	} else if (p[0] * 256 + p[1] == 070707) {
307 		/* big-endian binary cpio archives */
308 		cpio->read_header = header_bin_be;
309 		bid += 16;
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;
314 		bid += 16;
315 		/* Is more verification possible here? */
316 	} else
317 		return (ARCHIVE_WARN);
318 
319 	return (bid);
320 }
321 
322 static int
323 archive_read_format_cpio_options(struct archive_read *a,
324     const char *key, const char *val)
325 {
326 	struct cpio *cpio;
327 	int ret = ARCHIVE_FAILED;
328 
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;
333 		return (ARCHIVE_OK);
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");
338 		else {
339 			cpio->opt_sconv =
340 			    archive_string_conversion_from_charset(
341 				&a->archive, val, 0);
342 			if (cpio->opt_sconv != NULL)
343 				ret = ARCHIVE_OK;
344 			else
345 				ret = ARCHIVE_FATAL;
346 		}
347 		return (ret);
348 	} else if (strcmp(key, "pwb")  == 0) {
349 		if (val != NULL && val[0] != 0)
350 			cpio->option_pwb = 1;
351 		return (ARCHIVE_OK);
352 	}
353 
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);
358 }
359 
360 static int
361 archive_read_format_cpio_read_header(struct archive_read *a,
362     struct archive_entry *entry)
363 {
364 	struct cpio *cpio;
365 	const void *h, *hl;
366 	struct archive_string_conv *sconv;
367 	size_t namelength;
368 	size_t name_pad;
369 	int r;
370 
371 	cpio = (struct cpio *)(a->format->data);
372 	sconv = cpio->opt_sconv;
373 	if (sconv == NULL) {
374 		if (!cpio->init_default_conversion) {
375 			cpio->sconv_default =
376 			    archive_string_default_conversion_for_read(
377 			      &(a->archive));
378 			cpio->init_default_conversion = 1;
379 		}
380 		sconv = cpio->sconv_default;
381 	}
382 
383 	r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
384 
385 	if (r < ARCHIVE_WARN)
386 		return (r);
387 
388 	/* Read name from buffer. */
389 	h = __archive_read_ahead(a, namelength + name_pad, NULL);
390 	if (h == 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);
398 		}
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));
402 		r = ARCHIVE_WARN;
403 	}
404 	cpio->entry_offset = 0;
405 
406 	__archive_read_consume(a, namelength + name_pad);
407 
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);
414 		}
415 		hl = __archive_read_ahead(a,
416 			(size_t)cpio->entry_bytes_remaining, NULL);
417 		if (hl == 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);
425 			}
426 			archive_set_error(&a->archive,
427 			    ARCHIVE_ERRNO_FILE_FORMAT,
428 			    "Linkname can't be converted from %s to "
429 			    "current locale.",
430 			    archive_string_conversion_charset_name(sconv));
431 			r = ARCHIVE_WARN;
432 		}
433 		__archive_read_consume(a, cpio->entry_bytes_remaining);
434 		cpio->entry_bytes_remaining = 0;
435 	}
436 
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
440 	 * header.  XXX */
441 
442 	/* Compare name to "TRAILER!!!" to test for end-of-archive. */
443 	if (namelength == 11 && strncmp((const char *)h, "TRAILER!!!",
444 	    11) == 0) {
445 		/* TODO: Store file location of start of block. */
446 		archive_clear_error(&a->archive);
447 		return (ARCHIVE_EOF);
448 	}
449 
450 	/* Detect and record hardlinks to previously-extracted entries. */
451 	if (record_hardlink(a, cpio, entry) != ARCHIVE_OK) {
452 		return (ARCHIVE_FATAL);
453 	}
454 
455 	return (r);
456 }
457 
458 static int
459 archive_read_format_cpio_read_data(struct archive_read *a,
460     const void **buff, size_t *size, int64_t *offset)
461 {
462 	ssize_t bytes_read;
463 	struct cpio *cpio;
464 
465 	cpio = (struct cpio *)(a->format->data);
466 
467 	if (cpio->entry_bytes_unconsumed) {
468 		__archive_read_consume(a, cpio->entry_bytes_unconsumed);
469 		cpio->entry_bytes_unconsumed = 0;
470 	}
471 
472 	if (cpio->entry_bytes_remaining > 0) {
473 		*buff = __archive_read_ahead(a, 1, &bytes_read);
474 		if (bytes_read <= 0)
475 			return (ARCHIVE_FATAL);
476 		if (bytes_read > cpio->entry_bytes_remaining)
477 			bytes_read = (ssize_t)cpio->entry_bytes_remaining;
478 		*size = bytes_read;
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;
483 		return (ARCHIVE_OK);
484 	} else {
485 		if (cpio->entry_padding !=
486 			__archive_read_consume(a, cpio->entry_padding)) {
487 			return (ARCHIVE_FATAL);
488 		}
489 		cpio->entry_padding = 0;
490 		*buff = NULL;
491 		*size = 0;
492 		*offset = cpio->entry_offset;
493 		return (ARCHIVE_EOF);
494 	}
495 }
496 
497 static int
498 archive_read_format_cpio_skip(struct archive_read *a)
499 {
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;
503 
504 	if (to_skip != __archive_read_consume(a, to_skip)) {
505 		return (ARCHIVE_FATAL);
506 	}
507 	cpio->entry_bytes_remaining = 0;
508 	cpio->entry_padding = 0;
509 	cpio->entry_bytes_unconsumed = 0;
510 	return (ARCHIVE_OK);
511 }
512 
513 /*
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.
517  */
518 static int
519 is_hex(const char *p, size_t len)
520 {
521 	while (len-- > 0) {
522 		if ((*p >= '0' && *p <= '9')
523 		    || (*p >= 'a' && *p <= 'f')
524 		    || (*p >= 'A' && *p <= 'F'))
525 			++p;
526 		else
527 			return (0);
528 	}
529 	return (1);
530 }
531 
532 static int
533 find_newc_header(struct archive_read *a)
534 {
535 	const void *h;
536 	const char *p, *q;
537 	size_t skip, skipped = 0;
538 	ssize_t bytes;
539 
540 	for (;;) {
541 		h = __archive_read_ahead(a, newc_header_size, &bytes);
542 		if (h == NULL)
543 			return (ARCHIVE_FATAL);
544 		p = h;
545 		q = p + bytes;
546 
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))
551 			return (ARCHIVE_OK);
552 
553 		/*
554 		 * Scan ahead until we find something that looks
555 		 * like a newc header.
556 		 */
557 		while (p + newc_header_size <= q) {
558 			switch (p[5]) {
559 			case '1':
560 			case '2':
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);
565 					skipped += skip;
566 					if (skipped > 0) {
567 						archive_set_error(&a->archive,
568 						    0,
569 						    "Skipped %d bytes before "
570 						    "finding valid header",
571 						    (int)skipped);
572 						return (ARCHIVE_WARN);
573 					}
574 					return (ARCHIVE_OK);
575 				}
576 				p += 2;
577 				break;
578 			case '0':
579 				p++;
580 				break;
581 			default:
582 				p += 6;
583 				break;
584 			}
585 		}
586 		skip = p - (const char *)h;
587 		__archive_read_consume(a, skip);
588 		skipped += skip;
589 	}
590 }
591 
592 static int
593 header_newc(struct archive_read *a, struct cpio *cpio,
594     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
595 {
596 	const void *h;
597 	const char *header;
598 	int r;
599 
600 	r = find_newc_header(a);
601 	if (r < ARCHIVE_WARN)
602 		return (r);
603 
604 	/* Read fixed-size portion of header. */
605 	h = __archive_read_ahead(a, newc_header_size, NULL);
606 	if (h == NULL)
607 	    return (ARCHIVE_FATAL);
608 
609 	/* Parse out hex fields. */
610 	header = (const char *)h;
611 
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)";
618 	} else {
619 		/* TODO: Abort here? */
620 	}
621 
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;
641 
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);
647 	}
648 
649 	/*
650 	 * Note: entry_bytes_remaining is at least 64 bits and
651 	 * therefore guaranteed to be big enough for a 33-bit file
652 	 * size.
653 	 */
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);
660 	return (r);
661 }
662 
663 /*
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
667  * cpio variants.
668  */
669 static int
670 is_octal(const char *p, size_t len)
671 {
672 	while (len-- > 0) {
673 		if (*p < '0' || *p > '7')
674 			return (0);
675 	        ++p;
676 	}
677 	return (1);
678 }
679 
680 static int
681 is_afio_large(const char *h, size_t len)
682 {
683 	if (len < afiol_header_size)
684 		return (0);
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] != ':')
689 		return (0);
690 	if (!is_hex(h + afiol_dev_offset, afiol_ino_m_offset - afiol_dev_offset))
691 		return (0);
692 	if (!is_hex(h + afiol_mode_offset, afiol_mtime_n_offset - afiol_mode_offset))
693 		return (0);
694 	if (!is_hex(h + afiol_namesize_offset, afiol_xsize_s_offset - afiol_namesize_offset))
695 		return (0);
696 	if (!is_hex(h + afiol_filesize_offset, afiol_filesize_size))
697 		return (0);
698 	return (1);
699 }
700 
701 static int
702 find_odc_header(struct archive_read *a)
703 {
704 	const void *h;
705 	const char *p, *q;
706 	size_t skip, skipped = 0;
707 	ssize_t bytes;
708 
709 	for (;;) {
710 		h = __archive_read_ahead(a, odc_header_size, &bytes);
711 		if (h == NULL)
712 			return (ARCHIVE_FATAL);
713 		p = h;
714 		q = p + bytes;
715 
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))
718 			return (ARCHIVE_OK);
719 		if (memcmp("070727", p, 6) == 0 && is_afio_large(p, bytes)) {
720 			a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
721 			return (ARCHIVE_OK);
722 		}
723 
724 		/*
725 		 * Scan ahead until we find something that looks
726 		 * like an odc header.
727 		 */
728 		while (p + odc_header_size <= q) {
729 			switch (p[5]) {
730 			case '7':
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);
737 					skipped += skip;
738 					if (p[4] == '2')
739 						a->archive.archive_format =
740 						    ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
741 					if (skipped > 0) {
742 						archive_set_error(&a->archive,
743 						    0,
744 						    "Skipped %d bytes before "
745 						    "finding valid header",
746 						    (int)skipped);
747 						return (ARCHIVE_WARN);
748 					}
749 					return (ARCHIVE_OK);
750 				}
751 				p += 2;
752 				break;
753 			case '0':
754 				p++;
755 				break;
756 			default:
757 				p += 6;
758 				break;
759 			}
760 		}
761 		skip = p - (const char *)h;
762 		__archive_read_consume(a, skip);
763 		skipped += skip;
764 	}
765 }
766 
767 static int
768 header_odc(struct archive_read *a, struct cpio *cpio,
769     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
770 {
771 	const void *h;
772 	int r;
773 	const char *header;
774 
775 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
776 	a->archive.archive_format_name = "POSIX octet-oriented cpio";
777 
778 	/* Find the start of the next header. */
779 	r = find_odc_header(a);
780 	if (r < ARCHIVE_WARN)
781 		return (r);
782 
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)
786 			return (r);
787 		else
788 			return (r2);
789 	}
790 
791 	/* Read fixed-size portion of header. */
792 	h = __archive_read_ahead(a, odc_header_size, NULL);
793 	if (h == NULL)
794 	    return (ARCHIVE_FATAL);
795 
796 	/* Parse out octal fields. */
797 	header = (const char *)h;
798 
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. */
813 
814 	/*
815 	 * Note: entry_bytes_remaining is at least 64 bits and
816 	 * therefore guaranteed to be big enough for a 33-bit file
817 	 * size.
818 	 */
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);
824 	return (r);
825 }
826 
827 /*
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.
833  */
834 static int
835 header_afiol(struct archive_read *a, struct cpio *cpio,
836     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
837 {
838 	const void *h;
839 	const char *header;
840 
841 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
842 	a->archive.archive_format_name = "afio large ASCII";
843 
844 	/* Read fixed-size portion of header. */
845 	h = __archive_read_ahead(a, afiol_header_size, NULL);
846 	if (h == NULL)
847 	    return (ARCHIVE_FATAL);
848 
849 	/* Parse out octal fields. */
850 	header = (const char *)h;
851 
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. */
866 
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);
872 	return (ARCHIVE_OK);
873 }
874 
875 
876 static int
877 header_bin_le(struct archive_read *a, struct cpio *cpio,
878     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
879 {
880 	const void *h;
881 	const unsigned char *header;
882 
883 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
884 	a->archive.archive_format_name = "cpio (little-endian binary)";
885 
886 	/* Read fixed-size portion of header. */
887 	h = __archive_read_ahead(a, bin_header_size, NULL);
888 	if (h == NULL) {
889 	    archive_set_error(&a->archive, 0,
890 		"End of file trying to read next cpio header");
891 	    return (ARCHIVE_FATAL);
892 	}
893 
894 	/* Parse out binary fields. */
895 	header = (const unsigned char *)h;
896 
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);
905 	}
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. */
913 
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);
918 	return (ARCHIVE_OK);
919 }
920 
921 static int
922 header_bin_be(struct archive_read *a, struct cpio *cpio,
923     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
924 {
925 	const void *h;
926 	const unsigned char *header;
927 
928 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
929 	a->archive.archive_format_name = "cpio (big-endian binary)";
930 
931 	/* Read fixed-size portion of header. */
932 	h = __archive_read_ahead(a, bin_header_size, NULL);
933 	if (h == NULL) {
934 	    archive_set_error(&a->archive, 0,
935 		"End of file trying to read next cpio header");
936 	    return (ARCHIVE_FATAL);
937 	}
938 
939 	/* Parse out binary fields. */
940 	header = (const unsigned char *)h;
941 
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);
950 	}
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. */
958 
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);
963 	return (ARCHIVE_OK);
964 }
965 
966 static int
967 archive_read_format_cpio_cleanup(struct archive_read *a)
968 {
969 	struct cpio *cpio;
970 
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;
975 
976                 free(cpio->links_head->name);
977                 free(cpio->links_head);
978                 cpio->links_head = lp;
979         }
980 	free(cpio);
981 	(a->format->data) = NULL;
982 	return (ARCHIVE_OK);
983 }
984 
985 static int64_t
986 le4(const unsigned char *p)
987 {
988 	return ((p[0] << 16) + (((int64_t)p[1]) << 24) + (p[2] << 0) + (p[3] << 8));
989 }
990 
991 
992 static int64_t
993 be4(const unsigned char *p)
994 {
995 	return ((((int64_t)p[0]) << 24) + (p[1] << 16) + (p[2] << 8) + (p[3]));
996 }
997 
998 /*
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.
1002  */
1003 static int64_t
1004 atol8(const char *p, unsigned char_cnt)
1005 {
1006 	int64_t l;
1007 	int digit;
1008 
1009 	l = 0;
1010 	while (char_cnt-- > 0) {
1011 		if (*p >= '0' && *p <= '7')
1012 			digit = *p - '0';
1013 		else
1014 			return (l);
1015 		p++;
1016 		l <<= 3;
1017 		l |= digit;
1018 	}
1019 	return (l);
1020 }
1021 
1022 static int64_t
1023 atol16(const char *p, unsigned char_cnt)
1024 {
1025 	int64_t l;
1026 	int digit;
1027 
1028 	l = 0;
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')
1035 			digit = *p - '0';
1036 		else
1037 			return (l);
1038 		p++;
1039 		l <<= 4;
1040 		l |= digit;
1041 	}
1042 	return (l);
1043 }
1044 
1045 static int
1046 record_hardlink(struct archive_read *a,
1047     struct cpio *cpio, struct archive_entry *entry)
1048 {
1049 	struct links_entry      *le;
1050 	dev_t dev;
1051 	int64_t ino;
1052 
1053 	if (archive_entry_nlink(entry) <= 1)
1054 		return (ARCHIVE_OK);
1055 
1056 	dev = archive_entry_dev(entry);
1057 	ino = archive_entry_ino64(entry);
1058 
1059 	/*
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.
1062 	 */
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);
1066 
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;
1074 				free(le->name);
1075 				free(le);
1076 			}
1077 
1078 			return (ARCHIVE_OK);
1079 		}
1080 	}
1081 
1082 	le = (struct links_entry *)malloc(sizeof(struct links_entry));
1083 	if (le == NULL) {
1084 		archive_set_error(&a->archive,
1085 		    ENOMEM, "Out of memory adding file to list");
1086 		return (ARCHIVE_FATAL);
1087 	}
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;
1093 	le->dev = dev;
1094 	le->ino = ino;
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);
1101 	}
1102 
1103 	return (ARCHIVE_OK);
1104 }
1105