Coverage Report

Created: 2026-09-14 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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 char *
35
parse_option(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
33.4k
{
78
33.4k
  int r1, r2;
79
80
33.4k
  if (o == NULL && v == NULL)
81
0
    return (ARCHIVE_OK);
82
33.4k
  if (o == NULL)
83
0
    return (ARCHIVE_FAILED);
84
85
33.4k
  r1 = use_format_option(a, m, o, v);
86
33.4k
  if (r1 == ARCHIVE_FATAL)
87
0
    return (ARCHIVE_FATAL);
88
89
33.4k
  r2 = use_filter_option(a, m, o, v);
90
33.4k
  if (r2 == ARCHIVE_FATAL)
91
0
    return (ARCHIVE_FATAL);
92
93
33.4k
  if (r1 == ARCHIVE_WARN - 1)
94
0
    return r2;
95
33.4k
  if (r2 == ARCHIVE_WARN -1)
96
0
    return r1;
97
33.4k
  return r1 > r2 ? r1 : r2;
98
33.4k
}
99
100
int
101
_archive_set_options(struct archive *a, const char *options,
102
    unsigned int magic, const char *fn, option_handler use_option)
103
33.4k
{
104
33.4k
  int allok = 1, anyok = 0, ignore_mod_err = 0, r;
105
33.4k
  char *data, *s;
106
33.4k
  const char *mod, *opt, *val;
107
108
33.4k
  archive_check_magic(a, magic, ARCHIVE_STATE_NEW, fn);
109
110
33.4k
  if (options == NULL || options[0] == '\0')
111
0
    return ARCHIVE_OK;
112
113
33.4k
  if ((data = strdup(options)) == NULL) {
114
0
    archive_set_error(a,
115
0
        ENOMEM, "Out of memory adding file to list");
116
0
    return (ARCHIVE_FATAL);
117
0
  }
118
33.4k
  s = data;
119
120
33.4k
  do {
121
33.4k
    mod = opt = val = NULL;
122
123
33.4k
    parse_option(&s, &mod, &opt, &val);
124
33.4k
    if (mod == NULL && opt != NULL &&
125
33.4k
        strcmp("__ignore_wrong_module_name__", opt) == 0) {
126
      /* Ignore module name error */
127
0
      if (val != NULL) {
128
0
        ignore_mod_err = 1;
129
0
        anyok = 1;
130
0
      }
131
0
      continue;
132
0
    }
133
134
33.4k
    r = use_option(a, mod, opt, val);
135
33.4k
    if (r == ARCHIVE_FATAL) {
136
0
      free(data);
137
0
      return (ARCHIVE_FATAL);
138
0
    }
139
33.4k
    if (r == ARCHIVE_FAILED && mod != NULL) {
140
0
      free(data);
141
0
      return (ARCHIVE_FAILED);
142
0
    }
143
33.4k
    if (r == ARCHIVE_WARN - 1) {
144
0
      if (ignore_mod_err)
145
0
        continue;
146
      /* The module name is wrong. */
147
0
      archive_set_error(a, ARCHIVE_ERRNO_MISC,
148
0
          "Unknown module name: `%s'", mod);
149
0
      free(data);
150
0
      return (ARCHIVE_FAILED);
151
0
    }
152
33.4k
    if (r == ARCHIVE_WARN) {
153
      /* The option name is wrong. No-one used this. */
154
0
      archive_set_error(a, ARCHIVE_ERRNO_MISC,
155
0
          "Undefined option: `%s%s%s'",
156
0
          mod?mod:"", mod?":":"", opt);
157
0
      free(data);
158
0
      return (ARCHIVE_FAILED);
159
0
    }
160
33.4k
    if (r == ARCHIVE_OK)
161
33.4k
      anyok = 1;
162
0
    else
163
0
      allok = 0;
164
33.4k
  } while (s != NULL);
165
166
33.4k
  free(data);
167
33.4k
  return allok ? ARCHIVE_OK : anyok ? ARCHIVE_WARN : ARCHIVE_FAILED;
168
33.4k
}
169
170
static char *
171
parse_option(char **s, const char **m, const char **o, const char **v)
172
33.4k
{
173
33.4k
  const char *mod, *val;
174
33.4k
  char *end, *opt, *p;
175
176
33.4k
  end = NULL;
177
33.4k
  mod = NULL;
178
33.4k
  opt = *s;
179
33.4k
  val = "1";
180
181
33.4k
  p = strchr(opt, ',');
182
183
33.4k
  if (p != NULL) {
184
0
    *p = '\0';
185
0
    end = p + 1;
186
0
  }
187
188
33.4k
  if (0 == strlen(opt)) {
189
0
    *s = end;
190
0
    *m = NULL;
191
0
    *o = NULL;
192
0
    *v = NULL;
193
0
    return end;
194
0
  }
195
196
33.4k
  p = strchr(opt, ':');
197
33.4k
  if (p != NULL) {
198
0
    *p = '\0';
199
0
    mod = opt;
200
0
    opt = ++p;
201
0
  }
202
203
33.4k
  p = strchr(opt, '=');
204
33.4k
  if (p != NULL) {
205
33.4k
    *p = '\0';
206
33.4k
    val = ++p;
207
33.4k
  } else if (opt[0] == '!') {
208
0
    ++opt;
209
0
    val = NULL;
210
0
  }
211
212
33.4k
  *s = end;
213
33.4k
  *m = mod;
214
33.4k
  *o = opt;
215
33.4k
  *v = val;
216
217
33.4k
  return end;
218
33.4k
}
219