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 * SPDX-License-Identifier: GPL-2.0-or-later
22 #include <epan/frame_data.h>
24 #include <epan/range.h>
28 * Size of the header of a range_t.
30 #define RANGE_HDR_SIZE (sizeof (range_t) - sizeof (range_admin_t))
32 /* Allocate an empty range. */
33 range_t *range_empty(wmem_allocator_t *scope)
37 range = (range_t *)wmem_alloc(scope, RANGE_HDR_SIZE);
42 /******************** Range Entry Parser *********************************/
44 /* Converts a range string to a fast comparable array of ranges.
45 * The parameter 'es' points to the string to be converted.
46 * The parameter 'max_value' specifies the maximum value in a
49 * This function allocates a range_t large enough to hold the number
50 * of ranges specified, and fills the array range->ranges containing
51 * low and high values with the number of ranges being range->nranges.
52 * After having called this function, the function value_is_in_range()
53 * determines whether a given number is within the range or not.
55 * In case of a single number, we make a range where low is equal to high.
56 * We take care on wrongly entered ranges; opposite order will be taken
59 * The following syntax is accepted :
61 * 1-20,30-40 Range from 1 to 20, and packets 30 to 40
62 * -20,30 Range from 1 to 20, and packet 30
63 * 20,30,40- 20, 30, and the range from 40 to the end
64 * 20-10,30-25 Range from 10 to 20, and from 25 to 30
69 range_convert_str(wmem_allocator_t *scope, range_t **rangep, const gchar *es, guint32 max_value)
71 return range_convert_str_work(scope, rangep, es, max_value, TRUE);
74 /* This version of range_convert_str() allows the caller to specify whether
75 * values in excess of the range's specified maximum should cause an error or
76 * be silently lowered.
77 * XXX - both the function and the variable could probably use better names.
80 range_convert_str_work(wmem_allocator_t *scope, range_t **rangep, const gchar *es, guint32 max_value,
93 if ( (rangep == NULL) || (es == NULL) )
94 return CVT_SYNTAX_ERROR;
96 /* Allocate a range; this has room for one subrange. */
97 range = (range_t *)wmem_alloc(scope, RANGE_HDR_SIZE + sizeof (range_admin_t));
101 /* Process the ranges separately until we get a comma or end of string.
103 * We build a structure array called ranges of high and low values. After the
104 * following loop, we have the nranges variable which tells how many ranges
105 * were found. The number of individual ranges is limited to 'MaxRanges'
110 /* Skip white space. */
111 while ((c = *p) == ' ' || c == '\t')
116 /* This must be a subrange. Make sure we have room for it. */
117 if (range->nranges >= nranges) {
118 /* Grow the structure.
119 * 4 is an arbitrarily chosen number.
120 * We start with 1, under the assumption that people
121 * will often give a single number or range, and then
122 * proceed to keep it a multiple of 4.
128 range = (range_t *)wmem_realloc(scope, range, RANGE_HDR_SIZE +
129 nranges*sizeof (range_admin_t));
133 /* Subrange starts with 1. */
134 range->ranges[range->nranges].low = 1;
135 } else if (g_ascii_isdigit(c)) {
136 /* Subrange starts with the specified number */
138 val = strtoul(p, &endp, 0);
140 /* That wasn't a valid number. */
141 wmem_free(scope, range);
142 return CVT_SYNTAX_ERROR;
144 if (errno == ERANGE || val > max_value) {
145 /* That was valid, but it's too big. Return an error if requested
146 * (e.g., except when reading from the preferences file).
149 wmem_free(scope, range);
150 return CVT_NUMBER_TOO_BIG;
152 /* Silently use the range's maximum value */
157 range->ranges[range->nranges].low = (guint32)val;
159 /* Skip white space. */
160 while ((c = *p) == ' ' || c == '\t')
163 /* Neither empty nor a number. */
164 wmem_free(scope, range);
165 return CVT_SYNTAX_ERROR;
169 /* There's a hyphen in the range. Skip past it. */
172 /* Skip white space. */
173 while ((c = *p) == ' ' || c == '\t')
176 if (c == ',' || c == '\0') {
177 /* End of subrange string; that means the subrange ends
180 range->ranges[range->nranges].high = max_value;
181 } else if (g_ascii_isdigit(c)) {
182 /* Subrange ends with the specified number. */
184 val = strtoul(p, &endp, 0);
186 /* That wasn't a valid number. */
187 wmem_free(scope, range);
188 return CVT_SYNTAX_ERROR;
190 if (errno == ERANGE || val > max_value) {
191 /* That was valid, but it's too big. Return an error if requested
192 * (e.g., except when reading from the preferences file).
195 wmem_free(scope, range);
196 return CVT_NUMBER_TOO_BIG;
198 /* Silently use the range's maximum value */
203 range->ranges[range->nranges].high = (guint32)val;
205 /* Skip white space. */
206 while ((c = *p) == ' ' || c == '\t')
209 /* Neither empty nor a number. */
210 wmem_free(scope, range);
211 return CVT_SYNTAX_ERROR;
213 } else if (c == ',' || c == '\0') {
214 /* End of subrange string; that means there's no hyphen
215 * in the subrange, so the start and the end are the same.
217 range->ranges[range->nranges].high = range->ranges[range->nranges].low;
219 /* Invalid character. */
220 wmem_free(scope, range);
221 return CVT_SYNTAX_ERROR;
226 /* Subrange is followed by a comma; skip it. */
231 /* Now we are going through the low and high values, and check
232 * whether they are in a proper order. Low should be equal or lower
233 * than high. So, go through the loop and swap if needed.
235 for (i=0; i < range->nranges; i++) {
236 if (range->ranges[i].low > range->ranges[i].high) {
237 tmp = range->ranges[i].low;
238 range->ranges[i].low = range->ranges[i].high;
239 range->ranges[i].high = tmp;
243 /* In case we want to know what the result ranges are :
245 * for (i=0; i < range->nranges; i++) {
246 * printf("Function : range_convert_str L=%u \t H=%u\n",range->ranges[i].low,range->ranges[i].high);
252 } /* range_convert_str */
254 /* This function returns TRUE if a given value is within one of the ranges
255 * stored in the ranges array.
258 value_is_in_range(range_t *range, guint32 val)
263 for (i=0; i < range->nranges; i++) {
264 if (val >= range->ranges[i].low && val <= range->ranges[i].high)
271 /* This function returns TRUE if val has successfully been added to
272 * a range. This may extend an existing range or create a new one
275 range_add_value(wmem_allocator_t *scope, range_t **range, guint32 val)
279 if ((range) && (*range)) {
280 for (i=0; i < (*range)->nranges; i++) {
281 if (val >= (*range)->ranges[i].low && val <= (*range)->ranges[i].high)
284 if (val == (*range)->ranges[i].low-1)
286 /* Sink to a new low */
287 (*range)->ranges[i].low = val;
291 if (val == (*range)->ranges[i].high+1)
293 /* Reach a new high */
294 (*range)->ranges[i].high = val;
299 (*range) = (range_t *)wmem_realloc(scope, (*range), RANGE_HDR_SIZE +
300 ((*range)->nranges+1)*sizeof (range_admin_t));
302 (*range)->ranges[i].low = (*range)->ranges[i].high = val;
308 /* This function returns TRUE if val has successfully been removed from
309 * a range. This may delete an existing range
312 range_remove_value(wmem_allocator_t *scope, range_t **range, guint32 val)
317 if ((range) && (*range)) {
318 for (i=0; i < (*range)->nranges; i++) {
320 /* value is in the middle of the range, so it can't really be removed */
321 if (val > (*range)->ranges[i].low && val < (*range)->ranges[i].high)
324 if ((val == (*range)->ranges[i].low) && (val == (*range)->ranges[i].high))
326 /* Remove the range item entirely */
327 new_range = (range_t*)wmem_alloc(scope, RANGE_HDR_SIZE + ((*range)->nranges-1)*sizeof (range_admin_t));
328 new_range->nranges = (*range)->nranges-1;
329 for (j=0, new_j = 0; j < (*range)->nranges; j++) {
331 /* Skip the current range */
335 new_range->ranges[new_j].low = (*range)->ranges[j].low;
336 new_range->ranges[new_j].high = (*range)->ranges[j].high;
340 wmem_free(scope, *range);
345 if (val == (*range)->ranges[i].low)
348 (*range)->ranges[i].low++;
352 if (val == (*range)->ranges[i].high)
354 /* Reach a new high */
355 (*range)->ranges[i].high--;
364 /* This function returns TRUE if the two given range_t's are equal.
367 ranges_are_equal(range_t *a, range_t *b)
371 if ( (a == NULL) || (b == NULL) )
374 if (a->nranges != b->nranges)
377 for (i=0; i < a->nranges; i++) {
378 if (a->ranges[i].low != b->ranges[i].low)
381 if (a->ranges[i].high != b->ranges[i].high)
389 /* This function calls the provided callback function for each value in
393 range_foreach(range_t *range, void (*callback)(guint32 val, gpointer ptr), gpointer ptr)
397 if (range && callback) {
398 for (i=0; i < range->nranges; i++) {
399 for (j = range->ranges[i].low; j <= range->ranges[i].high; j++)
405 /* This function converts a range_t to a (wmem-allocated) string. */
407 range_convert_range(wmem_allocator_t *scope, const range_t *range)
410 gboolean prepend_comma = FALSE;
411 wmem_strbuf_t *strbuf;
413 strbuf=wmem_strbuf_new(scope, "");
416 for (i=0; i < range->nranges; i++) {
417 if (range->ranges[i].low == range->ranges[i].high) {
418 wmem_strbuf_append_printf(strbuf, "%s%u", prepend_comma?",":"", range->ranges[i].low);
420 wmem_strbuf_append_printf(strbuf, "%s%u-%u", prepend_comma?",":"", range->ranges[i].low, range->ranges[i].high);
422 prepend_comma = TRUE;
425 return wmem_strbuf_finalize(strbuf);
428 /* Create a copy of a range. */
430 range_copy(wmem_allocator_t *scope, range_t *src)
438 range_size = RANGE_HDR_SIZE + src->nranges*sizeof (range_admin_t);
439 dst = (range_t *)wmem_memdup(scope, src, range_size);
444 /* This is a debug function to check the range functionality */
446 value_is_in_range_check(range_t *range, guint32 val)
448 /* Print the result for a given value */
449 printf("Function : value_is_in_range_check Number %u\t",val);
451 if (value_is_in_range(range, val)) {
452 printf("is in range\n");
454 printf("is not in range\n");
460 * Editor modelines - http://www.wireshark.org/tools/modelines.html
465 * indent-tabs-mode: nil
468 * ex: set shiftwidth=3 tabstop=8 expandtab:
469 * :indentSize=3:tabSize=8:noTabs=true: