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 * GLib 1.2[.x] doesn't define G_MAXUINT32; if it's not defined, we define
46 * it as the maximum 32-bit unsigned number.
49 #define G_MAXUINT32 ((guint32)0xFFFFFFFFU)
53 * Size of the header of a range_t.
55 #define RANGE_HDR_SIZE (sizeof (range_t) - sizeof (range_admin_t))
57 /* Allocate an empty range. */
58 range_t *range_empty(void)
62 range = g_malloc(RANGE_HDR_SIZE);
67 /******************** Range Entry Parser *********************************/
69 /* Converts a range string to a fast comparable array of ranges.
70 * The parameter 'es' points to the string to be converted.
71 * The parameter 'max_value' specifies the maximum value in a
74 * This function allocates a range_t large enough to hold the number
75 * of ranges specified, and fills the array range->ranges containing
76 * low and high values with the number of ranges being range->nranges.
77 * After having called this function, the function value_is_in_range()
78 * determines whether a given number is within the range or not.
80 * In case of a single number, we make a range where low is equal to high.
81 * We take care on wrongly entered ranges; opposite order will be taken
84 * The following syntax is accepted :
86 * 1-20,30-40 Range from 1 to 20, and packets 30 to 40
87 * -20,30 Range from 1 to 20, and packet 30
88 * 20,30,40- 20, 30, and the range from 40 to the end
89 * 20-10,30-25 Range from 10 to 20, and from 25 to 30
93 convert_ret_t range_convert_str(range_t **rangep, const gchar *es,
105 /* Allocate a range; this has room for one subrange. */
106 range = g_malloc(RANGE_HDR_SIZE + sizeof (range_admin_t));
110 /* Process the ranges separately until we get a comma or end of string.
112 * We build a structure array called ranges of high and low values. After the
113 * following loop, we have the nranges variable which tells how many ranges
114 * were found. The number of individual ranges is limited to 'MaxRanges'
119 /* Skip white space. */
120 while ((c = *p) == ' ' || c == '\t')
125 /* This must be a subrange. Make sure we have room for it. */
126 if (range->nranges >= nranges) {
127 /* Grow the structure.
128 * 4 is an arbitrarily chosen number.
129 * We start with 1, under the assumption that people
130 * will often give a single number or range, and then
131 * proceed to keep it a multiple of 4.
137 range = g_realloc(range, RANGE_HDR_SIZE +
138 nranges*sizeof (range_admin_t));
142 /* Subrange starts with 1. */
143 range->ranges[range->nranges].low = 1;
144 } else if (isdigit((unsigned char)c)) {
145 /* Subrange starts with the specified number */
147 val = strtoul(p, &endp, 10);
149 /* That wasn't a valid number. */
151 return CVT_SYNTAX_ERROR;
153 if (errno == ERANGE || val > G_MAXUINT32) {
154 /* That was valid, but it's too big. */
156 return CVT_NUMBER_TOO_BIG;
159 range->ranges[range->nranges].low = val;
161 /* Skip white space. */
162 while ((c = *p) == ' ' || c == '\t')
165 /* Neither empty nor a number. */
167 return CVT_SYNTAX_ERROR;
171 /* There's a hyphen in the range. Skip past it. */
174 /* Skip white space. */
175 while ((c = *p) == ' ' || c == '\t')
178 if (c == ',' || c == '\0') {
179 /* End of subrange string; that means the subrange ends
182 range->ranges[range->nranges].high = max_value;
183 } else if (isdigit((unsigned char)c)) {
184 /* Subrange ends with the specified number. */
186 val = strtoul(p, &endp, 10);
188 /* That wasn't a valid number. */
190 return CVT_SYNTAX_ERROR;
192 if (errno == ERANGE || val > G_MAXUINT32) {
193 /* That was valid, but it's too big. */
195 return CVT_NUMBER_TOO_BIG;
198 range->ranges[range->nranges].high = val;
200 /* Skip white space. */
201 while ((c = *p) == ' ' || c == '\t')
204 /* Neither empty nor a number. */
206 return CVT_SYNTAX_ERROR;
208 } else if (c == ',' || c == '\0') {
209 /* End of subrange string; that means there's no hyphen
210 * in the subrange, so the start and the end are the same.
212 range->ranges[range->nranges].high = range->ranges[range->nranges].low;
214 /* Invalid character. */
216 return CVT_SYNTAX_ERROR;
221 /* Subrange is followed by a comma; skip it. */
226 /* Now we are going through the low and high values, and check
227 * whether they are in a proper order. Low should be equal or lower
228 * than high. So, go through the loop and swap if needed.
230 for (i=0; i < range->nranges; i++) {
231 if (range->ranges[i].low > range->ranges[i].high) {
232 tmp = range->ranges[i].low;
233 range->ranges[i].low = range->ranges[i].high;
234 range->ranges[i].high = tmp;
238 /* In case we want to know what the result ranges are :
240 * for (i=0; i < range->nranges; i++) {
241 * printf("Function : range_convert_str L=%u \t H=%u\n",range->ranges[i].low,range->ranges[i].high);
247 } /* range_convert_str */
249 /* This function returns TRUE if a given value is within one of the ranges
250 * stored in the ranges array.
252 gboolean value_is_in_range(range_t *range, guint32 val)
256 for (i=0; i < range->nranges; i++) {
257 if (val >= range->ranges[i].low && val <= range->ranges[i].high)
263 /* This function returns TRUE if the two given range_t's are equal.
265 gboolean ranges_are_equal(range_t *a, range_t *b)
269 if (a->nranges != b->nranges)
272 for (i=0; i < a->nranges; i++) {
273 if (a->ranges[i].low != b->ranges[i].low)
276 if (a->ranges[i].high != b->ranges[i].high)
284 /* This function calls the provided callback function for each value in
288 range_foreach(range_t *range, void (*callback)(guint32 val))
292 for (i=0; i < range->nranges; i++) {
293 for (j = range->ranges[i].low; j <= range->ranges[i].high; j++)
298 /* This function converts a range_t to a (ep_alloc()-allocated) string. */
300 range_convert_range(range_t *range)
303 gboolean prepend_comma = FALSE;
306 string=ep_alloc(128);
310 for (i=0; i < range->nranges; i++) {
311 if (range->ranges[i].low == range->ranges[i].high)
312 str += g_snprintf(str, 128-(str-string), "%s%u", prepend_comma?",":"", range->ranges[i].low);
314 str += g_snprintf(str, 128-(str-string), "%s%u-%u", prepend_comma?",":"", range->ranges[i].low, range->ranges[i].high);
315 prepend_comma = TRUE;
321 /* Create a copy of a range. */
322 range_t *range_copy(range_t *src)
327 range_size = RANGE_HDR_SIZE + src->nranges*sizeof (range_admin_t);
328 dst = g_malloc(range_size);
329 memcpy(dst, src, range_size);
334 /* This is a debug function to check the range functionality */
335 static void value_is_in_range_check(range_t *range, guint32 val)
338 /* Print the result for a given value */
339 printf("Function : value_is_in_range_check Number %u\t",val);
341 if (value_is_in_range(range, val)) {
342 printf("is in range\n");
344 printf("is not in range\n");