[comp.emacs] reponses to gemacs binary editing mode

treed@dasys1.UUCP (Timothy Reed) (06/01/89)

Subject: gemacs binary editing mode?

The following are responses from my inquiry regarding editing 
binary files and executables with emacs.

Thanks for all the responses - the yunexus!mike's is the one I've used
so far, tho' I plan on examining the hex dumpers soon.

Timothy Reed
Acme Data Corp.
..!uunet!slcpi!treed

From uunet!utai!yunexus!mike Mon May 29 14:54:58 1989

Greetings...

Just find-file and write-file will do fine.
There's no special mode.
You may want to set the variable require-final-newline to nil though.


							Mike

Mike Marques                         | Usenet: ......!utzoo!yunexus!mike 
York University (Computing Services) |         mike@nexus.yorku.ca
-------------------------------------| Bitnet: mike@libra.yorku.ca
Only visiting this planet...         | Voice:  (416) 736-5257


From uunet!ernie.Berkeley.EDU!andy Mon May 29 14:55:38 1989
Subject: No personal expe with this yet... (hex.el)

HAve fun, please inform of any improv's you might make.
Andrew

/*
 * Andrew kindly mailed me lisp source for hex.el version 1.0. which is
 * pretty long. I will happily email it to any and all interested parties. 
 * Please send email to ..!uunet!slcpi!treed or treed@shearson.com.
 *
 * thanks - tim
 *
 */

From uunet!NMSU.Edu!ted Mon May 29 18:48:25 1989
Subject: gemacs binary editing mode?


here is an invertible hex dump package.  i use these programs for
patching when necessary by hex dumping, editing, and then un hex
dumping.  no guarantees.  please send corrections to me at
ted@nmsu.edu

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#      hd.c
#      unhd.c
# This archive created: Mon May 29 16:32:09 MDT 1989
if test -f 'hd.c'
then
	echo shar: will not over-write existing file 'hd.c'
else
sed -e '1,$s/^X//' << 'SHAR_EOF' > hd.c
X#include <stdio.h>
X#include <ctype.h>
X
X/* reasonably good hex dumper */
X
X/* 
Xusage is:
Xhd [option-list] [files]
Xoption-list is a string with a leading '-' followed by 
Xzero or more of the following
X
X	a	output addresses
X	h	output hex codes
X	t	output sanitized ascii representation (text)
X
Xa '-' alone is the same as -h
Xif no options are given -aht is assumed.
X
Xfiles is a list of file names to be dumped.  if no files are
Xspecified, then the standard input is used.  a '-' in place of a file
Xname specifies the standard input.
X
Xthe hex codes are surrounded by ( and ) so that unhd can find them.
X*/
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X    int addresses, hex, text;	/* flags for options */
X    int bytes;			/* number of bytes to show on a line */
X    int i;			/* handy for loops */
X    int width;			/* width of useable line */
X    FILE *file;
X
X    /* no flags defaults for everything */
X    addresses = text = hex = 1;
X
X    if (argc == 1) {
X    }
X    /* check for flags */
X    else if (argv[1][0] == '-') {
X	addresses = text = hex = 0;
X
X	/* scan a resonable number of characters */
X	for (i=1;i<5 && argv[1][i];i++) {
X	    switch (argv[1][i]) {
X
X		/* turn on address field */
X	      case 'a':
X		addresses = 1;
X		break;
X
X		/* turn on hex dump */
X	      case 'h':
X		hex = 1;
X		break;
X
X		/* turn on ascii dump */
X	      case 't':
X		text = 1;
X		break;
X	    }
X	}
X    }
X
X    /* set up the number of bytes to display on each line */
X    if (hex) {
X	bytes = 16;
X    }
X    else if (text) {
X	bytes = 64;
X    }
X    else {
X	fprintf(stderr,"must use either -h or -t");
X	exit(1);
X    }
X
X    /* if no files */
X    if (argc == 1 || (argc == 2 && argv[1][0] == '-')) {
X	copy(addresses,hex,text,bytes,stdin);
X    }
X    else {
X	/* or for all files that are specified */
X	for (i=(argv[1][0] == '-')+1;i<argc;i++) {
X	    if (argv[i][0] != '-') {
X		file = fopen(argv[i],"r");
X		if (file) copy(addresses,hex,text,bytes,file);
X		fclose(file);
X	    }
X	    else {
X		copy(addresses,hex,text,bytes,stdin);
X	    }
X	}
X    }
X}
X
X/* arguments to copy are the 3 flags, the number of bytes to 
X	print per line, and the input file */
Xcopy(a,h,t,b,in)
Xint a,h,t,b;
XFILE *in;
X{
X     long add;			/* address of this byte */
X     int n;			/* counter within a line */
X     int i;			/* loop counter */
X     int c;			/* current byte */
X     char buf[64];		/* where to stash chars for ascii dump */
X
X     /* initialize some counters and get the first character */
X     add = 0;
X     n = 0;
X     c = getc(in);
X
X     while (c != EOF) {
X	  /* print the address field if need be */
X	  if (!(add%b)) {
X	       if (a) printf("%6x ",add);
X	       if (h) printf("( ");
X	       else printf("(...) ");
X	       n = 0;
X	  }
X	  /* print a hex byte */
X	  if (h) {
X	      printf("%02x ",0xff & c);
X	  }
X
X	  /* perhaps save that byte */
X	  if (t) {
X	      buf[n] = c;
X	  }
X
X	  /* check for end of line time */
X	  if (n == b-1) {
X	       if (h) {		/* finish the hex bytes */
X		    printf(")");
X	       }
X
X	       if (t) {		/* should we display the text?? */
X		    /* do we need a gutter after the hex dump part? */
X		    if (h) printf("  ");
X
X		    /* print the characters */
X		    for (i=0;i<=n;i++) {
X			 /* if we can */
X			 if (isascii(buf[i]) && isprint(buf[i]))
X			      putchar(buf[i]);
X
X			 /* otherwise print something else */
X			 else putchar('.');
X		    }
X	       }
X	       /* end of line always gets this */
X	       putchar('\n');
X	       n = 0;
X	  }
X	  else {		/* not end of line */
X	       n++;
X	  }
X
X	  c = getc(in);
X	  add++;
X     }
X
X     /* pretty much done, may have started a partial line */
X     if (n) {
X	  if (h) {
X	       for (i=n;i<b;i++) printf("   ");
X	       printf(")");
X
X	       /* put in the gutter if we had a hex dump */
X	       if (h) printf("  ");
X	  }
X
X	  /* print any text dump that is needed */
X	  if (t) {
X	       /* and print the printable chars */
X	       for (i=0;i<n;i++) {
X		    if (isascii(buf[i]) && isprint(buf[i]))
X			 putchar(buf[i]);
X		    else putchar('.');
X	       }
X	  }
X
X	  /* even partial lines get this */
X	  putchar('\n');
X     }
X}
SHAR_EOF
echo recreated hd.c
chmod u=rw hd.c
chmod g=r hd.c
chmod o=r hd.c
fi
if test -f 'unhd.c'
then
	echo shar: will not over-write existing file 'unhd.c'
