aboutsummaryrefslogtreecommitdiffstats
path: root/src/hexdump.c
blob: 1d51d997dc476f0a005073d8515f676c6a6df1f2 (plain)
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
/*
 * hexdump.c:
 *
 * Copyright (c) 2017 James McKenzie <foss@madingley.org>,
 * All rights reserved.
 *
 */

#include "project.h"


static unsigned char
printable (unsigned char c)
{

  if (c < ' ')
    c = '.';
  if (c > 126)
    c = '.';

  return c;
}



void
hexdump (char *prefix, void *_d, size_t os, size_t oe)
{
  unsigned char *d = (unsigned char *) _d;
  static unsigned char zero[0x10];

  size_t s, e;
  size_t i, j;
  int gap = 0;

  s = os & ~15;
  e = (oe - 1) | 15;
  e++;

  for (i = s; i < e; i += 0x10)
    {

      if (((i > s) && ((i + 0x10) < oe)) && !memcmp (&d[i], zero, 0x10))
        {
          gap = 1;
          continue;
        }
      if (gap)
        {
          printf ("%s ...\n", prefix);
          gap = 0;
        }


      printf ("%s %06lx:", prefix, i);

      for (j = i; j < (i + 0x10); ++j)
        {

          if (j == 8)
            printf (" ");

          if ((j < os) || (j >= oe))
            printf ("   ");
          else
            printf (" %02x", d[j]);
        }

      printf ("  ");

      for (j = i; j < (i + 0x10); ++j)
        {

          if (j == 8)
            printf (" ");

          if ((j < os) || (j >= oe))
            printf (" ");
          else
            printf ("%c", printable (d[j]));
        }



      printf ("\n");
    }
}