name change
[metze/wireshark/wip.git] / plugins / mate / mate_util.h
1 /* mate_util.h
2 *
3 * Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
4 *
5 * $Id$
6 *
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26
27 #ifndef __AVP_H_
28 #define __AVP_H_
29 #include "epan/proto.h"
30 #include <sys/types.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35
36 /* #define _AVP_DEBUGGING */
37
38
39 /******* dbg_print *********/
40 #define DEBUG_BUFFER_SIZE 4096
41 extern void dbg_print(const gint* which, gint how, FILE* where, const gchar* fmt, ... );
42
43
44 /******* single copy strings *********/
45 typedef struct _scs_collection SCS_collection;
46
47 #define SCS_SMALL_SIZE 16
48 #define SCS_MEDIUM_SIZE 256
49 #define SCS_LARGE_SIZE 4096
50 #define SCS_HUGE_SIZE 65536
51
52 #define SCS_SMALL_CHUNK_SIZE 4096
53 #define SCS_MEDIUM_CHUNK_SIZE 1024
54 #define SCS_LARGE_CHUNK_SIZE 256
55 #define SCS_HUGE_CHUNK_SIZE 128
56
57 extern void destroy_scs_collection(SCS_collection* c);
58 extern SCS_collection* scs_init(void);
59 extern gchar* scs_subscribe(SCS_collection* collection, const gchar* s);
60 extern void scs_unsubscribe(SCS_collection* collection, gchar* s);
61 extern gchar* scs_subscribe_printf(SCS_collection* collection, gchar* fmt, ...);
62
63 /******* AVPs & Co. *********/
64 #define AVP_CHUNK_SIZE 4096
65
66 /* these are the defined oreators of avps */
67 #define AVP_OP_EQUAL            '='
68 #define AVP_OP_NOTEQUAL         '!'
69 #define AVP_OP_STARTS           '^'
70 #define AVP_OP_ENDS                     '$'
71 #define AVP_OP_CONTAINS         '~'
72 #define AVP_OP_LOWER            '<'
73 #define AVP_OP_HIGHER           '>'
74 #define AVP_OP_EXISTS           '?'
75 #define AVP_OP_ONEOFF           '|'
76 #define AVP_OP_TRANSF           '&'
77
78
79 /* an avp is an object made of a name a value and an operator */
80 typedef struct _avp {
81         gchar* n;
82         gchar* v;
83         gchar o;
84 } AVP;
85
86 /* avp nodes are used in avp lists */
87 typedef struct _avp_node {
88         AVP* avp;
89         struct _avp_node* next;
90         struct _avp_node* prev;
91 } AVPN;
92
93 /* an avp list is a sorted set of avps */
94 typedef struct _avp_list {
95         gchar* name;
96         guint32 len;
97         AVPN null;
98 } AVPL;
99
100
101
102 /* an avpl transformation operation */
103 typedef enum _avpl_match_mode {
104         AVPL_NO_MATCH,
105         AVPL_STRICT,
106         AVPL_LOOSE,
107         AVPL_EVERY
108 } avpl_match_mode;
109
110 typedef enum _avpl_replace_mode {
111         AVPL_NO_REPLACE,
112         AVPL_INSERT,
113         AVPL_REPLACE
114 } avpl_replace_mode;
115
116 typedef struct _avpl_transf AVPL_Transf;
117
118 struct _avpl_transf {
119         gchar* name;
120
121         AVPL* match;
122         AVPL* replace;
123
124         avpl_match_mode match_mode;
125         avpl_replace_mode replace_mode;
126
127         GHashTable* map;
128         AVPL_Transf* next;
129 };
130
131 /* loalnodes are used in LoALs */
132 typedef struct _loal_node {
133         AVPL* avpl;
134         struct _loal_node *next;
135         struct _loal_node *prev;
136 } LoALnode;
137
138
139 /* a loal is a list of avp lists */
140 typedef struct _loal {
141         gchar* name;
142         guint len;
143         LoALnode null;
144 } LoAL;
145
146
147 /* avp library (re)initialization */
148 extern void avp_init(void);
149
150 /* If enabled set's up the debug facilities for the avp library */
151 #ifdef _AVP_DEBUGGING
152 extern void setup_avp_debug(FILE* fp, int* general, int* avp, int* avp_op, int* avpl, int* avpl_op);
153 #endif /* _AVP_DEBUGGING */
154
155 /*
156  * avp constructors
157  */
158
159 /* creates a new avp */
160 extern AVP* new_avp(const gchar* name, const gchar* value, gchar op);
161
162 /* creates a copy od an avp */
163 extern AVP* avp_copy(AVP* from);
164
165 /* creates an avp from a field_info record */
166 extern AVP* new_avp_from_finfo(const gchar* name, field_info* finfo);
167
168 /*
169  * avp destructor
170  */
171 extern void delete_avp(AVP* avp);
172
173 /*
174  * avp methods
175  */
176 /* returns a newly allocated string containing a representation of the avp */
177 #define avp_to_str(avp) (g_strdup_printf("%s%c%s",avp->n,avp->o,avp->v))
178
179 /* returns the src avp if the src avp matches(*) the op avp or NULL if it doesn't */
180 extern AVP* match_avp(AVP* src, AVP* op);
181
182
183 /*
184  * avplist constructors
185  */
186
187 /* creates an empty avp list */
188 extern AVPL* new_avpl(const gchar* name);
189
190
191 /* creates a copy of an avp list */
192 extern AVPL* new_avpl_from_avpl(const gchar* name, AVPL* avpl, gboolean copy_avps);
193
194 /* creates an avp list containing any avps in src matching any avps in op
195    it will eventually create an empty list in none match */
196 extern AVPL* new_avpl_loose_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps);
197
198 /* creates an avp list containing any avps in src matching every avp in op
199   it will not create a list if there is not a match for every attribute in op */
200 extern AVPL* new_avpl_every_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps);
201
202 /* creates an avp list containing every avp in src matching every avp in op
203    it will not create a list unless every avp in op is matched only once to avery avp in op */
204 extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps);
205
206 /* uses mode to call one of the former matches. NO_MATCH = merge(merge(copy(src),op)) */
207 extern AVPL* new_avpl_from_match(avpl_match_mode mode, const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps);
208
209
210
211 /*
212  * avplist destructor
213  */
214 extern void delete_avpl(AVPL* avpl, gboolean avps_too);
215
216 /*
217  * functions on avpls
218  */
219
220 /* it will insert an avp to an avpl */
221 extern gboolean insert_avp(AVPL* avpl, AVP* avp);
222
223 /* renames an avpl */
224 extern void rename_avpl(AVPL* avpl, gchar* name);
225
226 /* it will add all the avps in src which don't match(*) any attribute in dest */
227 extern void merge_avpl(AVPL* dest, AVPL* src, gboolean copy);
228
229 /* it will return the first avp in an avpl whose name matches the given name.
230   will return NULL if there is not anyone matching */
231 extern AVP* get_avp_by_name(AVPL* avpl, gchar* name, void** cookie);
232
233 /* it will get the next avp from an avpl, using cookie to keep state */
234 extern AVP* get_next_avp(AVPL* avpl, void** cookie);
235
236 /* it will extract the first avp from an avp list */
237 extern AVP* extract_first_avp(AVPL* avpl);
238
239 /* it will extract the last avp from an avp list */
240 extern AVP* extract_last_avp(AVPL* avpl);
241
242 /* it will extract the first avp in an avpl whose name matches the given name.
243    it will not extract any and  return NULL if there is not anyone matching */
244 extern AVP* extract_avp_by_name(AVPL* avpl, gchar* name);
245
246 /* returns a newly allocated string containing a representation of the avp list */
247 extern gchar* avpl_to_str(AVPL* avpl);
248 extern gchar* avpl_to_dotstr(AVPL*);
249
250 /* deletes an avp list  and eventually it's contents */
251 extern void delete_avpl(AVPL* avpl, gboolean avps_too);
252
253 /*
254  *  AVPL transformations
255  */
256 extern void delete_avpl_transform(AVPL_Transf* it);
257 extern void avpl_transform(AVPL* src, AVPL_Transf* op);
258
259
260 /*
261  * Lists of AVP lists
262  */
263
264 /* creates an empty list of avp lists */
265 extern LoAL* new_loal(const gchar* name);
266
267 /* given a file loads all the avpls contained in it
268    every line is formatted as it is the output of avplist_to_string */
269 extern LoAL* loal_from_file(gchar* filename);
270
271 /* inserts an avplist into a LoAL */
272 extern void loal_append(LoAL* loal, AVPL* avpl);
273
274 /* extracts the first avp list from the loal */
275 extern AVPL* extract_first_avpl(LoAL* loal);
276
277 /* extracts the last avp list from the loal */
278 extern AVPL* extract_last_avpl(LoAL* loal);
279
280 /* it will get the next avp list from a LoAL, using cookie to keep state */
281 extern AVPL* get_next_avpl(LoAL* loal,void** cookie);
282
283 /* deletes a loal and eventually it's contents */
284 extern void delete_loal(LoAL* loal, gboolean avpls_too, gboolean avps_too);
285
286
287 #endif