2001-02-01 20:31:21 +00:00
|
|
|
/*
|
2004-07-18 00:24:25 +00:00
|
|
|
* $Id$
|
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
|
|
|
*
|
2001-02-01 20:31:21 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2001-02-01 20:31:21 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2001-02-01 20:31:21 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
2001-02-01 20:21:25 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
2003-08-27 15:23:11 +00:00
|
|
|
val_from_unparsed(fvalue_t *fv, char *s, gboolean allow_partial_value _U_, LogFunc logfunc)
|
2001-02-01 20:21:25 +00:00
|
|
|
{
|
|
|
|
char *endptr = NULL;
|
|
|
|
|
|
|
|
fv->value.floating = strtod(s, &endptr);
|
|
|
|
|
|
|
|
if (endptr == s || *endptr != '\0') {
|
|
|
|
/* This isn't a valid number. */
|
2002-02-05 22:50:17 +00:00
|
|
|
logfunc("\"%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) {
|
2002-02-05 22:50:17 +00:00
|
|
|
logfunc("\"%s\" causes floating-point underflow.", s);
|
2001-02-01 20:21:25 +00:00
|
|
|
}
|
|
|
|
else if (fv->value.floating == HUGE_VAL) {
|
2002-02-05 22:50:17 +00:00
|
|
|
logfunc("\"%s\" causes floating-point overflow.", s);
|
2001-02-01 20:21:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-02-05 22:50:17 +00:00
|
|
|
logfunc("\"%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
|
|
|
|
float_val_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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?
|
|
|
|
*/
|
|
|
|
return 1 + 26;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
float_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
|
|
|
|
{
|
2009-08-29 07:53:48 +00:00
|
|
|
sprintf(buf, "%." STRINGIFY(FLT_DIG) "g", fv->value.floating);
|
2003-07-31 03:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
double_val_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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?
|
|
|
|
*/
|
|
|
|
return 1 + 26;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
double_val_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, char *buf)
|
|
|
|
{
|
2009-08-29 07:53:48 +00:00
|
|
|
sprintf(buf, "%." 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
|
|
|
|
cmp_eq(fvalue_t *a, fvalue_t *b)
|
|
|
|
{
|
|
|
|
return a->value.floating == b->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
cmp_ne(fvalue_t *a, fvalue_t *b)
|
|
|
|
{
|
|
|
|
return a->value.floating != b->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
cmp_gt(fvalue_t *a, fvalue_t *b)
|
|
|
|
{
|
|
|
|
return a->value.floating > b->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
cmp_ge(fvalue_t *a, fvalue_t *b)
|
|
|
|
{
|
|
|
|
return a->value.floating >= b->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
cmp_lt(fvalue_t *a, fvalue_t *b)
|
|
|
|
{
|
|
|
|
return a->value.floating < b->value.floating;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
cmp_le(fvalue_t *a, fvalue_t *b)
|
|
|
|
{
|
|
|
|
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 */
|
|
|
|
"floating point (single-precision)", /* pretty_name */
|
|
|
|
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
|
|
|
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* set_value */
|
2007-01-18 11:02:26 +00:00
|
|
|
NULL, /* set_value_uinteger */
|
|
|
|
NULL, /* set_value_snteger */
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* set_value_integer64 */
|
|
|
|
double_fvalue_set_floating, /* set_value_floating */
|
2002-08-24 19:45:28 +00:00
|
|
|
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* get_value */
|
2007-01-18 11:02:26 +00:00
|
|
|
NULL, /* get_value_uinteger */
|
|
|
|
NULL, /* get_value_sinteger */
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* get_value_integer64 */
|
|
|
|
value_get_floating, /* get_value_floating */
|
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 */
|
|
|
|
"floating point (double-precision)", /* pretty_name */
|
|
|
|
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
|
|
|
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* set_value */
|
2007-01-18 11:02:26 +00:00
|
|
|
NULL, /* set_value_uinteger */
|
|
|
|
NULL, /* set_value_sinteger */
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* set_value_integer64 */
|
|
|
|
double_fvalue_set_floating, /* set_value_floating */
|
2001-02-01 20:21:25 +00:00
|
|
|
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* get_value */
|
2007-01-18 11:02:26 +00:00
|
|
|
NULL, /* get_value_uinteger */
|
|
|
|
NULL, /* get_value_snteger */
|
2004-08-22 00:31:58 +00:00
|
|
|
NULL, /* get_value_integer64 */
|
|
|
|
value_get_floating, /* get_value_floating */
|
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);
|
|
|
|
}
|