Merge branch 'vendor/BZIP'
[games.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. 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.
24  *
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
35  * SUCH DAMAGE.
36  *
37  * $FreeBSD: src/games/caesar/caesar.c,v 1.8.2.1 2000/08/17 06:13:06 jhb Exp $
38  * $DragonFly: src/games/caesar/caesar.c,v 1.4 2005/03/01 22:47:10 joerg Exp $
39  *
40  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
41  * @(#)caesar.c    8.1 (Berkeley) 5/31/93
42  * $FreeBSD: src/games/caesar/caesar.c,v 1.8.2.1 2000/08/17 06:13:06 jhb Exp $
43  */
44
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <math.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #define LETTERS         26
55 #define LINELENGTH      2048
56 #define ROTATE(ch, perm) \
57      isascii(ch) ? ( \
58         isupper(ch) ? ('A' + (ch - 'A' + perm) % LETTERS) : \
59             islower(ch) ? ('a' + (ch - 'a' + perm) % LETTERS) : ch) : ch
60
61 /*
62  * letter frequencies (taken from some unix(tm) documentation)
63  * (unix is a trademark of Bell Laboratories)
64  */
65 static double stdf[LETTERS] = {
66         7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04,
67         0.42, 3.81, 2.69, 5.92,  6.96, 2.91, 0.08, 6.63, 8.77, 9.68,
68         2.62, 0.81, 1.88, 0.23,  2.07, 0.06
69 };
70
71 static void
72 printit(const char *arg)
73 {
74         int ch, rot;
75
76         if ((rot = atoi(arg)) < 0)
77                 errx(1, "bad rotation value");
78
79         while ((ch = getchar()) != EOF)
80                 putchar(ROTATE(ch, rot));
81         exit(0);
82 }
83
84 int
85 main(int argc, char *argv[])
86 {
87         int ch, dot, i, nread, winnerdot;
88         char *inbuf;
89         int obs[LETTERS], try, winner;
90
91         /* revoke setgid privileges */
92         setgid(getgid());
93
94         if (argc > 1)
95                 printit(argv[1]);
96
97         if ((inbuf = malloc(LINELENGTH)) == NULL)
98                 err(1, "malloc failed");
99
100         /* adjust frequency table to weight low probs REAL low */
101         for (i = 0; i < LETTERS; i++)
102                 stdf[i] = log(stdf[i]) + log(LETTERS / 100.0);
103
104         /* zero out observation table */
105         bzero(obs, LETTERS * sizeof(int));
106
107         if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0)
108                 err(1, "read failed");
109         for (i = nread; i--;) {
110                 ch = (unsigned char) inbuf[i];
111                 if (isascii(ch)) {
112                         if (islower(ch))
113                                 ++obs[ch - 'a'];
114                         else if (isupper(ch))
115                                 ++obs[ch - 'A'];
116                 }
117         }
118
119         /*
120          * now "dot" the freqs with the observed letter freqs
121          * and keep track of best fit
122          */
123         winner = 0;
124         winnerdot = 0;
125         for (try = 0; try < LETTERS; try++) {
126                 dot = 0;
127                 for (i = 0; i < LETTERS; i++)
128                         dot += obs[i] * stdf[(i + try) % LETTERS];
129                 /* initialize winning score */
130                 if (try == 0)
131                         winnerdot = dot;
132                 if (dot > winnerdot) {
133                         /* got a new winner! */
134                         winner = try;
135                         winnerdot = dot;
136                 }
137         }
138
139         for (;;) {
140                 for (i = 0; i < nread; ++i) {
141                         ch = (unsigned char) inbuf[i];
142                         putchar(ROTATE(ch, winner));
143                 }
144                 if (nread < LINELENGTH)
145                         break;
146                 if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0)
147                         err(1, "read failed");
148         }
149         exit(0);
150 }