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