[net.sources] unod -- reverse an 'od -c'

levy@ttrdc.UUCP (Daniel R. Levy) (11/21/85)

Here is a small, simple utility for reversing an 'od -c' dump on a file.
This is useful in the event that, say, you want to alter a string in an
executable binary, and do not have the source.  You just do an 'od -c'
on the file, capture the output in another file, use your favorite text
editor to change the dump, and use 'unod' to restore the original with
changes.  While the amount that you can do in this fashion to a binary
is understandably quite limited, in the case of altering strings it
is probably easier to use than adb.

unod takes input (the od -c dump) on stdin, and writes to stdout.  There are
no arguments or options.  Diagnostics consist of writing the bad line of input
to stderr with a brief message, and are fatal.

unod has been tested under Sys5r2 Unix on a 3B2 and on a 3B20, and under
BSD4.1 (actually Eunice) on a VAX.  I do not know if the od -c format for
these machines is identical to that of others; so if this will not work
on your system, send flames; I have my asbestos terminal ready :-).
I have sicced lint on this source, and the only complaint I get (which I
can't seem to fix; wizards or others are welcome to point out the fault)
is regarding strncpy, regarding inconsistent declaration of value type.
Everything seems to be according to Hoyle (or at least K&R and the man pages)
so I am baffled.  So port at your own risk.

This shar ends with exit, so do not worry about trailing signature.

#!/bin/sh
: This is a shar archive.  Extract with sh, not csh.
: The rest of this file will extract:
: 'unod.c'
echo extracting - 'unod.c'
sed 's/^X//' > 'unod.c' << '!EOR!'
X/* unod.c -- reverse an 'od -c' */
X#include <stdio.h>
Xmain()
X{
X	char mnbuf[75];
X	char chbuf[2][65];
X	char outbuf[512];
X	register char *obpnd, *obp;
X	int bytcnt[2];
X	register int incbyt;
X	register int idubl = 0;
X	register int ndubld = 1;
X	register int i, j;
X	int k;
X	unsigned int m;
X	register char *p;
X	char c;
X	mnbuf[74] = '\0';	/* a stop just in case */
X	chbuf[0][64] = '\0';
X	chbuf[1][64] = '\0';
X	bytcnt[0] = 0;
X	bytcnt[1] = 0;
X	obp = outbuf;
X	obpnd = outbuf + 511;
X#define BADINPUT(string)	(void)fprintf(stderr,"Bad input '%s'\n",string)
X#define FLUSHOUT	{ \
X			m = obp-outbuf; \
X			if (write(1,outbuf,m) != m) { \
X			    (void)write(2,"Error writing to stdout!\n",25); \
X			    return 4; \
X			} \
X			obp=outbuf; \
X			}
X	while (1) {
X		if (fgets(mnbuf,73,stdin) == NULL) {
X			FLUSHOUT;
X			return 0;
X		}
X		if (mnbuf[0] == '*') continue;
X		if ((i =sscanf(mnbuf,"%o7",&bytcnt[idubl])) != 1) {
X			BADINPUT(mnbuf);
X			return 1;
X		}
X		(void)strncpy(chbuf[idubl],mnbuf+7,64);
X		incbyt = bytcnt[idubl] - bytcnt[1 - idubl];
X		idubl = 1 - idubl;
X		if (ndubld) {
X			ndubld = 0;
X			continue;
X		}
X		/* Now we have two filled double buffers. */
X		for (i = 0; i < incbyt; i++) {
X			j = (i % 16) * 4;
X			p = chbuf[idubl] + j;
X			if (strncmp("   ",p,3) == 0) {
X				/* the character itself */
X				*obp++ = *(p+3);
X				if (obp > obpnd) FLUSHOUT;
X			} else if (strncmp("  \\",p,3) == 0) {
X				switch (*(p+3)) {
X				case '0':
X					c = '\0';
X					break;
X				case 'b':
X					c = '\b';
X					break;
X				case 't':
X					c = '\t';
X					break;
X				case 'n':
X					c = '\n';
X					break;
X				case 'f':
X					c = '\f';
X					break;
X				case 'r':
X					c = '\r';
X					break;
X				default:
X					BADINPUT(chbuf[idubl]);
X					return 2;
X				}
X				
X				*obp++ = c;
X				if (obp > obpnd) FLUSHOUT;
X			} else {
X				if (sscanf(p+1,"%o3",&k) != 1) {
X					BADINPUT(chbuf[idubl]);
X					return 3;
X				}
X				*obp++ = k;
X				if (obp > obpnd) FLUSHOUT;
X			}
X		}
X	}
X}
!EOR!
exit 0

stephen@dcl-cs.UUCP (Stephen J. Muir) (11/26/85)

In article <596@ttrdc.UUCP> levy@ttrdc.UUCP (Daniel R. Levy) writes:
>Here is a small, simple utility for reversing an 'od -c' dump on a file.

Why not use my binary editor (available from the moderator for mod.sources)?
-- 
UUCP:	...!seismo!mcvax!ukc!dcl-cs!stephen
DARPA:	stephen%comp.lancs.ac.uk@ucl-cs	| Post: University of Lancaster,
JANET:	stephen@uk.ac.lancs.comp	|	Department of Computing,
Phone:	+44 524 65201 Ext. 4599		|	Bailrigg, Lancaster, UK.
Project:Alvey ECLIPSE Distribution	|	LA1 4YR