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

  1. /*
  2.  * Test driver for equivalence class 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. ecinfo    myecinfo;
  13.  
  14. char    plainbuf[BLOCKSIZE+1];
  15. float    accept_level;
  16.  
  17. extern    ec_best(), ec_dplain(), ec_dscipher();
  18. extern    ec_dnext(), ec_dsizetab(), ec_dperm(), ec_dpmap();
  19. extern    ec_init(), ec_best(), ec_cscore();
  20.  
  21. extern    char    mcbuf[];
  22. extern    char    *fname;            /* Used by fillcbuf. */
  23. /* Test routine for equiv class info. */
  24. main(argc, argv)
  25. int        argc;
  26. char    *argv[];
  27. {
  28.     ecinfo    *eci;
  29.     FILE    *inp;
  30.     int        i, blknum;
  31.     int        maxblock;
  32.     long    filelength;
  33.     char    infile[100];
  34.     char    inplain[100];
  35.     char    *plain = ".txt";
  36.     char    *code = ".cipher";
  37.     char    *p, *q;
  38.  
  39.     if (argc != 3)  {
  40.         printf("Usage: %s input_file_root acceptance_level\n", argv[0]);
  41.         exit(0);
  42.         }
  43.  
  44.     p = inplain;
  45.     q = argv[1];
  46.     while (*p++ = *q++);
  47.     --p;
  48.     q = plain;
  49.     while (*p++ = *q++);
  50.  
  51.     p = infile;
  52.     q = argv[1];
  53.     while (*p++ = *q++);
  54.     --p;
  55.     q = code;
  56.     while (*p++ = *q++);
  57.  
  58.     if (sscanf(argv[2], "%f", &accept_level) != 1)  {
  59.         printf("Could not parse the acceptance level from %s.\n", argv[2]);
  60.         exit(0);
  61.         }
  62.  
  63.     load_1stats_from("mss.stats");
  64.     eci = &myecinfo;
  65.  
  66.     if ((inp = fopen(infile, "r")) == NULL) {
  67.         printf("\nCannot open %s for reading.\n", infile);
  68.         exit(0);
  69.         }
  70.     fseek(inp, 0L, 2);
  71.     filelength = ftell(inp);
  72.     fclose(inp);
  73.  
  74.     maxblock = filelength / BLOCKSIZE;
  75.     if (maxblock > 19)  maxblock = 19;
  76.  
  77.     printf("\t\tEquivalence Class Guessing\n\n");
  78.     printf("Filename = %s.  Acceptance level = %4.2f\n",infile,accept_level);
  79.     for (blknum = 0 ; blknum <= maxblock ; blknum++) {
  80.         do_block(eci, blknum, infile, inplain);
  81.         }
  82. }
  83.  
  84.  
  85. do_block(eci, blknum, cipherfile, plainfile)
  86. ecinfo    *eci;
  87. int        blknum;
  88. char    *cipherfile, *plainfile;
  89. {
  90.     int        i,c,x,y;
  91.     int        naccepted, nwrong;
  92.     int        classpos;
  93.     int        charcount;
  94.  
  95.     fname = cipherfile;
  96.     fillcbuf(blknum, mcbuf);
  97.     fname = plainfile;
  98.     fillcbuf(blknum, plainbuf);
  99.  
  100.     ec_init(mcbuf, refperm(blknum), eci);
  101.  
  102.     naccepted = 0;
  103.     nwrong = 0;
  104.  
  105.     for (i = 0 ; i < eci->sizelast ; i++)  {
  106.         classpos = eci->sizelist[i].firstpos;
  107.         c = ec_best(eci, classpos, accept_level);
  108.         if (c != NONE) {
  109.             x = eci->scipher[classpos];
  110.             y = MODMASK & (c + classpos);
  111.             if (eci->perm[x] == NONE  &&  eci->perm[y] == NONE) {
  112.                 naccepted++;
  113.                 eci->perm[x] = y;
  114.                 eci->perm[y] = x;
  115. /*                printf("ACCEPTING best guess of %d wired to %d.\n",
  116.                         x, y);
  117. */                if ((MODMASK & plainbuf[classpos]) != c) {
  118.                     nwrong++;
  119. /*                    printf("*** WRONG ***  First char should be %c.\n",
  120.                             plainbuf[classpos]);
  121. */                    }
  122.                 }
  123.             else if (eci->perm[x] == y) {
  124. /*                printf("CONFIRMING guess of %d wired to %d.\n",
  125.                         x, y);
  126. */                }
  127.             else {
  128. /*                printf("CONFLICTING guess of %d wired to %d.\n",
  129.                         x, y);
  130. */                }
  131.             }
  132.         }
  133.  
  134.     decode(eci->ciphertext, eci->plaintext, eci->perm);
  135.  
  136.     charcount = 0;
  137.     for (i = 0 ; i < BLOCKSIZE ; i++)  {
  138.         if (eci->plaintext[i] != NONE)  charcount++;
  139.         }
  140.  
  141.     printf("\n\nPlaintext for block %d using %d wires", blknum, naccepted);
  142.     printf(" (%d wrong)", nwrong);
  143.     printf(" yields %d characters.\n\n", charcount);
  144.     ec_dplain(stdout, eci);
  145. }
  146.  
  147. key    u_getkey()
  148. {
  149. }
  150.  
  151. keyer    topktab[] ={{0, NULL}};
  152.