6 * Dick Gooris <gooris@lucent.com>
7 * Ulf Lamping <ulf.lamping@web.de>
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 #include <epan/frame_data.h>
40 #include <epan/range.h>
41 #include <epan/emem.h>
45 * Size of the header of a range_t.
47 #define RANGE_HDR_SIZE (sizeof (range_t) - sizeof (range_admin_t))
49 /* Allocate an empty range. */
50 range_t *range_empty(void)
54 range = g_malloc(RANGE_HDR_SIZE);
59 /******************** Range Entry Parser *********************************/
61 /* Converts a range string to a fast comparable array of ranges.
62 * The parameter 'es' points to the string to be converted.
63 * The parameter 'max_value' specifies the maximum value in a
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.
72 * In case of a single number, we make a range where low is equal to high.
73 * We take care on wrongly entered ranges; opposite order will be taken
76 * The following syntax is accepted :
78 * 1-20,30-40 Range from 1 to 20, and packets 30 to 40
79 * -20,30 Range from 1 to 20, and packet 30
80 * 20,30,40- 20, 30, and the range from 40 to the end
81 * 20-10,30-25 Range from 10 to 20, and from 25 to 30
85 convert_ret_t range_convert_str(range_t **rangep, const gchar *es,
97 /* Allocate a range; this has room for one subrange. */
98 range = g_malloc(RANGE_HDR_SIZE + sizeof (range_admin_t));
102 /* Process the ranges separately until we get a comma or end of string.
104 * We build a structure array called ranges of high and low values. After the
105 * following loop, we have the nranges variable which tells how many ranges
106 * were found. The number of individual ranges is limited to 'MaxRanges'
111 /* Skip white space. */
112 while ((c = *p) == ' ' || c == '\t')
117 /* This must be a subrange. Make sure we have room for it. */
118 if (range->nranges >= nranges) {
119 /* Grow the structure.
120 * 4 is an arbitrarily chosen number.
121 * We start with 1, under the assumption that people
122 * will often give a single number or range, and then
123 * proceed to keep it a multiple of 4.
129 range = g_realloc(range, RANGE_HDR_SIZE +
130 nranges*sizeof (range_admin_t));
134 /* Subrange starts with 1. */
135 range->ranges[range->nranges].low = 1;
136 } else if (isdigit((unsigned char)c)) {
137 /* Subrange starts with the specified number */
139 val = strtoul(p, &endp, 10);
141 /* That wasn't a valid number. */
143 return CVT_SYNTAX_ERROR;
145 if (errno == ERANGE || val > G_MAXUINT32) {
146 /* That was valid, but it's too big. */
148 return CVT_NUMBER_TOO_BIG;
151 range->ranges[range->nranges].low = val;
153 /* Skip white space. */
154 while ((c = *p) == ' ' || c == '\t')
157 /* Neither empty nor a number. */
159 return CVT_SYNTAX_ERROR;
163 /* There's a hyphen in the range. Skip past it. */
166 /* Skip white space. */
167 while ((c = *p) == ' ' || c == '\t')
170 if (c == ',' || c == '\0') {
171 /* End of subrange string; that means the subrange ends
174 range->ranges[range->nranges].high = max_value;
175 } else if (isdigit((unsigned char)c)) {
176 /* Subrange ends with the specified number. */
178 val = strtoul(p, &endp, 10);
180 /* That wasn't a valid number. */
182 return CVT_SYNTAX_ERROR;
184 if (errno == ERANGE || val > G_MAXUINT32) {
185 /* That was valid, but it's too big. */
187 return CVT_NUMBER_TOO_BIG;
190 range->ranges[range->nranges].high = val;
192 /* Skip white space. */
193 while ((c = *p) == ' ' || c == '\t')
196 /* Neither empty nor a number. */
198 return CVT_SYNTAX_ERROR;
200 } else if (c == ',' || c == '\0') {
201 /* End of subrange string; that means there's no hyphen
202 * in the subrange, so the start and the end are the same.
204 range->ranges[range->nranges].high = range->ranges[range->nranges].low;
206 /* Invalid character. */
208 return CVT_SYNTAX_ERROR;
213 /* Subrange is followed by a comma; skip it. */
218 /* Now we are going through the low and high values, and check
219 * whether they are in a proper order. Low should be equal or lower
220 * than high. So, go through the loop and swap if needed.
222 for (i=0; i < range->nranges; i++) {
223 if (range->ranges[i].low > range->ranges[i].high) {
224 tmp = range->ranges[i].low;
225 range->ranges[i].low = range->ranges[i].high;
226 range->ranges[i].high = tmp;
230 /* In case we want to know what the result ranges are :
232 * for (i=0; i < range->nranges; i++) {
233 * printf("Function : range_convert_str L=%u \t H=%u\n",range->ranges[i].low,range->ranges[i].high);
239 } /* range_convert_str */
241 /* This function returns TRUE if a given value is within one of the ranges
242 * stored in the ranges array.
244 gboolean value_is_in_range(range_t *range, guint32 val)
249 for (i=0; i < range->nranges; i++) {
250 if (val >= range->ranges[i].low && val <= range->ranges[i].high)
257 /* This function returns TRUE if the two given range_t's are equal.
259 gboolean ranges_are_equal(range_t *a, range_t *b)
263 if (a->nranges != b->nranges)
266 for (i=0; i < a->nranges; i++) {
267 if (a->ranges[i].low != b->ranges[i].low)
270 if (a->ranges[i].high != b->ranges[i].high)
278 /* This function calls the provided callback function for each value in
282 range_foreach(range_t *range, void (*callback)(guint32 val))
286 for (i=0; i < range->nranges; i++) {
287 for (j = range->ranges[i].low; j <= range->ranges[i].high; j++)
292 /* This function converts a range_t to a (ep_alloc()-allocated) string. */
294 range_convert_range(range_t *range)
297 gboolean prepend_comma = FALSE;
298 emem_strbuf_t *strbuf;
300 strbuf=ep_strbuf_new(NULL);
302 for (i=0; i < range->nranges; i++) {
303 if (range->ranges[i].low == range->ranges[i].high) {
304 ep_strbuf_append_printf(strbuf, "%s%u", prepend_comma?",":"", range->ranges[i].low);
306 ep_strbuf_append_printf(strbuf, "%s%u-%u", prepend_comma?",":"", range->ranges[i].low, range->ranges[i].high);
308 prepend_comma = TRUE;
314 /* Create a copy of a range. */
315 range_t *range_copy(range_t *src)
320 range_size = RANGE_HDR_SIZE + src->nranges*sizeof (range_admin_t);
321 dst = g_malloc(range_size);
322 memcpy(dst, src, range_size);
327 /* This is a debug function to check the range functionality */
328 static void value_is_in_range_check(range_t *range, guint32 val)
331 /* Print the result for a given value */
332 printf("Function : value_is_in_range_check Number %u\t",val);
334 if (value_is_in_range(range, val)) {
335 printf("is in range\n");
337 printf("is not in range\n");