1248590Smm/*-
2248590Smm * Copyright (c) 2003-2007 Tim Kientzle
3248590Smm * All rights reserved.
4248590Smm *
5248590Smm * Redistribution and use in source and binary forms, with or without
6248590Smm * modification, are permitted provided that the following conditions
7248590Smm * are met:
8248590Smm * 1. Redistributions of source code must retain the above copyright
9248590Smm *    notice, this list of conditions and the following disclaimer.
10248590Smm * 2. Redistributions in binary form must reproduce the above copyright
11248590Smm *    notice, this list of conditions and the following disclaimer in the
12248590Smm *    documentation and/or other materials provided with the distribution.
13248590Smm *
14248590Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15248590Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16248590Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17248590Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18248590Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19248590Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20248590Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21248590Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22248590Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23248590Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24248590Smm */
25248590Smm
26248590Smm#include "archive_platform.h"
27248590Smm
28248590Smm__FBSDID("$FreeBSD$");
29248590Smm
30248590Smm#ifdef HAVE_ERRNO_H
31248590Smm#include <errno.h>
32248590Smm#endif
33248590Smm#ifdef HAVE_STDLIB_H
34248590Smm#include <stdlib.h>
35248590Smm#endif
36248590Smm
37248590Smm#include "archive.h"
38248590Smm#include "archive_write_private.h"
39248590Smm
40248590Smmstruct write_grzip {
41248590Smm	struct archive_write_program_data *pdata;
42248590Smm};
43248590Smm
44248590Smmstatic int archive_write_grzip_open(struct archive_write_filter *);
45248590Smmstatic int archive_write_grzip_options(struct archive_write_filter *,
46248590Smm		    const char *, const char *);
47248590Smmstatic int archive_write_grzip_write(struct archive_write_filter *,
48248590Smm		    const void *, size_t);
49248590Smmstatic int archive_write_grzip_close(struct archive_write_filter *);
50248590Smmstatic int archive_write_grzip_free(struct archive_write_filter *);
51248590Smm
52248590Smmint
53248590Smmarchive_write_add_filter_grzip(struct archive *_a)
54248590Smm{
55248590Smm	struct archive_write_filter *f = __archive_write_allocate_filter(_a);
56248590Smm	struct write_grzip *data;
57248590Smm
58248590Smm	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
59248590Smm	    ARCHIVE_STATE_NEW, "archive_write_add_filter_grzip");
60248590Smm
61248590Smm	data = calloc(1, sizeof(*data));
62248590Smm	if (data == NULL) {
63248590Smm		archive_set_error(_a, ENOMEM, "Can't allocate memory");
64248590Smm		return (ARCHIVE_FATAL);
65248590Smm	}
66302001Smm	data->pdata = __archive_write_program_allocate("grzip");
67248590Smm	if (data->pdata == NULL) {
68248590Smm		free(data);
69248590Smm		archive_set_error(_a, ENOMEM, "Can't allocate memory");
70248590Smm		return (ARCHIVE_FATAL);
71248590Smm	}
72248590Smm
73248590Smm	f->name = "grzip";
74248590Smm	f->code = ARCHIVE_FILTER_GRZIP;
75248590Smm	f->data = data;
76248590Smm	f->open = archive_write_grzip_open;
77248590Smm	f->options = archive_write_grzip_options;
78248590Smm	f->write = archive_write_grzip_write;
79248590Smm	f->close = archive_write_grzip_close;
80248590Smm	f->free = archive_write_grzip_free;
81248590Smm
82248590Smm	/* Note: This filter always uses an external program, so we
83248590Smm	 * return "warn" to inform of the fact. */
84248590Smm	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
85248590Smm	    "Using external grzip program for grzip compression");
86248590Smm	return (ARCHIVE_WARN);
87248590Smm}
88248590Smm
89248590Smmstatic int
90248590Smmarchive_write_grzip_options(struct archive_write_filter *f, const char *key,
91248590Smm    const char *value)
92248590Smm{
93248590Smm	(void)f; /* UNUSED */
94248590Smm	(void)key; /* UNUSED */
95248590Smm	(void)value; /* UNUSED */
96248590Smm	/* Note: The "warn" return is just to inform the options
97248590Smm	 * supervisor that we didn't handle it.  It will generate
98248590Smm	 * a suitable error if no one used this option. */
99248590Smm	return (ARCHIVE_WARN);
100248590Smm}
101248590Smm
102248590Smmstatic int
103248590Smmarchive_write_grzip_open(struct archive_write_filter *f)
104248590Smm{
105248590Smm	struct write_grzip *data = (struct write_grzip *)f->data;
106248590Smm
107248590Smm	return __archive_write_program_open(f, data->pdata, "grzip");
108248590Smm}
109248590Smm
110248590Smmstatic int
111248590Smmarchive_write_grzip_write(struct archive_write_filter *f,
112248590Smm    const void *buff, size_t length)
113248590Smm{
114248590Smm	struct write_grzip *data = (struct write_grzip *)f->data;
115248590Smm
116248590Smm	return __archive_write_program_write(f, data->pdata, buff, length);
117248590Smm}
118248590Smm
119248590Smmstatic int
120248590Smmarchive_write_grzip_close(struct archive_write_filter *f)
121248590Smm{
122248590Smm	struct write_grzip *data = (struct write_grzip *)f->data;
123248590Smm
124248590Smm	return __archive_write_program_close(f, data->pdata);
125248590Smm}
126248590Smm
127248590Smmstatic int
128248590Smmarchive_write_grzip_free(struct archive_write_filter *f)
129248590Smm{
130248590Smm	struct write_grzip *data = (struct write_grzip *)f->data;
131248590Smm
132248590Smm	__archive_write_program_free(data->pdata);
133248590Smm	free(data);
134248590Smm	return (ARCHIVE_OK);
135248590Smm}
136