]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - games/caesar/caesar.c
MFC r306478:
[FreeBSD/stable/8.git] / games / caesar / caesar.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Adams.
7  *
8  * Authors:
9  *      Stan King, John Eldridge, based on algorithm suggested by
10  *      Bob Morris
11  * 29-Sep-82
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
28  *
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
39  * SUCH DAMAGE.
40  */
41
42 #if 0
43 #ifndef lint
44 static const char copyright[] =
45 "@(#) Copyright (c) 1989, 1993\n\
46         The Regents of the University of California.  All rights reserved.\n";
47 #endif /* not lint */
48
49 #ifndef lint
50 static const char sccsid[] = "@(#)caesar.c    8.1 (Berkeley) 5/31/93";
51 #endif /* not lint */
52 #endif
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55
56 #include <errno.h>
57 #include <math.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <ctype.h>
62 #include <unistd.h>
63
64 #define LINELENGTH      2048
65 #define ROTATE(ch, perm) \
66      isascii(ch) ? ( \
67         isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \
68             islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch) : ch
69
70 /*
71  * letter frequencies (taken from some unix(tm) documentation)
72  * (unix is a trademark of Bell Laboratories)
73  */
74 double stdf[26] = {
75         7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
76         0.42, 3.81, 2.69, 5.92,  6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
77         2.62, 0.81, 1.88, 0.23,  2.07, 0.06,
78 };
79
80 void printit(char *);
81
82 int
83 main(int argc, char **argv)
84 {
85         int ch, dot, i, nread, winnerdot = 0;
86         char *inbuf;
87         int obs[26], try, winner;
88
89         if (argc > 1)
90                 printit(argv[1]);
91
92         if (!(inbuf = malloc((size_t)LINELENGTH))) {
93                 (void)fprintf(stderr, "caesar: out of memory.\n");
94                 exit(1);
95         }
96
97         /* adjust frequency table to weight low probs REAL low */
98         for (i = 0; i < 26; ++i)
99                 stdf[i] = log(stdf[i]) + log(26.0 / 100.0);
100
101         /* zero out observation table */
102         bzero(obs, 26 * sizeof(int));
103
104         if ((nread = read(STDIN_FILENO, inbuf, (size_t)LINELENGTH)) < 0) {
105                 (void)fprintf(stderr, "caesar: %s\n", strerror(errno));
106                 exit(1);
107         }
108         for (i = nread; i--;) {
109                 ch = (unsigned char) inbuf[i];
110                 if (isascii(ch)) {
111                         if (islower(ch))
112                                 ++obs[ch - 'a'];
113                         else if (isupper(ch))
114                                 ++obs[ch - 'A'];
115                 }
116         }
117
118         /*
119          * now "dot" the freqs with the observed letter freqs
120          * and keep track of best fit
121          */
122         for (try = winner = 0; try < 26; ++try) { /* += 13) { */
123                 dot = 0;
124                 for (i = 0; i < 26; i++)
125                         dot += obs[i] * stdf[(i + try) % 26];
126                 /* initialize winning score */
127                 if (try == 0)
128                         winnerdot = dot;
129                 if (dot > winnerdot) {
130                         /* got a new winner! */
131                         winner = try;
132                         winnerdot = dot;
133                 }
134         }
135
136         for (;;) {
137                 for (i = 0; i < nread; ++i) {
138                         ch = (unsigned char) inbuf[i];
139                         putchar(ROTATE(ch, winner));
140                 }
141                 if (nread < LINELENGTH)
142                         break;
143                 if ((nread = read(STDIN_FILENO, inbuf, (size_t)LINELENGTH)) < 0) {
144                         (void)fprintf(stderr, "caesar: %s\n", strerror(errno));
145                         exit(1);
146                 }
147         }
148         exit(0);
149 }
150
151 void
152 printit(char *arg)
153 {
154         int ch, rot;
155
156         if ((rot = atoi(arg)) < 0) {
157                 (void)fprintf(stderr, "caesar: bad rotation value.\n");
158                 exit(1);
159         }
160         while ((ch = getchar()) != EOF)
161                 putchar(ROTATE(ch, rot));
162         exit(0);
163 }