home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part02 / bdriver.c next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  3.8 KB  |  198 lines

  1. /*
  2.  * Test driver for BIGRAM character guessing stuff.
  3.  */
  4.  
  5. #include    <stdio.h>
  6. #include    <math.h>
  7. #include    "window.h"
  8. #include    "specs.h"
  9. #include    "cipher.h"
  10.  
  11.  
  12. #define NDOBLOCKS    2        /* Number of blocks to do. */
  13. #define    DEBUG        FALSE
  14.  
  15. ecinfo    myecinfo;
  16.  
  17. char    plainbuf[BLOCKSIZE+1];
  18. float    accept_level;
  19. float    prob_cutoff;
  20.  
  21. extern    ec_best(), ec_dplain(), ec_dscipher();
  22. extern    ec_dnext(), ec_dsizetab(), ec_dperm(), ec_dpmap();
  23. extern    ec_init(), ec_best(), ec_cscore();
  24. extern    lp_init(), lp_best_char(), lp_cscore(), lp_accept(), lp_best_pos();
  25. extern    lp_dclasses();
  26.  
  27. extern    char    mcbuf[];
  28. extern    char    *fname;            /* Used by fillcbuf. */
  29. /* Test routine for equiv class info. */
  30. main(argc, argv)
  31. int        argc;
  32. char    *argv[];
  33. {
  34.     ecinfo    *eci;
  35.     FILE    *inp;
  36.     FILE    *sout, *sin;
  37.     int        i, blknum;
  38.     int        maxblock;
  39.     long    filelength;
  40.     char    infile[100];
  41.     char    inplain[100];
  42.     char    *plain = ".txt";
  43.     char    *code = ".cipher";
  44.     char    *p, *q;
  45.  
  46.     sout = stdout;        /* For use within debugger, dbx. */
  47.     sin = stdin;
  48.  
  49.     if (argc != 4)  {
  50.         printf("Usage: %s input_file_root acceptance_level prob_cutoff\n",
  51.                 argv[0]);
  52.         exit(0);
  53.         }
  54.  
  55.     p = inplain;
  56.     q = argv[1];
  57.     while (*p++ = *q++);
  58.     --p;
  59.     q = plain;
  60.     while (*p++ = *q++);
  61.  
  62.     p = infile;
  63.     q = argv[1];
  64.     while (*p++ = *q++);
  65.     --p;
  66.     q = code;
  67.     while (*p++ = *q++);
  68.  
  69.     if (sscanf(argv[2], "%f", &accept_level) != 1)  {
  70.         printf("Could not parse the acceptance level from %s.\n", argv[2]);
  71.         exit(0);
  72.         }
  73.  
  74.     if (sscanf(argv[3], "%f", &prob_cutoff) != 1)  {
  75.         printf("Could not parse the probability cutoff from %s.\n", argv[2]);
  76.         exit(0);
  77.         }
  78.  
  79.     printf("\t\tEquivalence Class Guessing\n\n");
  80.     printf("Filename = %s.  Acceptance level = %4.2f\n",infile,accept_level);
  81.  
  82.     printf("Loading statistics ...");
  83.     printf(" 1");
  84.     load_1stats_from("mss.stats");
  85.     printf(" 2");
  86.     load_2stats_from("mss-bigram.stats");
  87.     printf(" done.\n");
  88.  
  89.     eci = &myecinfo;
  90.  
  91.     if ((inp = fopen(infile, "r")) == NULL) {
  92.         printf("\nCannot open %s for reading.\n", infile);
  93.         exit(0);
  94.         }
  95.     fseek(inp, 0L, 2);
  96.     filelength = ftell(inp);
  97.     fclose(inp);
  98.  
  99.     maxblock = filelength / BLOCKSIZE;
  100.     if (maxblock > (NDOBLOCKS-1))  maxblock = (NDOBLOCKS-1);
  101.  
  102.     for (blknum = 0 ; blknum <= maxblock ; blknum++) {
  103.         do_lp_block(eci, blknum, infile, inplain);
  104.         }
  105. }
  106.  
  107.  
  108. /* Do a block using the letter pair statistics.
  109.  */
  110. do_lp_block(eci, blknum, cfile, plainfile)
  111. reg        ecinfo    *eci;
  112. int        blknum;
  113. char    *cfile, *plainfile;
  114. {
  115.     int        i;
  116. reg    int        c;
  117.     int        ntried;
  118.     int        naccepted, nwrong;
  119. reg    int        classpos;
  120.     int        charcount;
  121.     int        *permp, repeat;
  122.  
  123.     cipherfile = cfile;
  124.     fillcbuf(blknum, mcbuf);
  125.     cipherfile = plainfile;
  126.     fillcbuf(blknum, plainbuf);
  127.  
  128.     lp_init(mcbuf, refperm(blknum), eci);
  129.  
  130. for(repeat = 0 ; repeat < 3 ; repeat++)  {
  131.     naccepted = 0;
  132.     nwrong = 0;
  133.     ntried = 0;
  134.  
  135.     for (ntried = 0 ; ntried < BLOCKSIZE ; ntried++)  {
  136.         classpos = lp_best_pos(eci, 2);
  137.         if (classpos == NONE)
  138.             break;
  139.         c = lp_best_char(eci, classpos,
  140.                         accept_level - ((repeat == 0) ? 0.0 : 0.0),
  141.                         prob_cutoff);
  142.         if (c != NONE) {
  143.             lp_accept(eci, classpos, c);
  144.             naccepted++;
  145. #if DEBUG
  146.             printf("ACCEPTED");
  147. #endif
  148.             if (plainbuf[classpos] != c)  {
  149.                 nwrong++;
  150. #if DEBUG
  151.                 printf(" -- INCORRECT");
  152. #endif
  153.                 }
  154. #if DEBUG
  155.             printf("\n");
  156. #endif
  157.             }
  158.         }
  159.  
  160. /*    decode(eci->ciphertext, eci->plaintext, eci->perm);
  161. */
  162.  
  163.     for (i = 0 ; i < eci->nclasses ; i++)  {
  164.         eci->classlist[i].changed = TRUE;
  165.         }
  166.  
  167.  
  168.     charcount = 0;
  169.      for (i = 0 ; i < BLOCKSIZE ; i++)  {
  170.         if (eci->plaintext[i] != NONE)  charcount++;
  171.         }
  172.  
  173.     printf("\n\nPlaintext for block %d using %d wires", blknum, naccepted);
  174.     printf(" (%d wrong)", nwrong);
  175.     printf(" yields %d characters.", charcount);
  176.     printf("\nThere were %d classes and %d guess tries.",eci->nclasses,ntried);
  177.     printf("\n\n");
  178.     ec_dplain(stdout, eci);
  179.     }
  180.  
  181.     permp = refperm(blknum);
  182.     for (i = 0 ; i < BLOCKSIZE ; i++)  {
  183.         permp[i] = eci->perm[i];
  184.         }
  185. }
  186.  
  187.  
  188. key    u_getkey()
  189. {
  190. }
  191.  
  192. keyer    topktab[] ={{0, NULL}};
  193.  
  194.  
  195. char    *quitcmd()
  196. {
  197. }
  198.