From 8e33b47913fbcfa8fdfd7e58bf6cd1c3f525a448 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Thu, 22 Dec 2016 11:56:45 +0100 Subject: file-zip: further speed up DD search The previous implementation took 8.9 seconds with this command: tshark -Xlua_script:file-zip.lua -r TechnicLauncher.jar -Vx -ozip_archive.decompress:FALSE If the signature was not optional, we could optimize and avoid a linear search, using string.find with steps of four bytes on negative match. This would take 5.6 seconds (but does not handle a missing signature). The combined approach that first scans with string.find (assuming a signature) and then falling back to a linear search (assuming no signature) would take 14.4 seconds (terrible in the worst case). So try another approach, doing a byte for byte search (as before), but then delaying the signature check until the length is valid. This improves the running time to 7.5 seconds. --- lua/file-zip.lua | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/lua/file-zip.lua b/lua/file-zip.lua index dcfb165..d455540 100644 --- a/lua/file-zip.lua +++ b/lua/file-zip.lua @@ -196,21 +196,20 @@ local function find_data_desc(tvb) -- Scans (byte for byte) for the size field and try to confirm the validity -- of this length field. It might still have a false positive, but at least -- it allows for a linear scan through the file (without consulting CD). - while dd_offset + 8 < #data do - -- Size field is at offset 4 (if sig exists) or at offset 8 otherwise. - local size_offset - if Struct.unpack(" #data then return end - size_offset = dd_offset + 8 - else - size_offset = dd_offset + 4 - end - -- Validata size or continue with next byte on failure. - local comp_size = Struct.unpack("