test_utils.c
1.26 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
#include <stdlib.h>
#include <stdio.h>
#include <ultratypes.h>
int getFileSize(char *filename)
{
FILE *fdata;
int j;
if ((fdata=fopen(filename,"r"))==0) {
printf("cannot open %s\n",filename);
exit(1);
}
fseek(fdata,0,SEEK_END);
j=ftell(fdata);
fclose(fdata);
return j;
}
int getFileSizeAscii(char *filename)
{
int bytes,j;
FILE *fdata;
if ((fdata=fopen(filename,"r"))==0) {
printf("cannot open %s\n",filename);
exit(1);
}
for(bytes=0;fscanf(fdata,"%x",&j)!=EOF;bytes++){}
fclose(fdata);
return bytes;
}
void readAsciiBinaryData(char *filename,u8 *data,int bytes)
{
int i,j;
FILE *fdata;
if ((fdata=fopen(filename,"r"))==0) {
printf("cannot open %s\n",filename);
exit(1);
}
for(i=0; i<bytes; ++i){
fscanf(fdata,"%x",&j);
data[i]=(unsigned char)j;
}
fclose(fdata);
}
void writeAsciiBinaryData(char *filename,u8 *data,int bytes)
{
int i,j;
FILE *fdata;
if ((fdata=fopen(filename,"w"))==0) {
printf("cannot open %s\n",filename);
exit(0);
}
for(i=0; i<bytes/16; ++i) {
for(j=0; j<16; ++j)
fprintf(fdata,"%02x ",data[i*16+j]);
fprintf(fdata,"\n");
}
fclose(fdata);
}