]> code.delx.au - gnu-emacs/blob - lib-src/hexl.c
8ce0f115596ead607b9d4e290dae4d835586419d
[gnu-emacs] / lib-src / hexl.c
1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011 Free Software Foundation, Inc.
4
5 Author: Keith Gabryelski
6 (according to authors.el)
7
8 This file is not considered part of GNU Emacs.
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <ctype.h>
30 #ifdef DOS_NT
31 #include <fcntl.h>
32 #if __DJGPP__ >= 2
33 #include <io.h>
34 #endif
35 #endif
36 #ifdef WINDOWSNT
37 #include <io.h>
38 #endif
39
40 #define DEFAULT_GROUPING 0x01
41 #define DEFAULT_BASE 16
42
43 #undef TRUE
44 #undef FALSE
45 #define TRUE (1)
46 #define FALSE (0)
47
48 int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
49 int group_by = DEFAULT_GROUPING;
50 char *progname;
51
52 void usage();
53
54 int
55 main (argc, argv)
56 int argc;
57 char *argv[];
58 {
59 register long address;
60 char string[18];
61 FILE *fp;
62
63 progname = *argv++; --argc;
64
65 /*
66 ** -hex hex dump
67 ** -oct Octal dump
68 ** -group-by-8-bits
69 ** -group-by-16-bits
70 ** -group-by-32-bits
71 ** -group-by-64-bits
72 ** -iso iso character set.
73 ** -big-endian Big Endian
74 ** -little-endian Little Endian
75 ** -un || -de from hexl format to binary.
76 ** -- End switch list.
77 ** <filename> dump filename
78 ** - (as filename == stdin)
79 */
80
81 while (*argv && *argv[0] == '-' && (*argv)[1])
82 {
83 /* A switch! */
84 if (!strcmp (*argv, "--"))
85 {
86 --argc; argv++;
87 break;
88 }
89 else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
90 {
91 un_flag = TRUE;
92 --argc; argv++;
93 }
94 else if (!strcmp (*argv, "-hex"))
95 {
96 base = 16;
97 --argc; argv++;
98 }
99 else if (!strcmp (*argv, "-iso"))
100 {
101 iso_flag = TRUE;
102 --argc; argv++;
103 }
104 else if (!strcmp (*argv, "-oct"))
105 {
106 base = 8;
107 --argc; argv++;
108 }
109 else if (!strcmp (*argv, "-big-endian"))
110 {
111 endian = 1;
112 --argc; argv++;
113 }
114 else if (!strcmp (*argv, "-little-endian"))
115 {
116 endian = 0;
117 --argc; argv++;
118 }
119 else if (!strcmp (*argv, "-group-by-8-bits"))
120 {
121 group_by = 0x00;
122 --argc; argv++;
123 }
124 else if (!strcmp (*argv, "-group-by-16-bits"))
125 {
126 group_by = 0x01;
127 --argc; argv++;
128 }
129 else if (!strcmp (*argv, "-group-by-32-bits"))
130 {
131 group_by = 0x03;
132 --argc; argv++;
133 }
134 else if (!strcmp (*argv, "-group-by-64-bits"))
135 {
136 group_by = 0x07;
137 endian = 0;
138 --argc; argv++;
139 }
140 else
141 {
142 fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
143 *argv);
144 usage ();
145 }
146 }
147
148 do
149 {
150 if (*argv == NULL)
151 fp = stdin;
152 else
153 {
154 char *filename = *argv++;
155
156 if (!strcmp (filename, "-"))
157 fp = stdin;
158 else if ((fp = fopen (filename, "r")) == NULL)
159 {
160 perror (filename);
161 continue;
162 }
163 }
164
165 if (un_flag)
166 {
167 char buf[18];
168
169 #ifdef DOS_NT
170 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
171 if (!isatty (fileno (stdout)))
172 setmode (fileno (stdout), O_BINARY);
173 #else
174 (stdout)->_flag &= ~_IOTEXT; /* print binary */
175 _setmode (fileno (stdout), O_BINARY);
176 #endif
177 #endif
178 for (;;)
179 {
180 register int i, c = 0, d;
181
182 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
183
184 fread (buf, 1, 10, fp); /* skip 10 bytes */
185
186 for (i=0; i < 16; ++i)
187 {
188 if ((c = getc (fp)) == ' ' || c == EOF)
189 break;
190
191 d = getc (fp);
192 c = hexchar (c) * 0x10 + hexchar (d);
193 putchar (c);
194
195 if ((i&group_by) == group_by)
196 getc (fp);
197 }
198
199 if (c == ' ')
200 {
201 while ((c = getc (fp)) != '\n' && c != EOF)
202 ;
203
204 if (c == EOF)
205 break;
206 }
207 else
208 {
209 if (i < 16)
210 break;
211
212 fread (buf, 1, 18, fp); /* skip 18 bytes */
213 }
214 }
215 }
216 else
217 {
218 #ifdef DOS_NT
219 #if (__DJGPP__ >= 2) || (defined WINDOWSNT)
220 if (!isatty (fileno (fp)))
221 setmode (fileno (fp), O_BINARY);
222 #else
223 (fp)->_flag &= ~_IOTEXT; /* read binary */
224 _setmode (fileno (fp), O_BINARY);
225 #endif
226 #endif
227 address = 0;
228 string[0] = ' ';
229 string[17] = '\0';
230 for (;;)
231 {
232 register int i, c = 0;
233
234 for (i=0; i < 16; ++i)
235 {
236 if ((c = getc (fp)) == EOF)
237 {
238 if (!i)
239 break;
240
241 fputs (" ", stdout);
242 string[i+1] = '\0';
243 }
244 else
245 {
246 if (!i)
247 printf ("%08lx: ", address);
248
249 if (iso_flag)
250 string[i+1] =
251 (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
252 else
253 string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
254
255 printf ("%02x", c);
256 }
257
258 if ((i&group_by) == group_by)
259 putchar (' ');
260 }
261
262 if (i)
263 puts (string);
264
265 if (c == EOF)
266 break;
267
268 address += 0x10;
269
270 }
271 }
272
273 if (fp != stdin)
274 fclose (fp);
275
276 } while (*argv != NULL);
277 return EXIT_SUCCESS;
278 }
279
280 void
281 usage ()
282 {
283 fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
284 exit (EXIT_FAILURE);
285 }
286
287 /* arch-tag: 20e04fb7-926e-4e48-be86-64fe869ecdaa
288 (do not change this comment) */
289
290 /* hexl.c ends here */