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$");
38 #include "archive_write_private.h"
41 struct archive_write_program_data *pdata;
44 static int archive_write_grzip_open(struct archive_write_filter *);
45 static int archive_write_grzip_options(struct archive_write_filter *,
46 const char *, const char *);
47 static int archive_write_grzip_write(struct archive_write_filter *,
48 const void *, size_t);
49 static int archive_write_grzip_close(struct archive_write_filter *);
50 static int archive_write_grzip_free(struct archive_write_filter *);
53 archive_write_add_filter_grzip(struct archive *_a)
55 struct archive_write_filter *f = __archive_write_allocate_filter(_a);
56 struct write_grzip *data;
58 archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
59 ARCHIVE_STATE_NEW, "archive_write_add_filter_grzip");
61 data = calloc(1, sizeof(*data));
63 archive_set_error(_a, ENOMEM, "Can't allocate memory");
64 return (ARCHIVE_FATAL);
66 data->pdata = __archive_write_program_allocate();
67 if (data->pdata == NULL) {
69 archive_set_error(_a, ENOMEM, "Can't allocate memory");
70 return (ARCHIVE_FATAL);
74 f->code = ARCHIVE_FILTER_GRZIP;
76 f->open = archive_write_grzip_open;
77 f->options = archive_write_grzip_options;
78 f->write = archive_write_grzip_write;
79 f->close = archive_write_grzip_close;
80 f->free = archive_write_grzip_free;
82 /* Note: This filter always uses an external program, so we
83 * return "warn" to inform of the fact. */
84 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
85 "Using external grzip program for grzip compression");
86 return (ARCHIVE_WARN);
90 archive_write_grzip_options(struct archive_write_filter *f, const char *key,
94 (void)key; /* UNUSED */
95 (void)value; /* UNUSED */
96 /* Note: The "warn" return is just to inform the options
97 * supervisor that we didn't handle it. It will generate
98 * a suitable error if no one used this option. */
99 return (ARCHIVE_WARN);
103 archive_write_grzip_open(struct archive_write_filter *f)
105 struct write_grzip *data = (struct write_grzip *)f->data;
107 return __archive_write_program_open(f, data->pdata, "grzip");
111 archive_write_grzip_write(struct archive_write_filter *f,
112 const void *buff, size_t length)
114 struct write_grzip *data = (struct write_grzip *)f->data;
116 return __archive_write_program_write(f, data->pdata, buff, length);
120 archive_write_grzip_close(struct archive_write_filter *f)
122 struct write_grzip *data = (struct write_grzip *)f->data;
124 return __archive_write_program_close(f, data->pdata);
128 archive_write_grzip_free(struct archive_write_filter *f)
130 struct write_grzip *data = (struct write_grzip *)f->data;
132 __archive_write_program_free(data->pdata);