1 /*-
2  * Copyright (c) 2012 Michihiro NAKAJIMA
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 #ifdef HAVE_ERRNO_H
29 #include <errno.h>
30 #endif
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 
41 #include "archive.h"
42 #include "archive_private.h"
43 #include "archive_read_private.h"
44 
45 static const unsigned char grzip_magic[] = {
46 	0x47, 0x52, 0x5a, 0x69, 0x70, 0x49, 0x49, 0x00,
47 	0x02, 0x04, 0x3a, 0x29 };
48 
49 static int	grzip_bidder_bid(struct archive_read_filter_bidder *,
50 		    struct archive_read_filter *);
51 static int	grzip_bidder_init(struct archive_read_filter *);
52 
53 
54 static const struct archive_read_filter_bidder_vtable
55 grzip_bidder_vtable = {
56 	.bid = grzip_bidder_bid,
57 	.init = grzip_bidder_init,
58 };
59 
60 int
61 archive_read_support_filter_grzip(struct archive *_a)
62 {
63 	struct archive_read *a = (struct archive_read *)_a;
64 
65 	if (__archive_read_register_bidder(a, NULL, NULL,
66 				&grzip_bidder_vtable) != ARCHIVE_OK)
67 		return (ARCHIVE_FATAL);
68 
69 	/* This filter always uses an external program. */
70 	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
71 	    "Using external grzip program for grzip decompression");
72 	return (ARCHIVE_WARN);
73 }
74 
75 /*
76  * Bidder just verifies the header and returns the number of verified bits.
77  */
78 static int
79 grzip_bidder_bid(struct archive_read_filter_bidder *self,
80     struct archive_read_filter *filter)
81 {
82 	const unsigned char *p;
83 	ssize_t avail;
84 
85 	(void)self; /* UNUSED */
86 
87 	p = __archive_read_filter_ahead(filter, sizeof(grzip_magic), &avail);
88 	if (p == NULL || avail == 0)
89 		return (0);
90 
91 	if (memcmp(p, grzip_magic, sizeof(grzip_magic)))
92 		return (0);
93 
94 	return (sizeof(grzip_magic) * 8);
95 }
96 
97 static int
98 grzip_bidder_init(struct archive_read_filter *self)
99 {
100 	int r;
101 
102 	r = __archive_read_program(self, "grzip -d");
103 	/* Note: We set the format here even if __archive_read_program()
104 	 * above fails.  We do, after all, know what the format is
105 	 * even if we weren't able to read it. */
106 	self->code = ARCHIVE_FILTER_GRZIP;
107 	self->name = "grzip";
108 	return (r);
109 }
110