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