2001-02-01 20:31:21 +00:00
|
|
|
/*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2001-02-01 20:31:21 +00:00
|
|
|
* Copyright 2001 Gerald Combs
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2018-02-08 16:59:17 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2001-02-01 20:31:21 +00:00
|
|
|
*/
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2009-08-26 19:27:49 +00:00
|
|
|
#include <stdio.h>
|
2001-02-01 20:21:25 +00:00
|
|
|
#include <ftypes-int.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <errno.h>
|
2003-07-31 03:52:43 +00:00
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
#include "strutil.h"
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2015-02-05 10:57:27 +00:00
|
|
|
#define DOUBLE_REPR_LENGTH 27
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
static void
|
|
|
|
double_fvalue_new(fvalue_t *fv)
|
|
|
|
{
|
|
|
|
fv->value.floating = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
double_fvalue_set_floating(fvalue_t *fv, gdouble value)
|
|
|
|
{
|
|
|
|
fv->value.floating = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double
|
|
|
|
value_get_floating(fvalue_t *fv)
|
|
|
|
{
|
|
|
|
return fv->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2015-01-18 10:22:19 +00:00
|
|
|
val_from_unparsed(fvalue_t *fv, const char *s, gboolean allow_partial_value _U_, gchar **err_msg)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
char *endptr = NULL;
|
|
|
|
|
2012-06-11 19:27:57 +00:00
|
|
|
fv->value.floating = g_ascii_strtod(s, &endptr);
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
if (endptr == s || *endptr != '\0') {
|
|
|
|
/* This isn't a valid number. */
|
2015-01-18 10:22:19 +00:00
|
|
|
if (err_msg != NULL)
|
|
|
|
*err_msg = g_strdup_printf("\"%s\" is not a valid number.", s);
|
2001-02-01 20:21:25 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (errno == ERANGE) {
|
|
|
|
if (fv->value.floating == 0) {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (err_msg != NULL)
|
|
|
|
*err_msg = g_strdup_printf("\"%s\" causes floating-point underflow.", s);
|
2001-02-01 20:21:25 +00:00
|
|
|
}
|
|
|
|
else if (fv->value.floating == HUGE_VAL) {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (err_msg != NULL)
|
|
|
|
*err_msg = g_strdup_printf("\"%s\" causes floating-point overflow.", s);
|
2001-02-01 20:21:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (err_msg != NULL)
|
|
|
|
*err_msg = g_strdup_printf("\"%s\" is not a valid floating-point number.",
|
|
|
|
s);
|
2001-02-01 20:21:25 +00:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-07-31 03:52:43 +00:00
|
|
|
static int
|
2015-01-04 02:56:16 +00:00
|
|
|
float_val_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_, int field_display _U_)
|
2003-07-31 03:52:43 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 1 character for a sign.
|
|
|
|
* 26 characters for a Really Big Number.
|
|
|
|
* XXX - is that platform-dependent?
|
|
|
|
* XXX - smaller for float than for double?
|
|
|
|
* XXX - can we compute it from FLT_DIG and the like?
|
|
|
|
*/
|
2015-02-05 10:57:27 +00:00
|
|
|
return DOUBLE_REPR_LENGTH;
|
2003-07-31 03:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-05-10 20:37:44 +00:00
|
|
|
float_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_, char *buf, unsigned int size)
|
2003-07-31 03:52:43 +00:00
|
|
|
{
|
2016-05-10 20:37:44 +00:00
|
|
|
g_snprintf(buf, size, "%." G_STRINGIFY(FLT_DIG) "g", fv->value.floating);
|
2003-07-31 03:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-01-04 02:56:16 +00:00
|
|
|
double_val_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_, int field_display _U_)
|
2003-07-31 03:52:43 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 1 character for a sign.
|
|
|
|
* 26 characters for a Really Big Number.
|
|
|
|
* XXX - is that platform-dependent?
|
|
|
|
* XXX - can we compute it from DBL_DIG and the like?
|
|
|
|
*/
|
2015-02-05 10:57:27 +00:00
|
|
|
return DOUBLE_REPR_LENGTH;
|
2003-07-31 03:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-05-10 20:37:44 +00:00
|
|
|
double_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_, char *buf, unsigned int size)
|
2003-07-31 03:52:43 +00:00
|
|
|
{
|
2016-05-10 20:37:44 +00:00
|
|
|
g_snprintf(buf, size, "%." G_STRINGIFY(DBL_DIG) "g", fv->value.floating);
|
2003-07-31 03:52:43 +00:00
|
|
|
}
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_eq(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return a->value.floating == b->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_ne(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return a->value.floating != b->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_gt(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return a->value.floating > b->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_ge(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return a->value.floating >= b->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_lt(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return a->value.floating < b->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-11-06 13:28:59 +00:00
|
|
|
cmp_le(const fvalue_t *a, const fvalue_t *b)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
return a->value.floating <= b->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ftype_register_double(void)
|
|
|
|
{
|
|
|
|
|
2002-08-24 19:45:28 +00:00
|
|
|
static ftype_t float_type = {
|
2006-05-02 14:26:17 +00:00
|
|
|
FT_FLOAT, /* ftype */
|
2004-08-22 00:31:58 +00:00
|
|
|
"FT_FLOAT", /* name */
|
2011-12-27 20:35:41 +00:00
|
|
|
"Floating point (single-precision)", /* pretty_name */
|
2004-08-22 00:31:58 +00:00
|
|
|
0, /* wire_size */
|
|
|
|
double_fvalue_new, /* new_value */
|
|
|
|
NULL, /* free_value */
|
2003-07-25 03:44:05 +00:00
|
|
|
val_from_unparsed, /* val_from_unparsed */
|
|
|
|
NULL, /* val_from_string */
|
2003-07-31 03:52:43 +00:00
|
|
|
float_val_to_repr, /* val_to_string_repr */
|
|
|
|
float_val_repr_len, /* len_string_repr */
|
2002-08-24 19:45:28 +00:00
|
|
|
|
2017-03-15 08:37:03 +00:00
|
|
|
{ .set_value_floating = double_fvalue_set_floating }, /* union set_value */
|
2017-03-16 21:34:49 +00:00
|
|
|
{ .get_value_floating = value_get_floating }, /* union get_value */
|
2002-08-24 19:45:28 +00:00
|
|
|
|
|
|
|
cmp_eq,
|
|
|
|
cmp_ne,
|
|
|
|
cmp_gt,
|
|
|
|
cmp_ge,
|
|
|
|
cmp_lt,
|
|
|
|
cmp_le,
|
2004-02-27 12:00:32 +00:00
|
|
|
NULL, /* cmp_bitwise_and */
|
2003-08-27 15:23:11 +00:00
|
|
|
NULL, /* cmp_contains */
|
2003-12-06 16:35:20 +00:00
|
|
|
NULL, /* cmp_matches */
|
2002-08-24 19:45:28 +00:00
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2001-02-01 20:21:25 +00:00
|
|
|
static ftype_t double_type = {
|
2006-05-02 14:26:17 +00:00
|
|
|
FT_DOUBLE, /* ftype */
|
2004-08-22 00:31:58 +00:00
|
|
|
"FT_DOUBLE", /* name */
|
2011-12-27 20:35:41 +00:00
|
|
|
"Floating point (double-precision)", /* pretty_name */
|
2004-08-22 00:31:58 +00:00
|
|
|
0, /* wire_size */
|
|
|
|
double_fvalue_new, /* new_value */
|
|
|
|
NULL, /* free_value */
|
2003-07-25 03:44:05 +00:00
|
|
|
val_from_unparsed, /* val_from_unparsed */
|
|
|
|
NULL, /* val_from_string */
|
2003-07-31 03:52:43 +00:00
|
|
|
double_val_to_repr, /* val_to_string_repr */
|
|
|
|
double_val_repr_len, /* len_string_repr */
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2017-03-15 08:37:03 +00:00
|
|
|
{ .set_value_floating = double_fvalue_set_floating }, /* union set_value */
|
2017-03-16 21:34:49 +00:00
|
|
|
{ .get_value_floating = value_get_floating }, /* union get_value */
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
cmp_eq,
|
|
|
|
cmp_ne,
|
|
|
|
cmp_gt,
|
|
|
|
cmp_ge,
|
|
|
|
cmp_lt,
|
|
|
|
cmp_le,
|
2004-02-27 12:00:32 +00:00
|
|
|
NULL, /* cmp_bitwise_and */
|
2003-08-27 15:23:11 +00:00
|
|
|
NULL, /* cmp_contains */
|
2003-12-06 16:35:20 +00:00
|
|
|
NULL, /* cmp_matches */
|
2001-07-13 00:55:58 +00:00
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2001-02-01 20:21:25 +00:00
|
|
|
};
|
|
|
|
|
2002-08-24 19:45:28 +00:00
|
|
|
ftype_register(FT_FLOAT, &float_type);
|
2001-02-01 20:21:25 +00:00
|
|
|
ftype_register(FT_DOUBLE, &double_type);
|
|
|
|
}
|