tools/trasher.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007 Michael Niedermayer
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <inttypes.h>
00024 
00025 static uint32_t state;
00026 static uint32_t ran(void)
00027 {
00028     return state = state * 1664525 + 1013904223;
00029 }
00030 
00031 int main(int argc, char **argv)
00032 {
00033     FILE *f;
00034     int count, maxburst, length;
00035 
00036     if (argc < 5) {
00037         printf("USAGE: trasher <filename> <count> <maxburst> <seed>\n");
00038         return 1;
00039     }
00040 
00041     f = fopen(argv[1], "rb+");
00042     if (!f) {
00043         perror(argv[1]);
00044         return 2;
00045     }
00046     count    = atoi(argv[2]);
00047     maxburst = atoi(argv[3]);
00048     state    = atoi(argv[4]);
00049 
00050     fseek(f, 0, SEEK_END);
00051     length = ftell(f);
00052     fseek(f, 0, SEEK_SET);
00053 
00054     while (count--) {
00055         int burst = 1 + ran() * (uint64_t) (abs(maxburst) - 1) / UINT32_MAX;
00056         int pos   = ran() * (uint64_t) length / UINT32_MAX;
00057         fseek(f, pos, SEEK_SET);
00058 
00059         if (maxburst < 0)
00060             burst = -maxburst;
00061 
00062         if (pos + burst > length)
00063             continue;
00064 
00065         while (burst--) {
00066             int val = ran() * 256ULL / UINT32_MAX;
00067 
00068             if (maxburst < 0)
00069                 val = 0;
00070 
00071             fwrite(&val, 1, 1, f);
00072         }
00073     }
00074 
00075     return 0;
00076 }