xref: /freebsd/contrib/libarchive/libarchive/archive_read_set_options.c (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
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_read_private.h"
30 #include "archive_options_private.h"
31 
32 static int	archive_set_format_option(struct archive *a,
33 		    const char *m, const char *o, const char *v);
34 static int	archive_set_filter_option(struct archive *a,
35 		    const char *m, const char *o, const char *v);
36 static int	archive_set_option(struct archive *a,
37 		    const char *m, const char *o, const char *v);
38 
39 int
40 archive_read_set_format_option(struct archive *a, const char *m, const char *o,
41     const char *v)
42 {
43 	return _archive_set_option(a, m, o, v,
44 	    ARCHIVE_READ_MAGIC, "archive_read_set_format_option",
45 	    archive_set_format_option);
46 }
47 
48 int
49 archive_read_set_filter_option(struct archive *a, const char *m, const char *o,
50     const char *v)
51 {
52 	return _archive_set_option(a, m, o, v,
53 	    ARCHIVE_READ_MAGIC, "archive_read_set_filter_option",
54 	    archive_set_filter_option);
55 }
56 
57 int
58 archive_read_set_option(struct archive *a, const char *m, const char *o,
59     const char *v)
60 {
61 	return _archive_set_option(a, m, o, v,
62 	    ARCHIVE_READ_MAGIC, "archive_read_set_option",
63 	    archive_set_option);
64 }
65 
66 int
67 archive_read_set_options(struct archive *a, const char *options)
68 {
69 	return _archive_set_options(a, options,
70 	    ARCHIVE_READ_MAGIC, "archive_read_set_options",
71 	    archive_set_option);
72 }
73 
74 static int
75 archive_set_format_option(struct archive *_a, const char *m, const char *o,
76     const char *v)
77 {
78 	struct archive_read *a = (struct archive_read *)_a;
79 	struct archive_format_descriptor *format;
80 	size_t i;
81 	int r, rv = ARCHIVE_WARN;
82 
83 	for (i = 0; i < sizeof(a->formats)/sizeof(a->formats[0]); i++) {
84 		format = &a->formats[i];
85 		if (format == NULL || format->options == NULL ||
86 		    format->name == NULL)
87 			/* This format does not support option. */
88 			continue;
89 		if (m != NULL && strcmp(format->name, m) != 0)
90 			continue;
91 
92 		a->format = format;
93 		r = format->options(a, o, v);
94 		a->format = NULL;
95 
96 		if (r == ARCHIVE_FATAL)
97 			return (ARCHIVE_FATAL);
98 
99 		if (m != NULL)
100 			return (r);
101 
102 		if (r == ARCHIVE_OK)
103 			rv = ARCHIVE_OK;
104 	}
105 	/* If the format name didn't match, return a special code for
106 	 * _archive_set_option[s]. */
107 	if (rv == ARCHIVE_WARN && m != NULL)
108 		rv = ARCHIVE_WARN - 1;
109 	return (rv);
110 }
111 
112 static int
113 archive_set_filter_option(struct archive *_a, const char *m, const char *o,
114     const char *v)
115 {
116 	struct archive_read *a = (struct archive_read *)_a;
117 	struct archive_read_filter *filter;
118 	struct archive_read_filter_bidder *bidder;
119 	int r, rv = ARCHIVE_WARN;
120 
121 	for (filter = a->filter; filter != NULL; filter = filter->upstream) {
122 		bidder = filter->bidder;
123 		if (bidder == NULL)
124 			continue;
125 		if (bidder->options == NULL)
126 			/* This bidder does not support option */
127 			continue;
128 		if (m != NULL && strcmp(filter->name, m) != 0)
129 			continue;
130 
131 		r = bidder->options(bidder, o, v);
132 
133 		if (r == ARCHIVE_FATAL)
134 			return (ARCHIVE_FATAL);
135 
136 		if (m != NULL)
137 			return (r);
138 
139 		if (r == ARCHIVE_OK)
140 			rv = ARCHIVE_OK;
141 	}
142 	/* If the filter name didn't match, return a special code for
143 	 * _archive_set_option[s]. */
144 	if (rv == ARCHIVE_WARN && m != NULL)
145 		rv = ARCHIVE_WARN - 1;
146 	return (rv);
147 }
148 
149 static int
150 archive_set_option(struct archive *a, const char *m, const char *o,
151     const char *v)
152 {
153 	return _archive_set_either_option(a, m, o, v,
154 	    archive_set_format_option,
155 	    archive_set_filter_option);
156 }
157