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