ppkarwasz commented on code in PR #685:
URL: https://github.com/apache/commons-compress/pull/685#discussion_r2265449394


##########
src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java:
##########
@@ -241,102 +232,172 @@ private String getExtendedName(final int offset) throws 
IOException {
      */
     @Deprecated
     public ArArchiveEntry getNextArEntry() throws IOException {
-        if (currentEntry != null) {
-            final long entryEnd = entryOffset + currentEntry.getLength();
-            final long skipped = org.apache.commons.io.IOUtils.skip(in, 
entryEnd - offset);
-            trackReadBytes(skipped);
-            currentEntry = null;
-        }
-        if (offset == 0) {
-            final byte[] expected = 
ArchiveUtils.toAsciiBytes(ArArchiveEntry.HEADER);
-            final byte[] realized = IOUtils.readRange(in, expected.length);
-            final int read = realized.length;
-            trackReadBytes(read);
-            if (read != expected.length) {
-                throw new ArchiveException("Failed to read header. Occurred at 
byte: %,d", getBytesRead());
-            }
-            if (!Arrays.equals(expected, realized)) {
-                throw new ArchiveException("Invalid header " + 
ArchiveUtils.toAsciiString(realized));
+        return getNextEntry();
+    }
+
+    /*
+     * Returns the next AR file entry in this stream.
+     * <p>
+     *    The method skips special AR file entries, such as those used by GNU.
+     * </p>
+     * @return The next AR file entry.
+     * @throws IOException if the entry could not be read or is malformed.
+     */
+    @Override
+    public ArArchiveEntry getNextEntry() throws IOException {
+        skipGlobalSignature();
+
+        // Handle special GNU ar entries
+        boolean foundGNUStringTable = false;
+        do {
+            // If there is a current entry, skip any unread data and padding
+            if (currentEntry != null) {
+                IOUtils.skip(this, Long.MAX_VALUE); // Skip to end of current 
entry
+                skipRecordPadding(); // Skip padding to align to the next 
record
             }
-        }
-        if (offset % 2 != 0) {
-            if (in.read() < 0) {
-                // hit eof
-                return null;
+
+            // Read the next header record
+            final byte[] headerBuf = getRecord();
+            if (headerBuf == null) {
+                // If we encountered special records but no file entry, the 
archive is malformed
+                if (foundGNUStringTable) {
+                    throw new EOFException("Premature end of ar archive: no 
regular entry after GNU string table.");
+                }
+                currentEntry = null;
+                return null; // End of archive
             }
-            trackReadBytes(1);
-        }
-        {
-            final int read = IOUtils.readFully(in, metaData);
-            trackReadBytes(read);
-            if (read == 0) {
-                return null;
+            checkTrailer();
+
+            // Parse the header into a new entry
+            currentEntry = parseEntry(headerBuf);
+            entryOffset = getBytesRead(); // Store the offset of the entry
+
+            foundGNUStringTable = isGNUStringTable(currentEntry);
+            if (foundGNUStringTable) {
+                // If this is a GNU string table entry, read the extended 
names and continue
+                namebuffer = readGNUStringTable(currentEntry);
             }
-            if (read < metaData.length) {
-                throw new ArchiveException("Truncated ar archive");
+        } while (foundGNUStringTable);
+
+        // Handle long file names and other special cases
+        String name = currentEntry.getName();
+        long len = currentEntry.getLength();
+        // Handle GNU ar: names ending with '/' are terminated (allows spaces 
in names)
+        if (name.endsWith("/")) {
+            name = name.substring(0, name.length() - 1);
+        } else if (isGNULongName(name)) {
+            // GNU ar: name is a reference to the string table (e.g., "/42"), 
resolve the actual name
+            final int off = ParsingUtils.parseIntValue(name.substring(1));
+            name = getExtendedName(off);
+        } else if (isBSDLongName(name)) {
+            // BSD ar: name is stored after the header, retrieve it
+            name = getBSDLongName(name);
+            // The entry length includes the file name length; adjust to get 
the actual file data length
+            final int nameLen = name.length();
+            if (nameLen > len) {
+                throw new ArchiveException(
+                        "Invalid BSD long name: file name length (" + nameLen 
+ ") exceeds entry length (" + len + ")");
             }
+            len -= nameLen;
+            entryOffset += nameLen;
         }
-        {
-            final byte[] expected = 
ArchiveUtils.toAsciiBytes(ArArchiveEntry.TRAILER);
-            final byte[] realized = IOUtils.readRange(in, expected.length);
-            final int read = realized.length;
-            trackReadBytes(read);
-            if (read != expected.length) {
-                throw new ArchiveException("Failed to read entry trailer. 
Occurred at byte: " + getBytesRead());
+
+        currentEntry = new ArArchiveEntry(

Review Comment:
   Fixed in 
https://github.com/apache/commons-compress/pull/685/commits/da05e0ecd6860c51934b7751637961cc14011f41



-- 
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.

To unsubscribe, e-mail: issues-unsubscr...@commons.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to