From: Tim Kientzle Date: Tue, 14 Apr 2009 02:03:45 +0000 (-0400) Subject: Add a new "raw" format reader that just provides raw binary data from X-Git-Tag: v2.8.0~707 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ccdb0318e109084b3dec3b9963e0242cc4600e4f;p=thirdparty%2Flibarchive.git Add a new "raw" format reader that just provides raw binary data from the input. It matches (with a very low bid) any non-empty input. This basically stubs out the archive format handling so that the automatic decompression engine can be used for other purposes. SVN-Revision: 990 --- diff --git a/Makefile.am b/Makefile.am index 612867f49..c8d553872 100644 --- a/Makefile.am +++ b/Makefile.am @@ -120,6 +120,7 @@ libarchive_la_SOURCES= \ libarchive/archive_read_support_format_empty.c \ libarchive/archive_read_support_format_iso9660.c \ libarchive/archive_read_support_format_mtree.c \ + libarchive/archive_read_support_format_raw.c \ libarchive/archive_read_support_format_tar.c \ libarchive/archive_read_support_format_zip.c \ libarchive/archive_string.c \ diff --git a/libarchive/CMakeLists.txt b/libarchive/CMakeLists.txt index 135a98a90..e100bcd78 100644 --- a/libarchive/CMakeLists.txt +++ b/libarchive/CMakeLists.txt @@ -46,6 +46,7 @@ SET(libarchive_SOURCES archive_read_support_format_empty.c archive_read_support_format_iso9660.c archive_read_support_format_mtree.c + archive_read_support_format_raw.c archive_read_support_format_tar.c archive_read_support_format_zip.c archive_string.c diff --git a/libarchive/Makefile b/libarchive/Makefile index c86d88191..cf89a86db 100644 --- a/libarchive/Makefile +++ b/libarchive/Makefile @@ -46,6 +46,7 @@ SRCS= archive_check_magic.c \ archive_read_support_format_empty.c \ archive_read_support_format_iso9660.c \ archive_read_support_format_mtree.c \ + archive_read_support_format_raw.c \ archive_read_support_format_tar.c \ archive_read_support_format_zip.c \ archive_string.c \ diff --git a/libarchive/archive.h b/libarchive/archive.h index 572e19dbf..22f17b8ec 100644 --- a/libarchive/archive.h +++ b/libarchive/archive.h @@ -272,6 +272,7 @@ typedef int archive_close_callback(struct archive *, void *_client_data); #define ARCHIVE_FORMAT_AR_GNU (ARCHIVE_FORMAT_AR | 1) #define ARCHIVE_FORMAT_AR_BSD (ARCHIVE_FORMAT_AR | 2) #define ARCHIVE_FORMAT_MTREE 0x80000 +#define ARCHIVE_FORMAT_RAW 0x90000 /*- * Basic outline for reading an archive: @@ -315,6 +316,7 @@ __LA_DECL int archive_read_support_format_empty(struct archive *); __LA_DECL int archive_read_support_format_gnutar(struct archive *); __LA_DECL int archive_read_support_format_iso9660(struct archive *); __LA_DECL int archive_read_support_format_mtree(struct archive *); +__LA_DECL int archive_read_support_format_raw(struct archive *); __LA_DECL int archive_read_support_format_tar(struct archive *); __LA_DECL int archive_read_support_format_zip(struct archive *); diff --git a/libarchive/archive_read_support_format_raw.c b/libarchive/archive_read_support_format_raw.c new file mode 100644 index 000000000..3f922b2c4 --- /dev/null +++ b/libarchive/archive_read_support_format_raw.c @@ -0,0 +1,187 @@ +/*- + * Copyright (c) 2003-2009 Tim Kientzle + * 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 "archive_platform.h" +__FBSDID("$FreeBSD$"); + +#ifdef HAVE_ERRNO_H +#include +#endif +#include +#ifdef HAVE_STDLIB_H +#include +#endif + +#include "archive.h" +#include "archive_entry.h" +#include "archive_private.h" +#include "archive_read_private.h" + +struct raw_info { + int64_t offset; /* Current position in the file. */ + int end_of_file; +}; + +static int archive_read_format_raw_bid(struct archive_read *); +static int archive_read_format_raw_cleanup(struct archive_read *); +static int archive_read_format_raw_read_data(struct archive_read *, + const void **, size_t *, off_t *); +static int archive_read_format_raw_read_data_skip(struct archive_read *); +static int archive_read_format_raw_read_header(struct archive_read *, + struct archive_entry *); + +int +archive_read_support_format_raw(struct archive *_a) +{ + struct raw_info *info; + struct archive_read *a = (struct archive_read *)_a; + int r; + + info = (struct raw_info *)calloc(1, sizeof(*info)); + if (info == NULL) { + archive_set_error(&a->archive, ENOMEM, + "Can't allocate raw_info data"); + return (ARCHIVE_FATAL); + } + + r = __archive_read_register_format(a, + info, + "raw", + archive_read_format_raw_bid, + NULL, + archive_read_format_raw_read_header, + archive_read_format_raw_read_data, + archive_read_format_raw_read_data_skip, + archive_read_format_raw_cleanup); + if (r != ARCHIVE_OK) + free(info); + return (r); +} + +/* + * Bid 1 if this is a non-empty file. Anyone who can really support + * this should outbid us, so it should generally be safe to use "raw" + * in conjunction with other formats. But, this could really confuse + * folks if there are bid errors or minor file damage, so we don't + * include "raw" as part of support_format_all(). + */ +static int +archive_read_format_raw_bid(struct archive_read *a) +{ + const char *p; + + if ((p = __archive_read_ahead(a, 1, NULL)) == NULL) + return (-1); + return (1); +} + +/* + * Mock up a fake header. + */ +static int +archive_read_format_raw_read_header(struct archive_read *a, + struct archive_entry *entry) +{ + struct raw_info *info; + + info = (struct raw_info *)(a->format->data); + if (info->end_of_file) + return (ARCHIVE_EOF); + + a->archive.archive_format = ARCHIVE_FORMAT_RAW; + a->archive.archive_format_name = "Raw data"; + archive_entry_set_pathname(entry, "data"); + /* XXX should we set mode to mimic a regular file? XXX */ + /* I'm deliberately leaving most fields unset here. */ + return (ARCHIVE_OK); +} + +static int +archive_read_format_raw_read_data(struct archive_read *a, + const void **buff, size_t *size, off_t *offset) +{ + struct raw_info *info; + size_t avail; + + info = (struct raw_info *)(a->format->data); + if (info->end_of_file) + return (ARCHIVE_EOF); + + /* Get whatever bytes are immediately available. */ + *buff = __archive_read_ahead(a, 1, &avail); + if (avail > 0) { + /* Consume and return the bytes we just read */ + __archive_read_consume(a, avail); + *size = avail; + *offset = info->offset; + info->offset += *size; + return (ARCHIVE_OK); + } else if (0 == avail) { + /* Record and return end-of-file. */ + info->end_of_file = 1; + *size = 0; + *offset = info->offset; + return (ARCHIVE_EOF); + } else { + /* Record and return an error. */ + *size = 0; + *offset = info->offset; + return (avail); + } + return (ARCHIVE_OK); +} + +static int +archive_read_format_raw_read_data_skip(struct archive_read *a) +{ + struct raw_info *info; + off_t bytes_skipped; + int64_t request = 1024 * 1024 * 1024UL; /* Skip 1 GB at a time. */ + + info = (struct raw_info *)(a->format->data); + if (info->end_of_file) + return (ARCHIVE_EOF); + info->end_of_file = 1; + + for (;;) { + bytes_skipped = __archive_read_skip_lenient(a, request); + if (bytes_skipped < 0) + return (ARCHIVE_FATAL); + if (bytes_skipped < request) + return (ARCHIVE_OK); + /* We skipped all the bytes we asked for. There might + * be more, so try again. */ + } +} + +static int +archive_read_format_raw_cleanup(struct archive_read *a) +{ + struct raw_info *info; + + info = (struct raw_info *)(a->format->data); + free(info); + a->format->data = NULL; + return (ARCHIVE_OK); +} diff --git a/libarchive/test/Makefile b/libarchive/test/Makefile index a04e1b47f..2c997b034 100644 --- a/libarchive/test/Makefile +++ b/libarchive/test/Makefile @@ -53,6 +53,7 @@ TESTS= \ test_read_format_isorr_bz2.c \ test_read_format_mtree.c \ test_read_format_pax_bz2.c \ + test_read_format_raw.c \ test_read_format_tar.c \ test_read_format_tar_empty_filename.c \ test_read_format_tbz.c \ diff --git a/libarchive/test/test_read_format_raw.c b/libarchive/test/test_read_format_raw.c new file mode 100644 index 000000000..f036d0c19 --- /dev/null +++ b/libarchive/test/test_read_format_raw.c @@ -0,0 +1,89 @@ +/*- + * Copyright (c) 2007 Kai Wang + * Copyright (c) 2007 Tim Kientzle + * 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 + * in this position and unchanged. + * 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$"); + +DEFINE_TEST(test_read_format_raw) +{ + char buff[512]; + struct archive_entry *ae; + struct archive *a; + const char *reffile1 = "test_read_format_raw.data"; + const char *reffile2 = "test_read_format_raw.data.Z"; + + /* First, try pulling data out of an uninterpretable file. */ + extract_reference_file(reffile1); + assert((a = archive_read_new()) != NULL); + assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_all(a)); + assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); + assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_raw(a)); + assertEqualIntA(a, ARCHIVE_OK, + archive_read_open_filename(a, reffile1, 512)); + + /* First (and only!) Entry */ + assertA(0 == archive_read_next_header(a, &ae)); + assertEqualString("data", archive_entry_pathname(ae)); + /* Most fields should be unset (unknown) */ + assert(!archive_entry_size_is_set(ae)); + assert(!archive_entry_atime_is_set(ae)); + assert(!archive_entry_ctime_is_set(ae)); + assert(!archive_entry_mtime_is_set(ae)); + assertEqualInt(4, archive_read_data(a, buff, 32)); + assertEqualMem(buff, "foo\n", 4); + + /* Test EOF */ + assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); + assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); + assertEqualInt(ARCHIVE_OK, archive_read_finish(a)); + + + /* Second, try the same with a compressed file. */ + extract_reference_file(reffile2); + assert((a = archive_read_new()) != NULL); + assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_all(a)); + assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_raw(a)); + assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); + assertEqualIntA(a, ARCHIVE_OK, + archive_read_open_filename(a, reffile2, 1)); + + /* First (and only!) Entry */ + assertA(0 == archive_read_next_header(a, &ae)); + assertEqualString("data", archive_entry_pathname(ae)); + /* Most fields should be unset (unknown) */ + assert(!archive_entry_size_is_set(ae)); + assert(!archive_entry_atime_is_set(ae)); + assert(!archive_entry_ctime_is_set(ae)); + assert(!archive_entry_mtime_is_set(ae)); + assertEqualInt(4, archive_read_data(a, buff, 32)); + assertEqualMem(buff, "foo\n", 4); + + /* Test EOF */ + assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); + assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); + assertEqualInt(ARCHIVE_OK, archive_read_finish(a)); +} diff --git a/libarchive/test/test_read_format_raw.data.Z.uu b/libarchive/test/test_read_format_raw.data.Z.uu new file mode 100644 index 000000000..3fe4deaba --- /dev/null +++ b/libarchive/test/test_read_format_raw.data.Z.uu @@ -0,0 +1,4 @@ +begin 644 test_read_format_raw.data.Z +('YV09MZ\40`` +` +end diff --git a/libarchive/test/test_read_format_raw.data.uu b/libarchive/test/test_read_format_raw.data.uu new file mode 100644 index 000000000..7c68a2caa --- /dev/null +++ b/libarchive/test/test_read_format_raw.data.uu @@ -0,0 +1,4 @@ +begin 644 test_read_format_raw.data +$9F]O"@`` +` +end