From 62146bd9a6e7a738758a71ce2adb917f11657523 Mon Sep 17 00:00:00 2001 From: Issac Gerges Date: Wed, 18 Sep 2024 09:22:08 -0400 Subject: [PATCH] Add check for local desc flag in crc check --- headers/entryHeader.js | 2 ++ .../crc/good_crc_trailing_data_descriptor.zip | Bin 0 -> 1525 bytes test/crc/index.js | 18 ++++++++++++++++++ zipEntry.js | 2 +- 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/crc/good_crc_trailing_data_descriptor.zip diff --git a/headers/entryHeader.js b/headers/entryHeader.js index 61000ad..fb06524 100644 --- a/headers/entryHeader.js +++ b/headers/entryHeader.js @@ -220,6 +220,8 @@ module.exports = function () { _localHeader.version = data.readUInt16LE(Constants.LOCVER); // general purpose bit flag _localHeader.flags = data.readUInt16LE(Constants.LOCFLG); + // desc flag + _localHeader.flags_desc = (_localHeader.flags & Constants.FLG_DESC) > 0; // compression method _localHeader.method = data.readUInt16LE(Constants.LOCHOW); // modification time (2 bytes time, 2 bytes date) diff --git a/test/crc/good_crc_trailing_data_descriptor.zip b/test/crc/good_crc_trailing_data_descriptor.zip new file mode 100644 index 0000000000000000000000000000000000000000..e208087406bc311211e39a8ca3f5bd6001cde19c GIT binary patch literal 1525 zcmZY9X*ARe6aeu57-I_~ThcsbdxR`WvLy+_ATbP$dJUd1k1UaqUSoKX$?|O3BH6QR zCe#$k7~2Sggs~h9vWztb<>~dM^WMGZ+H;U7}v?;T%GIVy* ztyyl1<}=VKx3+14)XYcMaI=f&T!nD0m?8HvuMMe07;2 zWBF>;&0#;CqSP>3_T#u9=yURhY^#FR-|BtR06#aYm1@8K9GYD^HbSS89IlR0^p3{EnE|D$iO_JI z;SglBDuqb5Qs8*cvyI#>`J9hC&q<6wqfE+jtvcE}aoc`7=37usicqW1|I+~Ua&AT| z!!A-xs*5^O9XKD}cU4Y4(V-FC>_m%L{Z!X?hjK^9a_`ikQYI~G?KGFRV{F9og%XHS zl8k$zo7ZK&at%!y{%^~OVFhEPNjV8|HI};9scY5#61ll{{cBvL4Oo_r(uA#s_2^Qm zqaOtWtFm8^a!kTnpV7LMhdi;OU&bcOGvk!lXzu>b82`e%xjmJ$#t2mcEkQEa48ObG zGvU`*W+8U2NR36s~#8EEp}*&VsY7z?LC-I9Va!8#!0zVOT?yj zyn;`1S&-cR#g4+EK-}%Eq3&t*i#;<%byjGku&YAVher9GUnB;Oh;r_2tp{)5UY{Ao zSKE569J_a#!zAj$KRO!?))kc{dj!lV8?EgtE!Gf9)1n_ktWckVB6C6zT2-=SW`5qc zr6UVwH;YT6V(M*`0!Ce)7a}9CF^@^p;69Aquk3QhR`r@DD))NS$6K6g&CNGLN!{*> z{6w`1Yj;dm2;_dRo0+|s!C8roOS!+u@mhqe6?Wde8$Nb4=%%8|JE$fD>9MvSp4%23P}AcGw7feM0_9rCo|-S Y%>Pl@4gul$ehxZF$Acbw@C*R{0a~=tcmMzZ literal 0 HcmV?d00001 diff --git a/test/crc/index.js b/test/crc/index.js index cf2a01d..4da686f 100644 --- a/test/crc/index.js +++ b/test/crc/index.js @@ -26,6 +26,24 @@ describe("crc", () => { }); }); + it("Good CRC - trailing data descriptor ", (done) => { + const goodZip = new Zip(path.join(__dirname, "good_crc_trailing_data_descriptor.zip")); + const entries = goodZip.getEntries(); + assert(entries.length === 1, "Good CRC: Test archive contains exactly 1 file"); + + const testFile = entries.filter(function (entry) { + return entry.entryName === "lorem_ipsum.txt"; + }); + assert(testFile.length === 1, "Good CRC: lorem_ipsum.txt file exists as archive entry"); + + const testFileEntryName = testFile[0].entryName; + goodZip.readAsTextAsync(testFileEntryName, function (data, err) { + assert(!err, "Good CRC: error object not present"); + assert(data && data.length, "Good CRC: buffer not empty"); + done(); + }); + }); + it("Bad CRC - async method returns err string", (done) => { const badZip = new Zip(path.join(__dirname, "bad_crc.zip")); const entries = badZip.getEntries(); diff --git a/zipEntry.js b/zipEntry.js index e7804b6..0e506ce 100644 --- a/zipEntry.js +++ b/zipEntry.js @@ -30,7 +30,7 @@ module.exports = function (/** object */ options, /*Buffer*/ input) { function crc32OK(data) { // if bit 3 (0x08) of the general-purpose flags field is set, then the CRC-32 and file sizes are not known when the local header is written - if (!_centralHeader.flags_desc) { + if (!_centralHeader.flags_desc && !_centralHeader.localHeader.flags_desc) { if (Utils.crc32(data) !== _centralHeader.localHeader.crc) { return false; }