Check-in [2422ee3f48]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:more improvements in zipfs: mount from bytearray or open channel, doc update
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2422ee3f483368e6ffc7d2dbe0d0047ca37d2768
User & Date: chw 2019-09-23 04:43:33
Context
2019-09-24
04:59
add selected tk upstream changes check-in: 5a80b8cc36 user: chw tags: trunk
2019-09-23
04:45
merge with trunk check-in: ef359f628c user: chw tags: wtf-8-experiment
04:43
more improvements in zipfs: mount from bytearray or open channel, doc update check-in: 2422ee3f48 user: chw tags: trunk
2019-09-21
09:48
add selected tk upstream changes check-in: dfb619fdc9 user: chw tags: trunk
Changes

Changes to jni/tcl/doc/zipfs.3.

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
'\"
'\" Copyright (c) 2015 Jan Nijtmans <jan.nijtmans@gmail.com>
'\" Copyright (c) 2015 Christian Werner <chw@ch-werner.de>
'\"
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
.TH Tclzipfs 3 8.7 Tcl "Tcl Library Procedures"
.so man.macros
.BS
.SH NAME
Tclzipfs_Init, Tclzipfs_SafeInit, Tclzipfs_Mount, Tclzipfs_Unmount \- handle ZIP files as VFS
.SH SYNOPSIS
.nf
\fB#include <zipfs.h>\fR
.sp
int
\fBTclzipfs_Init\fR(\fIinterp\fR)
.sp
int
\fBTclzipfs_SafeInit\fR(\fIinterp\fR)
.sp
int
\fBTclzipfs_Mount\fR(\fIinterp, zipname, mntpt, passwd\fR)
.sp



int
\fBTclzipfs_Unmount\fR(\fIinterp, zipname\fR)
.SH ARGUMENTS
.AS Tcl_Interp **termPtr
.AP Tcl_Interp *interp in
Interpreter in which the zip file system is mounted.  The interpreter's result is
modified to hold the result or error message from the script.
.AP "const char" *zipname in
Name of a zipfile.
.AP "const char" *mntpt in
Name of a mount point.
.AP "const char" *passwd in
An (optional) password.






.BE
.SH DESCRIPTION
\fBTclzipfs_Init()\fR performs one-time initialization of the file system
and registers it process wide. Additionally, a package named \fIzipfs\fR
is provided and supplemental Tcl commands are created in the given
interpreter \fIinterp\fR.
.PP
\fBTclzipfs_SafeInit()\fR is the version of \fBTclzipfs_Init()\fR for
safe interpreters. It exposes only uncritical supplemental Tcl commands
in the given interpreter \fIinterp\fR.
.PP
\fBTclzipfs_Mount()\fR mount the ZIP archive \fIzipname\fR on the mount
point given in \fImntpt\fR using the optional ZIP password \fIpasswd\fR.
Errors during that process are reported in the interpreter \fIinterp\fR.
If \fIzipname\fR is a NULL pointer, information on all currently mounted
ZIP file systems is written into \fIinterp\fR's result as a sequence of
mount points and ZIP file names.









.PP
\fBTclzipfs_Unmount()\fR undoes the effect of \fBTclzipfs_Mount()\fR,
i.e. it unmounts the mounted ZIP archive file \fIzipname\fR. Errors are
reported in the interpreter \fIinterp\fR.
.SH KEYWORDS
compress, filesystem, zip


|








|













>
>
>













>
>
>
>
>
>











|





>
>
>
>
>
>
>
>
>






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
'\"
'\" Copyright (c) 2015 Jan Nijtmans <jan.nijtmans@gmail.com>
'\" Copyright (c) 2015-2019 Christian Werner <chw@ch-werner.de>
'\"
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
.TH Tclzipfs 3 8.7 Tcl "Tcl Library Procedures"
.so man.macros
.BS
.SH NAME
Tclzipfs_Init, Tclzipfs_SafeInit, Tclzipfs_Mount, Tclzipfs_MountBuffer, Tclzipfs_Unmount \- handle ZIP files as VFS
.SH SYNOPSIS
.nf
\fB#include <zipfs.h>\fR
.sp
int
\fBTclzipfs_Init\fR(\fIinterp\fR)
.sp
int
\fBTclzipfs_SafeInit\fR(\fIinterp\fR)
.sp
int
\fBTclzipfs_Mount\fR(\fIinterp, zipname, mntpt, passwd\fR)
.sp
int
\fBTclzipfs_MountBuffer\fR(\fIinterp, mntpt, data, length, copy\fR)
.sp
int
\fBTclzipfs_Unmount\fR(\fIinterp, zipname\fR)
.SH ARGUMENTS
.AS Tcl_Interp **termPtr
.AP Tcl_Interp *interp in
Interpreter in which the zip file system is mounted.  The interpreter's result is
modified to hold the result or error message from the script.
.AP "const char" *zipname in
Name of a zipfile.
.AP "const char" *mntpt in
Name of a mount point.
.AP "const char" *passwd in
An (optional) password.
.AP "unsigned char" *data in
Pointer to memory buffer representing a ZIP archive.
.AP "int" length in
Length of memory buffer representing a ZIP archive.
.AP "int" copy in
Flag, if true a private copy of the memory buffer is used.
.BE
.SH DESCRIPTION
\fBTclzipfs_Init()\fR performs one-time initialization of the file system
and registers it process wide. Additionally, a package named \fIzipfs\fR
is provided and supplemental Tcl commands are created in the given
interpreter \fIinterp\fR.
.PP
\fBTclzipfs_SafeInit()\fR is the version of \fBTclzipfs_Init()\fR for
safe interpreters. It exposes only uncritical supplemental Tcl commands
in the given interpreter \fIinterp\fR.
.PP
\fBTclzipfs_Mount()\fR mounts the ZIP archive \fIzipname\fR on the mount
point given in \fImntpt\fR using the optional ZIP password \fIpasswd\fR.
Errors during that process are reported in the interpreter \fIinterp\fR.
If \fIzipname\fR is a NULL pointer, information on all currently mounted
ZIP file systems is written into \fIinterp\fR's result as a sequence of
mount points and ZIP file names.
.PP
\fBTclzipfs_MountBuffer()\fR mounts the ZIP archive contained in the
memory buffer described by \fIdata\fR and \fIlength\fR on the mount
point given in \fImntpt\fR. Depending on \fIcopy\fR a private copy
of this memory buffer is made and used for the mount operation.
Errors during that process are reported in the interpreter \fIinterp\fR.
If \fImntpt\fR is a NULL pointer, information on all currently mounted
ZIP file systems is written into \fIinterp\fR's result as a sequence of
mount points and ZIP file names.
.PP
\fBTclzipfs_Unmount()\fR undoes the effect of \fBTclzipfs_Mount()\fR,
i.e. it unmounts the mounted ZIP archive file \fIzipname\fR. Errors are
reported in the interpreter \fIinterp\fR.
.SH KEYWORDS
compress, filesystem, zip

Changes to jni/tcl/doc/zipfs.n.

1
2
3
4
5
6
7
8
9
10
..
21
22
23
24
25
26
27




