xref: /freebsd/usr.bin/caesar/caesar.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <errno.h>
39 #include <math.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <unistd.h>
45 
46 #define	LINELENGTH	2048
47 #define	ROTATE(ch, perm) \
48      isascii(ch) ? ( \
49 	isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
50 	    islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch) : ch
51 
52 /*
53  * letter frequencies (taken from some unix(tm) documentation)
54  * (unix is a trademark of Bell Laboratories)
55  */
56 static double stdf[26] = {
57 	7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
58 	0.42, 3.81, 2.69, 5.92,  6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
59 	2.62, 0.81, 1.88, 0.23,  2.07, 0.06,
60 };
61 
62 static void printit(char *);
63 
64 int
65 main(int argc, char **argv)
66 {
67 	int ch, dot, i, nread, winnerdot = 0;
68 	char *inbuf;
69 	int obs[26], try, winner;
70 
71 	if (argc > 1)
72 		printit(argv[1]);
73 
74 	if (!(inbuf = malloc((size_t)LINELENGTH))) {
75 		(void)fprintf(stderr, "caesar: out of memory.\n");
76 		exit(1);
77 	}
78 
79 	/* adjust frequency table to weight low probs REAL low */
80 	for (i = 0; i < 26; ++i)
81 		stdf[i] = log(stdf[i]) + log(26.0 / 100.0);
82 
83 	/* zero out observation table */
84 	bzero(obs, 26 * sizeof(int));
85 
86 	if ((nread = read(STDIN_FILENO, inbuf, (size_t)LINELENGTH)) < 0) {
87 		(void)fprintf(stderr, "caesar: %s\n", strerror(errno));
88 		exit(1);
89 	}
90 	for (i = nread; i--;) {
91 		ch = (unsigned char) inbuf[i];
92 		if (isascii(ch)) {
93 			if (islower(ch))
94 				++obs[ch - 'a'];
95 			else if (isupper(ch))
96 				++obs[ch - 'A'];
97 		}
98 	}
99 
100 	/*
101 	 * now "dot" the freqs with the observed letter freqs
102 	 * and keep track of best fit
103 	 */
104 	for (try = winner = 0; try < 26; ++try) { /* += 13) { */
105 		dot = 0;
106 		for (i = 0; i < 26; i++)
107 			dot += obs[i] * stdf[(i + try) % 26];
108 		/* initialize winning score */
109 		if (try == 0)
110 			winnerdot = dot;
111 		if (dot > winnerdot) {
112 			/* got a new winner! */
113 			winner = try;
114 			winnerdot = dot;
115 		}
116 	}
117 
118 	for (;;) {
119 		for (i = 0; i < nread; ++i) {
120 			ch = (unsigned char) inbuf[i];
121 			putchar(ROTATE(ch, winner));
122 		}
123 		if (nread < LINELENGTH)
124 			break;
125 		if ((nread = read(STDIN_FILENO, inbuf, (size_t)LINELENGTH)) < 0) {
126 			(void)fprintf(stderr, "caesar: %s\n", strerror(errno));
127 			exit(1);
128 		}
129 	}
130 	exit(0);
131 }
132 
133 static void
134 printit(char *arg)
135 {
136 	int ch, rot;
137 
138 	if ((rot = atoi(arg)) < 0) {
139 		(void)fprintf(stderr, "caesar: bad rotation value.\n");
140 		exit(1);
141 	}
142 	while ((ch = getchar()) != EOF)
143 		putchar(ROTATE(ch, rot));
144 	exit(0);
145 }
146