fcfd996e97c9e7417c24416ddf1f60c20ab1e59d
[obnox/wireshark/wip.git] / epan / ftypes / ftype-integer.c
1 /*
2  * $Id: ftype-integer.c,v 1.6 2001/07/13 00:55:56 guy Exp $
3  *
4  * Ethereal - Network traffic analyzer
5  * By Gerald Combs <gerald@zing.org>
6  * Copyright 2001 Gerald Combs
7  *
8  * 
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28
29 #ifdef NEED_SNPRINTF_H
30 #include "snprintf.h"
31 #endif
32
33 #include <stdlib.h>
34 #include <errno.h>
35 #include "ftypes-int.h"
36 #include "resolv.h"
37
38
39 static void
40 int_fvalue_new(fvalue_t *fv)
41 {
42         fv->value.integer = 0;
43 }
44
45 static void
46 set_integer(fvalue_t *fv, guint32 value)
47 {
48         fv->value.integer = value;
49 }
50
51 static guint32
52 get_integer(fvalue_t *fv)
53 {
54         return fv->value.integer;
55 }
56
57 static gboolean
58 val_from_string(fvalue_t *fv, char *s, LogFunc log)
59 {
60         char    *endptr;
61
62         fv->value.integer = strtoul(s, &endptr, 0);
63
64         if (endptr == s || *endptr != '\0') {
65                 /* This isn't a valid number. */
66                 if (log != NULL)
67                         log("\"%s\" is not a valid number.", s);
68                 return FALSE;
69         }
70         if (errno == ERANGE) {
71                 if (log != NULL) {
72                         if (fv->value.integer == ULONG_MAX) {
73                                 log("\"%s\" causes an integer overflow.", s);
74                         }
75                         else {
76                                 log("\"%s\" is not an integer.", s);
77                         }
78                 }
79                 return FALSE;
80         }
81
82         return TRUE;
83 }
84
85 static gboolean
86 ipxnet_from_string(fvalue_t *fv, char *s, LogFunc log)
87 {
88         guint32         val;
89         gboolean        known;
90
91         /*
92          * Don't log a message if this fails; we'll try looking it
93          * up as an IPX network name if it does, and if that fails,
94          * we'll log a message.
95          */
96         if (val_from_string(fv, s, NULL)) {
97                 return TRUE;
98         }
99
100         val = get_ipxnet_addr(s, &known);
101         if (known) {
102                 fv->value.integer = val;
103                 return TRUE;
104         }
105
106         log("\"%s\" is not a valid IPX network name or address.", s);
107         return FALSE;
108 }
109
110 static gboolean
111 cmp_eq(fvalue_t *a, fvalue_t *b)
112 {
113         return a->value.integer == b->value.integer;
114 }
115
116 static gboolean
117 cmp_ne(fvalue_t *a, fvalue_t *b)
118 {
119         return a->value.integer != b->value.integer;
120 }
121
122 static gboolean
123 u_cmp_gt(fvalue_t *a, fvalue_t *b)
124 {
125         return (int)a->value.integer > (int)b->value.integer;
126 }
127
128 static gboolean
129 u_cmp_ge(fvalue_t *a, fvalue_t *b)
130 {
131         return (int)a->value.integer >= (int)b->value.integer;
132 }
133
134 static gboolean
135 u_cmp_lt(fvalue_t *a, fvalue_t *b)
136 {
137         return (int)a->value.integer < (int)b->value.integer;
138 }
139
140 static gboolean
141 u_cmp_le(fvalue_t *a, fvalue_t *b)
142 {
143         return (int)a->value.integer <= (int)b->value.integer;
144 }
145
146 static gboolean
147 s_cmp_gt(fvalue_t *a, fvalue_t *b)
148 {
149         return a->value.integer > b->value.integer;
150 }
151
152 static gboolean
153 s_cmp_ge(fvalue_t *a, fvalue_t *b)
154 {
155         return a->value.integer >= b->value.integer;
156 }
157
158 static gboolean
159 s_cmp_lt(fvalue_t *a, fvalue_t *b)
160 {
161         return a->value.integer < b->value.integer;
162 }
163
164 static gboolean
165 s_cmp_le(fvalue_t *a, fvalue_t *b)
166 {
167         return a->value.integer <= b->value.integer;
168 }
169
170 /* BOOLEAN-specific */
171
172 static void
173 boolean_fvalue_new(fvalue_t *fv)
174 {
175         fv->value.integer = TRUE;
176 }
177
178 /* Checks for equality with zero or non-zero */
179 static gboolean
180 bool_eq(fvalue_t *a, fvalue_t *b)
181 {
182         if (a->value.integer) {
183                 if (b->value.integer) {
184                         return TRUE;
185                 }
186                 else {
187                         return FALSE;
188                 }
189         }
190         else {
191                 if (b->value.integer) {
192                         return FALSE;
193                 }
194                 else {
195                         return TRUE;
196                 }
197         }
198 }
199
200 /* Checks for inequality with zero or non-zero */
201 static gboolean
202 bool_ne(fvalue_t *a, fvalue_t *b)
203 {
204         return (!bool_eq(a,b));
205 }
206
207
208
209 void
210 ftype_register_integers(void)
211 {
212
213         static ftype_t uint8_type = {
214                 "FT_UINT8",
215                 "unsigned, 1 byte",
216                 1,
217                 int_fvalue_new,
218                 NULL,
219                 val_from_string,
220
221                 NULL,
222                 set_integer,
223                 NULL,
224
225                 NULL,
226                 get_integer,
227                 NULL,
228
229                 cmp_eq,
230                 cmp_ne,
231                 u_cmp_gt,
232                 u_cmp_ge,
233                 u_cmp_lt,
234                 u_cmp_le,
235
236                 NULL,
237                 NULL,
238         };
239         static ftype_t uint16_type = {
240                 "FT_UINT16",
241                 "unsigned, 2 bytes",
242                 2,
243                 int_fvalue_new,
244                 NULL,
245                 val_from_string,
246
247                 NULL,
248                 set_integer,
249                 NULL,
250
251                 NULL,
252                 get_integer,
253                 NULL,
254
255                 cmp_eq,
256                 cmp_ne,
257                 u_cmp_gt,
258                 u_cmp_ge,
259                 u_cmp_lt,
260                 u_cmp_le,
261
262                 NULL,
263                 NULL,
264         };
265         static ftype_t uint24_type = {
266                 "FT_UINT24",
267                 "unsigned, 3 bytes",
268                 3,
269                 int_fvalue_new,
270                 NULL,
271                 val_from_string,
272
273                 NULL,
274                 set_integer,
275                 NULL,
276
277                 NULL,
278                 get_integer,
279                 NULL,
280
281                 cmp_eq,
282                 cmp_ne,
283                 u_cmp_gt,
284                 u_cmp_ge,
285                 u_cmp_lt,
286                 u_cmp_le,
287
288                 NULL,
289                 NULL,
290         };
291         static ftype_t uint32_type = {
292                 "FT_UINT32",
293                 "unsigned, 4 bytes",
294                 4,
295                 int_fvalue_new,
296                 NULL,
297                 val_from_string,
298
299                 NULL,
300                 set_integer,
301                 NULL,
302
303                 NULL,
304                 get_integer,
305                 NULL,
306
307                 cmp_eq,
308                 cmp_ne,
309                 u_cmp_gt,
310                 u_cmp_ge,
311                 u_cmp_lt,
312                 u_cmp_le,
313
314                 NULL,
315                 NULL,
316         };
317         static ftype_t int8_type = {
318                 "FT_INT8",
319                 "signed, 1 byte",
320                 1,
321                 int_fvalue_new,
322                 NULL,
323                 val_from_string,
324
325                 NULL,
326                 set_integer,
327                 NULL,
328
329                 NULL,
330                 get_integer,
331                 NULL,
332
333                 cmp_eq,
334                 cmp_ne,
335                 s_cmp_gt,
336                 s_cmp_ge,
337                 s_cmp_lt,
338                 s_cmp_le,
339
340                 NULL,
341                 NULL,
342         };
343         static ftype_t int16_type = {
344                 "FT_INT16",
345                 "signed, 2 bytes",
346                 2,
347                 int_fvalue_new,
348                 NULL,
349                 val_from_string,
350
351                 NULL,
352                 set_integer,
353                 NULL,
354
355                 NULL,
356                 get_integer,
357                 NULL,
358
359                 cmp_eq,
360                 cmp_ne,
361                 s_cmp_gt,
362                 s_cmp_ge,
363                 s_cmp_lt,
364                 s_cmp_le,
365
366                 NULL,
367                 NULL,
368         };
369         static ftype_t int24_type = {
370                 "FT_INT24",
371                 "signed, 3 bytes",
372                 3,
373                 int_fvalue_new,
374                 NULL,
375                 val_from_string,
376
377                 NULL,
378                 set_integer,
379                 NULL,
380
381                 NULL,
382                 get_integer,
383                 NULL,
384
385                 cmp_eq,
386                 cmp_ne,
387                 s_cmp_gt,
388                 s_cmp_ge,
389                 s_cmp_lt,
390                 s_cmp_le,
391
392                 NULL,
393                 NULL,
394         };
395         static ftype_t int32_type = {
396                 "FT_INT32",
397                 "signed, 4 bytes",
398                 4,
399                 int_fvalue_new,
400                 NULL,
401                 val_from_string,
402
403                 NULL,
404                 set_integer,
405                 NULL,
406
407                 NULL,
408                 get_integer,
409                 NULL,
410
411                 cmp_eq,
412                 cmp_ne,
413                 s_cmp_gt,
414                 s_cmp_ge,
415                 s_cmp_lt,
416                 s_cmp_le,
417
418                 NULL,
419                 NULL,
420         };
421         static ftype_t boolean_type = {
422                 "FT_BOOLEAN",
423                 "Boolean",
424                 0,
425                 boolean_fvalue_new,
426                 NULL,
427                 val_from_string,
428
429                 NULL,
430                 set_integer,
431                 NULL,
432
433                 NULL,
434                 get_integer,
435                 NULL,
436
437                 bool_eq,
438                 bool_ne,
439                 NULL,
440                 NULL,
441                 NULL,
442                 NULL,
443
444                 NULL,
445                 NULL,
446         };
447
448         static ftype_t ipxnet_type = {
449                 "FT_IPXNET",
450                 "IPX network number",
451                 4,
452                 int_fvalue_new,
453                 NULL,
454                 ipxnet_from_string,
455
456                 NULL,
457                 set_integer,
458                 NULL,
459
460                 NULL,
461                 get_integer,
462                 NULL,
463
464                 cmp_eq,
465                 cmp_ne,
466                 u_cmp_gt,
467                 u_cmp_ge,
468                 u_cmp_lt,
469                 u_cmp_le,
470
471                 NULL,
472                 NULL,
473         };
474
475
476         ftype_register(FT_UINT8, &uint8_type);
477         ftype_register(FT_UINT16, &uint16_type);
478         ftype_register(FT_UINT24, &uint24_type);
479         ftype_register(FT_UINT32, &uint32_type);
480         ftype_register(FT_INT8, &int8_type);
481         ftype_register(FT_INT16, &int16_type);
482         ftype_register(FT_INT24, &int24_type);
483         ftype_register(FT_INT32, &int32_type);
484         ftype_register(FT_BOOLEAN, &boolean_type);
485         ftype_register(FT_IPXNET, &ipxnet_type);
486 }