1 // This file is part of Deark.
2 // Copyright (C) 2016 Jason Summers
3 // See the file COPYING for terms of use.
5 #define DE_NOT_IN_MODULE
6 #include "deark-config.h"
7 #include "deark-private.h"
8 #include "deark-user.h"
10 #define DE_MODULE(x) DE_DECLARE_MODULE(x);
11 #define DE_MODULE_LAST(x) DE_DECLARE_MODULE(x);
12 #include "deark-modules.h"
16 static void register_a_module(deark
*c
, de_module_getinfo_fn infofunc
)
18 infofunc(c
, &c
->module_info
[c
->num_modules
++]);
21 static void disable_module(deark
*c
, struct deark_module_info
*mi
)
23 mi
->identify_fn
= NULL
;
27 // Caller supplies mod_set[c->num_modules].
28 // Sets mod_set[n] = 1 for each module in 's', a comma-separated list.
29 // Does not modify other entries in mod_set[].
30 static void module_list_string_to_set(deark
*c
, const char *s
, u8
*mod_set
)
33 const char *ptr1
, *ptr2
;
42 ptr2
= de_strchr(ptr1
, ',');
45 len
= (size_t)(ptr2
-ptr1
);
46 if(len
<sizeof(tmpname
)) {
47 de_memcpy(tmpname
, ptr1
, len
);
53 de_strlcpy(tmpname
, ptr1
, sizeof(tmpname
));
56 idx
= de_get_module_idx_by_id(c
, tmpname
);
65 static void disable_modules_as_requested(deark
*c
)
70 if(!c
->onlymods_string
&& !c
->disablemods_string
&&
71 !c
->onlydetectmods_string
&& !c
->nodetectmods_string
)
76 mod_set
= de_malloc(c
, c
->num_modules
);
78 if(c
->onlymods_string
) {
79 module_list_string_to_set(c
, c
->onlymods_string
, mod_set
);
80 // Disable modules not in the list
81 for(k
=0; k
<c
->num_modules
; k
++) {
83 disable_module(c
, &c
->module_info
[k
]);
88 if(c
->disablemods_string
) {
89 de_zeromem(mod_set
, c
->num_modules
);
90 module_list_string_to_set(c
, c
->disablemods_string
, mod_set
);
91 // Disable modules in the list
92 for(k
=0; k
<c
->num_modules
; k
++) {
94 disable_module(c
, &c
->module_info
[k
]);
99 if(c
->onlydetectmods_string
) {
100 de_zeromem(mod_set
, c
->num_modules
);
101 module_list_string_to_set(c
, c
->onlydetectmods_string
, mod_set
);
102 // Set MODFLAG_DISABLEDETECT for modules not in the list
103 for(k
=0; k
<c
->num_modules
; k
++) {
105 c
->module_info
[k
].flags
|= DE_MODFLAG_DISABLEDETECT
;
110 if(c
->nodetectmods_string
) {
111 de_zeromem(mod_set
, c
->num_modules
);
112 module_list_string_to_set(c
, c
->nodetectmods_string
, mod_set
);
113 // Set MODFLAG_DISABLEDETECT for modules in the list
114 for(k
=0; k
<c
->num_modules
; k
++) {
116 c
->module_info
[k
].flags
|= DE_MODFLAG_DISABLEDETECT
;
124 static void de_register_modules_internal(deark
*c
)
126 static const de_module_getinfo_fn infofunc_list
[] = {
127 #define DE_MODULE(x) x,
128 #define DE_MODULE_LAST(x) x
129 #include "deark-modules.h"
131 #undef DE_MODULE_LAST
136 if(c
->module_info
) return; // Already done.
138 num_modules
= DE_ARRAYCOUNT(infofunc_list
);
140 c
->module_info
= de_mallocarray(c
, num_modules
, sizeof(struct deark_module_info
));
142 for(i
=0; i
<num_modules
; i
++) {
143 register_a_module(c
, infofunc_list
[i
]);
146 disable_modules_as_requested(c
);
149 // A wrapper for the real de_create function (de_create_internal), which also
150 // records a pointer to the register_modules function.
151 // This allows deark-modules.c, and indirectly deark-cmd.c, to be the only C
152 // files for which the symbols in the individual modules have to be visible.
153 deark
*de_create(void)
156 c
= de_create_internal();
157 c
->module_register_fn
= de_register_modules_internal
;