hexl.c 3.96 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1 2
#include <stdio.h>
#include <ctype.h>
3 4 5
#ifdef MSDOS
#include <fcntl.h>
#endif
Jim Blandy's avatar
Jim Blandy committed
6 7 8 9 10 11 12 13 14

#define DEFAULT_GROUPING	0x01
#define DEFAULT_BASE		16

#undef TRUE
#undef FALSE
#define TRUE  (1)
#define FALSE (0)

15
extern void exit (), perror ();
Jim Blandy's avatar
Jim Blandy committed
16 17 18 19 20

int base = DEFAULT_BASE, un_flag = FALSE, iso_flag = FALSE, endian = 1;
int group_by = DEFAULT_GROUPING;
char *progname;

21 22 23
main (argc, argv)
     int argc;
     char *argv[];
Jim Blandy's avatar
Jim Blandy committed
24
{
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  register long address;
  char string[18];
  FILE *fp;

  progname = *argv++; --argc;

  /*
   ** -hex		hex dump
   ** -oct		Octal dump
   ** -group-by-8-bits
   ** -group-by-16-bits
   ** -group-by-32-bits
   ** -group-by-64-bits
   ** -iso		iso character set.
   ** -big-endian	Big Endian
   ** -little-endian	Little Endian
   ** -un || -de	from hexl format to binary.
   ** --		End switch list.
   ** <filename>	dump filename
   ** -		(as filename == stdin)
   */
Jim Blandy's avatar
Jim Blandy committed
46
    
47
  while (*argv && *argv[0] == '-' && (*argv)[1])
Jim Blandy's avatar
Jim Blandy committed
48
    {
49 50
      /* A switch! */
      if (!strcmp (*argv, "--"))
Jim Blandy's avatar
Jim Blandy committed
51
	{
52 53 54 55
	  --argc; argv++;
	  break;
	}
      else if (!strcmp (*argv, "-un") || !strcmp (*argv, "-de"))
Jim Blandy's avatar
Jim Blandy committed
56
	{
57 58 59 60
	  un_flag = TRUE;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-hex"))
Jim Blandy's avatar
Jim Blandy committed
61
	{
62 63 64 65
	  base = 16;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-iso"))
Jim Blandy's avatar
Jim Blandy committed
66
	{
67 68 69 70
	  iso_flag = TRUE;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-oct"))
Jim Blandy's avatar
Jim Blandy committed
71
	{
72 73 74 75
	  base = 8;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-big-endian"))
Jim Blandy's avatar
Jim Blandy committed
76
	{
77 78 79 80
	  endian = 1;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-little-endian"))
Jim Blandy's avatar
Jim Blandy committed
81
	{
82 83 84 85
	  endian = 0;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-group-by-8-bits"))
Jim Blandy's avatar
Jim Blandy committed
86
	{
87 88 89 90
	  group_by = 0x00;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-group-by-16-bits"))
Jim Blandy's avatar
Jim Blandy committed
91
	{
92 93 94 95
	  group_by = 0x01;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-group-by-32-bits"))
Jim Blandy's avatar
Jim Blandy committed
96
	{
97 98 99 100
	  group_by = 0x03;
	  --argc; argv++;
	}
      else if (!strcmp (*argv, "-group-by-64-bits"))
Jim Blandy's avatar
Jim Blandy committed
101
	{
102 103 104 105 106
	  group_by = 0x07;
	  endian = 0;
	  --argc; argv++;
	}
      else
Jim Blandy's avatar
Jim Blandy committed
107
	{
108 109 110
	  fprintf (stderr, "%s: invalid switch: \"%s\".\n", progname,
		   *argv);
	  usage ();
Jim Blandy's avatar
Jim Blandy committed
111 112 113
	}
    }

114
  do
Jim Blandy's avatar
Jim Blandy committed
115
    {
116 117 118
      if (*argv == NULL)
	fp = stdin;
      else
Jim Blandy's avatar
Jim Blandy committed
119
	{
120
	  char *filename = *argv++;
Jim Blandy's avatar
Jim Blandy committed
121

122 123 124 125 126 127 128
	  if (!strcmp (filename, "-"))
	    fp = stdin;
	  else if ((fp = fopen (filename, "r")) == NULL)
	    {
	      perror (filename);
	      continue;
	    }
Jim Blandy's avatar
Jim Blandy committed
129 130
	}

131
      if (un_flag)
Jim Blandy's avatar
Jim Blandy committed
132
	{
133
	  char buf[18];
Jim Blandy's avatar
Jim Blandy committed
134

135 136 137 138 139
#ifdef MSDOS
	  (stdout)->_flag &= ~_IOTEXT; /* print binary */
	  _setmode (fileno (stdout), O_BINARY);
#endif
	  for (;;)
Jim Blandy's avatar
Jim Blandy committed
140
	    {
141
	      register int i, c, d;
Jim Blandy's avatar
Jim Blandy committed
142

143
#define hexchar(x) (isdigit (x) ? x - '0' : x - 'a' + 10)
Jim Blandy's avatar
Jim Blandy committed
144

145
	      fread (buf, 1, 10, fp); /* skip 10 bytes */
Jim Blandy's avatar
Jim Blandy committed
146

147
	      for (i=0; i < 16; ++i)
Jim Blandy's avatar
Jim Blandy committed
148
		{
149 150
		  if ((c = getc (fp)) == ' ' || c == EOF)
		    break;
Jim Blandy's avatar
Jim Blandy committed
151

152 153 154
		  d = getc (fp);
		  c = hexchar (c) * 0x10 + hexchar (d);
		  putchar (c);
Jim Blandy's avatar
Jim Blandy committed
155

156 157
		  if ((i&group_by) == group_by)
		    getc (fp);
Jim Blandy's avatar
Jim Blandy committed
158 159
		}

160
	      if (c == ' ')
Jim Blandy's avatar
Jim Blandy committed
161
		{
162 163
		  while ((c = getc (fp)) != '\n' && c != EOF)
		    ;
Jim Blandy's avatar
Jim Blandy committed
164

165 166
		  if (c == EOF)
		    break;
Jim Blandy's avatar
Jim Blandy committed
167
		}
168
	      else
Jim Blandy's avatar
Jim Blandy committed
169
		{
170 171
		  if (i < 16)
		    break;
Jim Blandy's avatar
Jim Blandy committed
172

173
		  fread (buf, 1, 18, fp); /* skip 18 bytes */
Jim Blandy's avatar
Jim Blandy committed
174 175 176
		}
	    }
	}
177
      else
Jim Blandy's avatar
Jim Blandy committed
178
	{
179 180 181 182 183 184 185 186
#ifdef MSDOS
	  (fp)->_flag &= ~_IOTEXT; /* read binary */
	  _setmode (fileno (fp), O_BINARY);
#endif
	  address = 0;
	  string[0] = ' ';
	  string[17] = '\0';
	  for (;;)
Jim Blandy's avatar
Jim Blandy committed
187
	    {
188
	      register int i, c;
Jim Blandy's avatar
Jim Blandy committed
189

190
	      for (i=0; i < 16; ++i)
Jim Blandy's avatar
Jim Blandy committed
191
		{
192
		  if ((c = getc (fp)) == EOF)
Jim Blandy's avatar
Jim Blandy committed
193
		    {
194 195
		      if (!i)
			break;
Jim Blandy's avatar
Jim Blandy committed
196

197 198
		      fputs ("  ", stdout);
		      string[i+1] = '\0';
Jim Blandy's avatar
Jim Blandy committed
199
		    }
200
		  else
Jim Blandy's avatar
Jim Blandy committed
201
		    {
202 203
		      if (!i)
			printf ("%08x: ", address);
Jim Blandy's avatar
Jim Blandy committed
204

205 206 207 208 209
		      if (iso_flag)
			string[i+1] =
			  (c < 0x20 || (c >= 0x7F && c < 0xa0)) ? '.' :c;
		      else
			string[i+1] = (c < 0x20 || c >= 0x7F) ? '.' : c;
Jim Blandy's avatar
Jim Blandy committed
210

211
		      printf ("%02x", c);
Jim Blandy's avatar
Jim Blandy committed
212 213
		    }

214 215
		  if ((i&group_by) == group_by)
		    putchar (' ');
Jim Blandy's avatar
Jim Blandy committed
216 217
		}

218 219
	      if (i)
		puts (string);
Jim Blandy's avatar
Jim Blandy committed
220

221 222
	      if (c == EOF)
		break;
Jim Blandy's avatar
Jim Blandy committed
223

224
	      address += 0x10;
Jim Blandy's avatar
Jim Blandy committed
225 226 227 228

	    }
	}

229 230
      if (fp != stdin)
	fclose (fp);
Jim Blandy's avatar
Jim Blandy committed
231 232

    } while (*argv != NULL);
233
  return 0;
Jim Blandy's avatar
Jim Blandy committed
234 235
}

236
usage ()
Jim Blandy's avatar
Jim Blandy committed
237
{
238 239
  fprintf (stderr, "usage: %s [-de] [-iso]\n", progname);
  exit (1);
Jim Blandy's avatar
Jim Blandy committed
240
}