xref: /freebsd/usr.bin/caesar/caesar.c (revision edf8578117e8844e02c0121147f45e4609b30680)
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 #if 0
39 #ifndef lint
40 static const char copyright[] =
41 "@(#) Copyright (c) 1989, 1993\n\
42 	The Regents of the University of California.  All rights reserved.\n";
43 #endif /* not lint */
44 
45 #ifndef lint
46 static const char sccsid[] = "@(#)caesar.c    8.1 (Berkeley) 5/31/93";
47 #endif /* not lint */
48 #endif
49 #include <sys/cdefs.h>
50 #include <errno.h>
51 #include <math.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <ctype.h>
56 #include <unistd.h>
57 
58 #define	LINELENGTH	2048
59 #define	ROTATE(ch, perm) \
60      isascii(ch) ? ( \
61 	isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
62 	    islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch) : ch
63 
64 /*
65  * letter frequencies (taken from some unix(tm) documentation)
66  * (unix is a trademark of Bell Laboratories)
67  */
68 static double stdf[26] = {
69 	7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
70 	0.42, 3.81, 2.69, 5.92,  6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
71 	2.62, 0.81, 1.88, 0.23,  2.07, 0.06,
72 };
73 
74 static void printit(char *);
75 
76 int
77 main(int argc, char **argv)
78 {
79 	int ch, dot, i, nread, winnerdot = 0;
80 	char *inbuf;
81 	int obs[26], try, winner;
82 
83 	if (argc > 1)
84 		printit(argv[1]);
85 
86 	if (!(inbuf = malloc((size_t)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, (size_t)LINELENGTH)) < 0) {
99 		(void)fprintf(stderr, "caesar: %s\n", strerror(errno));
100 		exit(1);
101 	}
102 	for (i = nread; i--;) {
103 		ch = (unsigned char) inbuf[i];
104 		if (isascii(ch)) {
105 			if (islower(ch))
106 				++obs[ch - 'a'];
107 			else if (isupper(ch))
108 				++obs[ch - 'A'];
109 		}
110 	}
111 
112 	/*
113 	 * now "dot" the freqs with the observed letter freqs
114 	 * and keep track of best fit
115 	 */
116 	for (try = winner = 0; try < 26; ++try) { /* += 13) { */
117 		dot = 0;
118 		for (i = 0; i < 26; i++)
119 			dot += obs[i] * stdf[(i + try) % 26];
120 		/* initialize winning score */
121 		if (try == 0)
122 			winnerdot = dot;
123 		if (dot > winnerdot) {
124 			/* got a new winner! */
125 			winner = try;
126 			winnerdot = dot;
127 		}
128 	}
129 
130 	for (;;) {
131 		for (i = 0; i < nread; ++i) {
132 			ch = (unsigned char) inbuf[i];
133 			putchar(ROTATE(ch, winner));
134 		}
135 		if (nread < LINELENGTH)
136 			break;
137 		if ((nread = read(STDIN_FILENO, inbuf, (size_t)LINELENGTH)) < 0) {
138 			(void)fprintf(stderr, "caesar: %s\n", strerror(errno));
139 			exit(1);
140 		}
141 	}
142 	exit(0);
143 }
144 
145 static void
146 printit(char *arg)
147 {
148 	int ch, rot;
149 
150 	if ((rot = atoi(arg)) < 0) {
151 		(void)fprintf(stderr, "caesar: bad rotation value.\n");
152 		exit(1);
153 	}
154 	while ((ch = getchar()) != EOF)
155 		putchar(ROTATE(ch, rot));
156 	exit(0);
157 }
158