1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  */
25 
26 #include "archive_platform.h"
27 
28 __FBSDID("$FreeBSD$");
29 
30 
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 
44 #include "archive.h"
45 #include "archive_private.h"
46 #include "archive_read_private.h"
47 
48 #define LRZIP_HEADER_MAGIC "LRZI"
49 #define LRZIP_HEADER_MAGIC_LEN 4
50 
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 *);
54 
55 
56 static int
57 lrzip_reader_free(struct archive_read_filter_bidder *self)
58 {
59 	(void)self; /* UNUSED */
60 	return (ARCHIVE_OK);
61 }
62 
63 int
64 archive_read_support_filter_lrzip(struct archive *_a)
65 {
66 	struct archive_read *a = (struct archive_read *)_a;
67 	struct archive_read_filter_bidder *reader;
68 
69 	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
70 	    ARCHIVE_STATE_NEW, "archive_read_support_filter_lrzip");
71 
72 	if (__archive_read_get_bidder(a, &reader) != ARCHIVE_OK)
73 		return (ARCHIVE_FATAL);
74 
75 	reader->data = NULL;
76 	reader->name = "lrzip";
77 	reader->bid = lrzip_bidder_bid;
78 	reader->init = lrzip_bidder_init;
79 	reader->options = NULL;
80 	reader->free = lrzip_reader_free;
81 	/* This filter always uses an external program. */
82 	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
83 	    "Using external lrzip program for lrzip decompression");
84 	return (ARCHIVE_WARN);
85 }
86 
87 /*
88  * Bidder just verifies the header and returns the number of verified bits.
89  */
90 static int
91 lrzip_bidder_bid(struct archive_read_filter_bidder *self,
92     struct archive_read_filter *filter)
93 {
94 	const unsigned char *p;
95 	ssize_t avail, len;
96 	int i;
97 
98 	(void)self; /* UNUSED */
99 	/* Start by looking at the first six bytes of the header, which
100 	 * is all fixed layout. */
101 	len = 6;
102 	p = __archive_read_filter_ahead(filter, len, &avail);
103 	if (p == NULL || avail == 0)
104 		return (0);
105 
106 	if (memcmp(p, LRZIP_HEADER_MAGIC, LRZIP_HEADER_MAGIC_LEN))
107 		return (0);
108 
109 	/* current major version is always 0, verify this */
110 	if (p[LRZIP_HEADER_MAGIC_LEN])
111 		return 0;
112 	/* support only v0.6+ lrzip for sanity */
113 	i = p[LRZIP_HEADER_MAGIC_LEN + 1];
114 	if ((i < 6) || (i > 10))
115 		return 0;
116 
117 	return (int)len;
118 }
119 
120 static int
121 lrzip_bidder_init(struct archive_read_filter *self)
122 {
123 	int r;
124 
125 	r = __archive_read_program(self, "lrzip -d -q");
126 	/* Note: We set the format here even if __archive_read_program()
127 	 * above fails.  We do, after all, know what the format is
128 	 * even if we weren't able to read it. */
129 	self->code = ARCHIVE_FILTER_LRZIP;
130 	self->name = "lrzip";
131 	return (r);
132 }
133