]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libarchive/archive_write_set_format_pax.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libarchive / archive_write_set_format_pax.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD$");
28
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #endif
38
39 #include "archive.h"
40 #include "archive_entry.h"
41 #include "archive_private.h"
42 #include "archive_write_private.h"
43
44 struct pax {
45         uint64_t        entry_bytes_remaining;
46         uint64_t        entry_padding;
47         struct archive_string   pax_header;
48 };
49
50 static void              add_pax_attr(struct archive_string *, const char *key,
51                              const char *value);
52 static void              add_pax_attr_int(struct archive_string *,
53                              const char *key, int64_t value);
54 static void              add_pax_attr_time(struct archive_string *,
55                              const char *key, int64_t sec,
56                              unsigned long nanos);
57 static void              add_pax_attr_w(struct archive_string *,
58                              const char *key, const wchar_t *wvalue);
59 static ssize_t           archive_write_pax_data(struct archive_write *,
60                              const void *, size_t);
61 static int               archive_write_pax_finish(struct archive_write *);
62 static int               archive_write_pax_destroy(struct archive_write *);
63 static int               archive_write_pax_finish_entry(struct archive_write *);
64 static int               archive_write_pax_header(struct archive_write *,
65                              struct archive_entry *);
66 static char             *base64_encode(const char *src, size_t len);
67 static char             *build_pax_attribute_name(char *dest, const char *src);
68 static char             *build_ustar_entry_name(char *dest, const char *src,
69                              size_t src_length, const char *insert);
70 static char             *format_int(char *dest, int64_t);
71 static int               has_non_ASCII(const wchar_t *);
72 static char             *url_encode(const char *in);
73 static int               write_nulls(struct archive_write *, size_t);
74
75 /*
76  * Set output format to 'restricted pax' format.
77  *
78  * This is the same as normal 'pax', but tries to suppress
79  * the pax header whenever possible.  This is the default for
80  * bsdtar, for instance.
81  */
82 int
83 archive_write_set_format_pax_restricted(struct archive *_a)
84 {
85         struct archive_write *a = (struct archive_write *)_a;
86         int r;
87         r = archive_write_set_format_pax(&a->archive);
88         a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_RESTRICTED;
89         a->archive.archive_format_name = "restricted POSIX pax interchange";
90         return (r);
91 }
92
93 /*
94  * Set output format to 'pax' format.
95  */
96 int
97 archive_write_set_format_pax(struct archive *_a)
98 {
99         struct archive_write *a = (struct archive_write *)_a;
100         struct pax *pax;
101
102         if (a->format_destroy != NULL)
103                 (a->format_destroy)(a);
104
105         pax = (struct pax *)malloc(sizeof(*pax));
106         if (pax == NULL) {
107                 archive_set_error(&a->archive, ENOMEM, "Can't allocate pax data");
108                 return (ARCHIVE_FATAL);
109         }
110         memset(pax, 0, sizeof(*pax));
111         a->format_data = pax;
112
113         a->pad_uncompressed = 1;
114         a->format_name = "pax";
115         a->format_write_header = archive_write_pax_header;
116         a->format_write_data = archive_write_pax_data;
117         a->format_finish = archive_write_pax_finish;
118         a->format_destroy = archive_write_pax_destroy;
119         a->format_finish_entry = archive_write_pax_finish_entry;
120         a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
121         a->archive.archive_format_name = "POSIX pax interchange";
122         return (ARCHIVE_OK);
123 }
124
125 /*
126  * Note: This code assumes that 'nanos' has the same sign as 'sec',
127  * which implies that sec=-1, nanos=200000000 represents -1.2 seconds
128  * and not -0.8 seconds.  This is a pretty pedantic point, as we're
129  * unlikely to encounter many real files created before Jan 1, 1970,
130  * much less ones with timestamps recorded to sub-second resolution.
131  */
132 static void
133 add_pax_attr_time(struct archive_string *as, const char *key,
134     int64_t sec, unsigned long nanos)
135 {
136         int digit, i;
137         char *t;
138         /*
139          * Note that each byte contributes fewer than 3 base-10
140          * digits, so this will always be big enough.
141          */
142         char tmp[1 + 3*sizeof(sec) + 1 + 3*sizeof(nanos)];
143
144         tmp[sizeof(tmp) - 1] = 0;
145         t = tmp + sizeof(tmp) - 1;
146
147         /* Skip trailing zeros in the fractional part. */
148         for (digit = 0, i = 10; i > 0 && digit == 0; i--) {
149                 digit = nanos % 10;
150                 nanos /= 10;
151         }
152
153         /* Only format the fraction if it's non-zero. */
154         if (i > 0) {
155                 while (i > 0) {
156                         *--t = "0123456789"[digit];
157                         digit = nanos % 10;
158                         nanos /= 10;
159                         i--;
160                 }
161                 *--t = '.';
162         }
163         t = format_int(t, sec);
164
165         add_pax_attr(as, key, t);
166 }
167
168 static char *
169 format_int(char *t, int64_t i)
170 {
171         int sign;
172
173         if (i < 0) {
174                 sign = -1;
175                 i = -i;
176         } else
177                 sign = 1;
178
179         do {
180                 *--t = "0123456789"[i % 10];
181         } while (i /= 10);
182         if (sign < 0)
183                 *--t = '-';
184         return (t);
185 }
186
187 static void
188 add_pax_attr_int(struct archive_string *as, const char *key, int64_t value)
189 {
190         char tmp[1 + 3 * sizeof(value)];
191
192         tmp[sizeof(tmp) - 1] = 0;
193         add_pax_attr(as, key, format_int(tmp + sizeof(tmp) - 1, value));
194 }
195
196 static char *
197 utf8_encode(const wchar_t *wval)
198 {
199         int utf8len;
200         const wchar_t *wp;
201         unsigned long wc;
202         char *utf8_value, *p;
203
204         utf8len = 0;
205         for (wp = wval; *wp != L'\0'; ) {
206                 wc = *wp++;
207
208                 if (wc >= 0xd800 && wc <= 0xdbff
209                     && *wp >= 0xdc00 && *wp <= 0xdfff) {
210                         /* This is a surrogate pair.  Combine into a
211                          * full Unicode value before encoding into
212                          * UTF-8. */
213                         wc = (wc - 0xd800) << 10; /* High 10 bits */
214                         wc += (*wp++ - 0xdc00); /* Low 10 bits */
215                         wc += 0x10000; /* Skip BMP */
216                 }
217                 if (wc <= 0x7f)
218                         utf8len++;
219                 else if (wc <= 0x7ff)
220                         utf8len += 2;
221                 else if (wc <= 0xffff)
222                         utf8len += 3;
223                 else if (wc <= 0x1fffff)
224                         utf8len += 4;
225                 else if (wc <= 0x3ffffff)
226                         utf8len += 5;
227                 else if (wc <= 0x7fffffff)
228                         utf8len += 6;
229                 /* Ignore larger values; UTF-8 can't encode them. */
230         }
231
232         utf8_value = (char *)malloc(utf8len + 1);
233         if (utf8_value == NULL) {
234                 __archive_errx(1, "Not enough memory for attributes");
235                 return (NULL);
236         }
237
238         for (wp = wval, p = utf8_value; *wp != L'\0'; ) {
239                 wc = *wp++;
240                 if (wc >= 0xd800 && wc <= 0xdbff
241                     && *wp >= 0xdc00 && *wp <= 0xdfff) {
242                         /* Combine surrogate pair. */
243                         wc = (wc - 0xd800) << 10;
244                         wc += *wp++ - 0xdc00 + 0x10000;
245                 }
246                 if (wc <= 0x7f) {
247                         *p++ = (char)wc;
248                 } else if (wc <= 0x7ff) {
249                         p[0] = 0xc0 | ((wc >> 6) & 0x1f);
250                         p[1] = 0x80 | (wc & 0x3f);
251                         p += 2;
252                 } else if (wc <= 0xffff) {
253                         p[0] = 0xe0 | ((wc >> 12) & 0x0f);
254                         p[1] = 0x80 | ((wc >> 6) & 0x3f);
255                         p[2] = 0x80 | (wc & 0x3f);
256                         p += 3;
257                 } else if (wc <= 0x1fffff) {
258                         p[0] = 0xf0 | ((wc >> 18) & 0x07);
259                         p[1] = 0x80 | ((wc >> 12) & 0x3f);
260                         p[2] = 0x80 | ((wc >> 6) & 0x3f);
261                         p[3] = 0x80 | (wc & 0x3f);
262                         p += 4;
263                 } else if (wc <= 0x3ffffff) {
264                         p[0] = 0xf8 | ((wc >> 24) & 0x03);
265                         p[1] = 0x80 | ((wc >> 18) & 0x3f);
266                         p[2] = 0x80 | ((wc >> 12) & 0x3f);
267                         p[3] = 0x80 | ((wc >> 6) & 0x3f);
268                         p[4] = 0x80 | (wc & 0x3f);
269                         p += 5;
270                 } else if (wc <= 0x7fffffff) {
271                         p[0] = 0xfc | ((wc >> 30) & 0x01);
272                         p[1] = 0x80 | ((wc >> 24) & 0x3f);
273                         p[1] = 0x80 | ((wc >> 18) & 0x3f);
274                         p[2] = 0x80 | ((wc >> 12) & 0x3f);
275                         p[3] = 0x80 | ((wc >> 6) & 0x3f);
276                         p[4] = 0x80 | (wc & 0x3f);
277                         p += 6;
278                 }
279                 /* Ignore larger values; UTF-8 can't encode them. */
280         }
281         *p = '\0';
282
283         return (utf8_value);
284 }
285
286 static void
287 add_pax_attr_w(struct archive_string *as, const char *key, const wchar_t *wval)
288 {
289         char *utf8_value = utf8_encode(wval);
290         if (utf8_value == NULL)
291                 return;
292         add_pax_attr(as, key, utf8_value);
293         free(utf8_value);
294 }
295
296 /*
297  * Add a key/value attribute to the pax header.  This function handles
298  * the length field and various other syntactic requirements.
299  */
300 static void
301 add_pax_attr(struct archive_string *as, const char *key, const char *value)
302 {
303         int digits, i, len, next_ten;
304         char tmp[1 + 3 * sizeof(int)];  /* < 3 base-10 digits per byte */
305
306         /*-
307          * PAX attributes have the following layout:
308          *     <len> <space> <key> <=> <value> <nl>
309          */
310         len = 1 + strlen(key) + 1 + strlen(value) + 1;
311
312         /*
313          * The <len> field includes the length of the <len> field, so
314          * computing the correct length is tricky.  I start by
315          * counting the number of base-10 digits in 'len' and
316          * computing the next higher power of 10.
317          */
318         next_ten = 1;
319         digits = 0;
320         i = len;
321         while (i > 0) {
322                 i = i / 10;
323                 digits++;
324                 next_ten = next_ten * 10;
325         }
326         /*
327          * For example, if string without the length field is 99
328          * chars, then adding the 2 digit length "99" will force the
329          * total length past 100, requiring an extra digit.  The next
330          * statement adjusts for this effect.
331          */
332         if (len + digits >= next_ten)
333                 digits++;
334
335         /* Now, we have the right length so we can build the line. */
336         tmp[sizeof(tmp) - 1] = 0;       /* Null-terminate the work area. */
337         archive_strcat(as, format_int(tmp + sizeof(tmp) - 1, len + digits));
338         archive_strappend_char(as, ' ');
339         archive_strcat(as, key);
340         archive_strappend_char(as, '=');
341         archive_strcat(as, value);
342         archive_strappend_char(as, '\n');
343 }
344
345 static void
346 archive_write_pax_header_xattrs(struct pax *pax, struct archive_entry *entry)
347 {
348         struct archive_string s;
349         int i = archive_entry_xattr_reset(entry);
350
351         while (i--) {
352                 const char *name;
353                 const void *value;
354                 char *encoded_value;
355                 char *url_encoded_name = NULL, *encoded_name = NULL;
356                 wchar_t *wcs_name = NULL;
357                 size_t size;
358
359                 archive_entry_xattr_next(entry, &name, &value, &size);
360                 /* Name is URL-encoded, then converted to wchar_t,
361                  * then UTF-8 encoded. */
362                 url_encoded_name = url_encode(name);
363                 if (url_encoded_name != NULL) {
364                         /* Convert narrow-character to wide-character. */
365                         int wcs_length = strlen(url_encoded_name);
366                         wcs_name = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
367                         if (wcs_name == NULL)
368                                 __archive_errx(1, "No memory for xattr conversion");
369                         mbstowcs(wcs_name, url_encoded_name, wcs_length);
370                         wcs_name[wcs_length] = 0;
371                         free(url_encoded_name); /* Done with this. */
372                 }
373                 if (wcs_name != NULL) {
374                         encoded_name = utf8_encode(wcs_name);
375                         free(wcs_name); /* Done with wchar_t name. */
376                 }
377
378                 encoded_value = base64_encode((const char *)value, size);
379
380                 if (encoded_name != NULL && encoded_value != NULL) {
381                         archive_string_init(&s);
382                         archive_strcpy(&s, "LIBARCHIVE.xattr.");
383                         archive_strcat(&s, encoded_name);
384                         add_pax_attr(&(pax->pax_header), s.s, encoded_value);
385                         archive_string_free(&s);
386                 }
387                 free(encoded_name);
388                 free(encoded_value);
389         }
390 }
391
392 /*
393  * TODO: Consider adding 'comment' and 'charset' fields to
394  * archive_entry so that clients can specify them.  Also, consider
395  * adding generic key/value tags so clients can add arbitrary
396  * key/value data.
397  */
398 static int
399 archive_write_pax_header(struct archive_write *a,
400     struct archive_entry *entry_original)
401 {
402         struct archive_entry *entry_main;
403         const char *p;
404         char *t;
405         const wchar_t *wp;
406         const char *suffix;
407         int need_extension, r, ret;
408         struct pax *pax;
409         const char *hdrcharset = NULL;
410         const char *hardlink;
411         const char *path = NULL, *linkpath = NULL;
412         const char *uname = NULL, *gname = NULL;
413         const wchar_t *path_w = NULL, *linkpath_w = NULL;
414         const wchar_t *uname_w = NULL, *gname_w = NULL;
415
416         char paxbuff[512];
417         char ustarbuff[512];
418         char ustar_entry_name[256];
419         char pax_entry_name[256];
420
421         ret = ARCHIVE_OK;
422         need_extension = 0;
423         pax = (struct pax *)a->format_data;
424
425         hardlink = archive_entry_hardlink(entry_original);
426
427         /* Make sure this is a type of entry that we can handle here */
428         if (hardlink == NULL) {
429                 switch (archive_entry_filetype(entry_original)) {
430                 case AE_IFBLK:
431                 case AE_IFCHR:
432                 case AE_IFIFO:
433                 case AE_IFLNK:
434                 case AE_IFREG:
435                         break;
436                 case AE_IFDIR:
437                         /*
438                          * Ensure a trailing '/'.  Modify the original
439                          * entry so the client sees the change.
440                          */
441                         p = archive_entry_pathname(entry_original);
442                         if (p[strlen(p) - 1] != '/') {
443                                 t = (char *)malloc(strlen(p) + 2);
444                                 if (t == NULL) {
445                                         archive_set_error(&a->archive, ENOMEM,
446                                         "Can't allocate pax data");
447                                         return(ARCHIVE_FATAL);
448                                 }
449                                 strcpy(t, p);
450                                 strcat(t, "/");
451                                 archive_entry_copy_pathname(entry_original, t);
452                                 free(t);
453                         }
454                         break;
455                 case AE_IFSOCK:
456                         archive_set_error(&a->archive,
457                             ARCHIVE_ERRNO_FILE_FORMAT,
458                             "tar format cannot archive socket");
459                         return (ARCHIVE_WARN);
460                 default:
461                         archive_set_error(&a->archive,
462                             ARCHIVE_ERRNO_FILE_FORMAT,
463                             "tar format cannot archive this (type=0%lo)",
464                             (unsigned long)archive_entry_filetype(entry_original));
465                         return (ARCHIVE_WARN);
466                 }
467         }
468
469         /* Copy entry so we can modify it as needed. */
470         entry_main = archive_entry_clone(entry_original);
471         archive_string_empty(&(pax->pax_header)); /* Blank our work area. */
472
473         /*
474          * First, check the name fields and see if any of them
475          * require binary coding.  If any of them does, then all of
476          * them do.
477          */
478         hdrcharset = NULL;
479         path = archive_entry_pathname(entry_main);
480         path_w = archive_entry_pathname_w(entry_main);
481         if (path != NULL && path_w == NULL) {
482                 archive_set_error(&a->archive, EILSEQ,
483                     "Can't translate pathname '%s' to UTF-8", path);
484                 ret = ARCHIVE_WARN;
485                 hdrcharset = "BINARY";
486         }
487         uname = archive_entry_uname(entry_main);
488         uname_w = archive_entry_uname_w(entry_main);
489         if (uname != NULL && uname_w == NULL) {
490                 archive_set_error(&a->archive, EILSEQ,
491                     "Can't translate uname '%s' to UTF-8", uname);
492                 ret = ARCHIVE_WARN;
493                 hdrcharset = "BINARY";
494         }
495         gname = archive_entry_gname(entry_main);
496         gname_w = archive_entry_gname_w(entry_main);
497         if (gname != NULL && gname_w == NULL) {
498                 archive_set_error(&a->archive, EILSEQ,
499                     "Can't translate gname '%s' to UTF-8", gname);
500                 ret = ARCHIVE_WARN;
501                 hdrcharset = "BINARY";
502         }
503         linkpath = hardlink;
504         if (linkpath != NULL) {
505                 linkpath_w = archive_entry_hardlink_w(entry_main);
506         } else {
507                 linkpath = archive_entry_symlink(entry_main);
508                 if (linkpath != NULL)
509                         linkpath_w = archive_entry_symlink_w(entry_main);
510         }
511         if (linkpath != NULL && linkpath_w == NULL) {
512                 archive_set_error(&a->archive, EILSEQ,
513                     "Can't translate linkpath '%s' to UTF-8", linkpath);
514                 ret = ARCHIVE_WARN;
515                 hdrcharset = "BINARY";
516         }
517
518         /* Store the header encoding first, to be nice to readers. */
519         if (hdrcharset != NULL)
520                 add_pax_attr(&(pax->pax_header), "hdrcharset", hdrcharset);
521
522
523         /*
524          * If name is too long, or has non-ASCII characters, add
525          * 'path' to pax extended attrs.  (Note that an unconvertible
526          * name must have non-ASCII characters.)
527          */
528         if (path == NULL) {
529                 /* We don't have a narrow version, so we have to store
530                  * the wide version. */
531                 add_pax_attr_w(&(pax->pax_header), "path", path_w);
532                 archive_entry_set_pathname(entry_main, "@WidePath");
533                 need_extension = 1;
534         } else if (has_non_ASCII(path_w)) {
535                 /* We have non-ASCII characters. */
536                 if (path_w == NULL || hdrcharset != NULL) {
537                         /* Can't do UTF-8, so store it raw. */
538                         add_pax_attr(&(pax->pax_header), "path", path);
539                 } else {
540                         /* Store UTF-8 */
541                         add_pax_attr_w(&(pax->pax_header),
542                             "path", path_w);
543                 }
544                 archive_entry_set_pathname(entry_main,
545                     build_ustar_entry_name(ustar_entry_name,
546                         path, strlen(path), NULL));
547                 need_extension = 1;
548         } else {
549                 /* We have an all-ASCII path; we'd like to just store
550                  * it in the ustar header if it will fit.  Yes, this
551                  * duplicates some of the logic in
552                  * write_set_format_ustar.c
553                  */
554                 if (strlen(path) <= 100) {
555                         /* Fits in the old 100-char tar name field. */
556                 } else {
557                         /* Find largest suffix that will fit. */
558                         /* Note: strlen() > 100, so strlen() - 100 - 1 >= 0 */
559                         suffix = strchr(path + strlen(path) - 100 - 1, '/');
560                         /* Don't attempt an empty prefix. */
561                         if (suffix == path)
562                                 suffix = strchr(suffix + 1, '/');
563                         /* We can put it in the ustar header if it's
564                          * all ASCII and it's either <= 100 characters
565                          * or can be split at a '/' into a prefix <=
566                          * 155 chars and a suffix <= 100 chars.  (Note
567                          * the strchr() above will return NULL exactly
568                          * when the path can't be split.)
569                          */
570                         if (suffix == NULL       /* Suffix > 100 chars. */
571                             || suffix[1] == '\0'    /* empty suffix */
572                             || suffix - path > 155)  /* Prefix > 155 chars */
573                         {
574                                 if (path_w == NULL || hdrcharset != NULL) {
575                                         /* Can't do UTF-8, so store it raw. */
576                                         add_pax_attr(&(pax->pax_header),
577                                             "path", path);
578                                 } else {
579                                         /* Store UTF-8 */
580                                         add_pax_attr_w(&(pax->pax_header),
581                                             "path", path_w);
582                                 }
583                                 archive_entry_set_pathname(entry_main,
584                                     build_ustar_entry_name(ustar_entry_name,
585                                         path, strlen(path), NULL));
586                                 need_extension = 1;
587                         }
588                 }
589         }
590
591         if (linkpath != NULL) {
592                 /* If link name is too long or has non-ASCII characters, add
593                  * 'linkpath' to pax extended attrs. */
594                 if (strlen(linkpath) > 100 || linkpath_w == NULL
595                     || linkpath_w == NULL || has_non_ASCII(linkpath_w)) {
596                         if (linkpath_w == NULL || hdrcharset != NULL)
597                                 /* If the linkpath is not convertible
598                                  * to wide, or we're encoding in
599                                  * binary anyway, store it raw. */
600                                 add_pax_attr(&(pax->pax_header),
601                                     "linkpath", linkpath);
602                         else
603                                 /* If the link is long or has a
604                                  * non-ASCII character, store it as a
605                                  * pax extended attribute. */
606                                 add_pax_attr_w(&(pax->pax_header),
607                                     "linkpath", linkpath_w);
608                         if (strlen(linkpath) > 100) {
609                                 if (hardlink != NULL)
610                                         archive_entry_set_hardlink(entry_main,
611                                             "././@LongHardLink");
612                                 else
613                                         archive_entry_set_symlink(entry_main,
614                                             "././@LongSymLink");
615                         }
616                         need_extension = 1;
617                 }
618         }
619
620         /* If file size is too large, add 'size' to pax extended attrs. */
621         if (archive_entry_size(entry_main) >= (((int64_t)1) << 33)) {
622                 add_pax_attr_int(&(pax->pax_header), "size",
623                     archive_entry_size(entry_main));
624                 need_extension = 1;
625         }
626
627         /* If numeric GID is too large, add 'gid' to pax extended attrs. */
628         if (archive_entry_gid(entry_main) >= (1 << 18)) {
629                 add_pax_attr_int(&(pax->pax_header), "gid",
630                     archive_entry_gid(entry_main));
631                 need_extension = 1;
632         }
633
634         /* If group name is too large or has non-ASCII characters, add
635          * 'gname' to pax extended attrs. */
636         if (gname != NULL) {
637                 if (strlen(gname) > 31
638                     || gname_w == NULL
639                     || has_non_ASCII(gname_w))
640                 {
641                         if (gname_w == NULL || hdrcharset != NULL) {
642                                 add_pax_attr(&(pax->pax_header),
643                                     "gname", gname);
644                         } else  {
645                                 add_pax_attr_w(&(pax->pax_header),
646                                     "gname", gname_w);
647                         }
648                         need_extension = 1;
649                 }
650         }
651
652         /* If numeric UID is too large, add 'uid' to pax extended attrs. */
653         if (archive_entry_uid(entry_main) >= (1 << 18)) {
654                 add_pax_attr_int(&(pax->pax_header), "uid",
655                     archive_entry_uid(entry_main));
656                 need_extension = 1;
657         }
658
659         /* Add 'uname' to pax extended attrs if necessary. */
660         if (uname != NULL) {
661                 if (strlen(uname) > 31
662                     || uname_w == NULL
663                     || has_non_ASCII(uname_w))
664                 {
665                         if (uname_w == NULL || hdrcharset != NULL) {
666                                 add_pax_attr(&(pax->pax_header),
667                                     "uname", uname);
668                         } else {
669                                 add_pax_attr_w(&(pax->pax_header),
670                                     "uname", uname_w);
671                         }
672                         need_extension = 1;
673                 }
674         }
675
676         /*
677          * POSIX/SUSv3 doesn't provide a standard key for large device
678          * numbers.  I use the same keys here that Joerg Schilling
679          * used for 'star.'  (Which, somewhat confusingly, are called
680          * "devXXX" even though they code "rdev" values.)  No doubt,
681          * other implementations use other keys.  Note that there's no
682          * reason we can't write the same information into a number of
683          * different keys.
684          *
685          * Of course, this is only needed for block or char device entries.
686          */
687         if (archive_entry_filetype(entry_main) == AE_IFBLK
688             || archive_entry_filetype(entry_main) == AE_IFCHR) {
689                 /*
690                  * If rdevmajor is too large, add 'SCHILY.devmajor' to
691                  * extended attributes.
692                  */
693                 dev_t rdevmajor, rdevminor;
694                 rdevmajor = archive_entry_rdevmajor(entry_main);
695                 rdevminor = archive_entry_rdevminor(entry_main);
696                 if (rdevmajor >= (1 << 18)) {
697                         add_pax_attr_int(&(pax->pax_header), "SCHILY.devmajor",
698                             rdevmajor);
699                         /*
700                          * Non-strict formatting below means we don't
701                          * have to truncate here.  Not truncating improves
702                          * the chance that some more modern tar archivers
703                          * (such as GNU tar 1.13) can restore the full
704                          * value even if they don't understand the pax
705                          * extended attributes.  See my rant below about
706                          * file size fields for additional details.
707                          */
708                         /* archive_entry_set_rdevmajor(entry_main,
709                            rdevmajor & ((1 << 18) - 1)); */
710                         need_extension = 1;
711                 }
712
713                 /*
714                  * If devminor is too large, add 'SCHILY.devminor' to
715                  * extended attributes.
716                  */
717                 if (rdevminor >= (1 << 18)) {
718                         add_pax_attr_int(&(pax->pax_header), "SCHILY.devminor",
719                             rdevminor);
720                         /* Truncation is not necessary here, either. */
721                         /* archive_entry_set_rdevminor(entry_main,
722                            rdevminor & ((1 << 18) - 1)); */
723                         need_extension = 1;
724                 }
725         }
726
727         /*
728          * Technically, the mtime field in the ustar header can
729          * support 33 bits, but many platforms use signed 32-bit time
730          * values.  The cutoff of 0x7fffffff here is a compromise.
731          * Yes, this check is duplicated just below; this helps to
732          * avoid writing an mtime attribute just to handle a
733          * high-resolution timestamp in "restricted pax" mode.
734          */
735         if (!need_extension &&
736             ((archive_entry_mtime(entry_main) < 0)
737                 || (archive_entry_mtime(entry_main) >= 0x7fffffff)))
738                 need_extension = 1;
739
740         /* I use a star-compatible file flag attribute. */
741         p = archive_entry_fflags_text(entry_main);
742         if (!need_extension && p != NULL  &&  *p != '\0')
743                 need_extension = 1;
744
745         /* If there are non-trivial ACL entries, we need an extension. */
746         if (!need_extension && archive_entry_acl_count(entry_original,
747                 ARCHIVE_ENTRY_ACL_TYPE_ACCESS) > 0)
748                 need_extension = 1;
749
750         /* If there are non-trivial ACL entries, we need an extension. */
751         if (!need_extension && archive_entry_acl_count(entry_original,
752                 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) > 0)
753                 need_extension = 1;
754
755         /* If there are extended attributes, we need an extension */
756         if (!need_extension && archive_entry_xattr_count(entry_original) > 0)
757                 need_extension = 1;
758
759         /*
760          * The following items are handled differently in "pax
761          * restricted" format.  In particular, in "pax restricted"
762          * format they won't be added unless need_extension is
763          * already set (we're already generating an extended header, so
764          * may as well include these).
765          */
766         if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_RESTRICTED ||
767             need_extension) {
768
769                 if (archive_entry_mtime(entry_main) < 0  ||
770                     archive_entry_mtime(entry_main) >= 0x7fffffff  ||
771                     archive_entry_mtime_nsec(entry_main) != 0)
772                         add_pax_attr_time(&(pax->pax_header), "mtime",
773                             archive_entry_mtime(entry_main),
774                             archive_entry_mtime_nsec(entry_main));
775
776                 if (archive_entry_ctime(entry_main) != 0  ||
777                     archive_entry_ctime_nsec(entry_main) != 0)
778                         add_pax_attr_time(&(pax->pax_header), "ctime",
779                             archive_entry_ctime(entry_main),
780                             archive_entry_ctime_nsec(entry_main));
781
782                 if (archive_entry_atime(entry_main) != 0 ||
783                     archive_entry_atime_nsec(entry_main) != 0)
784                         add_pax_attr_time(&(pax->pax_header), "atime",
785                             archive_entry_atime(entry_main),
786                             archive_entry_atime_nsec(entry_main));
787
788                 /* Store birth/creationtime only if it's earlier than mtime */
789                 if (archive_entry_birthtime_is_set(entry_main) &&
790                     archive_entry_birthtime(entry_main)
791                     < archive_entry_mtime(entry_main))
792                         add_pax_attr_time(&(pax->pax_header),
793                             "LIBARCHIVE.creationtime",
794                             archive_entry_birthtime(entry_main),
795                             archive_entry_birthtime_nsec(entry_main));
796
797                 /* I use a star-compatible file flag attribute. */
798                 p = archive_entry_fflags_text(entry_main);
799                 if (p != NULL  &&  *p != '\0')
800                         add_pax_attr(&(pax->pax_header), "SCHILY.fflags", p);
801
802                 /* I use star-compatible ACL attributes. */
803                 wp = archive_entry_acl_text_w(entry_original,
804                     ARCHIVE_ENTRY_ACL_TYPE_ACCESS |
805                     ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID);
806                 if (wp != NULL && *wp != L'\0')
807                         add_pax_attr_w(&(pax->pax_header),
808                             "SCHILY.acl.access", wp);
809                 wp = archive_entry_acl_text_w(entry_original,
810                     ARCHIVE_ENTRY_ACL_TYPE_DEFAULT |
811                     ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID);
812                 if (wp != NULL && *wp != L'\0')
813                         add_pax_attr_w(&(pax->pax_header),
814                             "SCHILY.acl.default", wp);
815
816                 /* Include star-compatible metadata info. */
817                 /* Note: "SCHILY.dev{major,minor}" are NOT the
818                  * major/minor portions of "SCHILY.dev". */
819                 add_pax_attr_int(&(pax->pax_header), "SCHILY.dev",
820                     archive_entry_dev(entry_main));
821                 add_pax_attr_int(&(pax->pax_header), "SCHILY.ino",
822                     archive_entry_ino(entry_main));
823                 add_pax_attr_int(&(pax->pax_header), "SCHILY.nlink",
824                     archive_entry_nlink(entry_main));
825
826                 /* Store extended attributes */
827                 archive_write_pax_header_xattrs(pax, entry_original);
828         }
829
830         /* Only regular files have data. */
831         if (archive_entry_filetype(entry_main) != AE_IFREG)
832                 archive_entry_set_size(entry_main, 0);
833
834         /*
835          * Pax-restricted does not store data for hardlinks, in order
836          * to improve compatibility with ustar.
837          */
838         if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE &&
839             hardlink != NULL)
840                 archive_entry_set_size(entry_main, 0);
841
842         /*
843          * XXX Full pax interchange format does permit a hardlink
844          * entry to have data associated with it.  I'm not supporting
845          * that here because the client expects me to tell them whether
846          * or not this format expects data for hardlinks.  If I
847          * don't check here, then every pax archive will end up with
848          * duplicated data for hardlinks.  Someday, there may be
849          * need to select this behavior, in which case the following
850          * will need to be revisited. XXX
851          */
852         if (hardlink != NULL)
853                 archive_entry_set_size(entry_main, 0);
854
855         /* Format 'ustar' header for main entry.
856          *
857          * The trouble with file size: If the reader can't understand
858          * the file size, they may not be able to locate the next
859          * entry and the rest of the archive is toast.  Pax-compliant
860          * readers are supposed to ignore the file size in the main
861          * header, so the question becomes how to maximize portability
862          * for readers that don't support pax attribute extensions.
863          * For maximum compatibility, I permit numeric extensions in
864          * the main header so that the file size stored will always be
865          * correct, even if it's in a format that only some
866          * implementations understand.  The technique used here is:
867          *
868          *  a) If possible, follow the standard exactly.  This handles
869          *  files up to 8 gigabytes minus 1.
870          *
871          *  b) If that fails, try octal but omit the field terminator.
872          *  That handles files up to 64 gigabytes minus 1.
873          *
874          *  c) Otherwise, use base-256 extensions.  That handles files
875          *  up to 2^63 in this implementation, with the potential to
876          *  go up to 2^94.  That should hold us for a while. ;-)
877          *
878          * The non-strict formatter uses similar logic for other
879          * numeric fields, though they're less critical.
880          */
881         __archive_write_format_header_ustar(a, ustarbuff, entry_main, -1, 0);
882
883         /* If we built any extended attributes, write that entry first. */
884         if (archive_strlen(&(pax->pax_header)) > 0) {
885                 struct archive_entry *pax_attr_entry;
886                 time_t s;
887                 uid_t uid;
888                 gid_t gid;
889                 mode_t mode;
890                 long ns;
891
892                 pax_attr_entry = archive_entry_new();
893                 p = archive_entry_pathname(entry_main);
894                 archive_entry_set_pathname(pax_attr_entry,
895                     build_pax_attribute_name(pax_entry_name, p));
896                 archive_entry_set_size(pax_attr_entry,
897                     archive_strlen(&(pax->pax_header)));
898                 /* Copy uid/gid (but clip to ustar limits). */
899                 uid = archive_entry_uid(entry_main);
900                 if (uid >= 1 << 18)
901                         uid = (1 << 18) - 1;
902                 archive_entry_set_uid(pax_attr_entry, uid);
903                 gid = archive_entry_gid(entry_main);
904                 if (gid >= 1 << 18)
905                         gid = (1 << 18) - 1;
906                 archive_entry_set_gid(pax_attr_entry, gid);
907                 /* Copy mode over (but not setuid/setgid bits) */
908                 mode = archive_entry_mode(entry_main);
909 #ifdef S_ISUID
910                 mode &= ~S_ISUID;
911 #endif
912 #ifdef S_ISGID
913                 mode &= ~S_ISGID;
914 #endif
915 #ifdef S_ISVTX
916                 mode &= ~S_ISVTX;
917 #endif
918                 archive_entry_set_mode(pax_attr_entry, mode);
919
920                 /* Copy uname/gname. */
921                 archive_entry_set_uname(pax_attr_entry,
922                     archive_entry_uname(entry_main));
923                 archive_entry_set_gname(pax_attr_entry,
924                     archive_entry_gname(entry_main));
925
926                 /* Copy mtime, but clip to ustar limits. */
927                 s = archive_entry_mtime(entry_main);
928                 ns = archive_entry_mtime_nsec(entry_main);
929                 if (s < 0) { s = 0; ns = 0; }
930                 if (s > 0x7fffffff) { s = 0x7fffffff; ns = 0; }
931                 archive_entry_set_mtime(pax_attr_entry, s, ns);
932
933                 /* Ditto for atime. */
934                 s = archive_entry_atime(entry_main);
935                 ns = archive_entry_atime_nsec(entry_main);
936                 if (s < 0) { s = 0; ns = 0; }
937                 if (s > 0x7fffffff) { s = 0x7fffffff; ns = 0; }
938                 archive_entry_set_atime(pax_attr_entry, s, ns);
939
940                 /* Standard ustar doesn't support ctime. */
941                 archive_entry_set_ctime(pax_attr_entry, 0, 0);
942
943                 r = __archive_write_format_header_ustar(a, paxbuff,
944                     pax_attr_entry, 'x', 1);
945
946                 archive_entry_free(pax_attr_entry);
947
948                 /* Note that the 'x' header shouldn't ever fail to format */
949                 if (r != 0) {
950                         const char *msg = "archive_write_pax_header: "
951                             "'x' header failed?!  This can't happen.\n";
952                         write(2, msg, strlen(msg));
953                         exit(1);
954                 }
955                 r = (a->compressor.write)(a, paxbuff, 512);
956                 if (r != ARCHIVE_OK) {
957                         pax->entry_bytes_remaining = 0;
958                         pax->entry_padding = 0;
959                         return (ARCHIVE_FATAL);
960                 }
961
962                 pax->entry_bytes_remaining = archive_strlen(&(pax->pax_header));
963                 pax->entry_padding = 0x1ff & (-(int64_t)pax->entry_bytes_remaining);
964
965                 r = (a->compressor.write)(a, pax->pax_header.s,
966                     archive_strlen(&(pax->pax_header)));
967                 if (r != ARCHIVE_OK) {
968                         /* If a write fails, we're pretty much toast. */
969                         return (ARCHIVE_FATAL);
970                 }
971                 /* Pad out the end of the entry. */
972                 r = write_nulls(a, pax->entry_padding);
973                 if (r != ARCHIVE_OK) {
974                         /* If a write fails, we're pretty much toast. */
975                         return (ARCHIVE_FATAL);
976                 }
977                 pax->entry_bytes_remaining = pax->entry_padding = 0;
978         }
979
980         /* Write the header for main entry. */
981         r = (a->compressor.write)(a, ustarbuff, 512);
982         if (r != ARCHIVE_OK)
983                 return (r);
984
985         /*
986          * Inform the client of the on-disk size we're using, so
987          * they can avoid unnecessarily writing a body for something
988          * that we're just going to ignore.
989          */
990         archive_entry_set_size(entry_original, archive_entry_size(entry_main));
991         pax->entry_bytes_remaining = archive_entry_size(entry_main);
992         pax->entry_padding = 0x1ff & (-(int64_t)pax->entry_bytes_remaining);
993         archive_entry_free(entry_main);
994
995         return (ret);
996 }
997
998 /*
999  * We need a valid name for the regular 'ustar' entry.  This routine
1000  * tries to hack something more-or-less reasonable.
1001  *
1002  * The approach here tries to preserve leading dir names.  We do so by
1003  * working with four sections:
1004  *   1) "prefix" directory names,
1005  *   2) "suffix" directory names,
1006  *   3) inserted dir name (optional),
1007  *   4) filename.
1008  *
1009  * These sections must satisfy the following requirements:
1010  *   * Parts 1 & 2 together form an initial portion of the dir name.
1011  *   * Part 3 is specified by the caller.  (It should not contain a leading
1012  *     or trailing '/'.)
1013  *   * Part 4 forms an initial portion of the base filename.
1014  *   * The filename must be <= 99 chars to fit the ustar 'name' field.
1015  *   * Parts 2, 3, 4 together must be <= 99 chars to fit the ustar 'name' fld.
1016  *   * Part 1 must be <= 155 chars to fit the ustar 'prefix' field.
1017  *   * If the original name ends in a '/', the new name must also end in a '/'
1018  *   * Trailing '/.' sequences may be stripped.
1019  *
1020  * Note: Recall that the ustar format does not store the '/' separating
1021  * parts 1 & 2, but does store the '/' separating parts 2 & 3.
1022  */
1023 static char *
1024 build_ustar_entry_name(char *dest, const char *src, size_t src_length,
1025     const char *insert)
1026 {
1027         const char *prefix, *prefix_end;
1028         const char *suffix, *suffix_end;
1029         const char *filename, *filename_end;
1030         char *p;
1031         int need_slash = 0; /* Was there a trailing slash? */
1032         size_t suffix_length = 99;
1033         int insert_length;
1034
1035         /* Length of additional dir element to be added. */
1036         if (insert == NULL)
1037                 insert_length = 0;
1038         else
1039                 /* +2 here allows for '/' before and after the insert. */
1040                 insert_length = strlen(insert) + 2;
1041
1042         /* Step 0: Quick bailout in a common case. */
1043         if (src_length < 100 && insert == NULL) {
1044                 strncpy(dest, src, src_length);
1045                 dest[src_length] = '\0';
1046                 return (dest);
1047         }
1048
1049         /* Step 1: Locate filename and enforce the length restriction. */
1050         filename_end = src + src_length;
1051         /* Remove trailing '/' chars and '/.' pairs. */
1052         for (;;) {
1053                 if (filename_end > src && filename_end[-1] == '/') {
1054                         filename_end --;
1055                         need_slash = 1; /* Remember to restore trailing '/'. */
1056                         continue;
1057                 }
1058                 if (filename_end > src + 1 && filename_end[-1] == '.'
1059                     && filename_end[-2] == '/') {
1060                         filename_end -= 2;
1061                         need_slash = 1; /* "foo/." will become "foo/" */
1062                         continue;
1063                 }
1064                 break;
1065         }
1066         if (need_slash)
1067                 suffix_length--;
1068         /* Find start of filename. */
1069         filename = filename_end - 1;
1070         while ((filename > src) && (*filename != '/'))
1071                 filename --;
1072         if ((*filename == '/') && (filename < filename_end - 1))
1073                 filename ++;
1074         /* Adjust filename_end so that filename + insert fits in 99 chars. */
1075         suffix_length -= insert_length;
1076         if (filename_end > filename + suffix_length)
1077                 filename_end = filename + suffix_length;
1078         /* Calculate max size for "suffix" section (#3 above). */
1079         suffix_length -= filename_end - filename;
1080
1081         /* Step 2: Locate the "prefix" section of the dirname, including
1082          * trailing '/'. */
1083         prefix = src;
1084         prefix_end = prefix + 155;
1085         if (prefix_end > filename)
1086                 prefix_end = filename;
1087         while (prefix_end > prefix && *prefix_end != '/')
1088                 prefix_end--;
1089         if ((prefix_end < filename) && (*prefix_end == '/'))
1090                 prefix_end++;
1091
1092         /* Step 3: Locate the "suffix" section of the dirname,
1093          * including trailing '/'. */
1094         suffix = prefix_end;
1095         suffix_end = suffix + suffix_length; /* Enforce limit. */
1096         if (suffix_end > filename)
1097                 suffix_end = filename;
1098         if (suffix_end < suffix)
1099                 suffix_end = suffix;
1100         while (suffix_end > suffix && *suffix_end != '/')
1101                 suffix_end--;
1102         if ((suffix_end < filename) && (*suffix_end == '/'))
1103                 suffix_end++;
1104
1105         /* Step 4: Build the new name. */
1106         /* The OpenBSD strlcpy function is safer, but less portable. */
1107         /* Rather than maintain two versions, just use the strncpy version. */
1108         p = dest;
1109         if (prefix_end > prefix) {
1110                 strncpy(p, prefix, prefix_end - prefix);
1111                 p += prefix_end - prefix;
1112         }
1113         if (suffix_end > suffix) {
1114                 strncpy(p, suffix, suffix_end - suffix);
1115                 p += suffix_end - suffix;
1116         }
1117         if (insert != NULL) {
1118                 /* Note: assume insert does not have leading or trailing '/' */
1119                 strcpy(p, insert);
1120                 p += strlen(insert);
1121                 *p++ = '/';
1122         }
1123         strncpy(p, filename, filename_end - filename);
1124         p += filename_end - filename;
1125         if (need_slash)
1126                 *p++ = '/';
1127         *p++ = '\0';
1128
1129         return (dest);
1130 }
1131
1132 /*
1133  * The ustar header for the pax extended attributes must have a
1134  * reasonable name:  SUSv3 requires 'dirname'/PaxHeader.'pid'/'filename'
1135  * where 'pid' is the PID of the archiving process.  Unfortunately,
1136  * that makes testing a pain since the output varies for each run,
1137  * so I'm sticking with the simpler 'dirname'/PaxHeader/'filename'
1138  * for now.  (Someday, I'll make this settable.  Then I can use the
1139  * SUS recommendation as default and test harnesses can override it
1140  * to get predictable results.)
1141  *
1142  * Joerg Schilling has argued that this is unnecessary because, in
1143  * practice, if the pax extended attributes get extracted as regular
1144  * files, noone is going to bother reading those attributes to
1145  * manually restore them.  Based on this, 'star' uses
1146  * /tmp/PaxHeader/'basename' as the ustar header name.  This is a
1147  * tempting argument, in part because it's simpler than the SUSv3
1148  * recommendation, but I'm not entirely convinced.  I'm also
1149  * uncomfortable with the fact that "/tmp" is a Unix-ism.
1150  *
1151  * The following routine leverages build_ustar_entry_name() above and
1152  * so is simpler than you might think.  It just needs to provide the
1153  * additional path element and handle a few pathological cases).
1154  */
1155 static char *
1156 build_pax_attribute_name(char *dest, const char *src)
1157 {
1158         char buff[64];
1159         const char *p;
1160
1161         /* Handle the null filename case. */
1162         if (src == NULL || *src == '\0') {
1163                 strcpy(dest, "PaxHeader/blank");
1164                 return (dest);
1165         }
1166
1167         /* Prune final '/' and other unwanted final elements. */
1168         p = src + strlen(src);
1169         for (;;) {
1170                 /* Ends in "/", remove the '/' */
1171                 if (p > src && p[-1] == '/') {
1172                         --p;
1173                         continue;
1174                 }
1175                 /* Ends in "/.", remove the '.' */
1176                 if (p > src + 1 && p[-1] == '.'
1177                     && p[-2] == '/') {
1178                         --p;
1179                         continue;
1180                 }
1181                 break;
1182         }
1183
1184         /* Pathological case: After above, there was nothing left.
1185          * This includes "/." "/./." "/.//./." etc. */
1186         if (p == src) {
1187                 strcpy(dest, "/PaxHeader/rootdir");
1188                 return (dest);
1189         }
1190
1191         /* Convert unadorned "." into a suitable filename. */
1192         if (*src == '.' && p == src + 1) {
1193                 strcpy(dest, "PaxHeader/currentdir");
1194                 return (dest);
1195         }
1196
1197         /*
1198          * TODO: Push this string into the 'pax' structure to avoid
1199          * recomputing it every time.  That will also open the door
1200          * to having clients override it.
1201          */
1202 #if HAVE_GETPID && 0  /* Disable this for now; see above comment. */
1203         sprintf(buff, "PaxHeader.%d", getpid());
1204 #else
1205         /* If the platform can't fetch the pid, don't include it. */
1206         strcpy(buff, "PaxHeader");
1207 #endif
1208         /* General case: build a ustar-compatible name adding "/PaxHeader/". */
1209         build_ustar_entry_name(dest, src, p - src, buff);
1210
1211         return (dest);
1212 }
1213
1214 /* Write two null blocks for the end of archive */
1215 static int
1216 archive_write_pax_finish(struct archive_write *a)
1217 {
1218         struct pax *pax;
1219         int r;
1220
1221         if (a->compressor.write == NULL)
1222                 return (ARCHIVE_OK);
1223
1224         pax = (struct pax *)a->format_data;
1225         r = write_nulls(a, 512 * 2);
1226         return (r);
1227 }
1228
1229 static int
1230 archive_write_pax_destroy(struct archive_write *a)
1231 {
1232         struct pax *pax;
1233
1234         pax = (struct pax *)a->format_data;
1235         if (pax == NULL)
1236                 return (ARCHIVE_OK);
1237
1238         archive_string_free(&pax->pax_header);
1239         free(pax);
1240         a->format_data = NULL;
1241         return (ARCHIVE_OK);
1242 }
1243
1244 static int
1245 archive_write_pax_finish_entry(struct archive_write *a)
1246 {
1247         struct pax *pax;
1248         int ret;
1249
1250         pax = (struct pax *)a->format_data;
1251         ret = write_nulls(a, pax->entry_bytes_remaining + pax->entry_padding);
1252         pax->entry_bytes_remaining = pax->entry_padding = 0;
1253         return (ret);
1254 }
1255
1256 static int
1257 write_nulls(struct archive_write *a, size_t padding)
1258 {
1259         int ret, to_write;
1260
1261         while (padding > 0) {
1262                 to_write = padding < a->null_length ? padding : a->null_length;
1263                 ret = (a->compressor.write)(a, a->nulls, to_write);
1264                 if (ret != ARCHIVE_OK)
1265                         return (ret);
1266                 padding -= to_write;
1267         }
1268         return (ARCHIVE_OK);
1269 }
1270
1271 static ssize_t
1272 archive_write_pax_data(struct archive_write *a, const void *buff, size_t s)
1273 {
1274         struct pax *pax;
1275         int ret;
1276
1277         pax = (struct pax *)a->format_data;
1278         if (s > pax->entry_bytes_remaining)
1279                 s = pax->entry_bytes_remaining;
1280
1281         ret = (a->compressor.write)(a, buff, s);
1282         pax->entry_bytes_remaining -= s;
1283         if (ret == ARCHIVE_OK)
1284                 return (s);
1285         else
1286                 return (ret);
1287 }
1288
1289 static int
1290 has_non_ASCII(const wchar_t *wp)
1291 {
1292         if (wp == NULL)
1293                 return (1);
1294         while (*wp != L'\0' && *wp < 128)
1295                 wp++;
1296         return (*wp != L'\0');
1297 }
1298
1299 /*
1300  * Used by extended attribute support; encodes the name
1301  * so that there will be no '=' characters in the result.
1302  */
1303 static char *
1304 url_encode(const char *in)
1305 {
1306         const char *s;
1307         char *d;
1308         int out_len = 0;
1309         char *out;
1310
1311         for (s = in; *s != '\0'; s++) {
1312                 if (*s < 33 || *s > 126 || *s == '%' || *s == '=')
1313                         out_len += 3;
1314                 else
1315                         out_len++;
1316         }
1317
1318         out = (char *)malloc(out_len + 1);
1319         if (out == NULL)
1320                 return (NULL);
1321
1322         for (s = in, d = out; *s != '\0'; s++) {
1323                 /* encode any non-printable ASCII character or '%' or '=' */
1324                 if (*s < 33 || *s > 126 || *s == '%' || *s == '=') {
1325                         /* URL encoding is '%' followed by two hex digits */
1326                         *d++ = '%';
1327                         *d++ = "0123456789ABCDEF"[0x0f & (*s >> 4)];
1328                         *d++ = "0123456789ABCDEF"[0x0f & *s];
1329                 } else {
1330                         *d++ = *s;
1331                 }
1332         }
1333         *d = '\0';
1334         return (out);
1335 }
1336
1337 /*
1338  * Encode a sequence of bytes into a C string using base-64 encoding.
1339  *
1340  * Returns a null-terminated C string allocated with malloc(); caller
1341  * is responsible for freeing the result.
1342  */
1343 static char *
1344 base64_encode(const char *s, size_t len)
1345 {
1346         static const char digits[64] =
1347             { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
1348               'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
1349               'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
1350               't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
1351               '8','9','+','/' };
1352         int v;
1353         char *d, *out;
1354
1355         /* 3 bytes becomes 4 chars, but round up and allow for trailing NUL */
1356         out = (char *)malloc((len * 4 + 2) / 3 + 1);
1357         if (out == NULL)
1358                 return (NULL);
1359         d = out;
1360
1361         /* Convert each group of 3 bytes into 4 characters. */
1362         while (len >= 3) {
1363                 v = (((int)s[0] << 16) & 0xff0000)
1364                     | (((int)s[1] << 8) & 0xff00)
1365                     | (((int)s[2]) & 0x00ff);
1366                 s += 3;
1367                 len -= 3;
1368                 *d++ = digits[(v >> 18) & 0x3f];
1369                 *d++ = digits[(v >> 12) & 0x3f];
1370                 *d++ = digits[(v >> 6) & 0x3f];
1371                 *d++ = digits[(v) & 0x3f];
1372         }
1373         /* Handle final group of 1 byte (2 chars) or 2 bytes (3 chars). */
1374         switch (len) {
1375         case 0: break;
1376         case 1:
1377                 v = (((int)s[0] << 16) & 0xff0000);
1378                 *d++ = digits[(v >> 18) & 0x3f];
1379                 *d++ = digits[(v >> 12) & 0x3f];
1380                 break;
1381         case 2:
1382                 v = (((int)s[0] << 16) & 0xff0000)
1383                     | (((int)s[1] << 8) & 0xff00);
1384                 *d++ = digits[(v >> 18) & 0x3f];
1385                 *d++ = digits[(v >> 12) & 0x3f];
1386                 *d++ = digits[(v >> 6) & 0x3f];
1387                 break;
1388         }
1389         /* Add trailing NUL character so output is a valid C string. */
1390         *d++ = '\0';
1391         return (out);
1392 }