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