* Factored common range-parsing code.

This commit is contained in:
Sam Hocevar
2007-07-06 14:39:39 +00:00
committed by sam
parent 7693a37ae1
commit 7ce5c0c5cd
5 changed files with 118 additions and 84 deletions
+1 -1
View File
@@ -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
+4 -42
View File
@@ -1,6 +1,6 @@
/*
* zzuf - general purpose fuzzer
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* Copyright (c) 2006-2007 Sam Hocevar <sam@zoy.org>
* 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++;
+5 -41
View File
@@ -1,6 +1,6 @@
/*
* zzuf - general purpose fuzzer
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* Copyright (c) 2006-2007 Sam Hocevar <sam@zoy.org>
* 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])
+87
View File
@@ -0,0 +1,87 @@
/*
* zzuf - general purpose fuzzer
* Copyright (c) 2006-2007 Sam Hocevar <sam@zoy.org>
* 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 <stdint.h>
#elif defined HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <stdlib.h>
#include <string.h>
#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;
}
+21
View File
@@ -0,0 +1,21 @@
/*
* zzuf - general purpose fuzzer
* Copyright (c) 2006-2007 Sam Hocevar <sam@zoy.org>
* 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 *);