#! /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 the files:
#	atob.c
#	btoa.c
#	btoa.1
# This archive created: Thu Aug 30 13:18:56 1990
export PATH; PATH=/bin:$PATH
if test -f 'atob.c'
then
	echo shar: will not over-write existing file "'atob.c'"
else
cat << \SHAR_EOF > 'atob.c'
/* atob: version 4.0
 * stream filter to change printable ascii from "btoa" back into 8 bit bytes
 * if bad chars, or Csums do not match: exit(1) [and NO output]
 *
 *  Paul Rutter		Joe Orost
 *  philabs!per		petsd!joe
 */
     
#include <stdio.h>
     
#define reg register
     
#define streq(s0, s1)  strcmp(s0, s1) == 0
     
#define times85(x)	((((((x<<2)+x)<<2)+x)<<2)+x)
     
long int Ceor = 0;
long int Csum = 0;
long int Crot = 0;
long int word = 0;
long int bcount = 0;
     
fatal() {
  fprintf(stderr, "bad format or Csum to atob\n");
  exit(1);
}
     
#define DE(c) ((c) - '!')
     
decode(c)
  reg c;
{
  if (c == 'z') {
    if (bcount != 0) {
      fatal();
    } else {
      byteout(0);
      byteout(0);
      byteout(0);
      byteout(0);
    }
  } else if ((c >= '!') && (c < ('!' + 85))) {
    if (bcount == 0) {
      word = DE(c);
      ++bcount;
    } else if (bcount < 4) {
      word = times85(word);
      word += DE(c);
      ++bcount;
    } else {
      word = times85(word) + DE(c);
      byteout((int)((word >> 24) & 255));
      byteout((int)((word >> 16) & 255));
      byteout((int)((word >> 8) & 255));
      byteout((int)(word & 255));
      word = 0;
      bcount = 0;
    }
  } else {
    fatal();
  }
}
     
FILE *tmp_file;
     
byteout(c)
  reg c;
{
  Ceor ^= c;
  Csum += c;
  Csum += 1;
  if ((Crot & 0x80000000)) {
    Crot <<= 1;
    Crot += 1;
  } else {
    Crot <<= 1;
  }
  Crot += c;
  putc(c, tmp_file);
}
     
main(argc, argv)
  char **argv;
{
  reg c;
  reg long int i;
  char tmp_name[100];
  char buf[100];
  long int n1, n2, oeor, osum, orot;
     
  if (argc != 1) {
    fprintf(stderr,"bad args to %s\n", argv[0]);
    exit(2);
  }
  sprintf(tmp_name, "/usr/tmp/atob.%x", getpid());
  tmp_file = fopen(tmp_name, "w+");
  if (tmp_file == NULL) {
    fatal();
  }
  unlink(tmp_name);	/* Make file disappear */
  /*search for header line*/
  for (;;) {
    if (fgets(buf, sizeof buf, stdin) == NULL) {
      fatal();
    }
    if (streq(buf, "xbtoa Begin\n")) {
      break;
    }
  }
     
  while ((c = getchar()) != EOF) {
    if (c == '\n') {
      continue;
    } else if (c == 'x') {
      break;
    } else {
      decode(c);
    }
  }
  if(scanf("btoa End N %ld %lx E %lx S %lx R %lx\n",
         &n1, &n2, &oeor, &osum, &orot) != 5) {
    fatal();
  }
  if ((n1 != n2) || (oeor != Ceor) || (osum != Csum) || (orot != Crot)) {
    fatal();
  } else {
    /*copy OK tmp file to stdout*/;
    fseek(tmp_file, 0L, 0);
    for (i = n1; --i >= 0;) {
      putchar(getc(tmp_file));
    }
  }
  exit(0);

}
SHAR_EOF
fi # end of overwriting check
if test -f 'btoa.c'
then
	echo shar: will not over-write existing file "'btoa.c'"
else
cat << \SHAR_EOF > 'btoa.c'
/* btoa: version 4.0
 * stream filter to change 8 bit bytes into printable ascii
 * computes the number of bytes, and three kinds of simple checksums
 * incoming bytes are collected into 32-bit words, then printed in base 85
 *  exp(85,5) > exp(2,32)
 * the ASCII characters used are between '!' and 'u'
 * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
 *
 *  Paul Rutter		Joe Orost
 *  philabs!per		petsd!joe
 *
 *  WARNING: this version is not compatible with the original as sent out
 *  on the net.  The original encoded from ' ' to 't'; which cause problems
 *  with some mailers (stripping off trailing blanks).
 */
     
#include <stdio.h>
     
#define reg register
     
#define MAXPERLINE 78
     
long int Ceor = 0;
long int Csum = 0;
long int Crot = 0;
     
long int ccount = 0;
long int bcount = 0;
long int word;
     
