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
86 range_convert_str(range_t **rangep, const gchar *es, guint32 max_value)
88 return range_convert_str_work(rangep, es, max_value, TRUE);
91 /* This version of range_convert_str() allows the caller to specify whether
92 * values in excess of the range's specified maximum should cause an error or
93 * be silently lowered.
94 * XXX - both the function and the variable could probably use better names.
97 range_convert_str_work(range_t **rangep, const gchar *es, guint32 max_value,
110 if ( (rangep == NULL) || (es == NULL) )
111 return CVT_SYNTAX_ERROR;
113 /* Allocate a range; this has room for one subrange. */
114 range = g_malloc(RANGE_HDR_SIZE + sizeof (range_admin_t));
118 /* Process the ranges separately until we get a comma or end of string.
120 * We build a structure array called ranges of high and low values. After the
121 * following loop, we have the nranges variable which tells how many ranges
122 * were found. The number of individual ranges is limited to 'MaxRanges'
127 /* Skip white space. */
128 while ((c = *p) == ' ' || c == '\t')
133 /* This must be a subrange. Make sure we have room for it. */
134 if (range->nranges >= nranges) {
135 /* Grow the structure.
136 * 4 is an arbitrarily chosen number.
137 * We start with 1, under the assumption that people
138 * will often give a single number or range, and then
139 * proceed to keep it a multiple of 4.
145 range = g_realloc(range, RANGE_HDR_SIZE +
146 nranges*sizeof (range_admin_t));
150 /* Subrange starts with 1. */
151 range->ranges[range->nranges].low = 1;
152 } else if (isdigit((unsigned char)c)) {
153 /* Subrange starts with the specified number */
155 val = strtoul(p, &endp, 10);
157 /* That wasn't a valid number. */
159 return CVT_SYNTAX_ERROR;
161 if (errno == ERANGE || val > max_value) {
162 /* That was valid, but it's too big. Return an error if requested
163 * (e.g., except when reading from the preferences file).
167 return CVT_NUMBER_TOO_BIG;
169 /* Silently use the range's maximum value */
174 range->ranges[range->nranges].low = val;
176 /* Skip white space. */
177 while ((c = *p) == ' ' || c == '\t')
180 /* Neither empty nor a number. */
182 return CVT_SYNTAX_ERROR;
186 /* There's a hyphen in the range. Skip past it. */
189 /* Skip white space. */
190 while ((c = *p) == ' ' || c == '\t')
193 if (c == ',' || c == '\0') {
194 /* End of subrange string; that means the subrange ends
197 range->ranges[range->nranges].high = max_value;
198 } else if (isdigit((unsigned char)c)) {
199 /* Subrange ends with the specified number. */
201 val = strtoul(p, &endp, 10);
203 /* That wasn't a valid number. */
205 return CVT_SYNTAX_ERROR;
207 if (errno == ERANGE || val > max_value) {
208 /* That was valid, but it's too big. Return an error if requested
209 * (e.g., except when reading from the preferences file).
213 return CVT_NUMBER_TOO_BIG;
215 /* Silently use the range's maximum value */
220 range->ranges[range->nranges].high = val;
222 /* Skip white space. */
223 while ((c = *p) == ' ' || c == '\t')
226 /* Neither empty nor a number. */
228 return CVT_SYNTAX_ERROR;
230 } else if (c == ',' || c == '\0') {
231 /* End of subrange string; that means there's no hyphen
232 * in the subrange, so the start and the end are the same.
234 range->ranges[range->nranges].high = range->ranges[range->nranges].low;
236 /* Invalid character. */
238 return CVT_SYNTAX_ERROR;
243 /* Subrange is followed by a comma; skip it. */
248 /* Now we are going through the low and high values, and check
249 * whether they are in a proper order. Low should be equal or lower
250 * than high. So, go through the loop and swap if needed.
252 for (i=0; i < range->nranges; i++) {
253 if (range->ranges[i].low > range->ranges[i].high) {
254 tmp = range->ranges[i].low;
255 range->ranges[i].low = range->ranges[i].high;
256 range->ranges[i].high = tmp;
260 /* In case we want to know what the result ranges are :
262 * for (i=0; i < range->nranges; i++) {
263 * printf("Function : range_convert_str L=%u \t H=%u\n",range->ranges[i].low,range->ranges[i].high);
269 } /* range_convert_str */
271 /* This function returns TRUE if a given value is within one of the ranges
272 * stored in the ranges array.
275 value_is_in_range(range_t *range, guint32 val)
280 for (i=0; i < range->nranges; i++) {
281 if (val >= range->ranges[i].low && val <= range->ranges[i].high)
288 /* This function returns TRUE if the two given range_t's are equal.
291 ranges_are_equal(range_t *a, range_t *b)
295 if ( (a == NULL) || (b == NULL) )
298 if (a->nranges != b->nranges)
301 for (i=0; i < a->nranges; i++) {
302 if (a->ranges[i].low != b->ranges[i].low)
305 if (a->ranges[i].high != b->ranges[i].high)
313 /* This function calls the provided callback function for each value in
317 range_foreach(range_t *range, void (*callback)(guint32 val))
321 if (range && callback) {
322 for (i=0; i < range->nranges; i++) {
323 for (j = range->ranges[i].low; j <= range->ranges[i].high; j++)
329 /* This function converts a range_t to a (ep_alloc()-allocated) string. */
331 range_convert_range(range_t *range)
334 gboolean prepend_comma = FALSE;
335 emem_strbuf_t *strbuf;
337 strbuf=ep_strbuf_new(NULL);
340 for (i=0; i < range->nranges; i++) {
341 if (range->ranges[i].low == range->ranges[i].high) {
342 ep_strbuf_append_printf(strbuf, "%s%u", prepend_comma?",":"", range->ranges[i].low);
344 ep_strbuf_append_printf(strbuf, "%s%u-%u", prepend_comma?",":"", range->ranges[i].low, range->ranges[i].high);
346 prepend_comma = TRUE;
352 /* Create a copy of a range. */
354 range_copy(range_t *src)
362 range_size = RANGE_HDR_SIZE + src->nranges*sizeof (range_admin_t);
363 dst = g_malloc(range_size);
364 memcpy(dst, src, range_size);
369 /* This is a debug function to check the range functionality */
371 value_is_in_range_check(range_t *range, guint32 val)
373 /* Print the result for a given value */
374 printf("Function : value_is_in_range_check Number %u\t",val);
376 if (value_is_in_range(range, val)) {
377 printf("is in range\n");
379 printf("is not in range\n");