]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Add LZMA test, more detailed comments about the sorry state
authorTim Kientzle <kientzle@gmail.com>
Sun, 9 Nov 2008 17:45:24 +0000 (12:45 -0500)
committerTim Kientzle <kientzle@gmail.com>
Sun, 9 Nov 2008 17:45:24 +0000 (12:45 -0500)
of LZMA magic number checks, update Makefiles to include
lzma reader and lzma test.

Because LZMA support is optional, the lzma test is pretty forgiving
about failures at open time.  It does report the skip (including
the underlying libarchive failure) which should reduce the risk of
false passes somewhat.

SVN-Revision: 242

Makefile.am
libarchive/Makefile
libarchive/archive_read_support_compression_all.c
libarchive/archive_read_support_compression_lzma.c
libarchive/test/Makefile
libarchive/test/test_read_format_gtar_lzma.c [new file with mode: 0644]

index e749906b7b49cd6c8965bb19b5f312cffa91ed1c..8f8dcc5ac249270429c32bc82a02b57d5272fbbc 100644 (file)
@@ -202,6 +202,7 @@ libarchive_test_SOURCES=                                    \
        libarchive/test/test_read_format_cpio_svr4c_Z.c         \
        libarchive/test/test_read_format_empty.c                \
        libarchive/test/test_read_format_gtar_gz.c              \
+       libarchive/test/test_read_format_gtar_lzma.c            \
        libarchive/test/test_read_format_gtar_sparse.c          \
        libarchive/test/test_read_format_iso_gz.c               \
        libarchive/test/test_read_format_isorr_bz2.c            \
index c415ae93b87ec425c713c9be3266acd75c83ce80..d2517a1447506e16beee962252d0473c7dde3a7e 100644 (file)
@@ -37,6 +37,7 @@ SRCS= archive_check_magic.c                           \
        archive_read_support_compression_bzip2.c        \
        archive_read_support_compression_compress.c     \
        archive_read_support_compression_gzip.c         \
+       archive_read_support_compression_lzma.c         \
        archive_read_support_compression_none.c         \
        archive_read_support_compression_program.c      \
        archive_read_support_format_all.c               \
index 54226cc7a43231c4f83bb7bb7cfd47f3a8bce735..f6e785062c9faae9cf617c93f33bdbea227042b1 100644 (file)
@@ -40,7 +40,10 @@ archive_read_support_compression_all(struct archive *a)
        archive_read_support_compression_gzip(a);
 #endif
 #if HAVE_LZMADEC_H
-//     archive_read_support_compression_lzma(a);
+       /* LZMA bidding is subject to false positives because
+        * the LZMA file format has a very weak signature.  It
+        * may not be feasible to include LZMA detection here. */
+       /* archive_read_support_compression_lzma(a); */
 #endif
        return (ARCHIVE_OK);
 }
index 49749ed856df2f17b35f3c2c29f7259d8e03f3b9..e90c0d64a772bd53245460a7407c738b037f77a4 100644 (file)
@@ -102,6 +102,15 @@ lzma_reader_free(struct archive_reader *self){
  * This logic returns zero if any part of the signature fails.  It
  * also tries to Do The Right Thing if a very short buffer prevents us
  * from verifying as much as we would like.
+ *
+ * <sigh> LZMA has a rather poor file signature.  Zeros do not
+ * make good signature bytes as a rule, and the only non-zero byte
+ * here is an ASCII character.  For example, an uncompressed tar
+ * archive whose first file is ']' would satisfy this check.  It may
+ * be necessary to exclude LZMA from compression_all() because of
+ * this.  Clients of libarchive would then have to explicitly enable
+ * LZMA checking instead of (or in addition to) compression_all() when
+ * they have other evidence (file name, command-line option) to go on.
  */
 static int
 lzma_reader_bid(struct archive_reader *self, const void *buff, size_t len)
@@ -111,15 +120,42 @@ lzma_reader_bid(struct archive_reader *self, const void *buff, size_t len)
 
        (void)self; /* UNUSED */
 
+       buffer = (const unsigned char *)buff;
+
+
+       /* First byte of raw LZMA stream is always 0x5d. */
        if (len < 1)
                return (0);
-
-       buffer = (const unsigned char *)buff;
        bits_checked = 0;
-       if (buffer[0] != 0x5d)  /* Verify first ID byte. */
+       if (buffer[0] != 0x5d)
+               return (0);
+       bits_checked += 8;
+
+       /* Second through fifth bytes are dictionary code, stored in
+        * little-endian order.  The two least-significant bytes are
+        * always zero. */
+       if (len < 2)
+               return (bits_checked);
+       if (buffer[1] != 0)
+               return (0);
+       bits_checked += 8;
+
+       if (len < 3)
+               return (bits_checked);
+       if (buffer[2] != 0)
                return (0);
        bits_checked += 8;
 
+       /* ??? TODO:  Explain this.  ??? */
+       if (len < 6)
+               return (bits_checked);
+       if (buffer[5] != 0)
+               return (0);
+       bits_checked += 8;
+
+       /* TODO: The above test is still very weak.  It would be
+        * good to do better. */
+
        return (bits_checked);
 }
 
index c7ae043226d0fd434b854bd849400f06a9da9e9f..4c3965f01efbe2a7f74656aab9e17111286647b6 100644 (file)
@@ -34,6 +34,7 @@ TESTS= \
        test_read_format_cpio_svr4c_Z.c         \
        test_read_format_empty.c                \
        test_read_format_gtar_gz.c              \
+       test_read_format_gtar_lzma.c            \
        test_read_format_gtar_sparse.c          \
        test_read_format_iso_gz.c               \
        test_read_format_isorr_bz2.c            \
diff --git a/libarchive/test/test_read_format_gtar_lzma.c b/libarchive/test/test_read_format_gtar_lzma.c
new file mode 100644 (file)
index 0000000..4b0e29d
--- /dev/null
@@ -0,0 +1,72 @@
+/*-
+ * Copyright (c) 2008 Miklos Vajna
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "test.h"
+__FBSDID("$FreeBSD$");
+
+static unsigned char archive[] = {
+0x5d, 0x0, 0x0, 0x80, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+0x17, 0xb, 0xbc, 0x1c, 0x7d, 0x1, 0x95, 0xc0, 0x1d, 0x4a, 0x46, 0x9c,
+0x1c, 0xc5, 0x8, 0x82, 0x10, 0xed, 0x84, 0xf6, 0xea, 0x7a, 0xfe, 0x63,
+0x5a, 0x34, 0x5e, 0xf7, 0xc, 0x60, 0xd6, 0x8b, 0xc1, 0x47, 0xaf, 0x11,
+0x6f, 0x18, 0x94, 0x81, 0x74, 0x8a, 0xf8, 0x47, 0xcc, 0xdd, 0xc0, 0xd9,
+0x40, 0xa, 0xc3, 0xac, 0x43, 0x47, 0xb5, 0xac, 0x2b, 0x31, 0xd3, 0x6,
+0xa4, 0x2c, 0x44, 0x80, 0x24, 0x4b, 0xfe, 0x43, 0x22, 0x4e, 0x14, 0x30,
+0x7a, 0xef, 0x99, 0x6e, 0xf, 0x8b, 0xc1, 0x79, 0x93, 0x88, 0x54, 0x73,
+0x59, 0x3f, 0xc, 0xfb, 0xee, 0x9c, 0x83, 0x49, 0x93, 0x33, 0xad, 0x44,
+0xbe, 0x0};
+
+DEFINE_TEST(test_read_format_gtar_lzma)
+{
+       int r;
+
+       struct archive_entry *ae;
+       struct archive *a;
+       assert((a = archive_read_new()) != NULL);
+       assertEqualIntA(a, ARCHIVE_OK,
+           archive_read_support_compression_all(a));
+       assertEqualIntA(a, ARCHIVE_OK,
+           archive_read_support_compression_lzma(a));
+       assertEqualIntA(a, ARCHIVE_OK,
+           archive_read_support_format_all(a));
+       r = archive_read_open_memory(a, archive, sizeof(archive));
+       if (r != ARCHIVE_OK) {
+               skipping("Skipping LZMA compression check: %s",
+                   archive_error_string(a));
+               goto finish;
+       }
+       assertEqualIntA(a, ARCHIVE_OK,
+           archive_read_next_header(a, &ae));
+       assertEqualInt(archive_compression(a), ARCHIVE_COMPRESSION_LZMA);
+       assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_GNUTAR);
+       assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
+finish:
+#if ARCHIVE_VERSION_NUMBER < 2000000
+       archive_read_finish(a);
+#else
+       assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
+#endif
+}
+
+