regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / epan / ftypes / ftype-integer.c
blobbd79372b29594e8c67ed7f0bdfd43b74e2302d21
1 /*
2 * Wireshark - Network traffic analyzer
3 * By Gerald Combs <gerald@wireshark.org>
4 * Copyright 2001 Gerald Combs
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
9 #include "config.h"
11 #include <stdlib.h>
12 #include "ftypes-int.h"
13 #include <epan/addr_resolv.h>
14 #include <epan/strutil.h>
15 #include <epan/to_str.h>
17 #include <wsutil/safe-math.h>
18 #include <wsutil/array.h>
20 static void
21 int_fvalue_new(fvalue_t *fv)
23 memset(&fv->value, 0, sizeof(fv->value));
26 static void
27 set_uinteger(fvalue_t *fv, uint32_t value)
29 fv->value.uinteger64 = value;
32 static void
33 set_sinteger(fvalue_t *fv, int32_t value)
35 fv->value.sinteger64 = value;
39 static uint32_t
40 get_uinteger(fvalue_t *fv)
42 return (uint32_t)fv->value.uinteger64;
45 static int32_t
46 get_sinteger(fvalue_t *fv)
48 return (int32_t)fv->value.sinteger64;
51 static char *
52 char_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype _U_, int field_display)
54 size_t size = 7 + 1; /* enough for '\OOO' or '\xXX' */
55 char *result = wmem_alloc(scope, size);
56 char *buf = result;
59 * The longest possible strings are "'\OOO'" and "'\xXX'", which
60 * take 7 bytes, including the terminating '\0'.
62 *buf++ = '\'';
63 if (g_ascii_isprint(fv->value.uinteger64)) {
64 /* This perfectly fits into 4 or 5 bytes. */
65 if (fv->value.uinteger64 == '\\' || fv->value.uinteger64 == '\'')
66 *buf++ = '\\';
67 *buf++ = (char)fv->value.uinteger64;
69 else {
70 *buf++ = '\\';
71 switch (fv->value.uinteger64) {
73 case '\0':
74 *buf++ = '0';
75 break;
77 case '\a':
78 *buf++ = 'a';
79 break;
81 case '\b':
82 *buf++ = 'b';
83 break;
85 case '\f':
86 *buf++ = 'f';
87 break;
89 case '\n':
90 *buf++ = 'n';
91 break;
93 case '\r':
94 *buf++ = 'r';
95 break;
97 case '\t':
98 *buf++ = 't';
99 break;
101 case '\v':
102 *buf++ = 'v';
103 break;
105 default:
106 if (field_display == BASE_HEX) {
107 *buf++ = 'x';
108 buf = uint8_to_hex(buf, (uint8_t)fv->value.uinteger64);
110 else {
111 *buf++ = ((fv->value.uinteger64 >> 6) & 0x7) + '0';
112 *buf++ = ((fv->value.uinteger64 >> 3) & 0x7) + '0';
113 *buf++ = ((fv->value.uinteger64 >> 0) & 0x7) + '0';
115 break;
118 *buf++ = '\'';
119 *buf++ = '\0';
120 return result;
123 static enum ft_result
124 uint64_cmp_order(const fvalue_t *a, const fvalue_t *b, int *cmp)
126 uint64_t val_a, val_b;
127 enum ft_result res;
129 res = fvalue_to_uinteger64(a, &val_a);
130 /* As currently used, a should be an unsigned integer and this succeed. */
131 if (res != FT_OK)
132 return res;
134 res = fvalue_to_uinteger64(b, &val_b);
135 switch (res) {
136 case FT_OK:
137 if (val_a == val_b)
138 *cmp = 0;
139 else
140 *cmp = val_a < val_b ? -1 : 1;
141 break;
142 case FT_UNDERFLOW:
143 *cmp = 1;
144 break;
145 case FT_OVERFLOW:
146 *cmp = -1;
147 break;
148 default:
149 return res;
152 return FT_OK;
155 static enum ft_result
156 sint64_cmp_order(const fvalue_t *a, const fvalue_t *b, int *cmp)
158 int64_t val_a, val_b;
159 enum ft_result res;
161 res = fvalue_to_sinteger64(a, &val_a);
162 /* As currently used, a should be a signed integer and this succeed. */
163 if (res != FT_OK)
164 return res;
166 res = fvalue_to_sinteger64(b, &val_b);
167 switch (res) {
168 case FT_OK:
169 if (val_a == val_b)
170 *cmp = 0;
171 else
172 *cmp = val_a < val_b ? -1 : 1;
173 break;
174 case FT_UNDERFLOW:
175 *cmp = 1;
176 break;
177 case FT_OVERFLOW:
178 *cmp = -1;
179 break;
180 default:
181 return res;
184 return FT_OK;
187 static void
188 int64_fvalue_new(fvalue_t *fv)
190 fv->value.sinteger64 = 0;
193 static void
194 set_uinteger64(fvalue_t *fv, uint64_t value)
196 fv->value.uinteger64 = value;
199 static void
200 set_sinteger64(fvalue_t *fv, int64_t value)
202 fv->value.sinteger64 = value;
205 static uint64_t
206 get_uinteger64(fvalue_t *fv)
208 return fv->value.uinteger64;
211 static int64_t
212 get_sinteger64(fvalue_t *fv)
214 return fv->value.sinteger64;
217 static bool
218 _uint64_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg,
219 uint64_t max)
221 if (value > max) {
222 if (err_msg != NULL)
223 *err_msg = ws_strdup_printf("\"%s\" too big for this field, maximum %" PRIu64".", s, max);
224 return false;
227 fv->value.uinteger64 = value;
228 return true;
231 static bool
232 uint64_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
234 return _uint64_from_uinteger64(fv, s, value, err_msg, UINT64_MAX);
237 static bool
238 uint56_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
240 return _uint64_from_uinteger64(fv, s, value, err_msg, UINT64_C(0xFFFFFFFFFFFFFF));
243 static bool
244 uint48_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
246 return _uint64_from_uinteger64(fv, s, value, err_msg, UINT64_C(0xFFFFFFFFFFFF));
249 static bool
250 uint40_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
252 return _uint64_from_uinteger64(fv, s, value, err_msg, UINT64_C(0xFFFFFFFFFF));
255 static bool
256 uint32_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
258 return _uint64_from_uinteger64(fv, s, value, err_msg, UINT32_MAX);
261 static bool
262 uint24_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
264 return _uint64_from_uinteger64(fv, s, value, err_msg, 0xFFFFFF);
267 static bool
268 uint16_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
270 return _uint64_from_uinteger64(fv, s, value, err_msg, UINT16_MAX);
273 static bool
274 uint8_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
276 return _uint64_from_uinteger64(fv, s, value, err_msg, UINT8_MAX);
279 static bool
280 _uint64_from_sinteger64(fvalue_t *fv, const char *s, int64_t _value, char **err_msg,
281 uint64_t max)
283 if (_value < 0) {
284 if (err_msg != NULL)
285 *err_msg = ws_strdup_printf("Unsigned numbers cannot be negative.");
286 return false;
289 uint64_t value = (uint64_t)_value;
291 if (value > max) {
292 if (err_msg != NULL)
293 *err_msg = ws_strdup_printf("\"%s\" too big for this field, maximum %" PRIu64".", s, max);
294 return false;
297 fv->value.uinteger64 = value;
298 return true;
301 static bool
302 uint64_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
304 return _uint64_from_sinteger64(fv, s, value, err_msg, UINT64_MAX);
307 static bool
308 uint56_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
310 return _uint64_from_sinteger64(fv, s, value, err_msg, UINT64_C(0xFFFFFFFFFFFFFF));
313 static bool
314 uint48_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
316 return _uint64_from_sinteger64(fv, s, value, err_msg, UINT64_C(0xFFFFFFFFFFFF));
319 static bool
320 uint40_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
322 return _uint64_from_sinteger64(fv, s, value, err_msg, UINT64_C(0xFFFFFFFFFF));
325 static bool
326 uint32_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
328 return _uint64_from_sinteger64(fv, s, value, err_msg, UINT32_MAX);
331 static bool
332 uint24_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
334 return _uint64_from_sinteger64(fv, s, value, err_msg, 0xFFFFFF);
337 static bool
338 uint16_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
340 return _uint64_from_sinteger64(fv, s, value, err_msg, UINT16_MAX);
343 static bool
344 uint8_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
346 return _uint64_from_sinteger64(fv, s, value, err_msg, UINT8_MAX);
349 static bool
350 uint64_from_charconst(fvalue_t *fv, unsigned long num, char **err_msg _U_)
352 fv->value.uinteger64 = (uint64_t)num;
353 return true;
356 static bool
357 _sint64_from_uinteger64(fvalue_t *fv, const char *s, uint64_t uvalue, char **err_msg,
358 int64_t max, int64_t min)
360 if (uvalue > INT64_MAX) {
361 if (err_msg != NULL)
362 *err_msg = ws_strdup_printf("\"%s\" too big for this field, maximum %"PRId64".",
363 s, max);
364 return false;
367 int64_t value = (int64_t)uvalue;
369 if (value > max) {
370 if (err_msg != NULL)
371 *err_msg = ws_strdup_printf("\"%s\" too big for this field, maximum %" PRId64".", s, max);
372 return false;
374 else if (value < min) {
375 if (err_msg != NULL)
376 *err_msg = ws_strdup_printf("\"%s\" too small for this field, minimum %" PRId64 ".", s, min);
377 return false;
380 fv->value.sinteger64 = value;
381 return true;
384 static bool
385 sint64_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
387 return _sint64_from_uinteger64(fv, s, value, err_msg, INT64_MAX, INT64_MIN);
390 static bool
391 sint56_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
393 return _sint64_from_uinteger64(fv, s, value, err_msg, INT64_C(0x7FFFFFFFFFFFFF), INT64_C(-0x80000000000000));
396 static bool
397 sint48_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
399 return _sint64_from_uinteger64(fv, s, value, err_msg, INT64_C(0x7FFFFFFFFFFF), INT64_C(-0x800000000000));
402 static bool
403 sint40_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
405 return _sint64_from_uinteger64(fv, s, value, err_msg, INT64_C(0x7FFFFFFFFF), INT64_C(-0x8000000000));
408 static bool
409 sint32_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
411 return _sint64_from_uinteger64(fv, s, value, err_msg, INT32_MAX, INT32_MIN);
414 static bool
415 sint24_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
417 return _sint64_from_uinteger64(fv, s, value, err_msg, 0x7FFFFF, -0x800000);
420 static bool
421 sint16_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
423 return _sint64_from_uinteger64(fv, s, value, err_msg, INT16_MAX, INT16_MIN);
426 static bool
427 sint8_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
429 return _sint64_from_uinteger64(fv, s, value, err_msg, INT8_MAX, INT8_MIN);
432 static bool
433 _sint64_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg,
434 int64_t max, int64_t min)
436 if (value > max) {
437 if (err_msg != NULL)
438 *err_msg = ws_strdup_printf("\"%s\" too big for this field, maximum %"PRId64".",
439 s, max);
440 return false;
442 else if (value < min) {
443 if (err_msg != NULL)
444 *err_msg = ws_strdup_printf("\"%s\" too small for this field, minimum %"PRId64".",
445 s, min);
446 return false;
449 fv->value.sinteger64 = value;
450 return true;
453 static bool
454 sint64_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
456 return _sint64_from_sinteger64(fv, s, value, err_msg, INT64_MAX, INT64_MIN);
459 static bool
460 sint56_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
462 return _sint64_from_sinteger64(fv, s, value, err_msg, INT64_C(0x7FFFFFFFFFFFFF), INT64_C(-0x80000000000000));
465 static bool
466 sint48_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
468 return _sint64_from_sinteger64(fv, s, value, err_msg, INT64_C(0x7FFFFFFFFFFF), INT64_C(-0x800000000000));
471 static bool
472 sint40_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
474 return _sint64_from_sinteger64(fv, s, value, err_msg, INT64_C(0x7FFFFFFFFF), INT64_C(-0x8000000000));
477 static bool
478 sint32_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
480 return _sint64_from_sinteger64(fv, s, value, err_msg, INT32_MAX, INT32_MIN);
483 static bool
484 sint24_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
486 return _sint64_from_sinteger64(fv, s, value, err_msg, 0x7FFFFF, -0x800000);
489 static bool
490 sint16_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
492 return _sint64_from_sinteger64(fv, s, value, err_msg, INT16_MAX, INT16_MIN);
495 static bool
496 sint8_from_sinteger64(fvalue_t *fv, const char *s, int64_t value, char **err_msg)
498 return _sint64_from_sinteger64(fv, s, value, err_msg, INT8_MAX, INT8_MIN);
501 static bool
502 sint64_from_charconst(fvalue_t *fv, unsigned long num, char **err_msg _U_)
504 fv->value.sinteger64 = (int64_t)num;
505 return true;
508 static char *
509 sinteger64_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_)
511 uint64_t val;
513 size_t size = 20 + 1; /* enough for -2^63-1, in decimal */
514 char *result = wmem_alloc(scope, size);
515 char *buf = result;
517 if (fv->value.sinteger64 < 0) {
518 *buf++ = '-';
519 val = -fv->value.sinteger64;
521 else {
522 val = fv->value.sinteger64;
524 uint64_to_str_buf(val, buf, size);
525 return result;
528 static char *
529 uinteger64_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype _U_, int field_display)
531 size_t size = 20 + 1; /* enough for 2^64-1, in decimal or 0xXXXXXXXXXXXXXXXX */
532 char *result = wmem_alloc(scope, size);
533 char *buf = result;
535 if (FIELD_DISPLAY(field_display) == BASE_HEX || FIELD_DISPLAY(field_display) == BASE_HEX_DEC) {
536 /* This format perfectly fits into 19 bytes. */
537 *buf++ = '0';
538 *buf++ = 'x';
540 switch (fv->ftype->ftype) {
542 case FT_UINT8:
543 buf = uint8_to_hex(buf, (uint8_t)fv->value.uinteger64);
544 break;
546 case FT_UINT16:
547 buf = word_to_hex(buf, (uint16_t)fv->value.uinteger64);
548 break;
550 case FT_UINT24:
551 buf = uint8_to_hex(buf, (uint8_t)((fv->value.uinteger64 & 0x00ff0000) >> 16));
552 buf = word_to_hex(buf, (uint16_t)(fv->value.uinteger64 & 0x0000ffff));
553 break;
555 case FT_UINT32:
556 buf = dword_to_hex(buf, (uint32_t)fv->value.uinteger64);
557 break;
559 default:
560 buf = qword_to_hex(buf, fv->value.uinteger64);
563 *buf++ = '\0';
565 else {
566 uint64_to_str_buf(fv->value.uinteger64, buf, size);
568 return result;
571 static enum ft_result
572 uint64_bitwise_and(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr _U_)
574 dst->value.uinteger64 = a->value.uinteger64 & b->value.uinteger64;
575 return FT_OK;
578 static unsigned
579 uint64_hash(const fvalue_t *fv)
581 int64_t val = fv->value.uinteger64;
582 return g_int64_hash(&val);
585 static bool
586 uint64_is_zero(const fvalue_t *fv)
588 return fv->value.uinteger64 == 0;
591 static bool
592 uint64_is_negative(const fvalue_t *fv _U_)
594 return false;
597 static enum ft_result
598 uint64_unary_minus(fvalue_t *dst, const fvalue_t *src, char **err_ptr)
600 /* Unsigned64 integers are promoted to signed 64 bits. */
601 if (src->value.uinteger64 > INT64_MAX) {
602 if (err_ptr)
603 *err_ptr = ws_strdup_printf("%"PRIu64" overflows int64",
604 src->value.uinteger64);
605 return FT_ERROR;
607 FTYPE_LOOKUP(FT_INT64, dst->ftype);
608 dst->value.sinteger64 = -(int64_t)src->value.uinteger64;
609 return FT_OK;
612 static enum ft_result
613 sint64_bitwise_and(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr _U_)
615 dst->value.sinteger64 = a->value.sinteger64 & b->value.sinteger64;
616 return FT_OK;
619 static unsigned
620 sint64_hash(const fvalue_t *fv)
622 int64_t val = fv->value.sinteger64;
623 return g_int64_hash(&val);
626 static bool
627 sint64_is_zero(const fvalue_t *fv)
629 return fv->value.sinteger64 == 0;
632 static bool
633 sint64_is_negative(const fvalue_t *fv)
635 return fv->value.sinteger64 < 0;
638 static enum ft_result
639 sint64_unary_minus(fvalue_t * dst, const fvalue_t *src, char **err_ptr _U_)
641 dst->value.sinteger64 = -src->value.sinteger64;
642 return FT_OK;
645 static enum ft_result
646 sint64_add(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr)
648 if (!psnip_safe_int64_add(&dst->value.sinteger64, a->value.sinteger64, b->value.sinteger64)) {
649 *err_ptr = ws_strdup_printf("sint64_add: overflow");
650 return FT_ERROR;
652 return FT_OK;
655 static enum ft_result
656 _sint64_subtract(int64_t *sint_dst, int64_t sint_a, int64_t sint_b, char **err_ptr)
658 if (!psnip_safe_int64_sub(sint_dst, sint_a, sint_b)) {
659 *err_ptr = ws_strdup_printf("sint64_subtract: overflow");
660 return FT_ERROR;
662 return FT_OK;
665 static enum ft_result
666 sint64_subtract(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr)
668 return _sint64_subtract(&dst->value.sinteger64, a->value.sinteger64, b->value.sinteger64, err_ptr);
671 static enum ft_result
672 sint64_multiply(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr)
674 if (!psnip_safe_int64_mul(&dst->value.sinteger64, a->value.sinteger64, b->value.sinteger64)) {
675 *err_ptr = ws_strdup_printf("sint64_multiply: overflow");
676 return FT_ERROR;
678 return FT_OK;
681 static enum ft_result
682 sint64_divide(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr)
684 if (b->value.sinteger64 == 0) {
685 *err_ptr = ws_strdup_printf("sint64_divide: division by zero");
686 return FT_ERROR;
689 if (!psnip_safe_int64_div(&dst->value.sinteger64, a->value.sinteger64, b->value.sinteger64)) {
690 *err_ptr = ws_strdup_printf("sint64_divide: overflow");
691 return FT_ERROR;
693 return FT_OK;
696 static enum ft_result
697 sint64_modulo(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr)
699 if (b->value.sinteger64 == 0) {
700 *err_ptr = ws_strdup_printf("sint64_modulo: division by zero");
701 return FT_ERROR;
704 if (!psnip_safe_int64_mod(&dst->value.sinteger64, a->value.sinteger64, b->value.sinteger64)) {
705 *err_ptr = ws_strdup_printf("sint64_modulo: overflow");
706 return FT_ERROR;
708 return FT_OK;
711 static enum ft_result
712 uint64_add(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr)
714 if (!psnip_safe_uint64_add(&dst->value.uinteger64, a->value.uinteger64, b->value.uinteger64)) {
715 *err_ptr = ws_strdup_printf("uint64_add: overflow");
716 return FT_ERROR;
718 return FT_OK;
721 static enum ft_result
722 uint64_subtract(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr)
724 if (b->value.uinteger64 > a->value.uinteger64) {
725 /* Uses signed arithmetic. */
726 if (a->value.uinteger64 > INT64_MAX ||
727 b->value.uinteger64 > INT64_MAX) {
728 *err_ptr = ws_strdup_printf("uint64_subtract: signed overflow");
729 return FT_ERROR;
731 FTYPE_LOOKUP(FT_INT64, dst->ftype);
732 return _sint64_subtract(&dst->value.sinteger64, (int64_t)a->value.uinteger64, (int64_t)b->value.uinteger64, err_ptr);
735 if (!psnip_safe_uint64_sub(&dst->value.uinteger64, a->value.uinteger64, b->value.uinteger64)) {
736 *err_ptr = ws_strdup_printf("uint64_subtract: overflow");
737 return FT_ERROR;
739 return FT_OK;
742 static enum ft_result
743 uint64_multiply(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr)
745 if (!psnip_safe_uint64_mul(&dst->value.uinteger64, a->value.uinteger64, b->value.uinteger64)) {
746 *err_ptr = ws_strdup_printf("uint64_multiply: overflow");
747 return FT_ERROR;
749 return FT_OK;
752 static enum ft_result
753 uint64_divide(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr)
755 if (b->value.uinteger64 == 0) {
756 *err_ptr = ws_strdup_printf("uint64_divide: division by zero");
757 return FT_ERROR;
760 if (!psnip_safe_uint64_div(&dst->value.uinteger64, a->value.uinteger64, b->value.uinteger64)) {
761 *err_ptr = ws_strdup_printf("uint64_divide: overflow");
762 return FT_ERROR;
764 return FT_OK;
767 static enum ft_result
768 uint64_modulo(fvalue_t *dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr)
770 if (b->value.uinteger64 == 0) {
771 *err_ptr = ws_strdup_printf("uint64_modulo: division by zero");
772 return FT_ERROR;
775 if (!psnip_safe_uint64_mod(&dst->value.uinteger64, a->value.uinteger64, b->value.uinteger64)) {
776 *err_ptr = ws_strdup_printf("uint64_modulo: overflow");
777 return FT_ERROR;
779 return FT_OK;
782 static enum ft_result uint64_val_to_uinteger64(const fvalue_t *src, uint64_t *dst)
784 *dst = src->value.uinteger64;
785 return FT_OK;
788 static enum ft_result uint64_val_to_sinteger64(const fvalue_t *src, int64_t *dst)
790 if (src->value.uinteger64 > INT64_MAX)
791 return FT_OVERFLOW;
793 *dst = (int64_t)src->value.uinteger64;
794 return FT_OK;
797 static enum ft_result sint64_val_to_uinteger64(const fvalue_t *src, uint64_t *dst)
799 if (src->value.sinteger64 < 0)
800 return FT_UNDERFLOW;
802 *dst = (uint64_t)src->value.sinteger64;
803 return FT_OK;
806 static enum ft_result sint64_val_to_sinteger64(const fvalue_t *src, int64_t *dst)
808 *dst = src->value.sinteger64;
809 return FT_OK;
812 static enum ft_result uint64_val_to_double(const fvalue_t *src, double *dst)
814 *dst = (double)src->value.uinteger64;
815 return FT_OK;
818 static enum ft_result sint64_val_to_double(const fvalue_t *src, double *dst)
820 *dst = (double)src->value.sinteger64;
821 return FT_OK;
824 /* BOOLEAN-specific */
826 static bool
827 boolean_from_literal(fvalue_t *fv, const char *s, bool allow_partial_value _U_, char **err_msg)
829 if (g_ascii_strcasecmp(s, "true") == 0) {
830 fv->value.uinteger64 = 1;
831 return true;
833 if (g_ascii_strcasecmp(s, "false") == 0) {
834 fv->value.uinteger64 = 0;
835 return true;
838 if (err_msg)
839 *err_msg = ws_strdup_printf("\"%s\" is not a valid boolean", s);
840 return false;
843 static bool
844 boolean_from_uinteger64(fvalue_t *fv, const char *s _U_, uint64_t value, char **err_msg _U_)
846 fv->value.uinteger64 = (value != 0);
847 return true;
850 static bool
851 boolean_from_sinteger64(fvalue_t *fv, const char *s _U_, int64_t value, char **err_msg _U_)
853 fv->value.uinteger64 = (value != 0);
854 return true;
857 static bool
858 boolean_from_string(fvalue_t *fv, const char *s, size_t len, char **err_msg _U_)
860 if (g_ascii_strncasecmp(s, "true", len) == 0) {
861 fv->value.uinteger64 = 1;
862 return true;
864 if (g_ascii_strncasecmp(s, "false", len) == 0) {
865 fv->value.uinteger64 = 0;
866 return true;
869 if (err_msg)
870 *err_msg = ws_strdup_printf("expected \"True\" or \"False\", not \"%s\"", s);
871 return false;
874 static char *
875 boolean_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype, int field_display _U_)
877 bool val = fv->value.uinteger64;
878 const char *str = NULL;
880 switch (rtype) {
881 case FTREPR_DFILTER:
882 case FTREPR_DISPLAY:
883 str = val ? "True" : "False";
884 break;
885 case FTREPR_JSON:
886 case FTREPR_RAW:
887 str = val ? "1" : "0";
888 break;
891 return wmem_strdup(scope, str);
894 /* False is less than True (arbitrary):
895 * A B cmp(A, B)
896 * T T 0
897 * F F 0
898 * F T -1
899 * T F 1
901 static enum ft_result
902 boolean_cmp_order(const fvalue_t *a, const fvalue_t *b, int *cmp)
904 uint64_t val_a, val_b;
905 enum ft_result res;
907 val_a = a->value.uinteger64;
908 res = fvalue_to_uinteger64(b, &val_b);
909 if (res != FT_OK)
910 return res;
912 if (val_a) {
913 if (val_b) {
914 *cmp = 0;
916 else {
917 *cmp = 1;
920 else if (val_b) {
921 *cmp = -1;
923 else {
924 *cmp = 0;
927 return FT_OK;
930 static unsigned
931 boolean_hash(const fvalue_t *fv)
933 int val;
935 if (fv->value.uinteger64)
936 val = 1;
937 else
938 val = 0;
939 return g_int_hash(&val);
942 static bool
943 ipxnet_from_uinteger64(fvalue_t *fv, const char *s, uint64_t value, char **err_msg)
946 * Don't request an error message if bytes_from_literal fails;
947 * if it does, we'll report an error specific to this address
948 * type.
950 if (uint32_from_uinteger64(fv, s, value, NULL)) {
951 return true;
954 /* XXX - Try resolving as an IPX host name and parse that? */
956 if (err_msg != NULL)
957 *err_msg = ws_strdup_printf("\"%s\" is not a valid IPX network address.", s);
958 return false;
961 static char *
962 ipxnet_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype, int field_display _U_)
964 return uinteger64_to_repr(scope, fv, rtype, BASE_HEX);
967 /* EUI64-specific */
968 static bool
969 eui64_from_literal(fvalue_t *fv, const char *s, bool allow_partial_value _U_, char **err_msg)
971 GByteArray *bytes;
972 bool res;
973 union {
974 uint64_t value;
975 uint8_t bytes[8];
976 } eui64;
978 bytes = g_byte_array_new();
979 res = hex_str_to_bytes(s, bytes, true);
980 if (!res || bytes->len != 8) {
981 if (err_msg != NULL)
982 *err_msg = ws_strdup_printf("\"%s\" is not a valid EUI-64 address.", s);
983 g_byte_array_free(bytes, true);
984 return false;
987 memcpy(eui64.bytes, bytes->data, 8);
988 g_byte_array_free(bytes, true);
989 fv->value.uinteger64 = GUINT64_FROM_BE(eui64.value);
990 return true;
993 static bool
994 eui64_from_uinteger64(fvalue_t *fv, const char *s _U_, uint64_t value, char **err_msg _U_)
996 fv->value.uinteger64 = value;
997 return true;
1000 static char *
1001 eui64_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_)
1003 union {
1004 uint64_t value;
1005 uint8_t bytes[8];
1006 } eui64;
1008 /* Copy and convert the address from host to network byte order. */
1009 eui64.value = GUINT64_TO_BE(fv->value.uinteger64);
1011 return wmem_strdup_printf(scope, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
1012 eui64.bytes[0], eui64.bytes[1], eui64.bytes[2], eui64.bytes[3],
1013 eui64.bytes[4], eui64.bytes[5], eui64.bytes[6], eui64.bytes[7]);
1016 void
1017 ftype_register_integers(void)
1019 static const ftype_t char_type = {
1020 FT_CHAR, /* ftype */
1021 1, /* wire_size */
1022 int_fvalue_new, /* new_value */
1023 NULL, /* copy_value */
1024 NULL, /* free_value */
1025 NULL, /* val_from_literal */
1026 NULL, /* val_from_string */
1027 uint64_from_charconst, /* val_from_charconst */
1028 uint8_from_uinteger64, /* val_from_uinteger64 */
1029 uint8_from_sinteger64, /* val_from_sinteger64 */
1030 NULL, /* val_from_double */
1031 char_to_repr, /* val_to_string_repr */
1033 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1034 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1035 uint64_val_to_double, /* val_to_double */
1037 { .set_value_uinteger = set_uinteger }, /* union set_value */
1038 { .get_value_uinteger = get_uinteger }, /* union get_value */
1040 uint64_cmp_order, /* cmp_order */
1041 NULL, /* cmp_contains */
1042 NULL, /* cmp_matches */
1044 uint64_hash, /* hash */
1045 uint64_is_zero, /* is_zero */
1046 uint64_is_negative, /* is_negative */
1047 NULL, /* len */
1048 NULL, /* slice */
1049 uint64_bitwise_and, /* bitwise_and */
1050 uint64_unary_minus, /* unary_minus */
1051 uint64_add, /* add */
1052 uint64_subtract, /* subtract */
1053 uint64_multiply, /* multiply */
1054 uint64_divide, /* divide */
1055 uint64_modulo, /* modulo */
1057 static const ftype_t uint8_type = {
1058 FT_UINT8, /* ftype */
1059 1, /* wire_size */
1060 int_fvalue_new, /* new_value */
1061 NULL, /* copy_value */
1062 NULL, /* free_value */
1063 NULL, /* val_from_literal */
1064 NULL, /* val_from_string */
1065 uint64_from_charconst, /* val_from_charconst */
1066 uint8_from_uinteger64, /* val_from_uinteger64 */
1067 uint8_from_sinteger64, /* val_from_sinteger64 */
1068 NULL, /* val_from_double */
1069 uinteger64_to_repr, /* val_to_string_repr */
1071 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1072 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1073 uint64_val_to_double, /* val_to_double */
1075 { .set_value_uinteger = set_uinteger }, /* union set_value */
1076 { .get_value_uinteger = get_uinteger }, /* union get_value */
1078 uint64_cmp_order, /* cmp_order */
1079 NULL, /* cmp_contains */
1080 NULL, /* cmp_matches */
1082 uint64_hash, /* hash */
1083 uint64_is_zero, /* is_zero */
1084 uint64_is_negative, /* is_negative */
1085 NULL, /* len */
1086 NULL, /* slice */
1087 uint64_bitwise_and, /* bitwise_and */
1088 uint64_unary_minus, /* unary_minus */
1089 uint64_add, /* add */
1090 uint64_subtract, /* subtract */
1091 uint64_multiply, /* multiply */
1092 uint64_divide, /* divide */
1093 uint64_modulo, /* modulo */
1095 static const ftype_t uint16_type = {
1096 FT_UINT16, /* ftype */
1097 2, /* wire_size */
1098 int_fvalue_new, /* new_value */
1099 NULL, /* copy_value */
1100 NULL, /* free_value */
1101 NULL, /* val_from_literal */
1102 NULL, /* val_from_string */
1103 uint64_from_charconst, /* val_from_charconst */
1104 uint16_from_uinteger64, /* val_from_uinteger64 */
1105 uint16_from_sinteger64, /* val_from_sinteger64 */
1106 NULL, /* val_from_double */
1107 uinteger64_to_repr, /* val_to_string_repr */
1109 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1110 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1111 uint64_val_to_double, /* val_to_double */
1113 { .set_value_uinteger = set_uinteger }, /* union set_value */
1114 { .get_value_uinteger = get_uinteger }, /* union get_value */
1116 uint64_cmp_order, /* cmp_order */
1117 NULL, /* cmp_contains */
1118 NULL, /* cmp_matches */
1120 uint64_hash, /* hash */
1121 uint64_is_zero, /* is_zero */
1122 uint64_is_negative, /* is_negative */
1123 NULL, /* len */
1124 NULL, /* slice */
1125 uint64_bitwise_and, /* bitwise_and */
1126 uint64_unary_minus, /* unary_minus */
1127 uint64_add, /* add */
1128 uint64_subtract, /* subtract */
1129 uint64_multiply, /* multiply */
1130 uint64_divide, /* divide */
1131 uint64_modulo, /* modulo */
1133 static const ftype_t uint24_type = {
1134 FT_UINT24, /* ftype */
1135 3, /* wire_size */
1136 int_fvalue_new, /* new_value */
1137 NULL, /* copy_value */
1138 NULL, /* free_value */
1139 NULL, /* val_from_literal */
1140 NULL, /* val_from_string */
1141 uint64_from_charconst, /* val_from_charconst */
1142 uint24_from_uinteger64, /* val_from_uinteger64 */
1143 uint24_from_sinteger64, /* val_from_sinteger64 */
1144 NULL, /* val_from_double */
1145 uinteger64_to_repr, /* val_to_string_repr */
1147 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1148 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1149 uint64_val_to_double, /* val_to_double */
1151 { .set_value_uinteger = set_uinteger }, /* union set_value */
1152 { .get_value_uinteger = get_uinteger }, /* union get_value */
1154 uint64_cmp_order, /* cmp_order */
1155 NULL, /* cmp_contains */
1156 NULL, /* cmp_matches */
1158 uint64_hash, /* hash */
1159 uint64_is_zero, /* is_zero */
1160 uint64_is_negative, /* is_negative */
1161 NULL, /* len */
1162 NULL, /* slice */
1163 uint64_bitwise_and, /* bitwise_and */
1164 uint64_unary_minus, /* unary_minus */
1165 uint64_add, /* add */
1166 uint64_subtract, /* subtract */
1167 uint64_multiply, /* multiply */
1168 uint64_divide, /* divide */
1169 uint64_modulo, /* modulo */
1171 static const ftype_t uint32_type = {
1172 FT_UINT32, /* ftype */
1173 4, /* wire_size */
1174 int_fvalue_new, /* new_value */
1175 NULL, /* copy_value */
1176 NULL, /* free_value */
1177 NULL, /* val_from_literal */
1178 NULL, /* val_from_string */
1179 uint64_from_charconst, /* val_from_charconst */
1180 uint32_from_uinteger64, /* val_from_uinteger64 */
1181 uint32_from_sinteger64, /* val_from_sinteger64 */
1182 NULL, /* val_from_double */
1183 uinteger64_to_repr, /* val_to_string_repr */
1185 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1186 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1187 uint64_val_to_double, /* val_to_double */
1189 { .set_value_uinteger = set_uinteger }, /* union set_value */
1190 { .get_value_uinteger = get_uinteger }, /* union get_value */
1192 uint64_cmp_order, /* cmp_order */
1193 NULL, /* cmp_contains */
1194 NULL, /* cmp_matches */
1196 uint64_hash, /* hash */
1197 uint64_is_zero, /* is_zero */
1198 uint64_is_negative, /* is_negative */
1199 NULL, /* len */
1200 NULL, /* slice */
1201 uint64_bitwise_and, /* bitwise_and */
1202 uint64_unary_minus, /* unary_minus */
1203 uint64_add, /* add */
1204 uint64_subtract, /* subtract */
1205 uint64_multiply, /* multiply */
1206 uint64_divide, /* divide */
1207 uint64_modulo, /* modulo */
1209 static const ftype_t uint40_type = {
1210 FT_UINT40, /* ftype */
1211 5, /* wire_size */
1212 int64_fvalue_new, /* new_value */
1213 NULL, /* copy_value */
1214 NULL, /* free_value */
1215 NULL, /* val_from_literal */
1216 NULL, /* val_from_string */
1217 uint64_from_charconst, /* val_from_charconst */
1218 uint40_from_uinteger64, /* val_from_uinteger64 */
1219 uint40_from_sinteger64, /* val_from_sinteger64 */
1220 NULL, /* val_from_double */
1221 uinteger64_to_repr, /* val_to_string_repr */
1223 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1224 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1225 uint64_val_to_double, /* val_to_double */
1227 { .set_value_uinteger64 = set_uinteger64 }, /* union set_value */
1228 { .get_value_uinteger64 = get_uinteger64 }, /* union get_value */
1230 uint64_cmp_order, /* cmp_order */
1231 NULL, /* cmp_contains */
1232 NULL, /* cmp_matches */
1234 uint64_hash, /* hash */
1235 uint64_is_zero, /* is_zero */
1236 uint64_is_negative, /* is_negative */
1237 NULL, /* len */
1238 NULL, /* slice */
1239 uint64_bitwise_and, /* bitwise_and */
1240 uint64_unary_minus, /* unary_minus */
1241 uint64_add, /* add */
1242 uint64_subtract, /* subtract */
1243 uint64_multiply, /* multiply */
1244 uint64_divide, /* divide */
1245 uint64_modulo, /* modulo */
1247 static const ftype_t uint48_type = {
1248 FT_UINT48, /* ftype */
1249 6, /* wire_size */
1250 int64_fvalue_new, /* new_value */
1251 NULL, /* copy_value */
1252 NULL, /* free_value */
1253 NULL, /* val_from_literal */
1254 NULL, /* val_from_string */
1255 uint64_from_charconst, /* val_from_charconst */
1256 uint48_from_uinteger64, /* val_from_uinteger64 */
1257 uint48_from_sinteger64, /* val_from_sinteger64 */
1258 NULL, /* val_from_double */
1259 uinteger64_to_repr, /* val_to_string_repr */
1261 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1262 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1263 uint64_val_to_double, /* val_to_double */
1265 { .set_value_uinteger64 = set_uinteger64 }, /* union set_value */
1266 { .get_value_uinteger64 = get_uinteger64 }, /* union get_value */
1268 uint64_cmp_order, /* cmp_order */
1269 NULL, /* cmp_contains */
1270 NULL, /* cmp_matches */
1272 uint64_hash, /* hash */
1273 uint64_is_zero, /* is_zero */
1274 uint64_is_negative, /* is_negative */
1275 NULL, /* len */
1276 NULL, /* slice */
1277 uint64_bitwise_and, /* bitwise_and */
1278 uint64_unary_minus, /* unary_minus */
1279 uint64_add, /* add */
1280 uint64_subtract, /* subtract */
1281 uint64_multiply, /* multiply */
1282 uint64_divide, /* divide */
1283 uint64_modulo, /* modulo */
1285 static const ftype_t uint56_type = {
1286 FT_UINT56, /* ftype */
1287 7, /* wire_size */
1288 int64_fvalue_new, /* new_value */
1289 NULL, /* copy_value */
1290 NULL, /* free_value */
1291 NULL, /* val_from_literal */
1292 NULL, /* val_from_string */
1293 uint64_from_charconst, /* val_from_charconst */
1294 uint56_from_uinteger64, /* val_from_uinteger64 */
1295 uint56_from_sinteger64, /* val_from_sinteger64 */
1296 NULL, /* val_from_double */
1297 uinteger64_to_repr, /* val_to_string_repr */
1299 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1300 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1301 uint64_val_to_double, /* val_to_double */
1303 { .set_value_uinteger64 = set_uinteger64 }, /* union set_value */
1304 { .get_value_uinteger64 = get_uinteger64 }, /* union get_value */
1306 uint64_cmp_order, /* cmp_order */
1307 NULL, /* cmp_contains */
1308 NULL, /* cmp_matches */
1310 uint64_hash, /* hash */
1311 uint64_is_zero, /* is_zero */
1312 uint64_is_negative, /* is_negative */
1313 NULL, /* len */
1314 NULL, /* slice */
1315 uint64_bitwise_and, /* bitwise_and */
1316 uint64_unary_minus, /* unary_minus */
1317 uint64_add, /* add */
1318 uint64_subtract, /* subtract */
1319 uint64_multiply, /* multiply */
1320 uint64_divide, /* divide */
1321 uint64_modulo, /* modulo */
1323 static const ftype_t uint64_type = {
1324 FT_UINT64, /* ftype */
1325 8, /* wire_size */
1326 int64_fvalue_new, /* new_value */
1327 NULL, /* copy_value */
1328 NULL, /* free_value */
1329 NULL, /* val_from_literal */
1330 NULL, /* val_from_string */
1331 uint64_from_charconst, /* val_from_charconst */
1332 uint64_from_uinteger64, /* val_from_uinteger64 */
1333 uint64_from_sinteger64, /* val_from_sinteger64 */
1334 NULL, /* val_from_double */
1335 uinteger64_to_repr, /* val_to_string_repr */
1337 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1338 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1339 uint64_val_to_double, /* val_to_double */
1341 { .set_value_uinteger64 = set_uinteger64 }, /* union set_value */
1342 { .get_value_uinteger64 = get_uinteger64 }, /* union get_value */
1344 uint64_cmp_order, /* cmp_order */
1345 NULL, /* cmp_contains */
1346 NULL, /* cmp_matches */
1348 uint64_hash, /* hash */
1349 uint64_is_zero, /* is_zero */
1350 uint64_is_negative, /* is_negative */
1351 NULL, /* len */
1352 NULL, /* slice */
1353 uint64_bitwise_and, /* bitwise_and */
1354 uint64_unary_minus, /* unary_minus */
1355 uint64_add, /* add */
1356 uint64_subtract, /* subtract */
1357 uint64_multiply, /* multiply */
1358 uint64_divide, /* divide */
1359 uint64_modulo, /* modulo */
1361 static const ftype_t int8_type = {
1362 FT_INT8, /* ftype */
1363 1, /* wire_size */
1364 int_fvalue_new, /* new_value */
1365 NULL, /* copy_value */
1366 NULL, /* free_value */
1367 NULL, /* val_from_literal */
1368 NULL, /* val_from_string */
1369 sint64_from_charconst, /* val_from_charconst */
1370 sint8_from_uinteger64, /* val_from_uinteger64 */
1371 sint8_from_sinteger64, /* val_from_sinteger64 */
1372 NULL, /* val_from_double */
1373 sinteger64_to_repr, /* val_to_string_repr */
1375 sint64_val_to_uinteger64, /* val_to_uinteger64 */
1376 sint64_val_to_sinteger64, /* val_to_sinteger64 */
1377 sint64_val_to_double, /* val_to_double */
1379 { .set_value_sinteger = set_sinteger }, /* union set_value */
1380 { .get_value_sinteger = get_sinteger }, /* union get_value */
1382 sint64_cmp_order, /* cmp_order */
1383 NULL, /* cmp_contains */
1384 NULL, /* cmp_matches */
1386 sint64_hash, /* hash */
1387 sint64_is_zero, /* is_zero */
1388 sint64_is_negative, /* is_negative */
1389 NULL, /* len */
1390 NULL, /* slice */
1391 sint64_bitwise_and, /* bitwise_and */
1392 sint64_unary_minus, /* unary_minus */
1393 sint64_add, /* add */
1394 sint64_subtract, /* subtract */
1395 sint64_multiply, /* multiply */
1396 sint64_divide, /* divide */
1397 sint64_modulo, /* modulo */
1399 static const ftype_t int16_type = {
1400 FT_INT16, /* ftype */
1401 2, /* wire_size */
1402 int_fvalue_new, /* new_value */
1403 NULL, /* copy_value */
1404 NULL, /* free_value */
1405 NULL, /* val_from_literal */
1406 NULL, /* val_from_string */
1407 sint64_from_charconst, /* val_from_charconst */
1408 sint16_from_uinteger64, /* val_from_uinteger64 */
1409 sint16_from_sinteger64, /* val_from_sinteger64 */
1410 NULL, /* val_from_double */
1411 sinteger64_to_repr, /* val_to_string_repr */
1413 sint64_val_to_uinteger64, /* val_to_uinteger64 */
1414 sint64_val_to_sinteger64, /* val_to_sinteger64 */
1415 sint64_val_to_double, /* val_to_double */
1417 { .set_value_sinteger = set_sinteger }, /* union set_value */
1418 { .get_value_sinteger = get_sinteger }, /* union get_value */
1420 sint64_cmp_order, /* cmp_order */
1421 NULL, /* cmp_contains */
1422 NULL, /* cmp_matches */
1424 sint64_hash, /* hash */
1425 sint64_is_zero, /* is_zero */
1426 sint64_is_negative, /* is_negative */
1427 NULL, /* len */
1428 NULL, /* slice */
1429 sint64_bitwise_and, /* bitwise_and */
1430 sint64_unary_minus, /* unary_minus */
1431 sint64_add, /* add */
1432 sint64_subtract, /* subtract */
1433 sint64_multiply, /* multiply */
1434 sint64_divide, /* divide */
1435 sint64_modulo, /* modulo */
1437 static const ftype_t int24_type = {
1438 FT_INT24, /* ftype */
1439 3, /* wire_size */
1440 int_fvalue_new, /* new_value */
1441 NULL, /* copy_value */
1442 NULL, /* free_value */
1443 NULL, /* val_from_literal */
1444 NULL, /* val_from_string */
1445 sint64_from_charconst, /* val_from_charconst */
1446 sint24_from_uinteger64, /* val_from_uinteger64 */
1447 sint24_from_sinteger64, /* val_from_sinteger64 */
1448 NULL, /* val_from_double */
1449 sinteger64_to_repr, /* val_to_string_repr */
1451 sint64_val_to_uinteger64, /* val_to_uinteger64 */
1452 sint64_val_to_sinteger64, /* val_to_sinteger64 */
1453 sint64_val_to_double, /* val_to_double */
1455 { .set_value_sinteger = set_sinteger }, /* union set_value */
1456 { .get_value_sinteger = get_sinteger }, /* union get_value */
1458 sint64_cmp_order, /* cmp_order */
1459 NULL, /* cmp_contains */
1460 NULL, /* cmp_matches */
1462 sint64_hash, /* hash */
1463 sint64_is_zero, /* is_zero */
1464 sint64_is_negative, /* is_negative */
1465 NULL, /* len */
1466 NULL, /* slice */
1467 sint64_bitwise_and, /* bitwise_and */
1468 sint64_unary_minus, /* unary_minus */
1469 sint64_add, /* add */
1470 sint64_subtract, /* subtract */
1471 sint64_multiply, /* multiply */
1472 sint64_divide, /* divide */
1473 sint64_modulo, /* modulo */
1475 static const ftype_t int32_type = {
1476 FT_INT32, /* ftype */
1477 4, /* wire_size */
1478 int_fvalue_new, /* new_value */
1479 NULL, /* copy_value */
1480 NULL, /* free_value */
1481 NULL, /* val_from_literal */
1482 NULL, /* val_from_string */
1483 sint64_from_charconst, /* val_from_charconst */
1484 sint32_from_uinteger64, /* val_from_uinteger64 */
1485 sint32_from_sinteger64, /* val_from_sinteger64 */
1486 NULL, /* val_from_double */
1487 sinteger64_to_repr, /* val_to_string_repr */
1489 sint64_val_to_uinteger64, /* val_to_uinteger64 */
1490 sint64_val_to_sinteger64, /* val_to_sinteger64 */
1491 sint64_val_to_double, /* val_to_double */
1493 { .set_value_sinteger = set_sinteger }, /* union set_value */
1494 { .get_value_sinteger = get_sinteger }, /* union get_value */
1496 sint64_cmp_order, /* cmp_order */
1497 NULL, /* cmp_contains */
1498 NULL, /* cmp_matches */
1500 sint64_hash, /* hash */
1501 sint64_is_zero, /* is_zero */
1502 sint64_is_negative, /* is_negative */
1503 NULL, /* len */
1504 NULL, /* slice */
1505 sint64_bitwise_and, /* bitwise_and */
1506 sint64_unary_minus, /* unary_minus */
1507 sint64_add, /* add */
1508 sint64_subtract, /* subtract */
1509 sint64_multiply, /* multiply */
1510 sint64_divide, /* divide */
1511 sint64_modulo, /* modulo */
1513 static const ftype_t int40_type = {
1514 FT_INT40, /* ftype */
1515 5, /* wire_size */
1516 int64_fvalue_new, /* new_value */
1517 NULL, /* copy_value */
1518 NULL, /* free_value */
1519 NULL, /* val_from_literal */
1520 NULL, /* val_from_string */
1521 sint64_from_charconst, /* val_from_charconst */
1522 sint40_from_uinteger64, /* val_from_uinteger64 */
1523 sint40_from_sinteger64, /* val_from_sinteger64 */
1524 NULL, /* val_from_double */
1525 sinteger64_to_repr, /* val_to_string_repr */
1527 sint64_val_to_uinteger64, /* val_to_uinteger64 */
1528 sint64_val_to_sinteger64, /* val_to_sinteger64 */
1529 sint64_val_to_double, /* val_to_double */
1531 { .set_value_sinteger64 = set_sinteger64 }, /* union set_value */
1532 { .get_value_sinteger64 = get_sinteger64 }, /* union get_value */
1534 sint64_cmp_order, /* cmp_order */
1535 NULL, /* cmp_contains */
1536 NULL, /* cmp_matches */
1538 sint64_hash, /* hash */
1539 sint64_is_zero, /* is_zero */
1540 sint64_is_negative, /* is_negative */
1541 NULL, /* len */
1542 NULL, /* slice */
1543 sint64_bitwise_and, /* bitwise_and */
1544 sint64_unary_minus, /* unary_minus */
1545 sint64_add, /* add */
1546 sint64_subtract, /* subtract */
1547 sint64_multiply, /* multiply */
1548 sint64_divide, /* divide */
1549 sint64_modulo, /* modulo */
1551 static const ftype_t int48_type = {
1552 FT_INT48, /* ftype */
1553 6, /* wire_size */
1554 int64_fvalue_new, /* new_value */
1555 NULL, /* copy_value */
1556 NULL, /* free_value */
1557 NULL, /* val_from_literal */
1558 NULL, /* val_from_string */
1559 sint64_from_charconst, /* val_from_charconst */
1560 sint48_from_uinteger64, /* val_from_uinteger64 */
1561 sint48_from_sinteger64, /* val_from_sinteger64 */
1562 NULL, /* val_from_double */
1563 sinteger64_to_repr, /* val_to_string_repr */
1565 sint64_val_to_uinteger64, /* val_to_uinteger64 */
1566 sint64_val_to_sinteger64, /* val_to_sinteger64 */
1567 sint64_val_to_double, /* val_to_double */
1569 { .set_value_sinteger64 = set_sinteger64 }, /* union set_value */
1570 { .get_value_sinteger64 = get_sinteger64 }, /* union get_value */
1572 sint64_cmp_order, /* cmp_order */
1573 NULL, /* cmp_contains */
1574 NULL, /* cmp_matches */
1576 sint64_hash, /* hash */
1577 sint64_is_zero, /* is_zero */
1578 sint64_is_negative, /* is_negative */
1579 NULL, /* len */
1580 NULL, /* slice */
1581 sint64_bitwise_and, /* bitwise_and */
1582 sint64_unary_minus, /* unary_minus */
1583 sint64_add, /* add */
1584 sint64_subtract, /* subtract */
1585 sint64_multiply, /* multiply */
1586 sint64_divide, /* divide */
1587 sint64_modulo, /* modulo */
1589 static const ftype_t int56_type = {
1590 FT_INT56, /* ftype */
1591 7, /* wire_size */
1592 int64_fvalue_new, /* new_value */
1593 NULL, /* copy_value */
1594 NULL, /* free_value */
1595 NULL, /* val_from_literal */
1596 NULL, /* val_from_string */
1597 sint64_from_charconst, /* val_from_charconst */
1598 sint56_from_uinteger64, /* val_from_uinteger64 */
1599 sint56_from_sinteger64, /* val_from_sinteger64 */
1600 NULL, /* val_from_double */
1601 sinteger64_to_repr, /* val_to_string_repr */
1603 sint64_val_to_uinteger64, /* val_to_uinteger64 */
1604 sint64_val_to_sinteger64, /* val_to_sinteger64 */
1605 sint64_val_to_double, /* val_to_double */
1607 { .set_value_sinteger64 = set_sinteger64 }, /* union set_value */
1608 { .get_value_sinteger64 = get_sinteger64 }, /* union get_value */
1610 sint64_cmp_order, /* cmp_order */
1611 NULL, /* cmp_contains */
1612 NULL, /* cmp_matches */
1614 sint64_hash, /* hash */
1615 sint64_is_zero, /* is_zero */
1616 sint64_is_negative, /* is_negative */
1617 NULL, /* len */
1618 NULL, /* slice */
1619 sint64_bitwise_and, /* bitwise_and */
1620 sint64_unary_minus, /* unary_minus */
1621 sint64_add, /* add */
1622 sint64_subtract, /* subtract */
1623 sint64_multiply, /* multiply */
1624 sint64_divide, /* divide */
1625 sint64_modulo, /* modulo */
1627 static const ftype_t int64_type = {
1628 FT_INT64, /* ftype */
1629 8, /* wire_size */
1630 int64_fvalue_new, /* new_value */
1631 NULL, /* copy_value */
1632 NULL, /* free_value */
1633 NULL, /* val_from_literal */
1634 NULL, /* val_from_string */
1635 sint64_from_charconst, /* val_from_charconst */
1636 sint64_from_uinteger64, /* val_from_uinteger64 */
1637 sint64_from_sinteger64, /* val_from_sinteger64 */
1638 NULL, /* val_from_double */
1639 sinteger64_to_repr, /* val_to_string_repr */
1641 sint64_val_to_uinteger64, /* val_to_uinteger64 */
1642 sint64_val_to_sinteger64, /* val_to_sinteger64 */
1643 sint64_val_to_double, /* val_to_double */
1645 { .set_value_sinteger64 = set_sinteger64 }, /* union set_value */
1646 { .get_value_sinteger64 = get_sinteger64 }, /* union get_value */
1648 sint64_cmp_order, /* cmp_order */
1649 NULL, /* cmp_contains */
1650 NULL, /* cmp_matches */
1652 sint64_hash, /* hash */
1653 sint64_is_zero, /* is_zero */
1654 sint64_is_negative, /* is_negative */
1655 NULL, /* len */
1656 NULL, /* slice */
1657 sint64_bitwise_and, /* bitwise_and */
1658 sint64_unary_minus, /* unary_minus */
1659 sint64_add, /* add */
1660 sint64_subtract, /* subtract */
1661 sint64_multiply, /* multiply */
1662 sint64_divide, /* divide */
1663 sint64_modulo, /* modulo */
1665 static const ftype_t boolean_type = {
1666 FT_BOOLEAN, /* ftype */
1667 0, /* wire_size */
1668 int_fvalue_new, /* new_value */
1669 NULL, /* copy_value */
1670 NULL, /* free_value */
1671 boolean_from_literal, /* val_from_literal */
1672 boolean_from_string, /* val_from_string */
1673 uint64_from_charconst, /* val_from_charconst */
1674 boolean_from_uinteger64, /* val_from_uinteger64 */
1675 boolean_from_sinteger64, /* val_from_sinteger64 */
1676 NULL, /* val_from_double */
1677 boolean_to_repr, /* val_to_string_repr */
1679 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1680 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1681 uint64_val_to_double, /* val_to_double */
1683 { .set_value_uinteger64 = set_uinteger64 }, /* union set_value */
1684 { .get_value_uinteger64 = get_uinteger64 }, /* union get_value */
1686 boolean_cmp_order, /* cmp_order */
1687 NULL, /* cmp_contains */
1688 NULL, /* cmp_matches */
1690 boolean_hash, /* hash */
1691 uint64_is_zero, /* is_zero */
1692 uint64_is_negative, /* is_negative */
1693 NULL, /* len */
1694 NULL, /* slice */
1695 NULL, /* bitwise_and */
1696 NULL, /* unary_minus */
1697 NULL, /* add */
1698 NULL, /* subtract */
1699 NULL, /* multiply */
1700 NULL, /* divide */
1701 NULL, /* modulo */
1704 static const ftype_t ipxnet_type = {
1705 FT_IPXNET, /* ftype */
1706 4, /* wire_size */
1707 int_fvalue_new, /* new_value */
1708 NULL, /* copy_value */
1709 NULL, /* free_value */
1710 NULL, /* val_from_literal */
1711 NULL, /* val_from_string */
1712 NULL, /* val_from_charconst */
1713 ipxnet_from_uinteger64, /* val_from_uinteger64 */
1714 NULL, /* val_from_sinteger64 */
1715 NULL, /* val_from_double */
1716 ipxnet_to_repr, /* val_to_string_repr */
1718 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1719 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1720 uint64_val_to_double, /* val_to_double */
1722 { .set_value_uinteger = set_uinteger }, /* union set_value */
1723 { .get_value_uinteger = get_uinteger }, /* union get_value */
1725 uint64_cmp_order, /* cmp_order */
1726 NULL, /* cmp_contains */
1727 NULL, /* cmp_matches */
1729 uint64_hash, /* hash */
1730 uint64_is_zero, /* is_zero */
1731 uint64_is_negative, /* is_negative */
1732 NULL, /* len */
1733 NULL, /* slice */
1734 uint64_bitwise_and, /* bitwise_and */
1735 uint64_unary_minus, /* unary_minus */
1736 NULL, /* add */
1737 NULL, /* subtract */
1738 NULL, /* multiply */
1739 NULL, /* divide */
1740 NULL, /* modulo */
1743 static const ftype_t framenum_type = {
1744 FT_FRAMENUM, /* ftype */
1745 4, /* wire_size */
1746 int_fvalue_new, /* new_value */
1747 NULL, /* copy_value */
1748 NULL, /* free_value */
1749 NULL, /* val_from_literal */
1750 NULL, /* val_from_string */
1751 uint64_from_charconst, /* val_from_charconst */
1752 uint32_from_uinteger64, /* val_from_uinteger64 */
1753 uint32_from_sinteger64, /* val_from_sinteger64 */
1754 NULL, /* val_from_double */
1755 uinteger64_to_repr, /* val_to_string_repr */
1757 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1758 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1759 uint64_val_to_double, /* val_to_double */
1761 { .set_value_uinteger = set_uinteger }, /* union set_value */
1762 { .get_value_uinteger = get_uinteger }, /* union get_value */
1764 uint64_cmp_order, /* cmp_order */
1765 NULL, /* cmp_contains */
1766 NULL, /* cmp_matches */
1768 uint64_hash, /* hash */
1769 uint64_is_zero, /* is_zero */
1770 uint64_is_negative, /* is_negative */
1771 NULL, /* len */
1772 NULL, /* slice */
1773 uint64_bitwise_and, /* bitwise_and */
1774 uint64_unary_minus, /* unary_minus */
1775 uint64_add, /* add */
1776 uint64_subtract, /* subtract */
1777 uint64_multiply, /* multiply */
1778 uint64_divide, /* divide */
1779 uint64_modulo, /* modulo */
1782 static const ftype_t eui64_type = {
1783 FT_EUI64, /* ftype */
1784 FT_EUI64_LEN, /* wire_size */
1785 int64_fvalue_new, /* new_value */
1786 NULL, /* copy_value */
1787 NULL, /* free_value */
1788 eui64_from_literal, /* val_from_literal */
1789 NULL, /* val_from_string */
1790 NULL, /* val_from_charconst */
1791 eui64_from_uinteger64, /* val_from_uinteger64 */
1792 NULL, /* val_from_sinteger64 */
1793 NULL, /* val_from_double */
1794 eui64_to_repr, /* val_to_string_repr */
1796 uint64_val_to_uinteger64, /* val_to_uinteger64 */
1797 uint64_val_to_sinteger64, /* val_to_sinteger64 */
1798 uint64_val_to_double, /* val_to_double */
1800 { .set_value_uinteger64 = set_uinteger64 }, /* union set_value */
1801 { .get_value_uinteger64 = get_uinteger64 }, /* union get_value */
1803 uint64_cmp_order, /* cmp_order */
1804 NULL, /* cmp_contains */
1805 NULL, /* cmp_matches */
1807 uint64_hash, /* hash */
1808 uint64_is_zero, /* is_zero */
1809 uint64_is_negative, /* is_negative */
1810 NULL, /* len */
1811 NULL, /* slice */
1812 uint64_bitwise_and, /* bitwise_and */
1813 uint64_unary_minus, /* unary_minus */
1814 uint64_add, /* add */
1815 uint64_subtract, /* subtract */
1816 NULL, /* multiply */
1817 NULL, /* divide */
1818 NULL, /* modulo */
1821 ftype_register(FT_CHAR, &char_type);
1822 ftype_register(FT_UINT8, &uint8_type);
1823 ftype_register(FT_UINT16, &uint16_type);
1824 ftype_register(FT_UINT24, &uint24_type);
1825 ftype_register(FT_UINT32, &uint32_type);
1826 ftype_register(FT_UINT40, &uint40_type);
1827 ftype_register(FT_UINT48, &uint48_type);
1828 ftype_register(FT_UINT56, &uint56_type);
1829 ftype_register(FT_UINT64, &uint64_type);
1830 ftype_register(FT_INT8, &int8_type);
1831 ftype_register(FT_INT16, &int16_type);
1832 ftype_register(FT_INT24, &int24_type);
1833 ftype_register(FT_INT32, &int32_type);
1834 ftype_register(FT_INT40, &int40_type);
1835 ftype_register(FT_INT48, &int48_type);
1836 ftype_register(FT_INT56, &int56_type);
1837 ftype_register(FT_INT64, &int64_type);
1838 ftype_register(FT_BOOLEAN, &boolean_type);
1839 ftype_register(FT_IPXNET, &ipxnet_type);
1840 ftype_register(FT_FRAMENUM, &framenum_type);
1841 ftype_register(FT_EUI64, &eui64_type);
1844 void
1845 ftype_register_pseudofields_integer(int proto)
1847 static int hf_ft_char;
1848 static int hf_ft_uint8;
1849 static int hf_ft_uint16;
1850 static int hf_ft_uint24;
1851 static int hf_ft_uint32;
1852 static int hf_ft_uint40;
1853 static int hf_ft_uint48;
1854 static int hf_ft_uint56;
1855 static int hf_ft_uint64;
1856 static int hf_ft_int8;
1857 static int hf_ft_int16;
1858 static int hf_ft_int24;
1859 static int hf_ft_int32;
1860 static int hf_ft_int40;
1861 static int hf_ft_int48;
1862 static int hf_ft_int56;
1863 static int hf_ft_int64;
1864 static int hf_ft_boolean;
1865 static int hf_ft_ipxnet;
1866 static int hf_ft_framenum;
1867 static int hf_ft_eui64;
1869 static hf_register_info hf_ftypes[] = {
1870 { &hf_ft_char,
1871 { "FT_CHAR", "_ws.ftypes.char",
1872 FT_CHAR, BASE_HEX, NULL, 0x00,
1873 NULL, HFILL }
1875 { &hf_ft_uint8,
1876 { "FT_UINT8", "_ws.ftypes.uint8",
1877 FT_UINT8, BASE_HEX, NULL, 0x00,
1878 NULL, HFILL }
1880 { &hf_ft_uint16,
1881 { "FT_UINT16", "_ws.ftypes.uint16",
1882 FT_UINT16, BASE_HEX, NULL, 0x00,
1883 NULL, HFILL }
1885 { &hf_ft_uint24,
1886 { "FT_UINT24", "_ws.ftypes.uint24",
1887 FT_UINT24, BASE_HEX, NULL, 0x00,
1888 NULL, HFILL }
1890 { &hf_ft_uint32,
1891 { "FT_UINT32", "_ws.ftypes.uint32",
1892 FT_UINT32, BASE_HEX, NULL, 0x00,
1893 NULL, HFILL }
1895 { &hf_ft_uint40,
1896 { "FT_UINT40", "_ws.ftypes.uint40",
1897 FT_UINT40, BASE_HEX, NULL, 0x00,
1898 NULL, HFILL }
1900 { &hf_ft_uint48,
1901 { "FT_UINT48", "_ws.ftypes.uint48",
1902 FT_UINT48, BASE_HEX, NULL, 0x00,
1903 NULL, HFILL }
1905 { &hf_ft_uint56,
1906 { "FT_UINT56", "_ws.ftypes.uint56",
1907 FT_UINT56, BASE_HEX, NULL, 0x00,
1908 NULL, HFILL }
1910 { &hf_ft_uint64,
1911 { "FT_UINT64", "_ws.ftypes.uint64",
1912 FT_UINT64, BASE_HEX, NULL, 0x00,
1913 NULL, HFILL }
1915 { &hf_ft_int8,
1916 { "FT_INT8", "_ws.ftypes.int8",
1917 FT_INT8, BASE_DEC, NULL, 0x00,
1918 NULL, HFILL }
1920 { &hf_ft_int16,
1921 { "FT_INT16", "_ws.ftypes.int16",
1922 FT_INT16, BASE_DEC, NULL, 0x00,
1923 NULL, HFILL }
1925 { &hf_ft_int24,
1926 { "FT_INT24", "_ws.ftypes.int24",
1927 FT_INT24, BASE_DEC, NULL, 0x00,
1928 NULL, HFILL }
1930 { &hf_ft_int32,
1931 { "FT_INT32", "_ws.ftypes.int32",
1932 FT_INT32, BASE_DEC, NULL, 0x00,
1933 NULL, HFILL }
1935 { &hf_ft_int40,
1936 { "FT_INT40", "_ws.ftypes.int40",
1937 FT_INT40, BASE_DEC, NULL, 0x00,
1938 NULL, HFILL }
1940 { &hf_ft_int48,
1941 { "FT_INT48", "_ws.ftypes.int48",
1942 FT_INT48, BASE_DEC, NULL, 0x00,
1943 NULL, HFILL }
1945 { &hf_ft_int56,
1946 { "FT_INT56", "_ws.ftypes.int56",
1947 FT_INT56, BASE_DEC, NULL, 0x00,
1948 NULL, HFILL }
1950 { &hf_ft_int64,
1951 { "FT_INT64", "_ws.ftypes.int64",
1952 FT_INT64, BASE_DEC, NULL, 0x00,
1953 NULL, HFILL }
1955 { &hf_ft_boolean,
1956 { "FT_BOOLEAN", "_ws.ftypes.boolean",
1957 FT_BOOLEAN, BASE_NONE, NULL, 0x00,
1958 NULL, HFILL }
1960 { &hf_ft_ipxnet,
1961 { "FT_IPXNET", "_ws.ftypes.ipxnet",
1962 FT_IPXNET, BASE_NONE, NULL, 0x00,
1963 NULL, HFILL }
1965 { &hf_ft_framenum,
1966 { "FT_FRAMENUM", "_ws.ftypes.framenum",
1967 FT_FRAMENUM, BASE_NONE, NULL, 0x00,
1968 NULL, HFILL }
1970 { &hf_ft_eui64,
1971 { "FT_EUI64", "_ws.ftypes.eui64",
1972 FT_EUI64, BASE_NONE, NULL, 0x00,
1973 NULL, HFILL }
1977 proto_register_field_array(proto, hf_ftypes, array_length(hf_ftypes));
1981 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1983 * Local variables:
1984 * c-basic-offset: 8
1985 * tab-width: 8
1986 * indent-tabs-mode: t
1987 * End:
1989 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
1990 * :indentSize=8:tabSize=8:noTabs=false: