Blame libarchive/archive_write_set_format_cpio_newc.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2007 Tim Kientzle
Packit Service 1d0348
 * Copyright (c) 2006 Rudolf Marek SYSGO s.r.o.
Packit Service 1d0348
 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
#include "archive_platform.h"
Packit Service 1d0348
__FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_cpio_newc.c 201160 2009-12-29 05:41:57Z kientzle $");
Packit Service 1d0348
Packit Service 1d0348
#ifdef HAVE_ERRNO_H
Packit Service 1d0348
#include <errno.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#include <stdio.h>
Packit Service 1d0348
#ifdef HAVE_STDLIB_H
Packit Service 1d0348
#include <stdlib.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STRING_H
Packit Service 1d0348
#include <string.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
#include "archive.h"
Packit Service 1d0348
#include "archive_entry.h"
Packit Service 1d0348
#include "archive_entry_locale.h"
Packit Service 1d0348
#include "archive_private.h"
Packit Service 1d0348
#include "archive_write_private.h"
Packit Service 1d0348
Packit Service 1d0348
static ssize_t	archive_write_newc_data(struct archive_write *,
Packit Service 1d0348
		    const void *buff, size_t s);
Packit Service 1d0348
static int	archive_write_newc_close(struct archive_write *);
Packit Service 1d0348
static int	archive_write_newc_free(struct archive_write *);
Packit Service 1d0348
static int	archive_write_newc_finish_entry(struct archive_write *);
Packit Service 1d0348
static int	archive_write_newc_header(struct archive_write *,
Packit Service 1d0348
		    struct archive_entry *);
Packit Service 1d0348
static int      archive_write_newc_options(struct archive_write *,
Packit Service 1d0348
		    const char *, const char *);
