/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"$Header: unpack.c 1.8 89/11/28 $@(#)unpack:unpack.c	1.15"
/*
 *	Huffman decompressor
 *	Usage:	pcat filename...
 *	or	unpack filename...
 */

#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>

extern void exit();

#ifdef lint
	int	_void_;
#	define VOID	_void_ = (int)
#else
#	define VOID
#endif

jmp_buf env;
struct	stat status;
char	*argv0, *argvk;
int	rmflg = 0;	/* rmflg, when set it's ok to rm arvk file on caught signals */
short	errorm;

#define NAMELEN MAXPATHLEN
#define SUF0	'.'
#define SUF1	'z'
#define US	037
#define RS	036

/* variables associated with i/o */
char	filename[NAMELEN+2];
short	infile;
short	outfile;
short	inleft;
char	*inp;
char	*outp;
char	inbuff[8*BUFSIZ];
char	outbuff[8*BUFSIZ];

/* the dictionary */
long	origsize;
short	maxlev;
short	intnodes[25];
char	*tree[25];
char	characters[256];
char	*eof;

/* read in the dictionary portion and build decoding structures */
/* return 1 if successful, 0 otherwise */
getdict ()
{
	register int c, i, nchildren;

	/*
	 * check two-byte header
	 * get size of original file,
	 * get number of levels in maxlev,
	 * get number of leaves on level i in intnodes[i],
	 * set tree[i] to point to leaves for level i
	 */
	eof = &characters[0];

	inbuff[6] = 25;
	inleft = read (infile, &inbuff[0], 8*BUFSIZ);
	if (inleft < 0) {
		eprintf (gettxt("unpack:1", ".z: read error"));
		return (0);
	}
	if (inbuff[0] != US)
		goto goof;

	if (inbuff[1] == US) {		/* oldstyle packing */
		if (setjmp (env))
			return (0);
		expand ();
		return (1);
	}
	if (inbuff[1] != RS)
		goto goof;

	inp = &inbuff[2];
	origsize = 0;
	for (i=0; i<4; i++)
		origsize = origsize*256 + ((*inp++) & 0377);
	maxlev = *inp++ & 0377;
	if (maxlev > 24) {
goof:		eprintf (gettxt("unpack:2", ".z: not in packed format"));
		return (0);
	}
	for (i=1; i<=maxlev; i++)
		intnodes[i] = *inp++ & 0377;
	for (i=1; i<=maxlev; i++) {
		tree[i] = eof;
		for (c=intnodes[i]; c>0; c--) {
			if (eof >= &characters[255])
				goto goof;
			*eof++ = *inp++;
		}
	}
	*eof++ = *inp++;
	intnodes[maxlev] += 2;
	inleft -= inp - &inbuff[0];
	if (inleft < 0)
		goto goof;

	/*
	 * convert intnodes[i] to be number of
	 * internal nodes possessed by level i
	 */

	nchildren = 0;
	for (i=maxlev; i>=1; i--) {
		c = intnodes[i];
		intnodes[i] = nchildren /= 2;
		nchildren += c;
	}
	return (decode ());
}

/* unpack the file */
/* return 1 if successful, 0 otherwise */
decode ()
{
	register int bitsleft, c, i;
	int j, lev;
	char *p;

	outp = &outbuff[0];
	lev = 1;
	i = 0;
	while (1) {
		if (inleft <= 0) {
			inleft = read (infile, inp = &inbuff[0], 8*BUFSIZ);
			if (inleft < 0) {
				eprintf (gettxt("unpack:1", ".z: read error"));
				return (0);
			}
		}
		if (--inleft < 0) {
uggh:			eprintf (gettxt("unpack:3", ".z: unpacking error"));
			return (0);
		}
		c = *inp++;
		bitsleft = 8;
		while (--bitsleft >= 0) {
			i *= 2;
			if (c & 0200)
				i++;
			c <<= 1;
			if ((j = i - intnodes[lev]) >= 0) {
				p = &tree[lev][j];
				if (p == eof) {
					c = outp - &outbuff[0];
					if (write (outfile, &outbuff[0], c) != c) {
wrerr:						eprintf (gettxt("unpack:4", ": write error"));
						return (0);
					}
					origsize -= c;
					if (origsize != 0)
						goto uggh;
					return (1);
				}
				*outp++ = *p;
				if (outp == &outbuff[8*BUFSIZ]) {
					if (write (outfile, outp = &outbuff[0], 8*BUFSIZ) != 8*BUFSIZ)
						goto wrerr;
					origsize -= 8*BUFSIZ;
				}
				lev = 1;
				i = 0;
			} else
				lev++;
		}
	}
}

