1 /* 2 * Copyright (c) 1989 The Regents of the University of California. 3 * 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 * %sccs.include.redist.c% 14 */ 15 16 #ifndef lint 17 char copyright[] = 18 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ 19 All rights reserved.\n"; 20 #endif /* not lint */ 21 22 #ifndef lint 23 static char sccsid[] = "@(#)caesar.c 5.4 (Berkeley) 06/01/90"; 24 #endif /* not lint */ 25 26 #include <math.h> 27 #include <stdio.h> 28 #include <ctype.h> 29 #include <unistd.h> 30 31 #define LINELENGTH 2048 32 #define ROTATE(ch, perm) \ 33 isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \ 34 islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch 35 36 /* 37 * letter frequencies (taken from some unix(tm) documentation) 38 * (unix is a trademark of Bell Laboratories) 39 */ 40 double stdf[26] = { 41 7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04, 42 0.42, 3.81, 2.69, 5.92, 6.96, 2.91, 0.08, 6.63, 8.77, 9.68, 43 2.62, 0.81, 1.88, 0.23, 2.07, 0.06, 44 }; 45 46 main(argc, argv) 47 int argc; 48 char **argv; 49 { 50 extern int errno; 51 register int ch, dot, i, nread, winnerdot; 52 register char *inbuf; 53 int obs[26], try, winner; 54 char *malloc(), *strerror(); 55 56 if (argc > 1) 57 printit(argv[1]); 58 59 if (!(inbuf = malloc(LINELENGTH))) { 60 (void)fprintf(stderr, "caesar: out of memory.\n"); 61 exit(1); 62 } 63 64 /* adjust frequency table to weight low probs REAL low */ 65 for (i = 0; i < 26; ++i) 66 stdf[i] = log(stdf[i]) + log(26.0 / 100.0); 67 68 /* zero out observation table */ 69 bzero(obs, 26 * sizeof(int)); 70 71 if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) { 72 (void)fprintf(stderr, "caesar: %s\n", strerror(errno)); 73 exit(1); 74 } 75 for (i = nread; i--;) { 76 ch = inbuf[i]; 77 if (islower(ch)) 78 ++obs[ch - 'a']; 79 else if (isupper(ch)) 80 ++obs[ch - 'A']; 81 } 82 83 /* 84 * now "dot" the freqs with the observed letter freqs 85 * and keep track of best fit 86 */ 87 for (try = winner = 0; try < 26; ++try) { /* += 13) { */ 88 dot = 0; 89 for (i = 0; i < 26; i++) 90 dot += obs[i] * stdf[(i + try) % 26]; 91 /* initialize winning score */ 92 if (try == 0) 93 winnerdot = dot; 94 if (dot > winnerdot) { 95 /* got a new winner! */ 96 winner = try; 97 winnerdot = dot; 98 } 99 } 100 101 for (;;) { 102 for (i = 0; i < nread; ++i) { 103 ch = inbuf[i]; 104 putchar(ROTATE(ch, winner)); 105 } 106 if (nread < LINELENGTH) 107 break; 108 if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) { 109 (void)fprintf(stderr, "caesar: %s\n", strerror(errno)); 110 exit(1); 111 } 112 } 113 exit(0); 114 } 115 116 printit(arg) 117 char *arg; 118 { 119 register int ch, rot; 120 121 if ((rot = atoi(arg)) < 0) { 122 (void)fprintf(stderr, "caesar: bad rotation value.\n"); 123 exit(1); 124 } 125 while ((ch = getchar()) != EOF) 126 putchar(ROTATE(ch, rot)); 127 exit(0); 128 } 129