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