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