@zip.js/zip.js 2.18.0 → 2.18.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,6 +26,6 @@
26
26
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
28
 
29
- const VERSION = "2.18.0";
29
+ const VERSION = "2.18.2";
30
30
 
31
31
  export { VERSION };
@@ -1455,32 +1455,19 @@ async function detectOverlappingEntry({
1455
1455
  }) {
1456
1456
  let dataDescriptorLength = 0;
1457
1457
  if (dataDescriptor) {
1458
- if (extraFieldZip64) {
1459
- dataDescriptorLength = DATA_DESCRIPTOR_RECORD_ZIP_64_LENGTH;
1460
- } else {
1461
- dataDescriptorLength = DATA_DESCRIPTOR_RECORD_LENGTH;
1462
- }
1463
- }
1464
- if (dataDescriptorLength) {
1465
- const dataDescriptorArray = await readUint8Array(reader, dataOffset + compressedSize, dataDescriptorLength + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH);
1458
+ const zip64 = Boolean(extraFieldZip64);
1459
+ const dataDescriptorArray = await readUint8Array(reader, dataOffset + compressedSize, DATA_DESCRIPTOR_RECORD_ZIP_64_LENGTH + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH);
1466
1460
  const dataDescriptorView = getDataView(dataDescriptorArray);
1467
- let signature = dataDescriptorArray.length == dataDescriptorLength + DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH &&
1468
- getUint32(dataDescriptorView, 0) == DATA_DESCRIPTOR_RECORD_SIGNATURE;
1469
- if (signature) {
1470
- const signedDataDescriptor = readDataDescriptor(dataDescriptorView, DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH, extraFieldZip64);
1471
- const matchCrc32 = (fileEntry.encrypted && !fileEntry.zipCrypto) || signedDataDescriptor.crc32 == crc32;
1472
- if (matchCrc32 &&
1473
- signedDataDescriptor.compressedSize == compressedSize &&
1474
- signedDataDescriptor.uncompressedSize == uncompressedSize) {
1475
- dataDescriptorLength += DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH;
1476
- } else {
1477
- signature = false;
1478
- }
1479
- }
1480
- if (dataDescriptorArray.length >= dataDescriptorLength) {
1481
- const localDataDescriptor = readDataDescriptor(dataDescriptorView, signature ? DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH : 0, extraFieldZip64);
1482
- localDataDescriptor.signature = signature;
1461
+ const candidates = [[zip64, true], [zip64, false], [!zip64, true], [!zip64, false]]
1462
+ .map(([zip64Layout, signature]) => readDataDescriptor(dataDescriptorView, zip64Layout, signature))
1463
+ .filter(candidate => candidate && candidate.compressedSize == compressedSize && candidate.uncompressedSize == uncompressedSize);
1464
+ const localDataDescriptor = candidates.find(candidate => candidate.crc32 == crc32) || candidates[0] ||
1465
+ readDataDescriptor(dataDescriptorView, zip64, true) || readDataDescriptor(dataDescriptorView, zip64, false);
1466
+ if (localDataDescriptor) {
1483
1467
  fileEntry.localDirectory.dataDescriptor = localDataDescriptor;
1468
+ dataDescriptorLength = getDataDescriptorLength(localDataDescriptor.zip64, localDataDescriptor.signature);
1469
+ } else {
1470
+ dataDescriptorLength = getDataDescriptorLength(zip64, false);
1484
1471
  }
1485
1472
  }
1486
1473
  const range = {
@@ -1545,18 +1532,31 @@ function mergeRanges(sortedRanges, pendingRanges) {
1545
1532
  return mergedRanges;
1546
1533
  }
1547
1534
 
1548
- function readDataDescriptor(dataDescriptorView, offset, extraFieldZip64) {
1535
+ function readDataDescriptor(dataDescriptorView, zip64, signature) {
1536
+ const offset = signature ? DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH : 0;
1537
+ if (dataDescriptorView.byteLength < getDataDescriptorLength(zip64, signature) ||
1538
+ (signature && getUint32(dataDescriptorView, 0) != DATA_DESCRIPTOR_RECORD_SIGNATURE)) {
1539
+ return UNDEFINED_VALUE;
1540
+ }
1549
1541
  const crc32 = getUint32(dataDescriptorView, offset);
1550
1542
  let compressedSize;
1551
1543
  let uncompressedSize;
1552
- if (extraFieldZip64) {
1553
- compressedSize = getBigUint64(dataDescriptorView, offset + 4);
1554
- uncompressedSize = getBigUint64(dataDescriptorView, offset + 12);
1555
- } else {
1556
- compressedSize = getUint32(dataDescriptorView, offset + 4);
1557
- uncompressedSize = getUint32(dataDescriptorView, offset + 8);
1544
+ try {
1545
+ if (zip64) {
1546
+ compressedSize = getBigUint64(dataDescriptorView, offset + 4);
1547
+ uncompressedSize = getBigUint64(dataDescriptorView, offset + 12);
1548
+ } else {
1549
+ compressedSize = getUint32(dataDescriptorView, offset + 4);
1550
+ uncompressedSize = getUint32(dataDescriptorView, offset + 8);
1551
+ }
1552
+ } catch {
1553
+ return UNDEFINED_VALUE;
1558
1554
  }
1559
- return { crc32, compressedSize, uncompressedSize };
1555
+ return { signature, zip64, crc32, compressedSize, uncompressedSize };
1556
+ }
1557
+
1558
+ function getDataDescriptorLength(zip64, signature) {
1559
+ return (zip64 ? DATA_DESCRIPTOR_RECORD_ZIP_64_LENGTH : DATA_DESCRIPTOR_RECORD_LENGTH) + (signature ? DATA_DESCRIPTOR_RECORD_SIGNATURE_LENGTH : 0);
1560
1560
  }
1561
1561
 
1562
1562
  function getDiskOffset(reader, diskNumber) {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "A JavaScript library to zip and unzip files in the browser, Deno and Node.js",
4
4
  "author": "Gildas Lormeau",
5
5
  "license": "BSD-3-Clause",
6
- "version": "2.18.0",
6
+ "version": "2.18.2",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "zip",