1 /* Convert files for Emacs Hexl mode.
2 Copyright (C) 1989, 2001-2018 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 <https://www.gnu.org/licenses/>. */
28 #include <binary-io.h>
29 #include <unlocked-io.h>
31 static char *progname
;
36 fprintf (stderr
, "%s: write error\n", progname
);
43 return c
- ('0' <= c
&& c
<= '9' ? '0' : 'a' - 10);
47 main (int argc
, char **argv
)
49 int status
= EXIT_SUCCESS
;
50 int DEFAULT_GROUPING
= 0x01;
51 int group_by
= DEFAULT_GROUPING
;
52 bool un_flag
= false, iso_flag
= false;
61 ** -iso iso character set.
62 ** -un || -de from hexl format to binary.
63 ** -- End switch list.
64 ** <filename> dump filename
65 ** - (as filename == stdin)
68 for (; *argv
&& *argv
[0] == '-' && (*argv
)[1]; argv
++)
71 if (!strcmp (*argv
, "--"))
76 else if (!strcmp (*argv
, "-un") || !strcmp (*argv
, "-de"))
79 set_binary_mode (fileno (stdout
), O_BINARY
);
81 else if (!strcmp (*argv
, "-hex"))
82 /* Hex is the default and is only base supported. */;
83 else if (!strcmp (*argv
, "-iso"))
85 else if (!strcmp (*argv
, "-group-by-8-bits"))
87 else if (!strcmp (*argv
, "-group-by-16-bits"))
89 else if (!strcmp (*argv
, "-group-by-32-bits"))
91 else if (!strcmp (*argv
, "-group-by-64-bits"))
95 fprintf (stderr
, "%s: invalid switch: \"%s\".\n", progname
,
97 fprintf (stderr
, "usage: %s [-de] [-iso]\n", progname
);
102 char const *filename
= *argv
? *argv
++ : "-";
108 if (!strcmp (filename
, "-"))
112 set_binary_mode (fileno (stdin
), O_BINARY
);
116 fp
= fopen (filename
, un_flag
? "r" : "rb");
120 status
= EXIT_FAILURE
;
127 for (int c
; 0 <= (c
= getc (fp
)); )
129 /* Skip address at start of line. */
133 for (int i
= 0; i
< 16; i
++)
136 if (c
< 0 || c
== ' ')
139 int hc
= hexchar (c
);
143 putchar (hc
* 0x10 + hexchar (c
));
145 if ((i
& group_by
) == group_by
)
153 while (0 <= c
&& c
!= '\n')
167 for (uintmax_t address
= 0; 0 <= c
; address
+= 0x10)
170 for (i
= 0; i
< 16; i
++)
180 string
[i
+ 1] = '\0';
185 printf ("%08"PRIxMAX
": ", address
);
188 = (c
< 0x20 || (0x7F <= c
&& (!iso_flag
|| c
< 0xa0))
191 printf ("%02x", c
+ 0u);
194 if ((i
& group_by
) == group_by
)
206 bool trouble
= ferror (fp
) != 0;
207 trouble
|= fp
!= stdin
&& fclose (fp
) != 0;
210 fprintf (stderr
, "%s: read error\n", progname
);
211 status
= EXIT_FAILURE
;
218 if (ferror (stdout
) || fclose (stdout
) != 0)