#/bin/make
# @(#)Makefile	11.1 13 May 1995 17:27:10
#
# krypt - Noll Lightning Cypher based on SHS
#
# This makefile was written by:
#
#	 Landon Curt Noll  (chongo@toad.com)	chongo <was here> /\../\
#
# This makefile has been placed in the public domain.  Please do not 
# copyright this makefile.
#
# LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH  REGARD  TO
# THIS  SOFTWARE,  INCLUDING  ALL IMPLIED WARRANTIES OF MER-
# CHANTABILITY AND FITNESS.  IN NO EVENT SHALL  LANDON  CURT
# NOLL  BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM  LOSS  OF
# USE,  DATA  OR  PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR  IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# WARNING: This code, and the algorithm that it claims to implement
#	    are under-going peer review.  During peer review, both
#	    source and algorithm may be modified extensively.
#
# See krypt.c for version and modification history.

# tools
#
SHELL= /bin/sh
CFLAGS= ${OPT_DEBUG}
DIFF= diff
UUENCODE= uuencode
UUDECODE= uudecode
GUNZIP= gunzip
INSTALL= install
SED= sed
TAR= tar
DD= dd

# install locations
#
DESTDIR= /usr/local/bin
DESTLIBTOP= /usr/local/lib
DESTMAN= /usr/local/man/man1
MANEXT= 1

# test files
#
TEST= ${TEST_ALLTXT} ${TEST_CYPB} ${TEST_CYPL} ${TEST_TXT}
TEST_ALLTXT= trial/all.cypb.001 trial/all.cypb.256 trial/all.cypl.001 \
    trial/all.cypl.256 trial/alltxt 
TEST_CYPB= trial/cypb.001 trial/cypb.002 \
    trial/cypb.003 trial/cypb.004 trial/cypb.005 trial/cypb.060 trial/cypb.061 \
    trial/cypb.062 trial/cypb.063 trial/cypb.064 trial/cypb.065 trial/cypb.066 \
    trial/cypb.067 trial/cypb.068 trial/cypb.069 trial/cypb.124 trial/cypb.125 \
    trial/cypb.126 trial/cypb.127 trial/cypb.128 trial/cypb.129 trial/cypb.130 \
    trial/cypb.131 trial/cypb.132 trial/cypb.133 trial/cypb.188 trial/cypb.189 \
    trial/cypb.190 trial/cypb.191 trial/cypb.192 trial/cypb.193 trial/cypb.194 \
    trial/cypb.195 trial/cypb.196 trial/cypb.197 trial/cypb.252 trial/cypb.253 \
    trial/cypb.254 trial/cypb.255 trial/cypb.256 
TEST_CYPL= trial/cypl.001 trial/cypl.002 \
    trial/cypl.003 trial/cypl.004 trial/cypl.005 trial/cypl.060 trial/cypl.061 \
    trial/cypl.062 trial/cypl.063 trial/cypl.064 trial/cypl.065 trial/cypl.066 \
    trial/cypl.067 trial/cypl.068 trial/cypl.069 trial/cypl.124 trial/cypl.125 \
    trial/cypl.126 trial/cypl.127 trial/cypl.128 trial/cypl.129 trial/cypl.130 \
    trial/cypl.131 trial/cypl.132 trial/cypl.133 trial/cypl.188 trial/cypl.189 \
    trial/cypl.190 trial/cypl.191 trial/cypl.192 trial/cypl.193 trial/cypl.194 \
    trial/cypl.195 trial/cypl.196 trial/cypl.197 trial/cypl.252 trial/cypl.253 \
    trial/cypl.254 trial/cypl.255 trial/cypl.256 
