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 #ifdef HAVE_ERRNO_H
29 #include <errno.h>
30 #endif
31 #ifdef HAVE_STDLIB_H
32 #include <stdlib.h>
33 #endif
34 
35 #include "archive.h"
36 #include "archive_write_private.h"
37 
38 struct write_grzip {
39 	struct archive_write_program_data *pdata;
40 };
41 
42 static int archive_write_grzip_open(struct archive_write_filter *);
43 static int archive_write_grzip_options(struct archive_write_filter *,
44 		    const char *, const char *);
45 static int archive_write_grzip_write(struct archive_write_filter *,
46 		    const void *, size_t);
47 static int archive_write_grzip_close(struct archive_write_filter *);
48 static int archive_write_grzip_free(struct archive_write_filter *);
49 
50 int
51 archive_write_add_filter_grzip(struct archive *_a)
52 {
53 	struct archive_write_filter *f = __archive_write_allocate_filter(_a);
54 	struct write_grzip *data;
55 
56 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
57 	    ARCHIVE_STATE_NEW, "archive_write_add_filter_grzip");
58 
59 	data = calloc(1, sizeof(*data));
60 	if (data == NULL) {
61 		archive_set_error(_a, ENOMEM, "Can't allocate memory");
62 		return (ARCHIVE_FATAL);
63 	}
64 	data->pdata = __archive_write_program_allocate("grzip");
65 	if (data->pdata == NULL) {
66 		free(data);
67 		archive_set_error(_a, ENOMEM, "Can't allocate memory");
68 		return (ARCHIVE_FATAL);
69 	}
70 
71 	f->name = "grzip";
72 	f->code = ARCHIVE_FILTER_GRZIP;
73 	f->data = data;
74 	f->open = archive_write_grzip_open;
75 	f->options = archive_write_grzip_options;
76 	f->write = archive_write_grzip_write;
77 	f->close = archive_write_grzip_close;
78 	f->free = archive_write_grzip_free;
79 
80 	/* Note: This filter always uses an external program, so we
81 	 * return "warn" to inform of the fact. */
82 	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
83 	    "Using external grzip program for grzip compression");
84 	return (ARCHIVE_WARN);
85 }
86 
87 static int
88 archive_write_grzip_options(struct archive_write_filter *f, const char *key,
89     const char *value)
90 {
91 	(void)f; /* UNUSED */
92 	(void)key; /* UNUSED */
93 	(void)value; /* UNUSED */
94 	/* Note: The "warn" return is just to inform the options
95 	 * supervisor that we didn't handle it.  It will generate
96 	 * a suitable error if no one used this option. */
97 	return (ARCHIVE_WARN);
98 }
99 
100 static int
101 archive_write_grzip_open(struct archive_write_filter *f)
102 {
103 	struct write_grzip *data = (struct write_grzip *)f->data;
104 
105 	return __archive_write_program_open(f, data->pdata, "grzip");
106 }
107 
108 static int
109 archive_write_grzip_write(struct archive_write_filter *f,
110     const void *buff, size_t length)
111 {
112 	struct write_grzip *data = (struct write_grzip *)f->data;
113 
114 	return __archive_write_program_write(f, data->pdata, buff, length);
115 }
116 
117 static int
118 archive_write_grzip_close(struct archive_write_filter *f)
119 {
120 	struct write_grzip *data = (struct write_grzip *)f->data;
121 
122 	return __archive_write_program_close(f, data->pdata);
123 }
124 
125 static int
126 archive_write_grzip_free(struct archive_write_filter *f)
127 {
128 	struct write_grzip *data = (struct write_grzip *)f->data;
129 
130 	__archive_write_program_free(data->pdata);
131 	free(data);
132 	return (ARCHIVE_OK);
133 }
134