Qt: select a better default Decode As table
[metze/wireshark/wip.git] / file_packet_provider.c
1 /* file_packet_provider_data.c
2  * Routines for a packet_provider_data for packets from a file.
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include <glib.h>
12
13 #include "cfile.h"
14
15 static int
16 frame_cmp(gconstpointer a, gconstpointer b, gpointer user_data _U_)
17 {
18   const frame_data *fdata1 = (const frame_data *) a;
19   const frame_data *fdata2 = (const frame_data *) b;
20
21   return (fdata1->num < fdata2->num) ? -1 :
22     (fdata1->num > fdata2->num) ? 1 :
23     0;
24 }
25
26 const char *
27 cap_file_provider_get_interface_name(struct packet_provider_data *prov, guint32 interface_id)
28 {
29   wtapng_iface_descriptions_t *idb_info;
30   wtap_block_t wtapng_if_descr = NULL;
31   char* interface_name;
32
33   idb_info = wtap_file_get_idb_info(prov->wth);
34
35   if (interface_id < idb_info->interface_data->len)
36     wtapng_if_descr = g_array_index(idb_info->interface_data, wtap_block_t, interface_id);
37
38   g_free(idb_info);
39
40   if (wtapng_if_descr) {
41     if (wtap_block_get_string_option_value(wtapng_if_descr, OPT_IDB_NAME, &interface_name) == WTAP_OPTTYPE_SUCCESS)
42       return interface_name;
43     if (wtap_block_get_string_option_value(wtapng_if_descr, OPT_IDB_DESCR, &interface_name) == WTAP_OPTTYPE_SUCCESS)
44       return interface_name;
45   }
46   return "unknown";
47 }
48
49 const char *
50 cap_file_provider_get_interface_description(struct packet_provider_data *prov, guint32 interface_id)
51 {
52   wtapng_iface_descriptions_t *idb_info;
53   wtap_block_t wtapng_if_descr = NULL;
54   char* interface_name;
55
56   idb_info = wtap_file_get_idb_info(prov->wth);
57
58   if (interface_id < idb_info->interface_data->len)
59     wtapng_if_descr = g_array_index(idb_info->interface_data, wtap_block_t, interface_id);
60
61   g_free(idb_info);
62
63   if (wtapng_if_descr) {
64     if (wtap_block_get_string_option_value(wtapng_if_descr, OPT_IDB_DESCR, &interface_name) == WTAP_OPTTYPE_SUCCESS)
65       return interface_name;
66   }
67   return NULL;
68 }
69
70 const char *
71 cap_file_provider_get_user_comment(struct packet_provider_data *prov, const frame_data *fd)
72 {
73   if (prov->frames_user_comments)
74      return (const char *)g_tree_lookup(prov->frames_user_comments, fd);
75
76   /* g_warning? */
77   return NULL;
78 }
79
80 void
81 cap_file_provider_set_user_comment(struct packet_provider_data *prov, frame_data *fd, const char *new_comment)
82 {
83   if (!prov->frames_user_comments)
84     prov->frames_user_comments = g_tree_new_full(frame_cmp, NULL, NULL, g_free);
85
86   /* insert new packet comment */
87   g_tree_replace(prov->frames_user_comments, fd, g_strdup(new_comment));
88
89   fd->flags.has_user_comment = TRUE;
90 }