#define EN(c)	(int) ((c) + '!')
     
encode(c)
  reg c;
{
  Ceor ^= c;
  Csum += c;
  Csum += 1;
  if ((Crot & 0x80000000)) {
    Crot <<= 1;
    Crot += 1;
  } else {
    Crot <<= 1;
  }
  Crot += c;
     
  word <<= 8;
  word |= c;
  if (bcount == 3) {
    wordout(word);
    bcount = 0;
  } else {
    bcount += 1;
  }
}
     
wordout(word)
  reg long int word;
{
  if (word == 0) {
    charout('z');
  } else {
    reg int tmp = 0;
     
    if(word < 0) {	/* Because some don't support unsigned long */
      tmp = 32;
      word = word - (long)(85 * 85 * 85 * 85 * 32);
    }
    if(word < 0) {
      tmp = 64;
      word = word - (long)(85 * 85 * 85 * 85 * 32);
    }
    charout(EN((word / (long)(85 * 85 * 85 * 85)) + tmp));
    word %= (long)(85 * 85 * 85 * 85);
    charout(EN(word / (85 * 85 * 85)));
    word %= (85 * 85 * 85);
    charout(EN(word / (85 * 85)));
    word %= (85 * 85);
    charout(EN(word / 85));
    word %= 85;
    charout(EN(word));
  }
}
     
charout(c) {
  putchar(c);
  ccount += 1;
  if (ccount == MAXPERLINE) {
    putchar('\n');
    ccount = 0;
  }
}
     
main(argc,argv)
  char **argv;
{
  reg c;
  reg long int n;
     
  if (argc != 1) {
    fprintf(stderr,"bad args to %s\n", argv[0]);
    exit(2);
  }
  printf("xbtoa Begin\n");
  n = 0;
  while ((c = getchar()) != EOF) {
    encode(c);
    n += 1;
  }
  while (bcount != 0) {
    encode(0);
  }
  /* n is written twice as crude cross check*/
  printf("\nxbtoa End N %ld %lx E %lx S %lx R %lx\n", n, n, Ceor, Csum, Crot);
  exit(0);

}
SHAR_EOF
fi # end of overwriting check
if test -f 'btoa.1'
then
	echo shar: will not over-write existing file "'btoa.1'"
else
cat << \SHAR_EOF > 'btoa.1'
.TH BTOA 1 local
.SH NAME
btoa, atob, tarmail, untarmail \- encode/decode binary to printable ASCII
.SH SYNOPSIS
.B btoa
.br
.B atob
.br
.B tarmail
who subject files ...
.br
.B untarmail
[ file ]
.SH DESCRIPTION
.I Btoa
is a filter that reads anything from the standard input, and encodes it into
printable ASCII on the standard output.  It also attaches a header and checksum
information used by the reverse filter
.I atob
to find the start of the data and to check integrity.
.PP
.I Atob
reads an encoded file, strips off any leading and
trailing lines added by mailers, and recreates a copy of the original file
on the standard output.
.I Atob
gives NO output (and exits with an error message) if its input is garbage or
the checksums do not check.
.PP
.I Tarmail
is a shell script that tar's up all the given files, pipes them
through
.IR compress ","
.IR btoa ","
and mails them to the given person with the given subject phrase.  For
example:
.PP
.in 1i
tarmail ralph "here it is ralph" foo.c a.out
.in -1i
.PP
Will package up files "foo.c" and "a.out" and mail them to "ralph" using
subject "here it is ralph".  Notice the quotes on the subject.  They are
necessary to make it one argument to the shell.
.PP
.I Tarmail
with no args will print a short message reminding you what the required args
are.  When the mail is received at the other end, that person should use
mail to save the message in some temporary file name (say "xx").
Then saying "untarmail xx"
will decode the message and untar it.
.I Untarmail
can also be used as a filter.  By using
.IR tarmail ","
binary files and
entire directory structures can be easily transmitted between machines.
Naturally, you should understand what tar itself does before you use
.IR tarmail "."
.PP
Other uses:
.PP
compress < secrets | crypt | btoa | mail ralph
.PP
will mail the encrypted contents of the file "secrets" to ralph.  If ralph
knows the encryption key, he can decode it by saving the mail (say in "xx"),
and then running:
.PP
atob < xx | crypt | uncompress
.PP
(crypt requests the key from the terminal,
and the "secrets" come out on the terminal).
.SH AUTHOR
Paul Rutter (modified by Joe Orost)
.SH FEATURES
.I Btoa
uses a compact base-85 encoding so that
4 bytes are encoded into 5 characters (file is expanded by 25%).
As a special case, 32-bit zero is encoded as one character.  This encoding
produces less output than
.IR uuencode "(1)."
.SH "SEE ALSO"
compress(1), crypt(1), uuencode(1), mail(1)
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0
