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

Last change on this file since 1483 was 1483, checked in by Sam Hocevar, 16 years ago
  • Really fix the inconsistency bug. Now dd bs=2048 count=1 gives the same result as dd bs=1 count=2048.
  • Property svn:keywords set to Id
File size: 1.6 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 1483 2006-12-15 14:07:46Z 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#include <regex.h>
29
30#include "libzzuf.h"
31#include "debug.h"
32#include "random.h"
33#include "fuzz.h"
34
35#define CHUNK_SIZE 1024
36
37void zzuf_fuzz(int fd, uint8_t *buf, uint64_t len)
38{
39    uint8_t bits[CHUNK_SIZE];
40    uint64_t pos;
41    unsigned int i;
42
43    pos = files[fd].pos;
44
45    for(i = pos / CHUNK_SIZE;
46        i < (pos + len + CHUNK_SIZE - 1) / CHUNK_SIZE;
47        i++)
48    {
49        int todo;
50
51        /* Add some random dithering to handle percent < 1.0/CHUNK_SIZE */
52        zzuf_srand(_zzuf_seed ^ (i * 0x33ea84f7));
53        todo = (int)((_zzuf_percent * CHUNK_SIZE + zzuf_rand(100)) / 100.0);
54        zzuf_srand(_zzuf_seed ^ (i * 0x7f48ae33) ^ (todo * 0x783bc31f));
55
56        memset(bits, 0, CHUNK_SIZE);
57        while(todo--)
58        {
59            uint64_t idx = i * CHUNK_SIZE + zzuf_rand(CHUNK_SIZE);
60            uint8_t byte = (1 << zzuf_rand(8));
61
62            if(idx < pos || idx >= pos + len)
63                continue;
64
65            buf[idx - pos] ^= byte;
66        }
67    }
68}
69
Note: See TracBrowser for help on using the repository browser.