]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
7zip: Reduce archive_read_format_7zip_bid depth
authorTobias Stoeckmann <tobias@stoeckmann.org>
Mon, 27 Apr 2026 16:55:13 +0000 (18:55 +0200)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Mon, 27 Apr 2026 18:10:01 +0000 (20:10 +0200)
Prepare content of archive_read_format_7zip_bid to be split into its own
function which eventually can be reused by slurp_central_directory.

No real functional change (split if-blocks).

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
libarchive/archive_read_support_format_7zip.c

index a4f1fe76f6511e344bbb178965494d0e30776f9e..c98aa1256976a31fae8e3e3c2c5efdef65842ec8 100644 (file)
@@ -533,6 +533,9 @@ static int
 archive_read_format_7zip_bid(struct archive_read *a, int best_bid)
 {
        const unsigned char *p;
+       ssize_t min_addr;
+       ssize_t offset;
+       ssize_t window;
 
        /* If someone has already bid more than 32, then avoid
           trashing the look-ahead buffers with a seek. */
@@ -555,31 +558,34 @@ archive_read_format_7zip_bid(struct archive_read *a, int best_bid)
         * performing a seek, find_elf_data_sec requires one,
         * thus a performance difference between the two is expected. 
         */
-       if ((p[0] == 'M' && p[1] == 'Z') || memcmp(p, "\x7F\x45LF", 4) == 0) {
-               const ssize_t min_addr = p[0] == 'M' ? find_pe_overlay(a) :
-                                                      find_elf_data_sec(a);
-               ssize_t offset = min_addr;
-               ssize_t window = 4096;
+       if ((p[0] == 'M' && p[1] == 'Z'))
+               min_addr = find_pe_overlay(a);
+       else if (memcmp(p, "\x7F\x45LF", 4) == 0)
+               min_addr = find_elf_data_sec(a);
+       else
+               return (0);
+
+       offset = min_addr;
+       window = 4096;
+       while (offset + window <= (min_addr + SFX_MAX_OFFSET)) {
                ssize_t bytes_avail;
-               while (offset + window <= (min_addr + SFX_MAX_OFFSET)) {
-                       const unsigned char *buff = __archive_read_ahead(a,
-                                       offset + window, &bytes_avail);
-                       if (buff == NULL) {
-                               /* Remaining bytes are less than window. */
-                               window >>= 1;
-                               if (window < 0x40)
-                                       return (0);
-                               continue;
-                       }
-                       p = buff + offset;
-                       while (p + 32 < buff + bytes_avail) {
-                               int step = check_7zip_header_in_sfx(p);
-                               if (step == 0)
-                                       return (48);
-                               p += step;
-                       }
-                       offset = p - buff;
+               const unsigned char *buff = __archive_read_ahead(a,
+                               offset + window, &bytes_avail);
+               if (buff == NULL) {
+                       /* Remaining bytes are less than window. */
+                       window >>= 1;
+                       if (window < 0x40)
+                               return (0);
+                       continue;
+               }
+               p = buff + offset;
+               while (p + 32 < buff + bytes_avail) {
+                       int step = check_7zip_header_in_sfx(p);
+                       if (step == 0)
+                               return (48);
+                       p += step;
                }
+               offset = p - buff;
        }
        return (0);
 }