Use GSlice API instead of GMemChunk API for some memory allocation.
[obnox/wireshark/wip.git] / plugins / mate / mate_util.h
1 /* mate_util.h
2 *
3 * Copyright 2004, Luis E. Garcia Ontanon <luis@ontanon.org>
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 extern gchar* scs_subscribe(SCS_collection* collection, const gchar* s);
53 extern void scs_unsubscribe(SCS_collection* collection, gchar* s);
54 extern gchar* scs_subscribe_printf(SCS_collection* collection, gchar* fmt, ...);
55
56 /******* AVPs & Co. *********/
57
58 /* these are the defined oreators of avps */
59 #define AVP_OP_EQUAL            '='
60 #define AVP_OP_NOTEQUAL         '!'
61 #define AVP_OP_STARTS           '^'
62 #define AVP_OP_ENDS             '$'
63 #define AVP_OP_CONTAINS         '~'
64 #define AVP_OP_LOWER            '<'
65 #define AVP_OP_HIGHER           '>'
66 #define AVP_OP_EXISTS           '?'
67 #define AVP_OP_ONEOFF           '|'
68 #define AVP_OP_TRANSF           '&'
69
70
71 /* an avp is an object made of a name a value and an operator */
72 typedef struct _avp {
73         gchar* n;
74         gchar* v;
75         gchar o;
76 } AVP;
77
78 /* avp nodes are used in avp lists */
79 typedef struct _avp_node {
80         AVP* avp;
81         struct _avp_node* next;
82         struct _avp_node* prev;
83 } AVPN;
84
85 /* an avp list is a sorted set of avps */
86 typedef struct _avp_list {
87         gchar* name;
88         guint32 len;
89         AVPN null;
90 } AVPL;
91
92
93
94 /* an avpl transformation operation */
95 typedef enum _avpl_match_mode {
96         AVPL_NO_MATCH,
97         AVPL_STRICT,
98         AVPL_LOOSE,
99         AVPL_EVERY
100 } avpl_match_mode;
101
102 typedef enum _avpl_replace_mode {
103         AVPL_NO_REPLACE,
104         AVPL_INSERT,
105         AVPL_REPLACE
106 } avpl_replace_mode;
107
108 typedef struct _avpl_transf AVPL_Transf;
109
110 struct _avpl_transf {
111         gchar* name;
112
113         AVPL* match;
114         AVPL* replace;
115
116         avpl_match_mode match_mode;
117         avpl_replace_mode replace_mode;
118
119         GHashTable* map;
120         AVPL_Transf* next;
121 };
122
123 /* loalnodes are used in LoALs */
124 typedef struct _loal_node {
125         AVPL* avpl;
126         struct _loal_node *next;
127         struct _loal_node *prev;
128 } LoALnode;
129
130
131 /* a loal is a list of avp lists */
132 typedef struct _loal {
133         gchar* name;
134         guint len;
135         LoALnode null;
136 } LoAL;
137
138
139 /* avp library (re)initialization */
140 extern void avp_init(void);
141
142 /* If enabled set's up the debug facilities for the avp library */
143 #ifdef _AVP_DEBUGGING
144 extern void setup_avp_debug(FILE* fp, int* general, int* avp, int* avp_op, int* avpl, int* avpl_op);
145 #endif /* _AVP_DEBUGGING */
146
147 /*
148  * avp constructors
149  */
150
151 /* creates a new avp */
152 extern AVP* new_avp(const gchar* name, const gchar* value, gchar op);
153
154 /* creates a copy od an avp */
155 extern AVP* avp_copy(AVP* from);
156
157 /* creates an avp from a field_info record */
158 extern AVP* new_avp_from_finfo(const gchar* name, field_info* finfo);
159
160 /*
161  * avp destructor
162  */
163 extern void delete_avp(AVP* avp);
164
165 /*
166  * avp methods
167  */
168 /* returns a newly allocated string containing a representation of the avp */
169 #define avp_to_str(avp) (g_strdup_printf("%s%c%s",avp->n,avp->o,avp->v))
170
171 /* returns the src avp if the src avp matches(*) the op avp or NULL if it doesn't */
172 extern AVP* match_avp(AVP* src, AVP* op);
173
174
175 /*
176  * avplist constructors
177  */
178
179 /* creates an empty avp list */
180 extern AVPL* new_avpl(const gchar* name);
181
182
183 /* creates a copy of an avp list */
184 extern AVPL* new_avpl_from_avpl(const gchar* name, AVPL* avpl, gboolean copy_avps);
185
186 /* creates an avp list containing any avps in src matching any avps in op
187    it will eventually create an empty list in none match */
188 extern AVPL* new_avpl_loose_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps);
189
190 /* creates an avp list containing any avps in src matching every avp in op
191   it will not create a list if there is not a match for every attribute in op */
192 extern AVPL* new_avpl_every_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps);
193
194 /* creates an avp list containing every avp in src matching every avp in op
195    it will not create a list unless every avp in op is matched only once to avery avp in op */
196 extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps);
197
198 /* uses mode to call one of the former matches. NO_MATCH = merge(merge(copy(src),op)) */
199 extern AVPL* new_avpl_from_match(avpl_match_mode mode, const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps);
200
201
202 /*
203  * functions on avpls
204  */
205
206 /* it will insert an avp to an avpl */
207 extern gboolean insert_avp(AVPL* avpl, AVP* avp);
208
209 /* renames an avpl */
210 extern void rename_avpl(AVPL* avpl, gchar* name);
211
212 /* it will add all the avps in src which don't match(*) any attribute in dest */
213 extern void merge_avpl(AVPL* dest, AVPL* src, gboolean copy);
214
215 /* it will return the first avp in an avpl whose name matches the given name.
216   will return NULL if there is not anyone matching */
217 extern AVP* get_avp_by_name(AVPL* avpl, gchar* name, void** cookie);
218
219 /* it will get the next avp from an avpl, using cookie to keep state */
220 extern AVP* get_next_avp(AVPL* avpl, void** cookie);
221
222 /* it will extract the first avp from an avp list */
223 extern AVP* extract_first_avp(AVPL* avpl);
224
225 /* it will extract the last avp from an avp list */
226 extern AVP* extract_last_avp(AVPL* avpl);
227
228 /* it will extract the first avp in an avpl whose name matches the given name.
229    it will not extract any and  return NULL if there is not anyone matching */
230 extern AVP* extract_avp_by_name(AVPL* avpl, gchar* name);
231
232 /* returns a newly allocated string containing a representation of the avp list */
233 extern gchar* avpl_to_str(AVPL* avpl);
234 extern gchar* avpl_to_dotstr(AVPL*);
235
236 /* deletes an avp list  and eventually it's contents */
237 extern void delete_avpl(AVPL* avpl, gboolean avps_too);
238
239 /*
240  *  AVPL transformations
241  */
242 extern void delete_avpl_transform(AVPL_Transf* it);
243 extern void avpl_transform(AVPL* src, AVPL_Transf* op);
244
245
246 /*
247  * Lists of AVP lists
248  */
249
250 /* creates an empty list of avp lists */
251 extern LoAL* new_loal(const gchar* name);
252
253 /* given a file loads all the avpls contained in it
254    every line is formatted as it is the output of avplist_to_string */
255 extern LoAL* loal_from_file(gchar* filename);
256
257 /* inserts an avplist into a LoAL */
258 extern void loal_append(LoAL* loal, AVPL* avpl);
259
260 /* extracts the first avp list from the loal */
261 extern AVPL* extract_first_avpl(LoAL* loal);
262
263 /* extracts the last avp list from the loal */
264 extern AVPL* extract_last_avpl(LoAL* loal);
265
266 /* it will get the next avp list from a LoAL, using cookie to keep state */
267 extern AVPL* get_next_avpl(LoAL* loal,void** cookie);
268
269 /* deletes a loal and eventually it's contents */
270 extern void delete_loal(LoAL* loal, gboolean avpls_too, gboolean avps_too);
271
272
273 #endif