[comp.sources.d] Towards minimalist self-decompressing 'shar'chives

jaw@ames.UUCP (James A. Woods) (06/02/87)

#  "You've heard of Ali Baba -- forty thieves had he.
    Out for what we all want: lots of LSD."            -- V. Stanshall

     Re-directing a discussion from comp.sys.sources started by Karl Fox,
below we append the production version of a self-decompressing sharchive
maker.  It's yet another few hundred characters shorter (mainly from
combining 'atob' and 'zcat' into one program), but not enough to qualify
for 4th International Obfuscation Contest status!  The 1991-character script
still has a little fat in the way of "literary" allusion, preserved at
amazing cost for sheer perversity.  This one offers more robust tempfile
handling, swifter execution due to the single compilation, but more
importantly, capability to completely short-circuit the compile if the
requisite code is already installed on the target system.

     Raison-d'etre for this 'shar' remains its inherent nose-thumb at
what kind/version of decoding the other side might entertain.  You can
change things right out from under the recipient if desired, without need
for a "courier" to deliver the "key".  The basic idea probably goes back
to Claude Shannon.

     A contraindication for the shar would be for packaging USENET
postings, since the transport mechanism already does compression.
However, it should be just the ticket for a UNIX archive server.

-- James A. Woods (ames!jaw)

P.S. I dedicate the opaque-yet-functional brevity of what follows
to Donovan Fong, wherever he may be.

-------------- a bent 52-line compressing 'shar' machine ------------
echo '#!/bin/sh
# "Cleverly he dialed from within." -- D. Van Vliet, "Trout Mask Replica"
trap "rm -f zcat atob az$$*;exit" 0 1 2 3 15
echo decoding...
cat>az$$.c<<\_'
cat<<'TROUSER PRESS'
#include<stdio.h>
#define d define
#d R register
#d I if
#d E else
#d W while
#d C char
#d L long
#d U unsigned
#d B(n) (1<<(n))
#d K bcount
#d G getchar()
#d P putchar
#d X return
#d J 255
#d H(n) ((C*)h)[n]
#d V(a,b) (a&(B(b)-1))
#d Q 256
#d Y w=w*85+c-'!',
#d Z w=Q;W(w--)t[w]=0
#d x break;
#d F {fprintf(stderr,"shar botch: resend\n");X;}
L K,o,v,w,c,m,M=B(16),f;int i,q,b,n,k=16,e=128,j,O,S;U short t[69001];U L
h[69001];C D[J];FILE*T;d(c)R c;{I(c=='z'){I(K)F E{c=4;W(c--)y(0);}}E I(c>='!'
&&(c<'!'+85)){I(!K)w=c-'!',++K;E I(K<4)Y++K;E Y y(w>>24),y(w>>16),y(w>>8),y(w)
,w=K=0;}E F;}y(l)L l;{R c=(int)(l&J);o^=c;m+=c;m++;I(f&(1<<31))f*=2,f++;E f*=2
;f+=c;putc(c,T);}main(g,v)C**v;{R c;L i;L A,h,y,s,r;I(**v!='a')X(a());sprintf(
D,mktemp("azXXXXXX"));I(!(T=fopen(D,"w+")))F;unlink(D);W(1){I(!fgets(D,J,stdin
))F;I(!strcmp(D,"xbtoa Begin\n"))x}W((c=G)>=0){I(c=='\n')continue;E I(c=='x')x
E d(c);}I(scanf("btoa End N %ld %lx E %lx S %lx R %lx\n",&A,&h,&y,&s,&r)!=5)F;
I(A!=h||y!=o||s!=m||r!=f)F E{fseek(T,0L,0);i=A;W(i--)P(getc(T));}}L g(){R C*p=
D;I(j>0||O>=S||f>m){I(f>m)m= ++n==k?M:B(n)-1;I(j>0)m=B(n=9)-1,j=0;S=fread(D,1,
n,stdin);I(S<1)X-1;O=0;S=S*8-n+1;}q=O;b=n;p+=q>>3;q&=7;c=V(*p++>>q,8-q);q=8-q;
b-=q;I(b>7)c|=(*p++&J)<<q,q+=8,b-=8;c|=V(*p,b)<<q;O+=n;X c;}a(){I(G!=31||G!=
157)F;k=G;e=k&128;k&=31;M=B(k);I(k>16)F;z();}z(){L w;R C*s;m=B(n=9)-1;Z,H(w)=
(C)w;f=e?257:Q;i=o=g();I(o==-1)X;P((C)i);s=(C*)&H(B(16));W((w=g())>=0){I(w==Q
&&e){Z;j=1;f=Q;I((w=g())==-1)x}v=w;I(w>=f)*s++=i,w=o;W((U L)w>=(U L)Q)*s++=
H(w),w=t[w];*s++=i=H(w);do P(*--s);W(s>&H(B(16)));I((w=f)<M)t[w]=(U short)o,
H(w)=i,f=w+1;o=v;}}
TROUSER PRESS
echo '_
(echo T|compress|btoa|atob|zcat)2>/dev/null 1>&2||
(cc -o zcat az$$.c;ln zcat atob)
(atob|zcat|tar xvf -)<<\Feilgau'
tar cf - $*|compress|btoa
echo "Feilgau"

