]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libarchive/archive_entry.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / 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$");
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_set = entry->ae_set;
399         aes_copy(&entry2->ae_uname, &entry->ae_uname);
400
401         /* Copy ACL data over. */
402         ap = entry->acl_head;
403         while (ap != NULL) {
404                 ap2 = acl_new_entry(entry2,
405                     ap->type, ap->permset, ap->tag, ap->id);
406                 if (ap2 != NULL)
407                         aes_copy(&ap2->name, &ap->name);
408                 ap = ap->next;
409         }
410
411         /* Copy xattr data over. */
412         xp = entry->xattr_head;
413         while (xp != NULL) {
414                 archive_entry_xattr_add_entry(entry2,
415                     xp->name, xp->value, xp->size);
416                 xp = xp->next;
417         }
418
419         return (entry2);
420 }
421
422 void
423 archive_entry_free(struct archive_entry *entry)
424 {
425         archive_entry_clear(entry);
426         free(entry);
427 }
428
429 struct archive_entry *
430 archive_entry_new(void)
431 {
432         struct archive_entry *entry;
433
434         entry = (struct archive_entry *)malloc(sizeof(*entry));
435         if (entry == NULL)
436                 return (NULL);
437         memset(entry, 0, sizeof(*entry));
438         return (entry);
439 }
440
441 /*
442  * Functions for reading fields from an archive_entry.
443  */
444
445 time_t
446 archive_entry_atime(struct archive_entry *entry)
447 {
448         return (entry->ae_stat.aest_atime);
449 }
450
451 long
452 archive_entry_atime_nsec(struct archive_entry *entry)
453 {
454         return (entry->ae_stat.aest_atime_nsec);
455 }
456
457 int
458 archive_entry_atime_is_set(struct archive_entry *entry)
459 {
460         return (entry->ae_set & AE_SET_ATIME);
461 }
462
463 time_t
464 archive_entry_ctime(struct archive_entry *entry)
465 {
466         return (entry->ae_stat.aest_ctime);
467 }
468
469 int
470 archive_entry_ctime_is_set(struct archive_entry *entry)
471 {
472         return (entry->ae_set & AE_SET_CTIME);
473 }
474
475 long
476 archive_entry_ctime_nsec(struct archive_entry *entry)
477 {
478         return (entry->ae_stat.aest_ctime_nsec);
479 }
480
481 dev_t
482 archive_entry_dev(struct archive_entry *entry)
483 {
484         if (entry->ae_stat.aest_dev_is_broken_down)
485                 return ae_makedev(entry->ae_stat.aest_devmajor,
486                     entry->ae_stat.aest_devminor);
487         else
488                 return (entry->ae_stat.aest_dev);
489 }
490
491 dev_t
492 archive_entry_devmajor(struct archive_entry *entry)
493 {
494         if (entry->ae_stat.aest_dev_is_broken_down)
495                 return (entry->ae_stat.aest_devmajor);
496         else
497                 return major(entry->ae_stat.aest_dev);
498 }
499
500 dev_t
501 archive_entry_devminor(struct archive_entry *entry)
502 {
503         if (entry->ae_stat.aest_dev_is_broken_down)
504                 return (entry->ae_stat.aest_devminor);
505         else
506                 return minor(entry->ae_stat.aest_dev);
507 }
508
509 mode_t
510 archive_entry_filetype(struct archive_entry *entry)
511 {
512         return (AE_IFMT & entry->ae_stat.aest_mode);
513 }
514
515 void
516 archive_entry_fflags(struct archive_entry *entry,
517     unsigned long *set, unsigned long *clear)
518 {
519         *set = entry->ae_fflags_set;
520         *clear = entry->ae_fflags_clear;
521 }
522
523 /*
524  * Note: if text was provided, this just returns that text.  If you
525  * really need the text to be rebuilt in a canonical form, set the
526  * text, ask for the bitmaps, then set the bitmaps.  (Setting the
527  * bitmaps clears any stored text.)  This design is deliberate: if
528  * we're editing archives, we don't want to discard flags just because
529  * they aren't supported on the current system.  The bitmap<->text
530  * conversions are platform-specific (see below).
531  */
532 const char *
533 archive_entry_fflags_text(struct archive_entry *entry)
534 {
535         const char *f;
536         char *p;
537
538         f = aes_get_mbs(&entry->ae_fflags_text);
539         if (f != NULL)
540                 return (f);
541
542         if (entry->ae_fflags_set == 0  &&  entry->ae_fflags_clear == 0)
543                 return (NULL);
544
545         p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
546         if (p == NULL)
547                 return (NULL);
548
549         aes_copy_mbs(&entry->ae_fflags_text, p);
550         free(p);
551         f = aes_get_mbs(&entry->ae_fflags_text);
552         return (f);
553 }
554
555 gid_t
556 archive_entry_gid(struct archive_entry *entry)
557 {
558         return (entry->ae_stat.aest_gid);
559 }
560
561 const char *
562 archive_entry_gname(struct archive_entry *entry)
563 {
564         return (aes_get_mbs(&entry->ae_gname));
565 }
566
567 const wchar_t *
568 archive_entry_gname_w(struct archive_entry *entry)
569 {
570         return (aes_get_wcs(&entry->ae_gname));
571 }
572
573 const char *
574 archive_entry_hardlink(struct archive_entry *entry)
575 {
576         if (entry->ae_set & AE_SET_HARDLINK)
577                 return (aes_get_mbs(&entry->ae_hardlink));
578         return (NULL);
579 }
580
581 const wchar_t *
582 archive_entry_hardlink_w(struct archive_entry *entry)
583 {
584         if (entry->ae_set & AE_SET_HARDLINK)
585                 return (aes_get_wcs(&entry->ae_hardlink));
586         return (NULL);
587 }
588
589 ino_t
590 archive_entry_ino(struct archive_entry *entry)
591 {
592         return (entry->ae_stat.aest_ino);
593 }
594
595 mode_t
596 archive_entry_mode(struct archive_entry *entry)
597 {
598         return (entry->ae_stat.aest_mode);
599 }
600
601 time_t
602 archive_entry_mtime(struct archive_entry *entry)
603 {
604         return (entry->ae_stat.aest_mtime);
605 }
606
607 long
608 archive_entry_mtime_nsec(struct archive_entry *entry)
609 {
610         return (entry->ae_stat.aest_mtime_nsec);
611 }
612
613 int
614 archive_entry_mtime_is_set(struct archive_entry *entry)
615 {
616         return (entry->ae_set & AE_SET_MTIME);
617 }
618
619 unsigned int
620 archive_entry_nlink(struct archive_entry *entry)
621 {
622         return (entry->ae_stat.aest_nlink);
623 }
624
625 const char *
626 archive_entry_pathname(struct archive_entry *entry)
627 {
628         return (aes_get_mbs(&entry->ae_pathname));
629 }
630
631 const wchar_t *
632 archive_entry_pathname_w(struct archive_entry *entry)
633 {
634         return (aes_get_wcs(&entry->ae_pathname));
635 }
636
637 dev_t
638 archive_entry_rdev(struct archive_entry *entry)
639 {
640         if (entry->ae_stat.aest_rdev_is_broken_down)
641                 return ae_makedev(entry->ae_stat.aest_rdevmajor,
642                     entry->ae_stat.aest_rdevminor);
643         else
644                 return (entry->ae_stat.aest_rdev);
645 }
646
647 dev_t
648 archive_entry_rdevmajor(struct archive_entry *entry)
649 {
650         if (entry->ae_stat.aest_rdev_is_broken_down)
651                 return (entry->ae_stat.aest_rdevmajor);
652         else
653                 return major(entry->ae_stat.aest_rdev);
654 }
655
656 dev_t
657 archive_entry_rdevminor(struct archive_entry *entry)
658 {
659         if (entry->ae_stat.aest_rdev_is_broken_down)
660                 return (entry->ae_stat.aest_rdevminor);
661         else
662                 return minor(entry->ae_stat.aest_rdev);
663 }
664
665 int64_t
666 archive_entry_size(struct archive_entry *entry)
667 {
668         return (entry->ae_stat.aest_size);
669 }
670
671 int
672 archive_entry_size_is_set(struct archive_entry *entry)
673 {
674         return (entry->ae_set & AE_SET_SIZE);
675 }
676
677 const char *
678 archive_entry_sourcepath(struct archive_entry *entry)
679 {
680         return (aes_get_mbs(&entry->ae_sourcepath));
681 }
682
683 const char *
684 archive_entry_symlink(struct archive_entry *entry)
685 {
686         if (entry->ae_set & AE_SET_SYMLINK)
687                 return (aes_get_mbs(&entry->ae_symlink));
688         return (NULL);
689 }
690
691 const wchar_t *
692 archive_entry_symlink_w(struct archive_entry *entry)
693 {
694         if (entry->ae_set & AE_SET_SYMLINK)
695                 return (aes_get_wcs(&entry->ae_symlink));
696         return (NULL);
697 }
698
699 uid_t
700 archive_entry_uid(struct archive_entry *entry)
701 {
702         return (entry->ae_stat.aest_uid);
703 }
704
705 const char *
706 archive_entry_uname(struct archive_entry *entry)
707 {
708         return (aes_get_mbs(&entry->ae_uname));
709 }
710
711 const wchar_t *
712 archive_entry_uname_w(struct archive_entry *entry)
713 {
714         return (aes_get_wcs(&entry->ae_uname));
715 }
716
717 /*
718  * Functions to set archive_entry properties.
719  */
720
721 void
722 archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
723 {
724         entry->stat_valid = 0;
725         entry->ae_stat.aest_mode &= ~AE_IFMT;
726         entry->ae_stat.aest_mode |= AE_IFMT & type;
727 }
728
729 void
730 archive_entry_set_fflags(struct archive_entry *entry,
731     unsigned long set, unsigned long clear)
732 {
733         aes_clean(&entry->ae_fflags_text);
734         entry->ae_fflags_set = set;
735         entry->ae_fflags_clear = clear;
736 }
737
738 const char *
739 archive_entry_copy_fflags_text(struct archive_entry *entry,
740     const char *flags)
741 {
742         aes_copy_mbs(&entry->ae_fflags_text, flags);
743         return (ae_strtofflags(flags,
744                     &entry->ae_fflags_set, &entry->ae_fflags_clear));
745 }
746
747 const wchar_t *
748 archive_entry_copy_fflags_text_w(struct archive_entry *entry,
749     const wchar_t *flags)
750 {
751         aes_copy_wcs(&entry->ae_fflags_text, flags);
752         return (ae_wcstofflags(flags,
753                     &entry->ae_fflags_set, &entry->ae_fflags_clear));
754 }
755
756 void
757 archive_entry_set_gid(struct archive_entry *entry, gid_t g)
758 {
759         entry->stat_valid = 0;
760         entry->ae_stat.aest_gid = g;
761 }
762
763 void
764 archive_entry_set_gname(struct archive_entry *entry, const char *name)
765 {
766         aes_set_mbs(&entry->ae_gname, name);
767 }
768
769 void
770 archive_entry_copy_gname(struct archive_entry *entry, const char *name)
771 {
772         aes_copy_mbs(&entry->ae_gname, name);
773 }
774
775 void
776 archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
777 {
778         aes_copy_wcs(&entry->ae_gname, name);
779 }
780
781 int
782 archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
783 {
784         return (aes_update_utf8(&entry->ae_gname, name));
785 }
786
787 void
788 archive_entry_set_ino(struct archive_entry *entry, unsigned long ino)
789 {
790         entry->stat_valid = 0;
791         entry->ae_stat.aest_ino = ino;
792 }
793
794 void
795 archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
796 {
797         aes_set_mbs(&entry->ae_hardlink, target);
798         if (target != NULL)
799                 entry->ae_set |= AE_SET_HARDLINK;
800         else
801                 entry->ae_set &= ~AE_SET_HARDLINK;
802 }
803
804 void
805 archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
806 {
807         aes_copy_mbs(&entry->ae_hardlink, target);
808         if (target != NULL)
809                 entry->ae_set |= AE_SET_HARDLINK;
810         else
811                 entry->ae_set &= ~AE_SET_HARDLINK;
812 }
813
814 void
815 archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
816 {
817         aes_copy_wcs(&entry->ae_hardlink, target);
818         if (target != NULL)
819                 entry->ae_set |= AE_SET_HARDLINK;
820         else
821                 entry->ae_set &= ~AE_SET_HARDLINK;
822 }
823
824 void
825 archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
826 {
827         entry->stat_valid = 0;
828         entry->ae_set |= AE_SET_ATIME;
829         entry->ae_stat.aest_atime = t;
830         entry->ae_stat.aest_atime_nsec = ns;
831 }
832
833 void
834 archive_entry_unset_atime(struct archive_entry *entry)
835 {
836         archive_entry_set_atime(entry, 0, 0);
837         entry->ae_set &= ~AE_SET_ATIME;
838 }
839
840 void
841 archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
842 {
843         entry->stat_valid = 0;
844         entry->ae_set |= AE_SET_CTIME;
845         entry->ae_stat.aest_ctime = t;
846         entry->ae_stat.aest_ctime_nsec = ns;
847 }
848
849 void
850 archive_entry_unset_ctime(struct archive_entry *entry)
851 {
852         archive_entry_set_ctime(entry, 0, 0);
853         entry->ae_set &= ~AE_SET_CTIME;
854 }
855
856 void
857 archive_entry_set_dev(struct archive_entry *entry, dev_t d)
858 {
859         entry->stat_valid = 0;
860         entry->ae_stat.aest_dev_is_broken_down = 0;
861         entry->ae_stat.aest_dev = d;
862 }
863
864 void
865 archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
866 {
867         entry->stat_valid = 0;
868         entry->ae_stat.aest_dev_is_broken_down = 1;
869         entry->ae_stat.aest_devmajor = m;
870 }
871
872 void
873 archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
874 {
875         entry->stat_valid = 0;
876         entry->ae_stat.aest_dev_is_broken_down = 1;
877         entry->ae_stat.aest_devminor = m;
878 }
879
880 /* Set symlink if symlink is already set, else set hardlink. */
881 void
882 archive_entry_set_link(struct archive_entry *entry, const char *target)
883 {
884         if (entry->ae_set & AE_SET_SYMLINK)
885                 aes_set_mbs(&entry->ae_symlink, target);
886         else
887                 aes_set_mbs(&entry->ae_hardlink, target);
888 }
889
890 /* Set symlink if symlink is already set, else set hardlink. */
891 void
892 archive_entry_copy_link(struct archive_entry *entry, const char *target)
893 {
894         if (entry->ae_set & AE_SET_SYMLINK)
895                 aes_copy_mbs(&entry->ae_symlink, target);
896         else
897                 aes_copy_mbs(&entry->ae_hardlink, target);
898 }
899
900 /* Set symlink if symlink is already set, else set hardlink. */
901 void
902 archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
903 {
904         if (entry->ae_set & AE_SET_SYMLINK)
905                 aes_copy_wcs(&entry->ae_symlink, target);
906         else
907                 aes_copy_wcs(&entry->ae_hardlink, target);
908 }
909
910 int
911 archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
912 {
913         if (entry->ae_set & AE_SET_SYMLINK)
914                 return (aes_update_utf8(&entry->ae_symlink, target));
915         else
916                 return (aes_update_utf8(&entry->ae_hardlink, target));
917 }
918
919 void
920 archive_entry_set_mode(struct archive_entry *entry, mode_t m)
921 {
922         entry->stat_valid = 0;
923         entry->ae_stat.aest_mode = m;
924 }
925
926 void
927 archive_entry_set_mtime(struct archive_entry *entry, time_t m, long ns)
928 {
929         entry->stat_valid = 0;
930         entry->ae_set |= AE_SET_MTIME;
931         entry->ae_stat.aest_mtime = m;
932         entry->ae_stat.aest_mtime_nsec = ns;
933 }
934
935 void
936 archive_entry_unset_mtime(struct archive_entry *entry)
937 {
938         archive_entry_set_mtime(entry, 0, 0);
939         entry->ae_set &= ~AE_SET_MTIME;
940 }
941
942 void
943 archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
944 {
945         entry->stat_valid = 0;
946         entry->ae_stat.aest_nlink = nlink;
947 }
948
949 void
950 archive_entry_set_pathname(struct archive_entry *entry, const char *name)
951 {
952         aes_set_mbs(&entry->ae_pathname, name);
953 }
954
955 void
956 archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
957 {
958         aes_copy_mbs(&entry->ae_pathname, name);
959 }
960
961 void
962 archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
963 {
964         aes_copy_wcs(&entry->ae_pathname, name);
965 }
966
967 int
968 archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
969 {
970         return (aes_update_utf8(&entry->ae_pathname, name));
971 }
972
973 void
974 archive_entry_set_perm(struct archive_entry *entry, mode_t p)
975 {
976         entry->stat_valid = 0;
977         entry->ae_stat.aest_mode &= AE_IFMT;
978         entry->ae_stat.aest_mode |= ~AE_IFMT & p;
979 }
980
981 void
982 archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
983 {
984         entry->stat_valid = 0;
985         entry->ae_stat.aest_rdev = m;
986         entry->ae_stat.aest_rdev_is_broken_down = 0;
987 }
988
989 void
990 archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
991 {
992         entry->stat_valid = 0;
993         entry->ae_stat.aest_rdev_is_broken_down = 1;
994         entry->ae_stat.aest_rdevmajor = m;
995 }
996
997 void
998 archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
999 {
1000         entry->stat_valid = 0;
1001         entry->ae_stat.aest_rdev_is_broken_down = 1;
1002         entry->ae_stat.aest_rdevminor = m;
1003 }
1004
1005 void
1006 archive_entry_set_size(struct archive_entry *entry, int64_t s)
1007 {
1008         entry->stat_valid = 0;
1009         entry->ae_stat.aest_size = s;
1010         entry->ae_set |= AE_SET_SIZE;
1011 }
1012
1013 void
1014 archive_entry_unset_size(struct archive_entry *entry)
1015 {
1016         archive_entry_set_size(entry, 0);
1017         entry->ae_set &= ~AE_SET_SIZE;
1018 }
1019
1020 void
1021 archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
1022 {
1023         aes_set_mbs(&entry->ae_sourcepath, path);
1024 }
1025
1026 void
1027 archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
1028 {
1029         aes_set_mbs(&entry->ae_symlink, linkname);
1030         if (linkname != NULL)
1031                 entry->ae_set |= AE_SET_SYMLINK;
1032         else
1033                 entry->ae_set &= ~AE_SET_SYMLINK;
1034 }
1035
1036 void
1037 archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
1038 {
1039         aes_copy_mbs(&entry->ae_symlink, linkname);
1040         if (linkname != NULL)
1041                 entry->ae_set |= AE_SET_SYMLINK;
1042         else
1043                 entry->ae_set &= ~AE_SET_SYMLINK;
1044 }
1045
1046 void
1047 archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
1048 {
1049         aes_copy_wcs(&entry->ae_symlink, linkname);
1050         if (linkname != NULL)
1051                 entry->ae_set |= AE_SET_SYMLINK;
1052         else
1053                 entry->ae_set &= ~AE_SET_SYMLINK;
1054 }
1055
1056 void
1057 archive_entry_set_uid(struct archive_entry *entry, uid_t u)
1058 {
1059         entry->stat_valid = 0;
1060         entry->ae_stat.aest_uid = u;
1061 }
1062
1063 void
1064 archive_entry_set_uname(struct archive_entry *entry, const char *name)
1065 {
1066         aes_set_mbs(&entry->ae_uname, name);
1067 }
1068
1069 void
1070 archive_entry_copy_uname(struct archive_entry *entry, const char *name)
1071 {
1072         aes_copy_mbs(&entry->ae_uname, name);
1073 }
1074
1075 void
1076 archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
1077 {
1078         aes_copy_wcs(&entry->ae_uname, name);
1079 }
1080
1081 int
1082 archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
1083 {
1084         return (aes_update_utf8(&entry->ae_uname, name));
1085 }
1086
1087 /*
1088  * ACL management.  The following would, of course, be a lot simpler
1089  * if: 1) the last draft of POSIX.1e were a really thorough and
1090  * complete standard that addressed the needs of ACL archiving and 2)
1091  * everyone followed it faithfully.  Alas, neither is true, so the
1092  * following is a lot more complex than might seem necessary to the
1093  * uninitiated.
1094  */
1095
1096 void
1097 archive_entry_acl_clear(struct archive_entry *entry)
1098 {
1099         struct ae_acl   *ap;
1100
1101         while (entry->acl_head != NULL) {
1102                 ap = entry->acl_head->next;
1103                 aes_clean(&entry->acl_head->name);
1104                 free(entry->acl_head);
1105                 entry->acl_head = ap;
1106         }
1107         if (entry->acl_text_w != NULL) {
1108                 free(entry->acl_text_w);
1109                 entry->acl_text_w = NULL;
1110         }
1111         entry->acl_p = NULL;
1112         entry->acl_state = 0; /* Not counting. */
1113 }
1114
1115 /*
1116  * Add a single ACL entry to the internal list of ACL data.
1117  */
1118 void
1119 archive_entry_acl_add_entry(struct archive_entry *entry,
1120     int type, int permset, int tag, int id, const char *name)
1121 {
1122         struct ae_acl *ap;
1123
1124         if (acl_special(entry, type, permset, tag) == 0)
1125                 return;
1126         ap = acl_new_entry(entry, type, permset, tag, id);
1127         if (ap == NULL) {
1128                 /* XXX Error XXX */
1129                 return;
1130         }
1131         if (name != NULL  &&  *name != '\0')
1132                 aes_copy_mbs(&ap->name, name);
1133         else
1134                 aes_clean(&ap->name);
1135 }
1136
1137 /*
1138  * As above, but with a wide-character name.
1139  */
1140 void
1141 archive_entry_acl_add_entry_w(struct archive_entry *entry,
1142     int type, int permset, int tag, int id, const wchar_t *name)
1143 {
1144         archive_entry_acl_add_entry_w_len(entry, type, permset, tag, id, name, wcslen(name));
1145 }
1146
1147 void
1148 archive_entry_acl_add_entry_w_len(struct archive_entry *entry,
1149     int type, int permset, int tag, int id, const wchar_t *name, size_t len)
1150 {
1151         struct ae_acl *ap;
1152
1153         if (acl_special(entry, type, permset, tag) == 0)
1154                 return;
1155         ap = acl_new_entry(entry, type, permset, tag, id);
1156         if (ap == NULL) {
1157                 /* XXX Error XXX */
1158                 return;
1159         }
1160         if (name != NULL  &&  *name != L'\0' && len > 0)
1161                 aes_copy_wcs_len(&ap->name, name, len);
1162         else
1163                 aes_clean(&ap->name);
1164 }
1165
1166 /*
1167  * If this ACL entry is part of the standard POSIX permissions set,
1168  * store the permissions in the stat structure and return zero.
1169  */
1170 static int
1171 acl_special(struct archive_entry *entry, int type, int permset, int tag)
1172 {
1173         if (type == ARCHIVE_ENTRY_ACL_TYPE_ACCESS) {
1174                 switch (tag) {
1175                 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1176                         entry->ae_stat.aest_mode &= ~0700;
1177                         entry->ae_stat.aest_mode |= (permset & 7) << 6;
1178                         return (0);
1179                 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1180                         entry->ae_stat.aest_mode &= ~0070;
1181                         entry->ae_stat.aest_mode |= (permset & 7) << 3;
1182                         return (0);
1183                 case ARCHIVE_ENTRY_ACL_OTHER:
1184                         entry->ae_stat.aest_mode &= ~0007;
1185                         entry->ae_stat.aest_mode |= permset & 7;
1186                         return (0);
1187                 }
1188         }
1189         return (1);
1190 }
1191
1192 /*
1193  * Allocate and populate a new ACL entry with everything but the
1194  * name.
1195  */
1196 static struct ae_acl *
1197 acl_new_entry(struct archive_entry *entry,
1198     int type, int permset, int tag, int id)
1199 {
1200         struct ae_acl *ap;
1201
1202         if (type != ARCHIVE_ENTRY_ACL_TYPE_ACCESS &&
1203             type != ARCHIVE_ENTRY_ACL_TYPE_DEFAULT)
1204                 return (NULL);
1205         if (entry->acl_text_w != NULL) {
1206                 free(entry->acl_text_w);
1207                 entry->acl_text_w = NULL;
1208         }
1209
1210         /* XXX TODO: More sanity-checks on the arguments XXX */
1211
1212         /* If there's a matching entry already in the list, overwrite it. */
1213         for (ap = entry->acl_head; ap != NULL; ap = ap->next) {
1214                 if (ap->type == type && ap->tag == tag && ap->id == id) {
1215                         ap->permset = permset;
1216                         return (ap);
1217                 }
1218         }
1219
1220         /* Add a new entry to the list. */
1221         ap = (struct ae_acl *)malloc(sizeof(*ap));
1222         if (ap == NULL)
1223                 return (NULL);
1224         memset(ap, 0, sizeof(*ap));
1225         ap->next = entry->acl_head;
1226         entry->acl_head = ap;
1227         ap->type = type;
1228         ap->tag = tag;
1229         ap->id = id;
1230         ap->permset = permset;
1231         return (ap);
1232 }
1233
1234 /*
1235  * Return a count of entries matching "want_type".
1236  */
1237 int
1238 archive_entry_acl_count(struct archive_entry *entry, int want_type)
1239 {
1240         int count;
1241         struct ae_acl *ap;
1242
1243         count = 0;
1244         ap = entry->acl_head;
1245         while (ap != NULL) {
1246                 if ((ap->type & want_type) != 0)
1247                         count++;
1248                 ap = ap->next;
1249         }
1250
1251         if (count > 0 && ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0))
1252                 count += 3;
1253         return (count);
1254 }
1255
1256 /*
1257  * Prepare for reading entries from the ACL data.  Returns a count
1258  * of entries matching "want_type", or zero if there are no
1259  * non-extended ACL entries of that type.
1260  */
1261 int
1262 archive_entry_acl_reset(struct archive_entry *entry, int want_type)
1263 {
1264         int count, cutoff;
1265
1266         count = archive_entry_acl_count(entry, want_type);
1267
1268         /*
1269          * If the only entries are the three standard ones,
1270          * then don't return any ACL data.  (In this case,
1271          * client can just use chmod(2) to set permissions.)
1272          */
1273         if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)
1274                 cutoff = 3;
1275         else
1276                 cutoff = 0;
1277
1278         if (count > cutoff)
1279                 entry->acl_state = ARCHIVE_ENTRY_ACL_USER_OBJ;
1280         else
1281                 entry->acl_state = 0;
1282         entry->acl_p = entry->acl_head;
1283         return (count);
1284 }
1285
1286 /*
1287  * Return the next ACL entry in the list.  Fake entries for the
1288  * standard permissions and include them in the returned list.
1289  */
1290
1291 int
1292 archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
1293     int *permset, int *tag, int *id, const char **name)
1294 {
1295         *name = NULL;
1296         *id = -1;
1297
1298         /*
1299          * The acl_state is either zero (no entries available), -1
1300          * (reading from list), or an entry type (retrieve that type
1301          * from ae_stat.aest_mode).
1302          */
1303         if (entry->acl_state == 0)
1304                 return (ARCHIVE_WARN);
1305
1306         /* The first three access entries are special. */
1307         if ((want_type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1308                 switch (entry->acl_state) {
1309                 case ARCHIVE_ENTRY_ACL_USER_OBJ:
1310                         *permset = (entry->ae_stat.aest_mode >> 6) & 7;
1311                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1312                         *tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1313                         entry->acl_state = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1314                         return (ARCHIVE_OK);
1315                 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1316                         *permset = (entry->ae_stat.aest_mode >> 3) & 7;
1317                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1318                         *tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1319                         entry->acl_state = ARCHIVE_ENTRY_ACL_OTHER;
1320                         return (ARCHIVE_OK);
1321                 case ARCHIVE_ENTRY_ACL_OTHER:
1322                         *permset = entry->ae_stat.aest_mode & 7;
1323                         *type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
1324                         *tag = ARCHIVE_ENTRY_ACL_OTHER;
1325                         entry->acl_state = -1;
1326                         entry->acl_p = entry->acl_head;
1327                         return (ARCHIVE_OK);
1328                 default:
1329                         break;
1330                 }
1331         }
1332
1333         while (entry->acl_p != NULL && (entry->acl_p->type & want_type) == 0)
1334                 entry->acl_p = entry->acl_p->next;
1335         if (entry->acl_p == NULL) {
1336                 entry->acl_state = 0;
1337                 *type = 0;
1338                 *permset = 0;
1339                 *tag = 0;
1340                 *id = -1;
1341                 *name = NULL;
1342                 return (ARCHIVE_EOF); /* End of ACL entries. */
1343         }
1344         *type = entry->acl_p->type;
1345         *permset = entry->acl_p->permset;
1346         *tag = entry->acl_p->tag;
1347         *id = entry->acl_p->id;
1348         *name = aes_get_mbs(&entry->acl_p->name);
1349         entry->acl_p = entry->acl_p->next;
1350         return (ARCHIVE_OK);
1351 }
1352
1353 /*
1354  * Generate a text version of the ACL.  The flags parameter controls
1355  * the style of the generated ACL.
1356  */
1357 const wchar_t *
1358 archive_entry_acl_text_w(struct archive_entry *entry, int flags)
1359 {
1360         int count;
1361         size_t length;
1362         const wchar_t *wname;
1363         const wchar_t *prefix;
1364         wchar_t separator;
1365         struct ae_acl *ap;
1366         int id;
1367         wchar_t *wp;
1368
1369         if (entry->acl_text_w != NULL) {
1370                 free (entry->acl_text_w);
1371                 entry->acl_text_w = NULL;
1372         }
1373
1374         separator = L',';
1375         count = 0;
1376         length = 0;
1377         ap = entry->acl_head;
1378         while (ap != NULL) {
1379                 if ((ap->type & flags) != 0) {
1380                         count++;
1381                         if ((flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) &&
1382                             (ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT))
1383                                 length += 8; /* "default:" */
1384                         length += 5; /* tag name */
1385                         length += 1; /* colon */
1386                         wname = aes_get_wcs(&ap->name);
1387                         if (wname != NULL)
1388                                 length += wcslen(wname);
1389                         else
1390                                 length += sizeof(uid_t) * 3 + 1;
1391                         length ++; /* colon */
1392                         length += 3; /* rwx */
1393                         length += 1; /* colon */
1394                         length += max(sizeof(uid_t), sizeof(gid_t)) * 3 + 1;
1395                         length ++; /* newline */
1396                 }
1397                 ap = ap->next;
1398         }
1399
1400         if (count > 0 && ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0)) {
1401                 length += 10; /* "user::rwx\n" */
1402                 length += 11; /* "group::rwx\n" */
1403                 length += 11; /* "other::rwx\n" */
1404         }
1405
1406         if (count == 0)
1407                 return (NULL);
1408
1409         /* Now, allocate the string and actually populate it. */
1410         wp = entry->acl_text_w = (wchar_t *)malloc(length * sizeof(wchar_t));
1411         if (wp == NULL)
1412                 __archive_errx(1, "No memory to generate the text version of the ACL");
1413         count = 0;
1414         if ((flags & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1415                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_USER_OBJ, NULL,
1416                     entry->ae_stat.aest_mode & 0700, -1);
1417                 *wp++ = ',';
1418                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_GROUP_OBJ, NULL,
1419                     entry->ae_stat.aest_mode & 0070, -1);
1420                 *wp++ = ',';
1421                 append_entry_w(&wp, NULL, ARCHIVE_ENTRY_ACL_OTHER, NULL,
1422                     entry->ae_stat.aest_mode & 0007, -1);
1423                 count += 3;
1424
1425                 ap = entry->acl_head;
1426                 while (ap != NULL) {
1427                         if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_ACCESS) != 0) {
1428                                 wname = aes_get_wcs(&ap->name);
1429                                 *wp++ = separator;
1430                                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1431                                         id = ap->id;
1432                                 else
1433                                         id = -1;
1434                                 append_entry_w(&wp, NULL, ap->tag, wname,
1435                                     ap->permset, id);
1436                                 count++;
1437                         }
1438                         ap = ap->next;
1439                 }
1440         }
1441
1442
1443         if ((flags & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1444                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT)
1445                         prefix = L"default:";
1446                 else
1447                         prefix = NULL;
1448                 ap = entry->acl_head;
1449                 count = 0;
1450                 while (ap != NULL) {
1451                         if ((ap->type & ARCHIVE_ENTRY_ACL_TYPE_DEFAULT) != 0) {
1452                                 wname = aes_get_wcs(&ap->name);
1453                                 if (count > 0)
1454                                         *wp++ = separator;
1455                                 if (flags & ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID)
1456                                         id = ap->id;
1457                                 else
1458                                         id = -1;
1459                                 append_entry_w(&wp, prefix, ap->tag,
1460                                     wname, ap->permset, id);
1461                                 count ++;
1462                         }
1463                         ap = ap->next;
1464                 }
1465         }
1466
1467         return (entry->acl_text_w);
1468 }
1469
1470 static void
1471 append_id_w(wchar_t **wp, int id)
1472 {
1473         if (id < 0)
1474                 id = 0;
1475         if (id > 9)
1476                 append_id_w(wp, id / 10);
1477         *(*wp)++ = L"0123456789"[id % 10];
1478 }
1479
1480 static void
1481 append_entry_w(wchar_t **wp, const wchar_t *prefix, int tag,
1482     const wchar_t *wname, int perm, int id)
1483 {
1484         if (prefix != NULL) {
1485                 wcscpy(*wp, prefix);
1486                 *wp += wcslen(*wp);
1487         }
1488         switch (tag) {
1489         case ARCHIVE_ENTRY_ACL_USER_OBJ:
1490                 wname = NULL;
1491                 id = -1;
1492                 /* FALLTHROUGH */
1493         case ARCHIVE_ENTRY_ACL_USER:
1494                 wcscpy(*wp, L"user");
1495                 break;
1496         case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
1497                 wname = NULL;
1498                 id = -1;
1499                 /* FALLTHROUGH */
1500         case ARCHIVE_ENTRY_ACL_GROUP:
1501                 wcscpy(*wp, L"group");
1502                 break;
1503         case ARCHIVE_ENTRY_ACL_MASK:
1504                 wcscpy(*wp, L"mask");
1505                 wname = NULL;
1506                 id = -1;
1507                 break;
1508         case ARCHIVE_ENTRY_ACL_OTHER:
1509                 wcscpy(*wp, L"other");
1510                 wname = NULL;
1511                 id = -1;
1512                 break;
1513         }
1514         *wp += wcslen(*wp);
1515         *(*wp)++ = L':';
1516         if (wname != NULL) {
1517                 wcscpy(*wp, wname);
1518                 *wp += wcslen(*wp);
1519         } else if (tag == ARCHIVE_ENTRY_ACL_USER
1520             || tag == ARCHIVE_ENTRY_ACL_GROUP) {
1521                 append_id_w(wp, id);
1522                 id = -1;
1523         }
1524         *(*wp)++ = L':';
1525         *(*wp)++ = (perm & 0444) ? L'r' : L'-';
1526         *(*wp)++ = (perm & 0222) ? L'w' : L'-';
1527         *(*wp)++ = (perm & 0111) ? L'x' : L'-';
1528         if (id != -1) {
1529                 *(*wp)++ = L':';
1530                 append_id_w(wp, id);
1531         }
1532         **wp = L'\0';
1533 }
1534
1535 /*
1536  * Parse a textual ACL.  This automatically recognizes and supports
1537  * extensions described above.  The 'type' argument is used to
1538  * indicate the type that should be used for any entries not
1539  * explicitly marked as "default:".
1540  */
1541 int
1542 __archive_entry_acl_parse_w(struct archive_entry *entry,
1543     const wchar_t *text, int default_type)
1544 {
1545         struct {
1546                 const wchar_t *start;
1547                 const wchar_t *end;
1548         } field[4];
1549
1550         int fields;
1551         int type, tag, permset, id;
1552         const wchar_t *p;
1553         wchar_t sep;
1554
1555         while (text != NULL  &&  *text != L'\0') {
1556                 /*
1557                  * Parse the fields out of the next entry,
1558                  * advance 'text' to start of next entry.
1559                  */
1560                 fields = 0;
1561                 do {
1562                         const wchar_t *start, *end;
1563                         next_field_w(&text, &start, &end, &sep);
1564                         if (fields < 4) {
1565                                 field[fields].start = start;
1566                                 field[fields].end = end;
1567                         }
1568                         ++fields;
1569                 } while (sep == L':');
1570
1571                 if (fields < 3)
1572                         return (ARCHIVE_WARN);
1573
1574                 /* Check for a numeric ID in field 1 or 3. */
1575                 id = -1;
1576                 isint_w(field[1].start, field[1].end, &id);
1577                 /* Field 3 is optional. */
1578                 if (id == -1 && fields > 3)
1579                         isint_w(field[3].start, field[3].end, &id);
1580
1581                 /* Parse the permissions from field 2. */
1582                 permset = 0;
1583                 p = field[2].start;
1584                 while (p < field[2].end) {
1585                         switch (*p++) {
1586                         case 'r': case 'R':
1587                                 permset |= ARCHIVE_ENTRY_ACL_READ;
1588                                 break;
1589                         case 'w': case 'W':
1590                                 permset |= ARCHIVE_ENTRY_ACL_WRITE;
1591                                 break;
1592                         case 'x': case 'X':
1593                                 permset |= ARCHIVE_ENTRY_ACL_EXECUTE;
1594                                 break;
1595                         case '-':
1596                                 break;
1597                         default:
1598                                 return (ARCHIVE_WARN);
1599                         }
1600                 }
1601
1602                 /*
1603                  * Solaris extension:  "defaultuser::rwx" is the
1604                  * default ACL corresponding to "user::rwx", etc.
1605                  */
1606                 if (field[0].end-field[0].start > 7
1607                     && wmemcmp(field[0].start, L"default", 7) == 0) {
1608                         type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
1609                         field[0].start += 7;
1610                 } else
1611                         type = default_type;
1612
1613                 if (prefix_w(field[0].start, field[0].end, L"user")) {
1614                         if (id != -1 || field[1].start < field[1].end)
1615                                 tag = ARCHIVE_ENTRY_ACL_USER;
1616                         else
1617                                 tag = ARCHIVE_ENTRY_ACL_USER_OBJ;
1618                 } else if (prefix_w(field[0].start, field[0].end, L"group")) {
1619                         if (id != -1 || field[1].start < field[1].end)
1620                                 tag = ARCHIVE_ENTRY_ACL_GROUP;
1621                         else
1622                                 tag = ARCHIVE_ENTRY_ACL_GROUP_OBJ;
1623                 } else if (prefix_w(field[0].start, field[0].end, L"other")) {
1624                         if (id != -1 || field[1].start < field[1].end)
1625                                 return (ARCHIVE_WARN);
1626                         tag = ARCHIVE_ENTRY_ACL_OTHER;
1627                 } else if (prefix_w(field[0].start, field[0].end, L"mask")) {
1628                         if (id != -1 || field[1].start < field[1].end)
1629                                 return (ARCHIVE_WARN);
1630                         tag = ARCHIVE_ENTRY_ACL_MASK;
1631                 } else
1632                         return (ARCHIVE_WARN);
1633
1634                 /* Add entry to the internal list. */
1635                 archive_entry_acl_add_entry_w_len(entry, type, permset,
1636                     tag, id, field[1].start, field[1].end - field[1].start);
1637         }
1638         return (ARCHIVE_OK);
1639 }
1640
1641 /*
1642  * extended attribute handling
1643  */
1644
1645 void
1646 archive_entry_xattr_clear(struct archive_entry *entry)
1647 {
1648         struct ae_xattr *xp;
1649
1650         while (entry->xattr_head != NULL) {
1651                 xp = entry->xattr_head->next;
1652                 free(entry->xattr_head->name);
1653                 free(entry->xattr_head->value);
1654                 free(entry->xattr_head);
1655                 entry->xattr_head = xp;
1656         }
1657
1658         entry->xattr_head = NULL;
1659 }
1660
1661 void
1662 archive_entry_xattr_add_entry(struct archive_entry *entry,
1663         const char *name, const void *value, size_t size)
1664 {
1665         struct ae_xattr *xp;
1666
1667         for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1668                 ;
1669
1670         if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
1671                 /* XXX Error XXX */
1672                 return;
1673
1674         xp->name = strdup(name);
1675         if ((xp->value = malloc(size)) != NULL) {
1676                 memcpy(xp->value, value, size);
1677                 xp->size = size;
1678         } else
1679                 xp->size = 0;
1680
1681         xp->next = entry->xattr_head;
1682         entry->xattr_head = xp;
1683 }
1684
1685
1686 /*
1687  * returns number of the extended attribute entries
1688  */
1689 int
1690 archive_entry_xattr_count(struct archive_entry *entry)
1691 {
1692         struct ae_xattr *xp;
1693         int count = 0;
1694
1695         for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
1696                 count++;
1697
1698         return count;
1699 }
1700
1701 int
1702 archive_entry_xattr_reset(struct archive_entry * entry)
1703 {
1704         entry->xattr_p = entry->xattr_head;
1705
1706         return archive_entry_xattr_count(entry);
1707 }
1708
1709 int
1710 archive_entry_xattr_next(struct archive_entry * entry,
1711         const char **name, const void **value, size_t *size)
1712 {
1713         if (entry->xattr_p) {
1714                 *name = entry->xattr_p->name;
1715                 *value = entry->xattr_p->value;
1716                 *size = entry->xattr_p->size;
1717
1718                 entry->xattr_p = entry->xattr_p->next;
1719
1720                 return (ARCHIVE_OK);
1721         } else {
1722                 *name = NULL;
1723                 *value = NULL;
1724                 *size = (size_t)0;
1725                 return (ARCHIVE_WARN);
1726         }
1727 }
1728
1729 /*
1730  * end of xattr handling
1731  */
1732
1733 /*
1734  * Parse a string to a positive decimal integer.  Returns true if
1735  * the string is non-empty and consists only of decimal digits,
1736  * false otherwise.
1737  */
1738 static int
1739 isint_w(const wchar_t *start, const wchar_t *end, int *result)
1740 {
1741         int n = 0;
1742         if (start >= end)
1743                 return (0);
1744         while (start < end) {
1745                 if (*start < '0' || *start > '9')
1746                         return (0);
1747                 if (n > (INT_MAX / 10))
1748                         n = INT_MAX;
1749                 else {
1750                         n *= 10;
1751                         n += *start - '0';
1752                 }
1753                 start++;
1754         }
1755         *result = n;
1756         return (1);
1757 }
1758
1759 /*
1760  * Match "[:whitespace:]*(.*)[:whitespace:]*[:,\n]".  *wp is updated
1761  * to point to just after the separator.  *start points to the first
1762  * character of the matched text and *end just after the last
1763  * character of the matched identifier.  In particular *end - *start
1764  * is the length of the field body, not including leading or trailing
1765  * whitespace.
1766  */
1767 static void
1768 next_field_w(const wchar_t **wp, const wchar_t **start,
1769     const wchar_t **end, wchar_t *sep)
1770 {
1771         /* Skip leading whitespace to find start of field. */
1772         while (**wp == L' ' || **wp == L'\t' || **wp == L'\n') {
1773                 (*wp)++;
1774         }
1775         *start = *wp;
1776
1777         /* Scan for the separator. */
1778         while (**wp != L'\0' && **wp != L',' && **wp != L':' &&
1779             **wp != L'\n') {
1780                 (*wp)++;
1781         }
1782         *sep = **wp;
1783
1784         /* Trim trailing whitespace to locate end of field. */
1785         *end = *wp - 1;
1786         while (**end == L' ' || **end == L'\t' || **end == L'\n') {
1787                 (*end)--;
1788         }
1789         (*end)++;
1790
1791         /* Adjust scanner location. */
1792         if (**wp != L'\0')
1793                 (*wp)++;
1794 }
1795
1796 /*
1797  * Return true if the characters [start...end) are a prefix of 'test'.
1798  * This makes it easy to handle the obvious abbreviations: 'u' for 'user', etc.
1799  */
1800 static int
1801 prefix_w(const wchar_t *start, const wchar_t *end, const wchar_t *test)
1802 {
1803         if (start == end)
1804                 return (0);
1805
1806         if (*start++ != *test++)
1807                 return (0);
1808
1809         while (start < end  &&  *start++ == *test++)
1810                 ;
1811
1812         if (start < end)
1813                 return (0);
1814
1815         return (1);
1816 }
1817
1818
1819 /*
1820  * Following code is modified from UC Berkeley sources, and
1821  * is subject to the following copyright notice.
1822  */
1823
1824 /*-
1825  * Copyright (c) 1993
1826  *      The Regents of the University of California.  All rights reserved.
1827  *
1828  * Redistribution and use in source and binary forms, with or without
1829  * modification, are permitted provided that the following conditions
1830  * are met:
1831  * 1. Redistributions of source code must retain the above copyright
1832  *    notice, this list of conditions and the following disclaimer.
1833  * 2. Redistributions in binary form must reproduce the above copyright
1834  *    notice, this list of conditions and the following disclaimer in the
1835  *    documentation and/or other materials provided with the distribution.
1836  * 4. Neither the name of the University nor the names of its contributors
1837  *    may be used to endorse or promote products derived from this software
1838  *    without specific prior written permission.
1839  *
1840  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1841  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1842  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1843  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1844  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1845  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1846  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1847  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1848  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1849  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1850  * SUCH DAMAGE.
1851  */
1852
1853 static struct flag {
1854         const char      *name;
1855         const wchar_t   *wname;
1856         unsigned long    set;
1857         unsigned long    clear;
1858 } flags[] = {
1859         /* Preferred (shorter) names per flag first, all prefixed by "no" */
1860 #ifdef SF_APPEND
1861         { "nosappnd",   L"nosappnd",            SF_APPEND,      0 },
1862         { "nosappend",  L"nosappend",           SF_APPEND,      0 },
1863 #endif
1864 #ifdef  EXT2_APPEND_FL                          /* 'a' */
1865         { "nosappnd",   L"nosappnd",            EXT2_APPEND_FL, 0 },
1866         { "nosappend",  L"nosappend",           EXT2_APPEND_FL, 0 },
1867 #endif
1868 #ifdef SF_ARCHIVED
1869         { "noarch",     L"noarch",              SF_ARCHIVED,    0 },
1870         { "noarchived", L"noarchived",          SF_ARCHIVED,    0 },
1871 #endif
1872 #ifdef SF_IMMUTABLE
1873         { "noschg",     L"noschg",              SF_IMMUTABLE,   0 },
1874         { "noschange",  L"noschange",           SF_IMMUTABLE,   0 },
1875         { "nosimmutable",       L"nosimmutable",        SF_IMMUTABLE,   0 },
1876 #endif
1877 #ifdef EXT2_IMMUTABLE_FL                        /* 'i' */
1878         { "noschg",     L"noschg",              EXT2_IMMUTABLE_FL,      0 },
1879         { "noschange",  L"noschange",           EXT2_IMMUTABLE_FL,      0 },
1880         { "nosimmutable",       L"nosimmutable",        EXT2_IMMUTABLE_FL,      0 },
1881 #endif
1882 #ifdef SF_NOUNLINK
1883         { "nosunlnk",   L"nosunlnk",            SF_NOUNLINK,    0 },
1884         { "nosunlink",  L"nosunlink",           SF_NOUNLINK,    0 },
1885 #endif
1886 #ifdef SF_SNAPSHOT
1887         { "nosnapshot", L"nosnapshot",  SF_SNAPSHOT,    0 },
1888 #endif
1889 #ifdef UF_APPEND
1890         { "nouappnd",   L"nouappnd",            UF_APPEND,      0 },
1891         { "nouappend",  L"nouappend",           UF_APPEND,      0 },
1892 #endif
1893 #ifdef UF_IMMUTABLE
1894         { "nouchg",     L"nouchg",              UF_IMMUTABLE,   0 },
1895         { "nouchange",  L"nouchange",           UF_IMMUTABLE,   0 },
1896         { "nouimmutable",       L"nouimmutable",        UF_IMMUTABLE,   0 },
1897 #endif
1898 #ifdef UF_NODUMP
1899         { "nodump",     L"nodump",              0,              UF_NODUMP},
1900 #endif
1901 #ifdef EXT2_NODUMP_FL                           /* 'd' */
1902         { "nodump",     L"nodump",              0,              EXT2_NODUMP_FL},
1903 #endif
1904 #ifdef UF_OPAQUE
1905         { "noopaque",   L"noopaque",            UF_OPAQUE,      0 },
1906 #endif
1907 #ifdef UF_NOUNLINK
1908         { "nouunlnk",   L"nouunlnk",            UF_NOUNLINK,    0 },
1909         { "nouunlink",  L"nouunlink",           UF_NOUNLINK,    0 },
1910 #endif
1911 #ifdef EXT2_COMPR_FL                            /* 'c' */
1912         { "nocompress", L"nocompress",          EXT2_COMPR_FL,  0 },
1913 #endif
1914
1915 #ifdef EXT2_NOATIME_FL                          /* 'A' */
1916         { "noatime",    L"noatime",             0,              EXT2_NOATIME_FL},
1917 #endif
1918         { NULL,         NULL,                   0,              0 }
1919 };
1920
1921 /*
1922  * fflagstostr --
1923  *      Convert file flags to a comma-separated string.  If no flags
1924  *      are set, return the empty string.
1925  */
1926 static char *
1927 ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
1928 {
1929         char *string, *dp;
1930         const char *sp;
1931         unsigned long bits;
1932         struct flag *flag;
1933         size_t  length;
1934
1935         bits = bitset | bitclear;
1936         length = 0;
1937         for (flag = flags; flag->name != NULL; flag++)
1938                 if (bits & (flag->set | flag->clear)) {
1939                         length += strlen(flag->name) + 1;
1940                         bits &= ~(flag->set | flag->clear);
1941                 }
1942
1943         if (length == 0)
1944                 return (NULL);
1945         string = (char *)malloc(length);
1946         if (string == NULL)
1947                 return (NULL);
1948
1949         dp = string;
1950         for (flag = flags; flag->name != NULL; flag++) {
1951                 if (bitset & flag->set || bitclear & flag->clear) {
1952                         sp = flag->name + 2;
1953                 } else if (bitset & flag->clear  ||  bitclear & flag->set) {
1954                         sp = flag->name;
1955                 } else
1956                         continue;
1957                 bitset &= ~(flag->set | flag->clear);
1958                 bitclear &= ~(flag->set | flag->clear);
1959                 if (dp > string)
1960                         *dp++ = ',';
1961                 while ((*dp++ = *sp++) != '\0')
1962                         ;
1963                 dp--;
1964         }
1965
1966         *dp = '\0';
1967         return (string);
1968 }
1969
1970 /*
1971  * strtofflags --
1972  *      Take string of arguments and return file flags.  This
1973  *      version works a little differently than strtofflags(3).
1974  *      In particular, it always tests every token, skipping any
1975  *      unrecognized tokens.  It returns a pointer to the first
1976  *      unrecognized token, or NULL if every token was recognized.
1977  *      This version is also const-correct and does not modify the
1978  *      provided string.
1979  */
1980 static const char *
1981 ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
1982 {
1983         const char *start, *end;
1984         struct flag *flag;
1985         unsigned long set, clear;
1986         const char *failed;
1987
1988         set = clear = 0;
1989         start = s;
1990         failed = NULL;
1991         /* Find start of first token. */
1992         while (*start == '\t'  ||  *start == ' '  ||  *start == ',')
1993                 start++;
1994         while (*start != '\0') {
1995                 /* Locate end of token. */
1996                 end = start;
1997                 while (*end != '\0'  &&  *end != '\t'  &&
1998                     *end != ' '  &&  *end != ',')
1999                         end++;
2000                 for (flag = flags; flag->name != NULL; flag++) {
2001                         if (memcmp(start, flag->name, end - start) == 0) {
2002                                 /* Matched "noXXXX", so reverse the sense. */
2003                                 clear |= flag->set;
2004                                 set |= flag->clear;
2005                                 break;
2006                         } else if (memcmp(start, flag->name + 2, end - start)
2007                             == 0) {
2008                                 /* Matched "XXXX", so don't reverse. */
2009                                 set |= flag->set;
2010                                 clear |= flag->clear;
2011                                 break;
2012                         }
2013                 }
2014                 /* Ignore unknown flag names. */
2015                 if (flag->name == NULL  &&  failed == NULL)
2016                         failed = start;
2017
2018                 /* Find start of next token. */
2019                 start = end;
2020                 while (*start == '\t'  ||  *start == ' '  ||  *start == ',')
2021                         start++;
2022
2023         }
2024
2025         if (setp)
2026                 *setp = set;
2027         if (clrp)
2028                 *clrp = clear;
2029
2030         /* Return location of first failure. */
2031         return (failed);
2032 }
2033
2034 /*
2035  * wcstofflags --
2036  *      Take string of arguments and return file flags.  This
2037  *      version works a little differently than strtofflags(3).
2038  *      In particular, it always tests every token, skipping any
2039  *      unrecognized tokens.  It returns a pointer to the first
2040  *      unrecognized token, or NULL if every token was recognized.
2041  *      This version is also const-correct and does not modify the
2042  *      provided string.
2043  */
2044 static const wchar_t *
2045 ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
2046 {
2047         const wchar_t *start, *end;
2048         struct flag *flag;
2049         unsigned long set, clear;
2050         const wchar_t *failed;
2051
2052         set = clear = 0;
2053         start = s;
2054         failed = NULL;
2055         /* Find start of first token. */
2056         while (*start == L'\t'  ||  *start == L' '  ||  *start == L',')
2057                 start++;
2058         while (*start != L'\0') {
2059                 /* Locate end of token. */
2060                 end = start;
2061                 while (*end != L'\0'  &&  *end != L'\t'  &&
2062                     *end != L' '  &&  *end != L',')
2063                         end++;
2064                 for (flag = flags; flag->wname != NULL; flag++) {
2065                         if (wmemcmp(start, flag->wname, end - start) == 0) {
2066                                 /* Matched "noXXXX", so reverse the sense. */
2067                                 clear |= flag->set;
2068                                 set |= flag->clear;
2069                                 break;
2070                         } else if (wmemcmp(start, flag->wname + 2, end - start)
2071                             == 0) {
2072                                 /* Matched "XXXX", so don't reverse. */
2073                                 set |= flag->set;
2074                                 clear |= flag->clear;
2075                                 break;
2076                         }
2077                 }
2078                 /* Ignore unknown flag names. */
2079                 if (flag->wname == NULL  &&  failed == NULL)
2080                         failed = start;
2081
2082                 /* Find start of next token. */
2083                 start = end;
2084                 while (*start == L'\t'  ||  *start == L' '  ||  *start == L',')
2085                         start++;
2086
2087         }
2088
2089         if (setp)
2090                 *setp = set;
2091         if (clrp)
2092                 *clrp = clear;
2093
2094         /* Return location of first failure. */
2095         return (failed);
2096 }
2097
2098
2099 #ifdef TEST
2100 #include <stdio.h>
2101 int
2102 main(int argc, char **argv)
2103 {
2104         struct archive_entry *entry = archive_entry_new();
2105         unsigned long set, clear;
2106         const wchar_t *remainder;
2107
2108         remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
2109         archive_entry_fflags(entry, &set, &clear);
2110
2111         wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);
2112
2113         wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
2114         return (0);
2115 }
2116 #endif