160b4ad09SPeter Avalos /*-
260b4ad09SPeter Avalos  * Copyright (c) 2003-2007 Tim Kientzle
360b4ad09SPeter Avalos  * Copyright (c) 2006 Rudolf Marek SYSGO s.r.o.
459bf7050SPeter Avalos  * Copyright (c) 2011-2012 Michihiro NAKAJIMA
560b4ad09SPeter Avalos  * All rights reserved.
660b4ad09SPeter Avalos  *
760b4ad09SPeter Avalos  * Redistribution and use in source and binary forms, with or without
860b4ad09SPeter Avalos  * modification, are permitted provided that the following conditions
960b4ad09SPeter Avalos  * are met:
1060b4ad09SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
1160b4ad09SPeter Avalos  *    notice, this list of conditions and the following disclaimer.
1260b4ad09SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
1360b4ad09SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
1460b4ad09SPeter Avalos  *    documentation and/or other materials provided with the distribution.
1560b4ad09SPeter Avalos  *
1660b4ad09SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1760b4ad09SPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1860b4ad09SPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1960b4ad09SPeter Avalos  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
2060b4ad09SPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2160b4ad09SPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2260b4ad09SPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2360b4ad09SPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2460b4ad09SPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2560b4ad09SPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2660b4ad09SPeter Avalos  */
2760b4ad09SPeter Avalos 
2860b4ad09SPeter Avalos #include "archive_platform.h"
299c82a63eSPeter Avalos __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_cpio_newc.c 201160 2009-12-29 05:41:57Z kientzle $");
3060b4ad09SPeter Avalos 
3160b4ad09SPeter Avalos #ifdef HAVE_ERRNO_H
3260b4ad09SPeter Avalos #include <errno.h>
3360b4ad09SPeter Avalos #endif
3460b4ad09SPeter Avalos #include <stdio.h>
3560b4ad09SPeter Avalos #ifdef HAVE_STDLIB_H
3660b4ad09SPeter Avalos #include <stdlib.h>
3760b4ad09SPeter Avalos #endif
3860b4ad09SPeter Avalos #ifdef HAVE_STRING_H
3960b4ad09SPeter Avalos #include <string.h>
4060b4ad09SPeter Avalos #endif
4160b4ad09SPeter Avalos 
4260b4ad09SPeter Avalos #include "archive.h"
4360b4ad09SPeter Avalos #include "archive_entry.h"
44c09f92d2SPeter Avalos #include "archive_entry_locale.h"
4560b4ad09SPeter Avalos #include "archive_private.h"
4660b4ad09SPeter Avalos #include "archive_write_private.h"
47085658deSDaniel Fojt #include "archive_write_set_format_private.h"
4860b4ad09SPeter Avalos 
4960b4ad09SPeter Avalos static ssize_t	archive_write_newc_data(struct archive_write *,
5060b4ad09SPeter Avalos 		    const void *buff, size_t s);
51c09f92d2SPeter Avalos static int	archive_write_newc_close(struct archive_write *);
52c09f92d2SPeter Avalos static int	archive_write_newc_free(struct archive_write *);
5360b4ad09SPeter Avalos static int	archive_write_newc_finish_entry(struct archive_write *);
5460b4ad09SPeter Avalos static int	archive_write_newc_header(struct archive_write *,
5560b4ad09SPeter Avalos 		    struct archive_entry *);
56c09f92d2SPeter Avalos static int      archive_write_newc_options(struct archive_write *,
57c09f92d2SPeter Avalos 		    const char *, const char *);
5860b4ad09SPeter Avalos static int	format_hex(int64_t, void *, int);
5960b4ad09SPeter Avalos static int64_t	format_hex_recursive(int64_t, char *, int);
60c09f92d2SPeter Avalos static int	write_header(struct archive_write *, struct archive_entry *);
6160b4ad09SPeter Avalos 
6260b4ad09SPeter Avalos struct cpio {
6360b4ad09SPeter Avalos 	uint64_t	  entry_bytes_remaining;
6460b4ad09SPeter Avalos 	int		  padding;
65c09f92d2SPeter Avalos 
66c09f92d2SPeter Avalos 	struct archive_string_conv *opt_sconv;
67c09f92d2SPeter Avalos 	struct archive_string_conv *sconv_default;
68c09f92d2SPeter Avalos 	int		  init_default_conversion;
6960b4ad09SPeter Avalos };
7060b4ad09SPeter Avalos 
71c09f92d2SPeter Avalos #define	c_magic_offset 0
72c09f92d2SPeter Avalos #define	c_magic_size 6
73c09f92d2SPeter Avalos #define	c_ino_offset 6
74c09f92d2SPeter Avalos #define	c_ino_size 8
75c09f92d2SPeter Avalos #define	c_mode_offset 14
76c09f92d2SPeter Avalos #define	c_mode_size 8
77c09f92d2SPeter Avalos #define	c_uid_offset 22
78c09f92d2SPeter Avalos #define	c_uid_size 8
79c09f92d2SPeter Avalos #define	c_gid_offset 30
80c09f92d2SPeter Avalos #define	c_gid_size 8
81c09f92d2SPeter Avalos #define	c_nlink_offset 38
82c09f92d2SPeter Avalos #define	c_nlink_size 8
83c09f92d2SPeter Avalos #define	c_mtime_offset 46
84c09f92d2SPeter Avalos #define	c_mtime_size 8
85c09f92d2SPeter Avalos #define	c_filesize_offset 54
86c09f92d2SPeter Avalos #define	c_filesize_size 8
87c09f92d2SPeter Avalos #define	c_devmajor_offset 62
88c09f92d2SPeter Avalos #define	c_devmajor_size 8
89c09f92d2SPeter Avalos #define	c_devminor_offset 70
90c09f92d2SPeter Avalos #define	c_devminor_size 8
91c09f92d2SPeter Avalos #define	c_rdevmajor_offset 78
92c09f92d2SPeter Avalos #define	c_rdevmajor_size 8
93c09f92d2SPeter Avalos #define	c_rdevminor_offset 86
94c09f92d2SPeter Avalos #define	c_rdevminor_size 8
95c09f92d2SPeter Avalos #define	c_namesize_offset 94
96c09f92d2SPeter Avalos #define	c_namesize_size 8
97c09f92d2SPeter Avalos #define	c_checksum_offset 102
98c09f92d2SPeter Avalos #define	c_checksum_size 8
99c09f92d2SPeter Avalos #define	c_header_size 110
10060b4ad09SPeter Avalos 
1019c82a63eSPeter Avalos /* Logic trick: difference between 'n' and next multiple of 4 */
1029c82a63eSPeter Avalos #define PAD4(n)	(3 & (1 + ~(n)))
1039c82a63eSPeter Avalos 
10460b4ad09SPeter Avalos /*
10560b4ad09SPeter Avalos  * Set output format to 'cpio' format.
10660b4ad09SPeter Avalos  */
10760b4ad09SPeter Avalos int
archive_write_set_format_cpio_newc(struct archive * _a)10860b4ad09SPeter Avalos archive_write_set_format_cpio_newc(struct archive *_a)
10960b4ad09SPeter Avalos {
11060b4ad09SPeter Avalos 	struct archive_write *a = (struct archive_write *)_a;
11160b4ad09SPeter Avalos 	struct cpio *cpio;
11260b4ad09SPeter Avalos 
113c09f92d2SPeter Avalos 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
114c09f92d2SPeter Avalos 	    ARCHIVE_STATE_NEW, "archive_write_set_format_cpio_newc");
115c09f92d2SPeter Avalos 
11660b4ad09SPeter Avalos 	/* If someone else was already registered, unregister them. */
117c09f92d2SPeter Avalos 	if (a->format_free != NULL)
118c09f92d2SPeter Avalos 		(a->format_free)(a);
11960b4ad09SPeter Avalos 
120e95abc47Szrj 	cpio = (struct cpio *)calloc(1, sizeof(*cpio));
12160b4ad09SPeter Avalos 	if (cpio == NULL) {
12260b4ad09SPeter Avalos 		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
12360b4ad09SPeter Avalos 		return (ARCHIVE_FATAL);
12460b4ad09SPeter Avalos 	}
12560b4ad09SPeter Avalos 	a->format_data = cpio;
1268029ab02SPeter Avalos 	a->format_name = "cpio";
127c09f92d2SPeter Avalos 	a->format_options = archive_write_newc_options;
12860b4ad09SPeter Avalos 	a->format_write_header = archive_write_newc_header;
12960b4ad09SPeter Avalos 	a->format_write_data = archive_write_newc_data;
13060b4ad09SPeter Avalos 	a->format_finish_entry = archive_write_newc_finish_entry;
131c09f92d2SPeter Avalos 	a->format_close = archive_write_newc_close;
132c09f92d2SPeter Avalos 	a->format_free = archive_write_newc_free;
13360b4ad09SPeter Avalos 	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
13460b4ad09SPeter Avalos 	a->archive.archive_format_name = "SVR4 cpio nocrc";
13560b4ad09SPeter Avalos 	return (ARCHIVE_OK);
13660b4ad09SPeter Avalos }
13760b4ad09SPeter Avalos 
13860b4ad09SPeter Avalos static int
archive_write_newc_options(struct archive_write * a,const char * key,const char * val)139c09f92d2SPeter Avalos archive_write_newc_options(struct archive_write *a, const char *key,
140c09f92d2SPeter Avalos     const char *val)
141c09f92d2SPeter Avalos {
142c09f92d2SPeter Avalos 	struct cpio *cpio = (struct cpio *)a->format_data;
143c09f92d2SPeter Avalos 	int ret = ARCHIVE_FAILED;
144c09f92d2SPeter Avalos 
145c09f92d2SPeter Avalos 	if (strcmp(key, "hdrcharset")  == 0) {
146c09f92d2SPeter Avalos 		if (val == NULL || val[0] == 0)
147c09f92d2SPeter Avalos 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
148c09f92d2SPeter Avalos 			    "%s: hdrcharset option needs a character-set name",
149c09f92d2SPeter Avalos 			    a->format_name);
150c09f92d2SPeter Avalos 		else {
151c09f92d2SPeter Avalos 			cpio->opt_sconv = archive_string_conversion_to_charset(
152c09f92d2SPeter Avalos 			    &a->archive, val, 0);
153c09f92d2SPeter Avalos 			if (cpio->opt_sconv != NULL)
154c09f92d2SPeter Avalos 				ret = ARCHIVE_OK;
155c09f92d2SPeter Avalos 			else
156c09f92d2SPeter Avalos 				ret = ARCHIVE_FATAL;
157c09f92d2SPeter Avalos 		}
158c09f92d2SPeter Avalos 		return (ret);
159c09f92d2SPeter Avalos 	}
160c09f92d2SPeter Avalos 
16159bf7050SPeter Avalos 	/* Note: The "warn" return is just to inform the options
16259bf7050SPeter Avalos 	 * supervisor that we didn't handle it.  It will generate
16359bf7050SPeter Avalos 	 * a suitable error if no one used this option. */
16459bf7050SPeter Avalos 	return (ARCHIVE_WARN);
16559bf7050SPeter Avalos }
16659bf7050SPeter Avalos 
167c09f92d2SPeter Avalos static struct archive_string_conv *
get_sconv(struct archive_write * a)168c09f92d2SPeter Avalos get_sconv(struct archive_write *a)
169c09f92d2SPeter Avalos {
170c09f92d2SPeter Avalos 	struct cpio *cpio;
171c09f92d2SPeter Avalos 	struct archive_string_conv *sconv;
172c09f92d2SPeter Avalos 
173c09f92d2SPeter Avalos 	cpio = (struct cpio *)a->format_data;
174c09f92d2SPeter Avalos 	sconv = cpio->opt_sconv;
175c09f92d2SPeter Avalos 	if (sconv == NULL) {
176c09f92d2SPeter Avalos 		if (!cpio->init_default_conversion) {
177c09f92d2SPeter Avalos 			cpio->sconv_default =
178c09f92d2SPeter Avalos 			    archive_string_default_conversion_for_write(
179c09f92d2SPeter Avalos 			      &(a->archive));
180c09f92d2SPeter Avalos 			cpio->init_default_conversion = 1;
181c09f92d2SPeter Avalos 		}
182c09f92d2SPeter Avalos 		sconv = cpio->sconv_default;
183c09f92d2SPeter Avalos 	}
184c09f92d2SPeter Avalos 	return (sconv);
185c09f92d2SPeter Avalos }
186c09f92d2SPeter Avalos 
187c09f92d2SPeter Avalos static int
archive_write_newc_header(struct archive_write * a,struct archive_entry * entry)18860b4ad09SPeter Avalos archive_write_newc_header(struct archive_write *a, struct archive_entry *entry)
18960b4ad09SPeter Avalos {
190c09f92d2SPeter Avalos 	const char *path;
191c09f92d2SPeter Avalos 	size_t len;
192c09f92d2SPeter Avalos 
193*50f8aa9cSAntonio Huete Jimenez 	if (archive_entry_filetype(entry) == 0 && archive_entry_hardlink(entry) == NULL) {
194c09f92d2SPeter Avalos 		archive_set_error(&a->archive, -1, "Filetype required");
195c09f92d2SPeter Avalos 		return (ARCHIVE_FAILED);
196c09f92d2SPeter Avalos 	}
197c09f92d2SPeter Avalos 
198c09f92d2SPeter Avalos 	if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
199c09f92d2SPeter Avalos 	    && errno == ENOMEM) {
200c09f92d2SPeter Avalos 		archive_set_error(&a->archive, ENOMEM,
201c09f92d2SPeter Avalos 		    "Can't allocate memory for Pathname");
202c09f92d2SPeter Avalos 		return (ARCHIVE_FATAL);
203c09f92d2SPeter Avalos 	}
204c09f92d2SPeter Avalos 	if (len == 0 || path == NULL || path[0] == '\0') {
205c09f92d2SPeter Avalos 		archive_set_error(&a->archive, -1, "Pathname required");
206c09f92d2SPeter Avalos 		return (ARCHIVE_FAILED);
207c09f92d2SPeter Avalos 	}
208c09f92d2SPeter Avalos 
209c09f92d2SPeter Avalos 	if (archive_entry_hardlink(entry) == NULL
210c09f92d2SPeter Avalos 	    && (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0)) {
211c09f92d2SPeter Avalos 		archive_set_error(&a->archive, -1, "Size required");
212c09f92d2SPeter Avalos 		return (ARCHIVE_FAILED);
213c09f92d2SPeter Avalos 	}
214c09f92d2SPeter Avalos 	return write_header(a, entry);
215c09f92d2SPeter Avalos }
216c09f92d2SPeter Avalos 
217c09f92d2SPeter Avalos static int
write_header(struct archive_write * a,struct archive_entry * entry)218c09f92d2SPeter Avalos write_header(struct archive_write *a, struct archive_entry *entry)
219c09f92d2SPeter Avalos {
2209c82a63eSPeter Avalos 	int64_t ino;
22160b4ad09SPeter Avalos 	struct cpio *cpio;
22260b4ad09SPeter Avalos 	const char *p, *path;
223c09f92d2SPeter Avalos 	int pathlength, ret, ret_final;
224c09f92d2SPeter Avalos 	char h[c_header_size];
225c09f92d2SPeter Avalos 	struct archive_string_conv *sconv;
22659bf7050SPeter Avalos 	struct archive_entry *entry_main;
227c09f92d2SPeter Avalos 	size_t len;
22860b4ad09SPeter Avalos 	int pad;
22960b4ad09SPeter Avalos 
23060b4ad09SPeter Avalos 	cpio = (struct cpio *)a->format_data;
231c09f92d2SPeter Avalos 	ret_final = ARCHIVE_OK;
232c09f92d2SPeter Avalos 	sconv = get_sconv(a);
23360b4ad09SPeter Avalos 
23459bf7050SPeter Avalos #if defined(_WIN32) && !defined(__CYGWIN__)
235e95abc47Szrj 	/* Make sure the path separators in pathname, hardlink and symlink
23659bf7050SPeter Avalos 	 * are all slash '/', not the Windows path separator '\'. */
23759bf7050SPeter Avalos 	entry_main = __la_win_entry_in_posix_pathseparator(entry);
23859bf7050SPeter Avalos 	if (entry_main == NULL) {
23959bf7050SPeter Avalos 		archive_set_error(&a->archive, ENOMEM,
24059bf7050SPeter Avalos 		    "Can't allocate ustar data");
24159bf7050SPeter Avalos 		return(ARCHIVE_FATAL);
24259bf7050SPeter Avalos 	}
24359bf7050SPeter Avalos 	if (entry != entry_main)
24459bf7050SPeter Avalos 		entry = entry_main;
24559bf7050SPeter Avalos 	else
24659bf7050SPeter Avalos 		entry_main = NULL;
24759bf7050SPeter Avalos #else
24859bf7050SPeter Avalos 	entry_main = NULL;
24959bf7050SPeter Avalos #endif
25059bf7050SPeter Avalos 
251c09f92d2SPeter Avalos 	ret = archive_entry_pathname_l(entry, &path, &len, sconv);
252c09f92d2SPeter Avalos 	if (ret != 0) {
253c09f92d2SPeter Avalos 		if (errno == ENOMEM) {
254c09f92d2SPeter Avalos 			archive_set_error(&a->archive, ENOMEM,
255c09f92d2SPeter Avalos 			    "Can't allocate memory for Pathname");
25659bf7050SPeter Avalos 			ret_final = ARCHIVE_FATAL;
25759bf7050SPeter Avalos 			goto exit_write_header;
258c09f92d2SPeter Avalos 		}
259c09f92d2SPeter Avalos 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
260c09f92d2SPeter Avalos 		    "Can't translate pathname '%s' to %s",
261c09f92d2SPeter Avalos 		    archive_entry_pathname(entry),
262c09f92d2SPeter Avalos 		    archive_string_conversion_charset_name(sconv));
263c09f92d2SPeter Avalos 		ret_final = ARCHIVE_WARN;
264c09f92d2SPeter Avalos 	}
265c09f92d2SPeter Avalos 	pathlength = (int)len + 1; /* Include trailing null. */
26660b4ad09SPeter Avalos 
267c09f92d2SPeter Avalos 	memset(h, 0, c_header_size);
268c09f92d2SPeter Avalos 	format_hex(0x070701, h + c_magic_offset, c_magic_size);
269c09f92d2SPeter Avalos 	format_hex(archive_entry_devmajor(entry), h + c_devmajor_offset,
270c09f92d2SPeter Avalos 	    c_devmajor_size);
271c09f92d2SPeter Avalos 	format_hex(archive_entry_devminor(entry), h + c_devminor_offset,
272c09f92d2SPeter Avalos 	    c_devminor_size);
2739c82a63eSPeter Avalos 
2749c82a63eSPeter Avalos 	ino = archive_entry_ino64(entry);
2759c82a63eSPeter Avalos 	if (ino > 0xffffffff) {
2769c82a63eSPeter Avalos 		archive_set_error(&a->archive, ERANGE,
2779c82a63eSPeter Avalos 		    "large inode number truncated");
278c09f92d2SPeter Avalos 		ret_final = ARCHIVE_WARN;
27960b4ad09SPeter Avalos 	}
28060b4ad09SPeter Avalos 
281c09f92d2SPeter Avalos 	/* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
282c09f92d2SPeter Avalos 	format_hex(ino & 0xffffffff, h + c_ino_offset, c_ino_size);
283c09f92d2SPeter Avalos 	format_hex(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
284c09f92d2SPeter Avalos 	format_hex(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
285c09f92d2SPeter Avalos 	format_hex(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
286c09f92d2SPeter Avalos 	format_hex(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
28760b4ad09SPeter Avalos 	if (archive_entry_filetype(entry) == AE_IFBLK
28860b4ad09SPeter Avalos 	    || archive_entry_filetype(entry) == AE_IFCHR) {
289c09f92d2SPeter Avalos 	    format_hex(archive_entry_rdevmajor(entry), h + c_rdevmajor_offset, c_rdevmajor_size);
290c09f92d2SPeter Avalos 	    format_hex(archive_entry_rdevminor(entry), h + c_rdevminor_offset, c_rdevminor_size);
29160b4ad09SPeter Avalos 	} else {
292c09f92d2SPeter Avalos 	    format_hex(0, h + c_rdevmajor_offset, c_rdevmajor_size);
293c09f92d2SPeter Avalos 	    format_hex(0, h + c_rdevminor_offset, c_rdevminor_size);
29460b4ad09SPeter Avalos 	}
295c09f92d2SPeter Avalos 	format_hex(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
296c09f92d2SPeter Avalos 	format_hex(pathlength, h + c_namesize_offset, c_namesize_size);
297c09f92d2SPeter Avalos 	format_hex(0, h + c_checksum_offset, c_checksum_size);
29860b4ad09SPeter Avalos 
29960b4ad09SPeter Avalos 	/* Non-regular files don't store bodies. */
30060b4ad09SPeter Avalos 	if (archive_entry_filetype(entry) != AE_IFREG)
30160b4ad09SPeter Avalos 		archive_entry_set_size(entry, 0);
30260b4ad09SPeter Avalos 
30360b4ad09SPeter Avalos 	/* Symlinks get the link written as the body of the entry. */
304c09f92d2SPeter Avalos 	ret = archive_entry_symlink_l(entry, &p, &len, sconv);
305c09f92d2SPeter Avalos 	if (ret != 0) {
306c09f92d2SPeter Avalos 		if (errno == ENOMEM) {
307c09f92d2SPeter Avalos 			archive_set_error(&a->archive, ENOMEM,
308c09f92d2SPeter Avalos 			    "Can't allocate memory for Likname");
30959bf7050SPeter Avalos 			ret_final = ARCHIVE_FATAL;
31059bf7050SPeter Avalos 			goto exit_write_header;
311c09f92d2SPeter Avalos 		}
312c09f92d2SPeter Avalos 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
313c09f92d2SPeter Avalos 		    "Can't translate linkname '%s' to %s",
314c09f92d2SPeter Avalos 		    archive_entry_symlink(entry),
315c09f92d2SPeter Avalos 		    archive_string_conversion_charset_name(sconv));
316c09f92d2SPeter Avalos 		ret_final = ARCHIVE_WARN;
317c09f92d2SPeter Avalos 	}
318c09f92d2SPeter Avalos 	if (len > 0 && p != NULL  &&  *p != '\0')
319c09f92d2SPeter Avalos 		ret = format_hex(strlen(p), h + c_filesize_offset,
320c09f92d2SPeter Avalos 		    c_filesize_size);
32160b4ad09SPeter Avalos 	else
322c09f92d2SPeter Avalos 		ret = format_hex(archive_entry_size(entry),
323c09f92d2SPeter Avalos 		    h + c_filesize_offset, c_filesize_size);
324c09f92d2SPeter Avalos 	if (ret) {
325c09f92d2SPeter Avalos 		archive_set_error(&a->archive, ERANGE,
326c09f92d2SPeter Avalos 		    "File is too large for this format.");
32759bf7050SPeter Avalos 		ret_final = ARCHIVE_FAILED;
32859bf7050SPeter Avalos 		goto exit_write_header;
329c09f92d2SPeter Avalos 	}
33060b4ad09SPeter Avalos 
331c09f92d2SPeter Avalos 	ret = __archive_write_output(a, h, c_header_size);
33259bf7050SPeter Avalos 	if (ret != ARCHIVE_OK) {
33359bf7050SPeter Avalos 		ret_final = ARCHIVE_FATAL;
33459bf7050SPeter Avalos 		goto exit_write_header;
33559bf7050SPeter Avalos 	}
33660b4ad09SPeter Avalos 
33760b4ad09SPeter Avalos 	/* Pad pathname to even length. */
338c09f92d2SPeter Avalos 	ret = __archive_write_output(a, path, pathlength);
33959bf7050SPeter Avalos 	if (ret != ARCHIVE_OK) {
34059bf7050SPeter Avalos 		ret_final = ARCHIVE_FATAL;
34159bf7050SPeter Avalos 		goto exit_write_header;
34259bf7050SPeter Avalos 	}
343c09f92d2SPeter Avalos 	pad = PAD4(pathlength + c_header_size);
344c09f92d2SPeter Avalos 	if (pad) {
345c09f92d2SPeter Avalos 		ret = __archive_write_output(a, "\0\0\0", pad);
34659bf7050SPeter Avalos 		if (ret != ARCHIVE_OK) {
34759bf7050SPeter Avalos 			ret_final = ARCHIVE_FATAL;
34859bf7050SPeter Avalos 			goto exit_write_header;
34959bf7050SPeter Avalos 		}
350c09f92d2SPeter Avalos 	}
35160b4ad09SPeter Avalos 
35260b4ad09SPeter Avalos 	cpio->entry_bytes_remaining = archive_entry_size(entry);
35359bf7050SPeter Avalos 	cpio->padding = (int)PAD4(cpio->entry_bytes_remaining);
35460b4ad09SPeter Avalos 
35560b4ad09SPeter Avalos 	/* Write the symlink now. */
35660b4ad09SPeter Avalos 	if (p != NULL  &&  *p != '\0') {
357c09f92d2SPeter Avalos 		ret = __archive_write_output(a, p, strlen(p));
35859bf7050SPeter Avalos 		if (ret != ARCHIVE_OK) {
35959bf7050SPeter Avalos 			ret_final = ARCHIVE_FATAL;
36059bf7050SPeter Avalos 			goto exit_write_header;
36159bf7050SPeter Avalos 		}
3629c82a63eSPeter Avalos 		pad = PAD4(strlen(p));
363c09f92d2SPeter Avalos 		ret = __archive_write_output(a, "\0\0\0", pad);
36459bf7050SPeter Avalos 		if (ret != ARCHIVE_OK) {
36559bf7050SPeter Avalos 			ret_final = ARCHIVE_FATAL;
36659bf7050SPeter Avalos 			goto exit_write_header;
36760b4ad09SPeter Avalos 		}
36859bf7050SPeter Avalos 	}
36959bf7050SPeter Avalos exit_write_header:
37059bf7050SPeter Avalos 	archive_entry_free(entry_main);
371c09f92d2SPeter Avalos 	return (ret_final);
37260b4ad09SPeter Avalos }
37360b4ad09SPeter Avalos 
37460b4ad09SPeter Avalos static ssize_t
archive_write_newc_data(struct archive_write * a,const void * buff,size_t s)37560b4ad09SPeter Avalos archive_write_newc_data(struct archive_write *a, const void *buff, size_t s)
37660b4ad09SPeter Avalos {
37760b4ad09SPeter Avalos 	struct cpio *cpio;
37860b4ad09SPeter Avalos 	int ret;
37960b4ad09SPeter Avalos 
38060b4ad09SPeter Avalos 	cpio = (struct cpio *)a->format_data;
38160b4ad09SPeter Avalos 	if (s > cpio->entry_bytes_remaining)
38259bf7050SPeter Avalos 		s = (size_t)cpio->entry_bytes_remaining;
38360b4ad09SPeter Avalos 
384c09f92d2SPeter Avalos 	ret = __archive_write_output(a, buff, s);
38560b4ad09SPeter Avalos 	cpio->entry_bytes_remaining -= s;
38660b4ad09SPeter Avalos 	if (ret >= 0)
38760b4ad09SPeter Avalos 		return (s);
38860b4ad09SPeter Avalos 	else
38960b4ad09SPeter Avalos 		return (ret);
39060b4ad09SPeter Avalos }
39160b4ad09SPeter Avalos 
39260b4ad09SPeter Avalos /*
39360b4ad09SPeter Avalos  * Format a number into the specified field.
39460b4ad09SPeter Avalos  */
39560b4ad09SPeter Avalos static int
format_hex(int64_t v,void * p,int digits)39660b4ad09SPeter Avalos format_hex(int64_t v, void *p, int digits)
39760b4ad09SPeter Avalos {
39860b4ad09SPeter Avalos 	int64_t	max;
39960b4ad09SPeter Avalos 	int	ret;
40060b4ad09SPeter Avalos 
40160b4ad09SPeter Avalos 	max = (((int64_t)1) << (digits * 4)) - 1;
40260b4ad09SPeter Avalos 	if (v >= 0  &&  v <= max) {
40360b4ad09SPeter Avalos 	    format_hex_recursive(v, (char *)p, digits);
40460b4ad09SPeter Avalos 	    ret = 0;
40560b4ad09SPeter Avalos 	} else {
40660b4ad09SPeter Avalos 	    format_hex_recursive(max, (char *)p, digits);
40760b4ad09SPeter Avalos 	    ret = -1;
40860b4ad09SPeter Avalos 	}
40960b4ad09SPeter Avalos 	return (ret);
41060b4ad09SPeter Avalos }
41160b4ad09SPeter Avalos 
41260b4ad09SPeter Avalos static int64_t
format_hex_recursive(int64_t v,char * p,int s)41360b4ad09SPeter Avalos format_hex_recursive(int64_t v, char *p, int s)
41460b4ad09SPeter Avalos {
41560b4ad09SPeter Avalos 	if (s == 0)
41660b4ad09SPeter Avalos 		return (v);
41760b4ad09SPeter Avalos 	v = format_hex_recursive(v, p+1, s-1);
41860b4ad09SPeter Avalos 	*p = "0123456789abcdef"[v & 0xf];
4199c82a63eSPeter Avalos 	return (v >> 4);
42060b4ad09SPeter Avalos }
42160b4ad09SPeter Avalos 
42260b4ad09SPeter Avalos static int
archive_write_newc_close(struct archive_write * a)423c09f92d2SPeter Avalos archive_write_newc_close(struct archive_write *a)
42460b4ad09SPeter Avalos {
42560b4ad09SPeter Avalos 	int er;
42660b4ad09SPeter Avalos 	struct archive_entry *trailer;
42760b4ad09SPeter Avalos 
42860b4ad09SPeter Avalos 	trailer = archive_entry_new();
42960b4ad09SPeter Avalos 	archive_entry_set_nlink(trailer, 1);
430c09f92d2SPeter Avalos 	archive_entry_set_size(trailer, 0);
43160b4ad09SPeter Avalos 	archive_entry_set_pathname(trailer, "TRAILER!!!");
432c09f92d2SPeter Avalos 	/* Bypass the required data checks. */
433c09f92d2SPeter Avalos 	er = write_header(a, trailer);
43460b4ad09SPeter Avalos 	archive_entry_free(trailer);
43560b4ad09SPeter Avalos 	return (er);
43660b4ad09SPeter Avalos }
43760b4ad09SPeter Avalos 
43860b4ad09SPeter Avalos static int
archive_write_newc_free(struct archive_write * a)439c09f92d2SPeter Avalos archive_write_newc_free(struct archive_write *a)
44060b4ad09SPeter Avalos {
44160b4ad09SPeter Avalos 	struct cpio *cpio;
44260b4ad09SPeter Avalos 
44360b4ad09SPeter Avalos 	cpio = (struct cpio *)a->format_data;
44460b4ad09SPeter Avalos 	free(cpio);
44560b4ad09SPeter Avalos 	a->format_data = NULL;
44660b4ad09SPeter Avalos 	return (ARCHIVE_OK);
44760b4ad09SPeter Avalos }
44860b4ad09SPeter Avalos 
44960b4ad09SPeter Avalos static int
archive_write_newc_finish_entry(struct archive_write * a)45060b4ad09SPeter Avalos archive_write_newc_finish_entry(struct archive_write *a)
45160b4ad09SPeter Avalos {
45260b4ad09SPeter Avalos 	struct cpio *cpio;
45360b4ad09SPeter Avalos 
45460b4ad09SPeter Avalos 	cpio = (struct cpio *)a->format_data;
45559bf7050SPeter Avalos 	return (__archive_write_nulls(a,
45659bf7050SPeter Avalos 		(size_t)cpio->entry_bytes_remaining + cpio->padding));
45760b4ad09SPeter Avalos }
458