TEST_TXT= trial/txt.001 trial/txt.002 \
    trial/txt.003 trial/txt.004 trial/txt.005 trial/txt.060 trial/txt.061 \
    trial/txt.062 trial/txt.063 trial/txt.064 trial/txt.065 trial/txt.066 \
    trial/txt.067 trial/txt.068 trial/txt.069 trial/txt.124 trial/txt.125 \
    trial/txt.126 trial/txt.127 trial/txt.128 trial/txt.129 trial/txt.130 \
    trial/txt.131 trial/txt.132 trial/txt.133 trial/txt.188 trial/txt.189 \
    trial/txt.190 trial/txt.191 trial/txt.192 trial/txt.193 trial/txt.194 \
    trial/txt.195 trial/txt.196 trial/txt.197 trial/txt.252 trial/txt.253 \
    trial/txt.254 trial/txt.255 trial/txt.256

# select the proper compiler
#
#CC= gcc -ansi
#CC= gcc -ansi -fcaller-saves
CC= cc

# select the proper optimization or debug level
#
#OPT_DEBUG= -O -DDEBUG
#OPT_DEBUG= -O2 -DDEBUG
#OPT_DEBUG= -O3 -DDEBUG
#OPT_DEBUG= -g -DDEBUG
#OPT_DEBUG= -O
OPT_DEBUG= -O2
#OPT_DEBUG= -O3
#OPT_DEBUG= -g

# Some architectures such as Sparc do not allow one to access long that
# are not aligned.  If make test dumps core or fails to produce no
# output, use only -DMUST_ALIGN.
#
# ALIGN=		let must_align.c figure out if alignment is needed
# ALIGN=-DMUST_ALIGN 	force alignment (at the cost of performance)
# ALIGN=-UMUST_ALIGN	allow non-alignment (usually at a performance gain)
#
ALIGN=
#ALIGN=-DMUST_ALIGN
#ALIGN=-UMUST_ALIGN

all: krypt

# perform all tests
test: krypt krypttest

krypt: cypher.o krypt.o
	${CC} ${CFLAGS} cypher.o krypt.o -o krypt

cypher.o: cypher.c cypher.h align.h endian.h
	${CC} ${CFLAGS} cypher.c -c

krypt.o: krypt.c cypher.h endian.h
	${CC} ${CFLAGS} krypt.c -c

align.h: must_align
	-./must_align > align.h 2>/dev/null
	@-rm -f core core.must_align must_align.core

must_align: must_align.c
	${CC} ${CFLAGS} ${ALIGN} must_align.c -o must_align

endian.h: endian
	rm -f endian.h
	./endian > endian.h

endian: endian.c
	${CC} ${CFLAGS} endian.c -o endian

# test the krypt tool
#
krypttest: krypt ${TEST}
	@echo This krypt -b test should produce no output
	@for i in ${TEST_TXT}; do \
	    L=`echo $$i | ${SED} -e 's/[^0-9][^0-9]*//' -e 's/^00*//'`; \
	    ./krypt -b $$i | ././krypt -b -r | \
	      ${DD} bs=$$L count=1 2>/dev/null | cmp - $$i; \
	done
	@./krypt -b trial/alltxt | ././krypt -b -r | cmp - trial/alltxt
	@./krypt -b -K trial/alltxt trial/alltxt | \
	   ././krypt -b -r -K trial/alltxt | cmp - trial/alltxt
	@./krypt -b -K trial/alltxt trial/txt.001 | \
	   ././krypt -b -r -K trial/alltxt | \
	   ${DD} bs=1 count=1 2>/dev/null | cmp - trial/txt.001
	@echo This krypt -l test should produce no output
	@for i in ${TEST_TXT}; do \
	    L=`echo $$i | ${SED} -e 's/[^0-9][^0-9]*//' -e 's/^00*//'`; \
	    ./krypt -l $$i | ././krypt -l -r | \
	      ${DD} bs=$$L count=1 2>/dev/null | cmp - $$i; \
	done
	@./krypt -l trial/alltxt | ././krypt -l -r | cmp - trial/alltxt
	@./krypt -l -K trial/alltxt trial/alltxt | \
	   ././krypt -l -r -K trial/alltxt | cmp - trial/alltxt
	@./krypt -l -K trial/alltxt trial/txt.001 | \
	   ././krypt -l -r -K trial/alltxt | \
	   ${DD} bs=1 count=1 2>/dev/null | cmp - trial/txt.001
	@echo This krypt -b -r test should produce no output
	@for i in ${TEST_TXT}; do \
	    X=`echo $$i | ${SED} -e 's/[^0-9][^0-9]*//'`; \
	    L=`echo $$X | ${SED} -e 's/^00*//'`; \
	    ./krypt -b -r -K $$i trial/cypb.$$X | \
	      ${DD} bs=$$L count=1 2>/dev/null | cmp - $$i; \
	done
	@./krypt -b -r -K trial/alltxt trial/all.cypb.001 | \
	    ${DD} bs=1 count=1 2>/dev/null | cmp - trial/txt.001
	@./krypt -b -r -K trial/alltxt trial/all.cypb.256 | cmp - trial/txt.256
	@echo This krypt -l -r test should produce no output
	@for i in ${TEST_TXT}; do \
	    X=`echo $$i | ${SED} -e 's/[^0-9][^0-9]*//'`; \
	    L=`echo $$X | ${SED} -e 's/^00*//'`; \
	    ./krypt -l -r -K $$i trial/cypl.$$X | \
	      ${DD} bs=$$L count=1 2>/dev/null | cmp - $$i; \
	done
	@./krypt -l -r -K trial/alltxt trial/all.cypl.001 | \
	    ${DD} bs=1 count=1 2>/dev/null | cmp - trial/txt.001
	@./krypt -l -r -K trial/alltxt trial/all.cypl.256 | cmp - trial/txt.256

