epan/dissectors/pidl/samr/samr.cnf cnf_dissect_lsa_BinaryString => lsarpc_dissect_str...
[wireshark-sm.git] / epan / ftypes / ftype-double.c
blobccab6390a85ee8d61df45e47894efeccc706c07a
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 <ftypes-int.h>
12 #include <float.h>
13 #include <wsutil/array.h>
15 static void
16 double_fvalue_new(fvalue_t *fv)
18 fv->value.floating = 0.0;
21 static void
22 double_fvalue_set_floating(fvalue_t *fv, double value)
24 fv->value.floating = value;
27 static double
28 value_get_floating(fvalue_t *fv)
30 return fv->value.floating;
33 static bool
34 val_from_uinteger64(fvalue_t *fv, const char *s _U_, uint64_t value, char **err_msg _U_)
36 fv->value.floating = (double)value;
37 return true;
40 static bool
41 val_from_sinteger64(fvalue_t *fv, const char *s _U_, int64_t value, char **err_msg _U_)
43 fv->value.floating = (double)value;
44 return true;
47 static bool
48 val_from_double(fvalue_t *fv, const char *s _U_, double floating, char **err_msg _U_)
50 fv->value.floating = floating;
51 return true;
54 static char *
55 float_val_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype, int field_display _U_)
57 char *buf = wmem_alloc(scope, G_ASCII_DTOSTR_BUF_SIZE);
58 if (rtype == FTREPR_DFILTER)
59 g_ascii_dtostr(buf, G_ASCII_DTOSTR_BUF_SIZE, fv->value.floating);
60 else
61 g_ascii_formatd(buf, G_ASCII_DTOSTR_BUF_SIZE, "%." G_STRINGIFY(FLT_DIG) "g", fv->value.floating);
62 return buf;
65 static char *
66 double_val_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype, int field_display _U_)
68 char *buf = wmem_alloc(scope, G_ASCII_DTOSTR_BUF_SIZE);
69 if (rtype == FTREPR_DFILTER)
70 g_ascii_dtostr(buf, G_ASCII_DTOSTR_BUF_SIZE, fv->value.floating);
71 else
72 g_ascii_formatd(buf, G_ASCII_DTOSTR_BUF_SIZE, "%." G_STRINGIFY(DBL_DIG) "g", fv->value.floating);
73 return buf;
76 enum ft_result
77 double_val_to_double(const fvalue_t *fv, double *repr)
79 *repr = fv->value.floating;
80 return FT_OK;
83 static enum ft_result
84 val_unary_minus(fvalue_t * dst, const fvalue_t *src, char **err_ptr _U_)
86 dst->value.floating = -src->value.floating;
87 return FT_OK;
90 static enum ft_result
91 val_add(fvalue_t * dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr _U_)
93 dst->value.floating = a->value.floating + b->value.floating;
94 return FT_OK;
97 static enum ft_result
98 val_subtract(fvalue_t * dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr _U_)
100 dst->value.floating = a->value.floating - b->value.floating;
101 return FT_OK;
104 static enum ft_result
105 val_multiply(fvalue_t * dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr _U_)
107 dst->value.floating = a->value.floating * b->value.floating;
108 return FT_OK;
111 static enum ft_result
112 val_divide(fvalue_t * dst, const fvalue_t *a, const fvalue_t *b, char **err_ptr _U_)
114 dst->value.floating = a->value.floating / b->value.floating;
115 return FT_OK;
118 static enum ft_result
119 cmp_order(const fvalue_t *a, const fvalue_t *b, int *cmp)
121 if (a->value.floating < b->value.floating)
122 *cmp = -1;
123 else if (a->value.floating > b->value.floating)
124 *cmp = 1;
125 else
126 *cmp = 0;
127 return FT_OK;
130 static bool
131 val_is_zero(const fvalue_t *fv_a)
133 return fv_a->value.floating == 0;
136 static bool
137 val_is_negative(const fvalue_t *fv_a)
139 return fv_a->value.floating < 0;
142 static unsigned
143 val_hash(const fvalue_t *fv)
145 return g_double_hash(&fv->value.floating);
148 void
149 ftype_register_double(void)
152 static const ftype_t float_type = {
153 FT_FLOAT, /* ftype */
154 0, /* wire_size */
155 double_fvalue_new, /* new_value */
156 NULL, /* copy_value */
157 NULL, /* free_value */
158 NULL, /* val_from_literal */
159 NULL, /* val_from_string */
160 NULL, /* val_from_charconst */
161 val_from_uinteger64, /* val_from_uinteger64 */
162 val_from_sinteger64, /* val_from_sinteger64 */
163 val_from_double, /* val_from_double */
164 float_val_to_repr, /* val_to_string_repr */
166 NULL, /* val_to_uinteger64 */
167 NULL, /* val_to_sinteger64 */
168 double_val_to_double, /* val_to_double */
170 { .set_value_floating = double_fvalue_set_floating }, /* union set_value */
171 { .get_value_floating = value_get_floating }, /* union get_value */
173 cmp_order,
174 NULL, /* cmp_contains */
175 NULL, /* cmp_matches */
177 val_hash, /* hash */
178 val_is_zero, /* is_zero */
179 val_is_negative, /* is_negative */
180 NULL, /* len */
181 NULL, /* slice */
182 NULL, /* bitwise_and */
183 val_unary_minus, /* unary_minus */
184 val_add, /* add */
185 val_subtract, /* subtract */
186 val_multiply, /* multiply */
187 val_divide, /* divide */
188 NULL, /* modulo */
191 static const ftype_t double_type = {
192 FT_DOUBLE, /* ftype */
193 0, /* wire_size */
194 double_fvalue_new, /* new_value */
195 NULL, /* copy_value */
196 NULL, /* free_value */
197 NULL, /* val_from_literal */
198 NULL, /* val_from_string */
199 NULL, /* val_from_charconst */
200 val_from_uinteger64, /* val_from_uinteger64 */
201 val_from_sinteger64, /* val_from_sinteger64 */
202 val_from_double, /* val_from_double */
203 double_val_to_repr, /* val_to_string_repr */
205 NULL, /* val_to_uinteger64 */
206 NULL, /* val_to_sinteger64 */
207 double_val_to_double, /* val_to_double */
209 { .set_value_floating = double_fvalue_set_floating }, /* union set_value */
210 { .get_value_floating = value_get_floating }, /* union get_value */
212 cmp_order,
213 NULL, /* cmp_contains */
214 NULL, /* cmp_matches */
216 val_hash, /* hash */
217 val_is_zero, /* is_zero */
218 val_is_negative, /* is_negative */
219 NULL, /* len */
220 NULL, /* slice */
221 NULL, /* bitwise_and */
222 val_unary_minus, /* unary_minus */
223 val_add, /* add */
224 val_subtract, /* subtract */
225 val_multiply, /* multiply */
226 val_divide, /* divide */
227 NULL, /* modulo */
230 ftype_register(FT_FLOAT, &float_type);
231 ftype_register(FT_DOUBLE, &double_type);
234 void
235 ftype_register_pseudofields_double(int proto)
237 static int hf_ft_float;
238 static int hf_ft_double;
240 static hf_register_info hf_ftypes[] = {
241 { &hf_ft_float,
242 { "FT_FLOAT", "_ws.ftypes.float",
243 FT_FLOAT, BASE_NONE, NULL, 0x00,
244 NULL, HFILL }
246 { &hf_ft_double,
247 { "FT_DOUBLE", "_ws.ftypes.double",
248 FT_DOUBLE, BASE_NONE, NULL, 0x00,
249 NULL, HFILL }
253 proto_register_field_array(proto, hf_ftypes, array_length(hf_ftypes));
257 * Editor modelines - https://www.wireshark.org/tools/modelines.html
259 * Local variables:
260 * c-basic-offset: 8
261 * tab-width: 8
262 * indent-tabs-mode: t
263 * End:
265 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
266 * :indentSize=8:tabSize=8:noTabs=false: