From 7ce5c0c5cd98c234944bfa847bb9dd0a1e26b670 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 6 Jul 2007 14:39:39 +0000 Subject: [PATCH] * Factored common range-parsing code. --- src/Makefile.am | 2 +- src/fd.c | 46 +++----------------------- src/fuzz.c | 46 +++----------------------- src/ranges.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ src/ranges.h | 21 ++++++++++++ 5 files changed, 118 insertions(+), 84 deletions(-) create mode 100644 src/ranges.c create mode 100644 src/ranges.h diff --git a/src/Makefile.am b/src/Makefile.am index 2e96460..861b9c3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ # $Id$ -COMMON = random.c random.h fd.c fd.h fuzz.c fuzz.h +COMMON = random.c random.h ranges.c ranges.h fd.c fd.h fuzz.c fuzz.h if NEED_GETOPT_LONG GETOPT = mygetopt.c mygetopt.h endif diff --git a/src/fd.c b/src/fd.c index 5abd684..6258d16 100644 --- a/src/fd.c +++ b/src/fd.c @@ -1,6 +1,6 @@ /* * zzuf - general purpose fuzzer - * Copyright (c) 2006 Sam Hocevar + * Copyright (c) 2006-2007 Sam Hocevar * All Rights Reserved * * $Id$ @@ -35,6 +35,7 @@ #include "libzzuf.h" #include "fd.h" #include "fuzz.h" +#include "ranges.h" /* Regex stuff */ #if defined HAVE_REGEX_H @@ -97,37 +98,8 @@ void _zz_exclude(char const *regex) /* This function is the same as _zz_bytes() */ void _zz_pick(char const *list) { - char const *parser; - unsigned int i, chunks; - - /* Count commas */ - for(parser = list, chunks = 1; *parser; parser++) - if(*parser == ',') - chunks++; - /* TODO: free(ranges) if ranges != ranges_static */ - if(chunks >= 256) - ranges = malloc((chunks + 1) * 2 * sizeof(unsigned int)); - else - ranges = ranges_static; - - /* Fill ranges list */ - for(parser = list, i = 0; i < chunks; i++) - { - char const *comma = strchr(parser, ','); - char const *dash = strchr(parser, '-'); - - ranges[i * 2] = (dash == parser) ? 0 : atoi(parser); - if(dash && (dash + 1 == comma || dash[1] == '\0')) - ranges[i * 2 + 1] = ranges[i * 2]; /* special case */ - else if(dash && (!comma || dash < comma)) - ranges[i * 2 + 1] = atoi(dash + 1) + 1; - else - ranges[i * 2 + 1] = ranges[i * 2] + 1; - parser = comma + 1; - } - - ranges[i * 2] = ranges[i * 2 + 1] = 0; + ranges = _zz_allocrange(list, ranges_static); } void _zz_setseed(int32_t s) @@ -303,21 +275,11 @@ void _zz_register(int fd) if(ranges) { static int idx = 0; - int *r; - idx++; - - for(r = ranges; r[1]; r += 2) - if(idx >= r[0] && (r[0] == r[1] || idx < r[1])) - goto range_ok; - - files[i].active = 0; + files[i].active = _zz_isinrange(++idx, ranges); } else - { - range_ok: files[i].active = 1; - } if(autoinc) seed++; diff --git a/src/fuzz.c b/src/fuzz.c index 9be4a0e..dc73af3 100644 --- a/src/fuzz.c +++ b/src/fuzz.c @@ -1,6 +1,6 @@ /* * zzuf - general purpose fuzzer - * Copyright (c) 2006 Sam Hocevar + * Copyright (c) 2006-2007 Sam Hocevar * All Rights Reserved * * $Id$ @@ -32,6 +32,7 @@ #include "random.h" #include "fuzz.h" #include "fd.h" +#include "ranges.h" #define MAGIC1 0x33ea84f7 #define MAGIC2 0x783bc31f @@ -67,37 +68,8 @@ extern void _zz_fuzzing(char const *mode) /* This function is the same as _zz_pick() */ void _zz_bytes(char const *list) { - char const *parser; - unsigned int i, chunks; - - /* Count commas */ - for(parser = list, chunks = 1; *parser; parser++) - if(*parser == ',') - chunks++; - /* TODO: free(ranges) if ranges != ranges_static */ - if(chunks >= 256) - ranges = malloc((chunks + 1) * 2 * sizeof(unsigned int)); - else - ranges = ranges_static; - - /* Fill ranges list */ - for(parser = list, i = 0; i < chunks; i++) - { - char const *comma = strchr(parser, ','); - char const *dash = strchr(parser, '-'); - - ranges[i * 2] = (dash == parser) ? 0 : atoi(parser); - if(dash && (dash + 1 == comma || dash[1] == '\0')) - ranges[i * 2 + 1] = ranges[i * 2]; /* special case */ - else if(dash && (!comma || dash < comma)) - ranges[i * 2 + 1] = atoi(dash + 1) + 1; - else - ranges[i * 2 + 1] = ranges[i * 2] + 1; - parser = comma + 1; - } - - ranges[i * 2] = ranges[i * 2 + 1] = 0; + ranges = _zz_allocrange(list, ranges_static); } void _zz_protect(char const *list) @@ -161,19 +133,11 @@ void _zz_fuzz(int fd, volatile uint8_t *buf, int64_t len) for(j = start; j < stop; j++) { - int *r; uint8_t byte, fuzzbyte; - if(!ranges) - goto range_ok; + if(ranges && !_zz_isinrange(j, ranges)) + continue; /* Not in one of the ranges, skip byte */ - for(r = ranges; r[1]; r += 2) - if(j >= r[0] && (r[0] == r[1] || j < r[1])) - goto range_ok; - - continue; /* Not in one of the ranges, skip byte */ - - range_ok: byte = aligned_buf[j]; if(protect[byte]) diff --git a/src/ranges.c b/src/ranges.c new file mode 100644 index 0000000..ef706ec --- /dev/null +++ b/src/ranges.c @@ -0,0 +1,87 @@ +/* + * zzuf - general purpose fuzzer + * Copyright (c) 2006-2007 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +/* + * ranges.c: range handling helper functions + */ + +#include "config.h" + +#if defined HAVE_STDINT_H +# include +#elif defined HAVE_INTTYPES_H +# include +#endif +#include +#include + +#include "libzzuf.h" +#include "ranges.h" + +/* This function converts a string containing a list of ranges in the format + * understood by cut(1) such as "1-5,8,10-" into a C array for lookup. + * If more than 256 slots are required, new memory is allocated, otherwise + * the static array ranges_static is used. It is the caller's duty to call + * free() if the returned value is not ranges_static. */ +int *_zz_allocrange(char const *list, int *ranges_static) +{ + char const *parser; + int *ranges; + unsigned int i, chunks; + + /* Count commas */ + for(parser = list, chunks = 1; *parser; parser++) + if(*parser == ',') + chunks++; + + if(chunks >= 256) + ranges = malloc((chunks + 1) * 2 * sizeof(unsigned int)); + else + ranges = ranges_static; + + /* Fill ranges list */ + for(parser = list, i = 0; i < chunks; i++) + { + char const *comma = strchr(parser, ','); + char const *dash = strchr(parser, '-'); + + ranges[i * 2] = (dash == parser) ? 0 : atoi(parser); + if(dash && (dash + 1 == comma || dash[1] == '\0')) + ranges[i * 2 + 1] = ranges[i * 2]; /* special case */ + else if(dash && (!comma || dash < comma)) + ranges[i * 2 + 1] = atoi(dash + 1) + 1; + else + ranges[i * 2 + 1] = ranges[i * 2] + 1; + parser = comma + 1; + } + + ranges[i * 2] = ranges[i * 2 + 1] = 0; + + return ranges; +} + +int _zz_isinrange(int value, int const *ranges) +{ + int const *r; + + if(!ranges) + return 1; + + for(r = ranges; r[1]; r += 2) + if(value >= r[0] && (r[0] == r[1] || value < r[1])) + return 1; + + return 0; +} + diff --git a/src/ranges.h b/src/ranges.h new file mode 100644 index 0000000..5c6ab39 --- /dev/null +++ b/src/ranges.h @@ -0,0 +1,21 @@ +/* + * zzuf - general purpose fuzzer + * Copyright (c) 2006-2007 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +/* + * ranges.c: range handling helper functions + */ + +int *_zz_allocrange(char const *, int *); +int _zz_isinrange(int, int const *); +