aboutsummaryrefslogtreecommitdiffstats
path: root/epan/range.c
blob: 0bce5f9deab7e6327a7eebf3e75e561247c0bd1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* range.c
 * Range routines
 *
 * Dick Gooris <gooris@lucent.com>
 * Ulf Lamping <ulf.lamping@web.de>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <glib.h>

#include <epan/frame_data.h>

#include <epan/range.h>
#include <stdio.h>

/*
 * Size of the header of a range_t.
 */
#define RANGE_HDR_SIZE (sizeof (range_t) - sizeof (range_admin_t))

/* Allocate an empty range. */
range_t *range_empty(void)
{
   range_t *range;

   range = (range_t *)g_malloc(RANGE_HDR_SIZE);
   range->nranges = 0;
   return range;
}

/******************** Range Entry Parser *********************************/

/* Converts a range string to a fast comparable array of ranges.
 * The parameter 'es' points to the string to be converted.
 * The parameter 'max_value' specifies the maximum value in a
 * range.
 *
 * This function allocates a range_t large enough to hold the number
 * of ranges specified, and fills the array range->ranges containing
 * low and high values with the number of ranges being range->nranges.
 * After having called this function, the function value_is_in_range()
 * determines whether a given number is within the range or not.
 *
 * In case of a single number, we make a range where low is equal to high.
 * We take care on wrongly entered ranges; opposite order will be taken
 * care of.
 *
 * The following syntax is accepted :
 *
 *   1-20,30-40     Range from 1 to 20, and packets 30 to 40
 *   -20,30         Range from 1 to 20, and packet 30
 *   20,30,40-      20, 30, and the range from 40 to the end
 *   20-10,30-25    Range from 10 to 20, and from 25 to 30
 *   -              All values
 */

convert_ret_t
range_convert_str(range_t **rangep, const gchar *es, guint32 max_value)
{
   return range_convert_str_work(rangep, es, max_value, TRUE);
}

/*  This version of range_convert_str() allows the caller to specify whether
 *  values in excess of the range's specified maximum should cause an error or
 *  be silently lowered.
 *  XXX - both the function and the variable could probably use better names.
 */
