1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2002-2025 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
/* Simple hex dumper. */
#include <stdio.h>
#include <string.h>
#include <mailutils/cctype.h>
enum {
/* Nibbles per hex byte: */
HEXLEN = 2,
/* Number of characters to dump per line: */
NDUMP = 16,
/* Emit extra whitespace in the middle of the line: */
EXTRAOFF = ((NDUMP / 2) - 1),
/* Start of literal output: */
LITOFF = ((HEXLEN + 1) * NDUMP + 2),
/* Size of the required buffer: add one character for extra whitespace
in the middle of literal output part, and one more for the trailing \n */
DUMPBUFSIZE = (LITOFF+NDUMP+2)
};
static int
rtrim (char *str, int n)
{
while (n > 0 && str[n-1] == ' ')
n--;
str[n] = '\n';
return n;
}
int
main (int argc, char **argv)
{
char vbuf[DUMPBUFSIZE];
char *p, *q;
int i;
int c;
int n;
unsigned long off = 0;
static char xchar[] = "0123456789ABCDEF";
#define REWIND { \
p = vbuf; \
q = vbuf + LITOFF; \
i = 0; \
memset (vbuf, ' ', DUMPBUFSIZE-1); \
}
REWIND;
while ((c = getchar ()) != EOF)
{
if (i == NDUMP)
{
fprintf (stdout, "%08lX: ", off);
n = rtrim (vbuf, q - vbuf);
fwrite (vbuf, 1, n+1, stdout);
off += i;
REWIND;
}
*p++ = xchar[c>>4];
*p++ = xchar[c&0xf];
*p++ = ' ';
*q++ = mu_isprint (c) ? c : '.';
if (i == EXTRAOFF)
{
*p++ = ' ';
*q++ = ' ';
}
i++;
}
if (i)
{
fprintf (stdout, "%08lX: ", off);
n = rtrim (vbuf, q - vbuf);
fwrite (vbuf, 1, n+1, stdout);
}
return 0;
}
|