wesommer@bloom-beacon.UUCP (06/02/87)

What an utterly amazing and obscure compression job.  Alas, it doesn't
work for paranoids who don't have '.' in their search path, and who
are on systems without zcat and atob.  You may have to sacrifice a few
extra characters for that.

				Bill Sommerfeld
			  ARPA: wesommer@athena.mit.edu
			  UUCP: ...!mit-eddie!wesommer

jaw@aurora.UUCP (06/03/87)

# "Invest in dada."  -- subliminal, anon.

     The fix to W. Sommerfeld's quandry (leaving "dot" out of PATH is
indeed strange for California) is incorporated below.  Also another
~30-byte shortening, in the style to which I would not want to get
accustomed.  I'm not sure if I've "re-used" every possible variable,
but you (or your globally-optimizing compiler) may go blind finding out.

     For the newcomer, this script is like 'tarmail', but with *no* 
requirement for the recipient to have 'untarmail', 'zcat', or 'atob'.
It's useful enough here at ames-aurora.arpa to simply replace 'shar'.
The sender, of course, still must have an executable 'compress'
and 'btoa'.  E.g.
	
	sender:    'shark file1 file2 ... | mail santa\!northpole'
		     (~30% compression for sufficiently large e-mail)
	receiver:  'sh mailarchive'
		     (look ma, no pre-installed decoder!)

ames!jaw

----------------------- cut here for a new-wave 'shar' ------------------
echo '#!/bin/sh
# "Cleverly he dialed from within." -- D. Van Vliet, "Trout Mask Replica"
PATH=$PATH:.
trap "rm -f zcat atob az$$*;exit" 0 1 2 3 15
echo decoding...
cat>az$$.c<<\_'
cat<<'TROUSER PRESS'
#include<stdio.h>
#define d define
#d I if
#d E else
#d W while
#d C char
#d L long
#d U unsigned
#d B(n) (1<<(n))
#d K bcount
#d G getchar()
#d P putchar
#d X return
#d H(n) ((C*)h)[n]
#d V(a,b) (B(b)-1&a)
#d Q 256
#d Y w=w*85+c-33,
#d Z w=Q;W(w--)t[w]=0
#d x break;
#d F {X(write(2,"shar botch: resend\n",19));}
L K,o,v,w,c,m,M=B(16),f;int i,q,b,n,k=16,e=128,j,O,S;U short t[69001];FILE*T;U
L h[69001];C D[255];d(c)int c;{I(c=='z'){I(K)F E{c=4;W(c--)y(0);}}E I(c>32&&c<
118){I(!K)w=c-33,++K;E I(K<4)Y++K;E Y y(w>>24),y(w>>16),y(w>>8),y(w),w=K=0;}E
F;}y(l)L l;{c=(int)(l&255);o^=c;m+=c;m++;I(1<<31&f)f*=2,f++;E f*=2;f+=c;putc(c
,T);}main(g,v)C**v;{int c;L i,A,h,y,s,r;I(**v!=97)X(a());sprintf(D,mktemp(
"azXXXXXX"));I(T=fopen(D,"w+"))unlink(D);E F;W(1){I(!fgets(D,255,stdin))F;I(!
strcmp(D,"xbtoa Begin\n"))x}W((c=G)>=0){I(c==10)continue;E I(c=='x')x E d(c);}
I(scanf("btoa End N %ld %lx E %lx S %lx R %lx\n",&A,&h,&y,&s,&r)!=5)F;I(A!=h||
y!=o||s!=m||r!=f)F E{fseek(T,0L,0);i=A;W(i--)P(getc(T));}}L g(){C*p=D;I(j>0||O
>=S||f>m){I(f>m)m= ++n==k?M:B(n)-1;I(j>0)m=B(n=9)-1,j=0;S=fread(D,1,n,stdin);I
(S<1)X-1;O=0;S=S*8-n+1;}q=O;b=n;p+=q>>3;q&=7;c=V(*p++>>q,8-q);q=8-q;b-=q;I(b>7
)c|=(*p++&255)<<q,q+=8,b-=8;c|=V(*p,b)<<q;O+=n;X c;}a(){I(G!=31||G!=157)F;k=G;
e=k&128;k&=31;M=B(k);I(k>16)F;z();}z(){L w;C*s;m=B(n=9)-1;Z,H(w)=(C)w;f=e?257:
Q;i=o=g();I(o==-1)X;P((C)i);s=(C*)&H(B(16));W((w=g())>=0){I(w==Q&&e){Z;j=1;f=Q
;I((w=g())==-1)x}v=w;I(w>=f)*s++=i,w=o;W((U L)w>=(U L)Q)*s++=H(w),w=t[w];*s++=
i=H(w);do P(*--s);W(s>&H(B(16)));I((w=f)<M)t[w]=(U short)o,H(w)=i,f=w+1;o=v;}}
TROUSER PRESS
echo '_
(set|compress|btoa|atob|zcat)2>/dev/null 1>&2||
(cc -o zcat az$$.c;ln zcat atob)
(atob|zcat|tar xvf -)<<\TROUSERS'
tar cf - $*|compress|btoa
echo TROUSERS