source: zzuf/trunk/src/fuzz.c @ 1527

Last change on this file since 1527 was 1527, checked in by Sam Hocevar, 14 years ago
  • Factored regex matching stuff.
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: fuzz.c 1527 2006-12-29 17:49:11Z sam $
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 *  fuzz.c: fuzz 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 <stdio.h>
27#include <string.h>
28
29#include "libzzuf.h"
30#include "debug.h"
31#include "random.h"
32#include "fuzz.h"
33
34#define MAGIC1 0x33ea84f7
35#define MAGIC2 0x783bc31f
36
37void _zz_fuzz(int fd, uint8_t *buf, uint64_t len)
38{
39    uint64_t start, stop;
40    struct fuzz *fuzz;
41    uint8_t *aligned_buf;
42    unsigned long int pos = _zz_getpos(fd);
43    unsigned int i, j, todo;
44
45    fuzz = _zz_getfuzz(fd);
46
47    aligned_buf = buf - pos;
48
49    for(i = pos / CHUNKBYTES;
50        i < (pos + len + CHUNKBYTES - 1) / CHUNKBYTES;
51        i++)
52    {
53        /* Cache bitmask array */
54        if(fuzz->cur != (int)i)
55        {
56            uint32_t chunkseed = i * MAGIC1;
57
58            memset(fuzz->data, 0, CHUNKBYTES);
59
60            /* Add some random dithering to handle ratio < 1.0/CHUNKBYTES */
61            _zz_srand(_zz_seed ^ chunkseed);
62            todo = (int)((_zz_ratio * (8 * CHUNKBYTES * 1000)
63                                                + _zz_rand(1000)) / 1000.0);
64            _zz_srand(_zz_seed ^ chunkseed ^ (todo * MAGIC2));
65
66            while(todo--)
67            {
68                unsigned int idx = _zz_rand(CHUNKBYTES);
69                uint8_t byte = (1 << _zz_rand(8));
70
71                fuzz->data[idx] ^= byte;
72            }
73
74            fuzz->cur = i;
75        }
76
77        /* Apply our bitmask array to the buffer */
78        start = (i * CHUNKBYTES > pos) ? i * CHUNKBYTES : pos;
79
80        stop = ((i + 1) * CHUNKBYTES < pos + len)
81              ? (i + 1) * CHUNKBYTES : pos + len;
82
83        for(j = start; j < stop; j++)
84            aligned_buf[j] ^= fuzz->data[j % CHUNKBYTES];
85    }
86}
87
Note: See TracBrowser for help on using the repository browser.