1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2011 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: head/lib/libarchive/archive_write_set_format_ustar.c 191579 2009-04-27 18:35:03Z kientzle $");
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 termation: 12 bytes */
118 	'0','0','0','0','0','0','0','0','0','0','0', ' ',
119 	/* mtime, space termation: 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 *)malloc(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 	memset(ustar, 0, sizeof(*ustar));
194 	a->format_data = ustar;
195 	a->format_name = "ustar";
196 	a->format_options = archive_write_ustar_options;
197 	a->format_write_header = archive_write_ustar_header;
198 	a->format_write_data = archive_write_ustar_data;
199 	a->format_close = archive_write_ustar_close;
200 	a->format_free = archive_write_ustar_free;
201 	a->format_finish_entry = archive_write_ustar_finish_entry;
202 	a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
203 	a->archive.archive_format_name = "POSIX ustar";
204 	return (ARCHIVE_OK);
205 }
206 
207 static int
208 archive_write_ustar_options(struct archive_write *a, const char *key,
209     const char *val)
210 {
211 	struct ustar *ustar = (struct ustar *)a->format_data;
212 	int ret = ARCHIVE_FAILED;
213 
214 	if (strcmp(key, "hdrcharset")  == 0) {
215 		if (val == NULL || val[0] == 0)
216 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
217 			    "%s: hdrcharset option needs a character-set name",
218 			    a->format_name);
219 		else {
220 			ustar->opt_sconv = archive_string_conversion_to_charset(
221 			    &a->archive, val, 0);
222 			if (ustar->opt_sconv != NULL)
223 				ret = ARCHIVE_OK;
224 			else
225 				ret = ARCHIVE_FATAL;
226 		}
227 	} else
228 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
229 		    "%s: unknown keyword ``%s''", a->format_name, key);
230 
231 	return (ret);
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_string_conv *sconv;
241 
242 	ustar = (struct ustar *)a->format_data;
243 
244 	/* Setup default string conversion. */
245 	if (ustar->opt_sconv == NULL) {
246 		if (!ustar->init_default_conversion) {
247 			ustar->sconv_default =
248 			    archive_string_default_conversion_for_write(&(a->archive));
249 			ustar->init_default_conversion = 1;
250 		}
251 		sconv = ustar->sconv_default;
252 	} else
253 		sconv = ustar->opt_sconv;
254 
255 	/* Sanity check. */
256 	if (archive_entry_pathname(entry) == NULL) {
257 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
258 		    "Can't record entry in tar file without pathname");
259 		return (ARCHIVE_FAILED);
260 	}
261 
262 	/* Only regular files (not hardlinks) have data. */
263 	if (archive_entry_hardlink(entry) != NULL ||
264 	    archive_entry_symlink(entry) != NULL ||
265 	    !(archive_entry_filetype(entry) == AE_IFREG))
266 		archive_entry_set_size(entry, 0);
267 
268 	if (AE_IFDIR == archive_entry_filetype(entry)) {
269 		const char *p;
270 		char *t;
271 		/*
272 		 * Ensure a trailing '/'.  Modify the entry so
273 		 * the client sees the change.
274 		 */
275 		p = archive_entry_pathname(entry);
276 		if (p[strlen(p) - 1] != '/') {
277 			t = (char *)malloc(strlen(p) + 2);
278 			if (t == NULL) {
279 				archive_set_error(&a->archive, ENOMEM,
280 				"Can't allocate ustar data");
281 				return(ARCHIVE_FATAL);
282 			}
283 			strcpy(t, p);
284 			strcat(t, "/");
285 			archive_entry_copy_pathname(entry, t);
286 			free(t);
287 		}
288 	}
289 
290 	ret = __archive_write_format_header_ustar(a, buff, entry, -1, 1, sconv);
291 	if (ret < ARCHIVE_WARN)
292 		return (ret);
293 	ret2 = __archive_write_output(a, buff, 512);
294 	if (ret2 < ARCHIVE_WARN)
295 		return (ret2);
296 	if (ret2 < ret)
297 		ret = ret2;
298 
299 	ustar->entry_bytes_remaining = archive_entry_size(entry);
300 	ustar->entry_padding = 0x1ff & (-(int64_t)ustar->entry_bytes_remaining);
301 	return (ret);
302 }
303 
304 /*
305  * Format a basic 512-byte "ustar" header.
306  *
307  * Returns -1 if format failed (due to field overflow).
308  * Note that this always formats as much of the header as possible.
309  * If "strict" is set to zero, it will extend numeric fields as
310  * necessary (overwriting terminators or using base-256 extensions).
311  *
312  * This is exported so that other 'tar' formats can use it.
313  */
314 int
315 __archive_write_format_header_ustar(struct archive_write *a, char h[512],
316     struct archive_entry *entry, int tartype, int strict,
317     struct archive_string_conv *sconv)
318 {
319 	unsigned int checksum;
320 	int i, r, ret;
321 	size_t copy_length;
322 	const char *p, *pp;
323 	int mytartype;
324 
325 	ret = 0;
326 	mytartype = -1;
327 	/*
328 	 * The "template header" already includes the "ustar"
329 	 * signature, various end-of-field markers and other required
330 	 * elements.
331 	 */
332 	memcpy(h, &template_header, 512);
333 
334 	/*
335 	 * Because the block is already null-filled, and strings
336 	 * are allowed to exactly fill their destination (without null),
337 	 * I use memcpy(dest, src, strlen()) here a lot to copy strings.
338 	 */
339 	r = archive_entry_pathname_l(entry, &pp, &copy_length, sconv);
340 	if (r != 0) {
341 		if (errno == ENOMEM) {
342 			archive_set_error(&a->archive, ENOMEM,
343 			    "Can't allocate memory for Pathname");
344 			return (ARCHIVE_FATAL);
345 		}
346 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
347 		    "Can't translate pathname '%s' to %s",
348 		    pp, archive_string_conversion_charset_name(sconv));
349 		ret = ARCHIVE_WARN;
350 	}
351 	if (copy_length <= USTAR_name_size)
352 		memcpy(h + USTAR_name_offset, pp, copy_length);
353 	else {
354 		/* Store in two pieces, splitting at a '/'. */
355 		p = strchr(pp + copy_length - USTAR_name_size - 1, '/');
356 		/*
357 		 * Look for the next '/' if we chose the first character
358 		 * as the separator.  (ustar format doesn't permit
359 		 * an empty prefix.)
360 		 */
361 		if (p == pp)
362 			p = strchr(p + 1, '/');
363 		/* Fail if the name won't fit. */
364 		if (!p) {
365 			/* No separator. */
366 			archive_set_error(&a->archive, ENAMETOOLONG,
367 			    "Pathname too long");
368 			ret = ARCHIVE_FAILED;
369 		} else if (p[1] == '\0') {
370 			/*
371 			 * The only feasible separator is a final '/';
372 			 * this would result in a non-empty prefix and
373 			 * an empty name, which POSIX doesn't
374 			 * explicitly forbid, but it just feels wrong.
375 			 */
376 			archive_set_error(&a->archive, ENAMETOOLONG,
377 			    "Pathname too long");
378 			ret = ARCHIVE_FAILED;
379 		} else if (p  > pp + USTAR_prefix_size) {
380 			/* Prefix is too long. */
381 			archive_set_error(&a->archive, ENAMETOOLONG,
382 			    "Pathname too long");
383 			ret = ARCHIVE_FAILED;
384 		} else {
385 			/* Copy prefix and remainder to appropriate places */
386 			memcpy(h + USTAR_prefix_offset, pp, p - pp);
387 			memcpy(h + USTAR_name_offset, p + 1,
388 			    pp + copy_length - p - 1);
389 		}
390 	}
391 
392 	r = archive_entry_hardlink_l(entry, &p, &copy_length, sconv);
393 	if (r != 0) {
394 		if (errno == ENOMEM) {
395 			archive_set_error(&a->archive, ENOMEM,
396 			    "Can't allocate memory for Linkname");
397 			return (ARCHIVE_FATAL);
398 		}
399 		archive_set_error(&a->archive,
400 		    ARCHIVE_ERRNO_FILE_FORMAT,
401 		    "Can't translate linkname '%s' to %s",
402 		    p, archive_string_conversion_charset_name(sconv));
403 		ret = ARCHIVE_WARN;
404 	}
405 	if (copy_length > 0)
406 		mytartype = '1';
407 	else {
408 		r = archive_entry_symlink_l(entry, &p, &copy_length, sconv);
409 		if (r != 0) {
410 			if (errno == ENOMEM) {
411 				archive_set_error(&a->archive, ENOMEM,
412 				    "Can't allocate memory for Linkname");
413 				return (ARCHIVE_FATAL);
414 			}
415 			archive_set_error(&a->archive,
416 			    ARCHIVE_ERRNO_FILE_FORMAT,
417 			    "Can't translate linkname '%s' to %s",
418 			    p, archive_string_conversion_charset_name(sconv));
419 			ret = ARCHIVE_WARN;
420 		}
421 	}
422 	if (copy_length > 0) {
423 		if (copy_length > USTAR_linkname_size) {
424 			archive_set_error(&a->archive, ENAMETOOLONG,
425 			    "Link contents too long");
426 			ret = ARCHIVE_FAILED;
427 			copy_length = USTAR_linkname_size;
428 		}
429 		memcpy(h + USTAR_linkname_offset, p, copy_length);
430 	}
431 
432 	r = archive_entry_uname_l(entry, &p, &copy_length, sconv);
433 	if (r != 0) {
434 		if (errno == ENOMEM) {
435 			archive_set_error(&a->archive, ENOMEM,
436 			    "Can't allocate memory for Uname");
437 			return (ARCHIVE_FATAL);
438 		}
439 		archive_set_error(&a->archive,
440 		    ARCHIVE_ERRNO_FILE_FORMAT,
441 		    "Can't translate uname '%s' to %s",
442 		    p, archive_string_conversion_charset_name(sconv));
443 		ret = ARCHIVE_WARN;
444 	}
445 	if (copy_length > 0) {
446 		if (copy_length > USTAR_uname_size) {
447 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
448 			    "Username too long");
449 			ret = ARCHIVE_FAILED;
450 			copy_length = USTAR_uname_size;
451 		}
452 		memcpy(h + USTAR_uname_offset, p, copy_length);
453 	}
454 
455 	r = archive_entry_gname_l(entry, &p, &copy_length, sconv);
456 	if (r != 0) {
457 		if (errno == ENOMEM) {
458 			archive_set_error(&a->archive, ENOMEM,
459 			    "Can't allocate memory for Gname");
460 			return (ARCHIVE_FATAL);
461 		}
462 		archive_set_error(&a->archive,
463 		    ARCHIVE_ERRNO_FILE_FORMAT,
464 		    "Can't translate gname '%s' to %s",
465 		    p, archive_string_conversion_charset_name(sconv));
466 		ret = ARCHIVE_WARN;
467 	}
468 	if (copy_length > 0) {
469 		if (strlen(p) > USTAR_gname_size) {
470 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
471 			    "Group name too long");
472 			ret = ARCHIVE_FAILED;
473 			copy_length = USTAR_gname_size;
474 		}
475 		memcpy(h + USTAR_gname_offset, p, copy_length);
476 	}
477 
478 	if (format_number(archive_entry_mode(entry) & 07777,
479 	    h + USTAR_mode_offset, USTAR_mode_size, USTAR_mode_max_size, strict)) {
480 		archive_set_error(&a->archive, ERANGE,
481 		    "Numeric mode too large");
482 		ret = ARCHIVE_FAILED;
483 	}
484 
485 	if (format_number(archive_entry_uid(entry),
486 	    h + USTAR_uid_offset, USTAR_uid_size, USTAR_uid_max_size, strict)) {
487 		archive_set_error(&a->archive, ERANGE,
488 		    "Numeric user ID too large");
489 		ret = ARCHIVE_FAILED;
490 	}
491 
492 	if (format_number(archive_entry_gid(entry),
493 	    h + USTAR_gid_offset, USTAR_gid_size, USTAR_gid_max_size, strict)) {
494 		archive_set_error(&a->archive, ERANGE,
495 		    "Numeric group ID too large");
496 		ret = ARCHIVE_FAILED;
497 	}
498 
499 	if (format_number(archive_entry_size(entry),
500 	    h + USTAR_size_offset, USTAR_size_size, USTAR_size_max_size, strict)) {
501 		archive_set_error(&a->archive, ERANGE,
502 		    "File size out of range");
503 		ret = ARCHIVE_FAILED;
504 	}
505 
506 	if (format_number(archive_entry_mtime(entry),
507 	    h + USTAR_mtime_offset, USTAR_mtime_size, USTAR_mtime_max_size, strict)) {
508 		archive_set_error(&a->archive, ERANGE,
509 		    "File modification time too large");
510 		ret = ARCHIVE_FAILED;
511 	}
512 
513 	if (archive_entry_filetype(entry) == AE_IFBLK
514 	    || archive_entry_filetype(entry) == AE_IFCHR) {
515 		if (format_number(archive_entry_rdevmajor(entry),
516 		    h + USTAR_rdevmajor_offset, USTAR_rdevmajor_size,
517 		    USTAR_rdevmajor_max_size, strict)) {
518 			archive_set_error(&a->archive, ERANGE,
519 			    "Major device number too large");
520 			ret = ARCHIVE_FAILED;
521 		}
522 
523 		if (format_number(archive_entry_rdevminor(entry),
524 		    h + USTAR_rdevminor_offset, USTAR_rdevminor_size,
525 		    USTAR_rdevminor_max_size, strict)) {
526 			archive_set_error(&a->archive, ERANGE,
527 			    "Minor device number too large");
528 			ret = ARCHIVE_FAILED;
529 		}
530 	}
531 
532 	if (tartype >= 0) {
533 		h[USTAR_typeflag_offset] = tartype;
534 	} else if (mytartype >= 0) {
535 		h[USTAR_typeflag_offset] = mytartype;
536 	} else {
537 		switch (archive_entry_filetype(entry)) {
538 		case AE_IFREG: h[USTAR_typeflag_offset] = '0' ; break;
539 		case AE_IFLNK: h[USTAR_typeflag_offset] = '2' ; break;
540 		case AE_IFCHR: h[USTAR_typeflag_offset] = '3' ; break;
541 		case AE_IFBLK: h[USTAR_typeflag_offset] = '4' ; break;
542 		case AE_IFDIR: h[USTAR_typeflag_offset] = '5' ; break;
543 		case AE_IFIFO: h[USTAR_typeflag_offset] = '6' ; break;
544 		case AE_IFSOCK:
545 			archive_set_error(&a->archive,
546 			    ARCHIVE_ERRNO_FILE_FORMAT,
547 			    "tar format cannot archive socket");
548 			return (ARCHIVE_FAILED);
549 		default:
550 			archive_set_error(&a->archive,
551 			    ARCHIVE_ERRNO_FILE_FORMAT,
552 			    "tar format cannot archive this (mode=0%lo)",
553 			    (unsigned long)archive_entry_mode(entry));
554 			ret = ARCHIVE_FAILED;
555 		}
556 	}
557 
558 	checksum = 0;
559 	for (i = 0; i < 512; i++)
560 		checksum += 255 & (unsigned int)h[i];
561 	h[USTAR_checksum_offset + 6] = '\0'; /* Can't be pre-set in the template. */
562 	/* h[USTAR_checksum_offset + 7] = ' '; */ /* This is pre-set in the template. */
563 	format_octal(checksum, h + USTAR_checksum_offset, 6);
564 	return (ret);
565 }
566 
567 /*
568  * Format a number into a field, with some intelligence.
569  */
570 static int
571 format_number(int64_t v, char *p, int s, int maxsize, int strict)
572 {
573 	int64_t limit;
574 
575 	limit = ((int64_t)1 << (s*3));
576 
577 	/* "Strict" only permits octal values with proper termination. */
578 	if (strict)
579 		return (format_octal(v, p, s));
580 
581 	/*
582 	 * In non-strict mode, we allow the number to overwrite one or
583 	 * more bytes of the field termination.  Even old tar
584 	 * implementations should be able to handle this with no
585 	 * problem.
586 	 */
587 	if (v >= 0) {
588 		while (s <= maxsize) {
589 			if (v < limit)
590 				return (format_octal(v, p, s));
591 			s++;
592 			limit <<= 3;
593 		}
594 	}
595 
596 	/* Base-256 can handle any number, positive or negative. */
597 	return (format_256(v, p, maxsize));
598 }
599 
600 /*
601  * Format a number into the specified field using base-256.
602  */
603 static int
604 format_256(int64_t v, char *p, int s)
605 {
606 	p += s;
607 	while (s-- > 0) {
608 		*--p = (char)(v & 0xff);
609 		v >>= 8;
610 	}
611 	*p |= 0x80; /* Set the base-256 marker bit. */
612 	return (0);
613 }
614 
615 /*
616  * Format a number into the specified field.
617  */
618 static int
619 format_octal(int64_t v, char *p, int s)
620 {
621 	int len;
622 
623 	len = s;
624 
625 	/* Octal values can't be negative, so use 0. */
626 	if (v < 0) {
627 		while (len-- > 0)
628 			*p++ = '0';
629 		return (-1);
630 	}
631 
632 	p += s;		/* Start at the end and work backwards. */
633 	while (s-- > 0) {
634 		*--p = (char)('0' + (v & 7));
635 		v >>= 3;
636 	}
637 
638 	if (v == 0)
639 		return (0);
640 
641 	/* If it overflowed, fill field with max value. */
642 	while (len-- > 0)
643 		*p++ = '7';
644 
645 	return (-1);
646 }
647 
648 static int
649 archive_write_ustar_close(struct archive_write *a)
650 {
651 	return (__archive_write_nulls(a, 512*2));
652 }
653 
654 static int
655 archive_write_ustar_free(struct archive_write *a)
656 {
657 	struct ustar *ustar;
658 
659 	ustar = (struct ustar *)a->format_data;
660 	free(ustar);
661 	a->format_data = NULL;
662 	return (ARCHIVE_OK);
663 }
664 
665 static int
666 archive_write_ustar_finish_entry(struct archive_write *a)
667 {
668 	struct ustar *ustar;
669 	int ret;
670 
671 	ustar = (struct ustar *)a->format_data;
672 	ret = __archive_write_nulls(a,
673 	    ustar->entry_bytes_remaining + ustar->entry_padding);
674 	ustar->entry_bytes_remaining = ustar->entry_padding = 0;
675 	return (ret);
676 }
677 
678 static ssize_t
679 archive_write_ustar_data(struct archive_write *a, const void *buff, size_t s)
680 {
681 	struct ustar *ustar;
682 	int ret;
683 
684 	ustar = (struct ustar *)a->format_data;
685 	if (s > ustar->entry_bytes_remaining)
686 		s = ustar->entry_bytes_remaining;
687 	ret = __archive_write_output(a, buff, s);
688 	ustar->entry_bytes_remaining -= s;
689 	if (ret != ARCHIVE_OK)
690 		return (ret);
691 	return (s);
692 }
693