Home | History | Annotate | Line # | Download | only in libarchive
      1 /*-
      2  * Copyright (c) 2003-2007 Tim Kientzle
      3  * Copyright (c) 2011-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 
     29 #ifdef HAVE_ERRNO_H
     30 #include <errno.h>
     31 #endif
     32 #include <stdio.h>
     33 #ifdef HAVE_STDLIB_H
     34 #include <stdlib.h>
     35 #endif
     36 #ifdef HAVE_STRING_H
     37 #include <string.h>
     38 #endif
     39 
     40 #include "archive.h"
     41 #include "archive_entry.h"
     42 #include "archive_entry_locale.h"
     43 #include "archive_private.h"
     44 #include "archive_write_private.h"
     45 #include "archive_write_set_format_private.h"
     46 
     47 struct ustar {
     48 	uint64_t	entry_bytes_remaining;
     49 	uint64_t	entry_padding;
     50 
     51 	struct archive_string_conv *opt_sconv;
     52 	struct archive_string_conv *sconv_default;
     53 	int	init_default_conversion;
     54 };
     55 
     56 /*
     57  * Define structure of POSIX 'ustar' tar header.
     58  */
     59 #define	USTAR_name_offset 0
     60 #define	USTAR_name_size 100
     61 #define	USTAR_mode_offset 100
     62 #define	USTAR_mode_size 6
     63 #define	USTAR_mode_max_size 8
     64 #define	USTAR_uid_offset 108
     65 #define	USTAR_uid_size 6
     66 #define	USTAR_uid_max_size 8
     67 #define	USTAR_gid_offset 116
     68 #define	USTAR_gid_size 6
     69 #define	USTAR_gid_max_size 8
     70 #define	USTAR_size_offset 124
     71 #define	USTAR_size_size 11
     72 #define	USTAR_size_max_size 12
     73 #define	USTAR_mtime_offset 136
     74 #define	USTAR_mtime_size 11
     75 #define	USTAR_mtime_max_size 11
     76 #define	USTAR_checksum_offset 148
     77 #define	USTAR_checksum_size 8
     78 #define	USTAR_typeflag_offset 156
     79 #define	USTAR_typeflag_size 1
     80 #define	USTAR_linkname_offset 157
     81 #define	USTAR_linkname_size 100
     82 #define	USTAR_magic_offset 257
     83 #define	USTAR_magic_size 6
     84 #define	USTAR_version_offset 263
     85 #define	USTAR_version_size 2
     86 #define	USTAR_uname_offset 265
     87 #define	USTAR_uname_size 32
     88 #define	USTAR_gname_offset 297
     89 #define	USTAR_gname_size 32
     90 #define	USTAR_rdevmajor_offset 329
     91 #define	USTAR_rdevmajor_size 6
     92 #define	USTAR_rdevmajor_max_size 8
     93 #define	USTAR_rdevminor_offset 337
     94 #define	USTAR_rdevminor_size 6
     95 #define	USTAR_rdevminor_max_size 8
     96 #define	USTAR_prefix_offset 345
     97 #define	USTAR_prefix_size 155
     98 #define	USTAR_padding_offset 500
     99 #define	USTAR_padding_size 12
    100 
    101 /*
    102  * A filled-in copy of the header for initialization.
    103  */
    104 static const char template_header[] = {
    105 	/* name: 100 bytes */
    106 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    107 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    108 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    109 	0,0,0,0,
    110 	/* Mode, space-null termination: 8 bytes */
    111 	'0','0','0','0','0','0', ' ','\0',
    112 	/* uid, space-null termination: 8 bytes */
    113 	'0','0','0','0','0','0', ' ','\0',
    114 	/* gid, space-null termination: 8 bytes */
    115 	'0','0','0','0','0','0', ' ','\0',
    116 	/* size, space termination: 12 bytes */
    117 	'0','0','0','0','0','0','0','0','0','0','0', ' ',
    118 	/* mtime, space termination: 12 bytes */
    119 	'0','0','0','0','0','0','0','0','0','0','0', ' ',
    120 	/* Initial checksum value: 8 spaces */
    121 	' ',' ',' ',' ',' ',' ',' ',' ',
    122 	/* Typeflag: 1 byte */
    123 	'0',			/* '0' = regular file */
    124 	/* Linkname: 100 bytes */
    125 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    126 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    127 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    128 	0,0,0,0,
    129 	/* Magic: 6 bytes, Version: 2 bytes */
    130 	'u','s','t','a','r','\0', '0','0',
    131 	/* Uname: 32 bytes */
    132 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    133 	/* Gname: 32 bytes */
    134 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    135 	/* rdevmajor + space/null padding: 8 bytes */
    136 	'0','0','0','0','0','0', ' ','\0',
    137 	/* rdevminor + space/null padding: 8 bytes */
    138 	'0','0','0','0','0','0', ' ','\0',
    139 	/* Prefix: 155 bytes */
    140 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    141 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    142 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    143 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
    144 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,
    145 	/* Padding: 12 bytes */
    146 	0,0,0,0,0,0,0,0, 0,0,0,0
    147 };
    148 
    149 static ssize_t	archive_write_ustar_data(struct archive_write *a, const void *buff,
    150 		    size_t s);
    151 static int	archive_write_ustar_free(struct archive_write *);
    152 static int	archive_write_ustar_close(struct archive_write *);
    153 static int	archive_write_ustar_finish_entry(struct archive_write *);
    154 static int	archive_write_ustar_header(struct archive_write *,
    155 		    struct archive_entry *entry);
    156 static int	archive_write_ustar_options(struct archive_write *,
    157 		    const char *, const char *);
    158 static int	format_256(int64_t, char *, int);
    159 static int	format_number(int64_t, char *, int size, int max, int strict);
    160 static int	format_octal(int64_t, char *, int);
    161 
    162 /*
    163  * Set output format to 'ustar' format.
    164  */
    165 int
    166 archive_write_set_format_ustar(struct archive *_a)
    167 {
    168 	struct archive_write *a = (struct archive_write *)_a;
    169 	struct ustar *ustar;
    170 
    171 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
    172 	    ARCHIVE_STATE_NEW, "archive_write_set_format_ustar");
    173 
    174 	/* If someone else was already registered, unregister them. */
    175 	if (a->format_free != NULL)
    176 		(a->format_free)(a);
    177 
    178 	/* Basic internal sanity test. */
    179 	if (sizeof(template_header) != 512) {
    180 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    181 		    "Internal: template_header wrong size: %zu should be 512",
    182 		    sizeof(template_header));
    183 		return (ARCHIVE_FATAL);
    184 	}
    185 
    186 	ustar = calloc(1, sizeof(*ustar));
    187 	if (ustar == NULL) {
    188 		archive_set_error(&a->archive, ENOMEM,
    189 		    "Can't allocate ustar data");
    190 		return (ARCHIVE_FATAL);
    191 	}
    192 	a->format_data = ustar;
    193 	a->format_name = "ustar";
    194 	a->format_options = archive_write_ustar_options;
    195 	a->format_write_header = archive_write_ustar_header;
    196 	a->format_write_data = archive_write_ustar_data;
    197 	a->format_close = archive_write_ustar_close;
    198 	a->format_free = archive_write_ustar_free;
    199 	a->format_finish_entry = archive_write_ustar_finish_entry;
    200 	a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
    201 	a->archive.archive_format_name = "POSIX ustar";
    202 	return (ARCHIVE_OK);
    203 }
    204 
    205 static int
    206 archive_write_ustar_options(struct archive_write *a, const char *key,
    207     const char *val)
    208 {
    209 	struct ustar *ustar = (struct ustar *)a->format_data;
    210 	int ret = ARCHIVE_FAILED;
    211 
    212 	if (strcmp(key, "hdrcharset")  == 0) {
    213 		if (val == NULL || val[0] == 0)
    214 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    215 			    "%s: hdrcharset option needs a character-set name",
    216 			    a->format_name);
    217 		else {
    218 			ustar->opt_sconv = archive_string_conversion_to_charset(
    219 			    &a->archive, val, 0);
    220 			if (ustar->opt_sconv != NULL)
    221 				ret = ARCHIVE_OK;
    222 			else
    223 				ret = ARCHIVE_FATAL;
    224 		}
    225 		return (ret);
    226 	}
    227 
    228 	/* Note: The "warn" return is just to inform the options
    229 	 * supervisor that we didn't handle it.  It will generate
    230 	 * a suitable error if no one used this option. */
    231 	return (ARCHIVE_WARN);
    232 }
    233 
    234 static int
    235 archive_write_ustar_header(struct archive_write *a, struct archive_entry *entry)
    236 {
    237 	char buff[512];
    238 	int ret, ret2;
    239 	struct ustar *ustar;
    240 	struct archive_entry *entry_main;
    241 	struct archive_string_conv *sconv;
    242 
    243 	ustar = (struct ustar *)a->format_data;
    244 
    245 	/* Setup default string conversion. */
    246 	if (ustar->opt_sconv == NULL) {
    247 		if (!ustar->init_default_conversion) {
    248 			ustar->sconv_default =
    249 			    archive_string_default_conversion_for_write(&(a->archive));
    250 			ustar->init_default_conversion = 1;
    251 		}
    252 		sconv = ustar->sconv_default;
    253 	} else
    254 		sconv = ustar->opt_sconv;
    255 
    256 	/* Sanity check. */
    257 	if (archive_entry_pathname(entry) == NULL
    258 #if defined(_WIN32) && !defined(__CYGWIN__)
    259 	    && archive_entry_pathname_w(entry) == NULL
    260 #endif
    261 	    ) {
    262 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
    263 		    "Can't record entry in tar file without pathname");
    264 		return (ARCHIVE_FAILED);
    265 	}
    266 
    267 	/* Only regular files (not hardlinks) have data. */
    268 	if (archive_entry_hardlink(entry) != NULL ||
    269 	    archive_entry_symlink(entry) != NULL ||
    270 	    archive_entry_filetype(entry) != AE_IFREG)
    271 		archive_entry_set_size(entry, 0);
    272 
    273 	if (AE_IFDIR == archive_entry_filetype(entry)) {
    274 		const char *p;
    275 		size_t path_length;
    276 		/*
    277 		 * Ensure a trailing '/'.  Modify the entry so
    278 		 * the client sees the change.
    279 		 */
    280 #if defined(_WIN32) && !defined(__CYGWIN__)
    281 		const wchar_t *wp;
    282 
    283 		wp = archive_entry_pathname_w(entry);
    284 		if (wp != NULL && wp[wcslen(wp) -1] != L'/') {
    285 			struct archive_wstring ws;
    286 
    287 			archive_string_init(&ws);
    288 			path_length = wcslen(wp);
    289 			if (archive_wstring_ensure(&ws,
    290 			    path_length + 2) == NULL) {
    291 				archive_set_error(&a->archive, ENOMEM,
    292 				    "Can't allocate ustar data");
    293 				archive_wstring_free(&ws);
    294 				return(ARCHIVE_FATAL);
    295 			}
    296 			/* Should we keep '\' ? */
    297 			if (wp[path_length -1] == L'\\')
    298 				path_length--;
    299 			archive_wstrncpy(&ws, wp, path_length);
    300 			archive_wstrappend_wchar(&ws, L'/');
    301 			archive_entry_copy_pathname_w(entry, ws.s);
    302 			archive_wstring_free(&ws);
    303 			p = NULL;
    304 		} else
    305 #endif
    306 			p = archive_entry_pathname(entry);
    307 		/*
    308 		 * On Windows, this is a backup operation just in
    309 		 * case getting WCS failed. On POSIX, this is a
    310 		 * normal operation.
    311 		 */
    312 		if (p != NULL && p[0] != '\0' && p[strlen(p) - 1] != '/') {
    313 			struct archive_string as;
    314 
    315 			archive_string_init(&as);
    316 			path_length = strlen(p);
    317 			if (archive_string_ensure(&as,
    318 			    path_length + 2) == NULL) {
    319 				archive_set_error(&a->archive, ENOMEM,
    320 				    "Can't allocate ustar data");
    321 				archive_string_free(&as);
    322 				return(ARCHIVE_FATAL);
    323 			}
    324 #if defined(_WIN32) && !defined(__CYGWIN__)
    325 			/* NOTE: This might break the pathname
    326 			 * if the current code page is CP932 and
    327 			 * the pathname includes a character '\'
    328 			 * as a part of its multibyte pathname. */
    329 			if (p[strlen(p) -1] == '\\')
    330 				path_length--;
    331 			else
    332 #endif
    333 			archive_strncpy(&as, p, path_length);
    334 			archive_strappend_char(&as, '/');
    335 			archive_entry_copy_pathname(entry, as.s);
    336 			archive_string_free(&as);
    337 		}
    338 	}
    339 
    340 #if defined(_WIN32) && !defined(__CYGWIN__)
    341 	/* Make sure the path separators in pathname, hardlink and symlink
    342 	 * are all slash '/', not the Windows path separator '\'. */
    343 	entry_main = __la_win_entry_in_posix_pathseparator(entry);
    344 	if (entry_main == NULL) {
    345 		archive_set_error(&a->archive, ENOMEM,
    346 		    "Can't allocate ustar data");
    347 		return(ARCHIVE_FATAL);
    348 	}
    349 	if (entry != entry_main)
    350 		entry = entry_main;
    351 	else
    352 		entry_main = NULL;
    353 #else
    354 	entry_main = NULL;
    355 #endif
    356 	ret = __archive_write_format_header_ustar(a, buff, entry, -1, 1, sconv);
    357 	if (ret < ARCHIVE_WARN) {
    358 		archive_entry_free(entry_main);
    359 		return (ret);
    360 	}
    361 	ret2 = __archive_write_output(a, buff, 512);
    362 	if (ret2 < ARCHIVE_WARN) {
    363 		archive_entry_free(entry_main);
    364 		return (ret2);
    365 	}
    366 	if (ret2 < ret)
    367 		ret = ret2;
    368 
    369 	ustar->entry_bytes_remaining = archive_entry_size(entry);
    370 	ustar->entry_padding = 0x1ff & (-(int64_t)ustar->entry_bytes_remaining);
    371 	archive_entry_free(entry_main);
    372 	return (ret);
    373 }
    374 
    375 /*
    376  * Format a basic 512-byte "ustar" header.
    377  *
    378  * Returns -1 if format failed (due to field overflow).
    379  * Note that this always formats as much of the header as possible.
    380  * If "strict" is set to zero, it will extend numeric fields as
    381  * necessary (overwriting terminators or using base-256 extensions).
    382  *
    383  * This is exported so that other 'tar' formats can use it.
    384  */
    385 int
    386 __archive_write_format_header_ustar(struct archive_write *a, char h[512],
    387     struct archive_entry *entry, int tartype, int strict,
    388     struct archive_string_conv *sconv)
    389 {
    390 	unsigned int checksum;
    391 	int i, r, ret;
    392 	size_t copy_length;
    393 	const char *p, *pp;
    394 	int mytartype;
    395 
    396 	ret = 0;
    397 	mytartype = -1;
    398 	/*
    399 	 * The "template header" already includes the "ustar"
    400 	 * signature, various end-of-field markers and other required
    401 	 * elements.
    402 	 */
    403 	memcpy(h, &template_header, 512);
    404 
    405 	/*
    406 	 * Because the block is already null-filled, and strings
    407 	 * are allowed to exactly fill their destination (without null),
    408 	 * I use memcpy(dest, src, strlen()) here a lot to copy strings.
    409 	 */
    410 	r = archive_entry_pathname_l(entry, &pp, &copy_length, sconv);
    411 	if (r != 0) {
    412 		const char* p_mbs;
    413 		if (errno == ENOMEM) {
    414 			archive_set_error(&a->archive, ENOMEM,
    415 			    "Can't allocate memory for Pathname");
    416 			return (ARCHIVE_FATAL);
    417 		}
    418 		p_mbs = archive_entry_pathname(entry);
    419 		if (p_mbs) {
    420 			/* We have a wrongly-encoded MBS pathname.
    421 			 * Warn and use it.  */
    422 			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
    423 			    "Can't translate pathname '%s' to %s", p_mbs,
    424 			    archive_string_conversion_charset_name(sconv));
    425 			ret = ARCHIVE_WARN;
    426 		} else {
    427 			/* We have no MBS pathname.  Fail.  */
    428 			archive_set_error(&a->archive,
    429 			    ARCHIVE_ERRNO_FILE_FORMAT,
    430 			    "Can't translate pathname to %s",
    431 			    archive_string_conversion_charset_name(sconv));
    432 			return ARCHIVE_FAILED;
    433 		}
    434 	}
    435 	if (copy_length <= USTAR_name_size)
    436 		memcpy(h + USTAR_name_offset, pp, copy_length);
    437 	else {
    438 		/* Store in two pieces, splitting at a '/'. */
    439 		p = strchr(pp + copy_length - USTAR_name_size - 1, '/');
    440 		/*
    441 		 * Look for the next '/' if we chose the first character
    442 		 * as the separator.  (ustar format doesn't permit
    443 		 * an empty prefix.)
    444 		 */
    445 		if (p == pp)
    446 			p = strchr(p + 1, '/');
    447 		/* Fail if the name won't fit. */
    448 		if (!p) {
    449 			/* No separator. */
    450 			archive_set_error(&a->archive, ENAMETOOLONG,
    451 			    "Pathname too long");
    452 			ret = ARCHIVE_FAILED;
    453 		} else if (p[1] == '\0') {
    454 			/*
    455 			 * The only feasible separator is a final '/';
    456 			 * this would result in a non-empty prefix and
    457 			 * an empty name, which POSIX doesn't
    458 			 * explicitly forbid, but it just feels wrong.
    459 			 */
    460 			archive_set_error(&a->archive, ENAMETOOLONG,
    461 			    "Pathname too long");
    462 			ret = ARCHIVE_FAILED;
    463 		} else if (p  > pp + USTAR_prefix_size) {
    464 			/* Prefix is too long. */
    465 			archive_set_error(&a->archive, ENAMETOOLONG,
    466 			    "Pathname too long");
    467 			ret = ARCHIVE_FAILED;
    468 		} else {
    469 			/* Copy prefix and remainder to appropriate places */
    470 			memcpy(h + USTAR_prefix_offset, pp, p - pp);
    471 			memcpy(h + USTAR_name_offset, p + 1,
    472 			    pp + copy_length - p - 1);
    473 		}
    474 	}
    475 
    476 	r = archive_entry_hardlink_l(entry, &p, &copy_length, sconv);
    477 	if (r != 0) {
    478 		if (errno == ENOMEM) {
    479 			archive_set_error(&a->archive, ENOMEM,
    480 			    "Can't allocate memory for Linkname");
    481 			return (ARCHIVE_FATAL);
    482 		}
    483 		archive_set_error(&a->archive,
    484 		    ARCHIVE_ERRNO_FILE_FORMAT,
    485 		    "Can't translate linkname '%s' to %s",
    486 		    p, archive_string_conversion_charset_name(sconv));
    487 		ret = ARCHIVE_WARN;
    488 	}
    489 	if (copy_length > 0)
    490 		mytartype = '1';
    491 	else {
    492 		r = archive_entry_symlink_l(entry, &p, &copy_length, sconv);
    493 		if (r != 0) {
    494 			if (errno == ENOMEM) {
    495 				archive_set_error(&a->archive, ENOMEM,
    496 				    "Can't allocate memory for Linkname");
    497 				return (ARCHIVE_FATAL);
    498 			}
    499 			archive_set_error(&a->archive,
    500 			    ARCHIVE_ERRNO_FILE_FORMAT,
    501 			    "Can't translate linkname '%s' to %s",
    502 			    p, archive_string_conversion_charset_name(sconv));
    503 			ret = ARCHIVE_WARN;
    504 		}
    505 	}
    506 	if (copy_length > 0) {
    507 		if (copy_length > USTAR_linkname_size) {
    508 			archive_set_error(&a->archive, ENAMETOOLONG,
    509 			    "Link contents too long");
    510 			ret = ARCHIVE_FAILED;
    511 			copy_length = USTAR_linkname_size;
    512 		}
    513 		memcpy(h + USTAR_linkname_offset, p, copy_length);
    514 	}
    515 
    516 	r = archive_entry_uname_l(entry, &p, &copy_length, sconv);
    517 	if (r != 0) {
    518 		if (errno == ENOMEM) {
    519 			archive_set_error(&a->archive, ENOMEM,
    520 			    "Can't allocate memory for Uname");
    521 			return (ARCHIVE_FATAL);
    522 		}
    523 		archive_set_error(&a->archive,
    524 		    ARCHIVE_ERRNO_FILE_FORMAT,
    525 		    "Can't translate uname '%s' to %s",
    526 		    p, archive_string_conversion_charset_name(sconv));
    527 		ret = ARCHIVE_WARN;
    528 	}
    529 	if (copy_length > 0) {
    530 		if (copy_length > USTAR_uname_size) {
    531 			if (tartype != 'x') {
    532 				archive_set_error(&a->archive,
    533 				    ARCHIVE_ERRNO_MISC, "Username too long");
    534 				ret = ARCHIVE_FAILED;
    535 			}
    536 			copy_length = USTAR_uname_size;
    537 		}
    538 		memcpy(h + USTAR_uname_offset, p, copy_length);
    539 	}
    540 
    541 	r = archive_entry_gname_l(entry, &p, &copy_length, sconv);
    542 	if (r != 0) {
    543 		if (errno == ENOMEM) {
    544 			archive_set_error(&a->archive, ENOMEM,
    545 			    "Can't allocate memory for Gname");
    546 			return (ARCHIVE_FATAL);
    547 		}
    548 		archive_set_error(&a->archive,
    549 		    ARCHIVE_ERRNO_FILE_FORMAT,
    550 		    "Can't translate gname '%s' to %s",
    551 		    p, archive_string_conversion_charset_name(sconv));
    552 		ret = ARCHIVE_WARN;
    553 	}
    554 	if (copy_length > 0) {
    555 		if (copy_length > USTAR_gname_size) {
    556 			if (tartype != 'x') {
    557 				archive_set_error(&a->archive,
    558 				    ARCHIVE_ERRNO_MISC, "Group name too long");
    559 				ret = ARCHIVE_FAILED;
    560 			}
    561 			copy_length = USTAR_gname_size;
    562 		}
    563 		memcpy(h + USTAR_gname_offset, p, copy_length);
    564 	}
    565 
    566 	if (format_number(archive_entry_mode(entry) & 07777,
    567 	    h + USTAR_mode_offset, USTAR_mode_size, USTAR_mode_max_size, strict)) {
    568 		archive_set_error(&a->archive, ERANGE,
    569 		    "Numeric mode too large");
    570 		ret = ARCHIVE_FAILED;
    571 	}
    572 
    573 	if (format_number(archive_entry_uid(entry),
    574 	    h + USTAR_uid_offset, USTAR_uid_size, USTAR_uid_max_size, strict)) {
    575 		archive_set_error(&a->archive, ERANGE,
    576 		    "Numeric user ID too large");
    577 		ret = ARCHIVE_FAILED;
    578 	}
    579 
    580 	if (format_number(archive_entry_gid(entry),
    581 	    h + USTAR_gid_offset, USTAR_gid_size, USTAR_gid_max_size, strict)) {
    582 		archive_set_error(&a->archive, ERANGE,
    583 		    "Numeric group ID too large");
    584 		ret = ARCHIVE_FAILED;
    585 	}
    586 
    587 	if (format_number(archive_entry_size(entry),
    588 	    h + USTAR_size_offset, USTAR_size_size, USTAR_size_max_size, strict)) {
    589 		archive_set_error(&a->archive, ERANGE,
    590 		    "File size out of range");
    591 		ret = ARCHIVE_FAILED;
    592 	}
    593 
    594 	if (format_number(archive_entry_mtime(entry),
    595 	    h + USTAR_mtime_offset, USTAR_mtime_size, USTAR_mtime_max_size, strict)) {
    596 		archive_set_error(&a->archive, ERANGE,
    597 		    "File modification time too large");
    598 		ret = ARCHIVE_FAILED;
    599 	}
    600 
    601 	if (archive_entry_filetype(entry) == AE_IFBLK
    602 	    || archive_entry_filetype(entry) == AE_IFCHR) {
    603 		if (format_number(archive_entry_rdevmajor(entry),
    604 		    h + USTAR_rdevmajor_offset, USTAR_rdevmajor_size,
    605 		    USTAR_rdevmajor_max_size, strict)) {
    606 			archive_set_error(&a->archive, ERANGE,
    607 			    "Major device number too large");
    608 			ret = ARCHIVE_FAILED;
    609 		}
    610 
    611 		if (format_number(archive_entry_rdevminor(entry),
    612 		    h + USTAR_rdevminor_offset, USTAR_rdevminor_size,
    613 		    USTAR_rdevminor_max_size, strict)) {
    614 			archive_set_error(&a->archive, ERANGE,
    615 			    "Minor device number too large");
    616 			ret = ARCHIVE_FAILED;
    617 		}
    618 	}
    619 
    620 	if (tartype >= 0) {
    621 		h[USTAR_typeflag_offset] = tartype;
    622 	} else if (mytartype >= 0) {
    623 		h[USTAR_typeflag_offset] = mytartype;
    624 	} else {
    625 		switch (archive_entry_filetype(entry)) {
    626 		case AE_IFREG: h[USTAR_typeflag_offset] = '0' ; break;
    627 		case AE_IFLNK: h[USTAR_typeflag_offset] = '2' ; break;
    628 		case AE_IFCHR: h[USTAR_typeflag_offset] = '3' ; break;
    629 		case AE_IFBLK: h[USTAR_typeflag_offset] = '4' ; break;
    630 		case AE_IFDIR: h[USTAR_typeflag_offset] = '5' ; break;
    631 		case AE_IFIFO: h[USTAR_typeflag_offset] = '6' ; break;
    632 		default: /* AE_IFSOCK and unknown */
    633 			__archive_write_entry_filetype_unsupported(
    634 			    &a->archive, entry, "ustar");
    635 			ret = ARCHIVE_FAILED;
    636 		}
    637 	}
    638 
    639 	checksum = 0;
    640 	for (i = 0; i < 512; i++)
    641 		checksum += 255 & (unsigned int)h[i];
    642 	h[USTAR_checksum_offset + 6] = '\0'; /* Can't be pre-set in the template. */
    643 	/* h[USTAR_checksum_offset + 7] = ' '; */ /* This is pre-set in the template. */
    644 	format_octal(checksum, h + USTAR_checksum_offset, 6);
    645 	return (ret);
    646 }
    647 
    648 /*
    649  * Format a number into a field, with some intelligence.
    650  */
    651 static int
    652 format_number(int64_t v, char *p, int s, int maxsize, int strict)
    653 {
    654 	int64_t limit;
    655 
    656 	limit = ((int64_t)1 << (s*3));
    657 
    658 	/* "Strict" only permits octal values with proper termination. */
    659 	if (strict)
    660 		return (format_octal(v, p, s));
    661 
    662 	/*
    663 	 * In non-strict mode, we allow the number to overwrite one or
    664 	 * more bytes of the field termination.  Even old tar
    665 	 * implementations should be able to handle this with no
    666 	 * problem.
    667 	 */
    668 	if (v >= 0) {
    669 		while (s <= maxsize) {
    670 			if (v < limit)
    671 				return (format_octal(v, p, s));
    672 			s++;
    673 			limit <<= 3;
    674 		}
    675 	}
    676 
    677 	/* Base-256 can handle any number, positive or negative. */
    678 	return (format_256(v, p, maxsize));
    679 }
    680 
    681 /*
    682  * Format a number into the specified field using base-256.
    683  */
    684 static int
    685 format_256(int64_t v, char *p, int s)
    686 {
    687 	p += s;
    688 	while (s-- > 0) {
    689 		*--p = (char)(v & 0xff);
    690 		v >>= 8;
    691 	}
    692 	*p |= 0x80; /* Set the base-256 marker bit. */
    693 	return (0);
    694 }
    695 
    696 /*
    697  * Format a number into the specified field.
    698  */
    699 static int
    700 format_octal(int64_t v, char *p, int s)
    701 {
    702 	int len;
    703 
    704 	len = s;
    705 
    706 	/* Octal values can't be negative, so use 0. */
    707 	if (v < 0) {
    708 		while (len-- > 0)
    709 			*p++ = '0';
    710 		return (-1);
    711 	}
    712 
    713 	p += s;		/* Start at the end and work backwards. */
    714 	while (s-- > 0) {
    715 		*--p = (char)('0' + (v & 7));
    716 		v >>= 3;
    717 	}
    718 
    719 	if (v == 0)
    720 		return (0);
    721 
    722 	/* If it overflowed, fill field with max value. */
    723 	while (len-- > 0)
    724 		*p++ = '7';
    725 
    726 	return (-1);
    727 }
    728 
    729 static int
    730 archive_write_ustar_close(struct archive_write *a)
    731 {
    732 	return (__archive_write_nulls(a, 512*2));
    733 }
    734 
    735 static int
    736 archive_write_ustar_free(struct archive_write *a)
    737 {
    738 	struct ustar *ustar;
    739 
    740 	ustar = (struct ustar *)a->format_data;
    741 	free(ustar);
    742 	a->format_data = NULL;
    743 	return (ARCHIVE_OK);
    744 }
    745 
    746 static int
    747 archive_write_ustar_finish_entry(struct archive_write *a)
    748 {
    749 	struct ustar *ustar;
    750 	int ret;
    751 
    752 	ustar = (struct ustar *)a->format_data;
    753 	ret = __archive_write_nulls(a,
    754 	    (size_t)(ustar->entry_bytes_remaining + ustar->entry_padding));
    755 	ustar->entry_bytes_remaining = ustar->entry_padding = 0;
    756 	return (ret);
    757 }
    758 
    759 static ssize_t
    760 archive_write_ustar_data(struct archive_write *a, const void *buff, size_t s)
    761 {
    762 	struct ustar *ustar;
    763 	int ret;
    764 
    765 	ustar = (struct ustar *)a->format_data;
    766 	if (s > ustar->entry_bytes_remaining)
    767 		s = (size_t)ustar->entry_bytes_remaining;
    768 	ret = __archive_write_output(a, buff, s);
    769 	ustar->entry_bytes_remaining -= s;
    770 	if (ret != ARCHIVE_OK)
    771 		return (ret);
    772 	return (s);
    773 }
    774