4 * Dick Gooris <gooris@lucent.com>
5 * Ulf Lamping <ulf.lamping@web.de>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
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.
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.
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.
34 #include <epan/frame_data.h>
36 #include <epan/range.h>
40 * Size of the header of a range_t.
42 #define RANGE_HDR_SIZE (sizeof (range_t) - sizeof (range_admin_t))
44 /* Allocate an empty range. */
45 range_t *range_empty(void)
49 range = (range_t *)g_malloc(RANGE_HDR_SIZE);
54 /******************** Range Entry Parser *********************************/
56 /* Converts a range string to a fast comparable array of ranges.
57 * The parameter 'es' points to the string to be converted.
58 * The parameter 'max_value' specifies the maximum value in a
61 * This function allocates a range_t large enough to hold the number
62 * of ranges specified, and fills the array range->ranges containing
63 * low and high values with the number of ranges being range->nranges.
64 * After having called this function, the function value_is_in_range()
65 * determines whether a given number is within the range or not.
67 * In case of a single number, we make a range where low is equal to high.
68 * We take care on wrongly entered ranges; opposite order will be taken
71 * The following syntax is accepted :
73 * 1-20,30-40 Range from 1 to 20, and packets 30 to 40
74 * -20,30 Range from 1 to 20, and packet 30
75 * 20,30,40- 20, 30, and the range from 40 to the end
76 * 20-10,30-25 Range from 10 to 20, and from 25 to 30
81 range_convert_str(range_t **rangep, const gchar *es, guint32 max_value)
83 return range_convert_str_work(rangep, es, max_value, TRUE);
86 /* This version of range_convert_str() allows the caller to specify whether
87 * values in excess of the range's specified maximum should cause an error or
88 * be silently lowered.
89 * XXX - both the function and the variable could probably use better names.
92 range_convert_str_work(range_t **rangep, const gchar *es, guint32 max_value,
105 if ( (rangep == NULL) || (es == NULL) )
106 return CVT_SYNTAX_ERROR;
108 /* Allocate a range; this has room for one subrange. */
109 range = (range_t *)g_malloc(RANGE_HDR_SIZE + sizeof (range_admin_t));
113 /* Process the ranges separately until we get a comma or end of string.
115 * We build a structure array called ranges of high and low values. After the
116 * following loop, we have the nranges variable which tells how many ranges
117 * were found. The number of individual ranges is limited to 'MaxRanges'
122 /* Skip white space. */
123 while ((c = *p) == ' ' || c == '\t')
128 /* This must be a subrange. Make sure we have room for it. */
129 if (range->nranges >= nranges) {
130 /* Grow the structure.
131 * 4 is an arbitrarily chosen number.
132 * We start with 1, under the assumption that people
133 * will often give a single number or range, and then
134 * proceed to keep it a multiple of 4.
140 range = (range_t *)g_realloc(range, RANGE_HDR_SIZE +
141 nranges*sizeof (range_admin_t));
145 /* Subrange starts with 1. */
146 range->ranges[range->nranges].low = 1;
147 } else if (g_ascii_isdigit(c)) {
148 /* Subrange starts with the specified number */
150 val = strtoul(p, &endp, 0);
152 /* That wasn't a valid number. */
154 return CVT_SYNTAX_ERROR;
156 if (errno == ERANGE || val > max_value) {
157 /* That was valid, but it's too big. Return an error if requested
158 * (e.g., except when reading from the preferences file).
162 return CVT_NUMBER_TOO_BIG;
164 /* Silently use the range's maximum value */
169 range->ranges[range->nranges].low = (guint32)val;
171 /* Skip white space. */
172 while ((c = *p) == ' ' || c == '\t')
175 /* Neither empty nor a number. */
177 return CVT_SYNTAX_ERROR;
181 /* There's a hyphen in the range. Skip past it. */
184 /* Skip white space. */
185 while ((c = *p) == ' ' || c == '\t')
188 if (c == ',' || c == '\0') {
189 /* End of subrange string; that means the subrange ends
192 range->ranges[range->nranges].high = max_value;
193 } else if (g_ascii_isdigit(c)) {
194 /* Subrange ends with the specified number. */
196 val = strtoul(p, &endp, 0);
198 /* That wasn't a valid number. */
200 return CVT_SYNTAX_ERROR;
202 if (errno == ERANGE || val > max_value) {
203 /* That was valid, but it's too big. Return an error if requested
204 * (e.g., except when reading from the preferences file).
208 return CVT_NUMBER_TOO_BIG;
210 /* Silently use the range's maximum value */
215 range->ranges[range->nranges].high = (guint32)val;
217 /* Skip white space. */
218 while ((c = *p) == ' ' || c == '\t')
221 /* Neither empty nor a number. */
223 return CVT_SYNTAX_ERROR;
225 } else if (c == ',' || c == '\0') {
226 /* End of subrange string; that means there's no hyphen
227 * in the subrange, so the start and the end are the same.
229 range->ranges[range->nranges].high = range->ranges[range->nranges].low;
231 /* Invalid character. */
233 return CVT_SYNTAX_ERROR;
238 /* Subrange is followed by a comma; skip it. */
243 /* Now we are going through the low and high values, and check
244 * whether they are in a proper order. Low should be equal or lower
245 * than high. So, go through the loop and swap if needed.
247 for (i=0; i < range->nranges; i++) {
248 if (range->ranges[i].low > range->ranges[i].high) {
249 tmp = range->ranges[i].low;
250 range->ranges[i].low = range->ranges[i].high;
251 range->ranges[i].high = tmp;
255 /* In case we want to know what the result ranges are :
257 * for (i=0; i < range->nranges; i++) {
258 * printf("Function : range_convert_str L=%u \t H=%u\n",range->ranges[i].low,range->ranges[i].high);
264 } /* range_convert_str */
266 /* This function returns TRUE if a given value is within one of the ranges
267 * stored in the ranges array.
270 value_is_in_range(range_t *range, guint32 val)
275 for (i=0; i < range->nranges; i++) {
276 if (val >= range->ranges[i].low && val <= range->ranges[i].high)
283 /* This function returns TRUE if the two given range_t's are equal.
286 ranges_are_equal(range_t *a, range_t *b)
290 if ( (a == NULL) || (b == NULL) )
293 if (a->nranges != b->nranges)
296 for (i=0; i < a->nranges; i++) {
297 if (a->ranges[i].low != b->ranges[i].low)
300 if (a->ranges[i].high != b->ranges[i].high)
308 /* This function calls the provided callback function for each value in
312 range_foreach(range_t *range, void (*callback)(guint32 val))
316 if (range && callback) {
317 for (i=0; i < range->nranges; i++) {
318 for (j = range->ranges[i].low; j <= range->ranges[i].high; j++)
324 /* This function converts a range_t to a (wmem-allocated) string. */
326 range_convert_range(wmem_allocator_t *scope, const range_t *range)
329 gboolean prepend_comma = FALSE;
330 wmem_strbuf_t *strbuf;
332 strbuf=wmem_strbuf_new(scope, "");
335 for (i=0; i < range->nranges; i++) {
336 if (range->ranges[i].low == range->ranges[i].high) {
337 wmem_strbuf_append_printf(strbuf, "%s%u", prepend_comma?",":"", range->ranges[i].low);
339 wmem_strbuf_append_printf(strbuf, "%s%u-%u", prepend_comma?",":"", range->ranges[i].low, range->ranges[i].high);
341 prepend_comma = TRUE;
344 return wmem_strbuf_finalize(strbuf);
347 /* Create a copy of a range. */
349 range_copy(range_t *src)
357 range_size = RANGE_HDR_SIZE + src->nranges*sizeof (range_admin_t);
358 dst = (range_t *)g_malloc(range_size);
359 memcpy(dst, src, range_size);
364 /* This is a debug function to check the range functionality */
366 value_is_in_range_check(range_t *range, guint32 val)
368 /* Print the result for a given value */
369 printf("Function : value_is_in_range_check Number %u\t",val);
371 if (value_is_in_range(range, val)) {
372 printf("is in range\n");
374 printf("is not in range\n");
380 * Editor modelines - http://www.wireshark.org/tools/modelines.html
385 * indent-tabs-mode: nil
388 * ex: set shiftwidth=3 tabstop=8 expandtab:
389 * :indentSize=3:tabSize=8:noTabs=true: