1 | /* |
---|
2 | * zzuf - general purpose fuzzer |
---|
3 | * Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * This program is free software. It comes without any warranty, to |
---|
7 | * the extent permitted by applicable law. You can redistribute it |
---|
8 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
9 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
11 | */ |
---|
12 | |
---|
13 | /* |
---|
14 | * ranges.c: range handling helper functions |
---|
15 | */ |
---|
16 | |
---|
17 | #include "config.h" |
---|
18 | |
---|
19 | #if defined HAVE_STDINT_H |
---|
20 | # include <stdint.h> |
---|
21 | #elif defined HAVE_INTTYPES_H |
---|
22 | # include <inttypes.h> |
---|
23 | #endif |
---|
24 | #include <stdlib.h> |
---|
25 | #include <string.h> |
---|
26 | |
---|
27 | #include "common.h" |
---|
28 | #include "ranges.h" |
---|
29 | |
---|
30 | /* This function converts a string containing a list of ranges in the format |
---|
31 | * understood by cut(1) such as "1-5,8,10-" into a C array for lookup. |
---|
32 | * If more than 256 slots are required, new memory is allocated, otherwise |
---|
33 | * the static array static_ranges is used. It is the caller's duty to call |
---|
34 | * free() if the returned value is not static_ranges. */ |
---|
35 | int64_t *_zz_allocrange(char const *list, int64_t *static_ranges) |
---|
36 | { |
---|
37 | char const *parser; |
---|
38 | int64_t *ranges; |
---|
39 | unsigned int i, chunks; |
---|
40 | |
---|
41 | /* Count commas */ |
---|
42 | for(parser = list, chunks = 1; *parser; parser++) |
---|
43 | if(*parser == ',') |
---|
44 | chunks++; |
---|
45 | |
---|
46 | if(chunks >= 256) |
---|
47 | ranges = malloc((chunks + 1) * 2 * sizeof(int64_t)); |
---|
48 | else |
---|
49 | ranges = static_ranges; |
---|
50 | |
---|
51 | /* Fill ranges list */ |
---|
52 | for(parser = list, i = 0; i < chunks; i++) |
---|
53 | { |
---|
54 | char const *comma = strchr(parser, ','); |
---|
55 | char const *dash = strchr(parser, '-'); |
---|
56 | |
---|
57 | ranges[i * 2] = (dash == parser) ? 0 : atoi(parser); |
---|
58 | if(dash && (dash + 1 == comma || dash[1] == '\0')) |
---|
59 | ranges[i * 2 + 1] = ranges[i * 2]; /* special case */ |
---|
60 | else if(dash && (!comma || dash < comma)) |
---|
61 | ranges[i * 2 + 1] = atoi(dash + 1) + 1; |
---|
62 | else |
---|
63 | ranges[i * 2 + 1] = ranges[i * 2] + 1; |
---|
64 | parser = comma + 1; |
---|
65 | } |
---|
66 | |
---|
67 | ranges[i * 2] = ranges[i * 2 + 1] = 0; |
---|
68 | |
---|
69 | return ranges; |
---|
70 | } |
---|
71 | |
---|
72 | int _zz_isinrange(int64_t value, int64_t const *ranges) |
---|
73 | { |
---|
74 | int64_t const *r; |
---|
75 | |
---|
76 | if(!ranges) |
---|
77 | return 1; |
---|
78 | |
---|
79 | for(r = ranges; r[1]; r += 2) |
---|
80 | if(value >= r[0] && (r[0] == r[1] || value < r[1])) |
---|
81 | return 1; |
---|
82 | |
---|
83 | return 0; |
---|
84 | } |
---|
85 | |
---|