forked from hasse69/rar2fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optdb.c
291 lines (264 loc) · 9.6 KB
/
optdb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
Copyright (C) 2009 Hans Beckerus (hans.beckerus#AT#gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
This program take use of the freeware "Unrar C++ Library" (libunrar)
by Alexander Roshal and some extensions to it.
Unrar source may be used in any software to handle RAR archives
without limitations free of charge, but cannot be used to re-create
the RAR compression algorithm, which is proprietary. Distribution
of modified Unrar source in separate form or as a part of other
software is permitted, provided that it is clearly stated in
the documentation and source comments that the code may not be used
to develop a RAR (WinRAR) compatible archiver.
*/
#include <platform.h>
#include <memory.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <libgen.h>
#include "debug.h"
#include "optdb.h"
static struct opt_entry opt_entry_[] = {
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 1, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 1},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 1},
{{NULL,}, 0, 0, 0, 0, 1},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0},
{{NULL,}, 0, 0, 0, 0, 0}
};
struct opt_entry *opt_entry_p = &opt_entry_[0];
#define OPT_(o) (opt_entry_p+(o))
#define OPT_STR_ u.v_arr_str,char*
#define OPT_INT_ u.v_arr_int,long
#define ADD_OPT_(o, s1, mt) ADD_OPT__(o, s1, mt)
#define ADD_OPT__(o, s1, m, t) \
do { \
OPT_(o)->is_set = 1; \
if ((OPT_(o))->n_elem == (OPT_(o))->n_max) { \
OPT_(o)->n_max += 16; \
OPT_(o)->m = (t*)realloc((t*)OPT_(o)->m, \
OPT_(o)->n_max * sizeof(t*)); \
} \
if (IS_STR_(o)) \
OPT_(o)->m[OPT_(o)->n_elem++] = (t)strdup(s1); \
else \
OPT_(o)->m[OPT_(o)->n_elem++] = (t)strtoul(s1, NULL, 10); \
} while (0)
#define CLR_OPT_(o) \
do { \
OPT_(o)->is_set = 0; \
if ((OPT_(o))->n_elem && IS_STR_(o)) { \
int i = (OPT_(o))->n_elem; \
while (i--) { \
free((void*)OPT_(o)->u.v_arr_str[i]);\
} \
}\
OPT_(o)->n_elem = 0; \
} while (0)
#define IS_INT_(o) (OPT_(o)->type)
#define IS_STR_(o) (!OPT_(o)->type)
/*!
*****************************************************************************
*
****************************************************************************/
int optdb_save(int opt, const char *s)
{
char *s1 = NULL;
char *endptr = NULL;
if (opt < 0 || opt > OPT_KEY_LAST)
return 1;
OPT_(opt)->is_set = 1;
if (OPT_(opt)->read_from_file && s && *s == '/') {
FILE *fp = fopen(s, "r");
if (fp) {
struct stat st;
(void)fstat(fileno(fp), &st);
s1 = malloc(st.st_size * 2);
if (s1) {
char *s2 = s1;
NO_UNUSED_RESULT fread(s1, 1, st.st_size, fp);
while(*s1) {
if (*s1=='\n') *s1=';';
s1++;
}
s1 = s2;
}
fclose(fp);
}
} else {
if (s)
s1 = strdup(s);
}
if (!s1)
return 0;
switch (opt)
{
case OPT_KEY_SEEK_DEPTH:
break;
case OPT_KEY_SEEK_LENGTH:
case OPT_KEY_HIST_SIZE:
{
NO_UNUSED_RESULT strtoul(s1, &endptr, 10);
if (*endptr)
return 1;
CLR_OPT_(opt);
ADD_OPT_(opt, s1, OPT_INT_);
break;
}
case OPT_KEY_SRC:
case OPT_KEY_DST:
CLR_OPT_(opt);
ADD_OPT_(opt, s1, OPT_STR_);
break;
default:
{
/*
* One could easily have used strsep() here but I
* choose not to:
* "This function suffers from the same problems as
* strtok(). In particular, it modifies the original
* string. Avoid it."
*/
char *s2 = s1;
char *s_free = s1; /* save pointer for free() */
if (strlen(s1)) {
while ((s2 = strchr(s2, ';'))) {
*s2++ = 0;
if (strlen(s1) > 1)
ADD_OPT_(opt, s1, OPT_STR_);
s1 = s2;
}
if (*s1)
ADD_OPT_(opt, s1, OPT_STR_);
}
s1 = s_free; /* restore pointer */
}
break;
}
if (s1)
free(s1);
#ifdef DEBUG_
{
int i;
printd(5, "option %d : ", opt);
for(i = 0; i < OPT_(opt)->n_elem; i++)
if (opt != OPT_KEY_SEEK_LENGTH)
printd(5, "\"%s\" ", OPT_(opt)->u.v_arr_str[i]);
else
printd(5, "\"%ld\" ", OPT_(opt)->u.v_arr_int[i]);
printd(5, "\n");
}
#endif
return 0;
}
/*!
*****************************************************************************
*
****************************************************************************/
static void reset_opt(int opt, int init)
{
if (opt < 0 || opt > OPT_KEY_LAST)
return;
CLR_OPT_(opt);
if (init) {
switch (opt) {
case OPT_KEY_IMG_TYPE:
ADD_OPT_(OPT_KEY_IMG_TYPE, ".iso", OPT_STR_);
ADD_OPT_(OPT_KEY_IMG_TYPE, ".img", OPT_STR_);
ADD_OPT_(OPT_KEY_IMG_TYPE, ".nrg", OPT_STR_);
break;
default:
break;
}
}
}
/*!
*****************************************************************************
*
****************************************************************************/
void optdb_init()
{
int i = OPT_KEY_END;
while (i--)
reset_opt(i, 1);
}
/*!
*****************************************************************************
*
****************************************************************************/
void optdb_destroy()
{
int i = OPT_KEY_END;
while (i--)
reset_opt(i, 0);
}
#undef ADD_OPT_
#undef OPT_
#undef OPT_INT_
#undef OPT_STR_
/*!
*****************************************************************************
*
****************************************************************************/
static inline int get_ext_len(char *s)
{
char *s1 = s + strlen(s);
while (s1 != s && *s1 != '.')
--s1;
return s1 == s ? 0 : strlen(s1);
}
/*!
*****************************************************************************
*
****************************************************************************/
int optdb_find(int opt, char *path)
{
int i = 0;
if (opt == OPT_KEY_EXCLUDE) {
while (i != OPT_CNT(opt)) {
char *tmp = OPT_STR(OPT_KEY_EXCLUDE, i);
char *safe_path = strdup(path);
if (!strcmp(basename(safe_path), tmp ? tmp : "")) {
free(safe_path);
return 1;
}
free(safe_path);
++i;
}
} else if (opt == OPT_KEY_FAKE_ISO || opt == OPT_KEY_IMG_TYPE) {
int l = get_ext_len(path);
if (l <= 1)
return 0;
while (i != OPT_CNT(opt)) {
char *tmp = OPT_STR(opt, i);
if (!strcmp((path) + (strlen(path) - l), tmp ? tmp : ""))
return l - 1;
++i;
}
}
return 0;
}