else
sed -e '1,$s/^X//' << 'SHAR_EOF' > unhd.c
X#include <stdio.h>
X#include <ctype.h>
X
X/* the hex UN-dumper....takes a stream of hexadecimal numbers and
X	converts them to bytes....usage is:
Xunhd files
X
Xwhere files is a list of files to be undumped.  if files is null, then
Xthe standard input is undumped.  only things in parentheses are
Xconverted.  this program is an inverse of hd.
X
X*/
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X    int i;
X    FILE *file;
X
X    if (argc == 1) undump(stdin);
X    else {
X	for (i=1;i<argc;i++) {
X	    file = fopen(argv[i],"r");
X	    if (file) undump(file);
X	    fclose(file);
X	}
X    }
X}
X
Xint unhex(c)
Xint c;
X{
X	if (isdigit(c)) return c-'0';
X	else return c-'a'+10;
X}
X
Xundump(in)
XFILE *in;
X{
X     int c;
X     int byte;
X
X     c = getc(in);
X     while (c != EOF) {		/* do each line */
X	  /* skip to the real stuff */
X	  while (c != EOF && c != '(' && c != '\n') c = getc(in);
X
X	  if (c == '\n') {	/* empty line? */
X	       c = getc(in);	/* go on to the next one */
X	  }
X	  else {
X	       /* eat the ( */
X	       if (c != EOF) c = getc(in);
X
X	       /* skip any fluffy whitespace */
X	       while (c != EOF && isspace(c)) c = getc(in);
X
X	       /* convert this line */
X	       while (c != ')') {
X		    /* can't have any garbage in the hex part */
X		    if (c == EOF || !isxdigit(c)) {
X			 fprintf(stderr,"not in unhd format!!\n");
X			 exit(1);
X		    }
X		    
X		    /* we know we see a hex digit */
X		    byte = unhex(c);
X		    c = getc(in);
X		    if (c != EOF && isxdigit(c)) {
X			 byte = byte*16+unhex(c);
X			 c = getc(in);
X		    }
X		    putchar(byte);
X		    
X		    while (c != EOF && isspace(c)) c = getc(in);
X	       }
X	       /* eat to the end of the line */
X	       while (c != EOF && c != '\n') c = getc(in);
X	       if (c != EOF) c = getc(in);
X	  }
X     }
X}
SHAR_EOF
echo recreated unhd.c
chmod u=rw unhd.c
chmod g=r unhd.c
chmod o=r unhd.c
fi
#	End of shell archive

-- 
name(Timothy Reed);
phone(718-797-4634);
UUCP(..!uunet!slcpi!treed | treed@shearson.com);
Mail(300 Union St^MBkyn, NY^M11231);