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