trasher.c 1.9 KB
Newer Older
Diego Biurrun's avatar
Diego Biurrun committed
1 2 3
/*
 * Copyright (c) 2007 Michael Niedermayer
 *
4
 * This file is part of Libav.
Diego Biurrun's avatar
Diego Biurrun committed
5
 *
6
 * Libav is free software; you can redistribute it and/or
Diego Biurrun's avatar
Diego Biurrun committed
7 8 9 10
 * 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.
 *
11
 * Libav is distributed in the hope that it will be useful,
Diego Biurrun's avatar
Diego Biurrun committed
12 13 14 15 16
 * 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
17
 * License along with Libav; if not, write to the Free Software
Diego Biurrun's avatar
Diego Biurrun committed
18 19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
20 21

#include <stdio.h>
22
#include <stdlib.h>
23 24
#include <inttypes.h>

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

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

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

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

    fseek(f, 0, SEEK_END);
51
    length = ftell(f);
52 53
    fseek(f, 0, SEEK_SET);

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

59 60
        if (maxburst < 0)
            burst = -maxburst;
61

62
        if (pos + burst > length)
63 64
            continue;

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

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

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

    return 0;
}