main (argc, argv)
	char *argv[];
{
	register i, k;
	int sep, pcat = 0;
	register char *p1, *cp;
	int fcount = 0;		/* failure count */
	int (*onsig)();
	struct {
		time_t actime;
		time_t modtime;
	} times;
	struct stat statb;


	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
		signal(SIGHUP, onsig);
	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
		signal(SIGINT, onsig);
	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
		signal(SIGTERM, onsig);

	p1 = *argv;
	while(*p1++);		/* Point p1 to end of argv[0] string */
	while(--p1 >= *argv)
		if(*p1 == '/')break;
	*argv = p1 + 1;
	argv0 = argv[0];
	if(**argv == 'p')pcat++;	/* User entered pcat (or /xx/xx/pcat) */
	for (k=1; k<argc; k++) {
		errorm = 0;
		sep = -1;
		cp = filename;
		argvk = argv[k];
		for (i=0; i < (NAMELEN-3) && (*cp = argvk[i]); i++)
			if (*cp++ == '/')
				sep = i;
		if (cp[-1] == SUF1 && cp[-2] == SUF0) {
			argvk[i-2] = '\0'; /* Remove suffix and try again */
			k--;
			continue;
		}

		fcount++;	/* expect the worst */
		if (i >= (NAMELEN-3) || (i - sep) > DYNIX_MAXNAMLEN-1) {
			eprintf (gettxt("unpack:5", ": file name too long"));
			goto done;
		}
		*cp++ = SUF0;
		*cp++ = SUF1;
		*cp = '\0';
		if (stat(filename, &statb) != -1) {
			if ((statb.st_mode & S_IFMT) == S_IFDIR) {
				eprintf (gettxt("unpack:12", ".z: is a directory"));
				goto done;
			}
			if ((statb.st_mode & S_IFMT) == S_IFCHR) {
				eprintf (gettxt("unpack:13", ".z: is a character special file"));
				goto done;
			}
			if ((statb.st_mode & S_IFMT) == S_IFBLK) {
				eprintf (gettxt("unpack:14", ".z: is a block special file"));
				goto done;
			}
		}

		if ((infile = open (filename, 0)) == -1) {
			eprintf (gettxt("unpack:6", ".z: cannot open"));
			goto done;
		}

		if (pcat)
			outfile = 1;	/* standard output */
		else {
			if (stat (argvk, &status) != -1) {
				eprintf (gettxt("unpack:7", ": already exists"));
				goto done;
			}
			VOID fstat (infile, &status);
			if (status.st_nlink != 1)
				eprintf (gettxt("unpack:8", ".z: Warning: file has links"));
			if ((outfile = creat (argvk, status.st_mode&07777)) == -1) {
				eprintf (gettxt("unpack:9", ": cannot create"));
				goto done;
			}

			rmflg = 1;
			if (chmod (argvk, status.st_mode) != 0)
			    printf(gettxt("unpack:10", "can't change mode to %o\n"), status.st_mode);
			VOID chown (argvk, status.st_uid, status.st_gid);
		}

		if (getdict ()) {	/* unpack */
			fcount--; 	/* success after all */
			if (!pcat) {
				/*
				 * preserve acc & mod dates
				 */
				times.actime = status.st_atime;
				times.modtime = status.st_mtime;
				VOID utime (argvk, &times);
				rmflg = 0;
				eprintf (gettxt("unpack:11", ": unpacked"));
				VOID unlink (filename);

			}
		}
		else
			if (!pcat)
				VOID unlink (argvk);
done:		if (errorm)
			VOID fprintf (stderr, "\n");
		VOID close (infile);
		if (!pcat)
			VOID close (outfile);
	}
	return (fcount);
}

eprintf (s)
	char *s;
{
	if (!errorm) {
		errorm = 1;
		VOID fprintf (stderr, gettxt("colon:43", "%s: %s"), argv0, argvk);
	}
	VOID fprintf (stderr, s);
}

/*
 * This code is for unpacking files that
 * were packed using the previous algorithm.
 */

int	Tree[1024];

expand ()
{
	register tp, bit;
	short word;
	int keysize, i, *t;

	outp = outbuff;
	inp = &inbuff[2];
	inleft -= 2;
	origsize = ((long) (unsigned) getwd ())*256*256;
	origsize += (unsigned) getwd ();
	t = Tree;
	for (keysize = getwd (); keysize--; ) {
		if ((i = getch ()) == 0377)
			*t++ = getwd ();
		else
			*t++ = i & 0377;
	}

	bit = tp = 0;
	for (;;) {
		if (bit <= 0) {
			word = getwd ();
			bit = 16;
		}
		tp += Tree[tp + (word<0)];
		word <<= 1;
		bit--;
		if (Tree[tp] == 0) {
			putch (Tree[tp+1]);
			tp = 0;
			if ((origsize -= 1) == 0) {
				write (outfile, outbuff, outp - outbuff);
				return;
			}
		}
	}
}

getch ()
{
	if (inleft <= 0) {
		inleft = read (infile, inp = inbuff, 8*BUFSIZ);
		if (inleft < 0) {
			eprintf (gettxt("unpack:1", ".z: read error"));
			longjmp (env, 1);
		}
	}
	inleft--;
	return (*inp++ & 0377);
}

getwd ()
{
	register char c;
	register d;
	c = getch ();
	d = getch ();
	d <<= 8;
	d |= c & 0377;
	return (d);
}

void
onsig()
{
				/* could be running as unpack or pcat		*/
				/* but rmflg is set only when running	*/
				/* as unpack and only when file is	*/
				/* created by unpack and not yet done	*/
	if (rmflg == 1)
		VOID unlink(argvk);
	exit(1);
}

putch (c)
	char c;
{
	register n;

	*outp++ = c;
	if (outp == &outbuff[8*BUFSIZ]) {
		n = write (outfile, outp = outbuff, 8*BUFSIZ);
		if (n < 8*BUFSIZ) {
			eprintf (gettxt("unpack:4", ": write error"));
			longjmp (env, 2);
		}
	}
}
