caesar.c revision 1.18 1 /* $NetBSD: caesar.c,v 1.18 2005/10/08 18:18:18 rillig 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 * Roland Illig, 2005
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
44 The Regents of the University of California. All rights reserved.\n");
45 #endif /* not lint */
46
47 #ifndef lint
48 #if 0
49 static char sccsid[] = "@(#)caesar.c 8.1 (Berkeley) 5/31/93";
50 #else
51 __RCSID("$NetBSD: caesar.c,v 1.18 2005/10/08 18:18:18 rillig Exp $");
52 #endif
53 #endif /* not lint */
54
55 #include <ctype.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <limits.h>
59 #include <math.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <stdlib.h>
63
64 #define NCHARS (1 << CHAR_BIT)
65 #define LETTERS (26)
66
67 /*
68 * letter frequencies (taken from some unix(tm) documentation)
69 * (unix is a trademark of Bell Laboratories)
70 */
71 static const unsigned char upper[LETTERS] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
72 static const unsigned char lower[LETTERS] = "abcdefghijklmnopqrstuvwxyz";
73 static double stdf[LETTERS] = {
74 7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
75 0.42, 3.81, 2.69, 5.92, 6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
76 2.62, 0.81, 1.88, 0.23, 2.07, 0.06
77 };
78
79 static unsigned char rottbl[NCHARS];
80
81
82 static void
83 init_rottbl(int rot)
84 {
85 size_t i;
86
87 rot %= LETTERS; /* prevent integer overflow */
88
89 for (i = 0; i < NCHARS; i++)
90 rottbl[i] = (unsigned char)i;
91
92 for (i = 0; i < LETTERS; i++)
93 rottbl[upper[i]] = upper[(i + rot) % LETTERS];
94
95 for (i = 0; i < LETTERS; i++)
96 rottbl[lower[i]] = lower[(i + rot) % LETTERS];
97 }
98
99 static void
100 print_file(void)
101 {
102 int ch;
103
104 while ((ch = getchar()) != EOF) {
105 if (putchar(rottbl[ch]) == EOF) {
106 err(EXIT_FAILURE, "<stdout>");
107 /* NOTREACHED */
108 }
109 }
110 }
111
112 static void
113 print_array(const unsigned char *a, size_t len)
114 {
115 size_t i;
116
117 for (i = 0; i < len; i++) {
118 if (putchar(rottbl[a[i]]) == EOF) {
119 err(EXIT_FAILURE, "<stdout>");
120 /* NOTREACHED */
121 }
122 }
123 }
124
125 static int
126 get_rotation(const char *arg)
127 {
128 long rot;
129 char *endp;
130
131 errno = 0;
132 rot = strtol(arg, &endp, 10);
133 if (!(errno == 0 && *endp == '\0' && 0 <= rot && rot <= INT_MAX)) {
134 errx(EXIT_FAILURE, "bad rotation value: %s", arg);
135 /* NOTREACHED */
136 }
137 return (int) rot;
138 }
139
140 static void
141 guess_and_rotate(void)
142 {
143 unsigned char inbuf[2048];
144 unsigned int obs[NCHARS];
145 size_t i, nread;
146 double dot, winnerdot;
147 int try, winner;
148 int ch;
149
150 /* adjust frequency table to weight low probs REAL low */
151 for (i = 0; i < LETTERS; i++)
152 stdf[i] = log(stdf[i]) + log(LETTERS / 100.0);
153
154 /* zero out observation table */
155 (void)memset(obs, 0, sizeof(obs));
156
157 for (nread = 0; nread < sizeof(inbuf); nread++) {
158 if ((ch = getchar()) == EOF)
159 break;
160 inbuf[nread] = (unsigned char) ch;
161 }
162
163 for (i = 0; i < nread; i++)
164 obs[inbuf[i]]++;
165
166 /*
167 * now "dot" the freqs with the observed letter freqs
168 * and keep track of best fit
169 */
170 winner = 0;
171 winnerdot = 0.0;
172 for (try = 0; try < LETTERS; try++) {
173 dot = 0.0;
174 for (i = 0; i < LETTERS; i++)
175 dot += (obs[upper[i]] + obs[lower[i]])
176 * stdf[(i + try) % LETTERS];
177
178 if (try == 0 || dot > winnerdot) {
179 /* got a new winner! */
180 winner = try;
181 winnerdot = dot;
182 }
183 }
184
185 init_rottbl(winner);
186 print_array(inbuf, nread);
187 print_file();
188 }
189
190 int
191 main(int argc, char **argv)
192 {
193
194 if (argc == 1) {
195 guess_and_rotate();
196 } else if (argc == 2) {
197 init_rottbl(get_rotation(argv[1]));
198 print_file();
199 } else {
200 (void)fprintf(stderr, "usage: caesar [rotation]\n");
201 exit(EXIT_FAILURE);
202 /* NOTREACHED */
203 }
204
205 if (ferror(stdin)) {
206 errx(EXIT_FAILURE, "<stdin>");
207 /* NOTREACHED */
208 }
209
210 (void)fflush(stdout);
211 if (ferror(stdout)) {
212 err(EXIT_FAILURE, "<stdout>");
213 /* NOTREACHED */
214 }
215
216 return 0;
217 }
218