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$");
41
#include "archive_string.h"
42
#include "archive_write_private.h"
45
struct archive_write_program_data *pdata;
46
int compression_level;
47
enum { lzma = 0, bzip2, gzip, lzo, zpaq } compression;
50
static int archive_write_lrzip_open(struct archive_write_filter *);
51
static int archive_write_lrzip_options(struct archive_write_filter *,
52
const char *, const char *);
53
static int archive_write_lrzip_write(struct archive_write_filter *,
54
const void *, size_t);
55
static int archive_write_lrzip_close(struct archive_write_filter *);
56
static int archive_write_lrzip_free(struct archive_write_filter *);
59
archive_write_add_filter_lrzip(struct archive *_a)
61
struct archive_write_filter *f = __archive_write_allocate_filter(_a);
62
struct write_lrzip *data;
64
archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
65
ARCHIVE_STATE_NEW, "archive_write_add_filter_lrzip");
67
data = calloc(1, sizeof(*data));
69
archive_set_error(_a, ENOMEM, "Can't allocate memory");
70
return (ARCHIVE_FATAL);
72
data->pdata = __archive_write_program_allocate();
73
if (data->pdata == NULL) {
75
archive_set_error(_a, ENOMEM, "Can't allocate memory");
76
return (ARCHIVE_FATAL);
80
f->code = ARCHIVE_FILTER_LRZIP;
82
f->open = archive_write_lrzip_open;
83
f->options = archive_write_lrzip_options;
84
f->write = archive_write_lrzip_write;
85
f->close = archive_write_lrzip_close;
86
f->free = archive_write_lrzip_free;
88
/* Note: This filter always uses an external program, so we
89
* return "warn" to inform of the fact. */
90
archive_set_error(_a, ARCHIVE_ERRNO_MISC,
91
"Using external lrzip program for lrzip compression");
92
return (ARCHIVE_WARN);
96
archive_write_lrzip_options(struct archive_write_filter *f, const char *key,
99
struct write_lrzip *data = (struct write_lrzip *)f->data;
101
if (strcmp(key, "compression") == 0) {
103
return (ARCHIVE_WARN);
104
else if (strcmp(value, "bzip2") == 0)
105
data->compression = bzip2;
106
else if (strcmp(value, "gzip") == 0)
107
data->compression = gzip;
108
else if (strcmp(value, "lzo") == 0)
109
data->compression = lzo;
110
else if (strcmp(value, "zpaq") == 0)
111
data->compression = zpaq;
113
return (ARCHIVE_WARN);
115
} else if (strcmp(key, "compression-level") == 0) {
116
if (value == NULL || !(value[0] >= '1' && value[0] <= '9') ||
118
return (ARCHIVE_WARN);
119
data->compression_level = value[0] - '0';
122
/* Note: The "warn" return is just to inform the options
123
* supervisor that we didn't handle it. It will generate
124
* a suitable error if no one used this option. */
125
return (ARCHIVE_WARN);
129
archive_write_lrzip_open(struct archive_write_filter *f)
131
struct write_lrzip *data = (struct write_lrzip *)f->data;
132
struct archive_string as;
135
archive_string_init(&as);
136
archive_strcpy(&as, "lrzip -q");
138
/* Specify compression type. */
139
switch (data->compression) {
140
case lzma:/* default compression */
143
archive_strcat(&as, " -b");
146
archive_strcat(&as, " -g");
149
archive_strcat(&as, " -l");
152
archive_strcat(&as, " -z");
156
/* Specify compression level. */
157
if (data->compression_level > 0) {
158
archive_strcat(&as, " -L ");
159
archive_strappend_char(&as, '0' + data->compression_level);
162
r = __archive_write_program_open(f, data->pdata, as.s);
163
archive_string_free(&as);
168
archive_write_lrzip_write(struct archive_write_filter *f,
169
const void *buff, size_t length)
171
struct write_lrzip *data = (struct write_lrzip *)f->data;
173
return __archive_write_program_write(f, data->pdata, buff, length);
177
archive_write_lrzip_close(struct archive_write_filter *f)
179
struct write_lrzip *data = (struct write_lrzip *)f->data;
181
return __archive_write_program_close(f, data->pdata);
185
archive_write_lrzip_free(struct archive_write_filter *f)
187
struct write_lrzip *data = (struct write_lrzip *)f->data;
189
__archive_write_program_free(data->pdata);