trasher.c 1.94 KB
Newer Older
Diego Biurrun's avatar
Diego Biurrun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2007 Michael Niedermayer
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
20 21

#include <stdio.h>
22
#include <stdlib.h>
23
#include <inttypes.h>
24 25
#include <errno.h>
#include <string.h>
26

27
static uint32_t state;
28 29 30
static uint32_t ran(void)
{
    return state = state * 1664525 + 1013904223;
31 32
}

33
int main(int argc, char **argv)
34
{
35 36 37
    FILE *f;
    int count, maxburst, length;

38
    if (argc < 5) {
39
        printf("USAGE: trasher <filename> <count> <maxburst> <seed>\n");
40 41 42
        return 1;
    }

43 44
    f = fopen(argv[1], "rb+");
    if (!f) {
45 46 47
        perror(argv[1]);
        return 2;
    }
48 49 50
    count    = atoi(argv[2]);
    maxburst = atoi(argv[3]);
    state    = atoi(argv[4]);
51 52

    fseek(f, 0, SEEK_END);
53
    length = ftell(f);
54 55
    fseek(f, 0, SEEK_SET);

56 57 58
    while (count--) {
        int burst = 1 + ran() * (uint64_t) (abs(maxburst) - 1) / UINT32_MAX;
        int pos   = ran() * (uint64_t) length / UINT32_MAX;
59 60
        fseek(f, pos, SEEK_SET);

61 62
        if (maxburst < 0)
            burst = -maxburst;
63

64
        if (pos + burst > length)
65 66
            continue;

67 68
        while (burst--) {
            int val = ran() * 256ULL / UINT32_MAX;
69

70 71
            if (maxburst < 0)
                val = 0;
72 73 74 75 76 77 78

            fwrite(&val, 1, 1, f);
        }
    }

    return 0;
}