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