# form the trials
#
trial ${TEST}: trial.${TAR}.z.UU
	-@if [ -f krypt.c ]; then \
	    echo setup krypt test suite; \
	    rm -f trial.${TAR}.z.uu; \
	    ${SED} -e 's/{/%/g' trial.${TAR}.z.UU > trial.${TAR}.z.uu; \
	    rm -f trial.${TAR}.z; \
	    ${UUDECODE} trial.${TAR}.z.uu; \
	    rm -f trial.${TAR}.z.uu; \
	    rm -f trial.${TAR}; \
	    ${GUNZIP} -v trial.${TAR}.z; \
	    rm -f trial.${TAR}.z; \
	    rm -rf trial; \
	    ${TAR} -xmf trial.${TAR}; \
	    rm -f trial.${TAR}; \
	fi

# make a new trial set
#
mktrial: krypt
	find ./trial -type f ! -name '*txt*' -print | xargs rm -f
	for i in ${TEST_TXT}; do \
	    ./krypt -b -K $$i $$i > `echo $$i | ${SED} 's/txt./cypb./'`; \
	    ./krypt -l -K $$i $$i > `echo $$i | ${SED} 's/txt./cypl./'`; \
	done
	./krypt -b -K trial/alltxt trial/txt.001 > trial/all.cypb.001
	./krypt -l -K trial/alltxt trial/txt.001 > trial/all.cypl.001
	./krypt -b -K trial/alltxt trial/txt.256 > trial/all.cypb.256
	./krypt -l -K trial/alltxt trial/txt.256 > trial/all.cypl.256
	rm -f trial.${TAR}.z
	${TAR} -cf - ./trial | gzip -v -best > trial.${TAR}.z
	rm -f trial.${TAR}.z.uu
	${UUENCODE} trial.${TAR}.z < trial.${TAR}.z > trial.${TAR}.z.uu
	rm -f trial.${TAR}.z
	rm -f trial.${TAR}.z.UU
	${SED} -e 's/%/{/g' trial.${TAR}.z.uu > trial.${TAR}.z.UU
	rm -f trial.${TAR}.z.uu

install: all
	${INSTALL} -c -m 0555 krypt ${DESTDIR}

clean:
	rm -f cypher.o must_align.o endian.o krypt.o
	rm -f core core.must_align core.krypt must_align.core krypt.core
	rm -f endian must_align endian.h align.h
	rm -rf trial
	rm -f trial.${TAR}.z trial.${TAR} trial.${TAR}.z.uu

clobber: clean
	rm -f krypt
