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