1 /*-
2  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
3  * Author: Jonas Gastal <jgastal@profusion.mobi>
4  * Copyright (c) 2011-2012 Michihiro NAKAJIMA
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "archive_platform.h"
30 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_gnu_tar.c 191579 2009-04-27 18:35:03Z gastal $");
31 
32 
33 #ifdef HAVE_ERRNO_H
34 #include <errno.h>
35 #endif
36 #include <stdio.h>
37 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40 #ifdef HAVE_STRING_H
41 #include <string.h>
42 #endif
43 
44 #include "archive.h"
45 #include "archive_entry.h"
46 #include "archive_entry_locale.h"
47 #include "archive_private.h"
48 #include "archive_write_private.h"
49 
50 struct gnutar {
51 	uint64_t	entry_bytes_remaining;
52 	uint64_t	entry_padding;
53 	const char *	linkname;
54 	size_t		linkname_length;
55 	const char *	pathname;
56 	size_t		pathname_length;
57 	const char *	uname;
58 	size_t		uname_length;
59 	const char *	gname;
60 	size_t		gname_length;
61 	struct archive_string_conv *opt_sconv;
62 	struct archive_string_conv *sconv_default;
63 	int init_default_conversion;
64 };
65 
66 /*
67  * Define structure of GNU tar header.
68  */
69 #define	GNUTAR_name_offset 0
70 #define	GNUTAR_name_size 100
71 #define	GNUTAR_mode_offset 100
72 #define	GNUTAR_mode_size 7
73 #define	GNUTAR_mode_max_size 8
74 #define	GNUTAR_uid_offset 108
75 #define	GNUTAR_uid_size 7
76 #define	GNUTAR_uid_max_size 8
77 #define	GNUTAR_gid_offset 116
78 #define	GNUTAR_gid_size 7
79 #define	GNUTAR_gid_max_size 8
80 #define	GNUTAR_size_offset 124
81 #define	GNUTAR_size_size 11
82 #define	GNUTAR_size_max_size 12
83 #define	GNUTAR_mtime_offset 136
84 #define	GNUTAR_mtime_size 11
85 #define	GNUTAR_mtime_max_size 11
86 #define	GNUTAR_checksum_offset 148
87 #define	GNUTAR_checksum_size 8
88 #define	GNUTAR_typeflag_offset 156
89 #define	GNUTAR_typeflag_size 1
90 #define	GNUTAR_linkname_offset 157
91 #define	GNUTAR_linkname_size 100
92 #define	GNUTAR_magic_offset 257
93 #define	GNUTAR_magic_size 6
94 #define	GNUTAR_version_offset 263
95 #define	GNUTAR_version_size 2
96 #define	GNUTAR_uname_offset 265
97 #define	GNUTAR_uname_size 32
98 #define	GNUTAR_gname_offset 297
99 #define	GNUTAR_gname_size 32
100 #define	GNUTAR_rdevmajor_offset 329
101 #define	GNUTAR_rdevmajor_size 6
102 #define	GNUTAR_rdevmajor_max_size 8
103 #define	GNUTAR_rdevminor_offset 337
104 #define	GNUTAR_rdevminor_size 6
105 #define	GNUTAR_rdevminor_max_size 8
106 
107 /*
108  * A filled-in copy of the header for initialization.
109  */
110 static const char template_header[] = {
111 	/* name: 100 bytes */
112 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
113 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
114 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
115 	0,0,0,0,
116 	/* Mode, null termination: 8 bytes */
117 	'0','0','0','0','0','0', '0','\0',
118 	/* uid, null termination: 8 bytes */
119 	'0','0','0','0','0','0', '0','\0',
120 	/* gid, null termination: 8 bytes */
121 	'0','0','0','0','0','0', '0','\0',
122 	/* size, space termination: 12 bytes */
123 	'0','0','0','0','0','0','0','0','0','0','0', '\0',
124 	/* mtime, space termination: 12 bytes */
125 	'0','0','0','0','0','0','0','0','0','0','0', '\0',
126 	/* Initial checksum value: 8 spaces */
127 	' ',' ',' ',' ',' ',' ',' ',' ',
128 	/* Typeflag: 1 byte */
129 	'0',			/* '0' = regular file */
130 	/* Linkname: 100 bytes */
131 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
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 	0,0,0,0,0,0,0,0, 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 	0,0,0,0,
135 	/* Magic: 8 bytes */
136 	'u','s','t','a','r',' ', ' ','\0',
137 	/* Uname: 32 bytes */
138 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
139 	/* Gname: 32 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 	/* rdevmajor + null padding: 8 bytes */
142 	'\0','\0','\0','\0','\0','\0', '\0','\0',
143 	/* rdevminor + null padding: 8 bytes */
144 	'\0','\0','\0','\0','\0','\0', '\0','\0',
145 	/* Padding: 167 bytes */
146 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
147 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
148 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
149 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
150 	0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
151 	0,0,0,0,0,0,0
152 };
153 
154 static int      archive_write_gnutar_options(struct archive_write *,
155 		    const char *, const char *);
156 static int	archive_format_gnutar_header(struct archive_write *, char h[512],
157 		    struct archive_entry *, int tartype);
158 static int      archive_write_gnutar_header(struct archive_write *,
159 		    struct archive_entry *entry);
160 static ssize_t	archive_write_gnutar_data(struct archive_write *a, const void *buff,
161 		    size_t s);
162 static int	archive_write_gnutar_free(struct archive_write *);
163 static int	archive_write_gnutar_close(struct archive_write *);
164 static int	archive_write_gnutar_finish_entry(struct archive_write *);
165 static int	format_256(int64_t, char *, int);
166 static int	format_number(int64_t, char *, int size, int maxsize);
167 static int	format_octal(int64_t, char *, int);
168 
169 /*
170  * Set output format to 'GNU tar' format.
171  */
172 int
173 archive_write_set_format_gnutar(struct archive *_a)
174 {
175 	struct archive_write *a = (struct archive_write *)_a;
176 	struct gnutar *gnutar;
177 
178 	gnutar = (struct gnutar *)calloc(1, sizeof(*gnutar));
179 	if (gnutar == NULL) {
180 		archive_set_error(&a->archive, ENOMEM,
181 		    "Can't allocate gnutar data");
182 		return (ARCHIVE_FATAL);
183 	}
184 	a->format_data = gnutar;
185 	a->format_name = "gnutar";
186 	a->format_options = archive_write_gnutar_options;
187 	a->format_write_header = archive_write_gnutar_header;
188 	a->format_write_data = archive_write_gnutar_data;
189 	a->format_close = archive_write_gnutar_close;
190 	a->format_free = archive_write_gnutar_free;
191 	a->format_finish_entry = archive_write_gnutar_finish_entry;
192 	a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
193 	a->archive.archive_format_name = "GNU tar";
194 	return (ARCHIVE_OK);
195 }
196 
197 static int
198 archive_write_gnutar_options(struct archive_write *a, const char *key,
199     const char *val)
200 {
201 	struct gnutar *gnutar = (struct gnutar *)a->format_data;
202 	int ret = ARCHIVE_FAILED;
203 
204 	if (strcmp(key, "hdrcharset")  == 0) {
205 		if (val == NULL || val[0] == 0)
206 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
207 			    "%s: hdrcharset option needs a character-set name",
208 			    a->format_name);
209 		else {
210 			gnutar->opt_sconv = archive_string_conversion_to_charset(
211 			    &a->archive, val, 0);
212 			if (gnutar->opt_sconv != NULL)
213 				ret = ARCHIVE_OK;
214 			else
215 				ret = ARCHIVE_FATAL;
216 		}
217 		return (ret);
218 	}
219 
220 	/* Note: The "warn" return is just to inform the options
221 	 * supervisor that we didn't handle it.  It will generate
222 	 * a suitable error if no one used this option. */
223 	return (ARCHIVE_WARN);
224 }
225 
226 static int
227 archive_write_gnutar_close(struct archive_write *a)
228 {
229 	return (__archive_write_nulls(a, 512*2));
230 }
231 
232 static int
233 archive_write_gnutar_free(struct archive_write *a)
234 {
235 	struct gnutar *gnutar;
236 
237 	gnutar = (struct gnutar *)a->format_data;
238 	free(gnutar);
239 	a->format_data = NULL;
240 	return (ARCHIVE_OK);
241 }
242 
243 static int
244 archive_write_gnutar_finish_entry(struct archive_write *a)
245 {
246 	struct gnutar *gnutar;
247 	int ret;
248 
249 	gnutar = (struct gnutar *)a->format_data;
250 	ret = __archive_write_nulls(a, (size_t)
251 	    (gnutar->entry_bytes_remaining + gnutar->entry_padding));
252 	gnutar->entry_bytes_remaining = gnutar->entry_padding = 0;
253 	return (ret);
254 }
255 
256 static ssize_t
257 archive_write_gnutar_data(struct archive_write *a, const void *buff, size_t s)
258 {
259 	struct gnutar *gnutar;
260 	int ret;
261 
262 	gnutar = (struct gnutar *)a->format_data;
263 	if (s > gnutar->entry_bytes_remaining)
264 		s = (size_t)gnutar->entry_bytes_remaining;
265 	ret = __archive_write_output(a, buff, s);
266 	gnutar->entry_bytes_remaining -= s;
267 	if (ret != ARCHIVE_OK)
268 		return (ret);
269 	return (s);
270 }
271 
272 static int
273 archive_write_gnutar_header(struct archive_write *a,
274      struct archive_entry *entry)
275 {
276 	char buff[512];
277 	int r, ret, ret2 = ARCHIVE_OK;
278 	int tartype;
279 	struct gnutar *gnutar;
280 	struct archive_string_conv *sconv;
281 	struct archive_entry *entry_main;
282 
283 	gnutar = (struct gnutar *)a->format_data;
284 
285 	/* Setup default string conversion. */
286 	if (gnutar->opt_sconv == NULL) {
287 		if (!gnutar->init_default_conversion) {
288 			gnutar->sconv_default =
289 			    archive_string_default_conversion_for_write(
290 				&(a->archive));
291 			gnutar->init_default_conversion = 1;
292 		}
293 		sconv = gnutar->sconv_default;
294 	} else
295 		sconv = gnutar->opt_sconv;
296 
297 	/* Only regular files (not hardlinks) have data. */
298 	if (archive_entry_hardlink(entry) != NULL ||
299 	    archive_entry_symlink(entry) != NULL ||
300 	    !(archive_entry_filetype(entry) == AE_IFREG))
301 		archive_entry_set_size(entry, 0);
302 
303 	if (AE_IFDIR == archive_entry_filetype(entry)) {
304 		const char *p;
305 		size_t path_length;
306 		/*
307 		 * Ensure a trailing '/'.  Modify the entry so
308 		 * the client sees the change.
309 		 */
310 #if defined(_WIN32) && !defined(__CYGWIN__)
311 		const wchar_t *wp;
312 
313 		wp = archive_entry_pathname_w(entry);
314 		if (wp != NULL && wp[wcslen(wp) -1] != L'/') {
315 			struct archive_wstring ws;
316 
317 			archive_string_init(&ws);
318 			path_length = wcslen(wp);
319 			if (archive_wstring_ensure(&ws,
320 			    path_length + 2) == NULL) {
321 				archive_set_error(&a->archive, ENOMEM,
322 				    "Can't allocate ustar data");
323 				archive_wstring_free(&ws);
324 				return(ARCHIVE_FATAL);
325 			}
326 			/* Should we keep '\' ? */
327 			if (wp[path_length -1] == L'\\')
328 				path_length--;
329 			archive_wstrncpy(&ws, wp, path_length);
330 			archive_wstrappend_wchar(&ws, L'/');
331 			archive_entry_copy_pathname_w(entry, ws.s);
332 			archive_wstring_free(&ws);
333 			p = NULL;
334 		} else
335 #endif
336 			p = archive_entry_pathname(entry);
337 		/*
338 		 * On Windows, this is a backup operation just in
339 		 * case getting WCS failed. On POSIX, this is a
340 		 * normal operation.
341 		 */
342 		if (p != NULL && p[strlen(p) - 1] != '/') {
343 			struct archive_string as;
344 
345 			archive_string_init(&as);
346 			path_length = strlen(p);
347 			if (archive_string_ensure(&as,
348 			    path_length + 2) == NULL) {
349 				archive_set_error(&a->archive, ENOMEM,
350 				    "Can't allocate ustar data");
351 				archive_string_free(&as);
352 				return(ARCHIVE_FATAL);
353 			}
354 #if defined(_WIN32) && !defined(__CYGWIN__)
355 			/* NOTE: This might break the pathname
356 			 * if the current code page is CP932 and
357 			 * the pathname includes a character '\'
358 			 * as a part of its multibyte pathname. */
359 			if (p[strlen(p) -1] == '\\')
360 				path_length--;
361 			else
362 #endif
363 			archive_strncpy(&as, p, path_length);
364 			archive_strappend_char(&as, '/');
365 			archive_entry_copy_pathname(entry, as.s);
366 			archive_string_free(&as);
367 		}
368 	}
369 
370 #if defined(_WIN32) && !defined(__CYGWIN__)
371 	/* Make sure the path separators in pathname, hardlink and symlink
372 	 * are all slash '/', not the Windows path separator '\'. */
373 	entry_main = __la_win_entry_in_posix_pathseparator(entry);
374 	if (entry_main == NULL) {
375 		archive_set_error(&a->archive, ENOMEM,
376 		    "Can't allocate ustar data");
377 		return(ARCHIVE_FATAL);
378 	}
379 	if (entry != entry_main)
380 		entry = entry_main;
381 	else
382 		entry_main = NULL;
383 #else
384 	entry_main = NULL;
385 #endif
386 	r = archive_entry_pathname_l(entry, &(gnutar->pathname),
387 	    &(gnutar->pathname_length), sconv);
388 	if (r != 0) {
389 		if (errno == ENOMEM) {
390 			archive_set_error(&a->archive, ENOMEM,
391 			    "Can't allocate memory for Pathame");
392 			ret = ARCHIVE_FATAL;
393 			goto exit_write_header;
394 		}
395 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
396 		    "Can't translate pathname '%s' to %s",
397 		    archive_entry_pathname(entry),
398 		    archive_string_conversion_charset_name(sconv));
399 		ret2 = ARCHIVE_WARN;
400 	}
401 	r = archive_entry_uname_l(entry, &(gnutar->uname),
402 	    &(gnutar->uname_length), sconv);
403 	if (r != 0) {
404 		if (errno == ENOMEM) {
405 			archive_set_error(&a->archive, ENOMEM,
406 			    "Can't allocate memory for Uname");
407 			ret = ARCHIVE_FATAL;
408 			goto exit_write_header;
409 		}
410 		archive_set_error(&a->archive,
411 		    ARCHIVE_ERRNO_FILE_FORMAT,
412 		    "Can't translate uname '%s' to %s",
413 		    archive_entry_uname(entry),
414 		    archive_string_conversion_charset_name(sconv));
415 		ret2 = ARCHIVE_WARN;
416 	}
417 	r = archive_entry_gname_l(entry, &(gnutar->gname),
418 	    &(gnutar->gname_length), sconv);
419 	if (r != 0) {
420 		if (errno == ENOMEM) {
421 			archive_set_error(&a->archive, ENOMEM,
422 			    "Can't allocate memory for Gname");
423 			ret = ARCHIVE_FATAL;
424 			goto exit_write_header;
425 		}
426 		archive_set_error(&a->archive,
427 		    ARCHIVE_ERRNO_FILE_FORMAT,
428 		    "Can't translate gname '%s' to %s",
429 		    archive_entry_gname(entry),
430 		    archive_string_conversion_charset_name(sconv));
431 		ret2 = ARCHIVE_WARN;
432 	}
433 
434 	/* If linkname is longer than 100 chars we need to add a 'K' header. */
435 	r = archive_entry_hardlink_l(entry, &(gnutar->linkname),
436 	    &(gnutar->linkname_length), sconv);
437 	if (r != 0) {
438 		if (errno == ENOMEM) {
439 			archive_set_error(&a->archive, ENOMEM,
440 			    "Can't allocate memory for Linkname");
441 			ret = ARCHIVE_FATAL;
442 			goto exit_write_header;
443 		}
444 		archive_set_error(&a->archive,
445 		    ARCHIVE_ERRNO_FILE_FORMAT,
446 		    "Can't translate linkname '%s' to %s",
447 		    archive_entry_hardlink(entry),
448 		    archive_string_conversion_charset_name(sconv));
449 		ret2 = ARCHIVE_WARN;
450 	}
451 	if (gnutar->linkname_length == 0) {
452 		r = archive_entry_symlink_l(entry, &(gnutar->linkname),
453 		    &(gnutar->linkname_length), sconv);
454 		if (r != 0) {
455 			if (errno == ENOMEM) {
456 				archive_set_error(&a->archive, ENOMEM,
457 				    "Can't allocate memory for Linkname");
458 				ret = ARCHIVE_FATAL;
459 				goto exit_write_header;
460 			}
461 			archive_set_error(&a->archive,
462 			    ARCHIVE_ERRNO_FILE_FORMAT,
463 			    "Can't translate linkname '%s' to %s",
464 			    archive_entry_hardlink(entry),
465 			    archive_string_conversion_charset_name(sconv));
466 			ret2 = ARCHIVE_WARN;
467 		}
468 	}
469 	if (gnutar->linkname_length > GNUTAR_linkname_size) {
470 		size_t length = gnutar->linkname_length + 1;
471 		struct archive_entry *temp = archive_entry_new2(&a->archive);
472 
473 		/* Uname/gname here don't really matter since no one reads them;
474 		 * these are the values that GNU tar happens to use on FreeBSD. */
475 		archive_entry_set_uname(temp, "root");
476 		archive_entry_set_gname(temp, "wheel");
477 
478 		archive_entry_set_pathname(temp, "././@LongLink");
479 		archive_entry_set_size(temp, length);
480 		ret = archive_format_gnutar_header(a, buff, temp, 'K');
481 		archive_entry_free(temp);
482 		if (ret < ARCHIVE_WARN)
483 			goto exit_write_header;
484 		ret = __archive_write_output(a, buff, 512);
485 		if (ret < ARCHIVE_WARN)
486 			goto exit_write_header;
487 		/* Write name and trailing null byte. */
488 		ret = __archive_write_output(a, gnutar->linkname, length);
489 		if (ret < ARCHIVE_WARN)
490 			goto exit_write_header;
491 		/* Pad to 512 bytes */
492 		ret = __archive_write_nulls(a, 0x1ff & (-(ssize_t)length));
493 		if (ret < ARCHIVE_WARN)
494 			goto exit_write_header;
495 	}
496 
497 	/* If pathname is longer than 100 chars we need to add an 'L' header. */
498 	if (gnutar->pathname_length > GNUTAR_name_size) {
499 		const char *pathname = gnutar->pathname;
500 		size_t length = gnutar->pathname_length + 1;
501 		struct archive_entry *temp = archive_entry_new2(&a->archive);
502 
503 		/* Uname/gname here don't really matter since no one reads them;
504 		 * these are the values that GNU tar happens to use on FreeBSD. */
505 		archive_entry_set_uname(temp, "root");
506 		archive_entry_set_gname(temp, "wheel");
507 
508 		archive_entry_set_pathname(temp, "././@LongLink");
509 		archive_entry_set_size(temp, length);
510 		ret = archive_format_gnutar_header(a, buff, temp, 'L');
511 		archive_entry_free(temp);
512 		if (ret < ARCHIVE_WARN)
513 			goto exit_write_header;
514 		ret = __archive_write_output(a, buff, 512);
515 		if(ret < ARCHIVE_WARN)
516 			goto exit_write_header;
517 		/* Write pathname + trailing null byte. */
518 		ret = __archive_write_output(a, pathname, length);
519 		if(ret < ARCHIVE_WARN)
520 			goto exit_write_header;
521 		/* Pad to multiple of 512 bytes. */
522 		ret = __archive_write_nulls(a, 0x1ff & (-(ssize_t)length));
523 		if (ret < ARCHIVE_WARN)
524 			goto exit_write_header;
525 	}
526 
527 	if (archive_entry_hardlink(entry) != NULL) {
528 		tartype = '1';
529 	} else
530 		switch (archive_entry_filetype(entry)) {
531 		case AE_IFREG: tartype = '0' ; break;
532 		case AE_IFLNK: tartype = '2' ; break;
533 		case AE_IFCHR: tartype = '3' ; break;
534 		case AE_IFBLK: tartype = '4' ; break;
535 		case AE_IFDIR: tartype = '5' ; break;
536 		case AE_IFIFO: tartype = '6' ; break;
537 		case AE_IFSOCK:
538 			archive_set_error(&a->archive,
539 			    ARCHIVE_ERRNO_FILE_FORMAT,
540 			    "tar format cannot archive socket");
541 			ret = ARCHIVE_FAILED;
542 			goto exit_write_header;
543 		default:
544 			archive_set_error(&a->archive,
545 			    ARCHIVE_ERRNO_FILE_FORMAT,
546 			    "tar format cannot archive this (mode=0%lo)",
547 			    (unsigned long)archive_entry_mode(entry));
548 			ret = ARCHIVE_FAILED;
549 			goto exit_write_header;
550 		}
551 
552 	ret = archive_format_gnutar_header(a, buff, entry, tartype);
553 	if (ret < ARCHIVE_WARN)
554 		goto exit_write_header;
555 	if (ret2 < ret)
556 		ret = ret2;
557 	ret2 = __archive_write_output(a, buff, 512);
558 	if (ret2 < ARCHIVE_WARN) {
559 		ret = ret2;
560 		goto exit_write_header;
561 	}
562 	if (ret2 < ret)
563 		ret = ret2;
564 
565 	gnutar->entry_bytes_remaining = archive_entry_size(entry);
566 	gnutar->entry_padding = 0x1ff & (-(int64_t)gnutar->entry_bytes_remaining);
567 exit_write_header:
568 	if (entry_main)
569 		archive_entry_free(entry_main);
570 	return (ret);
571 }
572 
573 static int
574 archive_format_gnutar_header(struct archive_write *a, char h[512],
575     struct archive_entry *entry, int tartype)
576 {
577 	unsigned int checksum;
578 	int i, ret;
579 	size_t copy_length;
580 	const char *p;
581 	struct gnutar *gnutar;
582 
583 	gnutar = (struct gnutar *)a->format_data;
584 
585 	ret = 0;
586 
587 	/*
588 	 * The "template header" already includes the signature,
589 	 * various end-of-field markers, and other required elements.
590 	 */
591 	memcpy(h, &template_header, 512);
592 
593 	/*
594 	 * Because the block is already null-filled, and strings
595 	 * are allowed to exactly fill their destination (without null),
596 	 * I use memcpy(dest, src, strlen()) here a lot to copy strings.
597 	 */
598 
599 	if (tartype == 'K' || tartype == 'L') {
600 		p = archive_entry_pathname(entry);
601 		copy_length = strlen(p);
602 	} else {
603 		p = gnutar->pathname;
604 		copy_length = gnutar->pathname_length;
605 	}
606 	if (copy_length > GNUTAR_name_size)
607 		copy_length = GNUTAR_name_size;
608 	memcpy(h + GNUTAR_name_offset, p, copy_length);
609 
610 	if ((copy_length = gnutar->linkname_length) > 0) {
611 		if (copy_length > GNUTAR_linkname_size)
612 			copy_length = GNUTAR_linkname_size;
613 		memcpy(h + GNUTAR_linkname_offset, gnutar->linkname,
614 		    copy_length);
615 	}
616 
617 	/* TODO: How does GNU tar handle unames longer than GNUTAR_uname_size? */
618 	if (tartype == 'K' || tartype == 'L') {
619 		p = archive_entry_uname(entry);
620 		copy_length = strlen(p);
621 	} else {
622 		p = gnutar->uname;
623 		copy_length = gnutar->uname_length;
624 	}
625 	if (copy_length > 0) {
626 		if (copy_length > GNUTAR_uname_size)
627 			copy_length = GNUTAR_uname_size;
628 		memcpy(h + GNUTAR_uname_offset, p, copy_length);
629 	}
630 
631 	/* TODO: How does GNU tar handle gnames longer than GNUTAR_gname_size? */
632 	if (tartype == 'K' || tartype == 'L') {
633 		p = archive_entry_gname(entry);
634 		copy_length = strlen(p);
635 	} else {
636 		p = gnutar->gname;
637 		copy_length = gnutar->gname_length;
638 	}
639 	if (copy_length > 0) {
640 		if (strlen(p) > GNUTAR_gname_size)
641 			copy_length = GNUTAR_gname_size;
642 		memcpy(h + GNUTAR_gname_offset, p, copy_length);
643 	}
644 
645 	/* By truncating the mode here, we ensure it always fits. */
646 	format_octal(archive_entry_mode(entry) & 07777,
647 	    h + GNUTAR_mode_offset, GNUTAR_mode_size);
648 
649 	/* GNU tar supports base-256 here, so should never overflow. */
650 	if (format_number(archive_entry_uid(entry), h + GNUTAR_uid_offset,
651 		GNUTAR_uid_size, GNUTAR_uid_max_size)) {
652 		archive_set_error(&a->archive, ERANGE,
653 		    "Numeric user ID %jd too large",
654 		    (intmax_t)archive_entry_uid(entry));
655 		ret = ARCHIVE_FAILED;
656 	}
657 
658 	/* GNU tar supports base-256 here, so should never overflow. */
659 	if (format_number(archive_entry_gid(entry), h + GNUTAR_gid_offset,
660 		GNUTAR_gid_size, GNUTAR_gid_max_size)) {
661 		archive_set_error(&a->archive, ERANGE,
662 		    "Numeric group ID %jd too large",
663 		    (intmax_t)archive_entry_gid(entry));
664 		ret = ARCHIVE_FAILED;
665 	}
666 
667 	/* GNU tar supports base-256 here, so should never overflow. */
668 	if (format_number(archive_entry_size(entry), h + GNUTAR_size_offset,
669 		GNUTAR_size_size, GNUTAR_size_max_size)) {
670 		archive_set_error(&a->archive, ERANGE,
671 		    "File size out of range");
672 		ret = ARCHIVE_FAILED;
673 	}
674 
675 	/* Shouldn't overflow before 2106, since mtime field is 33 bits. */
676 	format_octal(archive_entry_mtime(entry),
677 	    h + GNUTAR_mtime_offset, GNUTAR_mtime_size);
678 
679 	if (archive_entry_filetype(entry) == AE_IFBLK
680 	    || archive_entry_filetype(entry) == AE_IFCHR) {
681 		if (format_octal(archive_entry_rdevmajor(entry),
682 		    h + GNUTAR_rdevmajor_offset,
683 			GNUTAR_rdevmajor_size)) {
684 			archive_set_error(&a->archive, ERANGE,
685 			    "Major device number too large");
686 			ret = ARCHIVE_FAILED;
687 		}
688 
689 		if (format_octal(archive_entry_rdevminor(entry),
690 		    h + GNUTAR_rdevminor_offset,
691 			GNUTAR_rdevminor_size)) {
692 			archive_set_error(&a->archive, ERANGE,
693 			    "Minor device number too large");
694 			ret = ARCHIVE_FAILED;
695 		}
696 	}
697 
698 	h[GNUTAR_typeflag_offset] = tartype;
699 
700 	checksum = 0;
701 	for (i = 0; i < 512; i++)
702 		checksum += 255 & (unsigned int)h[i];
703 	h[GNUTAR_checksum_offset + 6] = '\0'; /* Can't be pre-set in the template. */
704 	/* h[GNUTAR_checksum_offset + 7] = ' '; */ /* This is pre-set in the template. */
705 	format_octal(checksum, h + GNUTAR_checksum_offset, 6);
706 	return (ret);
707 }
708 
709 /*
710  * Format a number into a field, falling back to base-256 if necessary.
711  */
712 static int
713 format_number(int64_t v, char *p, int s, int maxsize)
714 {
715 	int64_t limit = ((int64_t)1 << (s*3));
716 
717 	if (v < limit)
718 		return (format_octal(v, p, s));
719 	return (format_256(v, p, maxsize));
720 }
721 
722 /*
723  * Format a number into the specified field using base-256.
724  */
725 static int
726 format_256(int64_t v, char *p, int s)
727 {
728 	p += s;
729 	while (s-- > 0) {
730 		*--p = (char)(v & 0xff);
731 		v >>= 8;
732 	}
733 	*p |= 0x80; /* Set the base-256 marker bit. */
734 	return (0);
735 }
736 
737 /*
738  * Format a number into the specified field using octal.
739  */
740 static int
741 format_octal(int64_t v, char *p, int s)
742 {
743 	int len = s;
744 
745 	/* Octal values can't be negative, so use 0. */
746 	if (v < 0)
747 		v = 0;
748 
749 	p += s;		/* Start at the end and work backwards. */
750 	while (s-- > 0) {
751 		*--p = (char)('0' + (v & 7));
752 		v >>= 3;
753 	}
754 
755 	if (v == 0)
756 		return (0);
757 
758 	/* If it overflowed, fill field with max value. */
759 	while (len-- > 0)
760 		*p++ = '7';
761 
762 	return (-1);
763 }
764