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 *, char 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 = 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 char 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 /* Sanity check. */
297 if (archive_entry_pathname(entry) == NULL
298 #if defined(_WIN32) && !defined(__CYGWIN__)
299 && archive_entry_pathname_w(entry) == NULL
300 #endif
301 ) {
302 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
303 "Can't record entry in tar file without pathname");
304 return ARCHIVE_FAILED;
305 }
306
307 /* Only regular files (not hardlinks) have data. */
308 if (archive_entry_hardlink(entry) != NULL ||
309 archive_entry_symlink(entry) != NULL ||
310 archive_entry_filetype(entry) != AE_IFREG)
311 archive_entry_set_size(entry, 0);
312
313 if (AE_IFDIR == archive_entry_filetype(entry)) {
314 const char *p;
315 size_t path_length;
316 /*
317 * Ensure a trailing '/'. Modify the entry so
318 * the client sees the change.
319 */
320 #if defined(_WIN32) && !defined(__CYGWIN__)
321 const wchar_t *wp;
322
323 wp = archive_entry_pathname_w(entry);
324 if (wp != NULL && wp[wcslen(wp) -1] != L'/') {
325 struct archive_wstring ws;
326
327 archive_string_init(&ws);
328 path_length = wcslen(wp);
329 if (archive_wstring_ensure(&ws,
330 path_length + 2) == NULL) {
331 archive_set_error(&a->archive, ENOMEM,
332 "Can't allocate ustar data");
333 archive_wstring_free(&ws);
334 return(ARCHIVE_FATAL);
335 }
336 /* Should we keep '\' ? */
337 if (wp[path_length -1] == L'\\')
338 path_length--;
339 archive_wstrncpy(&ws, wp, path_length);
340 archive_wstrappend_wchar(&ws, L'/');
341 archive_entry_copy_pathname_w(entry, ws.s);
342 archive_wstring_free(&ws);
343 p = NULL;
344 } else
345 #endif
346 p = archive_entry_pathname(entry);
347 /*
348 * On Windows, this is a backup operation just in
349 * case getting WCS failed. On POSIX, this is a
350 * normal operation.
351 */
352 if (p != NULL && p[0] != '\0' && p[strlen(p) - 1] != '/') {
353 struct archive_string as;
354
355 archive_string_init(&as);
356 path_length = strlen(p);
357 if (archive_string_ensure(&as,
358 path_length + 2) == NULL) {
359 archive_set_error(&a->archive, ENOMEM,
360 "Can't allocate ustar data");
361 archive_string_free(&as);
362 return(ARCHIVE_FATAL);
363 }
364 #if defined(_WIN32) && !defined(__CYGWIN__)
365 /* NOTE: This might break the pathname
366 * if the current code page is CP932 and
367 * the pathname includes a character '\'
368 * as a part of its multibyte pathname. */
369 if (p[strlen(p) -1] == '\\')
370 path_length--;
371 else
372 #endif
373 archive_strncpy(&as, p, path_length);
374 archive_strappend_char(&as, '/');
375 archive_entry_copy_pathname(entry, as.s);
376 archive_string_free(&as);
377 }
378 }
379
380 #if defined(_WIN32) && !defined(__CYGWIN__)
381 /* Make sure the path separators in pathname, hardlink and symlink
382 * are all slash '/', not the Windows path separator '\'. */
383 entry_main = __la_win_entry_in_posix_pathseparator(entry);
384 if (entry_main == NULL) {
385 archive_set_error(&a->archive, ENOMEM,
386 "Can't allocate ustar data");
387 return(ARCHIVE_FATAL);
388 }
389 if (entry != entry_main)
390 entry = entry_main;
391 else
392 entry_main = NULL;
393 #else
394 entry_main = NULL;
395 #endif
396 r = archive_entry_pathname_l(entry, &(gnutar->pathname),
397 &(gnutar->pathname_length), sconv);
398 if (r != 0) {
399 const char* p_mbs;
400 if (errno == ENOMEM) {
401 archive_set_error(&a->archive, ENOMEM,
402 "Can't allocate memory for pathname");
403 ret = ARCHIVE_FATAL;
404 goto exit_write_header;
405 }
406 p_mbs = archive_entry_pathname(entry);
407 if (p_mbs) {
408 /* We have a wrongly-encoded MBS pathname.
409 * Warn and use it. */
410 archive_set_error(&a->archive,
411 ARCHIVE_ERRNO_FILE_FORMAT,
412 "Can't translate pathname '%s' to %s", p_mbs,
413 archive_string_conversion_charset_name(sconv));
414 ret2 = ARCHIVE_WARN;
415 } else {
416 /* We have no MBS pathname. Fail. */
417 archive_set_error(&a->archive,
418 ARCHIVE_ERRNO_FILE_FORMAT,
419 "Can't translate pathname to %s",
420 archive_string_conversion_charset_name(sconv));
421 return ARCHIVE_FAILED;
422 }
423 }
424 r = archive_entry_uname_l(entry, &(gnutar->uname),
425 &(gnutar->uname_length), sconv);
426 if (r != 0) {
427 if (errno == ENOMEM) {
428 archive_set_error(&a->archive, ENOMEM,
429 "Can't allocate memory for Uname");
430 ret = ARCHIVE_FATAL;
431 goto exit_write_header;
432 }
433 archive_set_error(&a->archive,
434 ARCHIVE_ERRNO_FILE_FORMAT,
435 "Can't translate uname '%s' to %s",
436 archive_entry_uname(entry),
437 archive_string_conversion_charset_name(sconv));
438 ret2 = ARCHIVE_WARN;
439 }
440 r = archive_entry_gname_l(entry, &(gnutar->gname),
441 &(gnutar->gname_length), sconv);
442 if (r != 0) {
443 if (errno == ENOMEM) {
444 archive_set_error(&a->archive, ENOMEM,
445 "Can't allocate memory for Gname");
446 ret = ARCHIVE_FATAL;
447 goto exit_write_header;
448 }
449 archive_set_error(&a->archive,
450 ARCHIVE_ERRNO_FILE_FORMAT,
451 "Can't translate gname '%s' to %s",
452 archive_entry_gname(entry),
453 archive_string_conversion_charset_name(sconv));
454 ret2 = ARCHIVE_WARN;
455 }
456
457 /* If linkname is longer than 100 chars we need to add a 'K' header. */
458 r = archive_entry_hardlink_l(entry, &(gnutar->linkname),
459 &(gnutar->linkname_length), sconv);
460 if (r != 0) {
461 if (errno == ENOMEM) {
462 archive_set_error(&a->archive, ENOMEM,
463 "Can't allocate memory for Linkname");
464 ret = ARCHIVE_FATAL;
465 goto exit_write_header;
466 }
467 archive_set_error(&a->archive,
468 ARCHIVE_ERRNO_FILE_FORMAT,
469 "Can't translate linkname '%s' to %s",
470 archive_entry_hardlink(entry),
471 archive_string_conversion_charset_name(sconv));
472 ret2 = ARCHIVE_WARN;
473 }
474 if (gnutar->linkname_length == 0) {
475 r = archive_entry_symlink_l(entry, &(gnutar->linkname),
476 &(gnutar->linkname_length), sconv);
477 if (r != 0) {
478 if (errno == ENOMEM) {
479 archive_set_error(&a->archive, ENOMEM,
480 "Can't allocate memory for Linkname");
481 ret = ARCHIVE_FATAL;
482 goto exit_write_header;
483 }
484 archive_set_error(&a->archive,
485 ARCHIVE_ERRNO_FILE_FORMAT,
486 "Can't translate linkname '%s' to %s",
487 archive_entry_hardlink(entry),
488 archive_string_conversion_charset_name(sconv));
489 ret2 = ARCHIVE_WARN;
490 }
491 }
492 if (gnutar->linkname_length > GNUTAR_linkname_size) {
493 size_t length = gnutar->linkname_length + 1;
494 struct archive_entry *temp = archive_entry_new2(&a->archive);
495
496 /* Uname/gname here don't really matter since no one reads them;
497 * these are the values that GNU tar happens to use on FreeBSD. */
498 archive_entry_set_uname(temp, "root");
499 archive_entry_set_gname(temp, "wheel");
500
501 archive_entry_set_pathname(temp, "././@LongLink");
502 archive_entry_set_size(temp, length);
503 ret = archive_format_gnutar_header(a, buff, temp, 'K');
504 archive_entry_free(temp);
505 if (ret < ARCHIVE_WARN)
506 goto exit_write_header;
507 ret = __archive_write_output(a, buff, 512);
508 if (ret < ARCHIVE_WARN)
509 goto exit_write_header;
510 /* Write name and trailing null byte. */
511 ret = __archive_write_output(a, gnutar->linkname, length);
512 if (ret < ARCHIVE_WARN)
513 goto exit_write_header;
514 /* Pad to 512 bytes */
515 ret = __archive_write_nulls(a, 0x1ff & (-(ssize_t)length));
516 if (ret < ARCHIVE_WARN)
517 goto exit_write_header;
518 }
519
520 /* If pathname is longer than 100 chars we need to add an 'L' header. */
521 if (gnutar->pathname_length > GNUTAR_name_size) {
522 const char *pathname = gnutar->pathname;
523 size_t length = gnutar->pathname_length + 1;
524 struct archive_entry *temp = archive_entry_new2(&a->archive);
525
526 /* Uname/gname here don't really matter since no one reads them;
527 * these are the values that GNU tar happens to use on FreeBSD. */
528 archive_entry_set_uname(temp, "root");
529 archive_entry_set_gname(temp, "wheel");
530
531 archive_entry_set_pathname(temp, "././@LongName");
532 archive_entry_set_size(temp, length);
533 ret = archive_format_gnutar_header(a, buff, temp, 'L');
534 archive_entry_free(temp);
535 if (ret < ARCHIVE_WARN)
536 goto exit_write_header;
537 ret = __archive_write_output(a, buff, 512);
538 if(ret < ARCHIVE_WARN)
539 goto exit_write_header;
540 /* Write pathname + trailing null byte. */
541 ret = __archive_write_output(a, pathname, length);
542 if(ret < ARCHIVE_WARN)
543 goto exit_write_header;
544 /* Pad to multiple of 512 bytes. */
545 ret = __archive_write_nulls(a, 0x1ff & (-(ssize_t)length));
546 if (ret < ARCHIVE_WARN)
547 goto exit_write_header;
548 }
549
550 if (archive_entry_hardlink_is_set(entry)) {
551 tartype = '1';
552 } else
553 switch (archive_entry_filetype(entry)) {
554 case AE_IFREG: tartype = '0' ; break;
555 case AE_IFLNK: tartype = '2' ; break;
556 case AE_IFCHR: tartype = '3' ; break;
557 case AE_IFBLK: tartype = '4' ; break;
558 case AE_IFDIR: tartype = '5' ; break;
559 case AE_IFIFO: tartype = '6' ; break;
560 default: /* AE_IFSOCK and unknown */
561 __archive_write_entry_filetype_unsupported(
562 &a->archive, entry, "gnutar");
563 ret = ARCHIVE_FAILED;
564 goto exit_write_header;
565 }
566
567 ret = archive_format_gnutar_header(a, buff, entry, tartype);
568 if (ret < ARCHIVE_WARN)
569 goto exit_write_header;
570 if (ret2 < ret)
571 ret = ret2;
572 ret2 = __archive_write_output(a, buff, 512);
573 if (ret2 < ARCHIVE_WARN) {
574 ret = ret2;
575 goto exit_write_header;
576 }
577 if (ret2 < ret)
578 ret = ret2;
579
580 gnutar->entry_bytes_remaining = archive_entry_size(entry);
581 gnutar->entry_padding = 0x1ff & (-(int64_t)gnutar->entry_bytes_remaining);
582 exit_write_header:
583 archive_entry_free(entry_main);
584 return (ret);
585 }
586
587 static int
archive_format_gnutar_header(struct archive_write * a,char h[512],struct archive_entry * entry,char tartype)588 archive_format_gnutar_header(struct archive_write *a, char h[512],
589 struct archive_entry *entry, char tartype)
590 {
591 unsigned int checksum;
592 int i, ret;
593 size_t copy_length;
594 const char *p;
595 struct gnutar *gnutar;
596
597 gnutar = (struct gnutar *)a->format_data;
598
599 ret = 0;
600
601 /*
602 * The "template header" already includes the signature,
603 * various end-of-field markers, and other required elements.
604 */
605 memcpy(h, &template_header, 512);
606
607 /*
608 * Because the block is already null-filled, and strings
609 * are allowed to exactly fill their destination (without null),
610 * I use memcpy(dest, src, strlen()) here a lot to copy strings.
611 */
612
613 if (tartype == 'K' || tartype == 'L') {
614 p = archive_entry_pathname(entry);
615 copy_length = strlen(p);
616 } else {
617 p = gnutar->pathname;
618 copy_length = gnutar->pathname_length;
619 }
620 if (copy_length > GNUTAR_name_size)
621 copy_length = GNUTAR_name_size;
622 memcpy(h + GNUTAR_name_offset, p, copy_length);
623
624 if ((copy_length = gnutar->linkname_length) > 0) {
625 if (copy_length > GNUTAR_linkname_size)
626 copy_length = GNUTAR_linkname_size;
627 memcpy(h + GNUTAR_linkname_offset, gnutar->linkname,
628 copy_length);
629 }
630
631 /* TODO: How does GNU tar handle unames longer than GNUTAR_uname_size? */
632 if (tartype == 'K' || tartype == 'L') {
633 p = archive_entry_uname(entry);
634 copy_length = strlen(p);
635 } else {
636 p = gnutar->uname;
637 copy_length = gnutar->uname_length;
638 }
639 if (copy_length > 0) {
640 if (copy_length > GNUTAR_uname_size)
641 copy_length = GNUTAR_uname_size;
642 memcpy(h + GNUTAR_uname_offset, p, copy_length);
643 }
644
645 /* TODO: How does GNU tar handle gnames longer than GNUTAR_gname_size? */
646 if (tartype == 'K' || tartype == 'L') {
647 p = archive_entry_gname(entry);
648 copy_length = strlen(p);
649 } else {
650 p = gnutar->gname;
651 copy_length = gnutar->gname_length;
652 }
653 if (copy_length > 0) {
654 if (strlen(p) > GNUTAR_gname_size)
655 copy_length = GNUTAR_gname_size;
656 memcpy(h + GNUTAR_gname_offset, p, copy_length);
657 }
658
659 /* By truncating the mode here, we ensure it always fits. */
660 format_octal(archive_entry_mode(entry) & 07777,
661 h + GNUTAR_mode_offset, GNUTAR_mode_size);
662
663 /* GNU tar supports base-256 here, so should never overflow. */
664 if (format_number(archive_entry_uid(entry), h + GNUTAR_uid_offset,
665 GNUTAR_uid_size, GNUTAR_uid_max_size)) {
666 archive_set_error(&a->archive, ERANGE,
667 "Numeric user ID %jd too large for gnutar format",
668 (intmax_t)archive_entry_uid(entry));
669 ret = ARCHIVE_FAILED;
670 }
671
672 /* GNU tar supports base-256 here, so should never overflow. */
673 if (format_number(archive_entry_gid(entry), h + GNUTAR_gid_offset,
674 GNUTAR_gid_size, GNUTAR_gid_max_size)) {
675 archive_set_error(&a->archive, ERANGE,
676 "Numeric group ID %jd too large for gnutar format",
677 (intmax_t)archive_entry_gid(entry));
678 ret = ARCHIVE_FAILED;
679 }
680
681 /* GNU tar supports base-256 here, so should never overflow. */
682 if (format_number(archive_entry_size(entry), h + GNUTAR_size_offset,
683 GNUTAR_size_size, GNUTAR_size_max_size)) {
684 archive_set_error(&a->archive, ERANGE,
685 "File size out of range");
686 ret = ARCHIVE_FAILED;
687 }
688
689 /* Shouldn't overflow before 2106, since mtime field is 33 bits. */
690 format_octal(archive_entry_mtime(entry),
691 h + GNUTAR_mtime_offset, GNUTAR_mtime_size);
692
693 if (archive_entry_filetype(entry) == AE_IFBLK
694 || archive_entry_filetype(entry) == AE_IFCHR) {
695 if (format_octal(archive_entry_rdevmajor(entry),
696 h + GNUTAR_rdevmajor_offset,
697 GNUTAR_rdevmajor_size)) {
698 archive_set_error(&a->archive, ERANGE,
699 "Major device number too large for gnutar format");
700 ret = ARCHIVE_FAILED;
701 }
702
703 if (format_octal(archive_entry_rdevminor(entry),
704 h + GNUTAR_rdevminor_offset,
705 GNUTAR_rdevminor_size)) {
706 archive_set_error(&a->archive, ERANGE,
707 "Minor device number too large for gnutar format");
708 ret = ARCHIVE_FAILED;
709 }
710 }
711
712 h[GNUTAR_typeflag_offset] = tartype;
713
714 checksum = 0;
715 for (i = 0; i < 512; i++)
716 checksum += 255 & (unsigned int)h[i];
717 h[GNUTAR_checksum_offset + 6] = '\0'; /* Can't be pre-set in the template. */
718 /* h[GNUTAR_checksum_offset + 7] = ' '; */ /* This is pre-set in the template. */
719 format_octal(checksum, h + GNUTAR_checksum_offset, 6);
720 return (ret);
721 }
722
723 /*
724 * Format a number into a field, falling back to base-256 if necessary.
725 */
726 static int
format_number(int64_t v,char * p,int s,int maxsize)727 format_number(int64_t v, char *p, int s, int maxsize)
728 {
729 int64_t limit = ((int64_t)1 << (s*3));
730
731 if (v < limit)
732 return (format_octal(v, p, s));
733 return (format_256(v, p, maxsize));
734 }
735
736 /*
737 * Format a number into the specified field using base-256.
738 */
739 static int
format_256(int64_t v,char * p,int s)740 format_256(int64_t v, char *p, int s)
741 {
742 p += s;
743 while (s-- > 0) {
744 *--p = (char)(v & 0xff);
745 v >>= 8;
746 }
747 *p |= 0x80; /* Set the base-256 marker bit. */
748 return (0);
749 }
750
751 /*
752 * Format a number into the specified field using octal.
753 */
754 static int
format_octal(int64_t v,char * p,int s)755 format_octal(int64_t v, char *p, int s)
756 {
757 int len = s;
758
759 /* Octal values can't be negative, so use 0. */
760 if (v < 0)
761 v = 0;
762
763 p += s; /* Start at the end and work backwards. */
764 while (s-- > 0) {
765 *--p = (char)('0' + (v & 7));
766 v >>= 3;
767 }
768
769 if (v == 0)
770 return (0);
771
772 /* If it overflowed, fill field with max value. */
773 while (len-- > 0)
774 *p++ = '7';
775
776 return (-1);
777 }
778