28
29
30
31
32
33
34
...
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
'\"
'\" Copyright (c) 2015 Jan Nijtmans <jan.nijtmans@gmail.com>
'\" Copyright (c) 2015-2018 Christian Werner <chw@ch-werner.de>
'\"
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
.TH zipfs n 1.0 Zipfs "zipfs Commands"
.so man.macros
.BS
................................................................................
\fBzipfs::list\fR \fB?(-glob|-regexp)?\fR \fI?pattern?\fR
\fBzipfs::lmkimg\fR \fIoutfile\fR \fIinlist\fR \fI?password?\fR \fI?infile?\fR
\fBzipfs::lmkzip\fR \fIoutfile\fR \fIinlist\fR \fI?password?\fR
\fBzipfs::mkimg\fR \fIoutfile\fR \fIindir\fR \fI?strip?\fR \fI?password?\fR \fI?infile?\fR
\fBzipfs::mkkey\fR \fIpassword\fR
\fBzipfs::mkzip\fR \fIoutfile\fR \fIindir\fR \fI?strip?\fR \fI?password?\fR
\fBzipfs::mount\fR \fI?zipfile\fR \fI?mountpoint?\fR \fI?password?\fR




\fBzipfs::unmount\fR \fIzipfile\fR
.fi
.BE
.SH DESCRIPTION
.PP
The \fBzipfs\fR package provides tcl with the ability to mount
the contents of a ZIP file as a virtual file system.
................................................................................
.PP
Caution: the choice of the \fIindir\fR parameter
(less the optional stripped prefix) determines the later root name of the
archive's content.
.RE
.TP
\fBzipfs::mount ?\fIzipfile\fR? ?\fImountpoint\fR? ?\fIpassword\fR?




.
The \fBzipfs::mount\fR command mounts a ZIP archive file as a VFS.
After this command executes, files contained in \fIzipfile\fR
will appear to Tcl to be regular files at the mount point.
.RS
.PP
With no \fImountpoint\fR, returns the mount point for \fIzipfile\fR.
With no \fIzipfile\fR, return all zipfile/mount point pairs.
If \fImountpoint\fR is specified as an empty string, the mount point
will be the current directory. If \fIpassword\fR is specified, files from
\fIzipfile\fR are decrypted using this password when read.

.RE










.TP
\fBzipfs::unmount \fIzipfile\fR
.
Unmounts a previously mounted ZIP archive file \fIzipfile\fR.
.PP
The commands described above are available as subcommands in the \fBzipfs\fR
ensemble, i.e. \fBzipfs list\fR is equivalent to \fBzipfs::list\fR.





.SH "SEE ALSO"
tclsh(1), file(n), zlib(n)
.SH "KEYWORDS"
compress, filesystem, zip
'\" Local Variables:
'\" mode: nroff
'\" End:


|







 







>
>
>
>







 







>
>
>
>






|
|
|
|
|
>

>
>
>
>
>
>
>
>
>
>







>
>
>
>
>







1
2
3
4
5
6
7
8
9
10
..
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
...
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
'\"
'\" Copyright (c) 2015 Jan Nijtmans <jan.nijtmans@gmail.com>
'\" Copyright (c) 2015-2019 Christian Werner <chw@ch-werner.de>
'\"
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
.TH zipfs n 1.0 Zipfs "zipfs Commands"
.so man.macros
.BS
................................................................................
\fBzipfs::list\fR \fB?(-glob|-regexp)?\fR \fI?pattern?\fR
\fBzipfs::lmkimg\fR \fIoutfile\fR \fIinlist\fR \fI?password?\fR \fI?infile?\fR
\fBzipfs::lmkzip\fR \fIoutfile\fR \fIinlist\fR \fI?password?\fR
\fBzipfs::mkimg\fR \fIoutfile\fR \fIindir\fR \fI?strip?\fR \fI?password?\fR \fI?infile?\fR
\fBzipfs::mkkey\fR \fIpassword\fR
\fBzipfs::mkzip\fR \fIoutfile\fR \fIindir\fR \fI?strip?\fR \fI?password?\fR
\fBzipfs::mount\fR \fI?zipfile\fR \fI?mountpoint?\fR \fI?password?\fR
\fBzipfs::mount\fR \fI\-data\fR \fIbytearray\fR \fImountpoint\fR
\fBzipfs::mount\fR \fI\-chan\fR \fIchannelId\fR \fImountpoint\fR
\fBzipfs::mount\fR \fI\-file\fR \fIzipfile\fR \fImountpoint\fR \fI?password?\fR
\fBzipfs::mount\fR \fI\-\-\fR \fIzipfile\fR \fImountpoint\fR \fI?password?\fR
\fBzipfs::unmount\fR \fIzipfile\fR
.fi
.BE
.SH DESCRIPTION
.PP
The \fBzipfs\fR package provides tcl with the ability to mount
the contents of a ZIP file as a virtual file system.
................................................................................
.PP
Caution: the choice of the \fIindir\fR parameter
(less the optional stripped prefix) determines the later root name of the
archive's content.
.RE
.TP
\fBzipfs::mount ?\fIzipfile\fR? ?\fImountpoint\fR? ?\fIpassword\fR?
.TP
\fBzipfs::mount \fI\-file\fR \fIzipfile\fR \fImountpoint\fR ?\fIpassword\fR?
.TP
\fBzipfs::mount \fI\-\-\fR \fIzipfile\fR \fImountpoint\fR ?\fIpassword\fR?
.
The \fBzipfs::mount\fR command mounts a ZIP archive file as a VFS.
After this command executes, files contained in \fIzipfile\fR
will appear to Tcl to be regular files at the mount point.
.RS
.PP
In the first command form, with no \fImountpoint\fR, returns the mount
point for \fIzipfile\fR. With no \fIzipfile\fR, return all zipfile/mount
point pairs. If \fImountpoint\fR is specified as an empty string,
the mount point will be the current directory. If \fIpassword\fR is
specified, files from \fIzipfile\fR are decrypted using this password
when read.
.RE
.TP
\fBzipfs::mount \fI\-data\fR \fIbytearray\fR \fImountpoint\fR
.
The data in \fIbytearray\fR must represent a ZIP archive which gets
mounted on \fImountpoint\fR.
.TP
\fBzipfs::mount \fI\-chan\fR \fIchannelId\fR \fImountpoint\fR
.
A ZIP archive is read from channel \fIchannelId\f and mounted on
\fImountpoint\fR.
.TP
\fBzipfs::unmount \fIzipfile\fR
.
Unmounts a previously mounted ZIP archive file \fIzipfile\fR.
.PP
The commands described above are available as subcommands in the \fBzipfs\fR
ensemble, i.e. \fBzipfs list\fR is equivalent to \fBzipfs::list\fR.
.PP
It is possible to mount an arbitrary number of ZIP archives even
below each other yielding a "union" view of all mounted ZIP archives.
However, should file names overlap, the first mount operation creating
a name hides all following identical names.
.SH "SEE ALSO"
tclsh(1), file(n), zlib(n)
.SH "KEYWORDS"
compress, filesystem, zip
'\" Local Variables:
'\" mode: nroff
'\" End:

Changes to jni/tcl/generic/zipfs.c.

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
....
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
....
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
....
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
....
1956
1957
1958
1959
1960
1961
1962





















































































































































































1963
1964
1965
1966
1967
1968
1969
....
1995
1996
1997
1998
1999
2000
2001





2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
....
2085
2086
2087
2088
2089
2090
2091

























































2092
2093
2094
2095
2096
2097
2098
2099
....
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
....
4350
4351
4352
4353
4354
4355
4356


