2 * Routines for value_strings
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include "wmem/wmem.h"
34 #include "value_string.h"
36 /* REGULAR VALUE STRING */
38 /* Tries to match val against each element in the value_string array vs.
39 Returns the associated string ptr on a match.
40 Formats val with fmt, and returns the resulting string, on failure. */
42 val_to_str(const guint32 val, const value_string *vs, const char *fmt)
46 DISSECTOR_ASSERT(fmt != NULL);
48 ret = try_val_to_str(val, vs);
52 return ep_strdup_printf(fmt, val);
55 /* Tries to match val against each element in the value_string array vs.
56 Returns the associated string ptr on a match.
57 Returns 'unknown_str', on failure. */
59 val_to_str_const(const guint32 val, const value_string *vs,
60 const char *unknown_str)
64 DISSECTOR_ASSERT(unknown_str != NULL);
66 ret = try_val_to_str(val, vs);
73 /* Tries to match val against each element in the value_string array vs.
74 Returns the associated string ptr, and sets "*idx" to the index in
75 that table, on a match, and returns NULL, and sets "*idx" to -1,
78 try_val_to_str_idx(const guint32 val, const value_string *vs, gint *idx)
82 DISSECTOR_ASSERT(idx != NULL);
85 while (vs[i].strptr) {
86 if (vs[i].value == val) {
98 /* Like try_val_to_str_idx(), but doesn't return the index. */
100 try_val_to_str(const guint32 val, const value_string *vs)
103 return try_val_to_str_idx(val, vs, &ignore_me);
106 /* 64-BIT VALUE STRING */
109 val64_to_str(const guint64 val, const val64_string *vs, const char *fmt)
113 DISSECTOR_ASSERT(fmt != NULL);
115 ret = try_val64_to_str(val, vs);
119 return ep_strdup_printf(fmt, val);
123 val64_to_str_const(const guint64 val, const val64_string *vs,
124 const char *unknown_str)
128 DISSECTOR_ASSERT(unknown_str != NULL);
130 ret = try_val64_to_str(val, vs);
138 try_val64_to_str_idx(const guint64 val, const val64_string *vs, gint *idx)
142 DISSECTOR_ASSERT(idx != NULL);
145 while (vs[i].strptr) {
146 if (vs[i].value == val) {
148 return(vs[i].strptr);
159 try_val64_to_str(const guint64 val, const val64_string *vs)
162 return try_val64_to_str_idx(val, vs, &ignore_me);
165 /* REVERSE VALUE STRING */
167 /* We use the same struct as for regular value strings, but we look up strings
168 * and return values instead */
170 /* Like val_to_str except backwards */
172 str_to_val(const gchar *val, const value_string *vs, const guint32 err_val)
176 i = str_to_val_idx(val, vs);
185 /* Find the index of a string in a value_string, or -1 when not present */
187 str_to_val_idx(const gchar *val, const value_string *vs)
193 while (vs[i].strptr) {
195 if (strcmp(vs[i].strptr, val) == 0) {
207 /* EXTENDED VALUE STRING */
209 /* Extended value strings allow fast(er) value_string array lookups by
210 * using (if possible) direct access or a binary search of the array.
212 * If the values in the value_string array are a contiguous range of values
213 * from min to max, the value will be used as as a direct index into the array.
215 * If the values in the array are not contiguous (ie: there are "gaps"),
216 * but are in assending order a binary search will be used.
218 * If direct access or binary search cannot be used, then a linear search
219 * is used and a warning is emitted.
221 * Note that the value_string array used with VALUE_STRING_EXT_INIT
222 * *must* be terminated with {0, NULL}).
224 * Extended value strings are defined at compile time as follows:
225 * static const value_string vs[] = { {value1, "string1"},
226 * {value2, "string2"},
229 * static value_string_ext vse = VALUE_STRING_EXT_INIT(vs);
231 * Extended value strings can be created at runtime by calling
232 * value_string_ext_new(<ptr to value_string array>,
233 * <total number of entries in the value_string_array>,
234 * <value_string_name>);
235 * Note: The <total number of entries in the value_string_array> should include
236 * the {0, NULL} entry.
239 /* Create a value_string_ext given a ptr to a value_string array and the total
240 * number of entries. Note that the total number of entries should include the
241 * required {0, NULL} terminating entry of the array.
242 * Returns a pointer to an epan-scoped'd and initialized value_string_ext
244 const value_string_ext *
245 value_string_ext_new(const value_string *vs, guint vs_tot_num_entries,
246 const gchar *vs_name)
248 value_string_ext *vse;
250 DISSECTOR_ASSERT (vs_name != NULL);
251 DISSECTOR_ASSERT (vs_tot_num_entries > 0);
252 /* Null-terminated value-string ? */
253 DISSECTOR_ASSERT (vs[vs_tot_num_entries-1].strptr == NULL);
255 vse = wmem_new(wmem_epan_scope(), value_string_ext);
257 vse->_vs_num_entries = vs_tot_num_entries - 1;
258 /* We set our 'match' function to the init function, which finishes by
259 * setting the match function properly and then calling it. This is a
260 * simple way to do lazy initialization of extended value strings.
261 * The init function also sets up _vs_first_value for us. */
262 vse->_vs_first_value = 0;
263 vse->_vs_match2 = _try_val_to_str_ext_init;
264 vse->_vs_name = vs_name;
270 value_string_ext_free(const value_string_ext *vse)
272 wmem_free(wmem_epan_scope(), (void *)vse);
275 /* Like try_val_to_str for extended value strings */
277 try_val_to_str_ext(const guint32 val, const value_string_ext *vse)
280 const value_string *vs = vse->_vs_match2(val, vse);
290 /* Like try_val_to_str_idx for extended value strings */
292 try_val_to_str_idx_ext(const guint32 val, const value_string_ext *vse, gint *idx)
295 const value_string *vs = vse->_vs_match2(val, vse);
297 *idx = (gint) (vs - vse->_vs_p);
305 /* Like val_to_str for extended value strings */
307 val_to_str_ext(const guint32 val, const value_string_ext *vse, const char *fmt)
311 DISSECTOR_ASSERT(fmt != NULL);
313 ret = try_val_to_str_ext(val, vse);
317 return ep_strdup_printf(fmt, val);
320 /* Like val_to_str_const for extended value strings */
322 val_to_str_ext_const(const guint32 val, const value_string_ext *vse,
323 const char *unknown_str)
327 DISSECTOR_ASSERT(unknown_str != NULL);
329 ret = try_val_to_str_ext(val, vse);
336 /* Fallback linear matching algorithm for extended value strings */
337 static const value_string *
338 _try_val_to_str_linear(const guint32 val, const value_string_ext *vse)
340 const value_string *vs_p = vse->_vs_p;
342 for (i=0; i<vse->_vs_num_entries; i++) {
343 if (vs_p[i].value == val)
349 /* Constant-time matching algorithm for contiguous extended value strings */
350 static const value_string *
351 _try_val_to_str_index(const guint32 val, const value_string_ext *vse)
355 i = val - vse->_vs_first_value;
356 if (i < vse->_vs_num_entries) {
357 g_assert (val == vse->_vs_p[i].value);
358 return &(vse->_vs_p[i]);
363 /* log(n)-time matching algorithm for sorted extended value strings */
364 static const value_string *
365 _try_val_to_str_bsearch(const guint32 val, const value_string_ext *vse)
370 for (low = 0, max = vse->_vs_num_entries; low < max; ) {
372 item = vse->_vs_p[i].value;
379 return &(vse->_vs_p[i]);
384 /* Initializes an extended value string. Behaves like a match function to
385 * permit lazy initialization of extended value strings.
386 * - Goes through the value_string array to determine the fastest possible
388 * - Verifies that the value_string contains no NULL string pointers.
389 * - Verifies that the value_string is terminated by {0, NULL}
392 _try_val_to_str_ext_init(const guint32 val, const value_string_ext *a_vse)
394 /* Cast away the constness!
395 * It's better if the prototype for this function matches the other
396 * _try_val_to_str_* functions (so we don't have to cast it when storing it
397 * in _try_val_to_str so the compiler will notice if the prototypes get out
398 * of sync), but the init function is unique in that it does actually
401 value_string_ext *vse = (value_string_ext *)a_vse;
403 const value_string *vs_p = vse->_vs_p;
404 const guint vs_num_entries = vse->_vs_num_entries;
406 /* The matching algorithm used:
407 * VS_SEARCH - slow sequential search (as in a normal value string)
408 * VS_BIN_TREE - log(n)-time binary search, the values must be sorted
409 * VS_INDEX - constant-time index lookup, the values must be contiguous
411 enum { VS_SEARCH, VS_BIN_TREE, VS_INDEX } type = VS_INDEX;
413 /* Note: The value_string 'value' is *unsigned*, but we do a little magic
414 * to help with value strings that have negative values.
416 * { -3, -2, -1, 0, 1, 2 }
417 * will be treated as "ascending ordered" (although it isn't technically),
418 * thus allowing constant-time index search
420 * { -3, -2, 0, 1, 2 } and { -3, -2, -1, 0, 2 }
421 * will both be considered as "out-of-order with gaps", thus falling
422 * back to the slow linear search
424 * { 0, 1, 2, -3, -2 } and { 0, 2, -3, -2, -1 }
425 * will be considered "ascending ordered with gaps" thus allowing
426 * a log(n)-time 'binary' search
428 * If you're confused, think of how negative values are represented, or
429 * google two's complement.
436 DISSECTOR_ASSERT((vs_p[vs_num_entries].value == 0) &&
437 (vs_p[vs_num_entries].strptr == NULL));
439 vse->_vs_first_value = vs_p[0].value;
440 first_value = vs_p[0].value;
441 prev_value = first_value;
443 for (i = 0; i < vs_num_entries; i++) {
444 DISSECTOR_ASSERT(vs_p[i].strptr != NULL);
445 if ((type == VS_INDEX) && (vs_p[i].value != (i + first_value))) {
448 /* XXX: Should check for dups ?? */
449 if (type == VS_BIN_TREE) {
450 if (prev_value > vs_p[i].value) {
451 g_warning("Extended value string '%s' forced to fall back to linear search:\n"
452 " entry %u, value %u [%#x] < previous entry, value %u [%#x]",
453 vse->_vs_name, i, vs_p[i].value, vs_p[i].value, prev_value, prev_value);
457 if (first_value > vs_p[i].value) {
458 g_warning("Extended value string '%s' forced to fall back to linear search:\n"
459 " entry %u, value %u [%#x] < first entry, value %u [%#x]",
460 vse->_vs_name, i, vs_p[i].value, vs_p[i].value, first_value, first_value);
466 prev_value = vs_p[i].value;
471 vse->_vs_match2 = _try_val_to_str_linear;
474 vse->_vs_match2 = _try_val_to_str_bsearch;
477 vse->_vs_match2 = _try_val_to_str_index;
480 g_assert_not_reached();
484 return vse->_vs_match2(val, vse);
487 /* STRING TO STRING MATCHING */
489 /* string_string is like value_string except the values being matched are
490 * also strings (instead of unsigned integers) */
492 /* Like val_to_str except for string_string */
494 str_to_str(const gchar *val, const string_string *vs, const char *fmt)
498 DISSECTOR_ASSERT(fmt != NULL);
500 ret = try_str_to_str(val, vs);
504 return ep_strdup_printf(fmt, val);
507 /* Like try_val_to_str_idx except for string_string */
509 try_str_to_str_idx(const gchar *val, const string_string *vs, gint *idx)
514 while (vs[i].strptr) {
515 if (!strcmp(vs[i].value,val)) {
517 return(vs[i].strptr);
527 /* Like try_val_to_str except for string_string */
529 try_str_to_str(const gchar *val, const string_string *vs)
532 return try_str_to_str_idx(val, vs, &ignore_me);
535 /* RANGE TO STRING MATCHING */
537 /* range_string is like value_string except the values being matched are
538 * integer ranges (for example, 0-10, 11-19, etc.) instead of single values. */
540 /* Like val_to_str except for range_string */
542 rval_to_str(const guint32 val, const range_string *rs, const char *fmt)
544 const gchar *ret = NULL;
546 DISSECTOR_ASSERT(fmt != NULL);
548 ret = try_rval_to_str(val, rs);
552 return ep_strdup_printf(fmt, val);
555 /* Like val_to_str_const except for range_string */
557 rval_to_str_const(const guint32 val, const range_string *rs,
558 const char *unknown_str)
560 const gchar *ret = NULL;
562 DISSECTOR_ASSERT(unknown_str != NULL);
564 ret = try_rval_to_str(val, rs);
571 /* Like try_val_to_str_idx except for range_string */
573 try_rval_to_str_idx(const guint32 val, const range_string *rs, gint *idx)
578 while(rs[i].strptr) {
579 if( (val >= rs[i].value_min) && (val <= rs[i].value_max) ) {
581 return (rs[i].strptr);
591 /* Like try_val_to_str except for range_string */
593 try_rval_to_str(const guint32 val, const range_string *rs)
596 return try_rval_to_str_idx(val, rs, &ignore_me);
601 /* Functions for use by proto_registrar_dump_values(), see proto.c */
604 value_string_ext_validate(const value_string_ext *vse)
608 #ifndef _WIN32 /* doesn't work on Windows for refs from another DLL ?? */
609 if ((vse->_vs_match2 != _try_val_to_str_ext_init) &&
610 (vse->_vs_match2 != _try_val_to_str_linear) &&
611 (vse->_vs_match2 != _try_val_to_str_bsearch) &&
612 (vse->_vs_match2 != _try_val_to_str_index))
619 value_string_ext_match_type_str(const value_string_ext *vse)
621 if (vse->_vs_match2 == _try_val_to_str_linear)
622 return "[Linear Search]";
623 if (vse->_vs_match2 == _try_val_to_str_bsearch)
624 return "[Binary Search]";
625 if (vse->_vs_match2 == _try_val_to_str_index)
626 return "[Direct (indexed) Access]";
627 return "[Match Type not initialized or invalid]";
631 * Editor modelines - http://www.wireshark.org/tools/modelines.html
636 * indent-tabs-mode: nil
639 * vi: set shiftwidth=4 tabstop=8 expandtab:
640 * :indentSize=4:tabSize=8:noTabs=true: