Minor refactoring, related to lzah
[deark.git] / src / deark-modules.c
blob85de336978514c1d5489f14c7ad8511b27e48810
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"
13 #undef DE_MODULE
14 #undef DE_MODULE_LAST
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;
24 mi->run_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)
32 char tmpname[80];
33 const char *ptr1, *ptr2;
35 if(!s) return;
37 ptr1 = s;
38 while(1) {
39 int idx;
41 tmpname[0] = '\0';
42 ptr2 = de_strchr(ptr1, ',');
43 if(ptr2) {
44 size_t len;
45 len = (size_t)(ptr2-ptr1);
46 if(len<sizeof(tmpname)) {
47 de_memcpy(tmpname, ptr1, len);
48 tmpname[len] = '\0';
50 ptr1 = ptr2 + 1;
52 else {
53 de_strlcpy(tmpname, ptr1, sizeof(tmpname));
56 idx = de_get_module_idx_by_id(c, tmpname);
57 if(idx>=0) {
58 mod_set[idx] = 1;
61 if(!ptr2) break;
65 static void disable_modules_as_requested(deark *c)
67 int k;
68 u8 *mod_set;
70 if(!c->onlymods_string && !c->disablemods_string &&
71 !c->onlydetectmods_string && !c->nodetectmods_string)
73 return;
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++) {
82 if(!mod_set[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++) {
93 if(mod_set[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++) {
104 if(!mod_set[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++) {
115 if(mod_set[k]) {
116 c->module_info[k].flags |= DE_MODFLAG_DISABLEDETECT;
121 de_free(c, mod_set);
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"
130 #undef DE_MODULE
131 #undef DE_MODULE_LAST
133 size_t num_modules;
134 size_t i;
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)
155 deark *c;
156 c = de_create_internal();
157 c->module_register_fn = de_register_modules_internal;
158 return c;