You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2019/11/02 15:03:43 UTC

[GitHub] [commons-compress] bodewig commented on a change in pull request #85: COMPRESS-497 Handle missing endheader offset

bodewig commented on a change in pull request #85: COMPRESS-497 Handle missing endheader offset
URL: https://github.com/apache/commons-compress/pull/85#discussion_r341814277
 
 

 ##########
 File path: src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
 ##########
 @@ -441,17 +441,81 @@ private Archive readHeaders(final byte[] password) throws IOException {
                     archiveVersionMajor, archiveVersionMinor));
         }
 
+        boolean headerLooksValid = false;  // See https://www.7-zip.org/recover.html - "There is no correct End Header at the end of archive"
         final long startHeaderCrc = 0xffffFFFFL & buf.getInt();
-        final StartHeader startHeader = readStartHeader(startHeaderCrc);
+        if (startHeaderCrc == 0) {
+            // This is an indication of a corrupt header - peek the next 20 bytes
+            long currentPosition = channel.position();
+            ByteBuffer peekBuf = ByteBuffer.allocate(20);
+            readFully(peekBuf);
+            channel.position(currentPosition);
+            // Header invalid if all data is 0
+            while (peekBuf.hasRemaining()) {
+                if (peekBuf.get()!=0) {
+                    headerLooksValid = true;
+                    break;
+                }
+            }
+        } else {
+            headerLooksValid = true;
+        }
+
+        if (headerLooksValid) {
+            final StartHeader startHeader = readStartHeader(startHeaderCrc);
+            return initializeArchive(startHeader, password, true);
+        } else {
+            // No valid header found - probably first file of multipart archive was removed too early. Scan for end header.
 
 Review comment:
   Could you please extract the block starting here into a new method?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services