-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_modifier.c
More file actions
292 lines (255 loc) · 8.71 KB
/
Copy pathbit_modifier.c
File metadata and controls
292 lines (255 loc) · 8.71 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "errors.h"
void int_modifier_textsize(unsigned * ptextsize, FILE * f)
{
unsigned new_textsize = 0;
printf("The value of textsize is : %d\n",*ptextsize);
printf("Enter the new value\n>");
scanf("%d", &new_textsize);
printf("Old value of textsize : %d. New one is : %d\n", *ptextsize, new_textsize);
*ptextsize = new_textsize;
while(fseek(f, 0, SEEK_SET) != 0)
printf("fseek(f, 0, SEEK_SET)\n");
//On écrit le int :
fputc(new_textsize, f);
}
void int_modifier_datasize(unsigned * pdatasize, FILE * f)
{
unsigned new_datasize = 0;
printf("The value of datasize is : %d\n",*pdatasize);
printf("Enter the new value\n>");
scanf("%d", &new_datasize);
printf("Old value of textsize : %d. New one is : %d\n", *pdatasize, new_datasize);
*pdatasize = new_datasize;
while(fseek(f, 4, SEEK_SET) != 0)
printf("fseek(f, 4, SEEK_SET)\n");
//On écrit le int :
fputc(new_datasize, f);
}
void int_modifier_dataend(unsigned * pdataend, FILE * f)
{
unsigned new_dataend = 0;
printf("The value of new_dataend is : %d\n",*pdataend);
printf("Enter the new value\n>");
scanf("%d", &new_dataend);
printf("Old value of textsize : %d. New one is : %d\n", *pdataend, new_dataend);
*pdataend = new_dataend;
while(fseek(f, 8, SEEK_SET) != 0)
printf("fseek(f, 8, SEEK_SET)\n");
//On écrit le int :
fputc(new_dataend, f);
}
void ints_modifier(unsigned * ptextsize, unsigned * pdatasize, unsigned * pdataend, FILE * f)
{
int choice = 0;
do{
printf("\nMake your choice, you want to modifys :\n"
"\t1 textsize,\n"
"\t2 datasize,\n"
"\t3 dataend.\n>"
);
scanf("%d", &choice);
switch (choice)
{
case 1:
putchar('\n');
int_modifier_textsize(ptextsize, f);
break;
case 2:
putchar('\n');
int_modifier_datasize(pdatasize, f);
break;
case 3:
putchar('\n');
int_modifier_dataend(pdataend, f);
break;
default:
break;
}
}while((choice > 3) || (choice <= 0));
}
void text_modifier(unsigned * ptextsize)
{
/*
printf("Instruction text[] = {\n");
for(int i = 0 ; i < *ptextsize ; i++)
{
printf("\t0x%04x, ", text[i]);
if (i % 4 == 3 || (i == (*ptextsize - 1)) )
putchar('\n');
}
printf("};\n");
printf("unsigned textsize = %d;\n", *ptextsize);
*/
}
void data_modifier()
{
printf("Bienvenu dans data_modifier()\n");
}
void bit_modifier(FILE * f)
{
while(fseek(f, 0, SEEK_END) != 0){}; //On se place à la fin du fichier
int size_file = ftell(f);
int bit_to_modify = 0;
printf("\nWhat is the bit that you want to modify ?\n");
do{
printf("Enter a number between 0 and %d\n>",(size_file-4));
scanf("%d", &bit_to_modify);
}while((bit_to_modify > (size_file-4)) || (bit_to_modify < 0));
//On va a la position demandée pour lire :
while(fseek(f, (long)bit_to_modify, SEEK_SET) != 0)
printf("fseek(f, (long)bit_to_modify, SEEK_SET)");
//On lit le int a l'adresse :
unsigned int_read = fgetc(f);
unsigned new_value = 0;
printf("bit to modify is the %dth. Its value is : 0x%x.\n", bit_to_modify, int_read);
printf("Enter the new value\n>");
scanf("%d", &new_value);
//On va a la position demandée pour écrire :
while(fseek(f, (long)bit_to_modify, SEEK_SET) != 0)
printf("fseek(f, (long)bit_to_modify, SEEK_SET)\n");
//On écrit le int :
fputc(new_value, f);
//On va a la position demandée pour lire :
while(fseek(f, (long)bit_to_modify, SEEK_SET) != 0){};
new_value = fgetc(f);
printf("Old value : 0x%x. New value : 0x%x\n", int_read, new_value);}
void affichage(char * path_name, unsigned * ptextsize, unsigned * pdatasize, unsigned * pdataend)
{
FILE * f = fopen(path_name, "r+");
if (f == NULL)
exit(-1);//Erreur d'ouverture du fichier binaire
//else
//printf("File sucessfully opened.\n");
int bit_read = 0;
while(fseek(f, 0, SEEK_END) != 0){}; //On se place à la fin du fichier
int size_file = ftell(f);
printf("The bin file has %d bits\n",size_file);
if(fclose(f) != 0)
exit(-2);
//Ouverture du fichier :
int handle = open(path_name,O_RDONLY);
if(handle < 0)
fprintf(stderr, "Erreur d'ouverture du fichier binaire");
if( (bit_read = read(handle, ptextsize, sizeof(*ptextsize))) != sizeof(*ptextsize))
fprintf(stderr, "Erreur de lecture de 'textsize'. %d bit lus au lieu de %d", bit_read, sizeof(*ptextsize));;
if( (bit_read = read(handle, pdatasize, sizeof(*pdatasize))) != sizeof(*pdatasize))
fprintf(stderr, "Erreur de lecture de 'datasize'. %d bit lus au lieu de %d", bit_read, sizeof(*pdatasize));;
if( (bit_read = read(handle, pdataend, sizeof(*pdataend))) != sizeof(*pdataend))
fprintf(stderr, "Erreur de lecture de 'dataend'. %d bit lus au lieu de %d", bit_read, sizeof(*pdataend));
/*extern*/ Instruction * text = malloc(*ptextsize * sizeof(Instruction));
//lecture des instructions :
if( (bit_read = read(handle, text, *ptextsize * sizeof(Instruction))) != (*ptextsize * sizeof(Instruction)) )
fprintf(stderr, "Erreur de lecture de 'text'. %d bit lus au lieu de %d", bit_read, (*ptextsize * sizeof(Instruction)));
Word * data = malloc(*pdatasize * sizeof(Word));
//lecture des données :
if( (bit_read = read(handle, data, *pdatasize * sizeof(Word))) != (*pdatasize * sizeof(Word)))
fprintf(stderr, "Erreur de lecture de 'data'. %d bit lus au lieu de %d", bit_read, (*pdatasize * sizeof(Word)));
printf("Instruction text[] = {\n");
for(int i = 0 ; i < *ptextsize ; i++)
{
printf("\t0x%04x, ", text[i]);
if (i % 4 == 3 || (i == (*ptextsize - 1)) )
putchar('\n');
}
printf("};\n");
printf("unsigned textsize = %d;\n", *ptextsize);
printf("\nWord data[] = {\n");
//Affichage des données au format binaire:
for(int i = 0 ; i < *pdatasize ; i++)
{
printf("\t0x%08x, ", data[i]);
if (i % 4 == 3)
putchar('\n');
}
printf("\n};\n");
printf("unsigned datasize = %d;\n", *pdatasize);
printf("unsigned dataend = %d;\n", *pdataend);
//Fermeture du fichier :
if(close(handle) != 0)
fprintf(stderr, "Erreur de fermeture du fichier binaire dans <machine.c:read_program>");
}
void modification(char * path_name, unsigned * ptextsize, unsigned * pdatasize, unsigned * pdataend)
{
FILE * f = fopen(path_name, "r+");
if (f == NULL)
exit(-1);//Erreur d'ouverture du fichier binaire
//else
//printf("File sucessfully opened.\n");
int choice = 0;
do{
printf("\nMake your choice :\n"
"\t1 to modify textsize, datasize or dataend,\n"
"\t2 to modify text,\n"
"\t3 to modify data,\n"
"\t4 to any bit.\n>"
);
scanf("%d", &choice);
switch (choice)
{
case 1:
ints_modifier(ptextsize, pdatasize, pdataend, f);
break;
case 2:
text_modifier(ptextsize);
break;
case 3:
data_modifier();
break;
case 4:
bit_modifier(f);
break;
default:
break;
}
}while((choice > 4) || (choice <= 0));
/*
printf("\nWhat is the bit that you want to modify ?\n");
do{
printf("Enter a number between 0 and %d\n>",size_file);
scanf("%d", &bit_to_modify);
}while((bit_to_modify > size_file) || (bit_to_modify < 0));
//On va a la position demandée pour lire :
while(fseek(f, (long)bit_to_modify, SEEK_SET) != 0)
printf("fseek(f, (long)bit_to_modify, SEEK_SET)");
//On lit le int a l'adresse :
int int_read = fgetc(f);
int int_to_write = int_read+1;
printf("bit to modify is the %dth. Its value is : 0x%x, we wil try to write : 0x%x\n", bit_to_modify, int_read, int_to_write);
//On va a la position demandée pour écrire :
while(fseek(f, (long)bit_to_modify, SEEK_SET) != 0)
printf("fseek(f, (long)bit_to_modify, SEEK_SET)\n");
//On écrit le int :
fputc(int_to_write, f);
//On va a la position demandée pour lire :
while(fseek(f, (long)bit_to_modify, SEEK_SET) != 0){};
int new_value = fgetc(f);
printf("Old value : 0x%x. New value : 0x%x\n", int_read, new_value);
*/
if(fclose(f) != 0)
exit(-2);
}
int main(int argc, char *argv[])
{
if (!argc == 2)
{
fprintf(stderr, "usage: '%s path_name' where path_name is the path to the bin file to modify.\n",argv[0]);
exit(-3);
}
//else
unsigned textsize;
unsigned datasize;
unsigned dataend;
printf( "************************************************************\n"
"******************* Original file content ******************\n"
"************************************************************\n"
);
affichage(argv[1], &textsize, &datasize, &dataend);
modification(argv[1], &textsize, &datasize, &dataend);
printf( "\n************************************************************\n"
"********************* Final file content *******************\n"
"************************************************************\n"
);
affichage(argv[1], &textsize, &datasize, &dataend);
putchar('\n');
exit(0);
}