garydgregory commented on code in PR #685:
URL: https://github.com/apache/commons-compress/pull/685#discussion_r2265428892
##########
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.");
+ }
Review Comment:
Then let's leave a link to the spec in the `//` comment.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]