summaryrefslogtreecommitdiff
path: root/src/user/dumphex/dumphex.c
blob: 2944a2f01e288115eee4dbe9c93de22591b35d33 (plain) (blame)
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
#include <knob/file.h>
#include <knob/user.h>
#include <knob/format.h>

#define LINE_LEN 16

void print_zero_line(uint32_t offset) {
}

void main(const char *path) {
  struct file *f = open_file(path);
  if (!f) {
    tell_user_sz("Couldn't open file.\n");
    return;
  }

  uint8_t line[LINE_LEN + 2];
  uint8_t len;
  uint32_t offset = -LINE_LEN;

  enum {
    ZR_NO,
    ZR_FIRST,
    ZR_REST
  } zero_run;

  char nbuf[9];

  while ((len = read_from_file(f, LINE_LEN, line))) {
    offset += LINE_LEN;

    for (uint8_t i = 0; i < LINE_LEN; ++i)
      if (line[i]) {
        zero_run = ZR_NO;
        goto print_normal;
      }

    switch (zero_run) {
    case ZR_NO:
      zero_run = ZR_FIRST;
      _log_string("0x");
      itosz_h32(offset, nbuf);
      _log_string(nbuf);
      _log_string(" | 00 00 00 00 00 00 00 00 "
                     "00 00 00 00 00 00 00 00 |\n");
      continue;
    case ZR_FIRST:
      zero_run = ZR_REST;
      tell_user_sz("...\n");
    case ZR_REST:
      continue;
    }

  print_normal:
    _log_string("0x");
    itosz_h32(offset, nbuf);
    _log_string(nbuf);
    _log_string(" |");

    nbuf[0] = ' ';
    for (uint8_t i = 0; i < len; ++i) {
      itosz_h8(line[i], nbuf + 1);
      _log_string(nbuf);
    }

    for (uint8_t i = len; i < LINE_LEN; ++i)
      _log_string("   ");

    _log_string(" | ");

    for (uint8_t i = 0; i < len; ++i)
      if ((line[i] < 0x20) || (line[i] > 0x7e))
        line[i] = ' ';
    line[len] = '\n';
    line[len + 1] = '\0';

    _log_string(line);
  }

  close_file(f);
}