attr_dissector_fn_t
[wireshark-sm.git] / ui / text_import_regex.c
blobe587cdb736f2fbdd9dcd0544f296dfcbf5543630
1 /* text_import_regex.c
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
14 #include "config.h"
16 #include <stdio.h>
17 #include <stdlib.h>
19 #include <glib.h>
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) {
29 int status = 1;
30 int parsed_packets = 0;
31 ws_debug("starting import...");
33 // IO
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);
38 { /* zero terminate the file */
39 if (f_content[f_size - 1] != '\n') {
40 fprintf(stderr, "Error: file did not end on \\n\n");
41 g_mapped_file_unref(file);
42 return -1;
44 f_content[f_size] = 0;
47 // Regex result dissecting
48 bool re_time, re_dir, re_seqno;
49 GMatchInfo* match;
50 int field_start;
51 int field_end;
52 { /* analyze regex */
53 re_time = g_regex_get_string_number(info->regex.format, "time") >= 0;
54 re_dir = g_regex_get_string_number(info->regex.format, "dir") >= 0;
55 re_seqno = g_regex_get_string_number(info->regex.format, "seqno") >= 0;
56 if (g_regex_get_string_number(info->regex.format, "data") < 0) {
57 /* This should never happen, as the dialog checks for this */
58 fprintf(stderr, "Error could not find data in pattern\n");
59 g_mapped_file_unref(file);
60 return -1;
64 ws_debug("regex has %s%s%s", re_dir ? "dir, " : "",
65 re_time ? "time, " : "",
66 re_seqno ? "seqno, " : "");
67 g_regex_match(info->regex.format, f_content, G_REGEX_MATCH_NOTEMPTY, &match);
68 while (g_match_info_matches(match)) {
69 /* parse the data */
70 if (!g_match_info_fetch_named_pos(match, "data", &field_start, &field_end)) {
71 fprintf(stderr, "Warning: could not fetch data on would be packet %d, discarding\n", parsed_packets + 1);
72 continue;
74 parse_data(f_content + field_start, f_content + field_end, info->regex.encoding);
76 /* parse the auxiliary information if present */
77 if (re_time &&
78 g_match_info_fetch_named_pos(match, "time", &field_start, &field_end)) {
79 parse_time(f_content + field_start, f_content + field_end, info->timestamp_format);
80 } else {
81 /* No time present, so add a fixed delta. */
82 parse_time(NULL, NULL, NULL);
85 if (re_dir &&
86 g_match_info_fetch_named_pos(match, "dir", &field_start, &field_end))
87 parse_dir(f_content + field_start, f_content + field_end, info->regex.in_indication, info->regex.out_indication);
89 if (re_seqno &&
90 g_match_info_fetch_named_pos(match, "seqno", &field_start, &field_end))
91 parse_seqno(f_content + field_start, f_content + field_end);
93 if (ws_log_get_level() == LOG_LEVEL_NOISY) {
94 g_match_info_fetch_pos(match, 0, &field_start, &field_end);
95 ws_noisy("Packet %d at %x to %x: %.*s\n", parsed_packets + 1,
96 field_start, field_end,
97 field_end - field_start, f_content + field_start);
99 flush_packet();
102 /* prepare next packet */
103 ++parsed_packets;
104 g_match_info_next(match, &gerror);
105 if (gerror && gerror->code) {
106 status = -1;
107 g_error_free(gerror);
108 break;
111 ws_debug("processed %d packets", parsed_packets);
112 g_match_info_unref(match);
113 g_mapped_file_unref(file);
114 return status * parsed_packets;