1 /*-
2  * Copyright (c) 2011 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 __FBSDID("$FreeBSD$");
28 
29 #include "archive_options_private.h"
30 
31 static const char *
32 parse_option(const char **str,
33     const char **mod, const char **opt, const char **val);
34 
35 int
36 _archive_set_option(struct archive *a,
37     const char *m, const char *o, const char *v,
38     int magic, const char *fn, option_handler use_option)
39 {
40 	const char *mp, *op, *vp;
41 	int r;
42 
43 	archive_check_magic(a, magic, ARCHIVE_STATE_NEW, fn);
44 
45 	mp = m != NULL && m[0] == '\0' ? NULL : m;
46 	op = o != NULL && o[0] == '\0' ? NULL : o;
47 	vp = v != NULL && v[0] == '\0' ? NULL : v;
48 
49 	if (op == NULL && vp == NULL)
50 		return (ARCHIVE_OK);
51 	if (op == NULL) {
52 		archive_set_error(a, ARCHIVE_ERRNO_MISC, "Empty option");
53 		return (ARCHIVE_FAILED);
54 	}
55 
56 	r = use_option(a, mp, op, vp);
57 	if (r == ARCHIVE_WARN - 1) {
58 		archive_set_error(a, ARCHIVE_ERRNO_MISC,
59 		    "Unknown module name: `%s'", mp);
60 		return (ARCHIVE_FAILED);
61 	}
62 	if (r == ARCHIVE_WARN) {
63 		archive_set_error(a, ARCHIVE_ERRNO_MISC,
64 		    "Undefined option: `%s%s%s%s%s%s'",
65 		    vp?"":"!", mp?mp:"", mp?":":"", op, vp?"=":"", vp?vp:"");
66 		return (ARCHIVE_FAILED);
67 	}
68 	return (r);
69 }
70 
71 int
72 _archive_set_either_option(struct archive *a, const char *m, const char *o, const char *v,
73     option_handler use_format_option, option_handler use_filter_option)
74 {
75 	int r1, r2;
76 
77 	if (o == NULL && v == NULL)
78 		return (ARCHIVE_OK);
79 	if (o == NULL)
80 		return (ARCHIVE_FAILED);
81 
82 	r1 = use_format_option(a, m, o, v);
83 	if (r1 == ARCHIVE_FATAL)
84 		return (ARCHIVE_FATAL);
85 
86 	r2 = use_filter_option(a, m, o, v);
87 	if (r2 == ARCHIVE_FATAL)
88 		return (ARCHIVE_FATAL);
89 
90 	return r1 > r2 ? r1 : r2;
91 }
92 
93 int
94 _archive_set_options(struct archive *a, const char *options,
95     int magic, const char *fn, option_handler use_option)
96 {
97 	int allok = 1, anyok = 0, r;
98 	char *data;
99 	const char *s, *mod, *opt, *val;
100 
101 	archive_check_magic(a, magic, ARCHIVE_STATE_NEW, fn);
102 
103 	if (options == NULL || options[0] == '\0')
104 		return ARCHIVE_OK;
105 
106 	data = (char *)malloc(strlen(options) + 1);
107 	strcpy(data, options);
108 	s = (const char *)data;
109 
110 	do {
111 		mod = opt = val = NULL;
112 
113 		parse_option(&s, &mod, &opt, &val);
114 
115 		r = use_option(a, mod, opt, val);
116 		if (r == ARCHIVE_FATAL) {
117 			free(data);
118 			return (ARCHIVE_FATAL);
119 		}
120 		if (r == ARCHIVE_FAILED && mod != NULL) {
121 			free(data);
122 			return (ARCHIVE_FAILED);
123 		}
124 		if (r == ARCHIVE_WARN - 1) {
125 			/* The module name is wrong. */
126 			archive_set_error(a, ARCHIVE_ERRNO_MISC,
127 			    "Unknown module name: `%s'", mod);
128 			free(data);
129 			return (ARCHIVE_FAILED);
130 		}
131 		if (r == ARCHIVE_WARN) {
132 			/* The option name is wrong. No-one used this. */
133 			archive_set_error(a, ARCHIVE_ERRNO_MISC,
134 			    "Undefined option: `%s%s%s'",
135 			    mod?mod:"", mod?":":"", opt);
136 			free(data);
137 			return (ARCHIVE_FAILED);
138 		}
139 		if (r == ARCHIVE_OK)
140 			anyok = 1;
141 		else
142 			allok = 0;
143 	} while (s != NULL);
144 
145 	free(data);
146 	return allok ? ARCHIVE_OK : anyok ? ARCHIVE_WARN : ARCHIVE_FAILED;
147 }
148 
149 static const char *
150 parse_option(const char **s, const char **m, const char **o, const char **v)
151 {
152 	const char *end, *mod, *opt, *val;
153 	char *p;
154 
155 	end = NULL;
156 	mod = NULL;
157 	opt = *s;
158 	val = "1";
159 
160 	p = strchr(opt, ',');
161 
162 	if (p != NULL) {
163 		*p = '\0';
164 		end = ((const char *)p) + 1;
165 	}
166 
167 	if (0 == strlen(opt)) {
168 		*s = end;
169 		*m = NULL;
170 		*o = NULL;
171 		*v = NULL;
172 		return end;
173 	}
174 
175 	p = strchr(opt, ':');
176 	if (p != NULL) {
177 		*p = '\0';
178 		mod = opt;
179 		opt = ++p;
180 	}
181 
182 	p = strchr(opt, '=');
183 	if (p != NULL) {
184 		*p = '\0';
185 		val = ++p;
186 	} else if (opt[0] == '!') {
187 		++opt;
188 		val = NULL;
189 	}
190 
191 	*s = end;
192 	*m = mod;
193 	*o = opt;
194 	*v = val;
195 
196 	return end;
197 }
198 
199