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