|
|
1.1 ! root 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: * Redistribution and use in source and binary forms are permitted provided ! 14: * that: (1) source distributions retain this entire copyright notice and ! 15: * comment, and (2) distributions including binaries display the following ! 16: * acknowledgement: ``This product includes software developed by the ! 17: * University of California, Berkeley and its contributors'' in the ! 18: * documentation or other materials provided with the distribution and in ! 19: * all advertising materials mentioning features or use of this software. ! 20: * Neither the name of the University nor the names of its contributors may ! 21: * be used to endorse or promote products derived from this software without ! 22: * specific prior written permission. ! 23: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED ! 24: * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF ! 25: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 26: */ ! 27: ! 28: #ifndef lint ! 29: char copyright[] = ! 30: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ ! 31: All rights reserved.\n"; ! 32: #endif /* not lint */ ! 33: ! 34: #ifndef lint ! 35: static char sccsid[] = "@(#)caesar.c 5.4 (Berkeley) 6/1/90"; ! 36: #endif /* not lint */ ! 37: ! 38: #include <math.h> ! 39: #include <stdio.h> ! 40: #include <ctype.h> ! 41: #include <unistd.h> ! 42: ! 43: #define LINELENGTH 2048 ! 44: #define ROTATE(ch, perm) \ ! 45: isupper(ch) ? ('A' + (ch - 'A' + perm) % 26) : \ ! 46: islower(ch) ? ('a' + (ch - 'a' + perm) % 26) : ch ! 47: ! 48: /* ! 49: * letter frequencies (taken from some unix(tm) documentation) ! 50: * (unix is a trademark of Bell Laboratories) ! 51: */ ! 52: double stdf[26] = { ! 53: 7.97, 1.35, 3.61, 4.78, 12.37, 2.01, 1.46, 4.49, 6.39, 0.04, ! 54: 0.42, 3.81, 2.69, 5.92, 6.96, 2.91, 0.08, 6.63, 8.77, 9.68, ! 55: 2.62, 0.81, 1.88, 0.23, 2.07, 0.06, ! 56: }; ! 57: ! 58: main(argc, argv) ! 59: int argc; ! 60: char **argv; ! 61: { ! 62: extern int errno; ! 63: register int ch, dot, i, nread, winnerdot; ! 64: register char *inbuf; ! 65: int obs[26], try, winner; ! 66: char *malloc(), *strerror(); ! 67: ! 68: if (argc > 1) ! 69: printit(argv[1]); ! 70: ! 71: if (!(inbuf = malloc(LINELENGTH))) { ! 72: (void)fprintf(stderr, "caesar: out of memory.\n"); ! 73: exit(1); ! 74: } ! 75: ! 76: /* adjust frequency table to weight low probs REAL low */ ! 77: for (i = 0; i < 26; ++i) ! 78: stdf[i] = log(stdf[i]) + log(26.0 / 100.0); ! 79: ! 80: /* zero out observation table */ ! 81: bzero(obs, 26 * sizeof(int)); ! 82: ! 83: if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) { ! 84: (void)fprintf(stderr, "caesar: %s\n", strerror(errno)); ! 85: exit(1); ! 86: } ! 87: for (i = nread; i--;) { ! 88: ch = inbuf[i]; ! 89: if (islower(ch)) ! 90: ++obs[ch - 'a']; ! 91: else if (isupper(ch)) ! 92: ++obs[ch - 'A']; ! 93: } ! 94: ! 95: /* ! 96: * now "dot" the freqs with the observed letter freqs ! 97: * and keep track of best fit ! 98: */ ! 99: for (try = winner = 0; try < 26; ++try) { /* += 13) { */ ! 100: dot = 0; ! 101: for (i = 0; i < 26; i++) ! 102: dot += obs[i] * stdf[(i + try) % 26]; ! 103: /* initialize winning score */ ! 104: if (try == 0) ! 105: winnerdot = dot; ! 106: if (dot > winnerdot) { ! 107: /* got a new winner! */ ! 108: winner = try; ! 109: winnerdot = dot; ! 110: } ! 111: } ! 112: ! 113: for (;;) { ! 114: for (i = 0; i < nread; ++i) { ! 115: ch = inbuf[i]; ! 116: putchar(ROTATE(ch, winner)); ! 117: } ! 118: if (nread < LINELENGTH) ! 119: break; ! 120: if ((nread = read(STDIN_FILENO, inbuf, LINELENGTH)) < 0) { ! 121: (void)fprintf(stderr, "caesar: %s\n", strerror(errno)); ! 122: exit(1); ! 123: } ! 124: } ! 125: exit(0); ! 126: } ! 127: ! 128: printit(arg) ! 129: char *arg; ! 130: { ! 131: register int ch, rot; ! 132: ! 133: if ((rot = atoi(arg)) < 0) { ! 134: (void)fprintf(stderr, "caesar: bad rotation value.\n"); ! 135: exit(1); ! 136: } ! 137: while ((ch = getchar()) != EOF) ! 138: putchar(ROTATE(ch, rot)); ! 139: exit(0); ! 140: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.