* Copyright (c) 1989 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Stan King, John Eldridge, based on algorithm suggested by
* Redistribution and use in source and binary forms are permitted provided
* that: (1) source distributions retain this entire copyright notice and
* comment, and (2) distributions including binaries display the following
* acknowledgement: ``This product includes software developed by the
* University of California, Berkeley and its contributors'' in the
* documentation or other materials provided with the distribution and in
* all advertising materials mentioning features or use of this software.
* Neither the name of the University nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)caesar.c 5.4 (Berkeley) 6/1/90";
#define ROTATE(ch, perm) \
isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch
* letter frequencies (taken from some unix(tm) documentation)
* (unix is a trademark of Bell Laboratories)
7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
0.42, 3.81, 2.69, 5.92, 6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
2.62, 0.81, 1.88, 0.23, 2.07, 0.06,
register int ch
, dot
, i
, nread
, winnerdot
;
int obs
[26], try, winner
;
char *malloc(), *strerror();
if (!(inbuf
= malloc(LINELENGTH
))) {
(void)fprintf(stderr
, "caesar: out of memory.\n");
/* adjust frequency table to weight low probs REAL low */
stdf
[i
] = log(stdf
[i
]) + log(26.0 / 100.0);
/* zero out observation table */
bzero(obs
, 26 * sizeof(int));
if ((nread
= read(STDIN_FILENO
, inbuf
, LINELENGTH
)) < 0) {
(void)fprintf(stderr
, "caesar: %s\n", strerror(errno
));
* now "dot" the freqs with the observed letter freqs
* and keep track of best fit
for (try = winner
= 0; try < 26; ++try) { /* += 13) { */
dot
+= obs
[i
] * stdf
[(i
+ try) % 26];
/* initialize winning score */
for (i
= 0; i
< nread
; ++i
) {
putchar(ROTATE(ch
, winner
));
if ((nread
= read(STDIN_FILENO
, inbuf
, LINELENGTH
)) < 0) {
(void)fprintf(stderr
, "caesar: %s\n", strerror(errno
));
if ((rot
= atoi(arg
)) < 0) {
(void)fprintf(stderr
, "caesar: bad rotation value.\n");
while ((ch
= getchar()) != EOF
)
putchar(ROTATE(ch
, rot
));