1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001-2016 Free Software Foundation, Inc.
4 Author: Keith Gabryelski (according to authors.el)
6 This file is not considered part of GNU Emacs.
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.
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.
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/>. */
27 #include <binary-io.h>
29 #define DEFAULT_GROUPING 0x01
30 #define DEFAULT_BASE 16
32 int base
= DEFAULT_BASE
;
33 bool un_flag
= false, iso_flag
= false, endian
= true;
34 int group_by
= DEFAULT_GROUPING
;
37 _Noreturn
void usage (void);
40 main (int argc
, char **argv
)
42 register long address
;
46 progname
= *argv
++; --argc
;
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)
64 while (*argv
&& *argv
[0] == '-' && (*argv
)[1])
67 if (!strcmp (*argv
, "--"))
72 else if (!strcmp (*argv
, "-un") || !strcmp (*argv
, "-de"))
77 else if (!strcmp (*argv
, "-hex"))
82 else if (!strcmp (*argv
, "-iso"))
87 else if (!strcmp (*argv
, "-oct"))
92 else if (!strcmp (*argv
, "-big-endian"))
97 else if (!strcmp (*argv
, "-little-endian"))
102 else if (!strcmp (*argv
, "-group-by-8-bits"))
107 else if (!strcmp (*argv
, "-group-by-16-bits"))
112 else if (!strcmp (*argv
, "-group-by-32-bits"))
117 else if (!strcmp (*argv
, "-group-by-64-bits"))
125 fprintf (stderr
, "%s: invalid switch: \"%s\".\n", progname
,
137 char *filename
= *argv
++;
139 if (!strcmp (filename
, "-"))
141 else if ((fp
= fopen (filename
, "r")) == NULL
)
150 SET_BINARY (fileno (stdout
));
157 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
160 if (fread (buf
, 1, 10, fp
) != 10)
163 for (i
=0; i
< 16; ++i
)
165 if ((c
= getc (fp
)) == ' ' || c
== EOF
)
169 c
= hexchar (c
) * 0x10 + hexchar (d
);
172 if ((i
&group_by
) == group_by
)
178 while ((c
= getc (fp
)) != '\n' && c
!= EOF
)
190 if (fread (buf
, 1, 18, fp
) != 18)
197 SET_BINARY (fileno (fp
));
203 register int i
, c
= 0;
205 for (i
=0; i
< 16; ++i
)
207 if ((c
= getc (fp
)) == EOF
)
218 printf ("%08lx: ", address
+ 0ul);
222 (c
< 0x20 || (c
>= 0x7F && c
< 0xa0)) ? '.' :c
;
224 string
[i
+1] = (c
< 0x20 || c
>= 0x7F) ? '.' : c
;
226 printf ("%02x", c
+ 0u);
229 if ((i
&group_by
) == group_by
)
247 } while (*argv
!= NULL
);
254 fprintf (stderr
, "usage: %s [-de] [-iso]\n", progname
);
259 /* hexl.c ends here */