caesar.c revision 1.5 1 /* $NetBSD: caesar.c,v 1.5 1997/10/10 12:07:11 lukem 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 #include <sys/cdefs.h>
45 #ifndef lint
46 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: caesar.c,v 1.5 1997/10/10 12:07:11 lukem Exp $");
55 #endif
56 #endif /* not lint */
57
58 #include <ctype.h>
59 #include <errno.h>
60 #include <math.h>
61 #include <stdio.h>
62 #include <string.h>
63 #include <stdlib.h>
64 #include <unistd.h>
65
66 #define LINELENGTH 2048
67 #define ROTATE(ch, perm) \
68 isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
69 islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch
70
71 /*
72 * letter frequencies (taken from some unix(tm) documentation)
73 * (unix is a trademark of Bell Laboratories)
74 */
75 double stdf[26] = {
76 7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
77 0.42, 3.81, 2.69, 5.92, 6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
78 2.62, 0.81, 1.88, 0.23, 2.07, 0.06,
79 };
80
81
82 int main __P((int, char *[]));
83 void printit __P((char *));
84
85 int
86 main(argc, argv)
87 int argc;
88 char **argv;
89 {
90 int ch, dot, i, nread, winnerdot;
91 char *inbuf;
92 int obs[26], try, winner;
93
94 winnerdot = 0;
95 if (argc > 1)
96 printit(argv[1]);
97
98 if (!(inbuf = malloc(LINELENGTH))) {
99 (void)fprintf(stderr, "caesar: out of memory.\n");
100 exit(1);
101 }
102
103 /* adjust frequency table to weight low probs REAL low */
104 for (i = 0; i < 26; ++i)
105 stdf[i] = log(stdf[i]) + log(26.0 / 100.0);
106
107 /* zero out observation table */
108 memset(obs, 0, 26 * sizeof(int));
109
110 if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) {
111 (void)fprintf(stderr, "caesar: %s\n", strerror(errno));
112 exit(1);
113 }
114 for (i = nread; i--;) {
115 ch = inbuf[i];
116 if (islower(ch))
117 ++obs[ch - 'a'];
118 else if (isupper(ch))
119 ++obs[ch - 'A'];
120 }
121
122 /*
123 * now "dot" the freqs with the observed letter freqs
124 * and keep track of best fit
125 */
126 for (try = winner = 0; try < 26; ++try) { /* += 13) { */
127 dot = 0;
128 for (i = 0; i < 26; i++)
129 dot += obs[i] * stdf[(i + try) % 26];
130 /* initialize winning score */
131 if (try == 0)
132 winnerdot = dot;
133 if (dot > winnerdot) {
134 /* got a new winner! */
135 winner = try;
136 winnerdot = dot;
137 }
138 }
139
140 for (;;) {
141 for (i = 0; i < nread; ++i) {
142 ch = inbuf[i];
143 putchar(ROTATE(ch, winner));
144 }
145 if (nread < LINELENGTH)
146 break;
147 if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) {
148 (void)fprintf(stderr, "caesar: %s\n", strerror(errno));
149 exit(1);
150 }
151 }
152 exit(0);
153 }
154
155 void
156 printit(arg)
157 char *arg;
158 {
159 int ch, rot;
160
161 if ((rot = atoi(arg)) < 0) {
162 (void)fprintf(stderr, "caesar: bad rotation value.\n");
163 exit(1);
164 }
165 while ((ch = getchar()) != EOF)
166 putchar(ROTATE(ch, rot));
167 exit(0);
168 }
169