Add files from parent branch HEAD:
[pkgsrc.git] / archivers / libarchive / files / libarchive / archive_entry.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: src/lib/libarchive/archive_entry.c,v 1.52 2008/05/26 17:00:22 kientzle Exp $");
28
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35 #ifdef MAJOR_IN_MKDEV
36 #include <sys/mkdev.h>
37 #else
38 #ifdef MAJOR_IN_SYSMACROS
39 #include <sys/sysmacros.h>
40 #endif
41 #endif
42 #ifdef HAVE_LIMITS_H
43 #include <limits.h>
44 #endif
45 #ifdef HAVE_LINUX_FS_H
46 #include <linux/fs.h>   /* for Linux file flags */
47 #endif
48 #ifdef HAVE_LINUX_EXT2_FS_H
49 #include <linux/ext2_fs.h>      /* for Linux file flags */
50 #endif
51 #ifdef HAVE_EXT2FS_EXT2_FS_H
52 /*
53  * This must be after the include of linux/ext2_fs.h,
54  * otherwise the former will fail.
55  */
56 #include <ext2fs/ext2_fs.h>     /* for Linux file flags */
57 #endif
58 #include <stddef.h>
59 #include <stdio.h>
60 #ifdef HAVE_STDLIB_H
61 #include <stdlib.h>
62 #endif
63 #ifdef HAVE_STRING_H
64 #include <string.h>
65 #endif
66 #ifdef HAVE_WCHAR_H
67 #include <wchar.h>
68 #endif
69
70 #include "archive.h"
71 #include "archive_entry.h"
72 #include "archive_private.h"
73 #include "archive_entry_private.h"
74
75 #undef max
76 #define max(a, b)       ((a)>(b)?(a):(b))
77
78 /* Play games to come up with a suitable makedev() definition. */
79 #ifdef __QNXNTO__
80 /* QNX.  <sigh> */
81 #include <sys/netmgr.h>
82 #define ae_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
83 #elif defined makedev
84 /* There's a "makedev" macro. */
85 #define ae_makedev(maj, min) makedev((maj), (min))
86 #elif defined mkdev || defined _WIN32 || defined __WIN32__
87 /* Windows. <sigh> */
88 #define ae_makedev(maj, min) mkdev((maj), (min))
89 #else
90 /* There's a "makedev" function. */
91 #define ae_makedev(maj, min) makedev((maj), (min))
92 #endif
93
94 static void     aes_clean(struct aes *);
95 static void     aes_copy(struct aes *dest, struct aes *src);
96 static const char *     aes_get_mbs(struct aes *);
97 static const wchar_t *  aes_get_wcs(struct aes *);
98 static int      aes_set_mbs(struct aes *, const char *mbs);
99 static int      aes_copy_mbs(struct aes *, const char *mbs);
100 /* static void  aes_set_wcs(struct aes *, const wchar_t *wcs); */
101 static int      aes_copy_wcs(struct aes *, const wchar_t *wcs);
102 static int      aes_copy_wcs_len(struct aes *, const wchar_t *wcs, size_t);
103
104 static char *    ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
105 static const wchar_t    *ae_wcstofflags(const wchar_t *stringp,
106                     unsigned long *setp, unsigned long *clrp);
107 static const char       *ae_strtofflags(const char *stringp,
108                     unsigned long *setp, unsigned long *clrp);
109 static void     append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
110                     const wchar_t *wname, int perm, int id);
111 static void     append_id_w(wchar_t **wp, int id);
112
113 static int      acl_special(struct archive_entry *entry,
114                     int type, int permset, int tag);
115 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
116                     int type, int permset, int tag, int id);
117 static int      isint_w(const wchar_t *start, const wchar_t *end, int *result);
118 static void     next_field_w(const wchar_t **wp, const wchar_t **start,
119                     const wchar_t **end, wchar_t *sep);
120 static int      prefix_w(const wchar_t *start, const wchar_t *end,
121                     const wchar_t *test);
122 static void
123 archive_entry_acl_add_entry_w_len(struct archive_entry *entry, int type,
124                     int permset, int tag, int id, const wchar_t *name, size_t);
125
126
127 #ifndef HAVE_WCSCPY
128 static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
129 {
130         wchar_t *dest = s1;
131         while ((*s1 = *s2) != L'\0')
132                 ++s1, ++s2;
133         return dest;
134 }
135 #endif
136 #ifndef HAVE_WCSLEN
137 static size_t wcslen(const wchar_t *s)
138 {
139         const wchar_t *p = s;
140         while (*p != L'\0')
141                 ++p;
142         return p - s;
143 }
144 #endif
145 #ifndef HAVE_WMEMCMP
146 /* Good enough for simple equality testing, but not for sorting. */
147 #define wmemcmp(a,b,i)  memcmp((a), (b), (i) * sizeof(wchar_t))
148 #endif
149 #ifndef HAVE_WMEMCPY
150 #define wmemcpy(a,b,i)  (wchar_t *)memcpy((a), (b), (i) * sizeof(wchar_t))
151 #endif
152
153 static void
154 aes_clean(struct aes *aes)
155 {
156         if (aes->aes_wcs) {
157                 free((wchar_t *)(uintptr_t)aes->aes_wcs);
158                 aes->aes_wcs = NULL;
159         }
160         archive_string_free(&(aes->aes_mbs));
161         archive_string_free(&(aes->aes_utf8));
162         aes->aes_set = 0;
163 }
164
165 static void
166 aes_copy(struct aes *dest, struct aes *src)
167 {
168         wchar_t *wp;
169
170         dest->aes_set = src->aes_set;
171         archive_string_copy(&(dest->aes_mbs), &(src->aes_mbs));
172         archive_string_copy(&(dest->aes_utf8), &(src->aes_utf8));
173
174         if (src->aes_wcs != NULL) {
175                 wp = (wchar_t *)malloc((wcslen(src->aes_wcs) + 1)
176                     * sizeof(wchar_t));
177                 if (wp == NULL)
178                         __archive_errx(1, "No memory for aes_copy()");
179                 wcscpy(wp, src->aes_wcs);
180                 dest->aes_wcs = wp;
181         }
182 }
183
184 static const char *
185 aes_get_utf8(struct aes *aes)
186 {
187         if (aes->aes_set & AES_SET_UTF8)
188                 return (aes->aes_utf8.s);
189         if ((aes->aes_set & AES_SET_WCS)
190             && archive_strappend_w_utf8(&(aes->aes_utf8), aes->aes_wcs) != NULL) {
191                 aes->aes_set |= AES_SET_UTF8;
192                 return (aes->aes_utf8.s);
193         }
194         return (NULL);
195 }
196
197 static const char *
198 aes_get_mbs(struct aes *aes)
199 {
200         /* If we already have an MBS form, return that immediately. */
201         if (aes->aes_set & AES_SET_MBS)
202                 return (aes->aes_mbs.s);
203         /* If there's a WCS form, try converting with the native locale. */
204         if ((aes->aes_set & AES_SET_WCS)
205             && archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) != NULL) {
206                 aes->aes_set |= AES_SET_MBS;
207                 return (aes->aes_mbs.s);
208         }
209         /* We'll use UTF-8 for MBS if all else fails. */
210         return (aes_get_utf8(aes));
211 }
212
213 static const wchar_t *
214 aes_get_wcs(struct aes *aes)
215 {
216         wchar_t *w;
217         int r;
218
219         /* Return WCS form if we already have it. */
220         if (aes->aes_set & AES_SET_WCS)
221                 return (aes->aes_wcs);
222
223         if (aes->aes_set & AES_SET_MBS) {
224                 /* Try converting MBS to WCS using native locale. */
225                 /*
226                  * No single byte will be more than one wide character,
227                  * so this length estimate will always be big enough.
228                  */
229                 size_t wcs_length = aes->aes_mbs.length;
230
231                 w = (wchar_t *)malloc((wcs_length + 1) * sizeof(wchar_t));
232                 if (w == NULL)
233                         __archive_errx(1, "No memory for aes_get_wcs()");
234                 r = mbstowcs(w, aes->aes_mbs.s, wcs_length);
235                 w[wcs_length] = 0;
236                 if (r > 0) {
237                         aes->aes_set |= AES_SET_WCS;
238                         return (aes->aes_wcs = w);
239                 }
240                 free(w);
241         }
242
243         if (aes->aes_set & AES_SET_UTF8) {
244                 /* Try converting UTF8 to WCS. */
245                 aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
246                 aes->aes_set |= AES_SET_WCS;
247                 return (aes->aes_wcs);
248         }
249         return (NULL);
250 }
251
252 static int
253 aes_set_mbs(struct aes *aes, const char *mbs)
254 {
255         return (aes_copy_mbs(aes, mbs));
256 }
257
258 static int
259 aes_copy_mbs(struct aes *aes, const char *mbs)
260 {
261         if (mbs == NULL) {
262                 aes->aes_set = 0;
263                 return (0);
264         }
265         aes->aes_set = AES_SET_MBS; /* Only MBS form is set now. */
266         archive_strcpy(&(aes->aes_mbs), mbs);
267         archive_string_empty(&(aes->aes_utf8));
268         if (aes->aes_wcs) {
269                 free((wchar_t *)(uintptr_t)aes->aes_wcs);
270                 aes->aes_wcs = NULL;
271         }
272         return (0);
273 }
274
275 /*
276  * The 'update' form tries to proactively update all forms of
277  * this string (WCS and MBS) and returns an error if any of
278  * them fail.  This is used by the 'pax' handler, for instance,
279  * to detect and report character-conversion failures early while
280  * still allowing clients to get potentially useful values from
281  * the more tolerant lazy conversions.  (get_mbs and get_wcs will
282  * strive to give the user something useful, so you can get hopefully
283  * usable values even if some of the character conversions are failing.)
284  */
285 static int
286 aes_update_utf8(struct aes *aes, const char *utf8)
287 {
288         if (utf8 == NULL) {
289                 aes->aes_set = 0;
290                 return (1); /* Succeeded in clearing everything. */
291         }
292
293         /* Save the UTF8 string. */
294         archive_strcpy(&(aes->aes_utf8), utf8);
295
296         /* Empty the mbs and wcs strings. */
297         archive_string_empty(&(aes->aes_mbs));
298         if (aes->aes_wcs) {
299                 free((wchar_t *)(uintptr_t)aes->aes_wcs);
300                 aes->aes_wcs = NULL;
301         }
302
303         aes->aes_set = AES_SET_UTF8;    /* Only UTF8 is set now. */
304
305         /* TODO: We should just do a direct UTF-8 to MBS conversion
306          * here.  That would be faster, use less space, and give the
307          * same information.  (If a UTF-8 to MBS conversion succeeds,
308          * then UTF-8->WCS and Unicode->MBS conversions will both
309          * succeed.) */
310
311         /* Try converting UTF8 to WCS, return false on failure. */
312         aes->aes_wcs = __archive_string_utf8_w(&(aes->aes_utf8));
313         if (aes->aes_wcs == NULL)
314                 return (0);
315         aes->aes_set = AES_SET_UTF8 | AES_SET_WCS; /* Both UTF8 and WCS set. */
316
317         /* Try converting WCS to MBS, return false on failure. */
318         if (archive_strappend_w_mbs(&(aes->aes_mbs), aes->aes_wcs) == NULL)
319                 return (0);
320         aes->aes_set = AES_SET_UTF8 | AES_SET_WCS | AES_SET_MBS;
321
322         /* All conversions succeeded. */
323         return (1);
324 }
325
326 static int
327 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
328 {
329         return aes_copy_wcs_len(aes, wcs, wcs == NULL ? 0 : wcslen(wcs));
330 }
331
332 static int
333 aes_copy_wcs_len(struct aes *aes, const wchar_t *wcs, size_t len)
334 {
335         wchar_t *w;
336
337         if (wcs == NULL) {
338                 aes->aes_set = 0;
339                 return (0);
340         }
341         aes->aes_set = AES_SET_WCS; /* Only WCS form set. */
342         archive_string_empty(&(aes->aes_mbs));
343         archive_string_empty(&(aes->aes_utf8));
344         if (aes->aes_wcs) {
345                 free((wchar_t *)(uintptr_t)aes->aes_wcs);
346                 aes->aes_wcs = NULL;
347         }
348         w = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
349         if (w == NULL)
350                 __archive_errx(1, "No memory for aes_copy_wcs()");
351         wmemcpy(w, wcs, len);
352         w[len] = L'\0';
353         aes->aes_wcs = w;
354         return (0);
355 }
356
357 /****************************************************************************
358  *
359  * Public Interface
360  *
361  ****************************************************************************/
362
363 struct archive_entry *
364 archive_entry_clear(struct archive_entry *entry)
365 {
366         if (entry == NULL)
367                 return (NULL);
368         aes_clean(&entry->ae_fflags_text);
369         aes_clean(&entry->ae_gname);
370         aes_clean(&entry->ae_hardlink);
371         aes_clean(&entry->ae_pathname);
372         aes_clean(&entry->ae_symlink);
373         aes_clean(&entry->ae_uname);
374         archive_entry_acl_clear(entry);
375         archive_entry_xattr_clear(entry);
376         free(entry->stat);
377         memset(entry, 0, sizeof(*entry));
378         return entry;
379 }
380
381 struct archive_entry *
382 archive_entry_clone(struct archive_entry *entry)
383 {
384         struct archive_entry *entry2;
385         struct ae_acl *ap, *ap2;
386         struct ae_xattr *xp;
387
388         /* Allocate new structure and copy over all of the fields. */
389         entry2 = (struct archive_entry *)malloc(sizeof(*entry2));
390         if (entry2 == NULL)
391                 return (NULL);
392         memset(entry2, 0, sizeof(*entry2));
393         entry2->ae_stat = entry->ae_stat;
394         entry2->ae_fflags_set = entry->ae_fflags_set;
395         entry2->ae_fflags_clear = entry->ae_fflags_clear;
396
397         aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
398         aes_copy(&entry2->ae_gname, &entry->ae_gname);
399         aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
400         aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
401         aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
402         entry2->ae_hardlinkset = entry->ae_hardlinkset;
403         entry2->ae_symlinkset = entry->ae_symlinkset;
404         aes_copy(&entry2->ae_uname, &entry->ae_uname);
405
406         /* Copy ACL data over. */
407         ap = entry->acl_head;
408         while (ap != NULL) {
409                 ap2 = acl_new_entry(entry2,
410                     ap->type, ap->permset, ap->tag, ap->id);
411                 if (ap2 != NULL)
412                         aes_copy(&ap2->name, &ap->name);
413                 ap = ap->next;
414         }
415
416         /* Copy xattr data over. */
417         xp = entry->xattr_head;
418         while (xp != NULL) {
419                 archive_entry_xattr_add_entry(entry2,
420                     xp->name, xp->value, xp->size);
421                 xp = xp->next;
422         }
423
424         return (entry2);
425 }
426
427 void
428 archive_entry_free(struct archive_entry *entry)
429 {
430         archive_entry_clear(entry);
431         free(entry);
432 }
433
434 struct archive_entry *
435 archive_entry_new(void)
436 {
437         struct archive_entry *entry;
438
439         entry = (struct archive_entry *)malloc(sizeof(*entry));
440         if (entry == NULL)
441                 return (NULL);
442         memset(entry, 0, sizeof(*entry));
443         return (entry);
444 }
445
446 /*
447  * Functions for reading fields from an archive_entry.
448  */
449
450 time_t
451 archive_entry_atime(struct archive_entry *entry)
452 {
453         return (entry->ae_stat.aest_atime);
454 }
455
456 long
457 archive_entry_atime_nsec(struct archive_entry *entry)
458 {
459         return (entry->ae_stat.aest_atime_nsec);
460 }
461
462 time_t
463 archive_entry_ctime(struct archive_entry *entry)
464 {
465         return (entry->ae_stat.aest_ctime);
466 }
467
468 long
469 archive_entry_ctime_nsec(struct archive_entry *entry)
470 {
471         return (entry->ae_stat.aest_ctime_nsec);
472 }
473
474 dev_t
475 archive_entry_dev(struct archive_entry *entry)
476 {
477         if (entry->ae_stat.aest_dev_is_broken_down)
478                 return ae_makedev(entry->ae_stat.aest_devmajor,
479                     entry->ae_stat.aest_devminor);
480         else
481                 return (entry->ae_stat.aest_dev);
482 }
483
484 dev_t
485 archive_entry_devmajor(struct archive_entry *entry)
486 {
487         if (entry->ae_stat.aest_dev_is_broken_down)
488                 return (entry->ae_stat.aest_devmajor);
489         else
490                 return major(entry->ae_stat.aest_dev);
491 }
492
493 dev_t
494 archive_entry_devminor(struct archive_entry *entry)
495 {
496         if (entry->ae_stat.aest_dev_is_broken_down)
497                 return (entry->ae_stat.aest_devminor);
498         else
499                 return minor(entry->ae_stat.aest_dev);
500 }
501
502 mode_t
503 archive_entry_filetype(struct archive_entry *entry)
504 {
505         return (AE_IFMT & entry->ae_stat.aest_mode);
506 }
507
508 void
509 archive_entry_fflags(struct archive_entry *entry,
510     unsigned long *set, unsigned long *clear)
511 {
512         *set = entry->ae_fflags_set;
513         *clear = entry->ae_fflags_clear;
514 }
515
516 /*
517  * Note: if text was provided, this just returns that text.  If you
518  * really need the text to be rebuilt in a canonical form, set the
519  * text, ask for the bitmaps, then set the bitmaps.  (Setting the
520  * bitmaps clears any stored text.)  This design is deliberate: if
521  * we're editing archives, we don't want to discard flags just because
522  * they aren't supported on the current system.  The bitmap<->text
523  * conversions are platform-specific (see below).
524  */
525 const char *
526 archive_entry_fflags_text(struct archive_entry *entry)
527 {
528         const char *f;
529         char *p;
530
531         f = aes_get_mbs(&entry->ae_fflags_text);
532         if (f != NULL)
533                 return (f);
534
535         if (entry->ae_fflags_set == 0  &&  entry->ae_fflags_clear == 0)
536                 return (NULL);
537
538         p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
539         if (p == NULL)
540                 return (NULL);
541
542         aes_copy_mbs(&entry->ae_fflags_text, p);
543         free(p);
544         f = aes_get_mbs(&entry->ae_fflags_text);
545         return (f);
546 }
547
548 gid_t
549 archive_entry_gid(struct archive_entry *entry)
550 {
551         return (entry->ae_stat.aest_gid);
552 }
553
554 const char *
555 archive_entry_gname(struct archive_entry *entry)
556 {
557         return (aes_get_mbs(&entry->ae_gname));
558 }
559
560 const wchar_t *
561 archive_entry_gname_w(struct archive_entry *entry)
562 {
563         return (aes_get_wcs(&entry->ae_gname));
564 }
565
566 const char *
567 archive_entry_hardlink(struct archive_entry *entry)
568 {
569         if (!entry->ae_hardlinkset)
570                 return (NULL);
571         return (aes_get_mbs(&entry->ae_hardlink));
572 }
573
574 const wchar_t *
575 archive_entry_hardlink_w(struct archive_entry *entry)
576 {
577         if (!entry->ae_hardlinkset)
578                 return (NULL);
579         return (aes_get_wcs(&entry->ae_hardlink));
580 }
581
582 ino_t
583 archive_entry_ino(struct archive_entry *entry)
584 {
585         return (entry->ae_stat.aest_ino);
586 }
587
588 mode_t
589 archive_entry_mode(struct archive_entry *entry)
590 {
591         return (entry->ae_stat.aest_mode);
592 }
593
594 time_t
595 archive_entry_mtime(struct archive_entry *entry)
596 {
597         return (entry->ae_stat.aest_mtime);
598 }
599
600 long
601 archive_entry_mtime_nsec(struct archive_entry *entry)
602 {
603         return (entry->ae_stat.aest_mtime_nsec);
604 }
605
606 unsigned int
607 archive_entry_nlink(struct archive_entry *entry)
608 {
609         return (entry->ae_stat.aest_nlink);
610 }
611
612 const char *
613 archive_entry_pathname(struct archive_entry *entry)
614 {
615         return (aes_get_mbs(&entry->ae_pathname));
616 }
617
618 const wchar_t *
619 archive_entry_pathname_w(struct archive_entry *entry)
620 {
621         return (aes_get_wcs(&entry->ae_pathname));
622 }
623
624 dev_t
625 archive_entry_rdev(struct archive_entry *entry)
626 {
627         if (entry->ae_stat.aest_rdev_is_broken_down)
628                 return ae_makedev(entry->ae_stat.aest_rdevmajor,
629                     entry->ae_stat.aest_rdevminor);
630         else
631                 return (entry->ae_stat.aest_rdev);
632 }
633
634 dev_t
635 archive_entry_rdevmajor(struct archive_entry *entry)
636 {
637         if (entry->ae_stat.aest_rdev_is_broken_down)
638                 return (entry->ae_stat.aest_rdevmajor);
639         else
640                 return major(entry->ae_stat.aest_rdev);
641 }
642
643 dev_t
644 archive_entry_rdevminor(struct archive_entry *entry)
645 {
646         if (entry->ae_stat.aest_rdev_is_broken_down)
647                 return (entry->ae_stat.aest_rdevminor);
648         else
649                 return minor(entry->ae_stat.aest_rdev);
650 }
651
652 int64_t
653 archive_entry_size(struct archive_entry *entry)
654 {
655         return (entry->ae_stat.aest_size);
656 }
657
658 const char *
659 archive_entry_sourcepath(struct archive_entry *entry)
660 {
661         return (aes_get_mbs(&entry->ae_sourcepath));
662 }
663
664 const char *
665 archive_entry_symlink(struct archive_entry *entry)
666 {
667         if (!entry->ae_symlinkset)
668                 return (NULL);
669         return (aes_get_mbs(&entry->ae_symlink));
670 }
671
672 const wchar_t *
673 archive_entry_symlink_w(struct archive_entry *entry)
674 {
675         if (!entry->ae_symlinkset)
676                 return (NULL);
677         return (aes_get_wcs(&entry->ae_symlink));
678 }
679
680 uid_t
681 archive_entry_uid(struct archive_entry *entry)
682 {
683         return (entry->ae_stat.aest_uid);
684 }
685
686 const char *
687 archive_entry_uname(struct archive_entry *entry)
688 {
689         return (aes_get_mbs(&entry->ae_uname));
690 }
691
692 const wchar_t *
693 archive_entry_uname_w(struct archive_entry *entry)
694 {
695         return (aes_get_wcs(&entry->ae_uname));
696 }
697
698 /*
699  * Functions to set archive_entry properties.
700  */
701
702 void
703 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
704 {
705         entry->stat_valid = 0;
706         entry->ae_stat.aest_mode &= ~AE_IFMT;
707         entry->ae_stat.aest_mode |= AE_IFMT & type;
708 }
709
710 void
711 archive_entry_set_fflags(struct archive_entry *entry,
712     unsigned long set, unsigned long clear)
713 {
714         aes_clean(&entry->ae_fflags_text);
715         entry->ae_fflags_set = set;
716         entry->ae_fflags_clear = clear;
717 }
718
719 const char *
720 archive_entry_copy_fflags_text(struct archive_entry *entry,
721     const char *flags)
722 {
723         aes_copy_mbs(&entry->ae_fflags_text, flags);
724         return (ae_strtofflags(flags,
725                     &entry->ae_fflags_set, &entry->ae_fflags_clear));
726 }
727
728 const wchar_t *
729 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
730     const wchar_t *flags)
731 {
732         aes_copy_wcs(&entry->ae_fflags_text, flags);
733         return (ae_wcstofflags(flags,
734                     &entry->ae_fflags_set, &entry->ae_fflags_clear));
735 }
736
737 void
738 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
739 {
740         entry->stat_valid = 0;
741         entry->ae_stat.aest_gid = g;
742 }
743
744 void
745 archive_entry_set_gname(struct archive_entry *entry, const char *name)
746 {
747         aes_set_mbs(&entry->ae_gname, name);
748 }
749
750 void
751 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
752 {
753         aes_copy_mbs(&entry->ae_gname, name);
754 }
755
756 void
757 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
758 {
759         aes_copy_wcs(&entry->ae_gname, name);
760 }
761
762 int
763 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
764 {
765         return (aes_update_utf8(&entry->ae_gname, name));
766 }
767
768 void
769 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
770 {
771         entry->stat_valid = 0;
772         entry->ae_stat.aest_ino = ino;
773 }
774
775 void
776 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
777 {
778         aes_set_mbs(&entry->ae_hardlink, target);
779         if (target != NULL)
780                 entry->ae_hardlinkset = 1;
781 }
782
783 void
784 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
785 {
786         aes_copy_mbs(&entry->ae_hardlink, target);
787         if (target != NULL)
788                 entry->ae_hardlinkset = 1;
789 }
790
791 void
792 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
793 {
794         aes_copy_wcs(&entry->ae_hardlink, target);
795         if (target != NULL)
796                 entry->ae_hardlinkset = 1;
797 }
798
799 void
800 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
801 {
802         entry->stat_valid = 0;
803         entry->ae_stat.aest_atime = t;
804         entry->ae_stat.aest_atime_nsec = ns;
805 }
806
807 void
808 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
809 {
810         entry->stat_valid = 0;
811         entry->ae_stat.aest_ctime = t;
812         entry->ae_stat.aest_ctime_nsec = ns;
813 }
814
815 void
816 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
817 {
818         entry->stat_valid = 0;
819         entry->ae_stat.aest_dev_is_broken_down = 0;
820         entry->ae_stat.aest_dev = d;
821 }
822
823 void
824 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
825 {
826         entry->stat_valid = 0;
827         entry->ae_stat.aest_dev_is_broken_down = 1;
828         entry->ae_stat.aest_devmajor = m;
829 }
830
831 void
832 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
833 {
834         entry->stat_valid = 0;
835         entry->ae_stat.aest_dev_is_broken_down = 1;
836         entry->ae_stat.aest_devminor = m;
837 }
838
839 /* Set symlink if symlink is already set, else set hardlink. */
840 void
841 archive_entry_set_link(struct archive_entry *entry, const char *target)
842 {
843         if (entry->ae_symlinkset)
844                 aes_set_mbs(&entry->ae_symlink, target);
845         else
846                 aes_set_mbs(&entry->ae_hardlink, target);
847 }
848
849 /* Set symlink if symlink is already set, else set hardlink. */
850 void
851 archive_entry_copy_link(struct archive_entry *entry, const char *target)
852 {
853         if (entry->ae_symlinkset)
854                 aes_copy_mbs(&entry->ae_symlink, target);
855         else
856                 aes_copy_mbs(&entry->ae_hardlink, target);
857 }
858
859 /* Set symlink if symlink is already set, else set hardlink. */
860 void
861 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
862 {
863         if (entry->ae_symlinkset)
864                 aes_copy_wcs(&entry->ae_symlink, target);
865         else
866                 aes_copy_wcs(&entry->ae_hardlink, target);
867 }
868
869 int
870 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
871 {
872         if (entry->ae_symlinkset)
873                 return (aes_update_utf8(&entry->ae_symlink, target));
874         else
875                 return (aes_update_utf8(&entry->ae_hardlink, target));
876 }
877
878 void
879 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
880 {
881         entry->stat_valid = 0;
882         entry->ae_stat.aest_mode = m;
883 }
884
885 void
886 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
887 {
888         entry->stat_valid = 0;
889         entry->ae_stat.aest_mtime = m;
890         entry->ae_stat.aest_mtime_nsec = ns;
891 }
892
893 void
894 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
895 {
896         entry->stat_valid = 0;
897         entry->ae_stat.aest_nlink = nlink;
898 }
899
900 void
901 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
902 {
903         aes_set_mbs(&entry->ae_pathname, name);
904 }
905
906 void
907 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
908 {
909         aes_copy_mbs(&entry->ae_pathname, name);
910 }
911
912 void
913 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
914 {
915         aes_copy_wcs(&entry->ae_pathname, name);
916 }
917
918 int
919 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
920 {
921         return (aes_update_utf8(&entry->ae_pathname, name));
922 }
923
924 void
925 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
926 {
927         entry->stat_valid = 0;
928         entry->ae_stat.aest_mode &= AE_IFMT;
929         entry->ae_stat.aest_mode |= ~AE_IFMT & p;
930 }
931
932 void
933 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
934 {
935         entry->stat_valid = 0;
936         entry->ae_stat.aest_rdev = m;
937         entry->ae_stat.aest_rdev_is_broken_down = 0;
938 }
939
940 void
941 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
942 {
943         entry->stat_valid = 0;
944         entry->ae_stat.aest_rdev_is_broken_down = 1;
945         entry->ae_stat.aest_rdevmajor = m;
946 }
947
948 void
949 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
950 {
951         entry->stat_valid = 0;
952         entry->ae_stat.aest_rdev_is_broken_down = 1;
953         entry->ae_stat.aest_rdevminor = m;
954 }
955
956 void
957 archive_entry_set_size(struct archive_entry *entry, int64_t s)
958 {
959         entry->stat_valid = 0;
960         entry->ae_stat.aest_size = s;
961 }
962
963 void
964 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
965 {
966         aes_set_mbs(&entry->ae_sourcepath, path);
967 }
968
969 void
970 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
971 {
972         aes_set_mbs(&entry->ae_symlink, linkname);
973         if (linkname != NULL)
974                 entry->ae_symlinkset = 1;
975 }
976
977 void
978 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
979 {
980         aes_copy_mbs(&entry->ae_symlink, linkname);
981         if (linkname != NULL)
982                 entry->ae_symlinkset = 1;
983 }
984
985 void
986 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
987 {
988         aes_copy_wcs(&entry->ae_symlink, linkname);
989         if (linkname != NULL)
990                 entry->ae_symlinkset = 1;
991 }
992
993 void
994 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
995 {
996         entry->stat_valid = 0;
997         entry->ae_stat.aest_uid = u;
998 }
999
1000 void
1001 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1002 {
1003         aes_set_mbs(&entry->ae_uname, name);
1004 }
1005
1006 void
1007 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1008 {
1009         aes_copy_mbs(&entry->ae_uname, name);
1010 }
1011
1012 void
1013 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1014 {
1015         aes_copy_wcs(&entry->ae_uname, name);
1016 }
1017
1018 int
1019 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1020 {
1021         return (aes_update_utf8(&entry->ae_uname, name));
1022 }
1023
1024 /*
1025  * ACL management.  The following would, of course, be a lot simpler
1026  * if: 1) the last draft of POSIX.1e were a really thorough and
1027  * complete standard that addressed the needs of ACL archiving and 2)
1028  * everyone followed it faithfully.  Alas, neither is true, so the
1029  * following is a lot more complex than might seem necessary to the
1030  * uninitiated.
1031  */
1032
1033 void
1034 archive_entry_acl_clear(struct archive_entry *entry)
1035 {
1036         struct ae_acl   *ap;
1037
1038         while (entry->acl_head != NULL) {
1039                 ap = entry->acl_head->next;
1040                 aes_clean(&entry->acl_head->name);
1041                 free(entry->acl_head);
1042                 entry->acl_head = ap;
1043         }
1044         if (entry->acl_text_w != NULL) {
1045                 free(entry->acl_text_w);
1046                 entry->acl_text_w = NULL;
1047         }
1048         entry->acl_p = NULL;
1049         entry->acl_state = 0; /* Not counting. */
1050 }
1051
1052 /*
1053  * Add a single ACL entry to the internal list of ACL data.
1054  */
1055 void
1056 archive_entry_acl_add_entry(struct archive_entry *entry,
1057     int type, int permset, int tag, int id, const char *name)
1058 {
1059         struct ae_acl *ap;
1060
1061         if (acl_special(entry, type, permset, tag) == 0)
1062                 return;
1063         ap = acl_new_entry(entry, type, permset, tag, id);
1064         if (ap == NULL) {
1065                 /* XXX Error XXX */
1066                 return;
1067         }
1068         if (name != NULL  &&  *name != '\0')
1069                 aes_copy_mbs(&ap->name, name);
1070         else
1071                 aes_clean(&ap->name);
1072 }
1073
1074 /*
1075  * As above, but with a wide-character name.
1076  */
1077 void
1078 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1079     int type, int permset, int tag, int id, const wchar_t *name)
1080 {
1081         archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
1082 }
1083
1084 void
1085 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
1086     int type, int permset, int tag, int id, const wchar_t *name, size_t len)
1087 {
1088         struct ae_acl *ap;
1089
1090         if (acl_special(entry, type, permset, tag) == 0)
1091                 return;
1092         ap = acl_new_entry(entry, type, permset, tag, id);
1093         if (ap == NULL) {
1094                 /* XXX Error XXX */
1095                 return;
1096         }
1097         if (name != NULL  &&  *name != L'\0' && len > 0)
1098                 aes_copy_wcs_len(&ap->name, name, len);
1099         else
1100                 aes_clean(&ap->name);
1101 }
1102
1103 /*
1104  * If this ACL entry is part of the standard POSIX permissions set,
1105  * store the permissions in the stat structure and return zero.
1106  */
1107 static int
1108 acl_special(struct archive_entry *entry, int type, int permset, int tag)
1109 {
1110         if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
1111                 switch (tag) {
1112                 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1113                         entry->ae_stat.aest_mode &= ~0700;
1114                         entry->ae_stat.aest_mode |= (permset & 7) << 6;
1115                         return (0);
1116                 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1117                         entry->ae_stat.aest_mode &= ~0070;
1118                         entry->ae_stat.aest_mode |= (permset & 7) << 3;
1119                         return (0);
1120                 case ARCHIVE_ENTRY_ACL_OTHER:
1121                         entry->ae_stat.aest_mode &= ~0007;
1122                         entry->ae_stat.aest_mode |= permset & 7;
1123                         return (0);
1124                 }
1125         }
1126         return (1);
1127 }
1128
1129 /*
1130  * Allocate and populate a new ACL entry with everything but the
1131  * name.
1132  */
1133 static struct ae_acl *
1134 acl_new_entry(struct archive_entry *entry,
1135     int type, int permset, int tag, int id)
1136 {
1137         struct ae_acl *ap;
1138
1139         if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
1140             type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
1141                 return (NULL);
1142         if (entry->acl_text_w != NULL) {
1143                 free(entry->acl_text_w);
1144                 entry->acl_text_w = NULL;
1145         }
1146
1147         /* XXX TODO: More sanity-checks on the arguments XXX */
1148
1149         /* If there's a matching entry already in the list, overwrite it. */
1150         for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
1151                 if (ap->type == type && ap->tag == tag && ap->id == id) {
1152                         ap->permset = permset;
1153                         return (ap);
1154                 }
1155         }
1156
1157         /* Add a new entry to the list. */
1158         ap = (struct ae_acl *)malloc(sizeof(*ap));
1159         if (ap == NULL)
1160                 return (NULL);
1161         memset(ap, 0, sizeof(*ap));
1162         ap->next = entry->acl_head;
1163         entry->acl_head = ap;
1164         ap->type = type;
1165         ap->tag = tag;
1166         ap->id = id;
1167         ap->permset = permset;
1168         return (ap);
1169 }
1170
1171 /*
1172  * Return a count of entries matching "want_type".
1173  */
1174 int
1175 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1176 {
1177         int count;
1178         struct ae_acl *ap;
1179
1180         count = 0;
1181         ap = entry->acl_head;
1182         while (ap != NULL) {
1183                 if ((ap->type & want_type) != 0)
1184                         count++;
1185                 ap = ap->next;
1186         }
1187
1188         if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1189                 count += 3;
1190         return (count);
1191 }
1192
1193 /*
1194  * Prepare for reading entries from the ACL data.  Returns a count
1195  * of entries matching "want_type", or zero if there are no
1196  * non-extended ACL entries of that type.
1197  */
1198 int
1199 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1200 {
1201         int count, cutoff;
1202
1203         count = archive_entry_acl_count(entry, want_type);
1204
1205         /*
1206          * If the only entries are the three standard ones,
1207          * then don't return any ACL data.  (In this case,
1208          * client can just use chmod(2) to set permissions.)
1209          */
1210         if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1211                 cutoff = 3;
1212         else
1213                 cutoff = 0;
1214
1215         if (count > cutoff)
1216                 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1217         else
1218                 entry->acl_state = 0;
1219         entry->acl_p = entry->acl_head;
1220         return (count);
1221 }
1222
1223 /*
1224  * Return the next ACL entry in the list.  Fake entries for the
1225  * standard permissions and include them in the returned list.
1226  */
1227
1228 int
1229 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1230     int *permset, int *tag, int *id, const char **name)
1231 {
1232         *name = NULL;
1233         *id = -1;
1234
1235         /*
1236          * The acl_state is either zero (no entries available), -1
1237          * (reading from list), or an entry type (retrieve that type
1238          * from ae_stat.aest_mode).
1239          */
1240         if (entry->acl_state == 0)
1241                 return (ARCHIVE_WARN);
1242
1243         /* The first three access entries are special. */
1244         if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1245                 switch (entry->acl_state) {
1246                 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1247                         *permset = (entry->ae_stat.aest_mode >> 6) & 7;
1248                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1249                         *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1250                         entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1251                         return (ARCHIVE_OK);
1252                 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1253                         *permset = (entry->ae_stat.aest_mode >> 3) & 7;
1254                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1255                         *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1256                         entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1257                         return (ARCHIVE_OK);
1258                 case ARCHIVE_ENTRY_ACL_OTHER:
1259                         *permset = entry->ae_stat.aest_mode & 7;
1260                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1261                         *tag = ARCHIVE_ENTRY_ACL_OTHER;
1262                         entry->acl_state = -1;
1263                         entry->acl_p = entry->acl_head;
1264                         return (ARCHIVE_OK);
1265                 default:
1266                         break;
1267                 }
1268         }
1269
1270         while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1271                 entry->acl_p = entry->acl_p->next;
1272         if (entry->acl_p == NULL) {
1273                 entry->acl_state = 0;
1274                 *type = 0;
1275                 *permset = 0;
1276                 *tag = 0;
1277                 *id = -1;
1278                 *name = NULL;
1279                 return (ARCHIVE_EOF); /* End of ACL entries. */
1280         }
1281         *type = entry->acl_p->type;
1282         *permset = entry->acl_p->permset;
1283         *tag = entry->acl_p->tag;
1284         *id = entry->acl_p->id;
1285         *name = aes_get_mbs(&entry->acl_p->name);
1286         entry->acl_p = entry->acl_p->next;
1287         return (ARCHIVE_OK);
1288 }
1289
1290 /*
1291  * Generate a text version of the ACL.  The flags parameter controls
1292  * the style of the generated ACL.
1293  */
1294 const wchar_t *
1295 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1296 {
1297         int count;
1298         size_t length;
1299         const wchar_t *wname;
1300         const wchar_t *prefix;
1301         wchar_t separator;
1302         struct ae_acl *ap;
1303         int id;
1304         wchar_t *wp;
1305
1306         if (entry->acl_text_w != NULL) {
1307                 free (entry->acl_text_w);
1308                 entry->acl_text_w = NULL;
1309         }
1310
1311         separator = L',';
1312         count = 0;
1313         length = 0;
1314         ap = entry->acl_head;
1315         while (ap != NULL) {
1316                 if ((ap->type & flags) != 0) {
1317                         count++;
1318                         if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1319                             (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1320                                 length += 8; /* "default:" */
1321                         length += 5; /* tag name */
1322                         length += 1; /* colon */
1323                         wname = aes_get_wcs(&ap->name);
1324                         if (wname != NULL)
1325                                 length += wcslen(wname);
1326                         else
1327                                 length += sizeof(uid_t) * 3 + 1;
1328                         length ++; /* colon */
1329                         length += 3; /* rwx */
1330                         length += 1; /* colon */
1331                         length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1332                         length ++; /* newline */
1333                 }
1334                 ap = ap->next;
1335         }
1336
1337         if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1338                 length += 10; /* "user::rwx\n" */
1339                 length += 11; /* "group::rwx\n" */
1340                 length += 11; /* "other::rwx\n" */
1341         }
1342
1343         if (count == 0)
1344                 return (NULL);
1345
1346         /* Now, allocate the string and actually populate it. */
1347         wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1348         if (wp == NULL)
1349                 __archive_errx(1, "No memory to generate the text version of the ACL");
1350         count = 0;
1351         if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1352                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1353                     entry->ae_stat.aest_mode & 0700, -1);
1354                 *wp++ = ',';
1355                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1356                     entry->ae_stat.aest_mode & 0070, -1);
1357                 *wp++ = ',';
1358                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1359                     entry->ae_stat.aest_mode & 0007, -1);
1360                 count += 3;
1361
1362                 ap = entry->acl_head;
1363                 while (ap != NULL) {
1364                         if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1365                                 wname = aes_get_wcs(&ap->name);
1366                                 *wp++ = separator;
1367                                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1368                                         id = ap->id;
1369                                 else
1370                                         id = -1;
1371                                 append_entry_w(&wp, NULL, ap->tag, wname,
1372                                     ap->permset, id);
1373                                 count++;
1374                         }
1375                         ap = ap->next;
1376                 }
1377         }
1378
1379
1380         if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1381                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1382                         prefix = L"default:";
1383                 else
1384                         prefix = NULL;
1385                 ap = entry->acl_head;
1386                 count = 0;
1387                 while (ap != NULL) {
1388                         if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1389                                 wname = aes_get_wcs(&ap->name);
1390                                 if (count > 0)
1391                                         *wp++ = separator;
1392                                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1393                                         id = ap->id;
1394                                 else
1395                                         id = -1;
1396                                 append_entry_w(&wp, prefix, ap->tag,
1397                                     wname, ap->permset, id);
1398                                 count ++;
1399                         }
1400                         ap = ap->next;
1401                 }
1402         }
1403
1404         return (entry->acl_text_w);
1405 }
1406
1407 static void
1408 append_id_w(wchar_t **wp, int id)
1409 {
1410         if (id < 0)
1411                 id = 0;
1412         if (id > 9)
1413                 append_id_w(wp, id / 10);
1414         *(*wp)++ = L"0123456789"[id % 10];
1415 }
1416
1417 static void
1418 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1419     const wchar_t *wname, int perm, int id)
1420 {
1421         if (prefix != NULL) {
1422                 wcscpy(*wp, prefix);
1423                 *wp += wcslen(*wp);
1424         }
1425         switch (tag) {
1426         case ARCHIVE_ENTRY_ACL_USER_OBJ:
1427                 wname = NULL;
1428                 id = -1;
1429                 /* FALLTHROUGH */
1430         case ARCHIVE_ENTRY_ACL_USER:
1431                 wcscpy(*wp, L"user");
1432                 break;
1433         case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1434                 wname = NULL;
1435                 id = -1;
1436                 /* FALLTHROUGH */
1437         case ARCHIVE_ENTRY_ACL_GROUP:
1438                 wcscpy(*wp, L"group");
1439                 break;
1440         case ARCHIVE_ENTRY_ACL_MASK:
1441                 wcscpy(*wp, L"mask");
1442                 wname = NULL;
1443                 id = -1;
1444                 break;
1445         case ARCHIVE_ENTRY_ACL_OTHER:
1446                 wcscpy(*wp, L"other");
1447                 wname = NULL;
1448                 id = -1;
1449                 break;
1450         }
1451         *wp += wcslen(*wp);
1452         *(*wp)++ = L':';
1453         if (wname != NULL) {
1454                 wcscpy(*wp, wname);
1455                 *wp += wcslen(*wp);
1456         } else if (tag == ARCHIVE_ENTRY_ACL_USER
1457             || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1458                 append_id_w(wp, id);
1459                 id = -1;
1460         }
1461         *(*wp)++ = L':';
1462         *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1463         *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1464         *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1465         if (id != -1) {
1466                 *(*wp)++ = L':';
1467                 append_id_w(wp, id);
1468         }
1469         **wp = L'\0';
1470 }
1471
1472 /*
1473  * Parse a textual ACL.  This automatically recognizes and supports
1474  * extensions described above.  The 'type' argument is used to
1475  * indicate the type that should be used for any entries not
1476  * explicitly marked as "default:".
1477  */
1478 int
1479 __archive_entry_acl_parse_w(struct archive_entry *entry,
1480     const wchar_t *text, int default_type)
1481 {
1482         struct {
1483                 const wchar_t *start;
1484                 const wchar_t *end;
1485         } field[4];
1486
1487         int fields;
1488         int type, tag, permset, id;
1489         const wchar_t *p;
1490         wchar_t sep;
1491
1492         while (text != NULL  &&  *text != L'\0') {
1493                 /*
1494                  * Parse the fields out of the next entry,
1495                  * advance 'text' to start of next entry.
1496                  */
1497                 fields = 0;
1498                 do {
1499                         const wchar_t *start, *end;
1500                         next_field_w(&text, &start, &end, &sep);
1501                         if (fields < 4) {
1502                                 field[fields].start = start;
1503                                 field[fields].end = end;
1504                         }
1505                         ++fields;
1506                 } while (sep == L':');
1507
1508                 if (fields < 3)
1509                         return (ARCHIVE_WARN);
1510
1511                 /* Check for a numeric ID in field 1 or 3. */
1512                 id = -1;
1513                 isint_w(field[1].start, field[1].end, &id);
1514                 /* Field 3 is optional. */
1515                 if (id == -1 && fields > 3)
1516                         isint_w(field[3].start, field[3].end, &id);
1517
1518                 /* Parse the permissions from field 2. */
1519                 permset = 0;
1520                 p = field[2].start;
1521                 while (p < field[2].end) {
1522                         switch (*p++) {
1523                         case 'r': case 'R':
1524                                 permset |= ARCHIVE_ENTRY_ACL_READ;
1525                                 break;
1526                         case 'w': case 'W':
1527                                 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1528                                 break;
1529                         case 'x': case 'X':
1530                                 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1531                                 break;
1532                         case '-':
1533                                 break;
1534                         default:
1535                                 return (ARCHIVE_WARN);
1536                         }
1537                 }
1538
1539                 /*
1540                  * Solaris extension:  "defaultuser::rwx" is the
1541                  * default ACL corresponding to "user::rwx", etc.
1542                  */
1543                 if (field[0].end-field[0].start > 7
1544                     && wmemcmp(field[0].start, L"default", 7) == 0) {
1545                         type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1546                         field[0].start += 7;
1547                 } else
1548                         type = default_type;
1549
1550                 if (prefix_w(field[0].start, field[0].end, L"user")) {
1551                         if (id != -1 || field[1].start < field[1].end)
1552                                 tag = ARCHIVE_ENTRY_ACL_USER;
1553                         else
1554                                 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1555                 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1556                         if (id != -1 || field[1].start < field[1].end)
1557                                 tag = ARCHIVE_ENTRY_ACL_GROUP;
1558                         else
1559                                 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1560                 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1561                         if (id != -1 || field[1].start < field[1].end)
1562                                 return (ARCHIVE_WARN);
1563                         tag = ARCHIVE_ENTRY_ACL_OTHER;
1564                 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1565                         if (id != -1 || field[1].start < field[1].end)
1566                                 return (ARCHIVE_WARN);
1567                         tag = ARCHIVE_ENTRY_ACL_MASK;
1568                 } else
1569                         return (ARCHIVE_WARN);
1570
1571                 /* Add entry to the internal list. */
1572                 archive_entry_acl_add_entry_w_len(entry, type, permset,
1573                     tag, id, field[1].start, field[1].end - field[1].start);
1574         }
1575         return (ARCHIVE_OK);
1576 }
1577
1578 /*
1579  * extended attribute handling
1580  */
1581
1582 void
1583 archive_entry_xattr_clear(struct archive_entry *entry)
1584 {
1585         struct ae_xattr *xp;
1586
1587         while (entry->xattr_head != NULL) {
1588                 xp = entry->xattr_head->next;
1589                 free(entry->xattr_head->name);
1590                 free(entry->xattr_head->value);
1591                 free(entry->xattr_head);
1592                 entry->xattr_head = xp;
1593         }
1594
1595         entry->xattr_head = NULL;
1596 }
1597
1598 void
1599 archive_entry_xattr_add_entry(struct archive_entry *entry,
1600         const char *name, const void *value, size_t size)
1601 {
1602         struct ae_xattr *xp;
1603
1604         for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1605                 ;
1606
1607         if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1608                 /* XXX Error XXX */
1609                 return;
1610
1611         xp->name = strdup(name);
1612         if ((xp->value = malloc(size)) != NULL) {
1613                 memcpy(xp->value, value, size);
1614                 xp->size = size;
1615         } else
1616                 xp->size = 0;
1617
1618         xp->next = entry->xattr_head;
1619         entry->xattr_head = xp;
1620 }
1621
1622
1623 /*
1624  * returns number of the extended attribute entries
1625  */
1626 int
1627 archive_entry_xattr_count(struct archive_entry *entry)
1628 {
1629         struct ae_xattr *xp;
1630         int count = 0;
1631
1632         for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1633                 count++;
1634
1635         return count;
1636 }
1637
1638 int
1639 archive_entry_xattr_reset(struct archive_entry * entry)
1640 {
1641         entry->xattr_p = entry->xattr_head;
1642
1643         return archive_entry_xattr_count(entry);
1644 }
1645
1646 int
1647 archive_entry_xattr_next(struct archive_entry * entry,
1648         const char **name, const void **value, size_t *size)
1649 {
1650         if (entry->xattr_p) {
1651                 *name = entry->xattr_p->name;
1652                 *value = entry->xattr_p->value;
1653                 *size = entry->xattr_p->size;
1654
1655                 entry->xattr_p = entry->xattr_p->next;
1656
1657                 return (ARCHIVE_OK);
1658         } else {
1659                 *name = NULL;
1660                 *value = NULL;
1661                 *size = (size_t)0;
1662                 return (ARCHIVE_WARN);
1663         }
1664 }
1665
1666 /*
1667  * end of xattr handling
1668  */
1669
1670 /*
1671  * Parse a string to a positive decimal integer.  Returns true if
1672  * the string is non-empty and consists only of decimal digits,
1673  * false otherwise.
1674  */
1675 static int
1676 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1677 {
1678         int n = 0;
1679         if (start >= end)
1680                 return (0);
1681         while (start < end) {
1682                 if (*start < '0' || *start > '9')
1683                         return (0);
1684                 if (n > (INT_MAX / 10))
1685                         n = INT_MAX;
1686                 else {
1687                         n *= 10;
1688                         n += *start - '0';
1689                 }
1690                 start++;
1691         }
1692         *result = n;
1693         return (1);
1694 }
1695
1696 /*
1697  * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]".  *wp is updated
1698  * to point to just after the separator.  *start points to the first
1699  * character of the matched text and *end just after the last
1700  * character of the matched identifier.  In particular *end - *start
1701  * is the length of the field body, not including leading or trailing
1702  * whitespace.
1703  */
1704 static void
1705 next_field_w(const wchar_t **wp, const wchar_t **start,
1706     const wchar_t **end, wchar_t *sep)
1707 {
1708         /* Skip leading whitespace to find start of field. */
1709         while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1710                 (*wp)++;
1711         }
1712         *start = *wp;
1713
1714         /* Scan for the separator. */
1715         while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1716             **wp != L'\n') {
1717                 (*wp)++;
1718         }
1719         *sep = **wp;
1720
1721         /* Trim trailing whitespace to locate end of field. */
1722         *end = *wp - 1;
1723         while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1724                 (*end)--;
1725         }
1726         (*end)++;
1727
1728         /* Adjust scanner location. */
1729         if (**wp != L'\0')
1730                 (*wp)++;
1731 }
1732
1733 /*
1734  * Return true if the characters [start...end) are a prefix of 'test'.
1735  * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1736  */
1737 static int
1738 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1739 {
1740         if (start == end)
1741                 return (0);
1742
1743         if (*start++ != *test++)
1744                 return (0);
1745
1746         while (start < end  &&  *start++ == *test++)
1747                 ;
1748
1749         if (start < end)
1750                 return (0);
1751
1752         return (1);
1753 }
1754
1755
1756 /*
1757  * Following code is modified from UC Berkeley sources, and
1758  * is subject to the following copyright notice.
1759  */
1760
1761 /*-
1762  * Copyright (c) 1993
1763  *      The Regents of the University of California.  All rights reserved.
1764  *
1765  * Redistribution and use in source and binary forms, with or without
1766  * modification, are permitted provided that the following conditions
1767  * are met:
1768  * 1. Redistributions of source code must retain the above copyright
1769  *    notice, this list of conditions and the following disclaimer.
1770  * 2. Redistributions in binary form must reproduce the above copyright
1771  *    notice, this list of conditions and the following disclaimer in the
1772  *    documentation and/or other materials provided with the distribution.
1773  * 4. Neither the name of the University nor the names of its contributors
1774  *    may be used to endorse or promote products derived from this software
1775  *    without specific prior written permission.
1776  *
1777  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1778  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1779  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1780  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1781  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1782  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1783  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1784  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1785  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1786  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1787  * SUCH DAMAGE.
1788  */
1789
1790 static struct flag {
1791         const char      *name;
1792         const wchar_t   *wname;
1793         unsigned long    set;
1794         unsigned long    clear;
1795 } flags[] = {
1796         /* Preferred (shorter) names per flag first, all prefixed by "no" */
1797 #ifdef SF_APPEND
1798         { "nosappnd",   L"nosappnd",            SF_APPEND,      0 },
1799         { "nosappend",  L"nosappend",           SF_APPEND,      0 },
1800 #endif
1801 #ifdef  EXT2_APPEND_FL                          /* 'a' */
1802         { "nosappnd",   L"nosappnd",            EXT2_APPEND_FL, 0 },
1803         { "nosappend",  L"nosappend",           EXT2_APPEND_FL, 0 },
1804 #endif
1805 #ifdef SF_ARCHIVED
1806         { "noarch",     L"noarch",              SF_ARCHIVED,    0 },
1807         { "noarchived", L"noarchived",          SF_ARCHIVED,    0 },
1808 #endif
1809 #ifdef SF_IMMUTABLE
1810         { "noschg",     L"noschg",              SF_IMMUTABLE,   0 },
1811         { "noschange",  L"noschange",           SF_IMMUTABLE,   0 },
1812         { "nosimmutable",       L"nosimmutable",        SF_IMMUTABLE,   0 },
1813 #endif
1814 #ifdef EXT2_IMMUTABLE_FL                        /* 'i' */
1815         { "noschg",     L"noschg",              EXT2_IMMUTABLE_FL,      0 },
1816         { "noschange",  L"noschange",           EXT2_IMMUTABLE_FL,      0 },
1817         { "nosimmutable",       L"nosimmutable",        EXT2_IMMUTABLE_FL,      0 },
1818 #endif
1819 #ifdef SF_NOUNLINK
1820         { "nosunlnk",   L"nosunlnk",            SF_NOUNLINK,    0 },
1821         { "nosunlink",  L"nosunlink",           SF_NOUNLINK,    0 },
1822 #endif
1823 #ifdef SF_SNAPSHOT
1824         { "nosnapshot", L"nosnapshot",  SF_SNAPSHOT,    0 },
1825 #endif
1826 #ifdef UF_APPEND
1827         { "nouappnd",   L"nouappnd",            UF_APPEND,      0 },
1828         { "nouappend",  L"nouappend",           UF_APPEND,      0 },
1829 #endif
1830 #ifdef UF_IMMUTABLE
1831         { "nouchg",     L"nouchg",              UF_IMMUTABLE,   0 },
1832         { "nouchange",  L"nouchange",           UF_IMMUTABLE,   0 },
1833         { "nouimmutable",       L"nouimmutable",        UF_IMMUTABLE,   0 },
1834 #endif
1835 #ifdef UF_NODUMP
1836         { "nodump",     L"nodump",              0,              UF_NODUMP},
1837 #endif
1838 #ifdef EXT2_NODUMP_FL                           /* 'd' */
1839         { "nodump",     L"nodump",              0,              EXT2_NODUMP_FL},
1840 #endif
1841 #ifdef UF_OPAQUE
1842         { "noopaque",   L"noopaque",            UF_OPAQUE,      0 },
1843 #endif
1844 #ifdef UF_NOUNLINK
1845         { "nouunlnk",   L"nouunlnk",            UF_NOUNLINK,    0 },
1846         { "nouunlink",  L"nouunlink",           UF_NOUNLINK,    0 },
1847 #endif
1848 #ifdef EXT2_COMPR_FL                            /* 'c' */
1849         { "nocompress", L"nocompress",          EXT2_COMPR_FL,  0 },
1850 #endif
1851
1852 #ifdef EXT2_NOATIME_FL                          /* 'A' */
1853         { "noatime",    L"noatime",             0,              EXT2_NOATIME_FL},
1854 #endif
1855         { NULL,         NULL,                   0,              0 }
1856 };
1857
1858 /*
1859  * fflagstostr --
1860  *      Convert file flags to a comma-separated string.  If no flags
1861  *      are set, return the empty string.
1862  */
1863 static char *
1864 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1865 {
1866         char *string, *dp;
1867         const char *sp;
1868         unsigned long bits;
1869         struct flag *flag;
1870         size_t  length;
1871
1872         bits = bitset | bitclear;
1873         length = 0;
1874         for (flag = flags; flag->name != NULL; flag++)
1875                 if (bits & (flag->set | flag->clear)) {
1876                         length += strlen(flag->name) + 1;
1877                         bits &= ~(flag->set | flag->clear);
1878                 }
1879
1880         if (length == 0)
1881                 return (NULL);
1882         string = (char *)malloc(length);
1883         if (string == NULL)
1884                 return (NULL);
1885
1886         dp = string;
1887         for (flag = flags; flag->name != NULL; flag++) {
1888                 if (bitset & flag->set || bitclear & flag->clear) {
1889                         sp = flag->name + 2;
1890                 } else if (bitset & flag->clear  ||  bitclear & flag->set) {
1891                         sp = flag->name;
1892                 } else
1893                         continue;
1894                 bitset &= ~(flag->set | flag->clear);
1895                 bitclear &= ~(flag->set | flag->clear);
1896                 if (dp > string)
1897                         *dp++ = ',';
1898                 while ((*dp++ = *sp++) != '\0')
1899                         ;
1900                 dp--;
1901         }
1902
1903         *dp = '\0';
1904         return (string);
1905 }
1906
1907 /*
1908  * strtofflags --
1909  *      Take string of arguments and return file flags.  This
1910  *      version works a little differently than strtofflags(3).
1911  *      In particular, it always tests every token, skipping any
1912  *      unrecognized tokens.  It returns a pointer to the first
1913  *      unrecognized token, or NULL if every token was recognized.
1914  *      This version is also const-correct and does not modify the
1915  *      provided string.
1916  */
1917 static const char *
1918 ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
1919 {
1920         const char *start, *end;
1921         struct flag *flag;
1922         unsigned long set, clear;
1923         const char *failed;
1924
1925         set = clear = 0;
1926         start = s;
1927         failed = NULL;
1928         /* Find start of first token. */
1929         while (*start == '\t'  ||  *start == ' '  ||  *start == ',')
1930                 start++;
1931         while (*start != '\0') {
1932                 /* Locate end of token. */
1933                 end = start;
1934                 while (*end != '\0'  &&  *end != '\t'  &&
1935                     *end != ' '  &&  *end != ',')
1936                         end++;
1937                 for (flag = flags; flag->name != NULL; flag++) {
1938                         if (memcmp(start, flag->name, end - start) == 0) {
1939                                 /* Matched "noXXXX", so reverse the sense. */
1940                                 clear |= flag->set;
1941                                 set |= flag->clear;
1942                                 break;
1943                         } else if (memcmp(start, flag->name + 2, end - start)
1944                             == 0) {
1945                                 /* Matched "XXXX", so don't reverse. */
1946                                 set |= flag->set;
1947                                 clear |= flag->clear;
1948                                 break;
1949                         }
1950                 }
1951                 /* Ignore unknown flag names. */
1952                 if (flag->name == NULL  &&  failed == NULL)
1953                         failed = start;
1954
1955                 /* Find start of next token. */
1956                 start = end;
1957                 while (*start == '\t'  ||  *start == ' '  ||  *start == ',')
1958                         start++;
1959
1960         }
1961
1962         if (setp)
1963                 *setp = set;
1964         if (clrp)
1965                 *clrp = clear;
1966
1967         /* Return location of first failure. */
1968         return (failed);
1969 }
1970
1971 /*
1972  * wcstofflags --
1973  *      Take string of arguments and return file flags.  This
1974  *      version works a little differently than strtofflags(3).
1975  *      In particular, it always tests every token, skipping any
1976  *      unrecognized tokens.  It returns a pointer to the first
1977  *      unrecognized token, or NULL if every token was recognized.
1978  *      This version is also const-correct and does not modify the
1979  *      provided string.
1980  */
1981 static const wchar_t *
1982 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1983 {
1984         const wchar_t *start, *end;
1985         struct flag *flag;
1986         unsigned long set, clear;
1987         const wchar_t *failed;
1988
1989         set = clear = 0;
1990         start = s;
1991         failed = NULL;
1992         /* Find start of first token. */
1993         while (*start == L'\t'  ||  *start == L' '  ||  *start == L',')
1994                 start++;
1995         while (*start != L'\0') {
1996                 /* Locate end of token. */
1997                 end = start;
1998                 while (*end != L'\0'  &&  *end != L'\t'  &&
1999                     *end != L' '  &&  *end != L',')
2000                         end++;
2001                 for (flag = flags; flag->wname != NULL; flag++) {
2002                         if (wmemcmp(start, flag->wname, end - start) == 0) {
2003                                 /* Matched "noXXXX", so reverse the sense. */
2004                                 clear |= flag->set;
2005                                 set |= flag->clear;
2006                                 break;
2007                         } else if (wmemcmp(start, flag->wname + 2, end - start)
2008                             == 0) {
2009                                 /* Matched "XXXX", so don't reverse. */
2010                                 set |= flag->set;
2011                                 clear |= flag->clear;
2012                                 break;
2013                         }
2014                 }
2015                 /* Ignore unknown flag names. */
2016                 if (flag->wname == NULL  &&  failed == NULL)
2017                         failed = start;
2018
2019                 /* Find start of next token. */
2020                 start = end;
2021                 while (*start == L'\t'  ||  *start == L' '  ||  *start == L',')
2022                         start++;
2023
2024         }
2025
2026         if (setp)
2027                 *setp = set;
2028         if (clrp)
2029                 *clrp = clear;
2030
2031         /* Return location of first failure. */
2032         return (failed);
2033 }
2034
2035
2036 #ifdef TEST
2037 #include <stdio.h>
2038 int
2039 main(int argc, char **argv)
2040 {
2041         struct archive_entry *entry = archive_entry_new();
2042         unsigned long set, clear;
2043         const wchar_t *remainder;
2044
2045         remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
2046         archive_entry_fflags(entry, &set, &clear);
2047
2048         wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
2049
2050         wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
2051         return (0);
2052 }
2053 #endif