dcerpc SPLIT auth_session_key
[wireshark-sm.git] / ui / text_import_regex.c
blobe29732bc26394730ea5a0c4c1aebd50e9b936db1
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);
39 // Regex result dissecting
40 bool re_time, re_dir, re_seqno;
41 GMatchInfo* match;
42 int field_start;
43 int field_end;
44 { /* analyze regex */
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);
52 return -1;
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)) {
61 /* parse the data */
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 */
66 if (re_time &&
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);
69 } else {
70 /* No time present, so add a fixed delta. */
71 parse_time(NULL, NULL, NULL);
74 if (re_dir &&
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);
78 if (re_seqno &&
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);
88 flush_packet();
89 ++parsed_packets;
90 } else {
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) {
96 status = -1;
97 g_error_free(gerror);
98 break;
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;