aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.c
blob: 32071fd1217c78a7d435a6e023d8ab7d4725dd26 (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
#include "project.h"

void
hexdump (FILE * f, uint8_t * data, int s, int l)
{
  int e = s + l - 1;
  int b = s & ~15;
  int a = (e | 15) + 1;
  int i, j;

  for (i = b; i < a; i += 16)
    {
      fprintf (f, "      %04x: ", i);
      for (j = i; j < (i + 16); ++j)
        {
          if (j == (i + 8))
            fprintf (f, " ");

          if ((j >= s) && (j <= e))
            {
              fprintf (f, "%02x ", data[j]);
            }
          else
            {
              fprintf (f, "   ");
            }
        }

      for (j = i; j < (i + 16); ++j)
        {

          if (j == (i + 8))
            fprintf (f, " ");


          if ((j >= s) && (j <= e))
            {
              uint8_t c = data[j];
              if ((c < ' ') || (c > 126))
                c = '.';
              fprintf (f, "%c", c);
            }
          else
            {
              fprintf (f, " ");
            }
        }


      fprintf (f, "\n");
    }

}