Packit Service 1d0348
static int	format_hex(int64_t, void *, int);
Packit Service 1d0348
static int64_t	format_hex_recursive(int64_t, char *, int);
Packit Service 1d0348
static int	write_header(struct archive_write *, struct archive_entry *);
Packit Service 1d0348
Packit Service 1d0348
struct cpio {
Packit Service 1d0348
	uint64_t	  entry_bytes_remaining;
Packit Service 1d0348
	int		  padding;
Packit Service 1d0348
Packit Service 1d0348
	struct archive_string_conv *opt_sconv;
Packit Service 1d0348
	struct archive_string_conv *sconv_default;
Packit Service 1d0348
	int		  init_default_conversion;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
#define	c_magic_offset 0
Packit Service 1d0348
#define	c_magic_size 6
Packit Service 1d0348
#define	c_ino_offset 6
Packit Service 1d0348
#define	c_ino_size 8
Packit Service 1d0348
#define	c_mode_offset 14
Packit Service 1d0348
#define	c_mode_size 8
Packit Service 1d0348
#define	c_uid_offset 22
Packit Service 1d0348
#define	c_uid_size 8
Packit Service 1d0348
#define	c_gid_offset 30
Packit Service 1d0348
#define	c_gid_size 8
Packit Service 1d0348
#define	c_nlink_offset 38
Packit Service 1d0348
#define	c_nlink_size 8
Packit Service 1d0348
#define	c_mtime_offset 46
Packit Service 1d0348
#define	c_mtime_size 8
Packit Service 1d0348
#define	c_filesize_offset 54
Packit Service 1d0348
#define	c_filesize_size 8
Packit Service 1d0348
#define	c_devmajor_offset 62
Packit Service 1d0348
#define	c_devmajor_size 8
Packit Service 1d0348
#define	c_devminor_offset 70
Packit Service 1d0348
#define	c_devminor_size 8
Packit Service 1d0348
#define	c_rdevmajor_offset 78
Packit Service 1d0348
#define	c_rdevmajor_size 8
Packit Service 1d0348
#define	c_rdevminor_offset 86
Packit Service 1d0348
#define	c_rdevminor_size 8
Packit Service 1d0348
#define	c_namesize_offset 94
Packit Service 1d0348
#define	c_namesize_size 8
Packit Service 1d0348
#define	c_checksum_offset 102
Packit Service 1d0348
#define	c_checksum_size 8
Packit Service 1d0348
#define	c_header_size 110
Packit Service 1d0348
Packit Service 1d0348
/* Logic trick: difference between 'n' and next multiple of 4 */
Packit Service 1d0348
#define PAD4(n)	(3 & (1 + ~(n)))
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Set output format to 'cpio' format.
Packit Service 1d0348
 */
Packit Service 1d0348
int
Packit Service 1d0348
archive_write_set_format_cpio_newc(struct archive *_a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_write *a = (struct archive_write *)_a;
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
Packit Service 1d0348
	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
Packit Service 1d0348
	    ARCHIVE_STATE_NEW, "archive_write_set_format_cpio_newc");
Packit Service 1d0348
Packit Service 1d0348
	/* If someone else was already registered, unregister them. */
Packit Service 1d0348
	if (a->format_free != NULL)
Packit Service 1d0348
		(a->format_free)(a);
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)calloc(1, sizeof(*cpio));
Packit Service 1d0348
	if (cpio == NULL) {
Packit Service 1d0348
		archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	a->format_data = cpio;
Packit Service 1d0348
	a->format_name = "cpio";
Packit Service 1d0348
	a->format_options = archive_write_newc_options;
Packit Service 1d0348
	a->format_write_header = archive_write_newc_header;
Packit Service 1d0348
	a->format_write_data = archive_write_newc_data;
Packit Service 1d0348
	a->format_finish_entry = archive_write_newc_finish_entry;
Packit Service 1d0348
	a->format_close = archive_write_newc_close;
Packit Service 1d0348
	a->format_free = archive_write_newc_free;
Packit Service 1d0348
	a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
Packit Service 1d0348
	a->archive.archive_format_name = "SVR4 cpio nocrc";
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_newc_options(struct archive_write *a, const char *key,
Packit Service 1d0348
    const char *val)
Packit Service 1d0348
{
Packit Service 1d0348
	struct cpio *cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	int ret = ARCHIVE_FAILED;
Packit Service 1d0348
Packit Service 1d0348
	if (strcmp(key, "hdrcharset")  == 0) {
Packit Service 1d0348
		if (val == NULL || val[0] == 0)
Packit Service 1d0348
			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
			    "%s: hdrcharset option needs a character-set name",
Packit Service 1d0348
			    a->format_name);
Packit Service 1d0348
		else {
Packit Service 1d0348
			cpio->opt_sconv = archive_string_conversion_to_charset(
Packit Service 1d0348
			    &a->archive, val, 0);
Packit Service 1d0348
			if (cpio->opt_sconv != NULL)
Packit Service 1d0348
				ret = ARCHIVE_OK;
Packit Service 1d0348
			else
Packit Service 1d0348
				ret = ARCHIVE_FATAL;
Packit Service 1d0348
		}
Packit Service 1d0348
		return (ret);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Note: The "warn" return is just to inform the options
Packit Service 1d0348
	 * supervisor that we didn't handle it.  It will generate
Packit Service 1d0348
	 * a suitable error if no one used this option. */
Packit Service 1d0348
	return (ARCHIVE_WARN);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static struct archive_string_conv *
Packit Service 1d0348
get_sconv(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
	struct archive_string_conv *sconv;
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	sconv = cpio->opt_sconv;
Packit Service 1d0348
	if (sconv == NULL) {
Packit Service 1d0348
		if (!cpio->init_default_conversion) {
Packit Service 1d0348
			cpio->sconv_default =
Packit Service 1d0348
			    archive_string_default_conversion_for_write(
Packit Service 1d0348
			      &(a->archive));
Packit Service 1d0348
			cpio->init_default_conversion = 1;
Packit Service 1d0348
		}
Packit Service 1d0348
		sconv = cpio->sconv_default;
Packit Service 1d0348
	}
Packit Service 1d0348
	return (sconv);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_newc_header(struct archive_write *a, struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	const char *path;
Packit Service 1d0348
	size_t len;
Packit Service 1d0348
Packit Service 1d0348
	if (archive_entry_filetype(entry) == 0) {
Packit Service 1d0348
		archive_set_error(&a->archive, -1, "Filetype required");
Packit Service 1d0348
		return (ARCHIVE_FAILED);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (archive_entry_pathname_l(entry, &path, &len, get_sconv(a)) != 0
Packit Service 1d0348
	    && errno == ENOMEM) {
Packit Service 1d0348
		archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
		    "Can't allocate memory for Pathname");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	if (len == 0 || path == NULL || path[0] == '\0') {
Packit Service 1d0348
		archive_set_error(&a->archive, -1, "Pathname required");
Packit Service 1d0348
		return (ARCHIVE_FAILED);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (archive_entry_hardlink(entry) == NULL
Packit Service 1d0348
	    && (!archive_entry_size_is_set(entry) || archive_entry_size(entry) < 0)) {
Packit Service 1d0348
		archive_set_error(&a->archive, -1, "Size required");
Packit Service 1d0348
		return (ARCHIVE_FAILED);
Packit Service 1d0348
	}
Packit Service 1d0348
	return write_header(a, entry);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
write_header(struct archive_write *a, struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	int64_t ino;
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
	const char *p, *path;
Packit Service 1d0348
	int pathlength, ret, ret_final;
Packit Service 1d0348
	char h[c_header_size];
Packit Service 1d0348
	struct archive_string_conv *sconv;
Packit Service 1d0348
	struct archive_entry *entry_main;
Packit Service 1d0348
	size_t len;
Packit Service 1d0348
	int pad;
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	ret_final = ARCHIVE_OK;
Packit Service 1d0348
	sconv = get_sconv(a);
Packit Service 1d0348
Packit Service 1d0348
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit Service 1d0348
	/* Make sure the path separators in pathname, hardlink and symlink
Packit Service 1d0348
	 * are all slash '/', not the Windows path separator '\'. */
Packit Service 1d0348
	entry_main = __la_win_entry_in_posix_pathseparator(entry);
Packit Service 1d0348
	if (entry_main == NULL) {
Packit Service 1d0348
		archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
		    "Can't allocate ustar data");
Packit Service 1d0348
		return(ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	if (entry != entry_main)
Packit Service 1d0348
		entry = entry_main;
Packit Service 1d0348
	else
Packit Service 1d0348
		entry_main = NULL;
Packit Service 1d0348
#else
Packit Service 1d0348
	entry_main = NULL;
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
	ret = archive_entry_pathname_l(entry, &path, &len, sconv);
Packit Service 1d0348
	if (ret != 0) {
Packit Service 1d0348
		if (errno == ENOMEM) {
Packit Service 1d0348
			archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
			    "Can't allocate memory for Pathname");
Packit Service 1d0348
			ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
			goto exit_write_header;
Packit Service 1d0348
		}
Packit Service 1d0348
		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
Packit Service 1d0348
		    "Can't translate pathname '%s' to %s",
Packit Service 1d0348
		    archive_entry_pathname(entry),
Packit Service 1d0348
		    archive_string_conversion_charset_name(sconv));
Packit Service 1d0348
		ret_final = ARCHIVE_WARN;
Packit Service 1d0348
	}
Packit Service 1d0348
	pathlength = (int)len + 1; /* Include trailing null. */
Packit Service 1d0348
Packit Service 1d0348
	memset(h, 0, c_header_size);
Packit Service 1d0348
	format_hex(0x070701, h + c_magic_offset, c_magic_size);
Packit Service 1d0348
	format_hex(archive_entry_devmajor(entry), h + c_devmajor_offset,
Packit Service 1d0348
	    c_devmajor_size);
Packit Service 1d0348
	format_hex(archive_entry_devminor(entry), h + c_devminor_offset,
Packit Service 1d0348
	    c_devminor_size);
Packit Service 1d0348
Packit Service 1d0348
	ino = archive_entry_ino64(entry);
Packit Service 1d0348
	if (ino > 0xffffffff) {
Packit Service 1d0348
		archive_set_error(&a->archive, ERANGE,
Packit Service 1d0348
		    "large inode number truncated");
Packit Service 1d0348
		ret_final = ARCHIVE_WARN;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* TODO: Set ret_final to ARCHIVE_WARN if any of these overflow. */
Packit Service 1d0348
	format_hex(ino & 0xffffffff, h + c_ino_offset, c_ino_size);
Packit Service 1d0348
	format_hex(archive_entry_mode(entry), h + c_mode_offset, c_mode_size);
Packit Service 1d0348
	format_hex(archive_entry_uid(entry), h + c_uid_offset, c_uid_size);
Packit Service 1d0348
	format_hex(archive_entry_gid(entry), h + c_gid_offset, c_gid_size);
Packit Service 1d0348
	format_hex(archive_entry_nlink(entry), h + c_nlink_offset, c_nlink_size);
Packit Service 1d0348
	if (archive_entry_filetype(entry) == AE_IFBLK
Packit Service 1d0348
	    || archive_entry_filetype(entry) == AE_IFCHR) {
Packit Service 1d0348
	    format_hex(archive_entry_rdevmajor(entry), h + c_rdevmajor_offset, c_rdevmajor_size);
Packit Service 1d0348
	    format_hex(archive_entry_rdevminor(entry), h + c_rdevminor_offset, c_rdevminor_size);
Packit Service 1d0348
	} else {
Packit Service 1d0348
	    format_hex(0, h + c_rdevmajor_offset, c_rdevmajor_size);
Packit Service 1d0348
	    format_hex(0, h + c_rdevminor_offset, c_rdevminor_size);
Packit Service 1d0348
	}
Packit Service 1d0348
	format_hex(archive_entry_mtime(entry), h + c_mtime_offset, c_mtime_size);
Packit Service 1d0348
	format_hex(pathlength, h + c_namesize_offset, c_namesize_size);
Packit Service 1d0348
	format_hex(0, h + c_checksum_offset, c_checksum_size);
Packit Service 1d0348
Packit Service 1d0348
	/* Non-regular files don't store bodies. */
Packit Service 1d0348
	if (archive_entry_filetype(entry) != AE_IFREG)
Packit Service 1d0348
		archive_entry_set_size(entry, 0);
Packit Service 1d0348
Packit Service 1d0348
	/* Symlinks get the link written as the body of the entry. */
Packit Service 1d0348
	ret = archive_entry_symlink_l(entry, &p, &len, sconv);
Packit Service 1d0348
	if (ret != 0) {
Packit Service 1d0348
		if (errno == ENOMEM) {
Packit Service 1d0348
			archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
			    "Can't allocate memory for Likname");
Packit Service 1d0348
			ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
			goto exit_write_header;
Packit Service 1d0348
		}
Packit Service 1d0348
		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
Packit Service 1d0348
		    "Can't translate linkname '%s' to %s",
Packit Service 1d0348
		    archive_entry_symlink(entry),
Packit Service 1d0348
		    archive_string_conversion_charset_name(sconv));
Packit Service 1d0348
		ret_final = ARCHIVE_WARN;
Packit Service 1d0348
	}
Packit Service 1d0348
	if (len > 0 && p != NULL  &&  *p != '\0')
Packit Service 1d0348
		ret = format_hex(strlen(p), h + c_filesize_offset,
Packit Service 1d0348
		    c_filesize_size);
Packit Service 1d0348
	else
Packit Service 1d0348
		ret = format_hex(archive_entry_size(entry),
Packit Service 1d0348
		    h + c_filesize_offset, c_filesize_size);
Packit Service 1d0348
	if (ret) {
Packit Service 1d0348
		archive_set_error(&a->archive, ERANGE,
Packit Service 1d0348
		    "File is too large for this format.");
Packit Service 1d0348
		ret_final = ARCHIVE_FAILED;
Packit Service 1d0348
		goto exit_write_header;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	ret = __archive_write_output(a, h, c_header_size);
Packit Service 1d0348
	if (ret != ARCHIVE_OK) {
Packit Service 1d0348
		ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
		goto exit_write_header;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Pad pathname to even length. */
Packit Service 1d0348
	ret = __archive_write_output(a, path, pathlength);
Packit Service 1d0348
	if (ret != ARCHIVE_OK) {
Packit Service 1d0348
		ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
		goto exit_write_header;
Packit Service 1d0348
	}
Packit Service 1d0348
	pad = PAD4(pathlength + c_header_size);
Packit Service 1d0348
	if (pad) {
Packit Service 1d0348
		ret = __archive_write_output(a, "\0\0\0", pad);
Packit Service 1d0348
		if (ret != ARCHIVE_OK) {
Packit Service 1d0348
			ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
			goto exit_write_header;
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	cpio->entry_bytes_remaining = archive_entry_size(entry);
Packit Service 1d0348
	cpio->padding = (int)PAD4(cpio->entry_bytes_remaining);
Packit Service 1d0348
Packit Service 1d0348
	/* Write the symlink now. */
Packit Service 1d0348
	if (p != NULL  &&  *p != '\0') {
Packit Service 1d0348
		ret = __archive_write_output(a, p, strlen(p));
Packit Service 1d0348
		if (ret != ARCHIVE_OK) {
Packit Service 1d0348
			ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
			goto exit_write_header;
Packit Service 1d0348
		}
Packit Service 1d0348
		pad = PAD4(strlen(p));
Packit Service 1d0348
		ret = __archive_write_output(a, "\0\0\0", pad);
Packit Service 1d0348
		if (ret != ARCHIVE_OK) {
Packit Service 1d0348
			ret_final = ARCHIVE_FATAL;
Packit Service 1d0348
			goto exit_write_header;
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
exit_write_header:
Packit Service 1d0348
	if (entry_main)
Packit Service 1d0348
		archive_entry_free(entry_main);
Packit Service 1d0348
	return (ret_final);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static ssize_t
Packit Service 1d0348
archive_write_newc_data(struct archive_write *a, const void *buff, size_t s)
Packit Service 1d0348
{
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
	int ret;
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	if (s > cpio->entry_bytes_remaining)
Packit Service 1d0348
		s = (size_t)cpio->entry_bytes_remaining;
Packit Service 1d0348
Packit Service 1d0348
	ret = __archive_write_output(a, buff, s);
Packit Service 1d0348
	cpio->entry_bytes_remaining -= s;
Packit Service 1d0348
	if (ret >= 0)
Packit Service 1d0348
		return (s);
Packit Service 1d0348
	else
Packit Service 1d0348
		return (ret);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Format a number into the specified field.
Packit Service 1d0348
 */
Packit Service 1d0348
static int
Packit Service 1d0348
format_hex(int64_t v, void *p, int digits)
Packit Service 1d0348
{
Packit Service 1d0348
	int64_t	max;
Packit Service 1d0348
	int	ret;
Packit Service 1d0348
Packit Service 1d0348
	max = (((int64_t)1) << (digits * 4)) - 1;
Packit Service 1d0348
	if (v >= 0  &&  v <= max) {
Packit Service 1d0348
	    format_hex_recursive(v, (char *)p, digits);
Packit Service 1d0348
	    ret = 0;
Packit Service 1d0348
	} else {
Packit Service 1d0348
	    format_hex_recursive(max, (char *)p, digits);
Packit Service 1d0348
	    ret = -1;
Packit Service 1d0348
	}
Packit Service 1d0348
	return (ret);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int64_t
Packit Service 1d0348
format_hex_recursive(int64_t v, char *p, int s)
Packit Service 1d0348
{
Packit Service 1d0348
	if (s == 0)
Packit Service 1d0348
		return (v);
Packit Service 1d0348
	v = format_hex_recursive(v, p+1, s-1);
Packit Service 1d0348
	*p = "0123456789abcdef"[v & 0xf];
Packit Service 1d0348
	return (v >> 4);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_newc_close(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	int er;
Packit Service 1d0348
	struct archive_entry *trailer;
Packit Service 1d0348
Packit Service 1d0348
	trailer = archive_entry_new();
Packit Service 1d0348
	archive_entry_set_nlink(trailer, 1);
Packit Service 1d0348
	archive_entry_set_size(trailer, 0);
Packit Service 1d0348
	archive_entry_set_pathname(trailer, "TRAILER!!!");
Packit Service 1d0348
	/* Bypass the required data checks. */
Packit Service 1d0348
	er = write_header(a, trailer);
Packit Service 1d0348
	archive_entry_free(trailer);
Packit Service 1d0348
	return (er);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_newc_free(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	free(cpio);
Packit Service 1d0348
	a->format_data = NULL;
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_write_newc_finish_entry(struct archive_write *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct cpio *cpio;
Packit Service 1d0348
Packit Service 1d0348
	cpio = (struct cpio *)a->format_data;
Packit Service 1d0348
	return (__archive_write_nulls(a,
Packit Service 1d0348
		(size_t)cpio->entry_bytes_remaining + cpio->padding));
Packit Service 1d0348
}