caesar.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Rick Adams.
7 1.1 cgd *
8 1.1 cgd * Authors:
9 1.1 cgd * Stan King, John Eldridge, based on algorithm suggested by
10 1.1 cgd * Bob Morris
11 1.1 cgd * 29-Sep-82
12 1.1 cgd *
13 1.1 cgd * Redistribution and use in source and binary forms, with or without
14 1.1 cgd * modification, are permitted provided that the following conditions
15 1.1 cgd * are met:
16 1.1 cgd * 1. Redistributions of source code must retain the above copyright
17 1.1 cgd * notice, this list of conditions and the following disclaimer.
18 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 cgd * notice, this list of conditions and the following disclaimer in the
20 1.1 cgd * documentation and/or other materials provided with the distribution.
21 1.1 cgd * 3. All advertising materials mentioning features or use of this software
22 1.1 cgd * must display the following acknowledgement:
23 1.1 cgd * This product includes software developed by the University of
24 1.1 cgd * California, Berkeley and its contributors.
25 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
26 1.1 cgd * may be used to endorse or promote products derived from this software
27 1.1 cgd * without specific prior written permission.
28 1.1 cgd *
29 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 1.1 cgd * SUCH DAMAGE.
40 1.1 cgd */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.1 cgd char copyright[] =
44 1.1 cgd "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
45 1.1 cgd All rights reserved.\n";
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd #ifndef lint
49 1.1 cgd static char sccsid[] = "@(#)caesar.c 5.4 (Berkeley) 6/1/90";
50 1.1 cgd #endif /* not lint */
51 1.1 cgd
52 1.1 cgd #include <math.h>
53 1.1 cgd #include <stdio.h>
54 1.1 cgd #include <ctype.h>
55 1.1 cgd #include <unistd.h>
56 1.1 cgd
57 1.1 cgd #define LINELENGTH 2048
58 1.1 cgd #define ROTATE(ch, perm) \
59 1.1 cgd isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
60 1.1 cgd islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch
61 1.1 cgd
62 1.1 cgd /*
63 1.1 cgd * letter frequencies (taken from some unix(tm) documentation)
64 1.1 cgd * (unix is a trademark of Bell Laboratories)
65 1.1 cgd */
66 1.1 cgd double stdf[26] = {
67 1.1 cgd 7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
68 1.1 cgd 0.42, 3.81, 2.69, 5.92, 6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
69 1.1 cgd 2.62, 0.81, 1.88, 0.23, 2.07, 0.06,
70 1.1 cgd };
71 1.1 cgd
72 1.1 cgd main(argc, argv)
73 1.1 cgd int argc;
74 1.1 cgd char **argv;
75 1.1 cgd {
76 1.1 cgd extern int errno;
77 1.1 cgd register int ch, dot, i, nread, winnerdot;
78 1.1 cgd register char *inbuf;
79 1.1 cgd int obs[26], try, winner;
80 1.1 cgd char *malloc(), *strerror();
81 1.1 cgd
82 1.1 cgd if (argc > 1)
83 1.1 cgd printit(argv[1]);
84 1.1 cgd
85 1.1 cgd if (!(inbuf = malloc(LINELENGTH))) {
86 1.1 cgd (void)fprintf(stderr, "caesar: out of memory.\n");
87 1.1 cgd exit(1);
88 1.1 cgd }
89 1.1 cgd
90 1.1 cgd /* adjust frequency table to weight low probs REAL low */
91 1.1 cgd for (i = 0; i < 26; ++i)
92 1.1 cgd stdf[i] = log(stdf[i]) + log(26.0 / 100.0);
93 1.1 cgd
94 1.1 cgd /* zero out observation table */
95 1.1 cgd bzero(obs, 26 * sizeof(int));
96 1.1 cgd
97 1.1 cgd if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) {
98 1.1 cgd (void)fprintf(stderr, "caesar: %s\n", strerror(errno));
99 1.1 cgd exit(1);
100 1.1 cgd }
101 1.1 cgd for (i = nread; i--;) {
102 1.1 cgd ch = inbuf[i];
103 1.1 cgd if (islower(ch))
104 1.1 cgd ++obs[ch - 'a'];
105 1.1 cgd else if (isupper(ch))
106 1.1 cgd ++obs[ch - 'A'];
107 1.1 cgd }
108 1.1 cgd
109 1.1 cgd /*
110 1.1 cgd * now "dot" the freqs with the observed letter freqs
111 1.1 cgd * and keep track of best fit
112 1.1 cgd */
113 1.1 cgd for (try = winner = 0; try < 26; ++try) { /* += 13) { */
114 1.1 cgd dot = 0;
115 1.1 cgd for (i = 0; i < 26; i++)
116 1.1 cgd dot += obs[i] * stdf[(i + try) % 26];
117 1.1 cgd /* initialize winning score */
118 1.1 cgd if (try == 0)
119 1.1 cgd winnerdot = dot;
120 1.1 cgd if (dot > winnerdot) {
121 1.1 cgd /* got a new winner! */
122 1.1 cgd winner = try;
123 1.1 cgd winnerdot = dot;
124 1.1 cgd }
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd for (;;) {
128 1.1 cgd for (i = 0; i < nread; ++i) {
129 1.1 cgd ch = inbuf[i];
130 1.1 cgd putchar(ROTATE(ch, winner));
131 1.1 cgd }
132 1.1 cgd if (nread < LINELENGTH)
133 1.1 cgd break;
134 1.1 cgd if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) {
135 1.1 cgd (void)fprintf(stderr, "caesar: %s\n", strerror(errno));
136 1.1 cgd exit(1);
137 1.1 cgd }
138 1.1 cgd }
139 1.1 cgd exit(0);
140 1.1 cgd }
141 1.1 cgd
142 1.1 cgd printit(arg)
143 1.1 cgd char *arg;
144 1.1 cgd {
145 1.1 cgd register int ch, rot;
146 1.1 cgd
147 1.1 cgd if ((rot = atoi(arg)) < 0) {
148 1.1 cgd (void)fprintf(stderr, "caesar: bad rotation value.\n");
149 1.1 cgd exit(1);
150 1.1 cgd }
151 1.1 cgd while ((ch = getchar()) != EOF)
152 1.1 cgd putchar(ROTATE(ch, rot));
153 1.1 cgd exit(0);
154 1.1 cgd }
155