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