convert_ret_t
range_convert_str_work(range_t **rangep, const gchar *es, guint32 max_value,
                       gboolean err_on_max)
{

   range_t       *range;
   guint         nranges;
   const gchar   *p;
   char          *endp;
   gchar         c;
   guint         i;
   guint32       tmp;
   unsigned long val;

   if ( (rangep == NULL) || (es == NULL) )
      return CVT_SYNTAX_ERROR;

   /* Allocate a range; this has room for one subrange. */
   range = (range_t *)g_malloc(RANGE_HDR_SIZE + sizeof (range_admin_t));
   range->nranges = 0;
   nranges = 1;

   /* Process the ranges separately until we get a comma or end of string.
    *
    * We build a structure array called ranges of high and low values. After the
    * following loop, we have the nranges variable which tells how many ranges
    * were found. The number of individual ranges is limited to 'MaxRanges'
    */

   p = es;
   for (;;) {
      /* Skip white space. */
      while ((c = *p) == ' ' || c == '\t')
         p++;
      if (c == '\0')
         break;

      /* This must be a subrange.  Make sure we have room for it. */
      if (range->nranges >= nranges) {
         /* Grow the structure.
          * 4 is an arbitrarily chosen number.
          * We start with 1, under the assumption that people
          * will often give a single number or range, and then
          * proceed to keep it a multiple of 4.
          */
         if (nranges == 1)
            nranges = 4;
         else
            nranges += 4;
         range = (range_t *)g_realloc(range, RANGE_HDR_SIZE +
                                      nranges*sizeof (range_admin_t));
      }

      if (c == '-') {
         /* Subrange starts with 1. */
         range->ranges[range->nranges].low = 1;
      } else if (g_ascii_isdigit(c)) {
         /* Subrange starts with the specified number */
         errno = 0;
         val = strtoul(p, &endp, 0);
         if (p == endp) {
            /* That wasn't a valid number. */
            g_free(range);
            return CVT_SYNTAX_ERROR;
         }
         if (errno == ERANGE || val > max_value) {
            /* That was valid, but it's too big.  Return an error if requested
             * (e.g., except when reading from the preferences file).
             */
            if (err_on_max) {
               g_free(range);
               return CVT_NUMBER_TOO_BIG;
            } else {
               /* Silently use the range's maximum value */
               val = max_value;
            }
         }
         p = endp;
         range->ranges[range->nranges].low = (guint32)val;

         /* Skip white space. */
         while ((c = *p) == ' ' || c == '\t')
            p++;
      } else {
         /* Neither empty nor a number. */
         g_free(range);
         return CVT_SYNTAX_ERROR;
      }

      if (c == '-') {
         /* There's a hyphen in the range.  Skip past it. */
         p++;

         /* Skip white space. */
         while ((c = *p) == ' ' || c == '\t')
            p++;

         if (c == ',' || c == '\0') {
            /* End of subrange string; that means the subrange ends
             * with max_value.
             */
            range->ranges[range->nranges].high = max_value;
         } else if (g_ascii_isdigit(c)) {
            /* Subrange ends with the specified number. */
            errno = 0;
            val = strtoul(p, &endp, 0);
            if (p == endp) {
               /* That wasn't a valid number. */
               g_free(range);
               return CVT_SYNTAX_ERROR;
            }
            if (errno == ERANGE || val > max_value) {
               /* That was valid, but it's too big.  Return an error if requested
                * (e.g., except when reading from the preferences file).
                */
               if (err_on_max) {
                  g_free(range);
                  return CVT_NUMBER_TOO_BIG;
               } else {
                  /* Silently use the range's maximum value */
                  val = max_value;
               }
            }
            p = endp;
            range->ranges[range->nranges].high = (guint32)val;

            /* Skip white space. */
            while ((c = *p) == ' ' || c == '\t')
               p++;
         } else {
            /* Neither empty nor a number. */
            g_free(range);
            return CVT_SYNTAX_ERROR;
         }
      } else if (c == ',' || c == '\0') {
         /* End of subrange string; that means there's no hyphen
          * in the subrange, so the start and the end are the same.
          */
         range->ranges[range->nranges].high = range->ranges[range->nranges].low;
      } else {
         /* Invalid character. */
         g_free(range);
         return CVT_SYNTAX_ERROR;
      }
      range->nranges++;

      if (c == ',') {
         /* Subrange is followed by a comma; skip it. */
         p++;
      }
   }

   /* Now we are going through the low and high values, and check
    * whether they are in a proper order. Low should be equal or lower
    * than high. So, go through the loop and swap if needed.
    */
   for (i=0; i < range->nranges; i++) {
      if (range->ranges[i].low > range->ranges[i].high) {
         tmp = range->ranges[i].low;
         range->ranges[i].low  = range->ranges[i].high;
         range->ranges[i].high = tmp;
      }
   }

   /* In case we want to know what the result ranges are :
    *
    * for (i=0; i < range->nranges; i++) {
    *  printf("Function : range_convert_str L=%u \t H=%u\n",range->ranges[i].low,range->ranges[i].high);
    * }
    *
    */
   *rangep = range;
   return CVT_NO_ERROR;
} /* range_convert_str */

/* This function returns TRUE if a given value is within one of the ranges
 * stored in the ranges array.
 */
gboolean
value_is_in_range(range_t *range, guint32 val)
{
   guint i;

   if (range) {
      for (i=0; i < range->nranges; i++) {
         if (val >= range->ranges[i].low && val <= range->ranges[i].high)
            return TRUE;
      }
   }
   return(FALSE);
}

/* This function returns TRUE if val has successfully been added to
 * a range.  This may extend an existing range or create a new one
 */
gboolean
range_add_value(range_t **range, guint32 val)
{
   guint i;

   if ((range) && (*range)) {
      for (i=0; i < (*range)->nranges; i++) {
         if (val >= (*range)->ranges[i].low && val <= (*range)->ranges[i].high)
            return TRUE;

         if (val == (*range)->ranges[i].low-1)
         {
             /* Sink to a new low */
             (*range)->ranges[i].low = val;
             return TRUE;
         }

         if (val == (*range)->ranges[i].high+1)
         {
             /* Reach a new high */
             (*range)->ranges[i].high = val;
             return TRUE;
         }
      }

      (*range) = (range_t *)g_realloc((*range), RANGE_HDR_SIZE +
                                ((*range)->nranges+1)*sizeof (range_admin_t));
      (*range)->nranges++;
      (*range)->ranges[i].low = (*range)->ranges[i].high = val;
      return TRUE;
   }
   return(FALSE);
}

