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