caesar.c 4.3 KB

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