2 * Regex based text importer
3 * March 2021, Paul Weiß <paulniklasweiss@gmail.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * Based on text_import.c by Jaap Keuter <jaap.keuter@xs4all.nl>
11 * SPDX-License-Identifier: GPL-2.0-or-later
21 #include "text_import.h"
22 #include "text_import_regex.h"
24 typedef unsigned int uint
;
26 /*--- Options --------------------------------------------------------------------*/
28 int text_import_regex(const text_import_info_t
* info
) {
30 int parsed_packets
= 0;
31 ws_debug("starting import...");
34 GMappedFile
* file
= g_mapped_file_ref(info
->regex
.import_text_GMappedFile
);
35 GError
* gerror
= NULL
;
36 size_t f_size
= g_mapped_file_get_length(file
);
37 unsigned char* f_content
= g_mapped_file_get_contents(file
);
39 // Regex result dissecting
40 bool re_time
, re_dir
, re_seqno
;
45 re_time
= g_regex_get_string_number(info
->regex
.format
, "time") >= 0;
46 re_dir
= g_regex_get_string_number(info
->regex
.format
, "dir") >= 0;
47 re_seqno
= g_regex_get_string_number(info
->regex
.format
, "seqno") >= 0;
48 if (g_regex_get_string_number(info
->regex
.format
, "data") < 0) {
49 /* This should never happen, as the dialog checks for this */
50 fprintf(stderr
, "Error could not find data in pattern\n");
51 g_mapped_file_unref(file
);
56 ws_debug("regex has %s%s%s", re_dir
? "dir, " : "",
57 re_time
? "time, " : "",
58 re_seqno
? "seqno, " : "");
59 g_regex_match_full(info
->regex
.format
, f_content
, f_size
, 0, G_REGEX_MATCH_NOTEMPTY
, &match
, &gerror
);
60 while (g_match_info_matches(match
)) {
62 if (g_match_info_fetch_named_pos(match
, "data", &field_start
, &field_end
)) {
63 parse_data(f_content
+ field_start
, f_content
+ field_end
, info
->regex
.encoding
);
65 /* parse the auxiliary information if present */
67 g_match_info_fetch_named_pos(match
, "time", &field_start
, &field_end
)) {
68 parse_time(f_content
+ field_start
, f_content
+ field_end
, info
->timestamp_format
);
70 /* No time present, so add a fixed delta. */
71 parse_time(NULL
, NULL
, NULL
);
75 g_match_info_fetch_named_pos(match
, "dir", &field_start
, &field_end
))
76 parse_dir(f_content
+ field_start
, f_content
+ field_end
, info
->regex
.in_indication
, info
->regex
.out_indication
);
79 g_match_info_fetch_named_pos(match
, "seqno", &field_start
, &field_end
))
80 parse_seqno(f_content
+ field_start
, f_content
+ field_end
);
82 if (ws_log_get_level() == LOG_LEVEL_NOISY
) {
83 g_match_info_fetch_pos(match
, 0, &field_start
, &field_end
);
84 ws_noisy("Packet %d at %x to %x: %.*s\n", parsed_packets
+ 1,
85 field_start
, field_end
,
86 field_end
- field_start
, f_content
+ field_start
);
91 fprintf(stderr
, "Warning: could not fetch data on would be packet %d, discarding\n", parsed_packets
+ 1);
93 /* prepare next packet */
94 g_match_info_next(match
, &gerror
);
95 if (gerror
&& gerror
->code
) {
101 ws_debug("processed %d packets", parsed_packets
);
102 g_match_info_unref(match
);
103 g_mapped_file_unref(file
);
104 return status
* parsed_packets
;