We don't currently build ld-elf as binary for i386 and even if
[dragonfly.git] / contrib / libarchive / archive_entry.c
1 /*-
2  * Copyright (c) 2003-2004 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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_entry.c,v 1.23 2004/08/08 07:39:19 kientzle Exp $");
29
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #ifdef HAVE_EXT2FS_EXT2_FS_H
33 #include <ext2fs/ext2_fs.h>     /* for Linux file flags */
34 #endif
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <wchar.h>
40
41
42 #include "archive.h"
43 #include "archive_entry.h"
44
45 #undef max
46 #define max(a, b)       ((a)>(b)?(a):(b))
47
48 /*
49  * Handle wide character (i.e., Unicode) and non-wide character
50  * strings transparently.
51  *
52  */
53
54 struct aes {
55         const char *aes_mbs;
56         char *aes_mbs_alloc;
57         const wchar_t *aes_wcs;
58         wchar_t *aes_wcs_alloc;
59 };
60
61 struct ae_acl {
62         struct ae_acl *next;
63         int     type;                   /* E.g., access or default */
64         int     tag;                    /* E.g., user/group/other/mask */
65         int     permset;                /* r/w/x bits */
66         int     id;                     /* uid/gid for user/group */
67         struct aes name;                /* uname/gname */
68 };
69
70 static void     aes_clean(struct aes *);
71 static void     aes_copy(struct aes *dest, struct aes *src);
72 static const char *     aes_get_mbs(struct aes *);
73 static const wchar_t *  aes_get_wcs(struct aes *);
74 static void     aes_set_mbs(struct aes *, const char *mbs);
75 static void     aes_copy_mbs(struct aes *, const char *mbs);
76 /* static void  aes_set_wcs(struct aes *, const wchar_t *wcs); */
77 static void     aes_copy_wcs(struct aes *, const wchar_t *wcs);
78
79 static char *    ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
80 static const wchar_t    *ae_wcstofflags(const wchar_t *stringp,
81                     unsigned long *setp, unsigned long *clrp);
82 static void     append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
83                     const wchar_t *wname, int perm, int id);
84 static void     append_id_w(wchar_t **wp, int id);
85
86 static int      acl_special(struct archive_entry *entry,
87                     int type, int permset, int tag);
88 static struct ae_acl *acl_new_entry(struct archive_entry *entry,
89                     int type, int permset, int tag, int id);
90 static void     next_field_w(const wchar_t **wp, const wchar_t **start,
91                     const wchar_t **end, wchar_t *sep);
92 static int      prefix_w(const wchar_t *start, const wchar_t *end,
93                     const wchar_t *test);
94
95
96 /*
97  * Description of an archive entry.
98  *
99  * Basically, this is a "struct stat" with a few text fields added in.
100  *
101  * TODO: Add "comment", "charset", and possibly other entries
102  * that are supported by "pax interchange" format.  However, GNU, ustar,
103  * cpio, and other variants don't support these features, so they're not an
104  * excruciatingly high priority right now.
105  *
106  * TODO: "pax interchange" format allows essentially arbitrary
107  * key/value attributes to be attached to any entry.  Supporting
108  * such extensions may make this library useful for special
109  * applications (e.g., a package manager could attach special
110  * package-management attributes to each entry).  There are tricky
111  * API issues involved, so this is not going to happen until
112  * there's a real demand for it.
113  *
114  * TODO: Design a good API for handling sparse files.
115  */
116 struct archive_entry {
117         /*
118          * Note that ae_stat.st_mode & S_IFMT  can be  0!
119          *
120          * This occurs when the actual file type of the object is not
121          * in the archive.  For example, 'tar' archives store
122          * hardlinks without marking the type of the underlying
123          * object.
124          */
125         struct stat ae_stat;
126
127         /*
128          * Use aes here so that we get transparent mbs<->wcs conversions.
129          */
130         struct aes ae_fflags_text;      /* Text fflags per fflagstostr(3) */
131         unsigned long ae_fflags_set;            /* Bitmap fflags */
132         unsigned long ae_fflags_clear;
133         struct aes ae_gname;            /* Name of owning group */
134         struct aes ae_hardlink; /* Name of target for hardlink */
135         struct aes ae_pathname; /* Name of entry */
136         struct aes ae_symlink;          /* symlink contents */
137         struct aes ae_uname;            /* Name of owner */
138
139         struct ae_acl   *acl_head;
140         struct ae_acl   *acl_p;
141         int              acl_state;     /* See acl_next for details. */
142         wchar_t         *acl_text_w;
143 };
144
145 static void
146 aes_clean(struct aes *aes)
147 {
148         if (aes->aes_mbs_alloc) {
149                 free(aes->aes_mbs_alloc);
150                 aes->aes_mbs_alloc = NULL;
151         }
152         if (aes->aes_wcs_alloc) {
153                 free(aes->aes_wcs_alloc);
154                 aes->aes_wcs_alloc = NULL;
155         }
156         memset(aes, 0, sizeof(*aes));
157 }
158
159 static void
160 aes_copy(struct aes *dest, struct aes *src)
161 {
162         *dest = *src;
163         if (src->aes_mbs != NULL) {
164                 dest->aes_mbs_alloc = strdup(src->aes_mbs);
165                 dest->aes_mbs = dest->aes_mbs_alloc;
166         }
167
168         if (src->aes_wcs != NULL) {
169                 dest->aes_wcs_alloc = malloc((wcslen(src->aes_wcs) + 1)
170                     * sizeof(wchar_t));
171                 dest->aes_wcs = dest->aes_wcs_alloc;
172                 wcscpy(dest->aes_wcs_alloc, src->aes_wcs);
173         }
174 }
175
176 static const char *
177 aes_get_mbs(struct aes *aes)
178 {
179         if (aes->aes_mbs == NULL && aes->aes_wcs != NULL) {
180                 /*
181                  * XXX Need to estimate the number of byte in the
182                  * multi-byte form.  Assume that, on average, wcs
183                  * chars encode to no more than 3 bytes.  There must
184                  * be a better way... XXX
185                  */
186                 int mbs_length = wcslen(aes->aes_wcs) * 3 + 64;
187                 aes->aes_mbs_alloc = malloc(mbs_length);
188                 aes->aes_mbs = aes->aes_mbs_alloc;
189                 wcstombs(aes->aes_mbs_alloc, aes->aes_wcs, mbs_length - 1);
190                 aes->aes_mbs_alloc[mbs_length - 1] = 0;
191         }
192         return (aes->aes_mbs);
193 }
194
195 static const wchar_t *
196 aes_get_wcs(struct aes *aes)
197 {
198         if (aes->aes_wcs == NULL && aes->aes_mbs != NULL) {
199                 /*
200                  * No single byte will be more than one wide character,
201                  * so this length estimate will always be big enough.
202                  */
203                 int wcs_length = strlen(aes->aes_mbs);
204                 aes->aes_wcs_alloc
205                     = malloc((wcs_length + 1) * sizeof(wchar_t));
206                 aes->aes_wcs = aes->aes_wcs_alloc;
207                 mbstowcs(aes->aes_wcs_alloc, aes->aes_mbs, wcs_length);
208                 aes->aes_wcs_alloc[wcs_length] = 0;
209         }
210         return (aes->aes_wcs);
211 }
212
213 static void
214 aes_set_mbs(struct aes *aes, const char *mbs)
215 {
216         if (aes->aes_mbs_alloc) {
217                 free(aes->aes_mbs_alloc);
218                 aes->aes_mbs_alloc = NULL;
219         }
220         if (aes->aes_wcs_alloc) {
221                 free(aes->aes_wcs_alloc);
222                 aes->aes_wcs_alloc = NULL;
223         }
224         aes->aes_mbs = mbs;
225         aes->aes_wcs = NULL;
226 }
227
228 static void
229 aes_copy_mbs(struct aes *aes, const char *mbs)
230 {
231         if (aes->aes_mbs_alloc) {
232                 free(aes->aes_mbs_alloc);
233                 aes->aes_mbs_alloc = NULL;
234         }
235         if (aes->aes_wcs_alloc) {
236                 free(aes->aes_wcs_alloc);
237                 aes->aes_wcs_alloc = NULL;
238         }
239         aes->aes_mbs_alloc = malloc((strlen(mbs) + 1) * sizeof(char));
240         strcpy(aes->aes_mbs_alloc, mbs);
241         aes->aes_mbs = aes->aes_mbs_alloc;
242         aes->aes_wcs = NULL;
243 }
244
245 #if 0
246 static void
247 aes_set_wcs(struct aes *aes, const wchar_t *wcs)
248 {
249         if (aes->aes_mbs_alloc) {
250                 free(aes->aes_mbs_alloc);
251                 aes->aes_mbs_alloc = NULL;
252         }
253         if (aes->aes_wcs_alloc) {
254                 free(aes->aes_wcs_alloc);
255                 aes->aes_wcs_alloc = NULL;
256         }
257         aes->aes_mbs = NULL;
258         aes->aes_wcs = wcs;
259 }
260 #endif
261
262 static void
263 aes_copy_wcs(struct aes *aes, const wchar_t *wcs)
264 {
265         if (aes->aes_mbs_alloc) {
266                 free(aes->aes_mbs_alloc);
267                 aes->aes_mbs_alloc = NULL;
268         }
269         if (aes->aes_wcs_alloc) {
270                 free(aes->aes_wcs_alloc);
271                 aes->aes_wcs_alloc = NULL;
272         }
273         aes->aes_mbs = NULL;
274         aes->aes_wcs_alloc = malloc((wcslen(wcs) + 1) * sizeof(wchar_t));
275         wcscpy(aes->aes_wcs_alloc, wcs);
276         aes->aes_wcs = aes->aes_wcs_alloc;
277 }
278
279 struct archive_entry *
280 archive_entry_clear(struct archive_entry *entry)
281 {
282         aes_clean(&entry->ae_fflags_text);
283         aes_clean(&entry->ae_gname);
284         aes_clean(&entry->ae_hardlink);
285         aes_clean(&entry->ae_pathname);
286         aes_clean(&entry->ae_symlink);
287         aes_clean(&entry->ae_uname);
288         archive_entry_acl_clear(entry);
289         memset(entry, 0, sizeof(*entry));
290         return entry;
291 }
292
293 struct archive_entry *
294 archive_entry_clone(struct archive_entry *entry)
295 {
296         struct archive_entry *entry2;
297
298         /* Allocate new structure and copy over all of the fields. */
299         entry2 = malloc(sizeof(*entry2));
300         if(entry2 == NULL)
301                 return (NULL);
302         memset(entry2, 0, sizeof(*entry2));
303         entry2->ae_stat = entry->ae_stat;
304         entry2->ae_fflags_set = entry->ae_fflags_set;
305         entry2->ae_fflags_clear = entry->ae_fflags_clear;
306
307         aes_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
308         aes_copy(&entry2->ae_gname, &entry->ae_gname);
309         aes_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
310         aes_copy(&entry2->ae_pathname, &entry->ae_pathname);
311         aes_copy(&entry2->ae_symlink, &entry->ae_symlink);
312         aes_copy(&entry2->ae_uname, &entry->ae_uname);
313
314         /* XXX TODO: Copy ACL data over as well. XXX */
315         return (entry2);
316 }
317
318 void
319 archive_entry_free(struct archive_entry *entry)
320 {
321         archive_entry_clear(entry);
322         free(entry);
323 }
324
325 struct archive_entry *
326 archive_entry_new(void)
327 {
328         struct archive_entry *entry;
329
330         entry = malloc(sizeof(*entry));
331         if(entry == NULL)
332                 return (NULL);
333         memset(entry, 0, sizeof(*entry));
334         return (entry);
335 }
336
337 /*
338  * Functions for reading fields from an archive_entry.
339  */
340
341 time_t
342 archive_entry_atime(struct archive_entry *entry)
343 {
344         return (entry->ae_stat.st_atime);
345 }
346
347 long
348 archive_entry_atime_nsec(struct archive_entry *entry)
349 {
350         (void)entry; /* entry can be unused here. */
351         return (ARCHIVE_STAT_ATIME_NANOS(&entry->ae_stat));
352 }
353
354 dev_t
355 archive_entry_dev(struct archive_entry *entry)
356 {
357         return (entry->ae_stat.st_dev);
358 }
359
360 void
361 archive_entry_fflags(struct archive_entry *entry,
362     unsigned long *set, unsigned long *clear)
363 {
364         *set = entry->ae_fflags_set;
365         *clear = entry->ae_fflags_clear;
366 }
367
368 /*
369  * Note: if text was provided, this just returns that text.  If you
370  * really need the text to be rebuilt in a canonical form, set the
371  * text, ask for the bitmaps, then set the bitmaps.  (Setting the
372  * bitmaps clears any stored text.)  This design is deliberate: if
373  * we're editing archives, we don't want to discard flags just because
374  * they aren't supported on the current system.  The bitmap<->text
375  * conversions are platform-specific (see below).
376  */
377 const char *
378 archive_entry_fflags_text(struct archive_entry *entry)
379 {
380         const char *f;
381         char *p;
382
383         f = aes_get_mbs(&entry->ae_fflags_text);
384         if (f != NULL)
385                 return (f);
386
387         if (entry->ae_fflags_set == 0  &&  entry->ae_fflags_clear == 0)
388                 return (NULL);
389
390         p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
391         if (p == NULL)
392                 return (NULL);
393
394         aes_copy_mbs(&entry->ae_fflags_text, p);
395         free(p);
396         f = aes_get_mbs(&entry->ae_fflags_text);
397         return (f);
398 }
399
400 gid_t
401 archive_entry_gid(struct archive_entry *entry)
402 {
403         return (entry->ae_stat.st_gid);
404 }
405
406 const char *
407 archive_entry_gname(struct archive_entry *entry)
408 {
409         return (aes_get_mbs(&entry->ae_gname));
410 }
411
412 const char *
413 archive_entry_hardlink(struct archive_entry *entry)
414 {
415         return (aes_get_mbs(&entry->ae_hardlink));
416 }
417
418 ino_t
419 archive_entry_ino(struct archive_entry *entry)
420 {
421         return (entry->ae_stat.st_ino);
422 }
423
424 mode_t
425 archive_entry_mode(struct archive_entry *entry)
426 {
427         return (entry->ae_stat.st_mode);
428 }
429
430 time_t
431 archive_entry_mtime(struct archive_entry *entry)
432 {
433         return (entry->ae_stat.st_mtime);
434 }
435
436 long
437 archive_entry_mtime_nsec(struct archive_entry *entry)
438 {
439         (void)entry; /* entry can be unused here. */
440         return (ARCHIVE_STAT_MTIME_NANOS(&entry->ae_stat));
441 }
442
443 const char *
444 archive_entry_pathname(struct archive_entry *entry)
445 {
446         return (aes_get_mbs(&entry->ae_pathname));
447 }
448
449 const wchar_t *
450 archive_entry_pathname_w(struct archive_entry *entry)
451 {
452         return (aes_get_wcs(&entry->ae_pathname));
453 }
454
455 dev_t
456 archive_entry_rdev(struct archive_entry *entry)
457 {
458         return (entry->ae_stat.st_rdev);
459 }
460
461 dev_t
462 archive_entry_rdevmajor(struct archive_entry *entry)
463 {
464         return (major(entry->ae_stat.st_rdev));
465 }
466
467 dev_t
468 archive_entry_rdevminor(struct archive_entry *entry)
469 {
470         return (minor(entry->ae_stat.st_rdev));
471 }
472
473 int64_t
474 archive_entry_size(struct archive_entry *entry)
475 {
476         return (entry->ae_stat.st_size);
477 }
478
479 const struct stat *
480 archive_entry_stat(struct archive_entry *entry)
481 {
482         return (&entry->ae_stat);
483 }
484
485 const char *
486 archive_entry_symlink(struct archive_entry *entry)
487 {
488         return (aes_get_mbs(&entry->ae_symlink));
489 }
490
491 uid_t
492 archive_entry_uid(struct archive_entry *entry)
493 {
494         return (entry->ae_stat.st_uid);
495 }
496
497 const char *
498 archive_entry_uname(struct archive_entry *entry)
499 {
500         return (aes_get_mbs(&entry->ae_uname));
501 }
502
503 /*
504  * Functions to set archive_entry properties.
505  */
506
507 /*
508  * Note "copy" not "set" here.  The "set" functions that accept a pointer
509  * only store the pointer; they don't copy the underlying object.
510  */
511 void
512 archive_entry_copy_stat(struct archive_entry *entry, const struct stat *st)
513 {
514         entry->ae_stat = *st;
515 }
516
517 void
518 archive_entry_set_fflags(struct archive_entry *entry,
519     unsigned long set, unsigned long clear)
520 {
521         aes_clean(&entry->ae_fflags_text);
522         entry->ae_fflags_set = set;
523         entry->ae_fflags_clear = clear;
524 }
525
526 const wchar_t *
527 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
528     const wchar_t *flags)
529 {
530         aes_copy_wcs(&entry->ae_fflags_text, flags);
531         return (ae_wcstofflags(flags,
532                     &entry->ae_fflags_set, &entry->ae_fflags_clear));
533 }
534
535 void
536 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
537 {
538         entry->ae_stat.st_gid = g;
539 }
540
541 void
542 archive_entry_set_gname(struct archive_entry *entry, const char *name)
543 {
544         aes_set_mbs(&entry->ae_gname, name);
545 }
546
547 void
548 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
549 {
550         aes_copy_wcs(&entry->ae_gname, name);
551 }
552
553 void
554 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
555 {
556         aes_set_mbs(&entry->ae_hardlink, target);
557 }
558
559 void
560 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
561 {
562         aes_copy_mbs(&entry->ae_hardlink, target);
563 }
564
565 void
566 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
567 {
568         aes_copy_wcs(&entry->ae_hardlink, target);
569 }
570
571 /* Set symlink if symlink is already set, else set hardlink. */
572 void
573 archive_entry_set_link(struct archive_entry *entry, const char *target)
574 {
575         if (entry->ae_symlink.aes_mbs != NULL ||
576             entry->ae_symlink.aes_wcs != NULL)
577                 aes_set_mbs(&entry->ae_symlink, target);
578         aes_set_mbs(&entry->ae_hardlink, target);
579 }
580
581 void
582 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
583 {
584         entry->ae_stat.st_mode = m;
585 }
586
587 void
588 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
589 {
590         entry->ae_stat.st_mtime = m;
591         ARCHIVE_STAT_SET_MTIME_NANOS(&entry->ae_stat, ns);
592 }
593
594 void
595 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
596 {
597         aes_set_mbs(&entry->ae_pathname, name);
598 }
599
600 void
601 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
602 {
603         aes_copy_wcs(&entry->ae_pathname, name);
604 }
605
606 void
607 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
608 {
609         dev_t d;
610
611         d = entry->ae_stat.st_rdev;
612         entry->ae_stat.st_rdev = makedev(m, minor(d));
613 }
614
615 void
616 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
617 {
618         dev_t d;
619
620         d = entry->ae_stat.st_rdev;
621         entry->ae_stat.st_rdev = makedev( major(d), m);
622 }
623
624 void
625 archive_entry_set_size(struct archive_entry *entry, int64_t s)
626 {
627         entry->ae_stat.st_size = s;
628 }
629
630 void
631 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
632 {
633         aes_set_mbs(&entry->ae_symlink, linkname);
634 }
635
636 void
637 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
638 {
639         aes_copy_wcs(&entry->ae_symlink, linkname);
640 }
641
642 void
643 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
644 {
645         entry->ae_stat.st_uid = u;
646 }
647
648 void
649 archive_entry_set_uname(struct archive_entry *entry, const char *name)
650 {
651         aes_set_mbs(&entry->ae_uname, name);
652 }
653
654 void
655 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
656 {
657         aes_copy_wcs(&entry->ae_uname, name);
658 }
659
660 /*
661  * ACL management.  The following would, of course, be a lot simpler
662  * if: 1) the last draft of POSIX.1e were a really thorough and
663  * complete standard that addressed the needs of ACL archiving and 2)
664  * everyone followed it faithfully.  Alas, neither is true, so the
665  * following is a lot more complex than might seem necessary to the
666  * uninitiated.
667  */
668
669 void
670 archive_entry_acl_clear(struct archive_entry *entry)
671 {
672         struct ae_acl   *ap;
673
674         while (entry->acl_head != NULL) {
675                 ap = entry->acl_head->next;
676                 aes_clean(&entry->acl_head->name);
677                 free(entry->acl_head);
678                 entry->acl_head = ap;
679         }
680         if (entry->acl_text_w != NULL) {
681                 free(entry->acl_text_w);
682                 entry->acl_text_w = NULL;
683         }
684         entry->acl_p = NULL;
685         entry->acl_state = 0; /* Not counting. */
686 }
687
688 /*
689  * Add a single ACL entry to the internal list of ACL data.
690  */
691 void
692 archive_entry_acl_add_entry(struct archive_entry *entry,
693     int type, int permset, int tag, int id, const char *name)
694 {
695         struct ae_acl *ap;
696
697         if (acl_special(entry, type, permset, tag) == 0)
698                 return;
699         ap = acl_new_entry(entry, type, permset, tag, id);
700         if (ap == NULL) {
701                 /* XXX Error XXX */
702                 return;
703         }
704         if (name != NULL  &&  *name != '\0')
705                 aes_copy_mbs(&ap->name, name);
706         else
707                 aes_clean(&ap->name);
708 }
709
710 /*
711  * As above, but with a wide-character name.
712  */
713 void
714 archive_entry_acl_add_entry_w(struct archive_entry *entry,
715     int type, int permset, int tag, int id, const wchar_t *name)
716 {
717         struct ae_acl *ap;
718
719         if (acl_special(entry, type, permset, tag) == 0)
720                 return;
721         ap = acl_new_entry(entry, type, permset, tag, id);
722         if (ap == NULL) {
723                 /* XXX Error XXX */
724                 return;
725         }
726         if (name != NULL  &&  *name != L'\0')
727                 aes_copy_wcs(&ap->name, name);
728         else
729                 aes_clean(&ap->name);
730 }
731
732 /*
733  * If this ACL entry is part of the standard POSIX permissions set,
734  * store the permissions in the stat structure and return zero.
735  */
736 static int
737 acl_special(struct archive_entry *entry, int type, int permset, int tag)
738 {
739         if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
740                 switch (tag) {
741                 case ARCHIVE_ENTRY_ACL_USER_OBJ:
742                         entry->ae_stat.st_mode &= ~0700;
743                         entry->ae_stat.st_mode |= (permset & 7) << 6;
744                         return (0);
745                 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
746                         entry->ae_stat.st_mode &= ~0070;
747                         entry->ae_stat.st_mode |= (permset & 7) << 3;
748                         return (0);
749                 case ARCHIVE_ENTRY_ACL_OTHER:
750                         entry->ae_stat.st_mode &= ~0007;
751                         entry->ae_stat.st_mode |= permset & 7;
752                         return (0);
753                 }
754         }
755         return (1);
756 }
757
758 /*
759  * Allocate and populate a new ACL entry with everything but the
760  * name.
761  */
762 static struct ae_acl *
763 acl_new_entry(struct archive_entry *entry,
764     int type, int permset, int tag, int id)
765 {
766         struct ae_acl *ap;
767
768         if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
769             type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
770                 return (NULL);
771         if (entry->acl_text_w != NULL) {
772                 free(entry->acl_text_w);
773                 entry->acl_text_w = NULL;
774         }
775
776         /* XXX TODO: More sanity-checks on the arguments XXX */
777
778         /* If there's a matching entry already in the list, overwrite it. */
779         for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
780                 if (ap->type == type && ap->tag == tag && ap->id == id) {
781                         ap->permset = permset;
782                         return (ap);
783                 }
784         }
785
786         /* Add a new entry to the list. */
787         ap = malloc(sizeof(*ap));
788         memset(ap, 0, sizeof(*ap));
789         ap->next = entry->acl_head;
790         entry->acl_head = ap;
791         ap->type = type;
792         ap->tag = tag;
793         ap->id = id;
794         ap->permset = permset;
795         return (ap);
796 }
797
798 /*
799  * Return a count of entries matching "want_type".
800  */
801 int
802 archive_entry_acl_count(struct archive_entry *entry, int want_type)
803 {
804         int count;
805         struct ae_acl *ap;
806
807         count = 0;
808         ap = entry->acl_head;
809         while (ap != NULL) {
810                 if ((ap->type & want_type) != 0)
811                         count++;
812                 ap = ap->next;
813         }
814
815         if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
816                 count += 3;
817         return (count);
818 }
819
820 /*
821  * Prepare for reading entries from the ACL data.  Returns a count
822  * of entries matching "want_type", or zero if there are no
823  * non-extended ACL entries of that type.
824  */
825 int
826 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
827 {
828         int count, cutoff;
829
830         count = archive_entry_acl_count(entry, want_type);
831
832         /*
833          * If the only entries are the three standard ones,
834          * then don't return any ACL data.  (In this case,
835          * client can just use chmod(2) to set permissions.)
836          */
837         if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
838                 cutoff = 3;
839         else
840                 cutoff = 0;
841
842         if (count > cutoff)
843                 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
844         else
845                 entry->acl_state = 0;
846         entry->acl_p = entry->acl_head;
847         return (count);
848 }
849
850 /*
851  * Return the next ACL entry in the list.  Fake entries for the
852  * standard permissions and include them in the returned list.
853  */
854
855 int
856 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
857     int *permset, int *tag, int *id, const char **name)
858 {
859         *name = NULL;
860         *id = -1;
861
862         /*
863          * The acl_state is either zero (no entries available), -1
864          * (reading from list), or an entry type (retrieve that type
865          * from ae_stat.st_mode).
866          */
867         if (entry->acl_state == 0)
868                 return (ARCHIVE_WARN);
869
870         /* The first three access entries are special. */
871         if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
872                 switch (entry->acl_state) {
873                 case ARCHIVE_ENTRY_ACL_USER_OBJ:
874                         *permset = (entry->ae_stat.st_mode >> 6) & 7;
875                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
876                         *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
877                         entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
878                         return (ARCHIVE_OK);
879                 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
880                         *permset = (entry->ae_stat.st_mode >> 3) & 7;
881                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
882                         *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
883                         entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
884                         return (ARCHIVE_OK);
885                 case ARCHIVE_ENTRY_ACL_OTHER:
886                         *permset = entry->ae_stat.st_mode & 7;
887                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
888                         *tag = ARCHIVE_ENTRY_ACL_OTHER;
889                         entry->acl_state = -1;
890                         entry->acl_p = entry->acl_head;
891                         return (ARCHIVE_OK);
892                 default:
893                         break;
894                 }
895         }
896
897         while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
898                 entry->acl_p = entry->acl_p->next;
899         if (entry->acl_p == NULL) {
900                 entry->acl_state = 0;
901                 return (ARCHIVE_WARN);
902         }
903         *type = entry->acl_p->type;
904         *permset = entry->acl_p->permset;
905         *tag = entry->acl_p->tag;
906         *id = entry->acl_p->id;
907         *name = aes_get_mbs(&entry->acl_p->name);
908         entry->acl_p = entry->acl_p->next;
909         return (ARCHIVE_OK);
910 }
911
912 /*
913  * Generate a text version of the ACL.  The flags parameter controls
914  * the style of the generated ACL.
915  */
916 const wchar_t *
917 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
918 {
919         int count;
920         int length;
921         const wchar_t *wname;
922         const wchar_t *prefix;
923         wchar_t separator;
924         struct ae_acl *ap;
925         int id;
926         wchar_t *wp;
927
928         if (entry->acl_text_w != NULL) {
929                 free (entry->acl_text_w);
930                 entry->acl_text_w = NULL;
931         }
932
933         separator = L',';
934         count = 0;
935         length = 0;
936         ap = entry->acl_head;
937         while (ap != NULL) {
938                 if ((ap->type & flags) != 0) {
939                         count++;
940                         if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
941                             (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
942                                 length += 8; /* "default:" */
943                         length += 5; /* tag name */
944                         length += 1; /* colon */
945                         wname = aes_get_wcs(&ap->name);
946                         if (wname != NULL)
947                                 length += wcslen(wname);
948                         length ++; /* colon */
949                         length += 3; /* rwx */
950                         length += 1; /* colon */
951                         length += max(sizeof(uid_t),sizeof(gid_t)) * 3 + 1;
952                         length ++; /* newline */
953                 }
954                 ap = ap->next;
955         }
956
957         if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
958                 length += 10; /* "user::rwx\n" */
959                 length += 11; /* "group::rwx\n" */
960                 length += 11; /* "other::rwx\n" */
961         }
962
963         if (count == 0)
964                 return (NULL);
965
966         /* Now, allocate the string and actually populate it. */
967         wp = entry->acl_text_w = malloc(length * sizeof(wchar_t));
968         count = 0;
969         if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
970                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
971                     entry->ae_stat.st_mode & 0700, -1);
972                 *wp++ = ',';
973                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
974                     entry->ae_stat.st_mode & 0070, -1);
975                 *wp++ = ',';
976                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
977                     entry->ae_stat.st_mode & 0007, -1);
978                 count += 3;
979
980                 ap = entry->acl_head;
981                 while (ap != NULL) {
982                         if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
983                                 wname = aes_get_wcs(&ap->name);
984                                 *wp++ = separator;
985                                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
986                                         id = ap->id;
987                                 else
988                                         id = -1;
989                                 append_entry_w(&wp, NULL, ap->tag, wname,
990                                     ap->permset, id);
991                                 count++;
992                         }
993                         ap = ap->next;
994                 }
995         }
996
997
998         if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
999                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1000                         prefix = L"default:";
1001                 else
1002                         prefix = NULL;
1003                 ap = entry->acl_head;
1004                 count = 0;
1005                 while (ap != NULL) {
1006                         if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1007                                 wname = aes_get_wcs(&ap->name);
1008                                 if (count > 0)
1009                                         *wp++ = separator;
1010                                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1011                                         id = ap->id;
1012                                 else
1013                                         id = -1;
1014                                 append_entry_w(&wp, prefix, ap->tag,
1015                                     wname, ap->permset, id);
1016                                 count ++;
1017                         }
1018                         ap = ap->next;
1019                 }
1020         }
1021
1022         return (entry->acl_text_w);
1023 }
1024
1025 static void
1026 append_id_w(wchar_t **wp, int id)
1027 {
1028         if (id > 9)
1029                 append_id_w(wp, id / 10);
1030         *(*wp)++ = L"0123456789"[id % 10];
1031 }
1032
1033 static void
1034 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1035     const wchar_t *wname, int perm, int id)
1036 {
1037         if (prefix != NULL) {
1038                 wcscpy(*wp, prefix);
1039                 *wp += wcslen(*wp);
1040         }
1041         switch (tag) {
1042         case ARCHIVE_ENTRY_ACL_USER_OBJ:
1043                 wname = NULL;
1044                 id = -1;
1045                 /* FALL THROUGH */
1046         case ARCHIVE_ENTRY_ACL_USER:
1047                 wcscpy(*wp, L"user");
1048                 break;
1049         case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1050                 wname = NULL;
1051                 id = -1;
1052                 /* FALL THROUGH */
1053         case ARCHIVE_ENTRY_ACL_GROUP:
1054                 wcscpy(*wp, L"group");
1055                 break;
1056         case ARCHIVE_ENTRY_ACL_MASK:
1057                 wcscpy(*wp, L"mask");
1058                 wname = NULL;
1059                 id = -1;
1060                 break;
1061         case ARCHIVE_ENTRY_ACL_OTHER:
1062                 wcscpy(*wp, L"other");
1063                 wname = NULL;
1064                 id = -1;
1065                 break;
1066         }
1067         *wp += wcslen(*wp);
1068         *(*wp)++ = L':';
1069         if (wname != NULL) {
1070                 wcscpy(*wp, wname);
1071                 *wp += wcslen(*wp);
1072         }
1073         *(*wp)++ = L':';
1074         *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1075         *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1076         *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1077         if (id != -1) {
1078                 *(*wp)++ = L':';
1079                 append_id_w(wp, id);
1080         }
1081         **wp = L'\0';
1082 }
1083
1084 /*
1085  * Parse a textual ACL.  This automatically recognizes and supports
1086  * extensions described above.  The 'type' argument is used to
1087  * indicate the type that should be used for any entries not
1088  * explicitly marked as "default:".
1089  */
1090 int
1091 __archive_entry_acl_parse_w(struct archive_entry *entry,
1092     const wchar_t *text, int default_type)
1093 {
1094         int type, tag, permset, id;
1095         const wchar_t *start, *end;
1096         const wchar_t *name_start, *name_end;
1097         wchar_t sep;
1098         wchar_t *namebuff;
1099         int namebuff_length;
1100
1101         name_start = name_end = NULL;
1102         namebuff = NULL;
1103         namebuff_length = 0;
1104
1105         while (text != NULL  &&  *text != L'\0') {
1106                 next_field_w(&text, &start, &end, &sep);
1107                 if (sep != L':')
1108                         goto fail;
1109
1110                 /*
1111                  * Solaris extension:  "defaultuser::rwx" is the
1112                  * default ACL corresponding to "user::rwx", etc.
1113                  */
1114                 if (end-start > 7  && wmemcmp(start, L"default", 7) == 0) {
1115                         type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1116                         start += 7;
1117                 } else
1118                         type = default_type;
1119
1120                 if (prefix_w(start, end, L"user")) {
1121                         next_field_w(&text, &start, &end, &sep);
1122                         if (sep != L':')
1123                                 goto fail;
1124                         if (end > start) {
1125                                 tag = ARCHIVE_ENTRY_ACL_USER;
1126                                 name_start = start;
1127                                 name_end = end;
1128                         } else
1129                                 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1130                 } else if (prefix_w(start, end, L"group")) {
1131                         next_field_w(&text, &start, &end, &sep);
1132                         if (sep != L':')
1133                                 goto fail;
1134                         if (end > start) {
1135                                 tag = ARCHIVE_ENTRY_ACL_GROUP;
1136                                 name_start = start;
1137                                 name_end = end;
1138                         } else
1139                                 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1140                 } else if (prefix_w(start, end, L"other")) {
1141                         next_field_w(&text, &start, &end, &sep);
1142                         if (sep != L':')
1143                                 goto fail;
1144                         if (end > start)
1145                                 goto fail;
1146                         tag = ARCHIVE_ENTRY_ACL_OTHER;
1147                 } else if (prefix_w(start, end, L"mask")) {
1148                         next_field_w(&text, &start, &end, &sep);
1149                         if (sep != L':')
1150                                 goto fail;
1151                         if (end > start)
1152                                 goto fail;
1153                         tag = ARCHIVE_ENTRY_ACL_MASK;
1154                 } else
1155                         goto fail;
1156
1157                 next_field_w(&text, &start, &end, &sep);
1158                 permset = 0;
1159                 while (start < end) {
1160                         switch (*start++) {
1161                         case 'r': case 'R':
1162                                 permset |= ARCHIVE_ENTRY_ACL_READ;
1163                                 break;
1164                         case 'w': case 'W':
1165                                 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1166                                 break;
1167                         case 'x': case 'X':
1168                                 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1169                                 break;
1170                         case '-':
1171                                 break;
1172                         default:
1173                                 goto fail;
1174                         }
1175                 }
1176
1177                 /*
1178                  * Support star-compatible numeric UID/GID extension.
1179                  * This extension adds a ":" followed by the numeric
1180                  * ID so that "group:groupname:rwx", for example,
1181                  * becomes "group:groupname:rwx:999", where 999 is the
1182                  * numeric GID.  This extension makes it possible, for
1183                  * example, to correctly restore ACLs on a system that
1184                  * might have a damaged passwd file or be disconnected
1185                  * from a central NIS server.  This extension is compatible
1186                  * with POSIX.1e draft 17.
1187                  */
1188                 if (sep == L':' && (tag == ARCHIVE_ENTRY_ACL_USER ||
1189                     tag == ARCHIVE_ENTRY_ACL_GROUP)) {
1190                         next_field_w(&text, &start, &end, &sep);
1191
1192                         id = 0;
1193                         while (start < end  && *start >= '0' && *start <= '9') {
1194                                 if (id > (INT_MAX / 10))
1195                                         id = INT_MAX;
1196                                 else {
1197                                         id *= 10;
1198                                         id += *start - '0';
1199                                         start++;
1200                                 }
1201                         }
1202                 } else
1203                         id = -1; /* No id specified. */
1204
1205                 /* Skip any additional entries. */
1206                 while (sep == L':') {
1207                         next_field_w(&text, &start, &end, &sep);
1208                 }
1209
1210                 /* Add entry to the internal list. */
1211                 if (name_end == name_start) {
1212                         archive_entry_acl_add_entry_w(entry, type, permset,
1213                             tag, id, NULL);
1214                 } else {
1215                         if (namebuff_length <= name_end - name_start) {
1216                                 if (namebuff != NULL)
1217                                         free(namebuff);
1218                                 namebuff_length = name_end - name_start + 256;
1219                                 namebuff =
1220                                     malloc(namebuff_length * sizeof(wchar_t));
1221                         }
1222                         wmemcpy(namebuff, name_start, name_end - name_start);
1223                         namebuff[name_end - name_start] = L'\0';
1224                         archive_entry_acl_add_entry_w(entry, type,
1225                             permset, tag, id, namebuff);
1226                 }
1227         }
1228         if (namebuff != NULL)
1229                 free(namebuff);
1230         return (ARCHIVE_OK);
1231
1232 fail:
1233         if (namebuff != NULL)
1234                 free(namebuff);
1235         return (ARCHIVE_WARN);
1236 }
1237
1238 /*
1239  * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]".  *wp is updated
1240  * to point to just after the separator.  *start points to the first
1241  * character of the matched text and *end just after the last
1242  * character of the matched identifier.  In particular *end - *start
1243  * is the length of the field body, not including leading or trailing
1244  * whitespace.
1245  */
1246 static void
1247 next_field_w(const wchar_t **wp, const wchar_t **start,
1248     const wchar_t **end, wchar_t *sep)
1249 {
1250         /* Skip leading whitespace to find start of field. */
1251         while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1252                 (*wp)++;
1253         }
1254         *start = *wp;
1255
1256         /* Scan for the separator. */
1257         while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1258             **wp != L'\n') {
1259                 (*wp)++;
1260         }
1261         *sep = **wp;
1262
1263         /* Trim trailing whitespace to locate end of field. */
1264         *end = *wp - 1;
1265         while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1266                 (*end)--;
1267         }
1268         (*end)++;
1269
1270         /* Adjust scanner location. */
1271         if (**wp != L'\0')
1272                 (*wp)++;
1273 }
1274
1275 static int
1276 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1277 {
1278         if (start == end)
1279                 return (0);
1280
1281         if (*start++ != *test++)
1282                 return (0);
1283
1284         while (start < end  &&  *start++ == *test++)
1285                 ;
1286
1287         if (start < end)
1288                 return (0);
1289
1290         return (1);
1291 }
1292
1293
1294 /*
1295  * Following code is modified from UC Berkeley sources, and
1296  * is subject to the following copyright notice.
1297  */
1298
1299 /*-
1300  * Copyright (c) 1993
1301  *      The Regents of the University of California.  All rights reserved.
1302  *
1303  * Redistribution and use in source and binary forms, with or without
1304  * modification, are permitted provided that the following conditions
1305  * are met:
1306  * 1. Redistributions of source code must retain the above copyright
1307  *    notice, this list of conditions and the following disclaimer.
1308  * 2. Redistributions in binary form must reproduce the above copyright
1309  *    notice, this list of conditions and the following disclaimer in the
1310  *    documentation and/or other materials provided with the distribution.
1311  * 4. Neither the name of the University nor the names of its contributors
1312  *    may be used to endorse or promote products derived from this software
1313  *    without specific prior written permission.
1314  *
1315  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1316  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1317  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1318  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1319  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1320  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1321  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1322  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1323  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1324  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1325  * SUCH DAMAGE.
1326  */
1327
1328 static struct flag {
1329         const char      *name;
1330         const wchar_t   *wname;
1331         unsigned long    set;
1332         unsigned long    clear;
1333 } flags[] = {
1334         /* Preferred (shorter) names per flag first, all prefixed by "no" */
1335 #ifdef SF_APPEND
1336         { "nosappnd",   L"nosappnd",            SF_APPEND,      0 },
1337         { "nosappend",  L"nosappend",           SF_APPEND,      0 },
1338 #endif
1339 #ifdef  EXT2_APPEND_FL                          /* 'a' */
1340         { "nosappnd",   L"nosappnd",            EXT2_APPEND_FL, 0 },
1341         { "nosappend",  L"nosappend",           EXT2_APPEND_FL, 0 },
1342 #endif
1343 #ifdef SF_ARCHIVED
1344         { "noarch",     L"noarch",              SF_ARCHIVED,    0 },
1345         { "noarchived", L"noarchived",          SF_ARCHIVED,    0 },
1346 #endif
1347 #ifdef SF_IMMUTABLE
1348         { "noschg",     L"noschg",              SF_IMMUTABLE,   0 },
1349         { "noschange",  L"noschange",           SF_IMMUTABLE,   0 },
1350         { "nosimmutable",       L"nosimmutable",        SF_IMMUTABLE,   0 },
1351 #endif
1352 #ifdef EXT2_IMMUTABLE_FL                        /* 'i' */
1353         { "noschg",     L"noschg",              EXT2_IMMUTABLE_FL,      0 },
1354         { "noschange",  L"noschange",           EXT2_IMMUTABLE_FL,      0 },
1355         { "nosimmutable",       L"nosimmutable",        EXT2_IMMUTABLE_FL,      0 },
1356 #endif
1357 #ifdef SF_NOUNLINK
1358         { "nosunlnk",   L"nosunlnk",            SF_NOUNLINK,    0 },
1359         { "nosunlink",  L"nosunlink",           SF_NOUNLINK,    0 },
1360 #endif
1361 #ifdef SF_SNAPSHOT
1362         { "nosnapshot", L"nosnapshot",  SF_SNAPSHOT,    0 },
1363 #endif
1364 #ifdef UF_APPEND
1365         { "nouappnd",   L"nouappnd",            UF_APPEND,      0 },
1366         { "nouappend",  L"nouappend",           UF_APPEND,      0 },
1367 #endif
1368 #ifdef UF_IMMUTABLE
1369         { "nouchg",     L"nouchg",              UF_IMMUTABLE,   0 },
1370         { "nouchange",  L"nouchange",           UF_IMMUTABLE,   0 },
1371         { "nouimmutable",       L"nouimmutable",        UF_IMMUTABLE,   0 },
1372 #endif
1373 #ifdef UF_NODUMP
1374         { "nodump",     L"nodump",              0,              UF_NODUMP},
1375 #endif
1376 #ifdef EXT2_NODUMP_FL                           /* 'd' */
1377         { "nodump",     L"nodump",              0,              EXT2_NODUMP_FL},
1378 #endif
1379 #ifdef UF_OPAQUE
1380         { "noopaque",   L"noopaque",            UF_OPAQUE,      0 },
1381 #endif
1382 #ifdef UF_NOUNLINK
1383         { "nouunlnk",   L"nouunlnk",            UF_NOUNLINK,    0 },
1384         { "nouunlink",  L"nouunlink",           UF_NOUNLINK,    0 },
1385 #endif
1386 #ifdef EXT2_COMPR_FL                            /* 'c' */
1387         { "nocompress", L"nocompress",          EXT2_COMPR_FL,  0 },
1388 #endif
1389
1390 #ifdef EXT2_NOATIME_FL                          /* 'A' */
1391         { "noatime",    L"noatime",             0,              EXT2_NOATIME_FL},
1392 #endif
1393         { NULL,         NULL,                   0,              0 }
1394 };
1395
1396 /*
1397  * fflagstostr --
1398  *      Convert file flags to a comma-separated string.  If no flags
1399  *      are set, return the empty string.
1400  */
1401 char *
1402 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1403 {
1404         char *string, *dp;
1405         const char *sp;
1406         unsigned long bits;
1407         struct flag *flag;
1408         int     length;
1409
1410         bits = bitset | bitclear;
1411         length = 0;
1412         for (flag = flags; flag->name != NULL; flag++)
1413                 if (bits & (flag->set | flag->clear)) {
1414                         length += strlen(flag->name) + 1;
1415                         bits &= ~(flag->set | flag->clear);
1416                 }
1417
1418         if (length == 0)
1419                 return (NULL);
1420         string = malloc(length);
1421         if (string == NULL)
1422                 return (NULL);
1423
1424         dp = string;
1425         for (flag = flags; flag->name != NULL; flag++) {
1426                 if (bitset & flag->set || bitclear & flag->clear) {
1427                         sp = flag->name + 2;
1428                 } else if (bitset & flag->clear  ||  bitclear & flag->set) {
1429                         sp = flag->name;
1430                 } else
1431                         continue;
1432                 bitset &= ~(flag->set | flag->clear);
1433                 bitclear &= ~(flag->set | flag->clear);
1434                 if (dp > string)
1435                         *dp++ = ',';
1436                 while ((*dp++ = *sp++) != '\0')
1437                         ;
1438                 dp--;
1439         }
1440
1441         *dp = '\0';
1442         return (string);
1443 }
1444
1445 /*
1446  * wcstofflags --
1447  *      Take string of arguments and return file flags.  This
1448  *      version works a little differently than strtofflags(3).
1449  *      In particular, it always tests every token, skipping any
1450  *      unrecognized tokens.  It returns a pointer to the first
1451  *      unrecognized token, or NULL if every token was recognized.
1452  *      This version is also const-correct and does not modify the
1453  *      provided string.
1454  */
1455 const wchar_t *
1456 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
1457 {
1458         const wchar_t *start, *end;
1459         struct flag *flag;
1460         unsigned long set, clear;
1461         const wchar_t *failed;
1462
1463         set = clear = 0;
1464         start = s;
1465         failed = NULL;
1466         /* Find start of first token. */
1467         while (*start == L'\t'  ||  *start == L' '  ||  *start == L',')
1468                 start++;
1469         while (*start != L'\0') {
1470                 /* Locate end of token. */
1471                 end = start;
1472                 while (*end != L'\0'  &&  *end != L'\t'  &&
1473                     *end != L' '  &&  *end != L',')
1474                         end++;
1475                 for (flag = flags; flag->wname != NULL; flag++) {
1476                         if (wmemcmp(start, flag->wname, end - start) == 0) {
1477                                 /* Matched "noXXXX", so reverse the sense. */
1478                                 clear |= flag->set;
1479                                 set |= flag->clear;
1480                                 break;
1481                         } else if (wmemcmp(start, flag->wname + 2, end - start)
1482                             == 0) {
1483                                 /* Matched "XXXX", so don't reverse. */
1484                                 set |= flag->set;
1485                                 clear |= flag->clear;
1486                                 break;
1487                         }
1488                 }
1489                 /* Ignore unknown flag names. */
1490                 if (flag->wname == NULL  &&  failed == NULL)
1491                         failed = start;
1492
1493                 /* Find start of next token. */
1494                 start = end;
1495                 while (*start == L'\t'  ||  *start == L' '  ||  *start == L',')
1496                         start++;
1497
1498         }
1499
1500         if (setp)
1501                 *setp = set;
1502         if (clrp)
1503                 *clrp = clear;
1504
1505         /* Return location of first failure. */
1506         return (failed);
1507 }
1508
1509
1510 #ifdef TEST
1511 #include <stdio.h>
1512 int
1513 main(int argc, char **argv)
1514 {
1515         struct archive_entry *entry = archive_entry_new();
1516         unsigned long set, clear;
1517         const wchar_t *remainder;
1518
1519         remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
1520         archive_entry_fflags(entry, &set, &clear);
1521
1522         wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
1523
1524         wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
1525         return (0);
1526 }
1527 #endif