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