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

  1. /*
  2.  * Abstraction for the table of cipher text blocks and
  3.  * their decoded permutations so far.
  4.  *
  5.  * Robert W. Baldwin, December 1984.
  6.  */
  7.  
  8.  
  9. #include    <stdio.h>
  10. #include    "window.h"
  11. #include    "specs.h"
  12.  
  13.  
  14. #define    NPERLINE    10        /* How many values per line in save file. */
  15. #define    FROMSTART    0        /* For fseek call, how offset measured. */
  16.  
  17.  
  18. /* Input file name for permutations. */
  19. char    *permfile;
  20.  
  21.  
  22. /* Global state. */
  23. int        permchgflg = FALSE;    /* True if perms changed since last save. */
  24. int        *permtab[NPERMS];    /* Table of saved permutations or null. */
  25. int        perminit = FALSE;    /* Initialization flag. */
  26.  
  27.  
  28. /* Allocate and clear a permutation.
  29.  */
  30. int    *permalloc()
  31. {
  32.     int        i;
  33.     int        *perm;
  34.  
  35.     perm = ((int *) malloc((BLOCKSIZE+1)*sizeof(int)));
  36.     if (perm == NULL)  {
  37.         printf("\nNo room to allocate permutation.\n");
  38.         exit(0);
  39.         }
  40.  
  41.     for (i = 0 ; i < BLOCKSIZE ; i++)  {
  42.         perm[i] = -1;
  43.         }
  44.  
  45.     return(perm);
  46. }
  47.  
  48.  
  49. /* Return a pointer (for read or write use) to the permutation
  50.  * for the given block number.
  51.  * Return NULL if the block number is bad.
  52.  */
  53. int    *refperm(blocknum)
  54. int    blocknum;
  55. {
  56.     int        i;
  57.  
  58.     if ((blocknum < 0) || (NPERMS <= blocknum))  return(NULL);
  59.  
  60.     if (!perminit) {
  61.         perminit = TRUE;
  62.         for (i = 0 ; i < NPERMS ; i++)  permtab[i] = NULL;
  63.         }
  64.  
  65.     if (permtab[blocknum] == NULL)  {
  66.         permtab[blocknum] = permalloc();
  67.         }
  68.  
  69.     return(permtab[blocknum]);
  70. }
  71.  
  72.  
  73. /* Save all the permutations in a file.
  74.  * This can be invoked as a command.
  75.  * For now, the are no arguments, the filename is fixed.
  76.  * Return NULL if successful, else error mesage.
  77.  * First the Zee matrix is dumped, then the permutations.
  78.  * Each block is separated by a newline character.
  79.  * Individual numbers are separated by blanks.
  80.  */
  81. char    *permsave(str)
  82. char    *str;
  83. {
  84.     FILE    *fd;
  85.     int        i;
  86.  
  87.     if ((fd = fopen(permfile, "w")) == NULL)  {
  88.         sprintf(statmsg, "Could not open %s to write permutations.", permfile);
  89.         return(statmsg);
  90.         }
  91.  
  92.     storezee(fd);
  93.     
  94.     for (i = 0 ; i < NPERMS ; i++) {
  95.         writeperm(fd, refperm(i));
  96.         }
  97.  
  98.     fclose(fd);
  99.     permchgflg = FALSE;
  100.     return(NULL);
  101. }
  102.  
  103.  
  104. /* Restore all the permutations by reading them from a file.
  105.  * This can be invoked as a command.
  106.  * For now, the are no arguments, the filename is fixed.
  107.  * Return NULL if successful, else ptr to error message.
  108.  * Also call dblock to update its display.
  109.  */
  110. char    *permload(str)
  111. char    *str;
  112. {
  113.     FILE    *fd;
  114.     int        i;
  115.  
  116.     if ((fd = fopen(permfile, "r")) == NULL)  {
  117.         sprintf(statmsg, "Could not open %s to read permutations.", permfile);
  118.         return(statmsg);
  119.         }
  120.  
  121.     loadzee(fd);
  122.  
  123.     for (i = 0 ; i < NPERMS ; i++) {
  124.         readperm(fd, refperm(i));
  125.         }
  126.  
  127.     fclose(fd);
  128.     permchgflg = FALSE;
  129.  
  130.     dbssetblk(&dbstore, dbsgetblk(&dbstore));    /* Update perm and cbuf. */
  131.  
  132.     return(NULL);
  133.  
  134. }
  135.  
  136.  
  137.  
  138. /* Compute a permutation raised to some power.
  139.  */
  140. expperm(srcperm, dstperm, power)
  141. int    *srcperm, *dstperm;
  142. int    power;
  143. {
  144.     int        i, k, v;
  145.  
  146.     for (i = 0 ; i < BLOCKSIZE ; i++) {
  147.         v = i;
  148.         for (k = 0 ; k < power ; k++) {
  149.             v = srcperm[v];
  150.             if (v == -1)  break;
  151.             }
  152.         dstperm[i] = v;
  153.         }
  154. }
  155.  
  156.  
  157. /* Computer product of two permutations.
  158.  */
  159. multperm(left, right, result)
  160. int        *left;
  161. int        *right;
  162. int        *result;
  163. {
  164.     int        i, v;
  165.  
  166.     for (i = 0 ; i < BLOCKSIZE ; i++) {
  167.         v = right[i];
  168.         if (v != -1)  v = left[v];
  169.         result[i] = v;
  170.         }
  171. }
  172.  
  173.  
  174. /* Write a permutation onto the given stream.
  175.  */
  176. writeperm(fd, perm)
  177. FILE    *fd;
  178. int        perm[];
  179. {
  180.     int        j;
  181.  
  182.     for (j = 0 ; j < BLOCKSIZE ; j++)  {
  183.         fprintf(fd, "%3d ", perm[j]);
  184.         if ((j+1)%NPERLINE == 0)  fprintf(fd,"\n");
  185.         }
  186.  
  187.     fprintf(fd,"\n");
  188. }
  189.  
  190.  
  191.  
  192. /* Copy a permutation to another buffer.
  193.  */
  194. copyperm(src, dst)
  195. int        src[];
  196. int        dst[];
  197. {
  198.     int        j;
  199.  
  200.     for (j = 0 ; j < BLOCKSIZE ; j++)  {
  201.         dst[j] = src[j];
  202.         }
  203. }
  204.  
  205.  
  206. /* Read a permutation from the given stream into the given buffer.
  207.  */
  208. readperm(fd, perm)
  209. FILE    *fd;
  210. int        perm[];
  211. {
  212.     int        j;
  213.  
  214.     for (j = 0 ; j < BLOCKSIZE ; j++)  {
  215.         fscanf(fd, "%3d", &perm[j]);
  216.         }
  217.  
  218.     fscanf(fd,"\n");
  219. }
  220.  
  221.  
  222. /* Return a count of the number of values in the permutation that
  223.  * are not equal to -1.
  224.  * Max value is 256.
  225.  */
  226. int    permcount(perm)
  227. int        perm[];
  228. {
  229.     int        i;
  230.     int        count;
  231.  
  232.     count = 0;
  233.     for (i = 0 ; i < BLOCKSIZE ; i++)  {
  234.         if (perm[i] != -1)  count++;
  235.         }
  236.     return(count);
  237. }
  238.  
  239.  
  240. /* Return a count of the number of wires in a symetric permutation.
  241.  * Return -1 if the permutation is not its own inverse or 
  242.  * if it has fixed points.
  243.  * Max value is 128.
  244.  */
  245. int    permwcount(perm)
  246. int        perm[];
  247. {
  248.     int        i,v;
  249.     int        count;
  250.  
  251.     count = 0;
  252.     for (i = 0 ; i < BLOCKSIZE ; i++)  {
  253.         v = perm[i];
  254.         if (v == -1) continue;
  255.         if (perm[v] != i)  return(-1);        /* Not self inverse. */
  256.         if (v == i)        return(-1);        /* Has fixed point. */
  257.         if (i < v)   continue;                /* Count first instance. */
  258.         count++;
  259.         }
  260.     return(count);
  261. }
  262.