/* This function returns TRUE if val has successfully been removed from
 * a range.  This may delete an existing range
 */
gboolean
range_remove_value(range_t **range, guint32 val)
{
   guint i, j, new_j;
   range_t *new_range;

   if ((range) && (*range)) {
      for (i=0; i < (*range)->nranges; i++) {

          /* value is in the middle of the range, so it can't really be removed */
         if (val > (*range)->ranges[i].low && val < (*range)->ranges[i].high)
            return TRUE;

         if ((val ==  (*range)->ranges[i].low) && (val == (*range)->ranges[i].high))
         {
             /* Remove the range item entirely */
             new_range = (range_t*)g_malloc(RANGE_HDR_SIZE + ((*range)->nranges-1)*sizeof (range_admin_t));
             new_range->nranges = (*range)->nranges-1;
             for (j=0, new_j = 0; j < (*range)->nranges; j++) {

                 /* Skip the current range */
                 if (j == i)
                     continue;

                 new_range->ranges[new_j].low = (*range)->ranges[j].low;
                 new_range->ranges[new_j].high = (*range)->ranges[j].high;
                 new_j++;
             }

             g_free(*range);
             *range = new_range;
             return TRUE;
         }

         if (val == (*range)->ranges[i].low)
         {
             /* Raise low */
             (*range)->ranges[i].low++;
             return TRUE;
         }

         if (val == (*range)->ranges[i].high)
         {
             /* Reach a new high */
             (*range)->ranges[i].high--;
             return TRUE;
         }
      }
      return TRUE;
   }
   return(FALSE);
}

/* This function returns TRUE if the two given range_t's are equal.
 */
gboolean
ranges_are_equal(range_t *a, range_t *b)
{
   guint i;

   if ( (a == NULL) || (b == NULL) )
       return FALSE;

   if (a->nranges != b->nranges)
      return FALSE;

   for (i=0; i < a->nranges; i++) {
      if (a->ranges[i].low != b->ranges[i].low)
         return FALSE;

      if (a->ranges[i].high != b->ranges[i].high)
         return FALSE;
   }

   return TRUE;

}

/* This function calls the provided callback function for each value in
 * in the range.
 */
void
range_foreach(range_t *range, void (*callback)(guint32 val))
{
   guint32 i, j;

   if (range && callback) {
      for (i=0; i < range->nranges; i++) {
         for (j = range->ranges[i].low; j <= range->ranges[i].high; j++)
            callback(j);
      }
   }
}

/* This function converts a range_t to a (wmem-allocated) string.  */
char *
range_convert_range(wmem_allocator_t *scope, const range_t *range)
{
   guint32 i;
   gboolean prepend_comma = FALSE;
   wmem_strbuf_t *strbuf;

   strbuf=wmem_strbuf_new(scope, "");

   if (range) {
      for (i=0; i < range->nranges; i++) {
         if (range->ranges[i].low == range->ranges[i].high) {
            wmem_strbuf_append_printf(strbuf, "%s%u", prepend_comma?",":"", range->ranges[i].low);
         } else {
            wmem_strbuf_append_printf(strbuf, "%s%u-%u", prepend_comma?",":"", range->ranges[i].low, range->ranges[i].high);
         }
         prepend_comma = TRUE;
      }
   }
   return wmem_strbuf_finalize(strbuf);
}

/* Create a copy of a range. */
range_t *
range_copy(range_t *src)
{
   range_t *dst;
   size_t range_size;

   if (src == NULL)
       return NULL;

   range_size = RANGE_HDR_SIZE + src->nranges*sizeof (range_admin_t);
   dst = (range_t *)g_malloc(range_size);
   memcpy(dst, src, range_size);
   return dst;
}

#if 0
/* This is a debug function to check the range functionality */
static void
value_is_in_range_check(range_t *range, guint32 val)
{
   /* Print the result for a given value */
   printf("Function : value_is_in_range_check Number %u\t",val);

   if (value_is_in_range(range, val)) {
      printf("is in range\n");
   } else {
      printf("is not in range\n");
   }
}
#endif

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local Variables:
 * c-basic-offset: 3
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=3 tabstop=8 expandtab:
 * :indentSize=3:tabSize=8:noTabs=true:
 */