2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
45 #include "archive_private.h"
46 #include "archive_read_private.h"
48 #define LRZIP_HEADER_MAGIC "LRZI"
49 #define LRZIP_HEADER_MAGIC_LEN 4
51 static int lrzip_bidder_bid(struct archive_read_filter_bidder *,
52 struct archive_read_filter *);
53 static int lrzip_bidder_init(struct archive_read_filter *);
56 static const struct archive_read_filter_bidder_vtable
57 lrzip_bidder_vtable = {
58 .bid = lrzip_bidder_bid,
59 .init = lrzip_bidder_init,
63 archive_read_support_filter_lrzip(struct archive *_a)
65 struct archive_read *a = (struct archive_read *)_a;
67 if (__archive_read_register_bidder(a, NULL, "lrzip",
68 &lrzip_bidder_vtable) != ARCHIVE_OK)
69 return (ARCHIVE_FATAL);
71 /* This filter always uses an external program. */
72 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
73 "Using external lrzip program for lrzip decompression");
74 return (ARCHIVE_WARN);
78 * Bidder just verifies the header and returns the number of verified bits.
81 lrzip_bidder_bid(struct archive_read_filter_bidder *self,
82 struct archive_read_filter *filter)
84 const unsigned char *p;
88 (void)self; /* UNUSED */
89 /* Start by looking at the first six bytes of the header, which
90 * is all fixed layout. */
92 p = __archive_read_filter_ahead(filter, len, &avail);
93 if (p == NULL || avail == 0)
96 if (memcmp(p, LRZIP_HEADER_MAGIC, LRZIP_HEADER_MAGIC_LEN))
99 /* current major version is always 0, verify this */
100 if (p[LRZIP_HEADER_MAGIC_LEN])
102 /* support only v0.6+ lrzip for sanity */
103 i = p[LRZIP_HEADER_MAGIC_LEN + 1];
104 if ((i < 6) || (i > 10))
111 lrzip_bidder_init(struct archive_read_filter *self)
115 r = __archive_read_program(self, "lrzip -d -q");
116 /* Note: We set the format here even if __archive_read_program()
117 * above fails. We do, after all, know what the format is
118 * even if we weren't able to read it. */
119 self->code = ARCHIVE_FILTER_LRZIP;
120 self->name = "lrzip";