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