001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.archivers.ar;
020
021import java.io.EOFException;
022import java.io.IOException;
023import java.io.InputStream;
024import java.util.Arrays;
025
026import org.apache.commons.compress.archivers.ArchiveEntry;
027import org.apache.commons.compress.archivers.ArchiveInputStream;
028import org.apache.commons.compress.utils.ArchiveUtils;
029import org.apache.commons.compress.utils.IOUtils;
030
031/**
032 * Implements the "ar" archive format as an input stream.
033 *
034 * @NotThreadSafe
035 *
036 */
037public class ArArchiveInputStream extends ArchiveInputStream {
038
039    private final InputStream input;
040    private long offset;
041    private boolean closed;
042
043    /*
044     * If getNextEntry has been called, the entry metadata is stored in
045     * currentEntry.
046     */
047    private ArArchiveEntry currentEntry;
048
049    // Storage area for extra long names (GNU ar)
050    private byte[] namebuffer;
051
052    /*
053     * The offset where the current entry started. -1 if no entry has been
054     * called
055     */
056    private long entryOffset = -1;
057
058    // offsets and length of meta data parts
059    private static final int NAME_OFFSET = 0;
060    private static final int NAME_LEN = 16;
061    private static final int LAST_MODIFIED_OFFSET = NAME_LEN;
062    private static final int LAST_MODIFIED_LEN = 12;
063    private static final int USER_ID_OFFSET = LAST_MODIFIED_OFFSET + LAST_MODIFIED_LEN;
064    private static final int USER_ID_LEN = 6;
065    private static final int GROUP_ID_OFFSET = USER_ID_OFFSET + USER_ID_LEN;
066    private static final int GROUP_ID_LEN = 6;
067    private static final int FILE_MODE_OFFSET = GROUP_ID_OFFSET + GROUP_ID_LEN;
068    private static final int FILE_MODE_LEN = 8;
069    private static final int LENGTH_OFFSET = FILE_MODE_OFFSET + FILE_MODE_LEN;
070    private static final int LENGTH_LEN = 10;
071
072    // cached buffer for meta data - must only be used locally in the class (COMPRESS-172 - reduce garbage collection)
073    private final byte[] metaData =
074        new byte[NAME_LEN + LAST_MODIFIED_LEN + USER_ID_LEN + GROUP_ID_LEN + FILE_MODE_LEN + LENGTH_LEN];
075
076    /**
077     * Constructs an Ar input stream with the referenced stream
078     *
079     * @param pInput
080     *            the ar input stream
081     */
082    public ArArchiveInputStream(final InputStream pInput) {
083        input = pInput;
084        closed = false;
085    }
086
087    /**
088     * Returns the next AR entry in this stream.
089     *
090     * @return the next AR entry.
091     * @throws IOException
092     *             if the entry could not be read
093     */
094    public ArArchiveEntry getNextArEntry() throws IOException {
095        if (currentEntry != null) {
096            final long entryEnd = entryOffset + currentEntry.getLength();
097            final long skipped = IOUtils.skip(input, entryEnd - offset);
098            trackReadBytes(skipped);
099            currentEntry = null;
100        }
101
102        if (offset == 0) {
103            final byte[] expected = ArchiveUtils.toAsciiBytes(ArArchiveEntry.HEADER);
104            final byte[] realized = IOUtils.readRange(input, expected.length);
105            final int read = realized.length;
106            trackReadBytes(read);
107            if (read != expected.length) {
108                throw new IOException("Failed to read header. Occurred at byte: " + getBytesRead());
109            }
110            if (!Arrays.equals(expected, realized)) {
111                throw new IOException("Invalid header " + ArchiveUtils.toAsciiString(realized));
112            }
113        }
114
115        if (offset % 2 != 0) {
116            if (input.read() < 0) {
117                // hit eof
118                return null;
119            }
120            trackReadBytes(1);
121        }
122
123        {
124            final int read = IOUtils.readFully(input, metaData);
125            trackReadBytes(read);
126            if (read == 0) {
127                return null;
128            }
129            if (read < metaData.length) {
130                throw new IOException("Truncated ar archive");
131            }
132        }
133
134        {
135            final byte[] expected = ArchiveUtils.toAsciiBytes(ArArchiveEntry.TRAILER);
136            final byte[] realized = IOUtils.readRange(input, expected.length);
137            final int read = realized.length;
138            trackReadBytes(read);
139            if (read != expected.length) {
140                throw new IOException("Failed to read entry trailer. Occurred at byte: " + getBytesRead());
141            }
142            if (!Arrays.equals(expected, realized)) {
143                throw new IOException("Invalid entry trailer. not read the content? Occurred at byte: " + getBytesRead());
144            }
145        }
146
147        entryOffset = offset;
148
149//        GNU ar uses a '/' to mark the end of the filename; this allows for the use of spaces without the use of an extended filename.
150
151        // entry name is stored as ASCII string
152        String temp = ArchiveUtils.toAsciiString(metaData, NAME_OFFSET, NAME_LEN).trim();
153        if (isGNUStringTable(temp)) { // GNU extended filenames entry
154            currentEntry = readGNUStringTable(metaData, LENGTH_OFFSET, LENGTH_LEN);
155            return getNextArEntry();
156        }
157
158        long len = asLong(metaData, LENGTH_OFFSET, LENGTH_LEN);
159        if (temp.endsWith("/")) { // GNU terminator
160            temp = temp.substring(0, temp.length() - 1);
161        } else if (isGNULongName(temp)) {
162            final int off = Integer.parseInt(temp.substring(1));// get the offset
163            temp = getExtendedName(off); // convert to the long name
164        } else if (isBSDLongName(temp)) {
165            temp = getBSDLongName(temp);
166            // entry length contained the length of the file name in
167            // addition to the real length of the entry.
168            // assume file name was ASCII, there is no "standard" otherwise
169            final int nameLen = temp.length();
170            len -= nameLen;
171            entryOffset += nameLen;
172        }
173
174        if (len < 0) {
175            throw new IOException("broken archive, entry with negative size");
176        }
177
178        currentEntry = new ArArchiveEntry(temp, len,
179                                          asInt(metaData, USER_ID_OFFSET, USER_ID_LEN, true),
180                                          asInt(metaData, GROUP_ID_OFFSET, GROUP_ID_LEN, true),
181                                          asInt(metaData, FILE_MODE_OFFSET, FILE_MODE_LEN, 8),
182                                          asLong(metaData, LAST_MODIFIED_OFFSET, LAST_MODIFIED_LEN));
183        return currentEntry;
184    }
185
186    /**
187     * Get an extended name from the GNU extended name buffer.
188     *
189     * @param offset pointer to entry within the buffer
190     * @return the extended file name; without trailing "/" if present.
191     * @throws IOException if name not found or buffer not set up
192     */
193    private String getExtendedName(final int offset) throws IOException {
194        if (namebuffer == null) {
195            throw new IOException("Cannot process GNU long filename as no // record was found");
196        }
197        for (int i = offset; i < namebuffer.length; i++) {
198            if (namebuffer[i] == '\012' || namebuffer[i] == 0) {
199                if (namebuffer[i - 1] == '/') {
200                    i--; // drop trailing /
201                }
202                return ArchiveUtils.toAsciiString(namebuffer, offset, i - offset);
203            }
204        }
205        throw new IOException("Failed to read entry: " + offset);
206    }
207
208    private long asLong(final byte[] byteArray, final int offset, final int len) {
209        return Long.parseLong(ArchiveUtils.toAsciiString(byteArray, offset, len).trim());
210    }
211
212    private int asInt(final byte[] byteArray, final int offset, final int len) {
213        return asInt(byteArray, offset, len, 10, false);
214    }
215
216    private int asInt(final byte[] byteArray, final int offset, final int len, final boolean treatBlankAsZero) {
217        return asInt(byteArray, offset, len, 10, treatBlankAsZero);
218    }
219
220    private int asInt(final byte[] byteArray, final int offset, final int len, final int base) {
221        return asInt(byteArray, offset, len, base, false);
222    }
223
224    private int asInt(final byte[] byteArray, final int offset, final int len, final int base, final boolean treatBlankAsZero) {
225        final String string = ArchiveUtils.toAsciiString(byteArray, offset, len).trim();
226        if (string.isEmpty() && treatBlankAsZero) {
227            return 0;
228        }
229        return Integer.parseInt(string, base);
230    }
231
232    /*
233     * (non-Javadoc)
234     *
235     * @see
236     * org.apache.commons.compress.archivers.ArchiveInputStream#getNextEntry()
237     */
238    @Override
239    public ArchiveEntry getNextEntry() throws IOException {
240        return getNextArEntry();
241    }
242
243    /*
244     * (non-Javadoc)
245     *
246     * @see java.io.InputStream#close()
247     */
248    @Override
249    public void close() throws IOException {
250        if (!closed) {
251            closed = true;
252            input.close();
253        }
254        currentEntry = null;
255    }
256
257    /*
258     * (non-Javadoc)
259     *
260     * @see java.io.InputStream#read(byte[], int, int)
261     */
262    @Override
263    public int read(final byte[] b, final int off, final int len) throws IOException {
264        if (len == 0) {
265            return 0;
266        }
267        if (currentEntry == null) {
268            throw new IllegalStateException("No current ar entry");
269        }
270        final long entryEnd = entryOffset + currentEntry.getLength();
271        if (len < 0 || offset >= entryEnd) {
272            return -1;
273        }
274        final int toRead = (int) Math.min(len, entryEnd - offset);
275        final int ret = this.input.read(b, off, toRead);
276        trackReadBytes(ret);
277        return ret;
278    }
279
280    /**
281     * Checks if the signature matches ASCII "!&lt;arch&gt;" followed by a single LF
282     * control character
283     *
284     * @param signature
285     *            the bytes to check
286     * @param length
287     *            the number of bytes to check
288     * @return true, if this stream is an Ar archive stream, false otherwise
289     */
290    public static boolean matches(final byte[] signature, final int length) {
291        // 3c21 7261 6863 0a3e
292
293        return length >= 8 && signature[0] == 0x21 &&
294                signature[1] == 0x3c && signature[2] == 0x61 &&
295                signature[3] == 0x72 && signature[4] == 0x63 &&
296                signature[5] == 0x68 && signature[6] == 0x3e &&
297                signature[7] == 0x0a;
298    }
299
300    static final String BSD_LONGNAME_PREFIX = "#1/";
301    private static final int BSD_LONGNAME_PREFIX_LEN =
302        BSD_LONGNAME_PREFIX.length();
303    private static final String BSD_LONGNAME_PATTERN =
304        "^" + BSD_LONGNAME_PREFIX + "\\d+";
305
306    /**
307     * Does the name look like it is a long name (or a name containing
308     * spaces) as encoded by BSD ar?
309     *
310     * <p>From the FreeBSD ar(5) man page:</p>
311     * <pre>
312     * BSD   In the BSD variant, names that are shorter than 16
313     *       characters and without embedded spaces are stored
314     *       directly in this field.  If a name has an embedded
315     *       space, or if it is longer than 16 characters, then
316     *       the string "#1/" followed by the decimal represen-
317     *       tation of the length of the file name is placed in
318     *       this field. The actual file name is stored immedi-
319     *       ately after the archive header.  The content of the
320     *       archive member follows the file name.  The ar_size
321     *       field of the header (see below) will then hold the
322     *       sum of the size of the file name and the size of
323     *       the member.
324     * </pre>
325     *
326     * @since 1.3
327     */
328    private static boolean isBSDLongName(final String name) {
329        return name != null && name.matches(BSD_LONGNAME_PATTERN);
330    }
331
332    /**
333     * Reads the real name from the current stream assuming the very
334     * first bytes to be read are the real file name.
335     *
336     * @see #isBSDLongName
337     *
338     * @since 1.3
339     */
340    private String getBSDLongName(final String bsdLongName) throws IOException {
341        final int nameLen =
342            Integer.parseInt(bsdLongName.substring(BSD_LONGNAME_PREFIX_LEN));
343        final byte[] name = IOUtils.readRange(input, nameLen);
344        final int read = name.length;
345        trackReadBytes(read);
346        if (read != nameLen) {
347            throw new EOFException();
348        }
349        return ArchiveUtils.toAsciiString(name);
350    }
351
352    private static final String GNU_STRING_TABLE_NAME = "//";
353
354    /**
355     * Is this the name of the "Archive String Table" as used by
356     * SVR4/GNU to store long file names?
357     *
358     * <p>GNU ar stores multiple extended file names in the data section
359     * of a file with the name "//", this record is referred to by
360     * future headers.</p>
361     *
362     * <p>A header references an extended file name by storing a "/"
363     * followed by a decimal offset to the start of the file name in
364     * the extended file name data section.</p>
365     *
366     * <p>The format of the "//" file itself is simply a list of the
367     * long file names, each separated by one or more LF
368     * characters. Note that the decimal offsets are number of
369     * characters, not line or string number within the "//" file.</p>
370     */
371    private static boolean isGNUStringTable(final String name) {
372        return GNU_STRING_TABLE_NAME.equals(name);
373    }
374
375    private void trackReadBytes(final long read) {
376        count(read);
377        if (read > 0) {
378            offset += read;
379        }
380    }
381
382    /**
383     * Reads the GNU archive String Table.
384     *
385     * @see #isGNUStringTable
386     */
387    private ArArchiveEntry readGNUStringTable(final byte[] length, final int offset, final int len) throws IOException {
388        final int bufflen = asInt(length, offset, len); // Assume length will fit in an int
389        namebuffer = IOUtils.readRange(input, bufflen);
390        final int read = namebuffer.length;
391        trackReadBytes(read);
392        if (read != bufflen){
393            throw new IOException("Failed to read complete // record: expected="
394                                  + bufflen + " read=" + read);
395        }
396        return new ArArchiveEntry(GNU_STRING_TABLE_NAME, bufflen);
397    }
398
399    private static final String GNU_LONGNAME_PATTERN = "^/\\d+";
400
401    /**
402     * Does the name look like it is a long name (or a name containing
403     * spaces) as encoded by SVR4/GNU ar?
404     *
405     * @see #isGNUStringTable
406     */
407    private boolean isGNULongName(final String name) {
408        return name != null && name.matches(GNU_LONGNAME_PATTERN);
409    }
410}