2 * Routines for value_strings
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "wmem/wmem.h"
31 #include "value_string.h"
33 /* REGULAR VALUE STRING */
35 /* Tries to match val against each element in the value_string array vs.
36 Returns the associated string ptr on a match.
37 Formats val with fmt, and returns the resulting string, on failure. */
39 val_to_str(const guint32 val, const value_string *vs, const char *fmt)
43 DISSECTOR_ASSERT(fmt != NULL);
45 ret = try_val_to_str(val, vs);
49 return wmem_strdup_printf(wmem_packet_scope(), fmt, val);
53 val_to_str_wmem(wmem_allocator_t *scope, const guint32 val, const value_string *vs, const char *fmt)
57 DISSECTOR_ASSERT(fmt != NULL);
59 ret = try_val_to_str(val, vs);
61 return wmem_strdup(scope, ret);
63 return wmem_strdup_printf(scope, fmt, val);
66 /* Tries to match val against each element in the value_string array vs.
67 Returns the associated string ptr on a match.
68 Returns 'unknown_str', on failure. */
70 val_to_str_const(const guint32 val, const value_string *vs,
71 const char *unknown_str)
75 DISSECTOR_ASSERT(unknown_str != NULL);
77 ret = try_val_to_str(val, vs);
84 /* Tries to match val against each element in the value_string array vs.
85 Returns the associated string ptr, and sets "*idx" to the index in
86 that table, on a match, and returns NULL, and sets "*idx" to -1,
89 try_val_to_str_idx(const guint32 val, const value_string *vs, gint *idx)
93 DISSECTOR_ASSERT(idx != NULL);
96 while (vs[i].strptr) {
97 if (vs[i].value == val) {
109 /* Like try_val_to_str_idx(), but doesn't return the index. */
111 try_val_to_str(const guint32 val, const value_string *vs)
114 return try_val_to_str_idx(val, vs, &ignore_me);
117 /* 64-BIT VALUE STRING */
120 val64_to_str(const guint64 val, const val64_string *vs, const char *fmt)
124 DISSECTOR_ASSERT(fmt != NULL);
126 ret = try_val64_to_str(val, vs);
130 return wmem_strdup_printf(wmem_packet_scope(), fmt, val);
134 val64_to_str_const(const guint64 val, const val64_string *vs,
135 const char *unknown_str)
139 DISSECTOR_ASSERT(unknown_str != NULL);
141 ret = try_val64_to_str(val, vs);
149 try_val64_to_str_idx(const guint64 val, const val64_string *vs, gint *idx)
153 DISSECTOR_ASSERT(idx != NULL);
156 while (vs[i].strptr) {
157 if (vs[i].value == val) {
159 return(vs[i].strptr);
170 try_val64_to_str(const guint64 val, const val64_string *vs)
173 return try_val64_to_str_idx(val, vs, &ignore_me);
176 /* REVERSE VALUE STRING */
178 /* We use the same struct as for regular value strings, but we look up strings
179 * and return values instead */
181 /* Like val_to_str except backwards */
183 str_to_val(const gchar *val, const value_string *vs, const guint32 err_val)
187 i = str_to_val_idx(val, vs);
196 /* Find the index of a string in a value_string, or -1 when not present */
198 str_to_val_idx(const gchar *val, const value_string *vs)
204 while (vs[i].strptr) {
206 if (strcmp(vs[i].strptr, val) == 0) {
218 /* EXTENDED VALUE STRING */
220 /* Extended value strings allow fast(er) value_string array lookups by
221 * using (if possible) direct access or a binary search of the array.
223 * If the values in the value_string array are a contiguous range of values
224 * from min to max, the value will be used as as a direct index into the array.
226 * If the values in the array are not contiguous (ie: there are "gaps"),
227 * but are in assending order a binary search will be used.
229 * If direct access or binary search cannot be used, then a linear search
230 * is used and a warning is emitted.
232 * Note that the value_string array used with VALUE_STRING_EXT_INIT
233 * *must* be terminated with {0, NULL}).
235 * Extended value strings are defined at compile time as follows:
236 * static const value_string vs[] = { {value1, "string1"},
237 * {value2, "string2"},
240 * static value_string_ext vse = VALUE_STRING_EXT_INIT(vs);
242 * Extended value strings can be created at runtime by calling
243 * value_string_ext_new(<ptr to value_string array>,
244 * <total number of entries in the value_string_array>,
245 * <value_string_name>);
246 * Note: The <total number of entries in the value_string_array> should include
247 * the {0, NULL} entry.
250 /* Create a value_string_ext given a ptr to a value_string array and the total
251 * number of entries. Note that the total number of entries should include the
252 * required {0, NULL} terminating entry of the array.
253 * Returns a pointer to an epan-scoped'd and initialized value_string_ext
256 value_string_ext_new(const value_string *vs, guint vs_tot_num_entries,
257 const gchar *vs_name)
259 value_string_ext *vse;
261 DISSECTOR_ASSERT (vs_name != NULL);
262 DISSECTOR_ASSERT (vs_tot_num_entries > 0);
263 /* Null-terminated value-string ? */
264 DISSECTOR_ASSERT (vs[vs_tot_num_entries-1].strptr == NULL);
266 vse = wmem_new(wmem_epan_scope(), value_string_ext);
268 vse->_vs_num_entries = vs_tot_num_entries - 1;
269 /* We set our 'match' function to the init function, which finishes by
270 * setting the match function properly and then calling it. This is a
271 * simple way to do lazy initialization of extended value strings.
272 * The init function also sets up _vs_first_value for us. */
273 vse->_vs_first_value = 0;
274 vse->_vs_match2 = _try_val_to_str_ext_init;
275 vse->_vs_name = vs_name;
281 value_string_ext_free(const value_string_ext *vse)
283 wmem_free(wmem_epan_scope(), (void *)vse);
286 /* Like try_val_to_str for extended value strings */
288 try_val_to_str_ext(const guint32 val, value_string_ext *vse)
291 const value_string *vs = vse->_vs_match2(val, vse);
301 /* Like try_val_to_str_idx for extended value strings */
303 try_val_to_str_idx_ext(const guint32 val, value_string_ext *vse, gint *idx)
306 const value_string *vs = vse->_vs_match2(val, vse);
308 *idx = (gint) (vs - vse->_vs_p);
316 /* Like val_to_str for extended value strings */
318 val_to_str_ext(const guint32 val, value_string_ext *vse, const char *fmt)
322 DISSECTOR_ASSERT(fmt != NULL);
324 ret = try_val_to_str_ext(val, vse);
328 return wmem_strdup_printf(wmem_packet_scope(), fmt, val);
332 val_to_str_ext_wmem(wmem_allocator_t *scope, const guint32 val, value_string_ext *vse, const char *fmt)
336 DISSECTOR_ASSERT(fmt != NULL);
338 ret = try_val_to_str_ext(val, vse);
340 return wmem_strdup(scope, ret);
342 return wmem_strdup_printf(scope, fmt, val);
345 /* Like val_to_str_const for extended value strings */
347 val_to_str_ext_const(const guint32 val, value_string_ext *vse,
348 const char *unknown_str)
352 DISSECTOR_ASSERT(unknown_str != NULL);
354 ret = try_val_to_str_ext(val, vse);
361 /* Fallback linear matching algorithm for extended value strings */
362 static const value_string *
363 _try_val_to_str_linear(const guint32 val, value_string_ext *vse)
365 const value_string *vs_p = vse->_vs_p;
367 for (i=0; i<vse->_vs_num_entries; i++) {
368 if (vs_p[i].value == val)
374 /* Constant-time matching algorithm for contiguous extended value strings */
375 static const value_string *
376 _try_val_to_str_index(const guint32 val, value_string_ext *vse)
380 i = val - vse->_vs_first_value;
381 if (i < vse->_vs_num_entries) {
382 g_assert (val == vse->_vs_p[i].value);
383 return &(vse->_vs_p[i]);
388 /* log(n)-time matching algorithm for sorted extended value strings */
389 static const value_string *
390 _try_val_to_str_bsearch(const guint32 val, value_string_ext *vse)
395 for (low = 0, max = vse->_vs_num_entries; low < max; ) {
397 item = vse->_vs_p[i].value;
404 return &(vse->_vs_p[i]);
409 /* Initializes an extended value string. Behaves like a match function to
410 * permit lazy initialization of extended value strings.
411 * - Goes through the value_string array to determine the fastest possible
413 * - Verifies that the value_string contains no NULL string pointers.
414 * - Verifies that the value_string is terminated by {0, NULL}
417 _try_val_to_str_ext_init(const guint32 val, value_string_ext *vse)
419 const value_string *vs_p = vse->_vs_p;
420 const guint vs_num_entries = vse->_vs_num_entries;
422 /* The matching algorithm used:
423 * VS_SEARCH - slow sequential search (as in a normal value string)
424 * VS_BIN_TREE - log(n)-time binary search, the values must be sorted
425 * VS_INDEX - constant-time index lookup, the values must be contiguous
427 enum { VS_SEARCH, VS_BIN_TREE, VS_INDEX } type = VS_INDEX;
429 /* Note: The value_string 'value' is *unsigned*, but we do a little magic
430 * to help with value strings that have negative values.
432 * { -3, -2, -1, 0, 1, 2 }
433 * will be treated as "ascending ordered" (although it isn't technically),
434 * thus allowing constant-time index search
436 * { -3, -2, 0, 1, 2 } and { -3, -2, -1, 0, 2 }
437 * will both be considered as "out-of-order with gaps", thus falling
438 * back to the slow linear search
440 * { 0, 1, 2, -3, -2 } and { 0, 2, -3, -2, -1 }
441 * will be considered "ascending ordered with gaps" thus allowing
442 * a log(n)-time 'binary' search
444 * If you're confused, think of how negative values are represented, or
445 * google two's complement.
452 DISSECTOR_ASSERT((vs_p[vs_num_entries].value == 0) &&
453 (vs_p[vs_num_entries].strptr == NULL));
455 vse->_vs_first_value = vs_p[0].value;
456 first_value = vs_p[0].value;
457 prev_value = first_value;
459 for (i = 0; i < vs_num_entries; i++) {
460 DISSECTOR_ASSERT(vs_p[i].strptr != NULL);
461 if ((type == VS_INDEX) && (vs_p[i].value != (i + first_value))) {
464 /* XXX: Should check for dups ?? */
465 if (type == VS_BIN_TREE) {
466 if (prev_value > vs_p[i].value) {
467 g_warning("Extended value string '%s' forced to fall back to linear search:\n"
468 " entry %u, value %u [%#x] < previous entry, value %u [%#x]",
469 vse->_vs_name, i, vs_p[i].value, vs_p[i].value, prev_value, prev_value);
473 if (first_value > vs_p[i].value) {
474 g_warning("Extended value string '%s' forced to fall back to linear search:\n"
475 " entry %u, value %u [%#x] < first entry, value %u [%#x]",
476 vse->_vs_name, i, vs_p[i].value, vs_p[i].value, first_value, first_value);
482 prev_value = vs_p[i].value;
487 vse->_vs_match2 = _try_val_to_str_linear;
490 vse->_vs_match2 = _try_val_to_str_bsearch;
493 vse->_vs_match2 = _try_val_to_str_index;
496 g_assert_not_reached();
500 return vse->_vs_match2(val, vse);
503 /* STRING TO STRING MATCHING */
505 /* string_string is like value_string except the values being matched are
506 * also strings (instead of unsigned integers) */
508 /* Like val_to_str except for string_string */
510 str_to_str(const gchar *val, const string_string *vs, const char *fmt)
514 DISSECTOR_ASSERT(fmt != NULL);
516 ret = try_str_to_str(val, vs);
520 return wmem_strdup_printf(wmem_packet_scope(), fmt, val);
523 /* Like try_val_to_str_idx except for string_string */
525 try_str_to_str_idx(const gchar *val, const string_string *vs, gint *idx)
530 while (vs[i].strptr) {
531 if (!strcmp(vs[i].value,val)) {
533 return(vs[i].strptr);
543 /* Like try_val_to_str except for string_string */
545 try_str_to_str(const gchar *val, const string_string *vs)
548 return try_str_to_str_idx(val, vs, &ignore_me);
551 /* RANGE TO STRING MATCHING */
553 /* range_string is like value_string except the values being matched are
554 * integer ranges (for example, 0-10, 11-19, etc.) instead of single values. */
556 /* Like val_to_str except for range_string */
558 rval_to_str(const guint32 val, const range_string *rs, const char *fmt)
560 const gchar *ret = NULL;
562 DISSECTOR_ASSERT(fmt != NULL);
564 ret = try_rval_to_str(val, rs);
568 return wmem_strdup_printf(wmem_packet_scope(), fmt, val);
571 /* Like val_to_str_const except for range_string */
573 rval_to_str_const(const guint32 val, const range_string *rs,
574 const char *unknown_str)
576 const gchar *ret = NULL;
578 DISSECTOR_ASSERT(unknown_str != NULL);
580 ret = try_rval_to_str(val, rs);
587 /* Like try_val_to_str_idx except for range_string */
589 try_rval_to_str_idx(const guint32 val, const range_string *rs, gint *idx)
594 while(rs[i].strptr) {
595 if( (val >= rs[i].value_min) && (val <= rs[i].value_max) ) {
597 return (rs[i].strptr);
607 /* Like try_val_to_str except for range_string */
609 try_rval_to_str(const guint32 val, const range_string *rs)
612 return try_rval_to_str_idx(val, rs, &ignore_me);
617 /* Functions for use by proto_registrar_dump_values(), see proto.c */
620 value_string_ext_validate(const value_string_ext *vse)
624 #ifndef _WIN32 /* doesn't work on Windows for refs from another DLL ?? */
625 if ((vse->_vs_match2 != _try_val_to_str_ext_init) &&
626 (vse->_vs_match2 != _try_val_to_str_linear) &&
627 (vse->_vs_match2 != _try_val_to_str_bsearch) &&
628 (vse->_vs_match2 != _try_val_to_str_index))
635 value_string_ext_match_type_str(const value_string_ext *vse)
637 if (vse->_vs_match2 == _try_val_to_str_ext_init)
638 return "[Not Initialized]";
639 if (vse->_vs_match2 == _try_val_to_str_linear)
640 return "[Linear Search]";
641 if (vse->_vs_match2 == _try_val_to_str_bsearch)
642 return "[Binary Search]";
643 if (vse->_vs_match2 == _try_val_to_str_index)
644 return "[Direct (indexed) Access]";
649 * Editor modelines - http://www.wireshark.org/tools/modelines.html
654 * indent-tabs-mode: nil
657 * vi: set shiftwidth=4 tabstop=8 expandtab:
658 * :indentSize=4:tabSize=8:noTabs=true: