From a948a8887d8180a4c22dbc740d349a3ddfd02b9b Mon Sep 17 00:00:00 2001 From: Douglas Gilbert Date: Wed, 27 Jun 2007 02:56:58 +0000 Subject: Load sg3_utils-1.05 into trunk/. git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@34 6180dd3e-e324-4e3e-922d-17de1ae2f315 --- utils/Makefile | 58 +++++++++++++++++++ utils/hxascdmp.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 229 insertions(+) create mode 100644 utils/Makefile create mode 100644 utils/hxascdmp.c (limited to 'utils') diff --git a/utils/Makefile b/utils/Makefile new file mode 100644 index 00000000..35060548 --- /dev/null +++ b/utils/Makefile @@ -0,0 +1,58 @@ +SHELL = /bin/sh + +PREFIX=/usr/local +INSTDIR=$(DESTDIR)/$(PREFIX)/bin +MANDIR=$(DESTDIR)/$(PREFIX)/man + +CC = gcc +LD = gcc + +EXECS = hxascdmp + +MAN_PGS = +MAN_PREF = man8 + +LARGE_FILE_FLAGS = -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 + +CFLAGS = -g -O2 -Wall -D_REENTRANT $(LARGE_FILE_FLAGS) +# CFLAGS = -g -O2 -Wall -D_REENTRANT -DSG_KERNEL_INCLUDES $(LARGE_FILE_FLAGS) +# CFLAGS = -g -O2 -Wall -pedantic -D_REENTRANT $(LARGE_FILE_FLAGS) + +LDFLAGS = + +all: $(EXECS) + +depend dep: + for i in *.c; do $(CC) $(INCLUDES) $(CFLAGS) -M $$i; \ + done > .depend + +clean: + /bin/rm -f *.o $(EXECS) core .depend + +hxascdmp: hxascdmp.o + $(LD) -o $@ $(LDFLAGS) $^ + + +install: $(EXECS) + install -d $(INSTDIR) + for name in $^; \ + do install -s -o root -g root -m 755 $$name $(INSTDIR); \ + done + install -d $(MANDIR)/$(MAN_PREF) + for mp in $(MAN_PGS); \ + do install -o root -g root -m 644 $$mp $(MANDIR)/$(MAN_PREF); \ + gzip -9f $(MANDIR)/$(MAN_PREF)/$$mp; \ + done + +uninstall: + dists="$(EXECS)"; \ + for name in $$dists; do \ + rm -f $(INSTDIR)/$$name; \ + done + for mp in $(MAN_PGS); do \ + rm -f $(MANDIR)/$(MAN_PREF)/$$mp.gz; \ + done + +ifeq (.depend,$(wildcard .depend)) +include .depend +endif diff --git a/utils/hxascdmp.c b/utils/hxascdmp.c new file mode 100644 index 00000000..467e3a7c --- /dev/null +++ b/utils/hxascdmp.c @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include +#include +#include + +#define DEF_BYTES_PER_LINE 16 + +static int bytes_per_line = DEF_BYTES_PER_LINE; + +#define CHARS_PER_HEX_BYTE 3 +#define BINARY_START_COL 6 +#define MAX_LINE_LENGTH 257 + + +static void dStrHex(const char* str, int len, long start) +{ + const char* p = str; + unsigned char c; + char buff[MAX_LINE_LENGTH]; + long a = start; + const int bpstart = BINARY_START_COL; + const int cpstart = BINARY_START_COL + + ((CHARS_PER_HEX_BYTE * bytes_per_line) + 1) + 5; + int cpos = cpstart; + int bpos = bpstart; + int midline_space = (bytes_per_line / 2) + 1; + int i, k, line_length; + + if (len <= 0) + return; + line_length = BINARY_START_COL + + (bytes_per_line * (1 + CHARS_PER_HEX_BYTE)) + 7; + if (line_length >= MAX_LINE_LENGTH) + { + fprintf(stderr, "bytes_per_line causes maximum line length of %d " + "to be exceeded\n", MAX_LINE_LENGTH); + return; + } + memset(buff, ' ', line_length); + buff[line_length] = '\0'; + k = sprintf(buff + 1, "%.2lx", a); + buff[k + 1] = ' '; + if (bpos >= ((bpstart + (midline_space * CHARS_PER_HEX_BYTE)))) + bpos++; + + for(i = 0; i < len; i++) + { + c = *p++; + bpos += CHARS_PER_HEX_BYTE; + if (bpos == (bpstart + (midline_space * CHARS_PER_HEX_BYTE))) + bpos++; + sprintf(&buff[bpos], "%.2x", (int)(unsigned char)c); + buff[bpos + 2] = ' '; + if ((c < ' ') || (c >= 0x7f)) + c='.'; + buff[cpos++] = c; + if (cpos >= (cpstart + bytes_per_line)) + { + printf("%s\n", buff); + bpos = bpstart; + cpos = cpstart; + a += bytes_per_line; + memset(buff,' ', line_length); + k = sprintf(buff + 1, "%.2lx", a); + buff[k + 1] = ' '; + } + } + if (cpos > cpstart) + printf("%s\n", buff); +} + + +static void usage() +{ + fprintf(stderr, "Usage: hxascdmp [-b=] [-h] [-?] [file+] \n"); + fprintf(stderr, " Sends hex ASCII dump of stdin/file to stdout\n"); + fprintf(stderr, " where:\n"); + fprintf(stderr, " -b= bytes per line to display " + "(def: 16)\n"); + fprintf(stderr, " -h print this usage message\n"); + fprintf(stderr, " -? print this usage message\n"); + fprintf(stderr, " file reads file(s) and outputs it " + "as hex ASCII\n"); + fprintf(stderr, " if no files reads stdin\n"); +} + +int main(int argc, const char ** argv) +{ + char buff[8192]; + int num = 8192; + long start = 0; + int res, k, u; + int inFile = 0; /* stdin */ + int doHelp = 0; + int hasFilename = 0; + + for (k = 1; k < argc; k++) + { + if (0 == strncmp("-b=", argv[k], 3)) + { + res = sscanf(argv[k] + 3, "%d", &u); + if ((1 != res) || (u < 1)) + { + printf("Bad value after '-b' switch\n"); + usage(); + return 1; + } + bytes_per_line = u; + } + else if (0 == strcmp("-h", argv[k])) + doHelp = 1; + else if (0 == strcmp("-?", argv[k])) + doHelp = 1; + else if (*argv[k] == '-') + { + fprintf(stderr, "unknown switch: %s\n", argv[k]); + usage(); + return 1; + } + else + { + hasFilename = 1; + break; + } + } + if (doHelp) + { + usage(); + return 0; + } + + /* Make sure num to fetch is integral multiple of bytes_per_line */ + if (0 != (num % bytes_per_line)) + num = (num / bytes_per_line) * bytes_per_line; + + if (hasFilename) + { + for ( ; k < argc; k++) + { + inFile = open(argv[k], O_RDONLY); + if (inFile < 0) + { + fprintf(stderr, "Couldn't open file: %s\n", argv[k]); + } + else + { + start = 0; + printf("ASCII hex dump of file: %s\n", argv[k]); + while ((res = read(inFile, buff, num)) > 0) + { + dStrHex(buff, res, start); + start += (long)res; + } + } + close(inFile); + printf("\n"); + } + } + else + { + while ((res = read(inFile, buff, num)) > 0) + { + dStrHex(buff, res, start); + start += (long)res; + } + } + return 0; +} -- cgit v1.2.3