4357





4358

4359
4360
4361
4362
4363
4364
4365
    Tcl_DStringInit(&ds);
#if HAS_DRIVES
    realname = AbsolutePath(filename, &drive, &ds);
#else
    realname = AbsolutePath(filename, &ds);
#endif
    hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);
    while (hPtr != NULL) {
	if ((zf = (ZipFile *) Tcl_GetHashValue(hPtr)) != NULL) {
#if HAS_DRIVES
	    if (drive && (drive != zf->mntdrv)) {
		hPtr = Tcl_NextHashEntry(&search);
		continue;
	    }
#endif
	    if (strcmp(zf->mntpt, realname) == 0) {
		match = 1;
		break;
	    }
	}
	hPtr = Tcl_NextHashEntry(&search);
    }
    Tcl_DStringFree(&ds);
    return match;
}
#endif
 
/*
................................................................................
	zf->chan = NULL;
    }
}
 
/*
 *-------------------------------------------------------------------------
 *
 * ZipFSOpenArchive --
 *
 *	This function opens a ZIP archive file for reading. An attempt
 *	is made to memory map that file. Otherwise it is read into
 *	an allocated memory buffer. The ZIP archive header is verified



 *	and must be valid for the function to succeed. When "needZip"
 *	is zero an embedded ZIP archive in an executable file is accepted.

 *
 * Results:
 *	TCL_OK on success, TCL_ERROR otherwise with an error message
 *	placed into the given "interp" if it is not NULL.
 *
 * Side effects:
 *	ZIP archive is memory mapped or read into allocated memory,

 *	the given ZipFile struct is filled with information about
 *	the ZIP archive file.
 *
 *-------------------------------------------------------------------------
 */

static int
































































































ZipFSOpenArchive(Tcl_Interp *interp, const char *zipname, int needZip,
		 ZipFile *zf)
{
    int i;
    ClientData handle;
    unsigned char *p, *q;


#if defined(_WIN32) || defined(_WIN64)
    zf->data = NULL;
    zf->mh = INVALID_HANDLE_VALUE;
#else
    zf->data = MAP_FAILED;
#endif
    zf->length = 0;
................................................................................
#endif
    }

#ifdef ZIPFS_USE_ZEMBED
gotZembed:
#endif

    p = zf->data + zf->length - ZIP_CENTRAL_END_LEN;
    while (p >= zf->data) {
	if (*p == (ZIP_CENTRAL_END_SIG & 0xFF)) {
	    if (ZipReadInt(p) == ZIP_CENTRAL_END_SIG) {
		break;
	    }
	    p -= ZIP_SIG_LEN;
	} else {
	    --p;



	}
    }




    if (p < zf->data) {
	if (!needZip) {
	    zf->baseoffs = zf->baseoffsp = zf->length;
	    return TCL_OK;
	}

	if (interp != NULL) {

	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("wrong end signature", -1));

	}
	goto error;





    }
    zf->nfiles = ZipReadShort(p + ZIP_CENTRAL_ENTS_OFFS);


    if (zf->nfiles == 0) {
	if (!needZip) {
	    zf->baseoffs = zf->baseoffsp = zf->length;
	    return TCL_OK;
	}
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("empty archive", -1));
	}
	goto error;


    }
    q = zf->data + ZipReadInt(p + ZIP_CENTRAL_DIRSTART_OFFS);
    p -= ZipReadInt(p + ZIP_CENTRAL_DIRSIZE_OFFS);
    if ((p < zf->data) || (p > (zf->data + zf->length)) ||
	(q < zf->data) || (q > (zf->data + zf->length))) {
	if (!needZip) {
	    zf->baseoffs = zf->baseoffsp = zf->length;
	    return TCL_OK;
	}





	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
		Tcl_NewStringObj("archive directory not found", -1));
	}


	goto error;
    }

    zf->baseoffs = zf->baseoffsp = p - q;
    zf->centoffs = p - zf->data;
    q = p;
    for (i = 0; i < zf->nfiles; i++) {
	int pathlen, comlen, extra;

	if ((q + ZIP_CENTRAL_HEADER_LEN) > (zf->data + zf->length)) {
	    if (interp != NULL) {
		Tcl_SetObjResult(interp,
		    Tcl_NewStringObj("wrong header length", -1));


	    }





	    goto error;

	}
	if (ZipReadInt(q) != ZIP_CENTRAL_HEADER_SIG) {





	    if (interp != NULL) {
		Tcl_SetObjResult(interp,
		    Tcl_NewStringObj("wrong header signature", -1));



	    }
	    goto error;












	}
	pathlen = ZipReadShort(q + ZIP_CENTRAL_PATHLEN_OFFS);
	comlen = ZipReadShort(q + ZIP_CENTRAL_FCOMMENTLEN_OFFS);
	extra = ZipReadShort(q + ZIP_CENTRAL_EXTRALEN_OFFS);
	q += pathlen + comlen + extra + ZIP_CENTRAL_HEADER_LEN;

    }
    q = zf->data + zf->baseoffs;
    if ((zf->baseoffs >= 6) &&
	(ZipReadInt(q - 4) == ZIP_PASSWORD_END_SIG)) {
	i = q[-5];
	if (q - 5 - i > zf->data) {
	    zf->pwbuf[0] = i;
	    memcpy(zf->pwbuf + 1, q - 5 - i, i);
	    zf->baseoffsp -= i ? (5 + i) : 0;

	}


    }
    return TCL_OK;

error:
    ZipFSCloseArchive(interp, zf);
    return TCL_ERROR;
}
 
/*
 *-------------------------------------------------------------------------
 *
 * Zipfs_Mount --
 *
 *      This procedure is invoked to mount a given ZIP archive file on
 *	a given mountpoint with optional ZIP password.

 *
 * Results:
 *      A standard Tcl result.
 *
 * Side effects:
 *      A ZIP archive file is read, analyzed and mounted, resources are
 *	allocated.
 *
 *-------------------------------------------------------------------------
 */

