home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 519b.lha / FZIFF / iff.c < prev    next >
C/C++ Source or Header  |  1991-06-09  |  3KB  |  153 lines

  1. /* iffar - IFF CAT archiver, IFF support functions
  2.  
  3.    By Karl Lehenbauer, 4/25/88.
  4.    This code is released to the public domain.
  5.    See the README file for more information.
  6.  
  7. */
  8.  
  9. /* culled general purpose IFF file cracking routines for Karl's 
  10.  * IFF Stuff by Karl Lehenbauer, based originally on public domain IFF 
  11.  * code from Electronic Arts, 2/24/88
  12.  */
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <stdio.h>
  17. #include <fcntl.h>
  18. #include <ctype.h>
  19. #include <hackercorp/assert.h>
  20.  
  21. #include <hackercorp/iff.h>
  22.  
  23. #include "prototypes.h"
  24.  
  25. extern long lseek();
  26.  
  27. extern ULONG nextchunk();
  28.  
  29. /* print a chunkID to stderr */
  30. void PutID(ID id)
  31. {
  32.     fprintf(stderr,"%c%c%c%c",
  33.        (char)((id>>24L) & 0x7f),
  34.        (char)((id>>16L) & 0x7f),
  35.        (char)((id>>8)   & 0x7f),
  36.        (char)(id        & 0x7f) );
  37. }
  38.  
  39. /* write a chunk header, that's the 4-byte chunk ID and a 4-byte 
  40.  * chunk length 
  41.  */
  42. int WriteChunkHeader(int fd,ULONG chunktype,long length)
  43. {
  44.     ChunkHeader chunkheader;
  45.  
  46.     chunkheader.ckID = chunktype;
  47.     chunkheader.ckSize = length;
  48.  
  49.     if (write(fd,&chunkheader,sizeof(chunkheader)) == -1)
  50.     {
  51.         perror("WriteChunkHeader");
  52.         return(0);
  53.     }
  54.     return(1);
  55. }
  56.  
  57. /* write a chunk that has a four character subtype, like FORM, CAT or LIST
  58. */
  59. int WriteSuperChunkHeader(int fd, ULONG chunktype, ULONG subtype, long length)
  60. {
  61.     if (!WriteChunkHeader(fd,chunktype,length+sizeof(subtype)))
  62.         return(0);
  63.     return(write(fd,&subtype,sizeof(subtype)) != -1);
  64. }
  65.  
  66. /* write a chunk header and body, that's the 8-byte header and the data
  67.  * to match the specified length
  68.  */
  69. int WriteChunk(int fd, ULONG chunktype, void *data, long length)
  70. {
  71.     static char zero = '\0';
  72.  
  73.     if (!WriteChunkHeader(fd,chunktype,length))
  74.         return(0);
  75.  
  76.     /* if there's a body, write it out */
  77.     if (length != 0)
  78.         if (write(fd,data,length) == -1)
  79.         {
  80.             perror("WriteChunk");
  81.             return(0);
  82.         }
  83.  
  84.     /* if the length is odd, write out an additional zero byte */
  85.     if (length & 1)
  86.         if (write(fd,&zero,1) == -1)
  87.         {
  88.             perror("WriteChunk");
  89.             return(0);
  90.         }
  91.     return(1);
  92. }
  93.  
  94. int WriteTextChunk(int fd,ULONG chunktype,char *text)
  95. {
  96.     int len = strlen(text);
  97.     return(WriteChunk(fd,chunktype,text,len));
  98. }
  99.  
  100. /* rewrite_IFF_header - write (filesize - sizeof(ChunkHeader)) into
  101.  * IFF file's header, assumes file is open for write
  102.  */
  103. int Rewrite_IFF_header(int fd)
  104. {
  105.     long filesize;
  106.  
  107.     /* get filesize by seeking to EOF */
  108.     if ((filesize = lseek(fd,0,2)) == -1)
  109.     {
  110.         perror("rewrite_IFF_header");
  111.         return(0);
  112.     }
  113.  
  114.     /* go back to the beginning of the file */
  115.     if (lseek(fd,0,0) == -1)
  116.     {
  117.         perror("rewrite_IFF_header");
  118.         return(0);
  119.     }
  120.  
  121.     if (!WriteChunkHeader(fd,ID_CAT,(filesize - sizeof(ChunkHeader))))
  122.     {
  123.         perror("rewrite_IFF_header: WriteChunkHeader");
  124.         return(0);
  125.     }
  126.  
  127.     return(1);
  128. }
  129.  
  130. /* create an IFF file by opening it for write and writing the type and subtype
  131.  */
  132. int CreateIFF(char *filename, ULONG type, ULONG subtype)
  133. {
  134.     int fd;
  135.  
  136.     if ((fd = creat(filename, 0664)) == -1)
  137.     {
  138.         perror(filename);
  139.         return(-1);
  140.     }
  141.  
  142.     if (!WriteSuperChunkHeader(fd,type,subtype,0L))
  143.     {
  144.         fprintf(stderr,"create_archive: couldn't write CAT chunkheader of new archive\n");
  145.         return(-1);
  146.     }
  147.  
  148.     /* success, return the archive fd */
  149.     return(fd);
  150. }
  151.  
  152. /* end of iff.c */
  153.