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         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 
190 static int64_t	atol16(const char *, unsigned);
191 static int64_t	atol8(const char *, unsigned);
192 static int	archive_read_format_cpio_bid(struct archive_read *, int);
193 static int	archive_read_format_cpio_options(struct archive_read *,
194 		    const char *, const char *);
195 static int	archive_read_format_cpio_cleanup(struct archive_read *);
196 static int	archive_read_format_cpio_read_data(struct archive_read *,
197 		    const void **, size_t *, int64_t *);
198 static int	archive_read_format_cpio_read_header(struct archive_read *,
199 		    struct archive_entry *);
200 static int	archive_read_format_cpio_skip(struct archive_read *);
201 static int	be4(const unsigned char *);
202 static int	find_odc_header(struct archive_read *);
203 static int	find_newc_header(struct archive_read *);
204 static int	header_bin_be(struct archive_read *, struct cpio *,
205 		    struct archive_entry *, size_t *, size_t *);
206 static int	header_bin_le(struct archive_read *, struct cpio *,
207 		    struct archive_entry *, size_t *, size_t *);
208 static int	header_newc(struct archive_read *, struct cpio *,
209 		    struct archive_entry *, size_t *, size_t *);
210 static int	header_odc(struct archive_read *, struct cpio *,
211 		    struct archive_entry *, size_t *, size_t *);
212 static int	header_afiol(struct archive_read *, struct cpio *,
213 		    struct archive_entry *, size_t *, size_t *);
214 static int	is_octal(const char *, size_t);
215 static int	is_hex(const char *, size_t);
216 static int	le4(const unsigned char *);
217 static int	record_hardlink(struct archive_read *a,
218 		    struct cpio *cpio, struct archive_entry *entry);
219 
220 int
221 archive_read_support_format_cpio(struct archive *_a)
222 {
223 	struct archive_read *a = (struct archive_read *)_a;
224 	struct cpio *cpio;
225 	int r;
226 
227 	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
228 	    ARCHIVE_STATE_NEW, "archive_read_support_format_cpio");
229 
230 	cpio = (struct cpio *)calloc(1, sizeof(*cpio));
231 	if (cpio == NULL) {
232 		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
233 		return (ARCHIVE_FATAL);
234 	}
235 	cpio->magic = CPIO_MAGIC;
236 
237 	r = __archive_read_register_format(a,
238 	    cpio,
239 	    "cpio",
240 	    archive_read_format_cpio_bid,
241 	    archive_read_format_cpio_options,
242 	    archive_read_format_cpio_read_header,
243 	    archive_read_format_cpio_read_data,
244 	    archive_read_format_cpio_skip,
245 	    NULL,
246 	    archive_read_format_cpio_cleanup);
247 
248 	if (r != ARCHIVE_OK)
249 		free(cpio);
250 	return (ARCHIVE_OK);
251 }
252 
253 
254 static int
255 archive_read_format_cpio_bid(struct archive_read *a, int best_bid)
256 {
257 	const unsigned char *p;
258 	struct cpio *cpio;
259 	int bid;
260 
261 	(void)best_bid; /* UNUSED */
262 
263 	cpio = (struct cpio *)(a->format->data);
264 
265 	if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
266 		return (-1);
267 
268 	bid = 0;
269 	if (memcmp(p, "070707", 6) == 0) {
270 		/* ASCII cpio archive (odc, POSIX.1) */
271 		cpio->read_header = header_odc;
272 		bid += 48;
273 		/*
274 		 * XXX TODO:  More verification; Could check that only octal
275 		 * digits appear in appropriate header locations. XXX
276 		 */
277 	} else if (memcmp(p, "070727", 6) == 0) {
278 		/* afio large ASCII cpio archive */
279 		cpio->read_header = header_odc;
280 		bid += 48;
281 		/*
282 		 * XXX TODO:  More verification; Could check that almost hex
283 		 * digits appear in appropriate header locations. XXX
284 		 */
285 	} else if (memcmp(p, "070701", 6) == 0) {
286 		/* ASCII cpio archive (SVR4 without CRC) */
287 		cpio->read_header = header_newc;
288 		bid += 48;
289 		/*
290 		 * XXX TODO:  More verification; Could check that only hex
291 		 * digits appear in appropriate header locations. XXX
292 		 */
293 	} else if (memcmp(p, "070702", 6) == 0) {
294 		/* ASCII cpio archive (SVR4 with CRC) */
295 		/* XXX TODO: Flag that we should check the CRC. XXX */
296 		cpio->read_header = header_newc;
297 		bid += 48;
298 		/*
299 		 * XXX TODO:  More verification; Could check that only hex
300 		 * digits appear in appropriate header locations. XXX
301 		 */
302 	} else if (p[0] * 256 + p[1] == 070707) {
303 		/* big-endian binary cpio archives */
304 		cpio->read_header = header_bin_be;
305 		bid += 16;
306 		/* Is more verification possible here? */
307 	} else if (p[0] + p[1] * 256 == 070707) {
308 		/* little-endian binary cpio archives */
309 		cpio->read_header = header_bin_le;
310 		bid += 16;
311 		/* Is more verification possible here? */
312 	} else
313 		return (ARCHIVE_WARN);
314 
315 	return (bid);
316 }
317 
318 static int
319 archive_read_format_cpio_options(struct archive_read *a,
320     const char *key, const char *val)
321 {
322 	struct cpio *cpio;
323 	int ret = ARCHIVE_FAILED;
324 
325 	cpio = (struct cpio *)(a->format->data);
326 	if (strcmp(key, "compat-2x")  == 0) {
327 		/* Handle filnames as libarchive 2.x */
328 		cpio->init_default_conversion = (val != NULL)?1:0;
329 		return (ARCHIVE_OK);
330 	} else if (strcmp(key, "hdrcharset")  == 0) {
331 		if (val == NULL || val[0] == 0)
332 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
333 			    "cpio: hdrcharset option needs a character-set name");
334 		else {
335 			cpio->opt_sconv =
336 			    archive_string_conversion_from_charset(
337 				&a->archive, val, 0);
338 			if (cpio->opt_sconv != NULL)
339 				ret = ARCHIVE_OK;
340 			else
341 				ret = ARCHIVE_FATAL;
342 		}
343 		return (ret);
344 	}
345 
346 	/* Note: The "warn" return is just to inform the options
347 	 * supervisor that we didn't handle it.  It will generate
348 	 * a suitable error if no one used this option. */
349 	return (ARCHIVE_WARN);
350 }
351 
352 static int
353 archive_read_format_cpio_read_header(struct archive_read *a,
354     struct archive_entry *entry)
355 {
356 	struct cpio *cpio;
357 	const void *h;
358 	struct archive_string_conv *sconv;
359 	size_t namelength;
360 	size_t name_pad;
361 	int r;
362 
363 	cpio = (struct cpio *)(a->format->data);
364 	sconv = cpio->opt_sconv;
365 	if (sconv == NULL) {
366 		if (!cpio->init_default_conversion) {
367 			cpio->sconv_default =
368 			    archive_string_default_conversion_for_read(
369 			      &(a->archive));
370 			cpio->init_default_conversion = 1;
371 		}
372 		sconv = cpio->sconv_default;
373 	}
374 
375 	r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
376 
377 	if (r < ARCHIVE_WARN)
378 		return (r);
379 
380 	/* Read name from buffer. */
381 	h = __archive_read_ahead(a, namelength + name_pad, NULL);
382 	if (h == NULL)
383 	    return (ARCHIVE_FATAL);
384 	if (archive_entry_copy_pathname_l(entry,
385 	    (const char *)h, namelength, sconv) != 0) {
386 		if (errno == ENOMEM) {
387 			archive_set_error(&a->archive, ENOMEM,
388 			    "Can't allocate memory for Pathname");
389 			return (ARCHIVE_FATAL);
390 		}
391 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
392 		    "Pathname can't be converted from %s to current locale.",
393 		    archive_string_conversion_charset_name(sconv));
394 		r = ARCHIVE_WARN;
395 	}
396 	cpio->entry_offset = 0;
397 
398 	__archive_read_consume(a, namelength + name_pad);
399 
400 	/* If this is a symlink, read the link contents. */
401 	if (archive_entry_filetype(entry) == AE_IFLNK) {
402 		h = __archive_read_ahead(a,
403 			(size_t)cpio->entry_bytes_remaining, NULL);
404 		if (h == NULL)
405 			return (ARCHIVE_FATAL);
406 		if (archive_entry_copy_symlink_l(entry, (const char *)h,
407 		    (size_t)cpio->entry_bytes_remaining, sconv) != 0) {
408 			if (errno == ENOMEM) {
409 				archive_set_error(&a->archive, ENOMEM,
410 				    "Can't allocate memory for Linkname");
411 				return (ARCHIVE_FATAL);
412 			}
413 			archive_set_error(&a->archive,
414 			    ARCHIVE_ERRNO_FILE_FORMAT,
415 			    "Linkname can't be converted from %s to "
416 			    "current locale.",
417 			    archive_string_conversion_charset_name(sconv));
418 			r = ARCHIVE_WARN;
419 		}
420 		__archive_read_consume(a, cpio->entry_bytes_remaining);
421 		cpio->entry_bytes_remaining = 0;
422 	}
423 
424 	/* XXX TODO: If the full mode is 0160200, then this is a Solaris
425 	 * ACL description for the following entry.  Read this body
426 	 * and parse it as a Solaris-style ACL, then read the next
427 	 * header.  XXX */
428 
429 	/* Compare name to "TRAILER!!!" to test for end-of-archive. */
430 	if (namelength == 11 && strcmp((const char *)h, "TRAILER!!!") == 0) {
431 		/* TODO: Store file location of start of block. */
432 		archive_clear_error(&a->archive);
433 		return (ARCHIVE_EOF);
434 	}
435 
436 	/* Detect and record hardlinks to previously-extracted entries. */
437 	if (record_hardlink(a, cpio, entry) != ARCHIVE_OK) {
438 		return (ARCHIVE_FATAL);
439 	}
440 
441 	return (r);
442 }
443 
444 static int
445 archive_read_format_cpio_read_data(struct archive_read *a,
446     const void **buff, size_t *size, int64_t *offset)
447 {
448 	ssize_t bytes_read;
449 	struct cpio *cpio;
450 
451 	cpio = (struct cpio *)(a->format->data);
452 
453 	if (cpio->entry_bytes_unconsumed) {
454 		__archive_read_consume(a, cpio->entry_bytes_unconsumed);
455 		cpio->entry_bytes_unconsumed = 0;
456 	}
457 
458 	if (cpio->entry_bytes_remaining > 0) {
459 		*buff = __archive_read_ahead(a, 1, &bytes_read);
460 		if (bytes_read <= 0)
461 			return (ARCHIVE_FATAL);
462 		if (bytes_read > cpio->entry_bytes_remaining)
463 			bytes_read = (ssize_t)cpio->entry_bytes_remaining;
464 		*size = bytes_read;
465 		cpio->entry_bytes_unconsumed = bytes_read;
466 		*offset = cpio->entry_offset;
467 		cpio->entry_offset += bytes_read;
468 		cpio->entry_bytes_remaining -= bytes_read;
469 		return (ARCHIVE_OK);
470 	} else {
471 		if (cpio->entry_padding !=
472 			__archive_read_consume(a, cpio->entry_padding)) {
473 			return (ARCHIVE_FATAL);
474 		}
475 		cpio->entry_padding = 0;
476 		*buff = NULL;
477 		*size = 0;
478 		*offset = cpio->entry_offset;
479 		return (ARCHIVE_EOF);
480 	}
481 }
482 
483 static int
484 archive_read_format_cpio_skip(struct archive_read *a)
485 {
486 	struct cpio *cpio = (struct cpio *)(a->format->data);
487 	int64_t to_skip = cpio->entry_bytes_remaining + cpio->entry_padding +
488 		cpio->entry_bytes_unconsumed;
489 
490 	if (to_skip != __archive_read_consume(a, to_skip)) {
491 		return (ARCHIVE_FATAL);
492 	}
493 	cpio->entry_bytes_remaining = 0;
494 	cpio->entry_padding = 0;
495 	cpio->entry_bytes_unconsumed = 0;
496 	return (ARCHIVE_OK);
497 }
498 
499 /*
500  * Skip forward to the next cpio newc header by searching for the
501  * 07070[12] string.  This should be generalized and merged with
502  * find_odc_header below.
503  */
504 static int
505 is_hex(const char *p, size_t len)
506 {
507 	while (len-- > 0) {
508 		if ((*p >= '0' && *p <= '9')
509 		    || (*p >= 'a' && *p <= 'f')
510 		    || (*p >= 'A' && *p <= 'F'))
511 			++p;
512 		else
513 			return (0);
514 	}
515 	return (1);
516 }
517 
518 static int
519 find_newc_header(struct archive_read *a)
520 {
521 	const void *h;
522 	const char *p, *q;
523 	size_t skip, skipped = 0;
524 	ssize_t bytes;
525 
526 	for (;;) {
527 		h = __archive_read_ahead(a, newc_header_size, &bytes);
528 		if (h == NULL)
529 			return (ARCHIVE_FATAL);
530 		p = h;
531 		q = p + bytes;
532 
533 		/* Try the typical case first, then go into the slow search.*/
534 		if (memcmp("07070", p, 5) == 0
535 		    && (p[5] == '1' || p[5] == '2')
536 		    && is_hex(p, newc_header_size))
537 			return (ARCHIVE_OK);
538 
539 		/*
540 		 * Scan ahead until we find something that looks
541 		 * like a newc header.
542 		 */
543 		while (p + newc_header_size <= q) {
544 			switch (p[5]) {
545 			case '1':
546 			case '2':
547 				if (memcmp("07070", p, 5) == 0
548 				    && is_hex(p, newc_header_size)) {
549 					skip = p - (const char *)h;
550 					__archive_read_consume(a, skip);
551 					skipped += skip;
552 					if (skipped > 0) {
553 						archive_set_error(&a->archive,
554 						    0,
555 						    "Skipped %d bytes before "
556 						    "finding valid header",
557 						    (int)skipped);
558 						return (ARCHIVE_WARN);
559 					}
560 					return (ARCHIVE_OK);
561 				}
562 				p += 2;
563 				break;
564 			case '0':
565 				p++;
566 				break;
567 			default:
568 				p += 6;
569 				break;
570 			}
571 		}
572 		skip = p - (const char *)h;
573 		__archive_read_consume(a, skip);
574 		skipped += skip;
575 	}
576 }
577 
578 static int
579 header_newc(struct archive_read *a, struct cpio *cpio,
580     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
581 {
582 	const void *h;
583 	const char *header;
584 	int r;
585 
586 	r = find_newc_header(a);
587 	if (r < ARCHIVE_WARN)
588 		return (r);
589 
590 	/* Read fixed-size portion of header. */
591 	h = __archive_read_ahead(a, newc_header_size, NULL);
592 	if (h == NULL)
593 	    return (ARCHIVE_FATAL);
594 
595 	/* Parse out hex fields. */
596 	header = (const char *)h;
597 
598 	if (memcmp(header + newc_magic_offset, "070701", 6) == 0) {
599 		a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
600 		a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
601 	} else if (memcmp(header + newc_magic_offset, "070702", 6) == 0) {
602 		a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
603 		a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
604 	} else {
605 		/* TODO: Abort here? */
606 	}
607 
608 	archive_entry_set_devmajor(entry,
609 		(dev_t)atol16(header + newc_devmajor_offset, newc_devmajor_size));
610 	archive_entry_set_devminor(entry,
611 		(dev_t)atol16(header + newc_devminor_offset, newc_devminor_size));
612 	archive_entry_set_ino(entry, atol16(header + newc_ino_offset, newc_ino_size));
613 	archive_entry_set_mode(entry,
614 		(mode_t)atol16(header + newc_mode_offset, newc_mode_size));
615 	archive_entry_set_uid(entry, atol16(header + newc_uid_offset, newc_uid_size));
616 	archive_entry_set_gid(entry, atol16(header + newc_gid_offset, newc_gid_size));
617 	archive_entry_set_nlink(entry,
618 		(unsigned int)atol16(header + newc_nlink_offset, newc_nlink_size));
619 	archive_entry_set_rdevmajor(entry,
620 		(dev_t)atol16(header + newc_rdevmajor_offset, newc_rdevmajor_size));
621 	archive_entry_set_rdevminor(entry,
622 		(dev_t)atol16(header + newc_rdevminor_offset, newc_rdevminor_size));
623 	archive_entry_set_mtime(entry, atol16(header + newc_mtime_offset, newc_mtime_size), 0);
624 	*namelength = (size_t)atol16(header + newc_namesize_offset, newc_namesize_size);
625 	/* Pad name to 2 more than a multiple of 4. */
626 	*name_pad = (2 - *namelength) & 3;
627 
628 	/*
629 	 * Note: entry_bytes_remaining is at least 64 bits and
630 	 * therefore guaranteed to be big enough for a 33-bit file
631 	 * size.
632 	 */
633 	cpio->entry_bytes_remaining =
634 	    atol16(header + newc_filesize_offset, newc_filesize_size);
635 	archive_entry_set_size(entry, cpio->entry_bytes_remaining);
636 	/* Pad file contents to a multiple of 4. */
637 	cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
638 	__archive_read_consume(a, newc_header_size);
639 	return (r);
640 }
641 
642 /*
643  * Skip forward to the next cpio odc header by searching for the
644  * 070707 string.  This is a hand-optimized search that could
645  * probably be easily generalized to handle all character-based
646  * cpio variants.
647  */
648 static int
649 is_octal(const char *p, size_t len)
650 {
651 	while (len-- > 0) {
652 		if (*p < '0' || *p > '7')
653 			return (0);
654 	        ++p;
655 	}
656 	return (1);
657 }
658 
659 static int
660 is_afio_large(const char *h, size_t len)
661 {
662 	if (len < afiol_header_size)
663 		return (0);
664 	if (h[afiol_ino_m_offset] != 'm'
665 	    || h[afiol_mtime_n_offset] != 'n'
666 	    || h[afiol_xsize_s_offset] != 's'
667 	    || h[afiol_filesize_c_offset] != ':')
668 		return (0);
669 	if (!is_hex(h + afiol_dev_offset, afiol_ino_m_offset - afiol_dev_offset))
670 		return (0);
671 	if (!is_hex(h + afiol_mode_offset, afiol_mtime_n_offset - afiol_mode_offset))
672 		return (0);
673 	if (!is_hex(h + afiol_namesize_offset, afiol_xsize_s_offset - afiol_namesize_offset))
674 		return (0);
675 	if (!is_hex(h + afiol_filesize_offset, afiol_filesize_size))
676 		return (0);
677 	return (1);
678 }
679 
680 static int
681 find_odc_header(struct archive_read *a)
682 {
683 	const void *h;
684 	const char *p, *q;
685 	size_t skip, skipped = 0;
686 	ssize_t bytes;
687 
688 	for (;;) {
689 		h = __archive_read_ahead(a, odc_header_size, &bytes);
690 		if (h == NULL)
691 			return (ARCHIVE_FATAL);
692 		p = h;
693 		q = p + bytes;
694 
695 		/* Try the typical case first, then go into the slow search.*/
696 		if (memcmp("070707", p, 6) == 0 && is_octal(p, odc_header_size))
697 			return (ARCHIVE_OK);
698 		if (memcmp("070727", p, 6) == 0 && is_afio_large(p, bytes)) {
699 			a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
700 			return (ARCHIVE_OK);
701 		}
702 
703 		/*
704 		 * Scan ahead until we find something that looks
705 		 * like an odc header.
706 		 */
707 		while (p + odc_header_size <= q) {
708 			switch (p[5]) {
709 			case '7':
710 				if ((memcmp("070707", p, 6) == 0
711 				    && is_octal(p, odc_header_size))
712 				    || (memcmp("070727", p, 6) == 0
713 				        && is_afio_large(p, q - p))) {
714 					skip = p - (const char *)h;
715 					__archive_read_consume(a, skip);
716 					skipped += skip;
717 					if (p[4] == '2')
718 						a->archive.archive_format =
719 						    ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
720 					if (skipped > 0) {
721 						archive_set_error(&a->archive,
722 						    0,
723 						    "Skipped %d bytes before "
724 						    "finding valid header",
725 						    (int)skipped);
726 						return (ARCHIVE_WARN);
727 					}
728 					return (ARCHIVE_OK);
729 				}
730 				p += 2;
731 				break;
732 			case '0':
733 				p++;
734 				break;
735 			default:
736 				p += 6;
737 				break;
738 			}
739 		}
740 		skip = p - (const char *)h;
741 		__archive_read_consume(a, skip);
742 		skipped += skip;
743 	}
744 }
745 
746 static int
747 header_odc(struct archive_read *a, struct cpio *cpio,
748     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
749 {
750 	const void *h;
751 	int r;
752 	const char *header;
753 
754 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
755 	a->archive.archive_format_name = "POSIX octet-oriented cpio";
756 
757 	/* Find the start of the next header. */
758 	r = find_odc_header(a);
759 	if (r < ARCHIVE_WARN)
760 		return (r);
761 
762 	if (a->archive.archive_format == ARCHIVE_FORMAT_CPIO_AFIO_LARGE) {
763 		int r2 = (header_afiol(a, cpio, entry, namelength, name_pad));
764 		if (r2 == ARCHIVE_OK)
765 			return (r);
766 		else
767 			return (r2);
768 	}
769 
770 	/* Read fixed-size portion of header. */
771 	h = __archive_read_ahead(a, odc_header_size, NULL);
772 	if (h == NULL)
773 	    return (ARCHIVE_FATAL);
774 
775 	/* Parse out octal fields. */
776 	header = (const char *)h;
777 
778 	archive_entry_set_dev(entry,
779 		(dev_t)atol8(header + odc_dev_offset, odc_dev_size));
780 	archive_entry_set_ino(entry, atol8(header + odc_ino_offset, odc_ino_size));
781 	archive_entry_set_mode(entry,
782 		(mode_t)atol8(header + odc_mode_offset, odc_mode_size));
783 	archive_entry_set_uid(entry, atol8(header + odc_uid_offset, odc_uid_size));
784 	archive_entry_set_gid(entry, atol8(header + odc_gid_offset, odc_gid_size));
785 	archive_entry_set_nlink(entry,
786 		(unsigned int)atol8(header + odc_nlink_offset, odc_nlink_size));
787 	archive_entry_set_rdev(entry,
788 		(dev_t)atol8(header + odc_rdev_offset, odc_rdev_size));
789 	archive_entry_set_mtime(entry, atol8(header + odc_mtime_offset, odc_mtime_size), 0);
790 	*namelength = (size_t)atol8(header + odc_namesize_offset, odc_namesize_size);
791 	*name_pad = 0; /* No padding of filename. */
792 
793 	/*
794 	 * Note: entry_bytes_remaining is at least 64 bits and
795 	 * therefore guaranteed to be big enough for a 33-bit file
796 	 * size.
797 	 */
798 	cpio->entry_bytes_remaining =
799 	    atol8(header + odc_filesize_offset, odc_filesize_size);
800 	archive_entry_set_size(entry, cpio->entry_bytes_remaining);
801 	cpio->entry_padding = 0;
802 	__archive_read_consume(a, odc_header_size);
803 	return (r);
804 }
805 
806 /*
807  * NOTE: if a filename suffix is ".z", it is the file gziped by afio.
808  * it would be nice that we can show uncompressed file size and we can
809  * uncompressed file contents automatically, unfortunately we have nothing
810  * to get a uncompressed file size while reading each header. it means
811  * we also cannot uncompressed file contens under the our framework.
812  */
813 static int
814 header_afiol(struct archive_read *a, struct cpio *cpio,
815     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
816 {
817 	const void *h;
818 	const char *header;
819 
820 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
821 	a->archive.archive_format_name = "afio large ASCII";
822 
823 	/* Read fixed-size portion of header. */
824 	h = __archive_read_ahead(a, afiol_header_size, NULL);
825 	if (h == NULL)
826 	    return (ARCHIVE_FATAL);
827 
828 	/* Parse out octal fields. */
829 	header = (const char *)h;
830 
831 	archive_entry_set_dev(entry,
832 		(dev_t)atol16(header + afiol_dev_offset, afiol_dev_size));
833 	archive_entry_set_ino(entry, atol16(header + afiol_ino_offset, afiol_ino_size));
834 	archive_entry_set_mode(entry,
835 		(mode_t)atol8(header + afiol_mode_offset, afiol_mode_size));
836 	archive_entry_set_uid(entry, atol16(header + afiol_uid_offset, afiol_uid_size));
837 	archive_entry_set_gid(entry, atol16(header + afiol_gid_offset, afiol_gid_size));
838 	archive_entry_set_nlink(entry,
839 		(unsigned int)atol16(header + afiol_nlink_offset, afiol_nlink_size));
840 	archive_entry_set_rdev(entry,
841 		(dev_t)atol16(header + afiol_rdev_offset, afiol_rdev_size));
842 	archive_entry_set_mtime(entry, atol16(header + afiol_mtime_offset, afiol_mtime_size), 0);
843 	*namelength = (size_t)atol16(header + afiol_namesize_offset, afiol_namesize_size);
844 	*name_pad = 0; /* No padding of filename. */
845 
846 	cpio->entry_bytes_remaining =
847 	    atol16(header + afiol_filesize_offset, afiol_filesize_size);
848 	archive_entry_set_size(entry, cpio->entry_bytes_remaining);
849 	cpio->entry_padding = 0;
850 	__archive_read_consume(a, afiol_header_size);
851 	return (ARCHIVE_OK);
852 }
853 
854 
855 static int
856 header_bin_le(struct archive_read *a, struct cpio *cpio,
857     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
858 {
859 	const void *h;
860 	const unsigned char *header;
861 
862 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
863 	a->archive.archive_format_name = "cpio (little-endian binary)";
864 
865 	/* Read fixed-size portion of header. */
866 	h = __archive_read_ahead(a, bin_header_size, NULL);
867 	if (h == NULL)
868 	    return (ARCHIVE_FATAL);
869 
870 	/* Parse out binary fields. */
871 	header = (const unsigned char *)h;
872 
873 	archive_entry_set_dev(entry, header[bin_dev_offset] + header[bin_dev_offset + 1] * 256);
874 	archive_entry_set_ino(entry, header[bin_ino_offset] + header[bin_ino_offset + 1] * 256);
875 	archive_entry_set_mode(entry, header[bin_mode_offset] + header[bin_mode_offset + 1] * 256);
876 	archive_entry_set_uid(entry, header[bin_uid_offset] + header[bin_uid_offset + 1] * 256);
877 	archive_entry_set_gid(entry, header[bin_gid_offset] + header[bin_gid_offset + 1] * 256);
878 	archive_entry_set_nlink(entry, header[bin_nlink_offset] + header[bin_nlink_offset + 1] * 256);
879 	archive_entry_set_rdev(entry, header[bin_rdev_offset] + header[bin_rdev_offset + 1] * 256);
880 	archive_entry_set_mtime(entry, le4(header + bin_mtime_offset), 0);
881 	*namelength = header[bin_namesize_offset] + header[bin_namesize_offset + 1] * 256;
882 	*name_pad = *namelength & 1; /* Pad to even. */
883 
884 	cpio->entry_bytes_remaining = le4(header + bin_filesize_offset);
885 	archive_entry_set_size(entry, cpio->entry_bytes_remaining);
886 	cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
887 	__archive_read_consume(a, bin_header_size);
888 	return (ARCHIVE_OK);
889 }
890 
891 static int
892 header_bin_be(struct archive_read *a, struct cpio *cpio,
893     struct archive_entry *entry, size_t *namelength, size_t *name_pad)
894 {
895 	const void *h;
896 	const unsigned char *header;
897 
898 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
899 	a->archive.archive_format_name = "cpio (big-endian binary)";
900 
901 	/* Read fixed-size portion of header. */
902 	h = __archive_read_ahead(a, bin_header_size, NULL);
903 	if (h == NULL)
904 	    return (ARCHIVE_FATAL);
905 
906 	/* Parse out binary fields. */
907 	header = (const unsigned char *)h;
908 
909 	archive_entry_set_dev(entry, header[bin_dev_offset] * 256 + header[bin_dev_offset + 1]);
910 	archive_entry_set_ino(entry, header[bin_ino_offset] * 256 + header[bin_ino_offset + 1]);
911 	archive_entry_set_mode(entry, header[bin_mode_offset] * 256 + header[bin_mode_offset + 1]);
912 	archive_entry_set_uid(entry, header[bin_uid_offset] * 256 + header[bin_uid_offset + 1]);
913 	archive_entry_set_gid(entry, header[bin_gid_offset] * 256 + header[bin_gid_offset + 1]);
914 	archive_entry_set_nlink(entry, header[bin_nlink_offset] * 256 + header[bin_nlink_offset + 1]);
915 	archive_entry_set_rdev(entry, header[bin_rdev_offset] * 256 + header[bin_rdev_offset + 1]);
916 	archive_entry_set_mtime(entry, be4(header + bin_mtime_offset), 0);
917 	*namelength = header[bin_namesize_offset] * 256 + header[bin_namesize_offset + 1];
918 	*name_pad = *namelength & 1; /* Pad to even. */
919 
920 	cpio->entry_bytes_remaining = be4(header + bin_filesize_offset);
921 	archive_entry_set_size(entry, cpio->entry_bytes_remaining);
922 	cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
923 	    __archive_read_consume(a, bin_header_size);
924 	return (ARCHIVE_OK);
925 }
926 
927 static int
928 archive_read_format_cpio_cleanup(struct archive_read *a)
929 {
930 	struct cpio *cpio;
931 
932 	cpio = (struct cpio *)(a->format->data);
933         /* Free inode->name map */
934         while (cpio->links_head != NULL) {
935                 struct links_entry *lp = cpio->links_head->next;
936 
937                 if (cpio->links_head->name)
938                         free(cpio->links_head->name);
939                 free(cpio->links_head);
940                 cpio->links_head = lp;
941         }
942 	free(cpio);
943 	(a->format->data) = NULL;
944 	return (ARCHIVE_OK);
945 }
946 
947 static int
948 le4(const unsigned char *p)
949 {
950 	return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
951 }
952 
953 
954 static int
955 be4(const unsigned char *p)
956 {
957 	return ((p[0]<<24) + (p[1]<<16) + (p[2]<<8) + (p[3]));
958 }
959 
960 /*
961  * Note that this implementation does not (and should not!) obey
962  * locale settings; you cannot simply substitute strtol here, since
963  * it does obey locale.
964  */
965 static int64_t
966 atol8(const char *p, unsigned char_cnt)
967 {
968 	int64_t l;
969 	int digit;
970 
971 	l = 0;
972 	while (char_cnt-- > 0) {
973 		if (*p >= '0' && *p <= '7')
974 			digit = *p - '0';
975 		else
976 			return (l);
977 		p++;
978 		l <<= 3;
979 		l |= digit;
980 	}
981 	return (l);
982 }
983 
984 static int64_t
985 atol16(const char *p, unsigned char_cnt)
986 {
987 	int64_t l;
988 	int digit;
989 
990 	l = 0;
991 	while (char_cnt-- > 0) {
992 		if (*p >= 'a' && *p <= 'f')
993 			digit = *p - 'a' + 10;
994 		else if (*p >= 'A' && *p <= 'F')
995 			digit = *p - 'A' + 10;
996 		else if (*p >= '0' && *p <= '9')
997 			digit = *p - '0';
998 		else
999 			return (l);
1000 		p++;
1001 		l <<= 4;
1002 		l |= digit;
1003 	}
1004 	return (l);
1005 }
1006 
1007 static int
1008 record_hardlink(struct archive_read *a,
1009     struct cpio *cpio, struct archive_entry *entry)
1010 {
1011 	struct links_entry      *le;
1012 	dev_t dev;
1013 	int64_t ino;
1014 
1015 	if (archive_entry_nlink(entry) <= 1)
1016 		return (ARCHIVE_OK);
1017 
1018 	dev = archive_entry_dev(entry);
1019 	ino = archive_entry_ino64(entry);
1020 
1021 	/*
1022 	 * First look in the list of multiply-linked files.  If we've
1023 	 * already dumped it, convert this entry to a hard link entry.
1024 	 */
1025 	for (le = cpio->links_head; le; le = le->next) {
1026 		if (le->dev == dev && le->ino == ino) {
1027 			archive_entry_copy_hardlink(entry, le->name);
1028 
1029 			if (--le->links <= 0) {
1030 				if (le->previous != NULL)
1031 					le->previous->next = le->next;
1032 				if (le->next != NULL)
1033 					le->next->previous = le->previous;
1034 				if (cpio->links_head == le)
1035 					cpio->links_head = le->next;
1036 				free(le->name);
1037 				free(le);
1038 			}
1039 
1040 			return (ARCHIVE_OK);
1041 		}
1042 	}
1043 
1044 	le = (struct links_entry *)malloc(sizeof(struct links_entry));
1045 	if (le == NULL) {
1046 		archive_set_error(&a->archive,
1047 		    ENOMEM, "Out of memory adding file to list");
1048 		return (ARCHIVE_FATAL);
1049 	}
1050 	if (cpio->links_head != NULL)
1051 		cpio->links_head->previous = le;
1052 	le->next = cpio->links_head;
1053 	le->previous = NULL;
1054 	cpio->links_head = le;
1055 	le->dev = dev;
1056 	le->ino = ino;
1057 	le->links = archive_entry_nlink(entry) - 1;
1058 	le->name = strdup(archive_entry_pathname(entry));
1059 	if (le->name == NULL) {
1060 		archive_set_error(&a->archive,
1061 		    ENOMEM, "Out of memory adding file to list");
1062 		return (ARCHIVE_FATAL);
1063 	}
1064 
1065 	return (ARCHIVE_OK);
1066 }
1067