int
Zipfs_Mount(Tcl_Interp *interp, const char *zipname, const char *mntpt,
	    const char *passwd)
{
    char *realname, *p;
    int i, pwlen, isNew;
    ZipFile *zf, zf0;
    ZipEntry *z;
    Tcl_HashEntry *hPtr;
    Tcl_DString ds, dsm, fpBuf;
    unsigned char *q;
#if HAS_DRIVES

    int drive = 0;
#endif

    ReadLock();
    if (!ZipFS.initialized) {
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("not initialized", -1));
	}
	Unlock();
	return TCL_ERROR;
    }
    if (zipname == NULL) {
	Tcl_HashSearch search;
	int ret = TCL_OK;

	i = 0;
	hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);
	while (hPtr != NULL) {
	    if ((zf = (ZipFile *) Tcl_GetHashValue(hPtr)) != NULL) {
		if (interp != NULL) {
		    Tcl_AppendElement(interp, zf->mntpt);
		    Tcl_AppendElement(interp, zf->name);
		}
		++i;
	    }
	    hPtr = Tcl_NextHashEntry(&search);
	}
	if (interp == NULL) {
	    ret = (i > 0) ? TCL_OK : TCL_BREAK;
	}
	Unlock();
	return ret;
    }
    if (mntpt == NULL) {
	if (interp == NULL) {
	    Unlock();
	    return TCL_OK;
	}
	Tcl_DStringInit(&ds);
#if HAS_DRIVES
	p = AbsolutePath(zipname, &drive, &ds);
#else
	p = AbsolutePath(zipname, &ds);
#endif
	hPtr = Tcl_FindHashEntry(&ZipFS.zipHash, p);
	if (hPtr != NULL) {
	    if ((zf = Tcl_GetHashValue(hPtr)) != NULL) {
#if HAS_DRIVES
		if (drive == zf->mntdrv) {
		    Tcl_Obj *string;
		    char drvbuf[3];

		    drvbuf[0] = zf->mntdrv;
		    drvbuf[1] = ':';
		    drvbuf[2] = '\0';
		    string = Tcl_NewStringObj(drvbuf, 2);
		    Tcl_AppendToObj(string, zf->mntpt, zf->mntptlen);
		    Tcl_SetObjResult(interp, string);
		}
#else
		Tcl_SetObjResult(interp,
			Tcl_NewStringObj(zf->mntpt, zf->mntptlen));
#endif
	    }
	}
	Unlock();

	Tcl_DStringFree(&ds);
	return TCL_OK;
    }
    Unlock();
    pwlen = 0;
    if (passwd != NULL) {
	pwlen = strlen(passwd);
	if ((pwlen > 255) || (strchr(passwd, 0xff) != NULL)) {
	    if (interp) {
		Tcl_SetObjResult(interp,
				 Tcl_NewStringObj("illegal password", -1));
	    }
	    return TCL_ERROR;
	}
    }
    if (ZipFSOpenArchive(interp, zipname, 1, &zf0) != TCL_OK) {
	return TCL_ERROR;
    }
    Tcl_DStringInit(&ds);

#if HAS_DRIVES
    realname = AbsolutePath(zipname, NULL, &ds);
#else
    realname = AbsolutePath(zipname, &ds);
#endif

    /*
     * Mount point can come from Tcl_GetNameOfExecutable()
     * which sometimes is a relative or otherwise denormalized path.
     * But an absolute name is needed as mount point here.
     */
    Tcl_DStringInit(&dsm);
#if HAS_DRIVES
    mntpt = AbsolutePath(mntpt, &drive, &dsm);
















#else
    mntpt = AbsolutePath(mntpt, &dsm);
#endif
    WriteLock();
    hPtr = Tcl_CreateHashEntry(&ZipFS.zipHash, realname, &isNew);
    Tcl_DStringSetLength(&ds, 0);
    if (!isNew) {
	zf = (ZipFile *) Tcl_GetHashValue(hPtr);
	if (interp != NULL) {










	    Tcl_AppendResult(interp, "already mounted on \"", zf->mntptlen ?
			     zf->mntpt : "/", "\"", (char *) NULL);

	}
	Unlock();
	Tcl_DStringFree(&ds);
	Tcl_DStringFree(&dsm);
	ZipFSCloseArchive(interp, &zf0);
	return TCL_ERROR;
    }
    if (strcmp(mntpt, "/") == 0) {
	mntpt = "";
    }
    zf = (ZipFile *) Tcl_AttemptAlloc(sizeof(ZipFile) + strlen(mntpt) + 1);
    if (zf == NULL) {
	if (interp != NULL) {
	    Tcl_AppendResult(interp, "out of memory", (char *) NULL);
	}
	Unlock();
	Tcl_DStringFree(&ds);
	Tcl_DStringFree(&dsm);
	ZipFSCloseArchive(interp, &zf0);
	return TCL_ERROR;
    }
    *zf = zf0;
    zf->name = Tcl_GetHashKey(&ZipFS.zipHash, hPtr);
    strcpy(zf->mntpt, mntpt);
    zf->mntptlen = strlen(zf->mntpt);
#if HAS_DRIVES
    for (p = zf->mntpt; *p != '\0'; p++) {
	if (*p == '\\') {
	    *p = '/';
................................................................................
    }
    zf->mntdrv = drive;
#endif
    zf->entries = NULL;
    zf->topents = NULL;
    zf->nopen = 0;
    Tcl_SetHashValue(hPtr, (ClientData) zf);
    if ((zf->pwbuf[0] == 0) && pwlen) {
	int k = 0;

	i = pwlen;
	zf->pwbuf[k++] = i;
	while (i > 0) {
	    zf->pwbuf[k] = (passwd[i - 1] & 0x0f) |
		pwrot[(passwd[i - 1] >> 4) & 0x0f];
	    k++;
	    i--;
	}
	zf->pwbuf[k] = '\0';
    }
    if (mntpt[0] != '\0') {
	z = (ZipEntry *) Tcl_Alloc(sizeof(ZipDirEntry));
	z->name = NULL;
	z->tnext = NULL;
	z->parent = NULL;
	z->depth = CountSlashes(mntpt);
	z->zipfile = zf;
................................................................................
    Tcl_FSMountsChanged(NULL);
    return TCL_OK;
}
 
/*
 *-------------------------------------------------------------------------
 *





















































































































































































 * Zipfs_Unmount --
 *
 *      This procedure is invoked to unmount a given ZIP archive.
 *
 * Results:
 *      A standard Tcl result.
 *
................................................................................
#endif
    WriteLock();
    if (!ZipFS.initialized) {
	goto done;
    }
    hPtr = Tcl_FindHashEntry(&ZipFS.zipHash, realname);
    if (hPtr == NULL) {





	/* don't report error */
	goto done;
    }
    zf = (ZipFile *) Tcl_GetHashValue(hPtr);
#if HAS_DRIVES
    if (drive != zf->mntdrv) {
	/* don't report error */
	goto done;
    }
#endif
    if (zf->nopen > 0) {
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("filesystem is busy", -1));
	}
	ret = TCL_ERROR;
	goto done;
................................................................................
 *-------------------------------------------------------------------------
 */

