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
|
/*
BIN2C V1.0 CODED BY CHRISTIAN PADOVANO ON 17-MAY-1995
this little utility translates a binary file in a useful C structure
that can be included in a C source.
to contact me write to EMAIL: [[Email Removed]]
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define BUF_LEN 1024
#define LINE 12
/* Tell u the file size in bytes */
long int filesize( FILE *fp )
{
long int save_pos, size_of_file;
save_pos = ftell( fp );
fseek( fp,0L, SEEK_END );
size_of_file = ftell( fp );
fseek( fp, save_pos, SEEK_SET );
return( size_of_file );
}
/* lower chars --> upper chars */
void Upper_chars(char *buffer)
{
unsigned int c;
for (c=0; c <= strlen(buffer)-1; c++)
*(buffer+c)=toupper( *( buffer+c) );
}
int main( int argc, char **argv )
{
FILE *source,*dest;
char Dummy[BUF_LEN];
int buffer;
int c;
if ( (argc < 4) )
{
if ( ( argc == 2 ) && ( strcmp(argv[1],"-h")==0 ) )
{
puts(" - <<< BIN2C V1.0 >>> by Christian Padovano - \n");
puts("USAGE: bin2C <BINARY file name> <TARGET file name> <STRUCT name>");
puts("\n <STRUCT > = name of the C structure in the destination file name.\n");
puts(" <TARGET > = without extension '.h' it will be added by program.");
return EXIT_SUCCESS;
}
else
{
puts("Bad arguments !!! You must give me all the parameters !!!!\n"
"Type 'bin2c -h' to read the help !!!! ");
return EXIT_SUCCESS;
}
}
if( (source=fopen( argv[1], "rb" )) == NULL )
{
printf("ERROR : I can't find source file %s\n",argv[1]);
return EXIT_FAILURE;
}
strcpy(Dummy,argv[2]);
strcat(Dummy,".h"); /* add suffix .h to target name */
if( (dest=fopen( Dummy, "wb+" )) == NULL )
{
printf("ERROR : I can't open destination file %s\n",Dummy);
return EXIT_FAILURE;
}
strcpy(Dummy,argv[3]);
Upper_chars(Dummy); /* lower to upper chars */
strcat(Dummy,"_LEN"); /* add the suffix _LEN to the struct name */
/* for the #define stantment */
/* It writes the header information */
fprintf( dest, "\n#define %s %ld\n\n", Dummy, filesize(source) );
fprintf( dest, "static unsigned char %s[] = {\n ", argv[3] );
if( ferror( dest ))
{
printf( "ERROR writing on target file: %s\n",argv[2] );
return EXIT_FAILURE;
}
c = 0;
buffer = fgetc( source );
while( buffer != EOF )
{
fprintf(dest,"0x%02x", buffer);
buffer = fgetc( source );
if( !feof(source))
fputc(',', dest);
c++;
if(c == LINE ) {
fprintf(dest,"\n ");
c = 0;
}
}
fprintf(dest,"\n};\n\n");
return EXIT_SUCCESS;
}
|