xref: /openbsd/games/caesar/caesar.c (revision ab9f6680)
1 /*	$OpenBSD: caesar.c,v 1.21 2019/05/15 15:59:24 schwarze 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. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #include <ctype.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <math.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 
49 #define	LINELENGTH	2048
50 #define	ROTATE(ch, perm) \
51 	isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
52 	    islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch
53 
54 /*
55  * letter frequencies
56  */
57 double stdf[26] = {
58 	8.1, 1.4, 2.7, 3.8, 13.0, 2.9, 2.0, 5.2, 6.3, 0.13,
59 	0.4, 3.4, 2.5, 7.1, 7.9, 1.9, 0.11, 6.8, 6.1, 10.5,
60 	2.4, 0.9, 1.5, 0.15, 1.9, 0.07
61 };
62 
63 __dead void printit(int);
64 
65 int
main(int argc,char * argv[])66 main(int argc, char *argv[])
67 {
68 	int ch, i, nread;
69 	extern char *__progname;
70 	const char *errstr;
71 	char *inbuf;
72 	int obs[26], try, winner;
73 	double dot, winnerdot;
74 
75 	if (pledge("stdio", NULL) == -1)
76 		err(1, "pledge");
77 
78 	/* check to see if we were called as rot13 */
79 	if (strcmp(__progname, "rot13") == 0)
80 		printit(13);
81 
82 	if (argc > 1) {
83 		i = strtonum(argv[1], -25, 25, &errstr);
84 		if (errstr)
85 			errx(1, "rotation is %s: %s", errstr, argv[1]);
86 		else
87 			printit(i);
88 	}
89 
90 	if (!(inbuf = malloc(LINELENGTH)))
91 		err(1, NULL);
92 
93 	/* adjust frequency table to weight low probs REAL low */
94 	for (i = 0; i < 26; ++i)
95 		stdf[i] = log(stdf[i]) + log(26.0 / 100.0);
96 
97 	/* zero out observation table */
98 	memset(obs, 0, 26 * sizeof(int));
99 
100 	nread = 0;
101 	while ((nread < LINELENGTH) && ((ch = getchar()) != EOF)) {
102 		inbuf[nread++] = ch;
103 		if (islower(ch))
104 			++obs[ch - 'a'];
105 		else if (isupper(ch))
106 			++obs[ch - 'A'];
107 	}
108 
109 	/*
110 	 * now "dot" the freqs with the observed letter freqs
111 	 * and keep track of best fit
112 	 */
113 	winnerdot = 0;
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 		if (dot > winnerdot) {
119 			/* got a new winner! */
120 			winner = try;
121 			winnerdot = dot;
122 		}
123 	}
124 
125 	/* dump the buffer before calling printit */
126 	for (i = 0; i < nread; ++i) {
127 		ch = inbuf[i];
128 		putchar(ROTATE(ch, winner));
129 	}
130 	printit(winner);
131 }
132 
133 void
printit(int rot)134 printit(int rot)
135 {
136 	int ch;
137 
138 	if (rot < 0)
139 		rot = rot + 26;
140 	while ((ch = getchar()) != EOF)
141 		putchar(ROTATE(ch, rot));
142 	exit(0);
143 }
144