static int
ZipFSMountObjCmd(ClientData clientData, Tcl_Interp *interp,
		 int objc, Tcl_Obj *const objv[])
{

























































    if (objc > 4) {
	Tcl_WrongNumArgs(interp, 1, objv,
			 "?zipfile? ?mountpoint? ?password?");
	return TCL_ERROR;
    }
    return Zipfs_Mount(interp, (objc > 1) ? Tcl_GetString(objv[1]) : NULL,
		       (objc > 2) ? Tcl_GetString(objv[2]) : NULL,
		       (objc > 3) ? Tcl_GetString(objv[3]) : NULL);
................................................................................
    info->zipfile->nopen--;
    Unlock();
    if (info->tofree != NULL) {
	Tcl_Free((char *) info->tofree);
    }
    if ((tsdPtr != NULL) && tsdPtr->initialized) {
	Tcl_HashEntry *hPtr;
       
	hPtr = Tcl_FindHashEntry(&tsdPtr->chanTab, (ClientData) info);
	if (hPtr != NULL) {
	    Tcl_DeleteHashEntry(hPtr);
	}
    }
    Tcl_Free((char *) info);
    return TCL_OK;
................................................................................
		       Tcl_StringCaseMatch(zf->mntpt + len + 1, pattern, 0)) {
		if (!matchHidden) {
		    p = strrchr(zf->mntpt, '/');
		    if ((p != NULL) && (p[1] == '.')) {
			goto end;
		    }
		}


		Tcl_ListObjAppendElement(NULL, result,





		    Tcl_NewStringObj(zf->mntpt, zf->mntptlen));

	    }
	}
	goto end;
    }
    if ((pattern == NULL) || (pattern[0] == '\0')) {
	hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, path);
	if (hPtr != NULL) {







|



<








<







 







|

|
|
|
>
>
>
|
|
>







>
|
<





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





<

>







 







|
<
<
<
<
|
<
<
<
>
>
>
|
|
>
>
>
>
|
|
|
|
<
>
|
>

|
>

<
>
>
>
>
>

<
>
>
|
<
|
<
<
<
<
<
<
|
>
>
|
<
<
<
<
<
<
<
|
>
>
>
>
>
|
|
<
<
>
>
|
<
>
|
|
<
<
<
|
<
<
<
<
>
>
|
>
>
>
>
>
|
>
|
<
>
>
>
>
>

<
<
>
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
<
>
|
<
<
<
<
<
<
<
<
>
|
>
>

|
|
<
<
<
|
<



|

<
<
>











|
|
|

|
|
|





>



<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
>

|

|

>








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>









>
>
>
>
>
>
>
>
>
>


>




|













|


|







 







<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
|
|
|
<
<
<
<
<

|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|







 







|







 







>
>
|
>
>
>
>
>
|
>







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
....
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
....
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
....
1756
1757
1758
1759
1760
1761
1762













1763
1764
1765
1766
1767
1768
1769
....
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
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
....
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230





2231
2232
2233
2234
2235
2236
2237
2238
2239
....
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
....
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
....
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
    Tcl_DStringInit(&ds);
#if HAS_DRIVES
    realname = AbsolutePath(filename, &drive, &ds);
#else
    realname = AbsolutePath(filename, &ds);
#endif
    hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);
    for (; hPtr != NULL; hPtr = Tcl_NextHashEntry(&search);) {
	if ((zf = (ZipFile *) Tcl_GetHashValue(hPtr)) != NULL) {
#if HAS_DRIVES
	    if (drive && (drive != zf->mntdrv)) {

		continue;
	    }
#endif
	    if (strcmp(zf->mntpt, realname) == 0) {
		match = 1;
		break;
	    }
	}

    }
    Tcl_DStringFree(&ds);
    return match;
}
#endif
 
/*
................................................................................
	zf->chan = NULL;
    }
}
 
/*
 *-------------------------------------------------------------------------
 *
 * ZipFSOpenCommon, ZipFSOpenArchive, ZipFSOpenBuffer --
 *
 *	These functions open a ZIP archive file for reading.
 *	ZipFSOpenArchive attemtps to memory map the archive file.
 *	Otherwise it is read into an allocated memory buffer.
 *	ZipFSOpenBufer works on a user provided memory buffer,
 *	which optionally is copied to a private buffer.
 *	The common part verifies the ZIP archive header which
 *	must be valid for the function to succeed. When "needZip"
 *	is zero an embedded ZIP archive in an executable file is
 *	accepted.
 *
 * Results:
 *	TCL_OK on success, TCL_ERROR otherwise with an error message
 *	placed into the given "interp" if it is not NULL.
 *
 * Side effects:
 *	ZIP archive is memory mapped or read into allocated memory,
 *	or taken from a user provided memory buffer; the given ZipFile
 *	struct is filled with information about the ZIP archive file.

 *
 *-------------------------------------------------------------------------
 */

static int
ZipFSOpenCommon(Tcl_Interp *interp, int needZip, ZipFile *zf)
{
    int i;
    unsigned char *p, *q;

    p = zf->data + zf->length - ZIP_CENTRAL_END_LEN;
    while (p >= zf->data) {
	if (*p == (ZIP_CENTRAL_END_SIG & 0xFF)) {
	    if (ZipReadInt(p) == ZIP_CENTRAL_END_SIG) {
		break;
	    }
	    p -= ZIP_SIG_LEN;
	} else {
	    --p;
	}
    }
    if (p < zf->data) {
	if (!needZip) {
	    zf->baseoffs = zf->baseoffsp = zf->length;
	    return TCL_OK;
	}
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("wrong end signature", -1));
	}
	goto error;
    }
    zf->nfiles = ZipReadShort(p + ZIP_CENTRAL_ENTS_OFFS);
    if (zf->nfiles == 0) {
	if (!needZip) {
	    zf->baseoffs = zf->baseoffsp = zf->length;
	    return TCL_OK;
	}
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("empty archive", -1));
	}
	goto error;
    }
    q = zf->data + ZipReadInt(p + ZIP_CENTRAL_DIRSTART_OFFS);
    p -= ZipReadInt(p + ZIP_CENTRAL_DIRSIZE_OFFS);
    if ((p < zf->data) || (p > (zf->data + zf->length)) ||
	(q < zf->data) || (q > (zf->data + zf->length))) {
	if (!needZip) {
	    zf->baseoffs = zf->baseoffsp = zf->length;
	    return TCL_OK;
	}
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
		Tcl_NewStringObj("archive directory not found", -1));
	}
	goto error;
    }
    zf->baseoffs = zf->baseoffsp = p - q;
    zf->centoffs = p - zf->data;
    q = p;
    for (i = 0; i < zf->nfiles; i++) {
	int pathlen, comlen, extra;

	if ((q + ZIP_CENTRAL_HEADER_LEN) > (zf->data + zf->length)) {
	    if (interp != NULL) {
		Tcl_SetObjResult(interp,
		    Tcl_NewStringObj("wrong header length", -1));
	    }
	    goto error;
	}
	if (ZipReadInt(q) != ZIP_CENTRAL_HEADER_SIG) {
	    if (interp != NULL) {
		Tcl_SetObjResult(interp,
		    Tcl_NewStringObj("wrong header signature", -1));
	    }
	    goto error;
	}
	pathlen = ZipReadShort(q + ZIP_CENTRAL_PATHLEN_OFFS);
	comlen = ZipReadShort(q + ZIP_CENTRAL_FCOMMENTLEN_OFFS);
	extra = ZipReadShort(q + ZIP_CENTRAL_EXTRALEN_OFFS);
	q += pathlen + comlen + extra + ZIP_CENTRAL_HEADER_LEN;
    }
    q = zf->data + zf->baseoffs;
    if ((zf->baseoffs >= 6) &&
	(ZipReadInt(q - 4) == ZIP_PASSWORD_END_SIG)) {
	i = q[-5];
	if (q - 5 - i > zf->data) {
	    zf->pwbuf[0] = i;
	    memcpy(zf->pwbuf + 1, q - 5 - i, i);
	    zf->baseoffsp -= i ? (5 + i) : 0;
	}
    }
    return TCL_OK;

error:
    ZipFSCloseArchive(interp, zf);
    return TCL_ERROR;
}

static int
ZipFSOpenArchive(Tcl_Interp *interp, const char *zipname, int needZip,
		 ZipFile *zf)
{
    int i;
    ClientData handle;


    memset(zf, 0, sizeof(*zf));
#if defined(_WIN32) || defined(_WIN64)
    zf->data = NULL;
    zf->mh = INVALID_HANDLE_VALUE;
#else
    zf->data = MAP_FAILED;
#endif
    zf->length = 0;
................................................................................
#endif
    }

#ifdef ZIPFS_USE_ZEMBED
gotZembed:
#endif

    return ZipFSOpenCommon(interp, needZip, zf);








error:
    ZipFSCloseArchive(interp, zf);
    return TCL_ERROR;
}

