1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001-2017 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/>. */
29 #include <binary-io.h>
31 #define DEFAULT_GROUPING 0x01
32 #define DEFAULT_BASE 16
34 int base
= DEFAULT_BASE
;
35 bool un_flag
= false, iso_flag
= false, endian
= true;
36 int group_by
= DEFAULT_GROUPING
;
39 _Noreturn
void usage (void);
42 main (int argc
, char **argv
)
44 register long address
;
48 progname
= *argv
++; --argc
;
57 ** -iso iso character set.
58 ** -big-endian Big Endian
59 ** -little-endian Little Endian
60 ** -un || -de from hexl format to binary.
61 ** -- End switch list.
62 ** <filename> dump filename
63 ** - (as filename == stdin)
66 while (*argv
&& *argv
[0] == '-' && (*argv
)[1])
69 if (!strcmp (*argv
, "--"))
74 else if (!strcmp (*argv
, "-un") || !strcmp (*argv
, "-de"))
79 else if (!strcmp (*argv
, "-hex"))
84 else if (!strcmp (*argv
, "-iso"))
89 else if (!strcmp (*argv
, "-oct"))
94 else if (!strcmp (*argv
, "-big-endian"))
99 else if (!strcmp (*argv
, "-little-endian"))
104 else if (!strcmp (*argv
, "-group-by-8-bits"))
109 else if (!strcmp (*argv
, "-group-by-16-bits"))
114 else if (!strcmp (*argv
, "-group-by-32-bits"))
119 else if (!strcmp (*argv
, "-group-by-64-bits"))
127 fprintf (stderr
, "%s: invalid switch: \"%s\".\n", progname
,
139 char *filename
= *argv
++;
141 if (!strcmp (filename
, "-"))
143 else if ((fp
= fopen (filename
, "r")) == NULL
)
152 SET_BINARY (fileno (stdout
));
159 #define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
162 if (fread (buf
, 1, 10, fp
) != 10)
165 for (i
=0; i
< 16; ++i
)
167 if ((c
= getc (fp
)) == ' ' || c
== EOF
)
171 c
= hexchar (c
) * 0x10 + hexchar (d
);
174 if ((i
&group_by
) == group_by
)
180 while ((c
= getc (fp
)) != '\n' && c
!= EOF
)
192 if (fread (buf
, 1, 18, fp
) != 18)
199 SET_BINARY (fileno (fp
));
205 register int i
, c
= 0;
207 for (i
=0; i
< 16; ++i
)
209 if ((c
= getc (fp
)) == EOF
)
220 printf ("%08lx: ", address
+ 0ul);
224 (c
< 0x20 || (c
>= 0x7F && c
< 0xa0)) ? '.' :c
;
226 string
[i
+1] = (c
< 0x20 || c
>= 0x7F) ? '.' : c
;
228 printf ("%02x", c
+ 0u);
231 if ((i
&group_by
) == group_by
)
249 } while (*argv
!= NULL
);
256 fprintf (stderr
, "usage: %s [-de] [-iso]\n", progname
);
261 /* hexl.c ends here */