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. 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.
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
37 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
38 * @(#)caesar.c 8.1 (Berkeley) 5/31/93
39 * $FreeBSD: head/usr.bin/caesar/caesar.c 241846 2012-10-22 03:06:53Z eadler $
52 #define LINELENGTH 2048
53 #define ROTATE(ch, perm) \
55 isupper(ch) ? ('A' + (ch - 'A' + perm) % LETTERS) : \
56 islower(ch) ? ('a' + (ch - 'a' + perm) % LETTERS) : ch) : ch
59 * letter frequencies (taken from some unix(tm) documentation)
60 * (unix is a trademark of Bell Laboratories)
62 static double stdf
[LETTERS
] = {
63 7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
64 0.42, 3.81, 2.69, 5.92, 6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
65 2.62, 0.81, 1.88, 0.23, 2.07, 0.06
69 printit(const char *arg
)
73 if ((rot
= atoi(arg
)) < 0)
74 errx(1, "bad rotation value");
76 while ((ch
= getchar()) != EOF
)
77 putchar(ROTATE(ch
, rot
));
82 main(int argc
, char *argv
[])
84 int ch
, dot
, i
, nread
, winnerdot
;
86 int obs
[LETTERS
], try, winner
;
88 /* revoke setgid privileges */
94 if ((inbuf
= malloc(LINELENGTH
)) == NULL
)
95 err(1, "malloc failed");
97 /* adjust frequency table to weight low probs REAL low */
98 for (i
= 0; i
< LETTERS
; i
++)
99 stdf
[i
] = log(stdf
[i
]) + log(LETTERS
/ 100.0);
101 /* zero out observation table */
102 bzero(obs
, LETTERS
* sizeof(int));
104 if ((nread
= read(STDIN_FILENO
, inbuf
, LINELENGTH
)) < 0)
105 err(1, "read failed");
106 for (i
= nread
; i
--;) {
107 ch
= (unsigned char) inbuf
[i
];
111 else if (isupper(ch
))
117 * now "dot" the freqs with the observed letter freqs
118 * and keep track of best fit
122 for (try = 0; try < LETTERS
; try++) {
124 for (i
= 0; i
< LETTERS
; i
++)
125 dot
+= obs
[i
] * stdf
[(i
+ try) % LETTERS
];
126 /* initialize winning score */
129 if (dot
> winnerdot
) {
130 /* got a new winner! */
137 for (i
= 0; i
< nread
; ++i
) {
138 ch
= (unsigned char) inbuf
[i
];
139 putchar(ROTATE(ch
, winner
));
141 if (nread
< LINELENGTH
)
143 if ((nread
= read(STDIN_FILENO
, inbuf
, LINELENGTH
)) < 0)
144 err(1, "read failed");