static int
ZipFSOpenBuffer(Tcl_Interp *interp, unsigned char *data, int length,
		int copy, ZipFile *zf)
{
    memset(zf, 0, sizeof(*zf));
#if defined(_WIN32) || defined(_WIN64)
    zf->mh = INVALID_HANDLE_VALUE;
#endif

    if (copy) {
	zf->data = (unsigned char *) Tcl_AttemptAlloc(length);
	if (zf->data == NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("out of memory", -1));
	    return TCL_ERROR;
	}

	memcpy(zf->data, data, length);
	zf->tofree = zf->data;
    } else {
	zf->data = data;
	zf->tofree = NULL;
    }

    zf->length = length;
    zf->ismapped = 0;
    zf->nfiles = 0;

    zf->baseoffs = zf->baseoffsp = 0;






    zf->pwbuf[0] = 0;
    zf->chan = NULL;
    return ZipFSOpenCommon(interp, 1, zf);
}







 
/*
 *-------------------------------------------------------------------------
 *
 * Zipfs_ListMountInfo --
 *
 *      Write information about mounted ZIP file systems to
 *	interp's result.


 *
 * Results:
 *      A standard Tcl result.

 *
 * Side effects:
 *      None.



 *




 *-------------------------------------------------------------------------
 */

static int
Zipfs_ListMountInfo(Tcl_Interp *interp)
{
    int i, ret = TCL_OK;
    ZipFile *zf;
    Tcl_HashEntry *hPtr;
    Tcl_HashSearch search;


    i = 0;
    hPtr = Tcl_FirstHashEntry(&ZipFS.zipHash, &search);
    while (hPtr != NULL) {
	zf = (ZipFile *) Tcl_GetHashValue(hPtr);
	if (zf != NULL) {
	    if (interp != NULL) {


#if HAS_DRIVES
		Tcl_DString ds;
		char drvbuf[3];


		Tcl_DStringInit(&ds);
		drvbuf[0] = zf->mntdrv;
		drvbuf[1] = ':';
		drvbuf[2] = '\0';
		Tcl_DStringAppend(&ds, drvbuf, -1);
		Tcl_DStringAppend(&ds, zf->mntpt, zf->mntptlen);
		Tcl_AppendElement(interp, Tcl_DStringValue(&ds));
		Tcl_DStringFree(&ds);
#else
		Tcl_AppendElement(interp, zf->mntpt);
#endif
		Tcl_AppendElement(interp, zf->name);
	    }




	    ++i;
	}








	hPtr = Tcl_NextHashEntry(&search);
    }
    if (interp == NULL) {
	ret = (i > 0) ? TCL_OK : TCL_BREAK;
    }
    return ret;
}



 

/*
 *-------------------------------------------------------------------------
 *
 * Zipfs_MountCommon --
 *


 *      Common part of mounting a ZIP archive file.
 *
 * Results:
 *      A standard Tcl result.
 *
 * Side effects:
 *      A ZIP archive file is read, analyzed and mounted, resources are
 *	allocated.
 *
 *-------------------------------------------------------------------------
 */

static int
Zipfs_MountCommon(Tcl_Interp *interp, const char *zipname,
		  const char *mntpt, int isBuffer, ZipFile *zf0)
{
    char *realname;
    int i, isNew;
    ZipFile *zf;
    ZipEntry *z;
    Tcl_HashEntry *hPtr;
    Tcl_DString ds, dsm, fpBuf;
    unsigned char *q;
#if HAS_DRIVES
    char *p;
    int drive = 0;
#endif





































    Tcl_DStringInit(&ds);



























    if (isBuffer) {
	Tcl_DStringAppend(&ds, zipname, -1);

















	realname = Tcl_DStringValue(&ds);
    } else {
#if HAS_DRIVES
	realname = AbsolutePath(zipname, NULL, &ds);
#else
	realname = AbsolutePath(zipname, &ds);
#endif
    }
    /*
     * Mount point can come from Tcl_GetNameOfExecutable()
     * which sometimes is a relative or otherwise denormalized path.
     * But an absolute name is needed as mount point here.
     */
    Tcl_DStringInit(&dsm);
#if HAS_DRIVES
    mntpt = AbsolutePath(mntpt, &drive, &dsm);
    if (drive == 0) {
	/*
	 * Enforce a drive letter by using [pwd] information.
	 */
	Tcl_DString pwd;

	Tcl_DStringInit(&pwd);
	Tcl_GetCwd(NULL, &pwd);
	if (Tcl_DStringValue(&pwd)[1] == ':') {
	    drive = Tcl_DStringValue(&pwd)[0];
	    if ((drive >= 'a') && (drive <= 'z')) {
		drive -= 'a' - 'A';
	    }
	}
	Tcl_DStringFree(&pwd);
    }
#else
    mntpt = AbsolutePath(mntpt, &dsm);
#endif
    WriteLock();
    hPtr = Tcl_CreateHashEntry(&ZipFS.zipHash, realname, &isNew);
    Tcl_DStringSetLength(&ds, 0);
    if (!isNew) {
	zf = (ZipFile *) Tcl_GetHashValue(hPtr);
	if (interp != NULL) {
#if HAS_DRIVES
	    char drvbuf[3];

	    drvbuf[0] = zf->mntdrv;
	    drvbuf[1] = ':';
	    drvbuf[2] = '\0';
	    Tcl_AppendResult(interp, "already mounted on \"", drvbuf,
			     zf->mntptlen ? zf->mntpt : "/", "\"",
			     (char *) NULL);
#else
	    Tcl_AppendResult(interp, "already mounted on \"", zf->mntptlen ?
			     zf->mntpt : "/", "\"", (char *) NULL);
#endif
	}
	Unlock();
	Tcl_DStringFree(&ds);
	Tcl_DStringFree(&dsm);
	ZipFSCloseArchive(interp, zf0);
	return TCL_ERROR;
    }
    if (strcmp(mntpt, "/") == 0) {
	mntpt = "";
    }
    zf = (ZipFile *) Tcl_AttemptAlloc(sizeof(ZipFile) + strlen(mntpt) + 1);
    if (zf == NULL) {
	if (interp != NULL) {
	    Tcl_AppendResult(interp, "out of memory", (char *) NULL);
	}
	Unlock();
	Tcl_DStringFree(&ds);
	Tcl_DStringFree(&dsm);
	ZipFSCloseArchive(interp, zf0);
	return TCL_ERROR;
    }
    *zf = *zf0;
    zf->name = Tcl_GetHashKey(&ZipFS.zipHash, hPtr);
    strcpy(zf->mntpt, mntpt);
    zf->mntptlen = strlen(zf->mntpt);
#if HAS_DRIVES
    for (p = zf->mntpt; *p != '\0'; p++) {
	if (*p == '\\') {
	    *p = '/';
................................................................................
    }
    zf->mntdrv = drive;
#endif
    zf->entries = NULL;
    zf->topents = NULL;
    zf->nopen = 0;
    Tcl_SetHashValue(hPtr, (ClientData) zf);













    if (mntpt[0] != '\0') {
	z = (ZipEntry *) Tcl_Alloc(sizeof(ZipDirEntry));
	z->name = NULL;
	z->tnext = NULL;
	z->parent = NULL;
	z->depth = CountSlashes(mntpt);
	z->zipfile = zf;
................................................................................
    Tcl_FSMountsChanged(NULL);
    return TCL_OK;
}
 
/*
 *-------------------------------------------------------------------------
 *
 * Zipfs_Mount --
 *
 *      This procedure is invoked to mount a given ZIP archive file on
 *	a given mountpoint with optional ZIP password.
 *
 * Results:
 *      A standard Tcl result.
 *
 * Side effects:
 *      A ZIP archive file is read, analyzed and mounted, resources are
 *	allocated.
 *
 *-------------------------------------------------------------------------
 */

int
Zipfs_Mount(Tcl_Interp *interp, const char *zipname, const char *mntpt,
	    const char *passwd)
{
    int i, pwlen, ret;
    ZipFile *zf, zf0;
    Tcl_HashEntry *hPtr;
#if HAS_DRIVES
    int drive = 0;
#endif

    ReadLock();
    if (!ZipFS.initialized) {
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("not initialized", -1));
	}
	Unlock();
	return TCL_ERROR;
    }
    if (zipname == NULL) {
	ret = Zipfs_ListMountInfo(interp);
	Unlock();
	return ret;
    }
    if (mntpt == NULL) {
	char *p;
	Tcl_DString ds;

	if (interp == NULL) {
	    Unlock();
	    return TCL_OK;
	}
	Tcl_DStringInit(&ds);
#if HAS_DRIVES
	p = AbsolutePath(zipname, &drive, &ds);
#else
	p = AbsolutePath(zipname, &ds);
#endif
	hPtr = Tcl_FindHashEntry(&ZipFS.zipHash, p);
	if (hPtr == NULL) {
	    hPtr = Tcl_FindHashEntry(&ZipFS.zipHash, zipname);
	}
	if (hPtr != NULL) {
	    zf = Tcl_GetHashValue(hPtr);
	    if (zf != NULL) {
#if HAS_DRIVES
		if (drive == zf->mntdrv) {
		    Tcl_Obj *string;
		    char drvbuf[3];

		    drvbuf[0] = zf->mntdrv;
		    drvbuf[1] = ':';
		    drvbuf[2] = '\0';
		    string = Tcl_NewStringObj(drvbuf, -1);
		    Tcl_AppendToObj(string, zf->mntpt, zf->mntptlen);
		    Tcl_SetObjResult(interp, string);
		}
#else
		Tcl_SetObjResult(interp,
			Tcl_NewStringObj(zf->mntpt, zf->mntptlen));
#endif
	    }
	}
	Unlock();
	Tcl_DStringFree(&ds);
	return TCL_OK;
    }
    Unlock();
    pwlen = 0;
    if (passwd != NULL) {
	pwlen = strlen(passwd);
	if ((pwlen > 255) || (strchr(passwd, 0xff) != NULL)) {
	    if (interp) {
		Tcl_SetObjResult(interp,
				 Tcl_NewStringObj("illegal password", -1));
	    }
	    return TCL_ERROR;
	}
    }
    if (ZipFSOpenArchive(interp, zipname, 1, &zf0) != TCL_OK) {
	return TCL_ERROR;
    }
    if ((zf0.pwbuf[0] == 0) && pwlen) {
	int k = 0;

	i = pwlen;
	zf0.pwbuf[k++] = i;
	while (i > 0) {
	    zf0.pwbuf[k] = (passwd[i - 1] & 0x0f) |
		pwrot[(passwd[i - 1] >> 4) & 0x0f];
	    k++;
	    i--;
	}
	zf0.pwbuf[k] = '\0';
    }
    ret = Zipfs_MountCommon(interp, zipname, mntpt, 0, &zf0);
    if (zf0.pwbuf[0]) {
	memset(&zf0, 0, sizeof(zf0));
    }
    return ret;
}
 
