Add "Editor modelines"; Adjust whitespace as needed.
[metze/wireshark/wip.git] / epan / range.h
1 /* range.h
2  * Range routines
3  *
4  * Dick Gooris <gooris@lucent.com>
5  * Ulf Lamping <ulf.lamping@web.de>
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #ifndef __RANGE_H__
27 #define __RANGE_H__
28
29 #include <glib.h>
30 #include "ws_symbol_export.h"
31
32 /** @file
33  * Range strings a variant of value_strings
34  */
35
36 /**@todo where's the best place for these? */
37 #define MAX_SCTP_PORT 65535
38 #define MAX_TCP_PORT 65535
39 #define MAX_UDP_PORT 65535
40 #define MAX_DCCP_PORT 65535
41
42 typedef struct range_admin_tag {
43     guint32 low;
44     guint32 high;
45 } range_admin_t;
46
47 /** user specified range(s) */
48 typedef struct epan_range {
49     guint           nranges;   /**< number of entries in ranges */
50     range_admin_t   ranges[1]; /**< variable-length array */
51 } range_t;
52
53 /**
54  * Return value from range_convert_str().
55  */
56 typedef enum {
57     CVT_NO_ERROR,
58     CVT_SYNTAX_ERROR,
59     CVT_NUMBER_TOO_BIG
60 } convert_ret_t;
61
62 WS_DLL_PUBLIC range_t *range_empty(void);
63
64
65 /*** Converts a range string to a fast comparable array of ranges.
66  * This function allocates a range_t large enough to hold the number
67  * of ranges specified, and fills the array range->ranges containing
68  * low and high values with the number of ranges being range->nranges.
69  * After having called this function, the function value_is_in_range()
70  * determines whether a given number is within the range or not.<BR>
71  * In case of a single number, we make a range where low is equal to high.
72  * We take care on wrongly entered ranges; opposite order will be taken
73  * care of.
74  *
75  * The following syntax is accepted :
76  *
77  *   1-20,30-40     Range from 1 to 20, and packets 30 to 40
78  *   -20,30         Range from 1 to 20, and packet 30
79  *   20,30,40-      20, 30, and the range from 40 to the end
80  *   20-10,30-25    Range from 10 to 20, and from 25 to 30
81  *   -              All values
82  * @param range the range
83  * @param es points to the string to be converted.
84  * @param max_value specifies the maximum value in a range.
85  * @return convert_ret_t
86  */
87 WS_DLL_PUBLIC convert_ret_t range_convert_str(range_t **range, const gchar *es,
88     guint32 max_value);
89
90 convert_ret_t range_convert_str_work(range_t **range, const gchar *es,
91     guint32 max_value, gboolean err_on_max);
92
93 /** This function returns TRUE if a given value is within one of the ranges
94  * stored in the ranges array.
95  * @param range the range
96  * @param val the value to check
97  * @return TRUE if the value is in range
98  */
99 WS_DLL_PUBLIC gboolean value_is_in_range(range_t *range, guint32 val);
100
101 /** This function returns TRUE if the two given range_t's are equal.
102  * @param a first range
103  * @param b second range
104  * @return TRUE if the value is in range
105  */
106 WS_DLL_PUBLIC gboolean ranges_are_equal(range_t *a, range_t *b);
107
108 /** This function calls the provided callback function for each value in
109  * in the range.
110  * @param range the range
111  * @param callback the callback function
112  */
113 WS_DLL_PUBLIC void range_foreach(range_t *range, void (*callback)(guint32 val));
114
115 /**
116  * This function converts a range_t to a (ep_alloc()-allocated) string.
117  */
118 WS_DLL_PUBLIC char *range_convert_range(range_t *range);
119
120 /**
121  * Create a copy of a range.
122  * @param src the range to copy
123  * @return ep allocated copy of the range
124  */
125 WS_DLL_PUBLIC range_t *range_copy(range_t *src);
126
127 #endif /* __RANGE_H__ */