summaryrefslogtreecommitdiffstats
path: root/Utilities/cmlibarchive/libarchive/archive_entry.c
blob: 30fb4566eb9a7a20ac45e4c386ac817aa6262785 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
/*-
 * Copyright (c) 2003-2007 Tim Kientzle
 * Copyright (c) 2016 Martin Matuska
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "archive_platform.h"
__FBSDID("$FreeBSD: head/lib/libarchive/archive_entry.c 201096 2009-12-28 02:41:27Z kientzle $");

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#define HAVE_MAJOR
#elif MAJOR_IN_SYSMACROS
#include <sys/sysmacros.h>
#define HAVE_MAJOR
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_LINUX_FS_H
#include <linux/fs.h>	/* for Linux file flags */
#endif
/*
 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
 * As the include guards don't agree, the order of include is important.
 */
#ifdef HAVE_LINUX_EXT2_FS_H
#include <linux/ext2_fs.h>	/* for Linux file flags */
#endif
#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
#include <ext2fs/ext2_fs.h>	/* for Linux file flags */
#endif
#include <stddef.h>
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif

#include "archive.h"
#include "archive_acl_private.h"
#include "archive_entry.h"
#include "archive_entry_locale.h"
#include "archive_private.h"
#include "archive_entry_private.h"

#if !defined(HAVE_MAJOR) && !defined(major)
/* Replacement for major/minor/makedev. */
#define	major(x) ((int)(0x00ff & ((x) >> 8)))
#define	minor(x) ((int)(0xffff00ff & (x)))
#define	makedev(maj,min) ((0xff00 & ((maj)<<8)) | (0xffff00ff & (min)))
#endif

/* Play games to come up with a suitable makedev() definition. */
#ifdef __QNXNTO__
/* QNX.  <sigh> */
#include <sys/netmgr.h>
#define ae_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
#elif defined makedev
/* There's a "makedev" macro. */
#define ae_makedev(maj, min) makedev((maj), (min))
#elif defined mkdev || ((defined _WIN32 || defined __WIN32__) && !defined(__CYGWIN__))
/* Windows. <sigh> */
#define ae_makedev(maj, min) mkdev((maj), (min))
#else
/* There's a "makedev" function. */
#define ae_makedev(maj, min) makedev((maj), (min))
#endif

/*
 * This adjustment is needed to support the following idiom for adding
 * 1000ns to the stored time:
 * archive_entry_set_atime(archive_entry_atime(),
 *                         archive_entry_atime_nsec() + 1000)
 * The additional if() here compensates for ambiguity in the C standard,
 * which permits two possible interpretations of a % b when a is negative.
 */
#define FIX_NS(t,ns) \
	do {	\
		t += ns / 1000000000; \
		ns %= 1000000000; \
		if (ns < 0) { --t; ns += 1000000000; } \
	} while (0)

static char *	 ae_fflagstostr(unsigned long bitset, unsigned long bitclear);
static const wchar_t	*ae_wcstofflags(const wchar_t *stringp,
		    unsigned long *setp, unsigned long *clrp);
static const char	*ae_strtofflags(const char *stringp,
		    unsigned long *setp, unsigned long *clrp);

#ifndef HAVE_WCSCPY
static wchar_t * wcscpy(wchar_t *s1, const wchar_t *s2)
{
	wchar_t *dest = s1;
	while ((*s1 = *s2) != L'\0')
		++s1, ++s2;
	return dest;
}
#endif
#ifndef HAVE_WCSLEN
static size_t wcslen(const wchar_t *s)
{
	const wchar_t *p = s;
	while (*p != L'\0')
		++p;
	return p - s;
}
#endif
#ifndef HAVE_WMEMCMP
/* Good enough for simple equality testing, but not for sorting. */
#define wmemcmp(a,b,i)  memcmp((a), (b), (i) * sizeof(wchar_t))
#endif

/****************************************************************************
 *
 * Public Interface
 *
 ****************************************************************************/

struct archive_entry *
archive_entry_clear(struct archive_entry *entry)
{
	if (entry == NULL)
		return (NULL);
	archive_mstring_clean(&entry->ae_fflags_text);
	archive_mstring_clean(&entry->ae_gname);
	archive_mstring_clean(&entry->ae_hardlink);
	archive_mstring_clean(&entry->ae_pathname);
	archive_mstring_clean(&entry->ae_sourcepath);
	archive_mstring_clean(&entry->ae_symlink);
	archive_mstring_clean(&entry->ae_uname);
	archive_entry_copy_mac_metadata(entry, NULL, 0);
	archive_acl_clear(&entry->acl);
	archive_entry_xattr_clear(entry);
	archive_entry_sparse_clear(entry);
	free(entry->stat);
	memset(entry, 0, sizeof(*entry));
	return entry;
}

struct archive_entry *
archive_entry_clone(struct archive_entry *entry)
{
	struct archive_entry *entry2;
	struct ae_xattr *xp;
	struct ae_sparse *sp;
	size_t s;
	const void *p;

	/* Allocate new structure and copy over all of the fields. */
	/* TODO: Should we copy the archive over?  Or require a new archive
	 * as an argument? */
	entry2 = archive_entry_new2(entry->archive);
	if (entry2 == NULL)
		return (NULL);
	entry2->ae_stat = entry->ae_stat;
	entry2->ae_fflags_set = entry->ae_fflags_set;
	entry2->ae_fflags_clear = entry->ae_fflags_clear;

	/* TODO: XXX If clone can have a different archive, what do we do here if
	 * character sets are different? XXX */
	archive_mstring_copy(&entry2->ae_fflags_text, &entry->ae_fflags_text);
	archive_mstring_copy(&entry2->ae_gname, &entry->ae_gname);
	archive_mstring_copy(&entry2->ae_hardlink, &entry->ae_hardlink);
	archive_mstring_copy(&entry2->ae_pathname, &entry->ae_pathname);
	archive_mstring_copy(&entry2->ae_sourcepath, &entry->ae_sourcepath);
	archive_mstring_copy(&entry2->ae_symlink, &entry->ae_symlink);
	entry2->ae_set = entry->ae_set;
	archive_mstring_copy(&entry2->ae_uname, &entry->ae_uname);

	/* Copy encryption status */
	entry2->encryption = entry->encryption;
	
	/* Copy ACL data over. */
	archive_acl_copy(&entry2->acl, &entry->acl);

	/* Copy Mac OS metadata. */
	p = archive_entry_mac_metadata(entry, &s);
	archive_entry_copy_mac_metadata(entry2, p, s);

	/* Copy xattr data over. */
	xp = entry->xattr_head;
	while (xp != NULL) {
		archive_entry_xattr_add_entry(entry2,
		    xp->name, xp->value, xp->size);
		xp = xp->next;
	}

	/* Copy sparse data over. */
	sp = entry->sparse_head;
	while (sp != NULL) {
		archive_entry_sparse_add_entry(entry2,
		    sp->offset, sp->length);
		sp = sp->next;
	}

	return (entry2);
}

void
archive_entry_free(struct archive_entry *entry)
{
	archive_entry_clear(entry);
	free(entry);
}

struct archive_entry *
archive_entry_new(void)
{
	return archive_entry_new2(NULL);
}

struct archive_entry *
archive_entry_new2(struct archive *a)
{
	struct archive_entry *entry;

	entry = (struct archive_entry *)calloc(1, sizeof(*entry));
	if (entry == NULL)
		return (NULL);
	entry->archive = a;
	return (entry);
}

/*
 * Functions for reading fields from an archive_entry.
 */

time_t
archive_entry_atime(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_atime);
}

long
archive_entry_atime_nsec(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_atime_nsec);
}

int
archive_entry_atime_is_set(struct archive_entry *entry)
{
	return (entry->ae_set & AE_SET_ATIME);
}

time_t
archive_entry_birthtime(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_birthtime);
}

long
archive_entry_birthtime_nsec(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_birthtime_nsec);
}

int
archive_entry_birthtime_is_set(struct archive_entry *entry)
{
	return (entry->ae_set & AE_SET_BIRTHTIME);
}

time_t
archive_entry_ctime(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_ctime);
}

int
archive_entry_ctime_is_set(struct archive_entry *entry)
{
	return (entry->ae_set & AE_SET_CTIME);
}

long
archive_entry_ctime_nsec(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_ctime_nsec);
}

dev_t
archive_entry_dev(struct archive_entry *entry)
{
	if (entry->ae_stat.aest_dev_is_broken_down)
		return ae_makedev(entry->ae_stat.aest_devmajor,
		    entry->ae_stat.aest_devminor);
	else
		return (entry->ae_stat.aest_dev);
}

int
archive_entry_dev_is_set(struct archive_entry *entry)
{
	return (entry->ae_set & AE_SET_DEV);
}

dev_t
archive_entry_devmajor(struct archive_entry *entry)
{
	if (entry->ae_stat.aest_dev_is_broken_down)
		return (entry->ae_stat.aest_devmajor);
	else
		return major(entry->ae_stat.aest_dev);
}

dev_t
archive_entry_devminor(struct archive_entry *entry)
{
	if (entry->ae_stat.aest_dev_is_broken_down)
		return (entry->ae_stat.aest_devminor);
	else
		return minor(entry->ae_stat.aest_dev);
}

mode_t
archive_entry_filetype(struct archive_entry *entry)
{
	return (AE_IFMT & entry->acl.mode);
}

void
archive_entry_fflags(struct archive_entry *entry,
    unsigned long *set, unsigned long *clear)
{
	*set = entry->ae_fflags_set;
	*clear = entry->ae_fflags_clear;
}

/*
 * Note: if text was provided, this just returns that text.  If you
 * really need the text to be rebuilt in a canonical form, set the
 * text, ask for the bitmaps, then set the bitmaps.  (Setting the
 * bitmaps clears any stored text.)  This design is deliberate: if
 * we're editing archives, we don't want to discard flags just because
 * they aren't supported on the current system.  The bitmap<->text
 * conversions are platform-specific (see below).
 */
const char *
archive_entry_fflags_text(struct archive_entry *entry)
{
	const char *f;
	char *p;

	if (archive_mstring_get_mbs(entry->archive,
	    &entry->ae_fflags_text, &f) == 0) {
		if (f != NULL)
			return (f);
	} else if (errno == ENOMEM)
		__archive_errx(1, "No memory");

	if (entry->ae_fflags_set == 0  &&  entry->ae_fflags_clear == 0)
		return (NULL);

	p = ae_fflagstostr(entry->ae_fflags_set, entry->ae_fflags_clear);
	if (p == NULL)
		return (NULL);

	archive_mstring_copy_mbs(&entry->ae_fflags_text, p);
	free(p);
	if (archive_mstring_get_mbs(entry->archive,
	    &entry->ae_fflags_text, &f) == 0)
		return (f);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

la_int64_t
archive_entry_gid(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_gid);
}