/*
 *-------------------------------------------------------------------------
 *
 * Zipfs_MountBuffer --
 *
 *      This procedure is invoked to mount a given ZIP archive contained
 *	in a memory buffer on a given mountpoint.
 *
 * Results:
 *      A standard Tcl result.
 *
 * Side effects:
 *      A ZIP archive from memory is analyzed and mounted, resources are
 *	allocated.
 *
 *-------------------------------------------------------------------------
 */

int
Zipfs_MountBuffer(Tcl_Interp *interp, const char *mntpt,
		  unsigned char *data, size_t length, int copy)
{
    int ret;
    ZipFile zf0;
    char namebuf[64];

    ReadLock();
    if (!ZipFS.initialized) {
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("not initialized", -1));
	}
	Unlock();
	return TCL_ERROR;
    }
    if (mntpt == NULL) {
	ret = Zipfs_ListMountInfo(interp);
	Unlock();
	return ret;
    }
    if ((data == NULL) || (length == 0)) {
	Unlock();
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("empty data provided", -1));
	}
	return TCL_ERROR;
    }
    Unlock();
    if (ZipFSOpenBuffer(interp, data, length, 0, &zf0) != TCL_OK) {
	return TCL_ERROR;
    }
    sprintf(namebuf, "memory_%ld_%d", (long) length, ZipFS.idCount++);
    ret = Zipfs_MountCommon(interp, namebuf, mntpt, 1, &zf0);
    if (ret == TCL_OK) {
	Tcl_SetObjResult(interp, Tcl_NewStringObj(namebuf, -1));
    }
    return ret;
}
 
/*
 *-------------------------------------------------------------------------
 *
 * Zipfs_Unmount --
 *
 *      This procedure is invoked to unmount a given ZIP archive.
 *
 * Results:
 *      A standard Tcl result.
 *
................................................................................
#endif
    WriteLock();
    if (!ZipFS.initialized) {
	goto done;
    }
    hPtr = Tcl_FindHashEntry(&ZipFS.zipHash, realname);
    if (hPtr == NULL) {
#if HASH_DRIVES
	drive = 0;
#endif
	hPtr = Tcl_FindHashEntry(&ZipFS.zipHash, zipname);
	if (hPtr == NULL) {
	    /* don't report error */
	    goto done;
	}





    }
    zf = (ZipFile *) Tcl_GetHashValue(hPtr);
    if (zf->nopen > 0) {
	if (interp != NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("filesystem is busy", -1));
	}
	ret = TCL_ERROR;
	goto done;
................................................................................
 *-------------------------------------------------------------------------
 */

