-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitflipper.c
59 lines (48 loc) · 1.14 KB
/
bitflipper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* File: bitflipper.c
* Author: daniel
*
* Created on October 19, 2011, 10:46 PM
*/
#include <stdio.h>
#include <stdlib.h>
//#include <cstdlib>
#include <time.h>
/*
*
*/
int main(int argc, char** argv) {
FILE* fos;
long fSize;
int randNumBits, i, posi, maxBits;
time_t seconds;
fos = fopen("output.bin", "rb");
fseek (fos , 0 , SEEK_END);
fSize = ftell (fos);
rewind (fos);
char buffer[fSize];
fgets(buffer,fSize+1,fos);
fclose(fos);
time(&seconds);
srand((unsigned int) seconds);
maxBits = fSize/2000;
if(maxBits<6){
maxBits=6;
}
randNumBits = rand() % (maxBits - 2 + 1) + 2;
for(i=0; i < randNumBits; i++){
posi=rand()%(fSize-1+1);
if(buffer[posi]=='1'){
buffer[posi]='0';
}else{
buffer[posi]='1';
}
printf("\nFlipped Bit Location is %d\n", posi);
}
printf("\n%d bits were flipped.\n Press enter to exit.", randNumBits);
fos = fopen("output.bin", "wb");
fputs(buffer,fos);
fclose(fos);
getchar();
return (EXIT_SUCCESS);
}