Set the svn:eol-style property on all text files to "native", so that
[obnox/wireshark/wip.git] / epan / ftypes / ftypes.h
1 /* ftypes.h
2  * Definitions for field types
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 2001 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25
26 #ifndef FTYPES_H
27 #define FTYPES_H
28
29 #include <glib.h>
30 #include "../slab.h"
31
32 /* field types */
33 enum ftenum {
34         FT_NONE,        /* used for text labels with no value */
35         FT_PROTOCOL,
36         FT_BOOLEAN,     /* TRUE and FALSE come from <glib.h> */
37         FT_UINT8,
38         FT_UINT16,
39         FT_UINT24,      /* really a UINT32, but displayed as 3 hex-digits if FD_HEX*/
40         FT_UINT32,
41         FT_UINT64,
42         FT_INT8,
43         FT_INT16,
44         FT_INT24,       /* same as for UINT24 */
45         FT_INT32,
46         FT_INT64,
47         FT_FLOAT,
48         FT_DOUBLE,
49         FT_ABSOLUTE_TIME,
50         FT_RELATIVE_TIME,
51         FT_STRING,
52         FT_STRINGZ,     /* for use with proto_tree_add_item() */
53         FT_UINT_STRING, /* for use with proto_tree_add_item() */
54         /*FT_UCS2_LE, */    /* Unicode, 2 byte, Little Endian     */
55         FT_ETHER,
56         FT_BYTES,
57         FT_UINT_BYTES,
58         FT_IPv4,
59         FT_IPv6,
60         FT_IPXNET,
61         FT_FRAMENUM,    /* a UINT32, but if selected lets you go to frame with that numbe */
62         FT_PCRE,                /* a compiled Perl-Compatible Regular Expression object */
63         FT_NUM_TYPES /* last item number plus one */
64 };
65
66 #define IS_FT_INT(ft)    ((ft)==FT_INT8||(ft)==FT_INT16||(ft)==FT_INT24||(ft)==FT_INT32)
67 #define IS_FT_UINT(ft)   ((ft)==FT_UINT8||(ft)==FT_UINT16||(ft)==FT_UINT24||(ft)==FT_UINT32||(ft)==FT_FRAMENUM)
68 #define IS_FT_TIME(ft)   ((ft)==FT_ABSOLUTE_TIME||(ft)==FT_RELATIVE_TIME)
69 #define IS_FT_STRING(ft) ((ft)==FT_STRING||(ft)==FT_STRINGZ)
70
71 typedef enum ftenum ftenum_t;
72 typedef struct _ftype_t ftype_t;
73
74 /* String representation types. */
75 enum ftrepr {
76     FTREPR_DISPLAY,
77     FTREPR_DFILTER
78 };
79
80 typedef enum ftrepr ftrepr_t;
81
82 #ifdef HAVE_LIBPCRE
83 typedef struct _pcre_tuple_t pcre_tuple_t;
84 #endif /* HAVE_LIBPCRE */
85
86 /* Initialize the ftypes subsytem. Called once. */
87 void
88 ftypes_initialize(void);
89
90 /* ---------------- FTYPE ----------------- */
91
92 /* Return a string representing the name of the type */
93 const char*
94 ftype_name(ftenum_t ftype);
95
96 /* Return a string presenting a "pretty" representation of the
97  * name of the type. The pretty name means more to the user than
98  * that "FT_*" name. */
99 const char*
100 ftype_pretty_name(ftenum_t ftype);
101
102 /* Returns length of field in packet, or 0 if not determinable/defined. */
103 int
104 ftype_length(ftenum_t ftype);
105
106 gboolean
107 ftype_can_slice(enum ftenum ftype);
108
109 gboolean
110 ftype_can_eq(enum ftenum ftype);
111
112 gboolean
113 ftype_can_ne(enum ftenum ftype);
114
115 gboolean
116 ftype_can_gt(enum ftenum ftype);
117
118 gboolean
119 ftype_can_ge(enum ftenum ftype);
120
121 gboolean
122 ftype_can_lt(enum ftenum ftype);
123
124 gboolean
125 ftype_can_le(enum ftenum ftype);
126
127 gboolean
128 ftype_can_bitwise_and(enum ftenum ftype);
129
130 gboolean
131 ftype_can_contains(enum ftenum ftype);
132
133 gboolean
134 ftype_can_matches(enum ftenum ftype);
135
136 /* ---------------- FVALUE ----------------- */
137
138 #include <epan/ipv4.h>
139
140 #include <epan/tvbuff.h>
141 #include <epan/nstime.h>
142 #include <epan/dfilter/drange.h>
143
144 typedef struct _fvalue_t {
145         ftype_t *ftype;
146         union {
147                 /* Put a few basic types in here */
148                 gpointer        pointer;
149                 guint32         integer;
150                 gdouble         floating;
151                 gchar           *string;
152                 guchar          *ustring;
153                 GByteArray      *bytes;
154                 GString         *gstring;
155                 ipv4_addr       ipv4;
156                 nstime_t        time;
157                 tvbuff_t        *tvb;
158 #ifdef HAVE_LIBPCRE
159                 pcre_tuple_t    *re;
160 #endif /* HAVE_LIBPCRE */
161         } value;
162
163         /* The following is provided for private use
164          * by the fvalue. */
165         gboolean        fvalue_gboolean1;
166
167 } fvalue_t;
168
169 typedef void (*FvalueNewFunc)(fvalue_t*);
170 typedef void (*FvalueFreeFunc)(fvalue_t*);
171 typedef void (*LogFunc)(char*,...);
172
173 typedef gboolean (*FvalueFromUnparsed)(fvalue_t*, char*, gboolean, LogFunc);
174 typedef gboolean (*FvalueFromString)(fvalue_t*, char*, LogFunc);
175 typedef void (*FvalueToStringRepr)(fvalue_t*, ftrepr_t, char*);
176 typedef int (*FvalueStringReprLen)(fvalue_t*, ftrepr_t);
177
178 typedef void (*FvalueSetFunc)(fvalue_t*, gpointer, gboolean);
179 typedef void (*FvalueSetIntegerFunc)(fvalue_t*, guint32);
180 typedef void (*FvalueSetFloatingFunc)(fvalue_t*, gdouble);
181
182 typedef gpointer (*FvalueGetFunc)(fvalue_t*);
183 typedef guint32 (*FvalueGetIntegerFunc)(fvalue_t*);
184 typedef double (*FvalueGetFloatingFunc)(fvalue_t*);
185
186 typedef gboolean (*FvalueCmp)(fvalue_t*, fvalue_t*);
187
188 typedef guint (*FvalueLen)(fvalue_t*);
189 typedef void (*FvalueSlice)(fvalue_t*, GByteArray *, guint offset, guint length);
190
191 struct _ftype_t {
192         const char              *name;
193         const char              *pretty_name;
194         int                     wire_size;
195         FvalueNewFunc           new_value;
196         FvalueFreeFunc          free_value;
197         FvalueFromUnparsed      val_from_unparsed;
198         FvalueFromString        val_from_string;
199         FvalueToStringRepr      val_to_string_repr;
200         FvalueStringReprLen     len_string_repr;
201
202         /* could be union */
203         FvalueSetFunc           set_value;
204         FvalueSetIntegerFunc    set_value_integer;
205         FvalueSetFloatingFunc   set_value_floating;
206
207         /* could be union */
208         FvalueGetFunc           get_value;
209         FvalueGetIntegerFunc    get_value_integer;
210         FvalueGetFloatingFunc   get_value_floating;
211
212         FvalueCmp               cmp_eq;
213         FvalueCmp               cmp_ne;
214         FvalueCmp               cmp_gt;
215         FvalueCmp               cmp_ge;
216         FvalueCmp               cmp_lt;
217         FvalueCmp               cmp_le;
218         FvalueCmp               cmp_bitwise_and;
219         FvalueCmp               cmp_contains;
220         FvalueCmp               cmp_matches;
221
222         FvalueLen               len;
223         FvalueSlice             slice;
224 };
225
226
227 fvalue_t*
228 fvalue_new(ftenum_t ftype);
229
230 void
231 fvalue_init(fvalue_t *fv, ftenum_t ftype);
232
233
234 /* Define type needed for the fvalue_t free list. */
235 SLAB_ITEM_TYPE_DEFINE(fvalue_t)
236
237 /* Free all memory used by an fvalue_t. With MSVC and a 
238  * libethereal.dll, we need a special declaration.
239  */
240 ETH_VAR_IMPORT SLAB_FREE_LIST_DECLARE(fvalue_t)
241
242
243 #define FVALUE_CLEANUP(fv)                                      \
244         {                                                       \
245                 register FvalueFreeFunc free_value;             \
246                 free_value = (fv)->ftype->free_value;   \
247                 if (free_value) {                               \
248                         free_value((fv));                       \
249                 }                                               \
250         }
251
252 #define FVALUE_FREE(fv)                                         \
253         {                                                       \
254                 FVALUE_CLEANUP(fv)                              \
255                 SLAB_FREE(fv, fvalue_t);                        \
256         }
257
258
259 fvalue_t*
260 fvalue_from_unparsed(ftenum_t ftype, char *s, gboolean allow_partial_value, LogFunc logfunc);
261
262 fvalue_t*
263 fvalue_from_string(ftenum_t ftype, char *s, LogFunc logfunc);
264
265 /* Returns the length of the string required to hold the
266  * string representation of the the field value.
267  * The length DOES NOT include the terminating NUL. */
268 int
269 fvalue_string_repr_len(fvalue_t *fv, ftrepr_t rtype);
270
271 /* Creates the string representation of the field value.
272  * If given non-NULL 'buf', the string is written at the memory
273  * location pointed to by 'buf'. If 'buf' is NULL, new memory
274  * is malloc'ed and the string representation is written there.
275  * The pointer to the beginning of the string representation is
276  * returned. If 'buf' was NULL, this points to the newly-allocated
277  * memory. if 'buf' was non-NULL, then the return value will be
278  * 'buf'. */
279 char *
280 fvalue_to_string_repr(fvalue_t *fv, ftrepr_t rtype, char *buf);
281
282 const char*
283 fvalue_type_name(fvalue_t *fv);
284
285 void
286 fvalue_set(fvalue_t *fv, gpointer value, gboolean already_copied);
287
288 void
289 fvalue_set_integer(fvalue_t *fv, guint32 value);
290
291 void
292 fvalue_set_floating(fvalue_t *fv, gdouble value);
293
294 gpointer
295 fvalue_get(fvalue_t *fv);
296
297 guint32
298 fvalue_get_integer(fvalue_t *fv);
299
300 double
301 fvalue_get_floating(fvalue_t *fv);
302
303 gboolean
304 fvalue_eq(fvalue_t *a, fvalue_t *b);
305
306 gboolean
307 fvalue_ne(fvalue_t *a, fvalue_t *b);
308
309 gboolean
310 fvalue_gt(fvalue_t *a, fvalue_t *b);
311
312 gboolean
313 fvalue_ge(fvalue_t *a, fvalue_t *b);
314
315 gboolean
316 fvalue_lt(fvalue_t *a, fvalue_t *b);
317
318 gboolean
319 fvalue_le(fvalue_t *a, fvalue_t *b);
320
321 gboolean
322 fvalue_bitwise_and(fvalue_t *a, fvalue_t *b);
323
324 gboolean
325 fvalue_contains(fvalue_t *a, fvalue_t *b);
326
327 gboolean
328 fvalue_matches(fvalue_t *a, fvalue_t *b);
329
330 guint
331 fvalue_length(fvalue_t *fv);
332
333 fvalue_t*
334 fvalue_slice(fvalue_t *fv, drange *drange);
335
336 #endif /* ftypes.h */