static int
ZipFSMountObjCmd(ClientData clientData, Tcl_Interp *interp,
		 int objc, Tcl_Obj *const objv[])
{
    int optLen;
    const char *option;

    if (objc > 5) {
	Tcl_WrongNumArgs(interp, 1, objv,
			 "?-option ?arg? ?mountpoint? ?password?");
	return TCL_ERROR;
    }
    if (objc > 3) {
	option = Tcl_GetString(objv[1]);
	optLen = strlen(option);
	if ((optLen > 1) && strncmp(option, "-data", 5) == 0) {
	    unsigned char *data;
	    int length;

	    if (objc != 4) {
		Tcl_WrongNumArgs(interp, 1, objv,
				 "-data bytearray mountpoint");
		return TCL_ERROR;
	    }
	    data = Tcl_GetByteArrayFromObj(objv[2], &length);
	    return Zipfs_MountBuffer(interp, Tcl_GetString(objv[3]),
				     data, length, 1);
	} else if ((optLen > 1) && strncmp(option, "-chan", 5) == 0) {
	    unsigned char *data;
	    int length, ret;
	    Tcl_Channel chan;
	    Tcl_Obj *dataObj;

	    if (objc != 4) {
		Tcl_WrongNumArgs(interp, 1, objv,
				 "-chan channelId mountpoint");
		return TCL_ERROR;
	    }
	    chan = Tcl_GetChannel(interp, Tcl_GetString(objv[2]), NULL);
	    if (chan == NULL) {
		return TCL_ERROR;
	    }
	    dataObj = Tcl_NewObj();
	    Tcl_IncrRefCount(dataObj);
	    if (Tcl_ReadChars(chan, dataObj, -1, 0) == -1) {
		Tcl_DecrRefCount(dataObj);
		Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			"read error: %s", Tcl_PosixError(interp)));
 		return TCL_ERROR;
	    }
	    data = Tcl_GetByteArrayFromObj(dataObj, &length);
	    ret = Zipfs_MountBuffer(interp, Tcl_GetString(objv[3]),
				    data, length, 1);
	    Tcl_DecrRefCount(dataObj);
	    return ret;
	} else if ((optLen > 1) && ((strncmp(option, "-file", 5) == 0) ||
				    (strcmp(option, "--") == 0))) {
	    return Zipfs_Mount(interp, Tcl_GetString(objv[2]),
			       Tcl_GetString(objv[3]),
			       (objc > 4) ? Tcl_GetString(objv[4]) : NULL);
	}
    } else if (objc > 4) {
	Tcl_WrongNumArgs(interp, 1, objv,
			 "?zipfile? ?mountpoint? ?password?");
	return TCL_ERROR;
    }
    return Zipfs_Mount(interp, (objc > 1) ? Tcl_GetString(objv[1]) : NULL,
		       (objc > 2) ? Tcl_GetString(objv[2]) : NULL,
		       (objc > 3) ? Tcl_GetString(objv[3]) : NULL);
................................................................................
    info->zipfile->nopen--;
    Unlock();
    if (info->tofree != NULL) {
	Tcl_Free((char *) info->tofree);
    }
    if ((tsdPtr != NULL) && tsdPtr->initialized) {
	Tcl_HashEntry *hPtr;

	hPtr = Tcl_FindHashEntry(&tsdPtr->chanTab, (ClientData) info);
	if (hPtr != NULL) {
	    Tcl_DeleteHashEntry(hPtr);
	}
    }
    Tcl_Free((char *) info);
    return TCL_OK;
................................................................................
		       Tcl_StringCaseMatch(zf->mntpt + len + 1, pattern, 0)) {
		if (!matchHidden) {
		    p = strrchr(zf->mntpt, '/');
		    if ((p != NULL) && (p[1] == '.')) {
			goto end;
		    }
		}
		if (prefix != NULL) {
		    Tcl_DStringAppend(&dsPref, zf->mntpt, zf->mntptlen);
		    Tcl_ListObjAppendElement(NULL, result,
			Tcl_NewStringObj(Tcl_DStringValue(&dsPref),
			    Tcl_DStringLength(&dsPref)));
		    Tcl_DStringSetLength(&dsPref, prefixLen);
		} else {
		    Tcl_ListObjAppendElement(NULL, result,
			Tcl_NewStringObj(zf->mntpt, zf->mntptlen));
		}
	    }
	}
	goto end;
    }
    if ((pattern == NULL) || (pattern[0] == '\0')) {
	hPtr = Tcl_FindHashEntry(&ZipFS.fileHash, path);
	if (hPtr != NULL) {

Changes to jni/tcl/generic/zipfs.h.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
..
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
/*
 * zipfs.h --
 *
 *	This header file describes the interface of the ZIPFS filesystem
 *	used in AndroWish.
 *
 * Copyright (c) 2013-2018 Christian Werner <chw@ch-werner.de>
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#ifndef _ZIPFS_H
#define _ZIPFS_H
................................................................................
#endif

#ifndef ZIPFSAPI
#   define ZIPFSAPI extern
#endif

#ifdef ZIPFS_IN_TK
#define Zipfs_Mount    Tkzipfs_Mount

#define Zipfs_Unmount  Tkzipfs_Unmount
#define Zipfs_Init     Tkzipfs_Init
#define Zipfs_SafeInit Tkzipfs_SafeInit
#ifdef BUILD_tk
#   undef ZIPFSAPI
#   define ZIPFSAPI DLLEXPORT
#endif
#endif

#ifdef ZIPFS_IN_TCL
#define Zipfs_Mount    Tclzipfs_Mount

#define Zipfs_Unmount  Tclzipfs_Unmount
#define Zipfs_Init     Tclzipfs_Init
#define Zipfs_SafeInit Tclzipfs_SafeInit
#ifdef BUILD_tcl
#   undef ZIPFSAPI
#   define ZIPFSAPI DLLEXPORT
#endif
#endif

ZIPFSAPI int Zipfs_Mount(Tcl_Interp *interp, const char *zipname,
			 const char *mntpt, const char *passwd);


ZIPFSAPI int Zipfs_Unmount(Tcl_Interp *interp, const char *zipname);
ZIPFSAPI int Zipfs_Init(Tcl_Interp *interp);
ZIPFSAPI int Zipfs_SafeInit(Tcl_Interp *interp);

#ifdef __cplusplus
}
#endif






|







 







|
>
|
|
|







|
>
|
|
|








>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
..
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
/*
 * zipfs.h --
 *
 *	This header file describes the interface of the ZIPFS filesystem
 *	used in AndroWish.
 *
 * Copyright (c) 2013-2019 Christian Werner <chw@ch-werner.de>
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#ifndef _ZIPFS_H
#define _ZIPFS_H
................................................................................
#endif

#ifndef ZIPFSAPI
#   define ZIPFSAPI extern
#endif

#ifdef ZIPFS_IN_TK
#define Zipfs_Mount       Tkzipfs_Mount
#define Zipfs_MountBuffer Tkzipfs_MountBuffer
#define Zipfs_Unmount     Tkzipfs_Unmount
#define Zipfs_Init        Tkzipfs_Init
#define Zipfs_SafeInit    Tkzipfs_SafeInit
#ifdef BUILD_tk
#   undef ZIPFSAPI
#   define ZIPFSAPI DLLEXPORT
#endif
#endif

#ifdef ZIPFS_IN_TCL
#define Zipfs_Mount       Tclzipfs_Mount
#define Zipfs_MountBuffer Tclzipfs_MountBuffer
#define Zipfs_Unmount     Tclzipfs_Unmount
#define Zipfs_Init        Tclzipfs_Init
#define Zipfs_SafeInit    Tclzipfs_SafeInit
#ifdef BUILD_tcl
#   undef ZIPFSAPI
#   define ZIPFSAPI DLLEXPORT
#endif
#endif

ZIPFSAPI int Zipfs_Mount(Tcl_Interp *interp, const char *zipname,
			 const char *mntpt, const char *passwd);
ZIPFSAPI int Zipfs_MountBuffer(Tcl_Interp *interp, const char *mntpt,
			       unsigned char *data, size_t length, int copy);
ZIPFSAPI int Zipfs_Unmount(Tcl_Interp *interp, const char *zipname);
ZIPFSAPI int Zipfs_Init(Tcl_Interp *interp);
ZIPFSAPI int Zipfs_SafeInit(Tcl_Interp *interp);

#ifdef __cplusplus
}
#endif