xref: /netbsd-src/games/caesar/caesar.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: caesar.c,v 1.4 1996/02/06 22:47:15 jtc Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Adams.
9  *
10  * Authors:
11  *	Stan King, John Eldridge, based on algorithm suggested by
12  *	Bob Morris
13  * 29-Sep-82
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *	This product includes software developed by the University of
26  *	California, Berkeley and its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  */
43 
44 #ifndef lint
45 static char copyright[] =
46 "@(#) Copyright (c) 1989, 1993\n\
47 	The Regents of the University of California.  All rights reserved.\n";
48 #endif /* not lint */
49 
50 #ifndef lint
51 #if 0
52 static char sccsid[] = "@(#)caesar.c	8.1 (Berkeley) 5/31/93";
53 #else
54 static char rcsid[] = "$NetBSD: caesar.c,v 1.4 1996/02/06 22:47:15 jtc Exp $";
55 #endif
56 #endif /* not lint */
57 
58 #include <math.h>
59 #include <stdio.h>
60 #include <ctype.h>
61 #include <unistd.h>
62 #include <errno.h>
63 
64 #define	LINELENGTH	2048
65 #define	ROTATE(ch, perm) \
66 	isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
67 	    islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch
68 
69 /*
70  * letter frequencies (taken from some unix(tm) documentation)
71  * (unix is a trademark of Bell Laboratories)
72  */
73 double stdf[26] = {
74 	7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
75 	0.42, 3.81, 2.69, 5.92,  6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
76 	2.62, 0.81, 1.88, 0.23,  2.07, 0.06,
77 };
78 
79 main(argc, argv)
80 	int argc;
81 	char **argv;
82 {
83 	register int ch, dot, i, nread, winnerdot;
84 	register char *inbuf;
85 	int obs[26], try, winner;
86 	char *malloc(), *strerror();
87 
88 	if (argc > 1)
89 		printit(argv[1]);
90 
91 	if (!(inbuf = malloc(LINELENGTH))) {
92 		(void)fprintf(stderr, "caesar: out of memory.\n");
93 		exit(1);
94 	}
95 
96 	/* adjust frequency table to weight low probs REAL low */
97 	for (i = 0; i < 26; ++i)
98 		stdf[i] = log(stdf[i]) + log(26.0 / 100.0);
99 
100 	/* zero out observation table */
101 	bzero(obs, 26 * sizeof(int));
102 
103 	if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) {
104 		(void)fprintf(stderr, "caesar: %s\n", strerror(errno));
105 		exit(1);
106 	}
107 	for (i = nread; i--;) {
108 		ch = inbuf[i];
109 		if (islower(ch))
110 			++obs[ch - 'a'];
111 		else if (isupper(ch))
112 			++obs[ch - 'A'];
113 	}
114 
115 	/*
116 	 * now "dot" the freqs with the observed letter freqs
117 	 * and keep track of best fit
118 	 */
119 	for (try = winner = 0; try < 26; ++try) { /* += 13) { */
120 		dot = 0;
121 		for (i = 0; i < 26; i++)
122 			dot += obs[i] * stdf[(i + try) % 26];
123 		/* initialize winning score */
124 		if (try == 0)
125 			winnerdot = dot;
126 		if (dot > winnerdot) {
127 			/* got a new winner! */
128 			winner = try;
129 			winnerdot = dot;
130 		}
131 	}
132 
133 	for (;;) {
134 		for (i = 0; i < nread; ++i) {
135 			ch = inbuf[i];
136 			putchar(ROTATE(ch, winner));
137 		}
138 		if (nread < LINELENGTH)
139 			break;
140 		if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) {
141 			(void)fprintf(stderr, "caesar: %s\n", strerror(errno));
142 			exit(1);
143 		}
144 	}
145 	exit(0);
146 }
147 
148 printit(arg)
149 	char *arg;
150 {
151 	register int ch, rot;
152 
153 	if ((rot = atoi(arg)) < 0) {
154 		(void)fprintf(stderr, "caesar: bad rotation value.\n");
155 		exit(1);
156 	}
157 	while ((ch = getchar()) != EOF)
158 		putchar(ROTATE(ch, rot));
159 	exit(0);
160 }
161