1 /* ======================================================================== */
2 /* TOHEX J. Zbiciak */
3 /* */
4 /* Reads a file (or a pipe) containing an arbitrary file, producing a */
5 /* hex-dump. The hex-dump file is written in the following format: */
6 /* */
7 /* 23 69 6E 63 6C 75 64 65 20 3C 73 74 64 69 6F 2E # #include <stdio. */
8 /* 68 3E 0A # h>. */
9 /* */
10 /* eg. A list of hexidecimal bytes followed by a comment block containing */
11 /* the ASCII rendering of the characters. Non-printing characters are */
12 /* shown as dots. */
13 /* */
14 /* Usage: tohex [infile [outfile]] */
15 /* */
16 /* The files 'infile' and 'outfile' default to 'stdin' and 'stdout', */
17 /* respectively. If 'infile' is given as '-', then 'stdin' is used. */
18 /* This allows specifying an output file while taking input from a pipe. */
19 /* ======================================================================== */
20
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26
27 /* ======================================================================== */
28 /* MAIN -- Most of the action happens here. */
29 /* ======================================================================== */
main(int argc,char * argv[])30 int main(int argc, char *argv[])
31 {
32 const char hex[]="0123456789ABCDEF";
33 char buf[16];
34 char line[80];
35 int i,j,k;
36 unsigned addr = 0;
37 FILE * fi=stdin, * fo=stdout;
38
39 errno = 0;
40
41 for (i=0;i<80;i++) line[i]=' ';
42
43 /* ==================================================================== */
44 /* Check to see if the user is asking for help. */
45 /* ==================================================================== */
46 if (argc>1 && (!strcmp(argv[1],"-?") ||
47 !strcmp(argv[1],"-h") ||
48 !strcmp(argv[1],"--help")))
49 {
50 fprintf(stderr,
51 "Usage: tohex [infile [outfile]]\n\n"
52 " 'infile' and 'outfile' default to stdin and stdout.\n\n"
53 " Use 'tohex - outfile' to use stdin for infile while\n"
54 " still specifying an output file.\n\n");
55 exit(1);
56 }
57
58 /* ==================================================================== */
59 /* Open the input file (if specified). */
60 /* ==================================================================== */
61 if (argc>1 && argv[1][0] != '-')
62 {
63 fi=fopen(argv[1],"rb");
64
65 if (!fi)
66 {
67 perror("fopen()");
68 fprintf(stderr,"Couldn't open '%s' for reading.\n",argv[1]);
69 exit(1);
70 }
71 }
72
73 /* ==================================================================== */
74 /* Open the output file (if specified). */
75 /* ==================================================================== */
76 if (argc>2)
77 {
78 fo=fopen(argv[2],"w");
79
80 if (!fo)
81 {
82 perror("fopen()");
83 fprintf(stderr,"Couldn't open '%s' for reading.\n",argv[2]);
84 exit(1);
85 }
86 }
87
88 /* ==================================================================== */
89 /* Read the input file 16 bytes at a time, outputting the hex dump. */
90 /* ==================================================================== */
91 while (!feof(fi))
92 {
93 if (!(i=fread(buf,1,16,fi))) break;
94
95 line[52]='#';
96 line[79]='\n';
97
98 for (j=i;j<16;j++)
99 {
100 k=1+j*3+2*(j>7);
101 line[k ] =' ';
102 line[k+1] =' ';
103 line[k+2] =' ';
104 line[54+j]=' ';
105 }
106
107 for (j=0;j<i;j++)
108 {
109 k=1+j*3+2*(j>7);
110 line[k ] =hex[(buf[j]>>4)&0x0f];
111 line[k+1] =hex[buf[j]&0x0f];
112 line[54+j]=(buf[j]>=32)&&(buf[j]<127)?buf[j]:'.';
113 }
114
115 for (j = 0; j < 8; j++)
116 line[78 - j] = hex[(addr >> (j * 4)) & 0xF];
117
118 fwrite(line,1,80,fo);
119
120 addr += 16;
121 }
122
123 return errno;
124 }
125
126 /* ======================================================================== */
127 /* This program is free software; you can redistribute it and/or modify */
128 /* it under the terms of the GNU General Public License as published by */
129 /* the Free Software Foundation; either version 2 of the License, or */
130 /* (at your option) any later version. */
131 /* */
132 /* This program is distributed in the hope that it will be useful, */
133 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
134 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
135 /* General Public License for more details. */
136 /* */
137 /* You should have received a copy of the GNU General Public License along */
138 /* with this program; if not, write to the Free Software Foundation, Inc., */
139 /* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
140 /* ======================================================================== */
141 /* Copyright (c) 1998-2001, Joseph Zbiciak */
142 /* ======================================================================== */
143