const char *
archive_entry_gname(struct archive_entry *entry)
{
	const char *p;
	if (archive_mstring_get_mbs(entry->archive, &entry->ae_gname, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

const char *
archive_entry_gname_utf8(struct archive_entry *entry)
{
	const char *p;
	if (archive_mstring_get_utf8(entry->archive, &entry->ae_gname, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}


const wchar_t *
archive_entry_gname_w(struct archive_entry *entry)
{
	const wchar_t *p;
	if (archive_mstring_get_wcs(entry->archive, &entry->ae_gname, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

int
_archive_entry_gname_l(struct archive_entry *entry,
    const char **p, size_t *len, struct archive_string_conv *sc)
{
	return (archive_mstring_get_mbs_l(&entry->ae_gname, p, len, sc));
}

const char *
archive_entry_hardlink(struct archive_entry *entry)
{
	const char *p;
	if ((entry->ae_set & AE_SET_HARDLINK) == 0)
		return (NULL);
	if (archive_mstring_get_mbs(
	    entry->archive, &entry->ae_hardlink, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

const char *
archive_entry_hardlink_utf8(struct archive_entry *entry)
{
	const char *p;
	if ((entry->ae_set & AE_SET_HARDLINK) == 0)
		return (NULL);
	if (archive_mstring_get_utf8(
	    entry->archive, &entry->ae_hardlink, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

const wchar_t *
archive_entry_hardlink_w(struct archive_entry *entry)
{
	const wchar_t *p;
	if ((entry->ae_set & AE_SET_HARDLINK) == 0)
		return (NULL);
	if (archive_mstring_get_wcs(
	    entry->archive, &entry->ae_hardlink, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

int
_archive_entry_hardlink_l(struct archive_entry *entry,
    const char **p, size_t *len, struct archive_string_conv *sc)
{
	if ((entry->ae_set & AE_SET_HARDLINK) == 0) {
		*p = NULL;
		*len = 0;
		return (0);
	}
	return (archive_mstring_get_mbs_l(&entry->ae_hardlink, p, len, sc));
}

la_int64_t
archive_entry_ino(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_ino);
}

int
archive_entry_ino_is_set(struct archive_entry *entry)
{
	return (entry->ae_set & AE_SET_INO);
}

la_int64_t
archive_entry_ino64(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_ino);
}

mode_t
archive_entry_mode(struct archive_entry *entry)
{
	return (entry->acl.mode);
}

time_t
archive_entry_mtime(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_mtime);
}

long
archive_entry_mtime_nsec(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_mtime_nsec);
}

int
archive_entry_mtime_is_set(struct archive_entry *entry)
{
	return (entry->ae_set & AE_SET_MTIME);
}

unsigned int
archive_entry_nlink(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_nlink);
}

const char *
archive_entry_pathname(struct archive_entry *entry)
{
	const char *p;
	if (archive_mstring_get_mbs(
	    entry->archive, &entry->ae_pathname, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

const char *
archive_entry_pathname_utf8(struct archive_entry *entry)
{
	const char *p;
	if (archive_mstring_get_utf8(
	    entry->archive, &entry->ae_pathname, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

const wchar_t *
archive_entry_pathname_w(struct archive_entry *entry)
{
	const wchar_t *p;
	if (archive_mstring_get_wcs(
	    entry->archive, &entry->ae_pathname, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

int
_archive_entry_pathname_l(struct archive_entry *entry,
    const char **p, size_t *len, struct archive_string_conv *sc)
{
	return (archive_mstring_get_mbs_l(&entry->ae_pathname, p, len, sc));
}

mode_t
archive_entry_perm(struct archive_entry *entry)
{
	return (~AE_IFMT & entry->acl.mode);
}

dev_t
archive_entry_rdev(struct archive_entry *entry)
{
	if (entry->ae_stat.aest_rdev_is_broken_down)
		return ae_makedev(entry->ae_stat.aest_rdevmajor,
		    entry->ae_stat.aest_rdevminor);
	else
		return (entry->ae_stat.aest_rdev);
}

dev_t
archive_entry_rdevmajor(struct archive_entry *entry)
{
	if (entry->ae_stat.aest_rdev_is_broken_down)
		return (entry->ae_stat.aest_rdevmajor);
	else
		return major(entry->ae_stat.aest_rdev);
}

dev_t
archive_entry_rdevminor(struct archive_entry *entry)
{
	if (entry->ae_stat.aest_rdev_is_broken_down)
		return (entry->ae_stat.aest_rdevminor);
	else
		return minor(entry->ae_stat.aest_rdev);
}

la_int64_t
archive_entry_size(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_size);
}

int
archive_entry_size_is_set(struct archive_entry *entry)
{
	return (entry->ae_set & AE_SET_SIZE);
}

const char *
archive_entry_sourcepath(struct archive_entry *entry)
{
	const char *p;
	if (archive_mstring_get_mbs(
	    entry->archive, &entry->ae_sourcepath, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

const wchar_t *
archive_entry_sourcepath_w(struct archive_entry *entry)
{
	const wchar_t *p;
	if (archive_mstring_get_wcs(
	    entry->archive, &entry->ae_sourcepath, &p) == 0)
		return (p);
	return (NULL);
}

const char *
archive_entry_symlink(struct archive_entry *entry)
{
	const char *p;
	if ((entry->ae_set & AE_SET_SYMLINK) == 0)
		return (NULL);
	if (archive_mstring_get_mbs(
	    entry->archive, &entry->ae_symlink, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

const char *
archive_entry_symlink_utf8(struct archive_entry *entry)
{
	const char *p;
	if ((entry->ae_set & AE_SET_SYMLINK) == 0)
		return (NULL);
	if (archive_mstring_get_utf8(
	    entry->archive, &entry->ae_symlink, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

const wchar_t *
archive_entry_symlink_w(struct archive_entry *entry)
{
	const wchar_t *p;
	if ((entry->ae_set & AE_SET_SYMLINK) == 0)
		return (NULL);
	if (archive_mstring_get_wcs(
	    entry->archive, &entry->ae_symlink, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

int
_archive_entry_symlink_l(struct archive_entry *entry,
    const char **p, size_t *len, struct archive_string_conv *sc)
{
	if ((entry->ae_set & AE_SET_SYMLINK) == 0) {
		*p = NULL;
		*len = 0;
		return (0);
	}
	return (archive_mstring_get_mbs_l( &entry->ae_symlink, p, len, sc));
}

la_int64_t
archive_entry_uid(struct archive_entry *entry)
{
	return (entry->ae_stat.aest_uid);
}

const char *
archive_entry_uname(struct archive_entry *entry)
{
	const char *p;
	if (archive_mstring_get_mbs(entry->archive, &entry->ae_uname, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

const char *
archive_entry_uname_utf8(struct archive_entry *entry)
{
	const char *p;
	if (archive_mstring_get_utf8(entry->archive, &entry->ae_uname, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

const wchar_t *
archive_entry_uname_w(struct archive_entry *entry)
{
	const wchar_t *p;
	if (archive_mstring_get_wcs(entry->archive, &entry->ae_uname, &p) == 0)
		return (p);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (NULL);
}

int
_archive_entry_uname_l(struct archive_entry *entry,
    const char **p, size_t *len, struct archive_string_conv *sc)
{
	return (archive_mstring_get_mbs_l(&entry->ae_uname, p, len, sc));
}

int
archive_entry_is_data_encrypted(struct archive_entry *entry)
{
	return ((entry->encryption & AE_ENCRYPTION_DATA) == AE_ENCRYPTION_DATA);
}

int
archive_entry_is_metadata_encrypted(struct archive_entry *entry)
{
	return ((entry->encryption & AE_ENCRYPTION_METADATA) == AE_ENCRYPTION_METADATA);
}

int
archive_entry_is_encrypted(struct archive_entry *entry)
{
	return (entry->encryption & (AE_ENCRYPTION_DATA|AE_ENCRYPTION_METADATA));
}

/*
 * Functions to set archive_entry properties.
 */

void
archive_entry_set_filetype(struct archive_entry *entry, unsigned int type)
{
	entry->stat_valid = 0;
	entry->acl.mode &= ~AE_IFMT;
	entry->acl.mode |= AE_IFMT & type;
}

void
archive_entry_set_fflags(struct archive_entry *entry,
    unsigned long set, unsigned long clear)
{
	archive_mstring_clean(&entry->ae_fflags_text);
	entry->ae_fflags_set = set;
	entry->ae_fflags_clear = clear;
}

const char *
archive_entry_copy_fflags_text(struct archive_entry *entry,
    const char *flags)
{
	archive_mstring_copy_mbs(&entry->ae_fflags_text, flags);
	return (ae_strtofflags(flags,
		    &entry->ae_fflags_set, &entry->ae_fflags_clear));
}

const wchar_t *
archive_entry_copy_fflags_text_w(struct archive_entry *entry,
    const wchar_t *flags)
{
	archive_mstring_copy_wcs(&entry->ae_fflags_text, flags);
	return (ae_wcstofflags(flags,
		    &entry->ae_fflags_set, &entry->ae_fflags_clear));
}

void
archive_entry_set_gid(struct archive_entry *entry, la_int64_t g)
{
	entry->stat_valid = 0;
	entry->ae_stat.aest_gid = g;
}

void
archive_entry_set_gname(struct archive_entry *entry, const char *name)
{
	archive_mstring_copy_mbs(&entry->ae_gname, name);
}

void
archive_entry_set_gname_utf8(struct archive_entry *entry, const char *name)
{
	archive_mstring_copy_utf8(&entry->ae_gname, name);
}

void
archive_entry_copy_gname(struct archive_entry *entry, const char *name)
{
	archive_mstring_copy_mbs(&entry->ae_gname, name);
}

void
archive_entry_copy_gname_w(struct archive_entry *entry, const wchar_t *name)
{
	archive_mstring_copy_wcs(&entry->ae_gname, name);
}

int
archive_entry_update_gname_utf8(struct archive_entry *entry, const char *name)
{
	if (archive_mstring_update_utf8(entry->archive,
	    &entry->ae_gname, name) == 0)
		return (1);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (0);
}

int
_archive_entry_copy_gname_l(struct archive_entry *entry,
    const char *name, size_t len, struct archive_string_conv *sc)
{
	return (archive_mstring_copy_mbs_len_l(&entry->ae_gname, name, len, sc));
}

void
archive_entry_set_ino(struct archive_entry *entry, la_int64_t ino)
{
	entry->stat_valid = 0;
	entry->ae_set |= AE_SET_INO;
	entry->ae_stat.aest_ino = ino;
}

void
archive_entry_set_ino64(struct archive_entry *entry, la_int64_t ino)
{
	entry->stat_valid = 0;
	entry->ae_set |= AE_SET_INO;
	entry->ae_stat.aest_ino = ino;
}

void
archive_entry_set_hardlink(struct archive_entry *entry, const char *target)
{
	archive_mstring_copy_mbs(&entry->ae_hardlink, target);
	if (target != NULL)
		entry->ae_set |= AE_SET_HARDLINK;
	else
		entry->ae_set &= ~AE_SET_HARDLINK;
}

void
archive_entry_set_hardlink_utf8(struct archive_entry *entry, const char *target)
{
	archive_mstring_copy_utf8(&entry->ae_hardlink, target);
	if (target != NULL)
		entry->ae_set |= AE_SET_HARDLINK;
	else
		entry->ae_set &= ~AE_SET_HARDLINK;
}

void
archive_entry_copy_hardlink(struct archive_entry *entry, const char *target)
{
	archive_mstring_copy_mbs(&entry->ae_hardlink, target);
	if (target != NULL)
		entry->ae_set |= AE_SET_HARDLINK;
	else
		entry->ae_set &= ~AE_SET_HARDLINK;
}

void
archive_entry_copy_hardlink_w(struct archive_entry *entry, const wchar_t *target)
{
	archive_mstring_copy_wcs(&entry->ae_hardlink, target);
	if (target != NULL)
		entry->ae_set |= AE_SET_HARDLINK;
	else
		entry->ae_set &= ~AE_SET_HARDLINK;
}

int
archive_entry_update_hardlink_utf8(struct archive_entry *entry, const char *target)
{
	if (target != NULL)
		entry->ae_set |= AE_SET_HARDLINK;
	else
		entry->ae_set &= ~AE_SET_HARDLINK;
	if (archive_mstring_update_utf8(entry->archive,
	    &entry->ae_hardlink, target) == 0)
		return (1);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (0);
}

int
_archive_entry_copy_hardlink_l(struct archive_entry *entry,
    const char *target, size_t len, struct archive_string_conv *sc)
{
	int r;

	r = archive_mstring_copy_mbs_len_l(&entry->ae_hardlink,
	    target, len, sc);
	if (target != NULL && r == 0)
		entry->ae_set |= AE_SET_HARDLINK;
	else
		entry->ae_set &= ~AE_SET_HARDLINK;
	return (r);
}

void
archive_entry_set_atime(struct archive_entry *entry, time_t t, long ns)
{
	FIX_NS(t, ns);
	entry->stat_valid = 0;
	entry->ae_set |= AE_SET_ATIME;
	entry->ae_stat.aest_atime = t;
	entry->ae_stat.aest_atime_nsec = ns;
}

void
archive_entry_unset_atime(struct archive_entry *entry)
{
	archive_entry_set_atime(entry, 0, 0);
	entry->ae_set &= ~AE_SET_ATIME;
}

void
archive_entry_set_birthtime(struct archive_entry *entry, time_t t, long ns)
{
	FIX_NS(t, ns);
	entry->stat_valid = 0;
	entry->ae_set |= AE_SET_BIRTHTIME;
	entry->ae_stat.aest_birthtime = t;
	entry->ae_stat.aest_birthtime_nsec = ns;
}

void
archive_entry_unset_birthtime(struct archive_entry *entry)
{
	archive_entry_set_birthtime(entry, 0, 0);
	entry->ae_set &= ~AE_SET_BIRTHTIME;
}

void
archive_entry_set_ctime(struct archive_entry *entry, time_t t, long ns)
{
	FIX_NS(t, ns);
	entry->stat_valid = 0;
	entry->ae_set |= AE_SET_CTIME;
	entry->ae_stat.aest_ctime = t;
	entry->ae_stat.aest_ctime_nsec = ns;
}

void
archive_entry_unset_ctime(struct archive_entry *entry)
{
	archive_entry_set_ctime(entry, 0, 0);
	entry->ae_set &= ~AE_SET_CTIME;
}

void
archive_entry_set_dev(struct archive_entry *entry, dev_t d)
{
	entry->stat_valid = 0;
	entry->ae_set |= AE_SET_DEV;
	entry->ae_stat.aest_dev_is_broken_down = 0;
	entry->ae_stat.aest_dev = d;
}

void
archive_entry_set_devmajor(struct archive_entry *entry, dev_t m)
{
	entry->stat_valid = 0;
	entry->ae_set |= AE_SET_DEV;
	entry->ae_stat.aest_dev_is_broken_down = 1;
	entry->ae_stat.aest_devmajor = m;
}

void
archive_entry_set_devminor(struct archive_entry *entry, dev_t m)
{
	entry->stat_valid = 0;
	entry->ae_set |= AE_SET_DEV;
	entry->ae_stat.aest_dev_is_broken_down = 1;
	entry->ae_stat.aest_devminor = m;
}

/* Set symlink if symlink is already set, else set hardlink. */
void
archive_entry_set_link(struct archive_entry *entry, const char *target)
{
	if (entry->ae_set & AE_SET_SYMLINK)
		archive_mstring_copy_mbs(&entry->ae_symlink, target);
	else
		archive_mstring_copy_mbs(&entry->ae_hardlink, target);
}

void
archive_entry_set_link_utf8(struct archive_entry *entry, const char *target)
{
	if (entry->ae_set & AE_SET_SYMLINK)
		archive_mstring_copy_utf8(&entry->ae_symlink, target);
	else
		archive_mstring_copy_utf8(&entry->ae_hardlink, target);
}

/* Set symlink if symlink is already set, else set hardlink. */
void
archive_entry_copy_link(struct archive_entry *entry, const char *target)
{
	if (entry->ae_set & AE_SET_SYMLINK)
		archive_mstring_copy_mbs(&entry->ae_symlink, target);
	else
		archive_mstring_copy_mbs(&entry->ae_hardlink, target);
}

/* Set symlink if symlink is already set, else set hardlink. */
void
archive_entry_copy_link_w(struct archive_entry *entry, const wchar_t *target)
{
	if (entry->ae_set & AE_SET_SYMLINK)
		archive_mstring_copy_wcs(&entry->ae_symlink, target);
	else
		archive_mstring_copy_wcs(&entry->ae_hardlink, target);
}

int
archive_entry_update_link_utf8(struct archive_entry *entry, const char *target)
{
	int r;
	if (entry->ae_set & AE_SET_SYMLINK)
		r = archive_mstring_update_utf8(entry->archive,
		    &entry->ae_symlink, target);
	else
		r = archive_mstring_update_utf8(entry->archive,
		    &entry->ae_hardlink, target);
	if (r == 0)
		return (1);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (0);
}

int
_archive_entry_copy_link_l(struct archive_entry *entry,
    const char *target, size_t len, struct archive_string_conv *sc)
{
	int r;

	if (entry->ae_set & AE_SET_SYMLINK)
		r = archive_mstring_copy_mbs_len_l(&entry->ae_symlink,
		    target, len, sc);
	else
		r = archive_mstring_copy_mbs_len_l(&entry->ae_hardlink,
		    target, len, sc);
	return (r);
}

void
archive_entry_set_mode(struct archive_entry *entry, mode_t m)
{
	entry->stat_valid = 0;
	entry->acl.mode = m;
}

void
archive_entry_set_mtime(struct archive_entry *entry, time_t t, long ns)
{
	FIX_NS(t, ns);
	entry->stat_valid = 0;
	entry->ae_set |= AE_SET_MTIME;
	entry->ae_stat.aest_mtime = t;
	entry->ae_stat.aest_mtime_nsec = ns;
}

void
archive_entry_unset_mtime(struct archive_entry *entry)
{
	archive_entry_set_mtime(entry, 0, 0);
	entry->ae_set &= ~AE_SET_MTIME;
}

void
archive_entry_set_nlink(struct archive_entry *entry, unsigned int nlink)
{
	entry->stat_valid = 0;
	entry->ae_stat.aest_nlink = nlink;
}

void
archive_entry_set_pathname(struct archive_entry *entry, const char *name)
{
	archive_mstring_copy_mbs(&entry->ae_pathname, name);
}

void
archive_entry_set_pathname_utf8(struct archive_entry *entry, const char *name)
{
	archive_mstring_copy_utf8(&entry->ae_pathname, name);
}

void
archive_entry_copy_pathname(struct archive_entry *entry, const char *name)
{
	archive_mstring_copy_mbs(&entry->ae_pathname, name);
}

void
archive_entry_copy_pathname_w(struct archive_entry *entry, const wchar_t *name)
{
	archive_mstring_copy_wcs(&entry->ae_pathname, name);
}

int
archive_entry_update_pathname_utf8(struct archive_entry *entry, const char *name)
{
	if (archive_mstring_update_utf8(entry->archive,
	    &entry->ae_pathname, name) == 0)
		return (1);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (0);
}

int
_archive_entry_copy_pathname_l(struct archive_entry *entry,
    const char *name, size_t len, struct archive_string_conv *sc)
{
	return (archive_mstring_copy_mbs_len_l(&entry->ae_pathname,
	    name, len, sc));
}

void
archive_entry_set_perm(struct archive_entry *entry, mode_t p)
{
	entry->stat_valid = 0;
	entry->acl.mode &= AE_IFMT;
	entry->acl.mode |= ~AE_IFMT & p;
}

void
archive_entry_set_rdev(struct archive_entry *entry, dev_t m)
{
	entry->stat_valid = 0;
	entry->ae_stat.aest_rdev = m;
	entry->ae_stat.aest_rdev_is_broken_down = 0;
}

void
archive_entry_set_rdevmajor(struct archive_entry *entry, dev_t m)
{
	entry->stat_valid = 0;
	entry->ae_stat.aest_rdev_is_broken_down = 1;
	entry->ae_stat.aest_rdevmajor = m;
}

void
archive_entry_set_rdevminor(struct archive_entry *entry, dev_t m)
{
	entry->stat_valid = 0;
	entry->ae_stat.aest_rdev_is_broken_down = 1;
	entry->ae_stat.aest_rdevminor = m;
}

void
archive_entry_set_size(struct archive_entry *entry, la_int64_t s)
{
	entry->stat_valid = 0;
	entry->ae_stat.aest_size = s;
	entry->ae_set |= AE_SET_SIZE;
}

void
archive_entry_unset_size(struct archive_entry *entry)
{
	archive_entry_set_size(entry, 0);
	entry->ae_set &= ~AE_SET_SIZE;
}

void
archive_entry_copy_sourcepath(struct archive_entry *entry, const char *path)
{
	archive_mstring_copy_mbs(&entry->ae_sourcepath, path);
}

void
archive_entry_copy_sourcepath_w(struct archive_entry *entry, const wchar_t *path)
{
	archive_mstring_copy_wcs(&entry->ae_sourcepath, path);
}

void
archive_entry_set_symlink(struct archive_entry *entry, const char *linkname)
{
	archive_mstring_copy_mbs(&entry->ae_symlink, linkname);
	if (linkname != NULL)
		entry->ae_set |= AE_SET_SYMLINK;
	else
		entry->ae_set &= ~AE_SET_SYMLINK;
}

void
archive_entry_set_symlink_utf8(struct archive_entry *entry, const char *linkname)
{
	archive_mstring_copy_utf8(&entry->ae_symlink, linkname);
	if (linkname != NULL)
		entry->ae_set |= AE_SET_SYMLINK;
	else
		entry->ae_set &= ~AE_SET_SYMLINK;
}

void
archive_entry_copy_symlink(struct archive_entry *entry, const char *linkname)
{
	archive_mstring_copy_mbs(&entry->ae_symlink, linkname);
	if (linkname != NULL)
		entry->ae_set |= AE_SET_SYMLINK;
	else
		entry->ae_set &= ~AE_SET_SYMLINK;
}

void
archive_entry_copy_symlink_w(struct archive_entry *entry, const wchar_t *linkname)
{
	archive_mstring_copy_wcs(&entry->ae_symlink, linkname);
	if (linkname != NULL)
		entry->ae_set |= AE_SET_SYMLINK;
	else
		entry->ae_set &= ~AE_SET_SYMLINK;
}

int
archive_entry_update_symlink_utf8(struct archive_entry *entry, const char *linkname)
{
	if (linkname != NULL)
		entry->ae_set |= AE_SET_SYMLINK;
	else
		entry->ae_set &= ~AE_SET_SYMLINK;
	if (archive_mstring_update_utf8(entry->archive,
	    &entry->ae_symlink, linkname) == 0)
		return (1);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (0);
}

int
_archive_entry_copy_symlink_l(struct archive_entry *entry,
    const char *linkname, size_t len, struct archive_string_conv *sc)
{
	int r;

	r = archive_mstring_copy_mbs_len_l(&entry->ae_symlink,
	    linkname, len, sc);
	if (linkname != NULL && r == 0)
		entry->ae_set |= AE_SET_SYMLINK;
	else
		entry->ae_set &= ~AE_SET_SYMLINK;
	return (r);
}

void
archive_entry_set_uid(struct archive_entry *entry, la_int64_t u)
{
	entry->stat_valid = 0;
	entry->ae_stat.aest_uid = u;
}

void
archive_entry_set_uname(struct archive_entry *entry, const char *name)
{
	archive_mstring_copy_mbs(&entry->ae_uname, name);
}

void
archive_entry_set_uname_utf8(struct archive_entry *entry, const char *name)
{
	archive_mstring_copy_utf8(&entry->ae_uname, name);
}

void
archive_entry_copy_uname(struct archive_entry *entry, const char *name)
{
	archive_mstring_copy_mbs(&entry->ae_uname, name);
}

void
archive_entry_copy_uname_w(struct archive_entry *entry, const wchar_t *name)
{
	archive_mstring_copy_wcs(&entry->ae_uname, name);
}

int
archive_entry_update_uname_utf8(struct archive_entry *entry, const char *name)
{
	if (archive_mstring_update_utf8(entry->archive,
	    &entry->ae_uname, name) == 0)
		return (1);
	if (errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (0);
}

void
archive_entry_set_is_data_encrypted(struct archive_entry *entry, char is_encrypted)
{
	if (is_encrypted) {
		entry->encryption |= AE_ENCRYPTION_DATA;
	} else {
		entry->encryption &= ~AE_ENCRYPTION_DATA;
	}
}

void
archive_entry_set_is_metadata_encrypted(struct archive_entry *entry, char is_encrypted)
{
	if (is_encrypted) {
		entry->encryption |= AE_ENCRYPTION_METADATA;
	} else {
		entry->encryption &= ~AE_ENCRYPTION_METADATA;
	}
}

int
_archive_entry_copy_uname_l(struct archive_entry *entry,
    const char *name, size_t len, struct archive_string_conv *sc)
{
	return (archive_mstring_copy_mbs_len_l(&entry->ae_uname,
	    name, len, sc));
}

const void *
archive_entry_mac_metadata(struct archive_entry *entry, size_t *s)
{
  *s = entry->mac_metadata_size;
  return entry->mac_metadata;
}

void
archive_entry_copy_mac_metadata(struct archive_entry *entry,
    const void *p, size_t s)
{
  free(entry->mac_metadata);
  if (p == NULL || s == 0) {
    entry->mac_metadata = NULL;
    entry->mac_metadata_size = 0;
  } else {
    entry->mac_metadata_size = s;
    entry->mac_metadata = malloc(s);
    if (entry->mac_metadata == NULL)
      abort();
    memcpy(entry->mac_metadata, p, s);
  }
}

/*
 * ACL management.  The following would, of course, be a lot simpler
 * if: 1) the last draft of POSIX.1e were a really thorough and
 * complete standard that addressed the needs of ACL archiving and 2)
 * everyone followed it faithfully.  Alas, neither is true, so the
 * following is a lot more complex than might seem necessary to the
 * uninitiated.
 */

struct archive_acl *
archive_entry_acl(struct archive_entry *entry)
{
	return &entry->acl;
}

void
archive_entry_acl_clear(struct archive_entry *entry)
{
	archive_acl_clear(&entry->acl);
}

/*
 * Add a single ACL entry to the internal list of ACL data.
 */
int
archive_entry_acl_add_entry(struct archive_entry *entry,
    int type, int permset, int tag, int id, const char *name)
{
	return archive_acl_add_entry(&entry->acl, type, permset, tag, id, name);
}

/*
 * As above, but with a wide-character name.
 */
int
archive_entry_acl_add_entry_w(struct archive_entry *entry,
    int type, int permset, int tag, int id, const wchar_t *name)
{
	return archive_acl_add_entry_w_len(&entry->acl,
	    type, permset, tag, id, name, wcslen(name));
}

/*
 * Return a bitmask of ACL types in an archive entry ACL list
 */
int
archive_entry_acl_types(struct archive_entry *entry)
{
	return (archive_acl_types(&entry->acl));
}

/*
 * Return a count of entries matching "want_type".
 */
int
archive_entry_acl_count(struct archive_entry *entry, int want_type)
{
	return archive_acl_count(&entry->acl, want_type);
}

/*
 * Prepare for reading entries from the ACL data.  Returns a count
 * of entries matching "want_type", or zero if there are no
 * non-extended ACL entries of that type.
 */
int
archive_entry_acl_reset(struct archive_entry *entry, int want_type)
{
	return archive_acl_reset(&entry->acl, want_type);
}

/*
 * Return the next ACL entry in the list.  Fake entries for the
 * standard permissions and include them in the returned list.
 */
int
archive_entry_acl_next(struct archive_entry *entry, int want_type, int *type,
    int *permset, int *tag, int *id, const char **name)
{
	int r;
	r = archive_acl_next(entry->archive, &entry->acl, want_type, type,
		permset, tag, id, name);
	if (r == ARCHIVE_FATAL && errno == ENOMEM)
		__archive_errx(1, "No memory");
	return (r);
}

/*
 * Generate a text version of the ACL. The flags parameter controls
 * the style of the generated ACL.
 */
wchar_t *
archive_entry_acl_to_text_w(struct archive_entry *entry, ssize_t *len,
    int flags)
{
	return (archive_acl_to_text_w(&entry->acl, len, flags,
	    entry->archive));
}

char *
archive_entry_acl_to_text(struct archive_entry *entry, ssize_t *len,
    int flags)
{
	return (archive_acl_to_text_l(&entry->acl, len, flags, NULL));
}

char *
_archive_entry_acl_to_text_l(struct archive_entry *entry, ssize_t *len,
   int flags, struct archive_string_conv *sc)
{
	return (archive_acl_to_text_l(&entry->acl, len, flags, sc));
}

/*
 * ACL text parser.
 */
int
archive_entry_acl_from_text_w(struct archive_entry *entry,
    const wchar_t *wtext, int type)
{
	return (archive_acl_from_text_w(&entry->acl, wtext, type));
}

int
archive_entry_acl_from_text(struct archive_entry *entry,
    const char *text, int type)
{
	return (archive_acl_from_text_l(&entry->acl, text, type, NULL));
}

int
_archive_entry_acl_from_text_l(struct archive_entry *entry, const char *text,
    int type, struct archive_string_conv *sc)
{
	return (archive_acl_from_text_l(&entry->acl, text, type, sc));
}

/* Deprecated */
static int
archive_entry_acl_text_compat(int *flags)
{
	if ((*flags & ARCHIVE_ENTRY_ACL_TYPE_POSIX1E) == 0)
		return (1);

	/* ABI compat with old ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID */
	if ((*flags & OLD_ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID) != 0)
		*flags |= ARCHIVE_ENTRY_ACL_STYLE_EXTRA_ID;

	/* ABI compat with old ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT */
	if ((*flags & OLD_ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT) != 0)
		*flags |=  ARCHIVE_ENTRY_ACL_STYLE_MARK_DEFAULT;

	*flags |= ARCHIVE_ENTRY_ACL_STYLE_SEPARATOR_COMMA;

	return (0);
}

/* Deprecated */
const wchar_t *
archive_entry_acl_text_w(struct archive_entry *entry, int flags)
{
	if (entry->acl.acl_text_w != NULL) {
		free(entry->acl.acl_text_w);
		entry->acl.acl_text_w = NULL;
	}
	if (archive_entry_acl_text_compat(&flags) == 0)
		entry->acl.acl_text_w = archive_acl_to_text_w(&entry->acl,
		    NULL, flags, entry->archive);
	return (entry->acl.acl_text_w);
}

/* Deprecated */
const char *
archive_entry_acl_text(struct archive_entry *entry, int flags)
{
	if (entry->acl.acl_text != NULL) {
		free(entry->acl.acl_text);
		entry->acl.acl_text = NULL;
	}
	if (archive_entry_acl_text_compat(&flags) == 0)
		entry->acl.acl_text = archive_acl_to_text_l(&entry->acl, NULL,
		    flags, NULL);

	return (entry->acl.acl_text);
}

/* Deprecated */
int
_archive_entry_acl_text_l(struct archive_entry *entry, int flags,
    const char **acl_text, size_t *len, struct archive_string_conv *sc)
{
	if (entry->acl.acl_text != NULL) {
		free(entry->acl.acl_text);
		entry->acl.acl_text = NULL;
        }

	if (archive_entry_acl_text_compat(&flags) == 0)
		entry->acl.acl_text = archive_acl_to_text_l(&entry->acl,
		    (ssize_t *)len, flags, sc);

	*acl_text = entry->acl.acl_text;

	return (0);
}

/*
 * Following code is modified from UC Berkeley sources, and
 * is subject to the following copyright notice.
 */

/*-
 * Copyright (c) 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

static const struct flag {
	const char	*name;
	const wchar_t	*wname;
	unsigned long	 set;
	unsigned long	 clear;
} flags[] = {
	/* Preferred (shorter) names per flag first, all prefixed by "no" */
#ifdef SF_APPEND
	{ "nosappnd",	L"nosappnd",		SF_APPEND,	0 },
	{ "nosappend",	L"nosappend",		SF_APPEND,	0 },
#endif
#if defined(FS_APPEND_FL)			/* 'a' */
	{ "nosappnd",	L"nosappnd",		FS_APPEND_FL,	0 },
	{ "nosappend",	L"nosappend",		FS_APPEND_FL,	0 },
#elif defined(EXT2_APPEND_FL)			/* 'a' */
	{ "nosappnd",	L"nosappnd",		EXT2_APPEND_FL,	0 },
	{ "nosappend",	L"nosappend",		EXT2_APPEND_FL,	0 },
#endif
#ifdef SF_ARCHIVED
	{ "noarch",	L"noarch",		SF_ARCHIVED,	0 },
	{ "noarchived",	L"noarchived",       	SF_ARCHIVED,	0 },
#endif
#ifdef SF_IMMUTABLE
	{ "noschg",	L"noschg",		SF_IMMUTABLE,	0 },
	{ "noschange",	L"noschange",		SF_IMMUTABLE,	0 },
	{ "nosimmutable",	L"nosimmutable",	SF_IMMUTABLE,	0 },
#endif
#if defined(FS_IMMUTABLE_FL)			/* 'i' */
	{ "noschg",	L"noschg",		FS_IMMUTABLE_FL,	0 },
	{ "noschange",	L"noschange",		FS_IMMUTABLE_FL,	0 },
	{ "nosimmutable",	L"nosimmutable",	FS_IMMUTABLE_FL,	0 },
#elif defined(EXT2_IMMUTABLE_FL)		/* 'i' */
	{ "noschg",	L"noschg",		EXT2_IMMUTABLE_FL,	0 },
	{ "noschange",	L"noschange",		EXT2_IMMUTABLE_FL,	0 },
	{ "nosimmutable",	L"nosimmutable",	EXT2_IMMUTABLE_FL,	0 },
#endif
#ifdef SF_NOUNLINK
	{ "nosunlnk",	L"nosunlnk",		SF_NOUNLINK,	0 },
	{ "nosunlink",	L"nosunlink",		SF_NOUNLINK,	0 },
#endif
#ifdef SF_SNAPSHOT
	{ "nosnapshot",	L"nosnapshot",	SF_SNAPSHOT,	0 },
#endif
#ifdef UF_APPEND
	{ "nouappnd",	L"nouappnd",		UF_APPEND,	0 },
	{ "nouappend",	L"nouappend",		UF_APPEND,	0 },
#endif
#ifdef UF_IMMUTABLE
	{ "nouchg",	L"nouchg",		UF_IMMUTABLE,	0 },
	{ "nouchange",	L"nouchange",		UF_IMMUTABLE,	0 },
	{ "nouimmutable",	L"nouimmutable",	UF_IMMUTABLE,	0 },
#endif
#ifdef UF_NODUMP
	{ "nodump",	L"nodump",		0,		UF_NODUMP},
#endif
#if defined(FS_NODUMP_FL)	/* 'd' */
	{ "nodump",	L"nodump",		0,		FS_NODUMP_FL},
#elif defined(EXT2_NODUMP_FL) 	/* 'd' */
	{ "nodump",	L"nodump",		0,		EXT2_NODUMP_FL},
#endif
#ifdef UF_OPAQUE
	{ "noopaque",	L"noopaque",		UF_OPAQUE,	0 },
#endif
#ifdef UF_NOUNLINK
	{ "nouunlnk",	L"nouunlnk",		UF_NOUNLINK,	0 },
	{ "nouunlink",	L"nouunlink",		UF_NOUNLINK,	0 },
#endif
#ifdef UF_COMPRESSED
	{ "nocompressed",L"nocompressed",	UF_COMPRESSED,	0 },
#endif
#ifdef UF_HIDDEN
	{ "nohidden",	L"nohidden",		UF_HIDDEN,	0 },
#endif
#if defined(FS_UNRM_FL)
        { "nouunlink",	L"nouunlink",		FS_UNRM_FL,	0},
#elif defined(EXT2_UNRM_FL)
        { "nouunlink",	L"nouunlink",		EXT2_UNRM_FL,	0},
#endif

#if defined(FS_BTREE_FL)
        { "nobtree",	L"nobtree",       	FS_BTREE_FL,	0 },
#elif defined(EXT2_BTREE_FL)
        { "nobtree",	L"nobtree",       	EXT2_BTREE_FL,	0 },
#endif

#if defined(FS_ECOMPR_FL)
        { "nocomperr",	L"nocomperr",       	FS_ECOMPR_FL,	0 },
#elif defined(EXT2_ECOMPR_FL)
        { "nocomperr",	L"nocomperr",       	EXT2_ECOMPR_FL,	0 },
#endif

#if defined(FS_COMPR_FL)			/* 'c' */
        { "nocompress",	L"nocompress",       	FS_COMPR_FL,	0 },
#elif defined(EXT2_COMPR_FL)			/* 'c' */
        { "nocompress",	L"nocompress",       	EXT2_COMPR_FL,	0 },
#endif

#if defined(FS_NOATIME_FL)			/* 'A' */
        { "noatime",	L"noatime",		0,		FS_NOATIME_FL},
#elif defined(EXT2_NOATIME_FL)			/* 'A' */
        { "noatime",	L"noatime",		0,		EXT2_NOATIME_FL},
#endif

#if defined(FS_DIRTY_FL)
        { "nocompdirty",L"nocompdirty",		FS_DIRTY_FL,		0},
#elif defined(EXT2_DIRTY_FL)
        { "nocompdirty",L"nocompdirty",		EXT2_DIRTY_FL,		0},
#endif

#if defined(FS_COMPRBLK_FL)
#if defined(FS_NOCOMPR_FL)
        { "nocomprblk",	L"nocomprblk",		FS_COMPRBLK_FL, FS_NOCOMPR_FL},
#else
        { "nocomprblk",	L"nocomprblk",		FS_COMPRBLK_FL,	0},
#endif
#elif defined(EXT2_COMPRBLK_FL)
#if defined(EXT2_NOCOMPR_FL)
        { "nocomprblk",	L"nocomprblk",		EXT2_COMPRBLK_FL, EXT2_NOCOMPR_FL},
#else
        { "nocomprblk",	L"nocomprblk",		EXT2_COMPRBLK_FL,	0},
#endif
#endif
#if defined(FS_DIRSYNC_FL)
        { "nodirsync",	L"nodirsync",		FS_DIRSYNC_FL,	0},
#elif defined(EXT2_DIRSYNC_FL)
        { "nodirsync",	L"nodirsync",		EXT2_DIRSYNC_FL,	0},
#endif
#if defined(FS_INDEX_FL)
        { "nohashidx",	L"nohashidx",		FS_INDEX_FL,		0},
#elif defined(EXT2_INDEX_FL)
        { "nohashidx",	L"nohashidx",		EXT2_INDEX_FL,		0},
#endif
#if defined(FS_IMAGIC_FL)
        { "noimagic",	L"noimagic",		FS_IMAGIC_FL,		0},
#elif defined(EXT2_IMAGIC_FL)
        { "noimagic",	L"noimagic",		EXT2_IMAGIC_FL,		0},
#endif
#if defined(FS_JOURNAL_DATA_FL)
        { "nojournal",	L"nojournal",		FS_JOURNAL_DATA_FL,	0},
#elif defined(EXT3_JOURNAL_DATA_FL)
        { "nojournal",	L"nojournal",		EXT3_JOURNAL_DATA_FL,	0},
#endif
#if defined(FS_SECRM_FL)
        { "nosecuredeletion",L"nosecuredeletion",FS_SECRM_FL,		0},
#elif defined(EXT2_SECRM_FL)
        { "nosecuredeletion",L"nosecuredeletion",EXT2_SECRM_FL,		0},
#endif
#if defined(FS_SYNC_FL)
        { "nosync",	L"nosync",		FS_SYNC_FL,		0},
#elif defined(EXT2_SYNC_FL)
        { "nosync",	L"nosync",		EXT2_SYNC_FL,		0},
#endif
#if defined(FS_NOTAIL_FL)
        { "notail",	L"notail",		0,		FS_NOTAIL_FL},
#elif defined(EXT2_NOTAIL_FL)
        { "notail",	L"notail",		0,		EXT2_NOTAIL_FL},
#endif
#if defined(FS_TOPDIR_FL)
        { "notopdir",	L"notopdir",		FS_TOPDIR_FL,		0},
#elif defined(EXT2_TOPDIR_FL)
        { "notopdir",	L"notopdir",		EXT2_TOPDIR_FL,		0},
#endif
#ifdef FS_ENCRYPT_FL
        { "noencrypt",	L"noencrypt",		FS_ENCRYPT_FL,	0},
#endif
#ifdef FS_HUGE_FILE_FL
        { "nohugefile",	L"nohugefile",		FS_HUGE_FILE_FL,	0},
#endif
#ifdef FS_EXTENT_FL
        { "noextent",	L"noextent",		FS_EXTENT_FL,	0},
#endif
#ifdef FS_EA_INODE_FL
        { "noeainode",	L"noeainode",		FS_EA_INODE_FL,	0},
#endif
#ifdef FS_EOFBLOCKS_FL
        { "noeofblocks",L"noeofblocks",		FS_EOFBLOCKS_FL,	0},
#endif
#ifdef FS_NOCOW_FL
        { "nocow",	L"nocow",		FS_NOCOW_FL,	0},
#endif
#ifdef FS_INLINE_DATA_FL
        { "noinlinedata",L"noinlinedata",	FS_INLINE_DATA_FL,	0},
#endif
#ifdef FS_PROJINHERIT_FL
        { "noprojinherit",L"noprojinherit",	FS_PROJINHERIT_FL,	0},
#endif
#if defined(FS_RESERVED_FL)
        { "noreserved",	L"noreserved",		FS_RESERVED_FL,	0},
#elif defined(EXT2_RESERVED_FL)
        { "noreserved",	L"noreserved",		EXT2_RESERVED_FL,	0},
#endif
	{ NULL,		NULL,			0,		0 }
};

/*
 * fflagstostr --
 *	Convert file flags to a comma-separated string.  If no flags
 *	are set, return the empty string.
 */
static char *
ae_fflagstostr(unsigned long bitset, unsigned long bitclear)
{
	char *string, *dp;
	const char *sp;
	unsigned long bits;
	const struct flag *flag;
	size_t	length;

	bits = bitset | bitclear;
	length = 0;
	for (flag = flags; flag->name != NULL; flag++)
		if (bits & (flag->set | flag->clear)) {
			length += strlen(flag->name) + 1;
			bits &= ~(flag->set | flag->clear);
		}

	if (length == 0)
		return (NULL);
	string = (char *)malloc(length);
	if (string == NULL)
		return (NULL);

	dp = string;
	for (flag = flags; flag->name != NULL; flag++) {
		if (bitset & flag->set || bitclear & flag->clear) {
			sp = flag->name + 2;
		} else if (bitset & flag->clear  ||  bitclear & flag->set) {
			sp = flag->name;
		} else
			continue;
		bitset &= ~(flag->set | flag->clear);
		bitclear &= ~(flag->set | flag->clear);
		if (dp > string)
			*dp++ = ',';
		while ((*dp++ = *sp++) != '\0')
			;
		dp--;
	}

	*dp = '\0';
	return (string);
}

/*
 * strtofflags --
 *	Take string of arguments and return file flags.  This
 *	version works a little differently than strtofflags(3).
 *	In particular, it always tests every token, skipping any
 *	unrecognized tokens.  It returns a pointer to the first
 *	unrecognized token, or NULL if every token was recognized.
 *	This version is also const-correct and does not modify the
 *	provided string.
 */
static const char *
ae_strtofflags(const char *s, unsigned long *setp, unsigned long *clrp)
{
	const char *start, *end;
	const struct flag *flag;
	unsigned long set, clear;
	const char *failed;

	set = clear = 0;
	start = s;
	failed = NULL;
	/* Find start of first token. */
	while (*start == '\t'  ||  *start == ' '  ||  *start == ',')
		start++;
	while (*start != '\0') {
		size_t length;
		/* Locate end of token. */
		end = start;
		while (*end != '\0'  &&  *end != '\t'  &&
		    *end != ' '  &&  *end != ',')
			end++;
		length = end - start;
		for (flag = flags; flag->name != NULL; flag++) {
			size_t flag_length = strlen(flag->name);
			if (length == flag_length
			    && memcmp(start, flag->name, length) == 0) {
				/* Matched "noXXXX", so reverse the sense. */
				clear |= flag->set;
				set |= flag->clear;
				break;
			} else if (length == flag_length - 2
			    && memcmp(start, flag->name + 2, length) == 0) {
				/* Matched "XXXX", so don't reverse. */
				set |= flag->set;
				clear |= flag->clear;
				break;
			}
		}
		/* Ignore unknown flag names. */
		if (flag->name == NULL  &&  failed == NULL)
			failed = start;

		/* Find start of next token. */
		start = end;
		while (*start == '\t'  ||  *start == ' '  ||  *start == ',')
			start++;

	}

	if (setp)
		*setp = set;
	if (clrp)
		*clrp = clear;

	/* Return location of first failure. */
	return (failed);
}

/*
 * wcstofflags --
 *	Take string of arguments and return file flags.  This
 *	version works a little differently than strtofflags(3).
 *	In particular, it always tests every token, skipping any
 *	unrecognized tokens.  It returns a pointer to the first
 *	unrecognized token, or NULL if every token was recognized.
 *	This version is also const-correct and does not modify the
 *	provided string.
 */
static const wchar_t *
ae_wcstofflags(const wchar_t *s, unsigned long *setp, unsigned long *clrp)
{
	const wchar_t *start, *end;
	const struct flag *flag;
	unsigned long set, clear;
	const wchar_t *failed;

	set = clear = 0;
	start = s;
	failed = NULL;
	/* Find start of first token. */
	while (*start == L'\t'  ||  *start == L' '  ||  *start == L',')
		start++;
	while (*start != L'\0') {
		size_t length;
		/* Locate end of token. */
		end = start;
		while (*end != L'\0'  &&  *end != L'\t'  &&
		    *end != L' '  &&  *end != L',')
			end++;
		length = end - start;
		for (flag = flags; flag->wname != NULL; flag++) {
			size_t flag_length = wcslen(flag->wname);
			if (length == flag_length
			    && wmemcmp(start, flag->wname, length) == 0) {
				/* Matched "noXXXX", so reverse the sense. */
				clear |= flag->set;
				set |= flag->clear;
				break;
			} else if (length == flag_length - 2
			    && wmemcmp(start, flag->wname + 2, length) == 0) {
				/* Matched "XXXX", so don't reverse. */
				set |= flag->set;
				clear |= flag->clear;
				break;
			}
		}
		/* Ignore unknown flag names. */
		if (flag->wname == NULL  &&  failed == NULL)
			failed = start;

		/* Find start of next token. */
		start = end;
		while (*start == L'\t'  ||  *start == L' '  ||  *start == L',')
			start++;

	}

	if (setp)
		*setp = set;
	if (clrp)
		*clrp = clear;

	/* Return location of first failure. */
	return (failed);
}


#ifdef TEST
#include <stdio.h>
int
main(int argc, char **argv)
{
	struct archive_entry *entry = archive_entry_new();
	unsigned long set, clear;
	const wchar_t *remainder;

	remainder = archive_entry_copy_fflags_text_w(entry, L"nosappnd dump archive,,,,,,,");
	archive_entry_fflags(entry, &set, &clear);

	wprintf(L"set=0x%lX clear=0x%lX remainder='%ls'\n", set, clear, remainder);

	wprintf(L"new flags='%s'\n", archive_entry_fflags_text(entry));
	return (0);
}
#endif