aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/make-dissectors.c
blob: 68bf37df4eeb7afe4014f7ef53ca3dbdc0217c62 (plain)
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
/* make-dissectors.c
 * Tool to build the dissector registration arrays.
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <glib.h>
#include <wsutil/glib-compat.h>

#define ARRAY_RESERVED_SIZE     2048
#define STRING_RESERVED_SIZE    (300 * 1024)

#ifdef _WIN32
  #define SEP   "\r\n"
#else
  #define SEP   "\n"
#endif

GRegex *protos_regex, *handoffs_regex;

static int
compare_symbols(gconstpointer a, gconstpointer b)
{
    return g_strcmp0(*(const char **)a, *(const char **)b);
}

static void
scan_matches(GRegex *regex, const char *string, GPtrArray *dst)
{
    GMatchInfo *match_info;
    char *match;

    g_regex_match(regex, string, G_REGEX_MATCH_NOTEMPTY, &match_info);
    while (g_match_info_matches(match_info)) {
        match = g_match_info_fetch(match_info, 1);
        g_ptr_array_add(dst, match);
        g_match_info_next(match_info, NULL);
    }
    g_match_info_free(match_info);
}

static void
scan_file(const char *file, GPtrArray *protos, GPtrArray *handoffs)
{
    char *contents;
    GError *err = NULL;

    if (!g_file_get_contents(file, &contents, NULL, &err)) {
        fprintf(stderr, "%s: %s\n", file, err->message);
        exit(1);
    }
    scan_matches(protos_regex, contents, protos);
    scan_matches(handoffs_regex, contents, handoffs);
    g_free(contents);
}

static void
scan_list(const char *file, GPtrArray *protos, GPtrArray *handoffs)
{
    char *contents, *arg;
    GError *err = NULL;

    if (!g_file_get_contents(file, &contents, NULL, &err)) {
        fprintf(stderr, "%s: %s\n", file, err->message);
        exit(1);
    }
    for (arg = strtok(contents, SEP); arg != NULL; arg = strtok(NULL, SEP)) {
        scan_file(arg, protos, handoffs);
    }
    g_free(contents);
}

int main(int argc, char **argv)
{
    GPtrArray *protos = NULL, *handoffs = NULL;
    GError *err = NULL;
    guint i;
    GString *s;
    const char *outfile;
    guint count_protos, count_handoffs;

    if (argc < 3) {
        fprintf(stderr, "Usage: %s <outfile> <infiles...>\n", argv[0]);
        exit(1);
    }

    protos = g_ptr_array_new_full(ARRAY_RESERVED_SIZE, g_free);
    handoffs = g_ptr_array_new_full(ARRAY_RESERVED_SIZE, g_free);

    protos_regex = g_regex_new("void\\s+(proto_register_[[:alnum:]_]+)\\s*\\(\\s*void\\s*\\)\\s*{",
                                    G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, &err);
    if (err) {
        fprintf(stderr, "GRegex: %s\n", err->message);
        exit(1);
    }
    handoffs_regex = g_regex_new("void\\s+(proto_reg_handoff_[[:alnum:]_]+)\\s*\\(\\s*void\\s*\\)\\s*{",
                                    G_REGEX_OPTIMIZE, G_REGEX_MATCH_NOTEMPTY, &err);
    if (err) {
        fprintf(stderr, "GRegex: %s\n", err->message);
        exit(1);
    }

    outfile = argv[1];
    for (int arg = 2; arg < argc; arg++) {
        if (argv[arg][0] == '@') {
            scan_list(&argv[arg][1], protos, handoffs);
        }
        else {
            scan_file(argv[arg], protos, handoffs);
        }
    }

    if (protos->len == 0) {
        fprintf(stderr, "No protocol registrations found.\n");
        exit(1);
    }

    g_ptr_array_sort(protos, compare_symbols);
    g_ptr_array_sort(handoffs, compare_symbols);

    s = g_string_sized_new(STRING_RESERVED_SIZE);

    g_string_append(s,
           "/*\n"
           " * Do not modify this file. Changes will be overwritten.\n"
           " *\n"
           " * Generated automatically by the \"dissectors.c\" target using\n"
           " * \"make-dissectors\".\n"
           " */\n"
           "\n"
           "#include <dissectors.h>\n"
           "\n");

    g_string_append_printf(s,
           "const gulong dissector_reg_proto_count = %d;\n"
           "const gulong dissector_reg_handoff_count = %d;\n"
           "\n",
            protos->len, handoffs->len);

    for (i = 0; i < protos->len; i++) {
        g_string_append_printf(s, "void %s(void);\n", (char *)protos->pdata[i]);
    }
    g_string_append(s,
           "\n"
           "dissector_reg_t dissector_reg_proto[] = {\n");
    for (i = 0; i < protos->len; i++) {
        g_string_append_printf(s, "    { \"%s\", %s },\n", (char *)protos->pdata[i], (char *)protos->pdata[i]);
    }
    g_string_append(s,
           "    { NULL, NULL }\n"
           "};\n"
           "\n");

    for (i = 0; i < handoffs->len; i++) {
        g_string_append_printf(s, "void %s(void);\n", (char *)handoffs->pdata[i]);
    }
    g_string_append(s,
           "\n"
           "dissector_reg_t dissector_reg_handoff[] = {\n");
    for (i = 0; i < handoffs->len; i++) {
        g_string_append_printf(s, "    { \"%s\", %s },\n", (char *)handoffs->pdata[i], (char *)handoffs->pdata[i]);
    }
    g_string_append(s,
           "    { NULL, NULL }\n"
           "};\n");

    if (!g_file_set_contents(outfile, s->str, s->len, &err)) {
        fprintf(stderr, "%s: %s\n", outfile, err->message);
        exit(1);
    }

    count_protos = protos->len;
    count_handoffs = handoffs->len;

    g_string_free(s, TRUE);

    g_regex_unref(protos_regex);
    g_regex_unref(handoffs_regex);

    g_ptr_array_free(protos, TRUE);
    g_ptr_array_free(handoffs, TRUE);

    printf("Found %u registrations and %u handoffs.\n",
                count_protos, count_handoffs);
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */