File: nsZipArchive.cpp

package info (click to toggle)
iceweasel 2.0.0.19-0etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 298,784 kB
  • ctags: 317,912
  • sloc: cpp: 1,796,902; ansic: 987,677; xml: 109,036; makefile: 47,777; asm: 35,201; perl: 26,983; sh: 20,879; cs: 6,232; java: 5,513; python: 3,249; pascal: 459; lex: 306; php: 244; csh: 132; objc: 97; yacc: 79; ada: 49; awk: 14; sql: 4; sed: 4
file content (1909 lines) | stat: -rw-r--r-- 47,798 bytes parent folder | download | duplicates (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
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Mozilla Communicator client code, released
 * March 31, 1998.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Daniel Veditz <dveditz@netscape.com>
 *   Samir Gehani <sgehani@netscape.com>
 *   Mitch Stoltz <mstoltz@netscape.com>
 *   Jeroen Dobbelaere <jeroen.dobbelaere@acunia.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

/*
 * This module implements a simple archive extractor for the PKZIP format.
 *
 * The underlying nsZipArchive is NOT thread-safe. Do not pass references
 * or pointers to it across thread boundaries.
 */


#ifndef STANDALONE

#include "nsWildCard.h"
#include "nscore.h"
#include "prmem.h"
#include "prio.h"
#include "plstr.h"
#include "prlog.h"
#include "prprf.h"
#define ZFILE_CREATE    PR_WRONLY | PR_CREATE_FILE
#define READTYPE  PRInt32
#include "zlib.h"
#include "nsISupportsUtils.h"
#include "nsRecyclingAllocator.h"
#include "nsPrintfCString.h"
/**
 * Globals
 *
 * Global allocator used with zlib. Destroyed in module shutdown.
 */
#define NBUCKETS 6
#define BY4ALLOC_ITEMS 320
nsRecyclingAllocator *gZlibAllocator = NULL;

// For placement new used for arena allocations of zip file list
#include NEW_H

#else /* STANDALONE */

#ifdef XP_WIN
#include "windows.h"
#endif

#undef MOZILLA_CLIENT       // undoes prtypes damage in zlib.h
#define ZFILE_CREATE  "wb"
#define READTYPE  PRUint32
#include "zlib.h"
#undef PR_PUBLIC_API
#include "zipstub.h"

#ifdef XP_MAC
#include <string.h>
#include <stdlib.h>

char * strdup(const char *src);
char * strdup(const char *src)
{
    long len = strlen(src);
    char *dup = (char *)malloc(len+1 * sizeof(char));
    memcpy(dup, src, len+1);
    return dup;
}
#endif

#endif /* STANDALONE */

#ifdef XP_UNIX
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <limits.h>
    #include <unistd.h>
#elif defined(XP_WIN) || defined(XP_OS2)
    #include <io.h>
#endif

#ifndef XP_UNIX /* we need to have some constant defined in limits.h and unistd.h */
#  ifndef S_IFMT
#    define S_IFMT 0170000
#  endif
#  ifndef S_IFLNK
#    define S_IFLNK  0120000
#  endif
#  ifndef PATH_MAX
#    define PATH_MAX 1024
#  endif
#endif  /* XP_UNIX */

#include "zipfile.h"
#include "zipstruct.h"
#include "nsZipArchive.h"

static PRUint16 xtoint(unsigned char *ii);
static PRUint32 xtolong(unsigned char *ll);
static PRUint16 ExtractMode(PRUint32 ext_attr);
static PRBool   IsSymlink(PRUint32 ext_attr);

/*---------------------------------------------
 * C API wrapper for nsZipArchive
 *--------------------------------------------*/

#ifdef STANDALONE

/**
 * ZIP_OpenArchive
 *
 * opens the named zip/jar archive and returns a handle that
 * represents the archive in other ZIP_ calls.
 *
 * @param   zipname   archive filename
 * @param   hZip      receives handle if archive opened OK
 * @return  status code
 */
PR_PUBLIC_API(PRInt32) ZIP_OpenArchive(const char * zipname, void** hZip)
{
  PRInt32 status;

  /*--- error check args ---*/
  if (hZip == 0)
    return ZIP_ERR_PARAM;

  /*--- NULL output to prevent use by bozos who don't check errors ---*/
  *hZip = 0;

  /*--- create and open the archive ---*/
  nsZipArchive* zip = new nsZipArchive();
  if (zip == 0)
    return ZIP_ERR_MEMORY;

  status = zip->OpenArchive(zipname);

  if (status == ZIP_OK)
    *hZip = NS_STATIC_CAST(void*,zip);
  else
      delete zip;

  return status;
}



/**
 * ZIP_TestArchive
 *
 * Tests the integrity of this open zip archive by extracting each
 * item to memory and performing a CRC check.
 *
 * @param   hZip      handle obtained from ZIP_OpenArchive
 * @return  status code (success indicated by ZIP_OK)
 */
PR_PUBLIC_API(PRInt32) ZIP_TestArchive(void *hZip)
{
  /*--- error check args ---*/
  if (hZip == 0)
    return ZIP_ERR_PARAM;

  nsZipArchive* zip = NS_STATIC_CAST(nsZipArchive*,hZip);
  if (zip->kMagic != ZIP_MAGIC)
    return ZIP_ERR_PARAM;   /* whatever it is isn't one of ours! */

  /*--- test the archive ---*/
  return zip->Test(NULL, zip->GetFd());
}


/**
 * ZIP_CloseArchive
 *
 * closes zip archive and frees memory
 * @param   hZip  handle obtained from ZIP_OpenArchive
 * @return  status code
 */
PR_PUBLIC_API(PRInt32) ZIP_CloseArchive(void** hZip)
{
  /*--- error check args ---*/
  if (hZip == 0 || *hZip == 0)
    return ZIP_ERR_PARAM;

  nsZipArchive* zip = NS_STATIC_CAST(nsZipArchive*,*hZip);
  if (zip->kMagic != ZIP_MAGIC)
    return ZIP_ERR_PARAM;   /* whatever it is isn't one of ours! */

  /*--- close the archive ---*/
  *hZip = 0;
  delete zip;

  return ZIP_OK;
}



/**
 * ZIP_ExtractFile
 *
 * extracts named file from an opened archive
 *
 * @param   hZip      handle obtained from ZIP_OpenArchive
 * @param   filename  name of file in archive
 * @param   outname   filename to extract to
 */
PR_PUBLIC_API(PRInt32) ZIP_ExtractFile(void* hZip, const char * filename, const char * outname)
{
  /*--- error check args ---*/
  if (hZip == 0)
    return ZIP_ERR_PARAM;

  nsZipArchive* zip = NS_STATIC_CAST(nsZipArchive*,hZip);
  if (zip->kMagic != ZIP_MAGIC)
    return ZIP_ERR_PARAM;   /* whatever it is isn't one of ours! */

  /*--- extract the file ---*/
  return zip->ExtractFile(filename, outname, zip->GetFd());
}



/**
 * ZIP_FindInit
 *
 * Initializes an enumeration of files in the archive
 *
 * @param   hZip      handle obtained from ZIP_OpenArchive
 * @param   pattern   regexp to match files in archive, the usual shell expressions.
 *                    NULL pattern also matches all files, faster than "*"
 */
PR_PUBLIC_API(void*) ZIP_FindInit(void* hZip, const char * pattern)
{
  /*--- error check args ---*/
  if (hZip == 0)
    return 0;

  nsZipArchive* zip = NS_STATIC_CAST(nsZipArchive*,hZip);
  if (zip->kMagic != ZIP_MAGIC)
    return 0;   /* whatever it is isn't one of ours! */

  /*--- initialize the pattern search ---*/
  return zip->FindInit(pattern);
}



/**
 * ZIP_FindNext
 *
 * Puts the next name in the passed buffer. Returns ZIP_ERR_SMALLBUF when
 * the name is too large for the buffer, and ZIP_ERR_FNF when there are no
 * more files that match the pattern
 *
 * @param   hFind     handle obtained from ZIP_FindInit
 * @param   outbuf    buffer to receive next filename
 * @param   bufsize   size of allocated buffer
 */
PR_PUBLIC_API(PRInt32) ZIP_FindNext(void* hFind, char * outbuf, PRUint16 bufsize)
{
  PRInt32 status;

  /*--- error check args ---*/
  if (hFind == 0)
    return ZIP_ERR_PARAM;

  nsZipFind* find = NS_STATIC_CAST(nsZipFind*,hFind);
  if (find->kMagic != ZIPFIND_MAGIC)
    return ZIP_ERR_PARAM;   /* whatever it is isn't one of ours! */

  /*--- return next filename file ---*/
  nsZipItem* item;
  status = find->GetArchive()->FindNext(find, &item);
  if (status == ZIP_OK)
  {
    PRUint16 namelen = (PRUint16)PL_strlen(item->name);

    if (bufsize > namelen)
    {
        PL_strcpy(outbuf, item->name);
    }
    else
        status = ZIP_ERR_SMALLBUF;
  }

  return status;
}



/**
 * ZIP_FindFree
 *
 * Releases allocated memory associated with the find token
 *
 * @param   hFind     handle obtained from ZIP_FindInit
 */
PR_PUBLIC_API(PRInt32) ZIP_FindFree(void* hFind)
{
  /*--- error check args ---*/
  if (hFind == 0)
    return ZIP_ERR_PARAM;

  nsZipFind* find = NS_STATIC_CAST(nsZipFind*,hFind);
  if (find->kMagic != ZIPFIND_MAGIC)
    return ZIP_ERR_PARAM;   /* whatever it is isn't one of ours! */

  /* free the find structure */
  return find->GetArchive()->FindFree(find);
}

#if defined XP_WIN
void ProcessWindowsMessages()
{
  MSG msg;

  while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
}
#endif /* XP_WIN */

#else /* STANDALONE */

//***********************************************************
// Allocators for use with zlib
//
// These are allocators that are performance tuned for
// use with zlib. Our use of zlib for every file we read from
// the jar file when running navigator, we do these allocation.
// alloc 24
// alloc 64
// alloc 11520
// alloc 32768
// alloc 1216 [304x4] max
// alloc 76   [19x4]
// free  76   [19x4]
// alloc 1152 [288x4]
// free  1152 [288x4]
// free  1216 [304x4]
// alloc 28
// free  28
// free  32768
// free  11520
// free  64
// free  24
//
// The pool will allocate these as:
//
//          32,768
//          11,520
//           1,280 [320x4] - shared by first x4 alloc, 28
//           1,280 [320x4] - shared by second and third x4 alloc
//              64
//              24
//          ------
//          46,936
//
// And almost all of the file reads happen serially. Hence this
// allocator tries to keep one set of memory needed for one file around
// and reused the same blocks for other file reads.
//
// The interesting question is when should be free this ?
// - memory pressure should be one.
// - after startup of navigator
// - after startup of mail
// In general, this allocator should be enabled before
// we startup and disabled after we startup if memory is a concern.
//***********************************************************

PR_STATIC_CALLBACK(void *)
zlibAlloc(void *opaque, uInt items, uInt size)
{
  nsRecyclingAllocator *zallocator = (nsRecyclingAllocator *)opaque;
  if (zallocator) {
    // Bump up x4 allocations
    PRUint32 realitems = items;
    if (size == 4 && items < BY4ALLOC_ITEMS)
      realitems = BY4ALLOC_ITEMS;
     return zallocator->Calloc(realitems, size);
  }
  else
    return calloc(items, size);
}

PR_STATIC_CALLBACK(void)
zlibFree(void *opaque, void *ptr)
{
  nsRecyclingAllocator *zallocator = (nsRecyclingAllocator *)opaque;
  if (zallocator)
    zallocator->Free(ptr);
  else
    free(ptr);
  return;
}
#endif /* STANDALONE */

//***********************************************************
//      nsZipReadState  --  public methods
//***********************************************************

void nsZipReadState::Init(nsZipItem* aZipItem, PRFileDesc* aFd)
{
    PR_ASSERT(aFd);
    mItem = aZipItem;
    mCurPos = 0;
#ifndef STANDALONE
    // take ownership of the file descriptor
    mFd = aFd;
#endif

    if (mItem->compression != STORED) {
      memset(&mZs, 0, sizeof(mZs));

#ifndef STANDALONE
      //-- ensure we have our zlib allocator for better performance
      if (!gZlibAllocator) {
        gZlibAllocator = new nsRecyclingAllocator(NBUCKETS, NS_DEFAULT_RECYCLE_TIMEOUT, "libjar");
      }

      mZs.zalloc = zlibAlloc;
      mZs.zfree = zlibFree;
      mZs.opaque = gZlibAllocator;
#endif
      int zerr = inflateInit2(&mZs, -MAX_WBITS);
      PR_ASSERT(zerr == Z_OK);
    }
    mCrc = crc32(0L, Z_NULL, 0);
}

//***********************************************************
//      nsZipArchive  --  public methods
//***********************************************************


#ifdef STANDALONE
//---------------------------------------------
//  nsZipArchive::OpenArchive
//---------------------------------------------
PRInt32 nsZipArchive::OpenArchive(const char * aArchiveName)
{
  //-- validate arguments
  if (aArchiveName == 0 || *aArchiveName == '\0')
    return ZIP_ERR_PARAM;

  //-- open the physical file
  mFd = PR_Open(aArchiveName, PR_RDONLY, 0000);
  if (mFd == 0)
    return ZIP_ERR_DISK;

  //-- get table of contents for archive
  return BuildFileList(GetFd());
}

#else
PRInt32 nsZipArchive::OpenArchiveWithFileDesc(PRFileDesc* fd)
{
  //-- validate arguments
  if (fd == 0)
    return ZIP_ERR_PARAM;

  //-- get table of contents for archive
  return BuildFileList(fd);
}
#endif

//---------------------------------------------
//  nsZipArchive::Test
//---------------------------------------------
PRInt32 nsZipArchive::Test(const char *aEntryName, PRFileDesc* aFd)
{
  PRInt32 rv = ZIP_OK;
  nsZipItem *currItem = 0;

  if (aEntryName) // only test specified item
  {
    currItem = GetFileItem(aEntryName);
    if(!currItem)
      return ZIP_ERR_FNF;

    rv = TestItem(currItem, aFd);
  }
  else // test all items in archive
  {
    nsZipFind *iterator = FindInit(NULL);
    if (!iterator)
      return ZIP_ERR_GENERAL;

    // iterate over items in list
    while (ZIP_OK == FindNext(iterator, &currItem))
    {
      rv = TestItem(currItem, aFd);

      // check if crc check failed
      if (rv != ZIP_OK)
        break;

#if defined STANDALONE && defined XP_WIN
      ProcessWindowsMessages();
#endif
    }

    FindFree(iterator);
  }

  return rv;
}

//---------------------------------------------
//  nsZipArchive::CloseArchive
//---------------------------------------------
PRInt32 nsZipArchive::CloseArchive()
{
  
#ifndef STANDALONE
  PL_FinishArenaPool(&mArena);

  // CAUTION:
  // We dont need to delete each of the nsZipItem as the memory for
  // the zip item and the filename it holds are both allocated from the Arena.
  // Hence, destroying the Arena is like destroying all the memory
  // for all the nsZipItem in one shot. But if the ~nsZipItem is doing
  // anything more than cleaning up memory, we should start calling it.

  memset(mFiles, 0, sizeof(mFiles));

#else
  // delete nsZipItems in table
  nsZipItem* pItem;
  for (int i = 0; i < ZIP_TABSIZE; ++i)
  {
    pItem = mFiles[i];
    while (pItem != 0)
    {
      mFiles[i] = pItem->next;
      delete pItem;
      pItem = mFiles[i];
    }
    mFiles[i] = 0;              // make sure we don't double-delete
  }
  
  if (mFd) {
    PR_Close(mFd);
    mFd = 0;
  }
#endif

  return ZIP_OK;
}

//---------------------------------------------
// nsZipArchive::GetItem
//---------------------------------------------
PRInt32 nsZipArchive::GetItem(const char * aFilename, nsZipItem **result)
{
    //-- Parameter validity check
    if (aFilename == 0)
        return ZIP_ERR_PARAM;

    nsZipItem* item;

    //-- find file information
    item = GetFileItem(aFilename);
    if (item == 0)
    {
        return ZIP_ERR_FNF;
    }

    *result = item; // Return a pointer to the struct
    return ZIP_OK;
}

//---------------------------------------------
// nsZipArchive::ReadInit
//---------------------------------------------
PRInt32 nsZipArchive::ReadInit(const char* zipEntry, nsZipReadState* aRead,
                               PRFileDesc* aFd)
{
 
  //-- Parameter validity check
  if (zipEntry == 0 || aRead == 0)
    return ZIP_ERR_PARAM;

  //-- find item
  nsZipItem* item = GetFileItem(zipEntry);
  if (!item) {
    PR_Close(aFd);
    return ZIP_ERR_FNF;
  }

  //-- verify we can handle the compression type
  if (item->compression != DEFLATED && item->compression != STORED) {
    PR_Close(aFd);
    return ZIP_ERR_UNSUPPORTED;
  }

  SeekToItem(item, aFd);

#ifdef STANDALONE
  // in standalone, nsZipArchive owns the file descriptor
  mFd = aFd;
#endif
  
  // in non-standalone builds, the nsZipReadState will take ownership
  // of the file descriptor
  aRead->Init(item, aFd);
    
  return ZIP_OK;
}

//---------------------------------------------
// nsZipReadState::Available
//---------------------------------------------
PRUint32 nsZipReadState::Available()
{
  if (mItem->compression == DEFLATED)
    return (mItem->realsize - mZs.total_out);

  return mItem->size - mCurPos;
}

//---------------------------------------------
// nsZipArchive::ExtractFile
//---------------------------------------------
PRInt32 nsZipArchive::ExtractFile(const char* zipEntry, const char* aOutname,
                                  PRFileDesc* aFd)
{
  //-- Find item in archive
  nsZipItem* item = GetFileItem(zipEntry);
  if (!item)
    return ZIP_ERR_FNF;

  // delete any existing file so that we overwrite the file permissions
  PR_Delete(aOutname);

  PRFileDesc* fOut = PR_Open(aOutname, ZFILE_CREATE, item->mode);
  if (fOut == 0)
    return ZIP_ERR_DISK;

#if defined(XP_UNIX) && defined(STANDALONE)
  // When STANDALONE is defined, PR_Open ignores its 3d argument.
  mode_t msk = umask(0);
  umask(msk);
  chmod(aOutname, item->mode & ~msk);
#endif

  PRInt32 status = ExtractItemToFileDesc(item, fOut, aFd);
  PR_Close(fOut);

  if (status != ZIP_OK)
  {
    PR_Delete(aOutname);
  }
#if defined(XP_UNIX)
  else
  {
    if (item->isSymlink)
    {
      status = ResolveSymlink(aOutname, item);
    }
  }
#endif
  return status;
}

PRInt32
nsZipArchive::ExtractItemToFileDesc(nsZipItem* item, PRFileDesc* outFD,
                                    PRFileDesc* aFd)
{
  //-- sanity check arguments
  if (item == 0 || outFD == 0)
    return ZIP_ERR_PARAM;

  PRInt32 status;

  //-- extract the file using the appropriate method
  switch(item->compression)
  {
    case STORED:
      status = CopyItemToDisk(item, outFD, aFd);
      break;

    case DEFLATED:
      status = InflateItem(item, outFD, aFd);
      break;

    default:
      //-- unsupported compression type
      return ZIP_ERR_UNSUPPORTED;
  }

  return status;
}

//---------------------------------------------
// nsZipArchive::FindInit
//---------------------------------------------
nsZipFind* nsZipArchive::FindInit(const char * aPattern)
{
  PRBool  regExp = PR_FALSE;
  char*   pattern = 0;

  // validate the pattern
  if (aPattern)
  {
    switch (NS_WildCardValid((char*)aPattern))
    {
      case INVALID_SXP:
        return 0;

      case NON_SXP:
        regExp = PR_FALSE;
        break;

      case VALID_SXP:
        regExp = PR_TRUE;
        break;

      default:
        // undocumented return value from RegExpValid!
        PR_ASSERT(PR_FALSE);
        return 0;
    }

    pattern = PL_strdup(aPattern);
    if (!pattern)
      return 0;
  }

  return new nsZipFind(this, pattern, regExp);
}



//---------------------------------------------
// nsZipArchive::FindNext
//---------------------------------------------
PRInt32 nsZipArchive::FindNext(nsZipFind* aFind, nsZipItem** aResult)
{
  PRInt32    status;
  PRBool     found  = PR_FALSE;
  PRUint16   slot   = aFind->mSlot;
  nsZipItem* item   = aFind->mItem;

  if (aFind->mArchive != this)
    return ZIP_ERR_PARAM;

  // we start from last match, look for next
  while (slot < ZIP_TABSIZE && !found)
  {
    if (item != 0)
      item = item->next;    // move to next in current chain
    else
      item = mFiles[slot];  // starting a new slot

    if (item == 0)
    { // no more in this chain, move to next slot
      ++slot;
      continue;
    }
    else if (aFind->mPattern == 0)
      found = PR_TRUE;            // always match
    else if (aFind->mRegExp)
      found = (NS_WildCardMatch(item->name, aFind->mPattern, PR_FALSE) == MATCH);
    else
#if defined(STANDALONE) && defined(XP_MAC)
      // simulate <regexp>* matches
      found = (strncmp(item->name, aFind->mPattern, strlen(aFind->mPattern)) == 0);
#else
      found = (PL_strcmp(item->name, aFind->mPattern) == 0);
#endif
  }

  if (found)
  {
      *aResult = item;
      aFind->mSlot = slot;
      aFind->mItem = item;
      status = ZIP_OK;
  }
  else
    status = ZIP_ERR_FNF;

  return status;
}



//---------------------------------------------
// nsZipArchive::FindFree
//---------------------------------------------
PRInt32 nsZipArchive::FindFree(nsZipFind* aFind)
{
  if (aFind->mArchive != this)
    return ZIP_ERR_PARAM;

  delete aFind;
  return ZIP_OK;
}

#ifdef XP_UNIX
//---------------------------------------------
// nsZipArchive::ResolveSymlink
//---------------------------------------------
PRInt32 nsZipArchive::ResolveSymlink(const char *path, nsZipItem *item)
{
  PRInt32    status = ZIP_OK;
  if (item->isSymlink)
  {
    char buf[PATH_MAX+1];
    PRFileDesc * fIn = PR_Open(path, PR_RDONLY, 0000);
    if (fIn)
    {
      PRInt32 length = PATH_MAX;
      length = PR_Read(fIn,(void*)buf,length);
      PR_Close(fIn);
      fIn = 0;
      if (  length <= 0
      || (buf[length] = 0, PR_Delete(path)) != 0
      || symlink(buf, path) != 0)
      {
        status = ZIP_ERR_DISK;
      }
    } else {
      status = ZIP_ERR_DISK;
    }
    if (fIn)
    {
      PR_Close(fIn);
    }
  }
  return status;
}
#endif

//***********************************************************
//      nsZipArchive  --  private implementation
//***********************************************************

#define BR_BUF_SIZE 1024 /* backward read buffer size */

//---------------------------------------------
//  nsZipArchive::BuildFileList
//---------------------------------------------
PRInt32 nsZipArchive::BuildFileList(PRFileDesc* aFd)
{
  PRInt32   status = ZIP_OK;
  PRUint8   buf[4*BR_BUF_SIZE];

  ZipEnd     *End;

  //-----------------------------------------------------------------------
  // locate the central directory via the End record
  //-----------------------------------------------------------------------
  PRInt32  pos = 0L;
  PRInt32  bufsize = 0;

  //-- get archive size using end pos
  pos = PR_Seek(aFd, 0, PR_SEEK_END);
#ifndef STANDALONE
  if (pos <= 0)
#else
  if (pos || ((pos = ftell(aFd)) <= 0))
#endif
    status = ZIP_ERR_CORRUPT;

  while (status == ZIP_OK)
  {
    //-- read backwards in 1K-sized chunks (unless file is less than 1K)
    pos > BR_BUF_SIZE ? bufsize = BR_BUF_SIZE : bufsize = pos;
    pos -= bufsize;

    if (!ZIP_Seek(aFd, pos, PR_SEEK_SET))
    {
      status = ZIP_ERR_CORRUPT;
      break;
    }

    if (PR_Read(aFd, buf, bufsize) != (READTYPE)bufsize)
    {
      status = ZIP_ERR_CORRUPT;
      break;
    }

    //-- scan for ENDSIG
    PRUint8 *endp = buf + bufsize;
    PRUint32 endsig;
    PRBool bEndsigFound = PR_FALSE;

    for (endp -= ZIPEND_SIZE; endp >= buf; endp--)
    {
      endsig = xtolong(endp);
      if (endsig == ENDSIG)
      {
        bEndsigFound = PR_TRUE;
        break;
      }
    }

    if (bEndsigFound)
    {
      End = (ZipEnd *) endp;

      //-- set pos to start of central directory
      pos = xtolong(End->offset_central_dir);
      if (!ZIP_Seek(aFd, pos, PR_SEEK_SET))
        status = ZIP_ERR_CORRUPT;

      break;
    }

    if(pos <= 0)
      //-- We're at the beginning of the file, and still no sign
      //-- of the end signiture.  File must be corrupted!
      status = ZIP_ERR_CORRUPT;

    //-- backward read must overlap ZipEnd length
    pos += ZIPEND_SIZE;

  } /* while looking for end signature */


  //-------------------------------------------------------
  // read the central directory headers
  //-------------------------------------------------------
  if (status == ZIP_OK)
  {
    //-- we think we found the central directory, read in the first chunk
    pos = 0;
    bufsize = PR_Read(aFd, &buf, sizeof(buf));
    if (bufsize < (PRInt32)(ZIPCENTRAL_SIZE + ZIPEND_SIZE))
    {
      // We know we read the end sig and got pointed at the central
      // directory--there should be at least this much
      //
      // (technically there can be a completely empty archive with only a
      // ZipEnd structure; that's pointless and might as well be an error.)
      status = ZIP_ERR_DISK;
    }

    //-- verify it's a central directory record
    if (xtolong(buf) != CENTRALSIG)
      status = ZIP_ERR_CORRUPT;
  }

  //-- loop through directory records
  //
  //-- we enter the loop positioned at a central directory record
  //-- with enough valid data in the buffer to contain one
  while (status == ZIP_OK)
  {
    //-------------------------------------------------------
    // read the fixed-size data
    //-------------------------------------------------------
    ZipCentral* central = (ZipCentral*)(buf+pos);

    PRUint32 namelen = xtoint(central->filename_len);
    PRUint32 extralen = xtoint(central->extrafield_len);
    PRUint32 commentlen = xtoint(central->commentfield_len);

    //-- sanity check variable sizes and refuse to deal with
    //-- anything too big: it's likely a corrupt archive
    if (namelen > BR_BUF_SIZE || extralen > BR_BUF_SIZE || commentlen > 2*BR_BUF_SIZE) {
      status = ZIP_ERR_CORRUPT;
      break;
    }

#ifndef STANDALONE
    // Arena allocate the nsZipItem
    void *mem;
    PL_ARENA_ALLOCATE(mem, &mArena, sizeof(nsZipItem));
    // Use placement new to arena allcoate the nsZipItem
    nsZipItem* item = mem ? new (mem) nsZipItem() : nsnull;
#else
    nsZipItem* item = new nsZipItem();
#endif
    if (!item)
    {
      status = ZIP_ERR_MEMORY;
      break;
    }

    item->headerOffset = xtolong(central->localhdr_offset);
    item->dataOffset = 0;
    item->hasDataOffset = PR_FALSE;
    item->size = xtolong(central->size);
    item->realsize = xtolong(central->orglen);
    item->crc32 = xtolong(central->crc32);
    PRUint32 external_attributes = xtolong(central->external_attributes);
    item->mode = ExtractMode(external_attributes);
    item->isSymlink = IsSymlink(external_attributes);
    item->time = xtoint(central->time);
    item->date = xtoint(central->date);

    PRUint16 compression = xtoint(central->method);
    item->compression = (compression < UNSUPPORTED) ? (PRUint8)compression
                                                    : UNSUPPORTED;

    pos += ZIPCENTRAL_SIZE;

    //-------------------------------------------------------
    // get the item name
    //-------------------------------------------------------
#ifndef STANDALONE
    PL_ARENA_ALLOCATE(mem, &mArena, (namelen + 1));
    item->name = (char *) mem;
    if (!item->name)
    {
      status = ZIP_ERR_MEMORY;
      // No need to delete name. It gets deleted only when the entire arena
      // goes away.
      break;
    }
#else
    item->name = new char[namelen + 1];
    if (!item->name)
    {
      status = ZIP_ERR_MEMORY;
      delete item;
      break;
    }
#endif

    PRUint32 leftover = (PRUint32)(bufsize - pos);
    if (leftover < namelen)
    {
      //-- not enough data left in buffer for the name.
      //-- move leftover to top of buffer and read more
      memcpy(buf, buf+pos, leftover);
      bufsize = leftover + PR_Read(aFd, buf+leftover, bufsize-leftover);
      pos = 0;

      //-- make sure we were able to read enough
      if ((PRUint32)bufsize < namelen)
      {
        status = ZIP_ERR_CORRUPT;
        break;
      }
    }
    memcpy(item->name, buf+pos, namelen);
    item->name[namelen] = 0;

    //-- add item to file table
    PRUint32 hash = HashName(item->name);
    item->next = mFiles[hash];
    mFiles[hash] = item;

    pos += namelen;

    //-------------------------------------------------------
    // set up to process the next item at the top of loop
    //-------------------------------------------------------
    leftover = (PRUint32)(bufsize - pos);
    if (leftover < (extralen + commentlen + ZIPCENTRAL_SIZE))
    {
      //-- not enough data left to process at top of loop.
      //-- move leftover and read more
      memcpy(buf, buf+pos, leftover);
      bufsize = leftover + PR_Read(aFd, buf+leftover, bufsize-leftover);
      pos = 0;

      //-- make sure we were able to read enough
      if ((PRUint32)bufsize < (extralen + commentlen + sizeof(PRUint32)))
      {
        status = ZIP_ERR_CORRUPT;
        break;
      }
    }
    //-- set position to start of next ZipCentral record
    pos += extralen + commentlen;

    // verify we're at an expected structure in the file
    PRUint32 sig = xtolong(buf+pos);
    if (sig != CENTRALSIG)
    {
      //-- we must be done or else archive is corrupt
      if (sig != ENDSIG)
        status = ZIP_ERR_CORRUPT;
      break;
    }

    //-- make sure we've read enough
    if (bufsize < pos + ZIPCENTRAL_SIZE)
    {
      status = ZIP_ERR_CORRUPT;
      break;
    }
  } /* while reading central directory records */

#if defined(DEBUG)
  if (status != ZIP_OK) {
    const char* msgs[] = { "ZIP_OK", "ZIP_ERR_GENERAL", "ZIP_ERR_MEMORY", "ZIP_ERR_DISK", "ZIP_ERR_CORRUPT", "ZIP_ERR_PARAM", "ZIP_ERR_FNF", "ZIP_ERR_UNSUPPORTED", "ZIP_ERR_SMALLBUF", "UNKNOWN" };
    printf("nsZipArchive::BuildFileList  status = %d '%s'\n", (int)status, msgs[(status <= 0 && status >= -8) ? -status : 9]);
  }
#endif

  return status;
}

//---------------------------------------------
// nsZipArchive::GetFileItem
//---------------------------------------------
nsZipItem*  nsZipArchive::GetFileItem(const char * zipEntry)
{
  PR_ASSERT(zipEntry != 0);

  nsZipItem* item = mFiles[ HashName(zipEntry) ];

  for (; item != 0; item = item->next)
  {
    if (0 == PL_strcmp(zipEntry, item->name))
      break; //-- found it
  }

  return item;
}


//---------------------------------------------
// nsZipArchive::HashName
//---------------------------------------------
PRUint32 nsZipArchive::HashName(const char* aName)
{
  PRUint32 val = 0;
  PRUint8* c;

  PR_ASSERT(aName != 0);

  for (c = (PRUint8*)aName; *c != 0; c++) {
    val = val*37 + *c;
  }

  return (val % ZIP_TABSIZE);
}

//---------------------------------------------
// nsZipArchive::SeekToItem
//---------------------------------------------
PRInt32  nsZipArchive::SeekToItem(const nsZipItem* aItem, PRFileDesc* aFd)
{
  PR_ASSERT (aItem);

  PRFileDesc* fd = aFd;
  
  //-- the first time an item is used we need to calculate its offset
  if (!aItem->hasDataOffset)
  {
    //-- read local header to get variable length values and calculate
    //-- the real data offset
    //--
    //-- NOTE: extralen is different in central header and local header
    //--       for archives created using the Unix "zip" utility. To set
    //--       the offset accurately we need the _local_ extralen.
    if (!ZIP_Seek(fd, aItem->headerOffset, PR_SEEK_SET))
      return ZIP_ERR_CORRUPT;

    ZipLocal   Local;
    if (PR_Read(fd, (char*)&Local, ZIPLOCAL_SIZE) != (READTYPE) ZIPLOCAL_SIZE
         || xtolong(Local.signature) != LOCALSIG)
    {
      //-- read error or local header not found
      return ZIP_ERR_CORRUPT;
    }

    ((nsZipItem*)aItem)->dataOffset = aItem->headerOffset +
                                      ZIPLOCAL_SIZE +
                                      xtoint(Local.filename_len) +
                                      xtoint(Local.extrafield_len);
    ((nsZipItem*)aItem)->hasDataOffset = PR_TRUE;
  }

  //-- move to start of file in archive
  if (!ZIP_Seek(fd, aItem->dataOffset, PR_SEEK_SET))
    return  ZIP_ERR_CORRUPT;

  return ZIP_OK;
}

//---------------------------------------------
// nsZipArchive::CopyItemToDisk
//---------------------------------------------
PRInt32
nsZipArchive::CopyItemToDisk(const nsZipItem* aItem,
                             PRFileDesc* fOut, PRFileDesc* aFd)
{
  PRInt32     status = ZIP_OK;
  PRUint32    chunk, pos, size, crc;

  PR_ASSERT(aItem != 0 && fOut != 0);

  PRFileDesc* fd = aFd;
  
  //-- move to the start of file's data
  if (SeekToItem(aItem, aFd) != ZIP_OK)
    return ZIP_ERR_CORRUPT;

  char buf[ZIP_BUFLEN];

  //-- initialize crc
  crc = crc32(0L, Z_NULL, 0);

  //-- copy chunks until file is done
  size = aItem->size;
  for (pos=0; pos < size; pos += chunk)
  {
    chunk = (pos+ZIP_BUFLEN <= size) ? ZIP_BUFLEN : size - pos;

    if (PR_Read(fd, buf, chunk) != (READTYPE)chunk)
    {
      //-- unexpected end of data in archive
      status = ZIP_ERR_CORRUPT;
      break;
    }

    //-- incrementally update crc32
    crc = crc32(crc, (const unsigned char*)buf, chunk);

    if (PR_Write(fOut, buf, chunk) < (READTYPE)chunk)
    {
      //-- Couldn't write all the data (disk full?)
      status = ZIP_ERR_DISK;
      break;
    }
  }

  //-- verify crc32
  if ((status == ZIP_OK) && (crc != aItem->crc32))
      status = ZIP_ERR_CORRUPT;

  return status;
}

#ifndef STANDALONE
//------------------------------------------
// nsZipArchive::Read
//------------------------------------------
PRInt32
nsZipReadState::Read(char* aBuffer, PRUint32 aCount,
                     PRUint32* aBytesRead)
{
  if (!aBuffer)
    return ZIP_ERR_GENERAL;

  PRInt32 result;

  if (!Available()) {
    *aBytesRead = 0;
    return ZIP_OK;
  }

  switch (mItem->compression) {
  case DEFLATED:
    result = ContinueInflate(aBuffer, aCount, aBytesRead);
    break;
  case STORED:
    result = ContinueCopy(aBuffer, aCount, aBytesRead);
    break;
  default:
    result = ZIP_ERR_UNSUPPORTED;
  }

  // be agressive about closing!
  // note that sometimes, we will close mFd before we've finished
  // deflating - this is because zlib buffers the input
  if (mCurPos >= mItem->size && mFd) {
    PR_Close(mFd);
    mFd = NULL;
  }

  return result;
}

PRInt32
nsZipReadState::ContinueInflate(char* aBuffer, PRUint32 aCount,
                                PRUint32* aBytesRead)
{

  // just some stuff that will be helpful later
  const PRUint32 inSize = mItem->size;
  const PRUint32 outSize = mItem->realsize;

  int zerr = Z_OK;
  //-- inflate loop

  const PRUint32 oldTotalOut = mZs.total_out;

  mZs.next_out = (unsigned char*)aBuffer;
  mZs.avail_out = ((mZs.total_out + aCount) < outSize) ?
    aCount : (outSize - mZs.total_out);

  // make sure we aren't reading too much
  PR_ASSERT(mZs.avail_out <= aCount);
  
  *aBytesRead = 0;
  while (mZs.avail_out != 0 && zerr == Z_OK) {
    
    if (mZs.avail_in == 0 && mCurPos < inSize) {
      // time to fill the buffer!
      PRUint32 bytesToRead = ((mCurPos + ZIP_BUFLEN) < inSize) ?
        ZIP_BUFLEN : inSize - mCurPos;

      PR_ASSERT(mFd);
      PRInt32 bytesRead = PR_Read(mFd, mReadBuf, bytesToRead);
      if (bytesRead < 0) {
        zerr = Z_ERRNO;
        break;
      }

      mCrc = crc32(mCrc, mReadBuf, bytesRead);
      mCurPos += bytesRead;

      // now reset the state
      mZs.next_in = mReadBuf;
      mZs.avail_in = bytesRead;
    }

#if 0
    // stop returning valid data as soon as we know we have a bad CRC
    if (mCurPos >= inSize &&
        mCrc != mItem->crc32) {
      // asserting because while this rarely happens, you definitely
      // want to catch it in debug builds!
      PR_ASSERT(0);
      return ZIP_ERR_CORRUPT;
    }
#endif
    
    // now inflate
    zerr = inflate(&mZs, Z_PARTIAL_FLUSH);
  }

  if ((zerr != Z_OK) && (zerr != Z_STREAM_END))
    return ZIP_ERR_CORRUPT;
  
  *aBytesRead = (mZs.total_out - oldTotalOut);

  // be aggressive about closing the stream
  // for some reason we don't always get Z_STREAM_END
  if (zerr == Z_STREAM_END || mZs.total_out == mItem->realsize) {
    inflateEnd(&mZs);
  }

  return ZIP_OK;
}

PRInt32 nsZipReadState::ContinueCopy(char* aBuf,
                                     PRUint32 aCount,
                                     PRUint32* aBytesRead)
{
  // we still use the fields of mZs, we just use memcpy rather than inflate

  if (mCurPos + aCount > mItem->size)
    aCount = (mItem->size - mCurPos);

  PR_ASSERT(mFd);
  PRInt32 bytesRead = PR_Read(mFd, aBuf, aCount);
  if (bytesRead < 0)
    return ZIP_ERR_DISK;

  mCurPos += bytesRead;
  if (bytesRead != aCount)
    // either file was truncated or archive lied about size
    return ZIP_ERR_CORRUPT;

  *aBytesRead = bytesRead;

  return ZIP_OK;
}

#endif

//---------------------------------------------
// nsZipArchive::InflateItem
//---------------------------------------------
PRInt32 nsZipArchive::InflateItem(const nsZipItem* aItem, PRFileDesc* fOut, PRFileDesc* aFd)
/*
 * This function either inflates an archive item to disk, to the
 * file specified by aOutname, or into a buffer specified by
 * bigBuf. bigBuf then gets copied into the "real" output
 * buffer a chunk at a time by ReadInflatedItem(). Memory-wise,
 * this is inefficient, since it stores an entire copy of the
 * decompressed item in memory, then copies it to the caller's
 * buffer. A more memory-efficient implementation is possible,
 * in which the outbuf in this function could be returned
 * directly, but implementing it would be complex.
 */
{
  PRInt32     status = ZIP_OK;
  PRUint32    chunk, inpos, outpos, size, crc;
  PRUint32    bigBufSize=0;
  z_stream    zs;
  int         zerr;
  PRBool      bInflating = PR_FALSE;
  PRBool      bRead;
  PRBool      bWrote;
  PRBool      bToFile;
  Bytef*      old_next_out;

  // -- if aOutname is null, we'll be writing to a buffer instead of a file
  if (fOut != 0)
  {
    PR_ASSERT(aItem != 0);
    bToFile = PR_TRUE;
  }
  else
  {
    // -- Writing to a buffer, so bigBuf must not be null.
    PR_ASSERT(aItem);
    bToFile = PR_FALSE;
    bigBufSize = aItem->realsize;
  }

  //-- move to the start of file's data
  if (SeekToItem(aItem, aFd) != ZIP_OK)
    return ZIP_ERR_CORRUPT;

  //-- allocate deflation buffers
  Bytef inbuf[ZIP_BUFLEN];
  Bytef outbuf[ZIP_BUFLEN];

  //-- set up the inflate
  memset(&zs, 0, sizeof(zs));
#ifndef STANDALONE
  //-- ensure we have our zlib allocator for better performance
  if (!gZlibAllocator) {
    gZlibAllocator = new nsRecyclingAllocator(NBUCKETS, NS_DEFAULT_RECYCLE_TIMEOUT, "libjar");
  }

  zs.zalloc = zlibAlloc;
  zs.zfree = zlibFree;
  zs.opaque = gZlibAllocator;
#endif

  zerr = inflateInit2(&zs, -MAX_WBITS);
  if (zerr != Z_OK)
  {
    status = ZIP_ERR_GENERAL;
    goto cleanup;
  }
  bInflating = PR_TRUE;


  //-- inflate loop
  size = aItem->size;
  outpos = inpos = 0;
  zs.next_out = outbuf;
  zs.avail_out = ZIP_BUFLEN;
  crc = crc32(0L, Z_NULL, 0);
  while (zerr == Z_OK)
  {
    bRead  = PR_FALSE;
    bWrote = PR_FALSE;

    if (zs.avail_in == 0 && zs.total_in < size)
    {
      //-- no data to inflate yet still more in file:
      //-- read another chunk of compressed data

      inpos = zs.total_in;  // input position
      chunk = (inpos + ZIP_BUFLEN <= size) ? ZIP_BUFLEN : size - inpos;

      if (PR_Read(aFd, inbuf, chunk) != (READTYPE)chunk)
      {
        //-- unexpected end of data
        status = ZIP_ERR_CORRUPT;
        break;
      }

      zs.next_in  = inbuf;
      zs.avail_in = chunk;
      bRead       = PR_TRUE;
    }

    if (zs.avail_out == 0)
    {
      //-- write inflated buffer to disk and make space
      if (PR_Write(fOut, outbuf, ZIP_BUFLEN) < ZIP_BUFLEN)
      {
        //-- Couldn't write all the data (disk full?)
        status = ZIP_ERR_DISK;
        break;
      }

      outpos = zs.total_out;
      zs.next_out  = outbuf;
      zs.avail_out = ZIP_BUFLEN;
      bWrote       = PR_TRUE;
    }

    if(bRead || bWrote)
    {
      old_next_out = zs.next_out;

      zerr = inflate(&zs, Z_PARTIAL_FLUSH);

      //-- incrementally update crc32
      crc = crc32(crc, (const unsigned char*)old_next_out, zs.next_out - old_next_out);
    }
    else
      zerr = Z_STREAM_END;

#if defined STANDALONE && defined XP_WIN
    ProcessWindowsMessages();
#endif
  } // while

  //-- verify crc32
  if ((status == ZIP_OK) && (crc != aItem->crc32))
  {
      status = ZIP_ERR_CORRUPT;
      goto cleanup;
  }

  //-- write last inflated bit to disk
  if (zerr == Z_STREAM_END && outpos < zs.total_out)
  {
    chunk = zs.total_out - outpos;
    if (PR_Write(fOut, outbuf, chunk) < (READTYPE)chunk)
      status = ZIP_ERR_DISK;
  }

  //-- convert zlib error to return value
  if (status == ZIP_OK && zerr != Z_OK && zerr != Z_STREAM_END)
  {
    status = (zerr == Z_MEM_ERROR) ? ZIP_ERR_MEMORY : ZIP_ERR_CORRUPT;
  }

  //-- if found no errors make sure we've converted the whole thing
  PR_ASSERT(status != ZIP_OK || zs.total_in == aItem->size);
  PR_ASSERT(status != ZIP_OK || zs.total_out == aItem->realsize);

cleanup:
  if (bInflating)
  {
    //-- free zlib internal state
    inflateEnd(&zs);
  }

  return status;
}

//---------------------------------------------
// nsZipArchive::TestItem
//---------------------------------------------
PRInt32 nsZipArchive::TestItem(const nsZipItem* aItem, PRFileDesc* aFd)
{
  Bytef inbuf[ZIP_BUFLEN], outbuf[ZIP_BUFLEN], *old_next_out;
  PRUint32 size, chunk=0, inpos, crc;
  PRInt32 status = ZIP_OK;
  int zerr = Z_OK;
  z_stream zs;
  PRBool bInflating = PR_FALSE;
  PRBool bRead;
  PRBool bWrote;

  //-- param checks
  if (!aItem)
    return ZIP_ERR_PARAM;
  if (aItem->compression != STORED && aItem->compression != DEFLATED)
    return ZIP_ERR_UNSUPPORTED;

  //-- move to the start of file's data
  if (SeekToItem(aItem, aFd) != ZIP_OK)
    return ZIP_ERR_CORRUPT;

  //-- set up the inflate if DEFLATED
  if (aItem->compression == DEFLATED)
  {
    memset(&zs, 0, sizeof(zs));
    zerr = inflateInit2(&zs, -MAX_WBITS);
    if (zerr != Z_OK)
    {
      status = ZIP_ERR_GENERAL;
      goto cleanup;
    }
    else
    {
      zs.next_out = outbuf;
      zs.avail_out = ZIP_BUFLEN;
    }
    bInflating = PR_TRUE;
  }

  //-- initialize crc checksum
  crc = crc32(0L, Z_NULL, 0);

  size = aItem->size;
  inpos = 0;

  //-- read in ZIP_BUFLEN-sized chunks of item
  //-- inflating if item is DEFLATED
  while (zerr == Z_OK)
  {
    bRead = PR_FALSE;  // used to check if new data to inflate
    bWrote = PR_FALSE; // used to reset zs.next_out to outbuf
                       //   when outbuf fills up

    //-- read to inbuf
    if (aItem->compression == DEFLATED)
    {
      if (zs.avail_in == 0 && zs.total_in < size)
      {
        //-- no data to inflate yet still more in file:
        //-- read another chunk of compressed data

        inpos = zs.total_in;  // input position
        chunk = (inpos + ZIP_BUFLEN <= size) ? ZIP_BUFLEN : size - inpos;

        if (PR_Read(aFd, inbuf, chunk) != (READTYPE)chunk)
        {
          //-- unexpected end of data
          status = ZIP_ERR_CORRUPT;
          break;
        }

        zs.next_in  = inbuf;
        zs.avail_in = chunk;
        bRead = PR_TRUE;
      }

      if (zs.avail_out == 0)
      {
        //-- reuse output buffer
        zs.next_out = outbuf;
        zs.avail_out = ZIP_BUFLEN;
        bWrote = PR_TRUE; // mimic writing to disk/memory
      }
    }
    else
    {
      if (inpos < size)
      {
        //-- read a chunk in
        chunk = (inpos + ZIP_BUFLEN <= size) ? ZIP_BUFLEN : size - inpos;

        if (PR_Read(aFd, inbuf, chunk) != (READTYPE)chunk)
        {
          //-- unexpected end of data
          status = ZIP_ERR_CORRUPT;
          break;
        }

        inpos += chunk;
      }
      else
      {
        //-- finished reading STORED item
        break;
      }
    }

    //-- inflate if item is DEFLATED
    if (aItem->compression == DEFLATED)
    {
      if (bRead || bWrote)
      {
        old_next_out = zs.next_out;

        zerr = inflate(&zs, Z_PARTIAL_FLUSH);

        //-- incrementally update crc checksum
        crc = crc32(crc, (const unsigned char*)old_next_out, zs.next_out - old_next_out);
      }
      else
        zerr = Z_STREAM_END;
    }
    //-- else just use input buffer containing data from STORED item
    else
    {
      //-- incrementally update crc checksum
      crc = crc32(crc, (const unsigned char*)inbuf, chunk);
    }
  }

  //-- convert zlib error to return value
  if (status == ZIP_OK && zerr != Z_OK && zerr != Z_STREAM_END)
  {
    status = (zerr == Z_MEM_ERROR) ? ZIP_ERR_MEMORY : ZIP_ERR_CORRUPT;
    goto cleanup;
  }

  //-- verify computed crc checksum against header info crc
  if (status == ZIP_OK && crc != aItem->crc32)
  {
    status = ZIP_ERR_CORRUPT;
  }

cleanup:
  if (bInflating)
  {
    //-- free zlib internal state
    inflateEnd(&zs);
  }

  return status;
}

//------------------------------------------
// nsZipArchive constructor and destructor
//------------------------------------------

MOZ_DECL_CTOR_COUNTER(nsZipArchive)

nsZipArchive::nsZipArchive()
  : kMagic(ZIP_MAGIC), kArenaBlockSize(1*1024)
#ifdef STANDALONE
    , mFd(0)
#endif
{
  MOZ_COUNT_CTOR(nsZipArchive);

  // initialize the table to NULL
  memset(mFiles, 0, sizeof mFiles);

#ifndef STANDALONE
  // Initialize our arena
  PL_INIT_ARENA_POOL(&mArena, "ZipArena", kArenaBlockSize);
#endif
}

nsZipArchive::~nsZipArchive()
{
  CloseArchive();

  MOZ_COUNT_DTOR(nsZipArchive);
}




//------------------------------------------
// nsZipItem constructor and destructor
//------------------------------------------

nsZipItem::nsZipItem() : name(0), headerOffset(0), dataOffset(0), next(0), hasDataOffset(0), isSymlink(0)
{
}

nsZipItem::~nsZipItem()
{
#ifdef STANDALONE
  if (name != 0)
  {
    delete [] name;
    name = 0;
  }
#endif
}

//------------------------------------------
// nsZipReadState
//------------------------------------------

MOZ_DECL_CTOR_COUNTER(nsZipReadState)

//------------------------------------------
// nsZipFind constructor and destructor
//------------------------------------------

MOZ_DECL_CTOR_COUNTER(nsZipFind)

nsZipFind::nsZipFind(nsZipArchive* aZip, char* aPattern, PRBool aRegExp)
: kMagic(ZIPFIND_MAGIC),
  mArchive(aZip),
  mPattern(aPattern),
  mSlot(0),
  mItem(0),
  mRegExp(aRegExp)
{
  MOZ_COUNT_CTOR(nsZipFind);
}

nsZipFind::~nsZipFind()
{
  if (mPattern != 0)
    PL_strfree(mPattern);

  MOZ_COUNT_DTOR(nsZipFind);
}

//------------------------------------------
// nsZipFind::GetArchive
//------------------------------------------
nsZipArchive* nsZipFind::GetArchive()
{
    if (!mArchive)
        return NULL;

    return mArchive;
}


//------------------------------------------
// helper functions
//------------------------------------------

/*
 *  x t o i n t
 *
 *  Converts a two byte ugly endianed integer
 *  to our platform's integer.
 */
static PRUint16 xtoint (unsigned char *ii)
{
  return (PRUint16) ((ii [0]) | (ii [1] << 8));
}

/*
 *  x t o l o n g
 *
 *  Converts a four byte ugly endianed integer
 *  to our platform's integer.
 */
static PRUint32 xtolong (unsigned char *ll)
{
  PRUint32 ret;

  ret =  (
         (((PRUint32) ll [0]) <<  0) |
         (((PRUint32) ll [1]) <<  8) |
         (((PRUint32) ll [2]) << 16) |
         (((PRUint32) ll [3]) << 24)
        );

  return ret;
}

/*
 * ExtractMode
 *
 * Extracts bits 17-24 from a 32-bit unsigned long
 * representation of the external attributes field.
 * Subsequently it tacks on the implicit user-read
 * bit.
 */
static PRUint16 ExtractMode(PRUint32 ext_attr)
{
    ext_attr &= 0x00FF0000;
    ext_attr >>= 16;
    ext_attr |= 0x00000100;

    return (PRUint16) ext_attr;
}


/*
 *
 *  Return true if the attributes are for a symbolic link
 *
 */

static PRBool IsSymlink(PRUint32 ext_attr)
{
  return (((ext_attr>>16) & S_IFMT) == S_IFLNK) ? PR_TRUE : PR_FALSE;
}

#ifndef STANDALONE
PRTime
nsZipItem::GetModTime()
{
  char buffer[17];

  PRUint16 aDate = this->date;
  PRUint16 aTime = this->time;

  PR_snprintf(buffer, sizeof(buffer), "%02d/%02d/%04d %02d:%02d",
        ((aDate >> 5) & 0x0F), (aDate & 0x1F), (aDate >> 9) + 1980,
        ((aTime >> 11) & 0x1F), ((aTime >> 5) & 0x3F));

  PRTime result;
  PR_ParseTimeString(buffer, PR_FALSE, &result);
  return result;
}
#endif