home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part03 / tdriver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  5.7 KB  |  278 lines

  1. /*
  2.  * Test driver for automated trigram guessing.
  3.  */
  4.  
  5. #include    <stdio.h>
  6. #include    <math.h>
  7. #include    "window.h"
  8. #include    "specs.h"
  9. #include    "cipher.h"
  10. #include    "autotri.h"
  11.  
  12.  
  13. extern    char    *atr_best();
  14. extern            atr_autoguess();
  15. extern    float    atr_score();
  16. extern    int        accept_permvec();
  17.  
  18. atrinfo    myatrinfo;
  19. char    plainbuf[BLOCKSIZE+1];
  20. int        naccepted, nwrong, nright;        /* Number of wires accepted / wrong. */
  21.  
  22. extern    char    mcbuf[];
  23.  
  24. /* Test routine for automated trigram guessing. */
  25. main(argc, argv)
  26. int        argc;
  27. char    *argv[];
  28. {
  29.     FILE    *inp;
  30.     int        i, blknum;
  31.     int        nblocks;
  32.     int        *saveperm;
  33.     long    filelength;
  34.     atrinfo    *atri;
  35.     char    permfbuf[100];
  36.     char    cipherfbuf[100];
  37.     char    plainfbuf[100];
  38.     char    *perm = ".perm";
  39.     char    *plain = ".txt";
  40.     char    *code = ".cipher";
  41.     char    *p, *q;
  42.  
  43.     if (argc != 5)  {
  44.         printf("Usage: %s input_file_root", argv[0]);
  45.         printf(" min_score min_total_chars min_per_wire_chars\n");
  46.         exit(0);
  47.         }
  48.  
  49.     p = cipherfile = cipherfbuf;
  50.     q = argv[1];
  51.     while (*p++ = *q++);
  52.     --p;
  53.     q = code;
  54.     while (*p++ = *q++);
  55.  
  56.     p = plainfbuf;
  57.     q = argv[1];
  58.     while (*p++ = *q++);
  59.     --p;
  60.     q = plain;
  61.     while (*p++ = *q++);
  62.  
  63.     p = permfile = permfbuf;
  64.     q = argv[1];
  65.     while (*p++ = *q++);
  66.     --p;
  67.     q = perm;
  68.     while (*p++ = *q++);
  69.  
  70.     atri = &myatrinfo;
  71.  
  72.     if (sscanf(argv[2], "%f", &atri->max_score) != 1)  {
  73.         printf("Could not parse the max score from %s.\n", argv[2]);
  74.         exit(0);
  75.         }
  76.  
  77.     if (sscanf(argv[3], "%d", &atri->min_total_chars) != 1)  {
  78.         printf("Could not parse the min chars from %s.\n", argv[2]);
  79.         exit(0);
  80.         }
  81.  
  82.     if (sscanf(argv[4], "%d", &atri->min_wire_chars) != 1)  {
  83.         printf("Could not parse the min chars from %s.\n", argv[2]);
  84.         exit(0);
  85.         }
  86.  
  87.     permchgflg = FALSE;
  88.  
  89.     letterstats = "mss.stats";
  90.     trigramstats = "trigrams.stats";
  91.     load_1stats_from(letterstats);
  92.     load_tri_from(trigramstats);
  93.  
  94.     if ((inp = fopen(cipherfile, "r")) == NULL) {
  95.         printf("\nCannot open %s for reading.\n", cipherfile);
  96.         exit(0);
  97.         }
  98.     fseek(inp, 0L, 2);
  99.     filelength = ftell(inp);
  100.     fclose(inp);
  101.  
  102.     nblocks = filelength / BLOCKSIZE;
  103.     if (nblocks > NPERMS)  nblocks = NPERMS;
  104.  
  105.     printf("\t\tAutomated Trigram Guessing");
  106.     printf(" for %s\n\n",cipherfile);
  107.     printf("Max score = %4.2f", atri->max_score);
  108.     printf(".  Min total chars = %d", atri->min_total_chars);
  109.     printf(".  Min per wire chars = %d", atri->min_wire_chars);
  110.     printf("\n\n");
  111.     for (blknum = 0 ; blknum < nblocks ; blknum++) {
  112.         do_block(blknum, cipherfile, plainfbuf, atri);
  113.         saveperm = refperm(blknum);
  114.         for (i = 0 ; i < BLOCKSIZE ; i++)
  115.             saveperm[i] = atri->eci->perm[i];
  116.         }
  117.  
  118.     permsave();
  119. }
  120.  
  121.  
  122. do_block(blknum, cfile, pfile, atri)
  123. int        blknum;
  124. char    *cfile, *pfile;
  125. atrinfo    *atri;
  126. {
  127.     int        i,c,x,y;
  128.     int        j;
  129.     int        pos;
  130.     char    *trigram;
  131.     int        charcount;                /* Number of characters deduced. */
  132.     float    score;
  133.     int        *dbsperm;
  134.     perment    permvector[PERMSZ];
  135.     int        pvec[BLOCKSIZE+1];
  136.     char    str[BLOCKSIZE+1];
  137.  
  138.     cipherfile = pfile;
  139.     fillcbuf(blknum, plainbuf);
  140.     cipherfile = cfile;
  141.     fillcbuf(blknum, mcbuf);
  142.  
  143.     dbsperm = refperm(blknum);
  144.     atr_init(mcbuf, dbsperm, atri);
  145.  
  146.     ec_autoguess(atri->eci, 1.7);
  147. /*    decode(atri->eci->ciphertext, atri->eci->plaintext, atri->eci->perm);
  148.  
  149.     naccepted = 0;
  150.     nwrong = 0;
  151.     nright = 0;
  152.     charcount = 0;
  153.     for (i = 0 ; i < BLOCKSIZE ; i++)  {
  154.         if (atri->eci->plaintext[i] != NONE)  charcount++;
  155.         if (((y=atri->eci->perm[i]) != NONE) && (i < y))  naccepted++;
  156.         }
  157.  
  158.     printf("\n\nEquiv Guessing for block %d yields %d wires",blknum,naccepted);
  159.     printf(" and %d characters.\n\n", charcount);
  160.     ec_dplain(stdout, atri->eci);
  161. */
  162.  
  163.     naccepted = 0;
  164.     nwrong = 0;
  165.     nright = 0;
  166.  
  167.     printf("\n\nStarting block %d.\n", blknum);
  168. #if TRUE
  169.     for (pos = 0 ; pos < BLOCKSIZE ; pos++) {
  170.         trigram = atr_best(atri, pos);
  171.         if (trigram != NULL) {
  172.             accept_permvec(atri, atri->best_permvec);
  173. /*            printf("\n");
  174.             printf("Best trigram at %d is '%s'", pos, atri->best_trigram);
  175.             pvec2str(str, atri->best_pvec);
  176.             printf(" which deduced '%s'", str);
  177.             printf(" with a score of %f", atri->best_score);
  178.             printf(".\n");
  179.             printf("There were %d guesses", atri->gcount);
  180.             printf(" yeilding a total score of %f", atri->total_score);
  181.             printf(".\n");
  182. */
  183.             if (wrong_guess(plainbuf, pos, atri->best_trigram))  {
  184.                 nwrong++;
  185. /*                printf("WRONG\n");*/
  186.                 }
  187.             else {
  188.                 nright++;
  189. /*                printf("CORRECT\n");*/
  190.                 }
  191.             }
  192.         }
  193. #else
  194.     atr_autoguess(atri);
  195. #endif
  196.  
  197.     decode(atri->eci->ciphertext, atri->eci->plaintext, atri->eci->perm);
  198.  
  199.     charcount = 0;
  200.     for (i = 0 ; i < BLOCKSIZE ; i++)  {
  201.         if (atri->eci->plaintext[i] != NONE)  charcount++;
  202.         if (((y=atri->eci->perm[i]) != NONE) && (i < y))  naccepted++;
  203.         }
  204.  
  205.     printf("\n\nPlaintext for block %d using %d wires", blknum, naccepted);
  206.     printf(" yields %d characters.", charcount);
  207. #if TRUE
  208.     printf("\nThere were %d right guesses and %d wrong ones.",nright, nwrong);
  209. #endif
  210.     printf("\n\n");
  211.     ec_dplain(stdout, atri->eci);
  212. }
  213.  
  214.  
  215. /* Look for best trigram at given position.
  216.  */
  217. trytri(atri, pos, tindex)
  218. atrinfo    *atri;
  219. int        pos;
  220. int        tindex;
  221. {
  222.     int        i;
  223.     int        j, x, y;
  224.     char    *trigram;
  225.     char    str[BLOCKSIZE+1];
  226.  
  227. /*
  228.     trigram = atr_best(atri, pos, min_score);
  229.  
  230.     if (trigram != NULL) {
  231.         printf("Best trigram is %s", atri->best_trigram);
  232.         pvec2str(str, atri->best_pvec);
  233.         printf(" which deduced '%s'", str);
  234.         printf("' with a score of %f", atri->best_score);
  235.         printf(".\n");
  236.         printf("There were %d guesses", atri->gcount);
  237.         printf(" yeilding a total score of %d", atri->total_score);
  238.         printf(".\n");
  239.         }
  240. */
  241. }
  242.  
  243.  
  244. /* Return TRUE if the guess is wrong.
  245.  */
  246. int    wrong_guess(plaintext, position, trigram)
  247. char    *plaintext;
  248. int        position;
  249. char    *trigram;
  250. {
  251.     char    *guess, *reality;
  252.  
  253.     guess = trigram;
  254.     reality = &plaintext[position];
  255.  
  256.     while (*guess) {
  257.         if (*guess++ != *reality++)  return(TRUE);
  258.         }
  259.  
  260.     return(FALSE);
  261. }
  262.  
  263.  
  264.  
  265. key    u_getkey()
  266. {
  267. }
  268.  
  269. keyer    topktab[] ={{0, NULL}};
  270.  
  271.  
  272. char *quitcmd(arg)
  273. char    *arg;
  274. {
  275.     printf("\n");
  276.     exit(1);
  277. }
  278.