Detect clang and llvm-gcc.
[obnox/wireshark/wip.git] / epan / range.c
1 /* range.c
2  * Range routines
3  *
4  * $Id$
5  *
6  * Dick Gooris <gooris@lucent.com>
7  * Ulf Lamping <ulf.lamping@web.de>
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
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.
17  *
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.
22  *
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.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <string.h>
33 #include <ctype.h>
34 #include <errno.h>
35
36 #include <glib.h>
37
38 #include <epan/frame_data.h>
39
40 #include <epan/range.h>
41 #include <epan/emem.h>
42 #include <stdio.h>
43
44 /*
45  * Size of the header of a range_t.
46  */
47 #define RANGE_HDR_SIZE (sizeof (range_t) - sizeof (range_admin_t))
48
49 /* Allocate an empty range. */
50 range_t *range_empty(void)
51 {
52    range_t *range;
53
54    range = g_malloc(RANGE_HDR_SIZE);
55    range->nranges = 0;
56    return range;
57 }
58
59 /******************** Range Entry Parser *********************************/
60
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
64  * range.
65  *
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. 
71  *
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
74  * care of.
75  * 
76  * The following syntax is accepted :
77  *
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
82  *   -              All values
83  */
84
85 convert_ret_t range_convert_str(range_t **rangep, const gchar *es,
86                                 guint32 max_value)
87 {
88    range_t       *range;
89    guint         nranges;
90    const gchar   *p;
91    char          *endp;
92    gchar         c;
93    guint         i;
94    guint32       tmp;
95    unsigned long val;
96
97    /* Allocate a range; this has room for one subrange. */
98    range = g_malloc(RANGE_HDR_SIZE + sizeof (range_admin_t));
99    range->nranges = 0;
100    nranges = 1;
101
102    /* Process the ranges separately until we get a comma or end of string.
103     *
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'
107     */
108
109    p = es;
110    for (;;) {
111       /* Skip white space. */
112       while ((c = *p) == ' ' || c == '\t')
113          p++;
114       if (c == '\0')
115          break;
116
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.
124           */
125          if (nranges == 1)
126             nranges = 4;
127          else
128             nranges += 4;
129          range = g_realloc(range, RANGE_HDR_SIZE +
130                            nranges*sizeof (range_admin_t));
131       }
132
133       if (c == '-') {
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 */
138          errno = 0;
139          val = strtoul(p, &endp, 10);
140          if (p == endp) {
141             /* That wasn't a valid number. */
142             g_free(range);
143             return CVT_SYNTAX_ERROR;
144          }
145          if (errno == ERANGE || val > G_MAXUINT32) {
146             /* That was valid, but it's too big. */
147             g_free(range);
148             return CVT_NUMBER_TOO_BIG;
149          } 
150          p = endp;
151          range->ranges[range->nranges].low = val;
152
153          /* Skip white space. */
154          while ((c = *p) == ' ' || c == '\t')
155             p++;
156       } else {
157          /* Neither empty nor a number. */
158          g_free(range);
159          return CVT_SYNTAX_ERROR;
160       }
161
162       if (c == '-') {
163          /* There's a hyphen in the range.  Skip past it. */
164          p++;
165
166          /* Skip white space. */
167          while ((c = *p) == ' ' || c == '\t')
168             p++;
169
170          if (c == ',' || c == '\0') {
171            /* End of subrange string; that means the subrange ends
172             * with max_value.
173             */
174            range->ranges[range->nranges].high = max_value;
175          } else if (isdigit((unsigned char)c)) {
176             /* Subrange ends with the specified number. */
177             errno = 0;
178             val = strtoul(p, &endp, 10);
179             if (p == endp) {
180                /* That wasn't a valid number. */
181                g_free(range);
182                return CVT_SYNTAX_ERROR;
183             }
184             if (errno == ERANGE || val > G_MAXUINT32) {
185                /* That was valid, but it's too big. */
186                g_free(range);
187                return CVT_NUMBER_TOO_BIG;
188             } 
189             p = endp;
190             range->ranges[range->nranges].high = val;
191
192             /* Skip white space. */
193             while ((c = *p) == ' ' || c == '\t')
194                p++;
195          } else {
196             /* Neither empty nor a number. */
197             g_free(range);
198             return CVT_SYNTAX_ERROR;
199          }
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.
203           */
204          range->ranges[range->nranges].high = range->ranges[range->nranges].low;
205       } else {
206          /* Invalid character. */ 
207          g_free(range);
208          return CVT_SYNTAX_ERROR;
209       }
210       range->nranges++;
211
212       if (c == ',') {
213          /* Subrange is followed by a comma; skip it. */
214          p++;
215       }
216    }
217
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.
221     */
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;
227       }
228    }
229
230    /* In case we want to know what the result ranges are :
231     *
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);
234     * }
235     *
236     */
237    *rangep = range;
238    return CVT_NO_ERROR;
239 } /* range_convert_str */
240
241 /* This function returns TRUE if a given value is within one of the ranges
242  * stored in the ranges array.
243  */
244 gboolean value_is_in_range(range_t *range, guint32 val)
245 {
246    guint i;
247
248    if (range) {
249       for (i=0; i < range->nranges; i++) {
250          if (val >= range->ranges[i].low && val <= range->ranges[i].high)
251              return TRUE;
252       }
253    }
254    return(FALSE);
255 }
256
257 /* This function returns TRUE if the two given range_t's are equal.
258  */
259 gboolean ranges_are_equal(range_t *a, range_t *b)
260 {
261    guint i;
262
263    if (a->nranges != b->nranges)
264       return FALSE;
265
266    for (i=0; i < a->nranges; i++) {
267       if (a->ranges[i].low != b->ranges[i].low)
268          return FALSE;
269
270       if (a->ranges[i].high != b->ranges[i].high)
271          return FALSE;
272    }
273
274    return TRUE;
275
276 }
277
278 /* This function calls the provided callback function for each value in
279  * in the range.
280  */
281 void
282 range_foreach(range_t *range, void (*callback)(guint32 val))
283 {
284    guint32 i, j;
285
286    for (i=0; i < range->nranges; i++) {
287       for (j = range->ranges[i].low; j <= range->ranges[i].high; j++)
288          callback(j);
289    }
290 }
291
292 /* This function converts a range_t to a (ep_alloc()-allocated) string.  */
293 char *
294 range_convert_range(range_t *range)
295 {
296    guint32 i;
297    gboolean prepend_comma = FALSE;
298    emem_strbuf_t *strbuf;
299
300    strbuf=ep_strbuf_new(NULL);
301
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);
305       } else {
306          ep_strbuf_append_printf(strbuf, "%s%u-%u", prepend_comma?",":"", range->ranges[i].low, range->ranges[i].high);
307       }
308       prepend_comma = TRUE;
309    }
310
311    return strbuf->str;
312 }
313
314 /* Create a copy of a range. */
315 range_t *range_copy(range_t *src)
316 {
317    range_t *dst;
318    size_t range_size;
319
320    range_size = RANGE_HDR_SIZE + src->nranges*sizeof (range_admin_t);
321    dst = g_malloc(range_size);
322    memcpy(dst, src, range_size);
323    return dst;
324 }
325
326 #if 0
327 /* This is a debug function to check the range functionality */
328 static void value_is_in_range_check(range_t *range, guint32 val)
329 {
330
331   /* Print the result for a given value */
332   printf("Function : value_is_in_range_check Number %u\t",val);
333
334   if (value_is_in_range(range, val)) {
335      printf("is in range\n");
336   } else {
337      printf("is not in range\n");
338   }
339 }
340 #endif
341