Ignore:
Timestamp:
Dec 15, 2006, 5:10:56 PM (16 years ago)
Author:
Sam Hocevar
Message:
  • Rename percent to ratio.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zzuf/trunk/src/fuzz.c

    r1483 r1486  
    3333#include "fuzz.h"
    3434
    35 #define CHUNK_SIZE 1024
     35#define MAGIC1 0x33ea84f7
     36#define MAGIC2 0x783bc31f
     37/* We arbitrarily split files into 1024-byte chunks. Each chunk has an
     38 * associated seed that can be computed from the zzuf seed, the chunk
     39 * index and the fuzziness density. This allows us to predictably fuzz
     40 * any part of the file without reading the whole file. */
     41#define CHUNKSIZE 1024
    3642
    3743void zzuf_fuzz(int fd, uint8_t *buf, uint64_t len)
    3844{
    39     uint8_t bits[CHUNK_SIZE];
    40     uint64_t pos;
    41     unsigned int i;
     45    uint64_t start, stop;
     46    unsigned int i, todo;
    4247
    43     pos = files[fd].pos;
     48    start = files[fd].pos;
     49    stop = start + len;
    4450
    45     for(i = pos / CHUNK_SIZE;
    46         i < (pos + len + CHUNK_SIZE - 1) / CHUNK_SIZE;
    47         i++)
     51    for(i = start / CHUNKSIZE; i < (stop + CHUNKSIZE - 1) / CHUNKSIZE; i++)
    4852    {
    49         int todo;
     53        uint32_t chunkseed = i * MAGIC1;
    5054
    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        /* Add some random dithering to handle ratio < 1.0/CHUNKSIZE */
     56        zzuf_srand(_zzuf_seed ^ chunkseed);
     57        todo = (int)((_zzuf_ratio * (CHUNKSIZE * 1000) + zzuf_rand(1000))
     58                     / 1000.0);
     59        zzuf_srand(_zzuf_seed ^ chunkseed ^ (todo * MAGIC2));
    5560
    56         memset(bits, 0, CHUNK_SIZE);
    5761        while(todo--)
    5862        {
    59             uint64_t idx = i * CHUNK_SIZE + zzuf_rand(CHUNK_SIZE);
     63            uint64_t idx = i * CHUNKSIZE + zzuf_rand(CHUNKSIZE);
    6064            uint8_t byte = (1 << zzuf_rand(8));
    6165
    62             if(idx < pos || idx >= pos + len)
     66            if(idx < start || idx >= stop)
    6367                continue;
    6468
    65             buf[idx - pos] ^= byte;
     69            buf[idx - start] ^= byte;
    6670        }
    6771    }
Note: See TracChangeset for help on using the changeset viewer.