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