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

  1. /*
  2.  * Abstraction for the table of cipher text blocks.
  3.  *
  4.  * Robert W. Baldwin, December 1984.
  5.  *
  6.  * History:
  7.  * 3/8/85    Bob Baldwin        Changed fname to cipherfile.
  8.  * 1/13/85  Bob Baldwin        Permutation stuff moved to perm.c
  9.  */
  10.  
  11.  
  12. #include    <stdio.h>
  13. #include    "window.h"
  14. #include    "layout.h"
  15. #include    "specs.h"
  16.  
  17.  
  18.  
  19. #define    FROMSTART    0        /* For fseek call, how offset measured. */
  20.  
  21.  
  22. /* Input file name for ciphertext, set by main. */
  23. char    *cipherfile;
  24.  
  25.  
  26. /* Fill the given buffer with the i-th ciphertext block.
  27.  * The block index is zero-based.
  28.  * Return FALSE if try to read non-existant bytes.
  29.  */
  30. int    fillcbuf(blocknum, cbuf)
  31. int        blocknum;
  32. char    *cbuf;
  33. {
  34.     FILE    *fd;
  35.     long    offset;
  36.     long    res;
  37.     int        i;
  38.  
  39.     if ((blocknum < 0) || (NPERMS <= blocknum))  return(FALSE);
  40.  
  41.     if ((fd = fopen(cipherfile, "r")) == NULL)  {
  42.         printf("\nCould not open %s to read ciphertext.\n", cipherfile);
  43.         exit(0);
  44.         }
  45.  
  46.     offset = blocknum * BLOCKSIZE;
  47.     fseek(fd, offset, FROMSTART);
  48.     res = ftell(fd);
  49.     if (res != offset) {
  50.         printf("\nSeek failed on %s to %d, got %d.\n", cipherfile, offset,res);
  51.         exit(0);
  52.         }
  53.  
  54.     if (fread(cbuf,sizeof(*(cbuf)),BLOCKSIZE,fd) != BLOCKSIZE)  {
  55.         return(FALSE);
  56.         }
  57.  
  58.     fclose(fd);
  59.  
  60.     return(TRUE);
  61. }
  62.