summaryrefslogtreecommitdiff
path: root/bin2db.c
blob: 2f7cb6b0bcc28ebff046b397967f93cf33759fca (plain)
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
/*
	Convert a binary file to a set of assembly DB instructions
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#define PERLINE	8

int main(int argc, char *argv[])
{
    char *name="bfile";
    FILE *in,*out;
    unsigned char num;
    int col;

    in=stdin;
    out=stdout;

    if (argc>1)
    {
    	if (!(in=fopen(argv[1],"rb")))
	{
	    perror(argv[1]);
	    exit(EXIT_FAILURE);
	}
    }

    if (argc>2)
    {
    	if (!(out=fopen(argv[2],"w")))
	{
	    perror(argv[0]);
	    exit(EXIT_FAILURE);
	}
    }

    if (argc>1)
	fprintf(out,"; Auto-generated binary of %s\n\n",argv[1]);
    else
	fprintf(out,"; Auto-generated binary\n\n");

    col=0;
    num=0;

    while(!feof(in))
    	{
	fread(&num,sizeof num,1,in);

	if (col==0)
	    fprintf(out,"\n\tDB\t");

	fprintf(out,"%s0x%2.2X",col==0?"":",",num);

	col=(col+1)%PERLINE;
	}

    fclose(in);
    fclose(out);
}