aboutsummaryrefslogtreecommitdiff
path: root/toys/other/xxd.c
blob: 0ff5947ffc0bcbde15a7d088b27178257be921a6 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* xxd.c - hexdump.
 *
 * Copyright 2015 The Android Open Source Project
 *
 * No obvious standard, output looks like:
 * 0000000: 4c69 6e75 7820 7665 7273 696f 6e20 332e  Linux version 3.

USE_XXD(NEWTOY(xxd, ">1c#<1>4096=16l#g#<1=2prs#[!rs]", TOYFLAG_USR|TOYFLAG_BIN))

config XXD
  bool "xxd"
  default y
  help
    usage: xxd [-c n] [-g n] [-l n] [-p] [-r] [-s n] [file]

    Hexdump a file to stdout.  If no file is listed, copy from stdin.
    Filename "-" is a synonym for stdin.

    -c n	Show n bytes per line (default 16)
    -g n	Group bytes by adding a ' ' every n bytes (default 2)
    -l n	Limit of n bytes before stopping (default is no limit)
    -p	Plain hexdump (30 bytes/line, no grouping)
    -r	Reverse operation: turn a hexdump into a binary file
    -s n	Skip to offset n
*/

#define FOR_xxd
#include "toys.h"

GLOBALS(
  long s;
  long g;
  long l;
  long c;
)

static void do_xxd(int fd, char *name)
{
  long long pos = 0;
  long long limit = TT.l;
  int i, len, space;

  if (toys.optflags&FLAG_s) {
    xlseek(fd, TT.s, SEEK_SET);
    pos = TT.s;
    if (limit) limit += TT.s;
  }

  while (0<(len = readall(fd, toybuf,
                          (limit && limit-pos<TT.c)?limit-pos:TT.c))) {
    if (!(toys.optflags&FLAG_p)) printf("%08llx: ", pos);
    pos += len;
    space = 2*TT.c+TT.c/TT.g+1;

    for (i=0; i<len;) {
      space -= printf("%02x", toybuf[i]);
      if (!(++i%TT.g)) {
        putchar(' ');
        space--;
      }
    }

    if (!(toys.optflags&FLAG_p)) {
      printf("%*s", space, "");
      for (i=0; i<len; i++)
        putchar((toybuf[i]>=' ' && toybuf[i]<='~') ? toybuf[i] : '.');
    }
    putchar('\n');
  }
  if (len<0) perror_exit("read");
}

static int dehex(char ch)
{
  if (ch >= '0' && ch <= '9') return ch - '0';
  if (ch >= 'a' && ch <= 'f') return ch - 'a' + 10;
  if (ch >= 'A' && ch <= 'F') return ch - 'a' + 10;
  return (ch == '\n') ? -2 : -1;
}

static void do_xxd_reverse(int fd, char *name)
{
  FILE *fp = xfdopen(fd, "r");

  while (!feof(fp)) {
    int col = 0;
    int tmp;

    // Each line of a non-plain hexdump starts with an offset/address.
    if (!(toys.optflags&FLAG_p)) {
      long long pos;

      if (fscanf(fp, "%llx: ", &pos) == 1) {
        if (fseek(stdout, pos, SEEK_SET) != 0) {
          // TODO: just write out zeros if non-seekable?
          perror_exit("%s: seek failed", name);
        }
      }
    }

    // A plain hexdump can have as many bytes per line as you like,
    // but a non-plain hexdump assumes garbage after it's seen the
    // specified number of bytes.
    while (toys.optflags&FLAG_p || col < TT.c) {
      int n1, n2;

      // If we're at EOF or EOL or we read some non-hex...
      if ((n1 = n2 = dehex(fgetc(fp))) < 0 || (n2 = dehex(fgetc(fp))) < 0) {
        // If we're at EOL, start on that line.
        if (n1 == -2 || n2 == -2) continue;
        // Otherwise, skip to the next line.
        break;
      }

      fputc((n1 << 4) | (n2 & 0xf), stdout);
      col++;

      // Is there any grouping going on? Ignore a single space.
      tmp = fgetc(fp);
      if (tmp != ' ') ungetc(tmp, fp);
    }

    // Skip anything else on this line (such as the ASCII dump).
    while ((tmp = fgetc(fp)) != EOF && tmp != '\n')
      ;
  }
  if (ferror(fp)) perror_msg_raw(name);

  fclose(fp);
}

void xxd_main(void)
{
  // Plain style is 30 bytes/line, no grouping.
  if (toys.optflags&FLAG_p) TT.c = TT.g = 30;

  loopfiles(toys.optargs, toys.optflags&FLAG_r ? do_xxd_reverse : do_xxd);
}