File: dump.c

package info (click to toggle)
mailutils 1%3A3.15-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 36,672 kB
  • sloc: ansic: 181,539; sh: 110,509; yacc: 7,458; cpp: 3,834; makefile: 3,152; lex: 1,972; python: 1,617; exp: 1,562; awk: 152; lisp: 132; sed: 31
file content (79 lines) | stat: -rw-r--r-- 1,546 bytes parent folder | download | duplicates (3)
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
/* 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;
}