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