2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
9 * Stan King, John Eldridge, based on algorithm suggested by
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * $FreeBSD: src/games/caesar/caesar.c,v 1.8.2.1 2000/08/17 06:13:06 jhb Exp $
42 * $DragonFly: src/games/caesar/caesar.c,v 1.4 2005/03/01 23:47:10 joerg Exp $
44 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
45 * @(#)caesar.c 8.1 (Berkeley) 5/31/93
46 * $FreeBSD: src/games/caesar/caesar.c,v 1.8.2.1 2000/08/17 06:13:06 jhb Exp $
58 #define LINELENGTH 2048
59 #define ROTATE(ch, perm) \
61 isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
62 islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch) : ch
65 * letter frequencies (taken from some unix(tm) documentation)
66 * (unix is a trademark of Bell Laboratories)
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,
74 static void printit(const char *);
77 main(int argc
, char **argv
)
79 int ch
, dot
, i
, nread
, winnerdot
= 0;
81 int obs
[26], try, winner
;
83 /* revoke setgid privileges */
89 if ((inbuf
= malloc(LINELENGTH
)) == NULL
)
90 err(1, "malloc failed");
92 /* adjust frequency table to weight low probs REAL low */
93 for (i
= 0; i
< 26; ++i
)
94 stdf
[i
] = log(stdf
[i
]) + log(26.0 / 100.0);
96 /* zero out observation table */
97 bzero(obs
, 26 * sizeof(int));
99 if ((nread
= read(STDIN_FILENO
, inbuf
, LINELENGTH
)) < 0)
100 err(1, "read failed");
101 for (i
= nread
; i
--;) {
102 ch
= (unsigned char) inbuf
[i
];
106 else if (isupper(ch
))
112 * now "dot" the freqs with the observed letter freqs
113 * and keep track of best fit
115 for (try = winner
= 0; try < 26; ++try) { /* += 13) { */
117 for (i
= 0; i
< 26; i
++)
118 dot
+= obs
[i
] * stdf
[(i
+ try) % 26];
119 /* initialize winning score */
122 if (dot
> winnerdot
) {
123 /* got a new winner! */
130 for (i
= 0; i
< nread
; ++i
) {
131 ch
= (unsigned char) inbuf
[i
];
132 putchar(ROTATE(ch
, winner
));
134 if (nread
< LINELENGTH
)
136 if ((nread
= read(STDIN_FILENO
, inbuf
, LINELENGTH
)) < 0)
137 err(1, "read failed");
143 printit(const char *arg
)
147 if ((rot
= atoi(arg
)) < 0)
148 errx(1, "bad rotation value");
150 while ((ch
= getchar()) != EOF
)
151 putchar(ROTATE(ch
, rot
));