-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
139 lines (131 loc) · 3.82 KB
/
main.c
File metadata and controls
139 lines (131 loc) · 3.82 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
#include <stdio.h>
#include <stdlib.h>
#define BYTES_PER_LINE 16
unsigned long int findSize(FILE *file);
void *loadFile(FILE *file,unsigned long int fileSize);
void binToHexString(char binary, char *hexString);
int main(int argc,char **argv)
{
if(argc < 2 || argc >3)
{
printf("This program accepts 2 arguments.\n");
printf("binScanner.exe <path1> [<path2>]\n");
printf("<path1> : Path to input Binary file\n");
printf("<path2> : (Optional) Path to output text file.\n");
printf(" if file at <path2> does not exist, it will be created.\n");
printf(" if file at <path2> exists, it will be replaced.\n");
return 0;
}
FILE *file;
void *fileBuffer;
unsigned long int i=0;
char hexString[3];
unsigned long int fileSize=0;
//open file,find file size and load file to dynamic array then close file
file=fopen(argv[1],"rb");
if(file == NULL)
{
printf("Failed to open file %s!\n",argv[1]);
return 1;
}
fileSize=findSize(file);
fileBuffer=loadFile(file,fileSize);
if(fileBuffer == NULL)
{
printf("Failed to load file %s!\n",argv[1]);
return 1;
}
fclose(file);
if(argc == 3)
{
file=fopen(argv[2],"w");
if(file == NULL)
{
printf("Failed to create file %s!\n",argv[2]);
return 1;
}
fprintf(file,"Size of input file %s is %d bytes.\n",argv[1],fileSize);
}
for(i=0;i<=(BYTES_PER_LINE+2)*4;i++)
{
printf("-");
if(argc == 3) fprintf(file,"-");
}
//scanning through file
for(i=0;i<fileSize;i++)
{
if(!(i%BYTES_PER_LINE))
{
unsigned char highWord;
unsigned char lowWord;
char highString[3];
char lowString[3];
lowWord = i&0x00FF;
highWord = (i&0xFF00)>>8;
binToHexString(highWord,highString);
binToHexString(lowWord,lowString);
printf("\n %s%sH : ",highString,lowString);
if(argc == 3) fprintf(file,"\n %s%sH : ",highString,lowString);
}
binToHexString(*((char *)fileBuffer+i),hexString);
printf("%sH ",hexString);
if(argc == 3) fprintf(file,"%sH ",hexString);
}
printf("\n");
if(argc == 3) fprintf(file,"\n");
for(i=0;i<=(BYTES_PER_LINE+2)*4;i++)
{
printf("-");
if(argc == 3) fprintf(file,"-");
}
printf("\nFile ends here.\n");
if(argc == 3) fprintf(file,"\nFile ends here.\n");
fclose(file);
free(fileBuffer);
return 0;
}
unsigned long int findSize(FILE *file)
{
if(file == NULL)
return 0;
unsigned long int fileSize;
//finding size of file
fseek(file,0,SEEK_END);
fileSize = ftell(file);
rewind(file);
printf("Size of file = %d bytes.\n",fileSize);
return fileSize;
}
void *loadFile(FILE *file,unsigned long int fileSize)
{
//dynamic allocation of fileBuffer
void *fileBuffer;
fileBuffer = malloc(fileSize);
if (!fileBuffer)
{
printf("Memory allocation failed!\n");
return NULL;
}
//reading file and store it in an dynamic string
long unsigned int readSize;
readSize = fread(fileBuffer,1,fileSize,file);
if (readSize != fileSize)
{
printf("Unable to read complete file!\n");
printf("Read only %d bytes...\n",readSize);
return NULL;
}
printf("Reading success.\n");
return fileBuffer;
}
void binToHexString(char binary, char *hexString)
{
char hexLookUp[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
unsigned char lowerNibble=0;
unsigned char upperNibble=0;
lowerNibble=binary&0x0F;
upperNibble=(binary&0xF0)>>4;
hexString[0]=hexLookUp[upperNibble];
hexString[1]=hexLookUp[lowerNibble];
hexString[2]='\0';
}