home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 497a.lha / ComSMUS_v2.2 / voicebank.prog / iff.c < prev    next >
C/C++ Source or Header  |  1991-04-07  |  3KB  |  168 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.  
  20. #include <hackercorp/assert.h>
  21. #include <hackercorp/iff.h>
  22.  
  23. extern long lseek();
  24.  
  25. extern ULONG nextchunk();
  26.  
  27. /* print a chunkID to stderr */
  28. PutID(id)
  29. ID id;
  30. {
  31.     fprintf(stderr,"%c%c%c%c",
  32.        (char)((id>>24L) & 0x7f),
  33.        (char)((id>>16L) & 0x7f),
  34.        (char)((id>>8)   & 0x7f),
  35.        (char)(id        & 0x7f) );
  36. }
  37.  
  38. /* write a chunk header, that's the 4-byte chunk ID and a 4-byte 
  39.  * chunk length 
  40.  */
  41. WriteChunkHeader(fd,chunktype,length)
  42. int fd;
  43. ULONG chunktype;
  44. long length;
  45. {
  46.     ChunkHeader chunkheader;
  47.  
  48.     chunkheader.ckID = chunktype;
  49.     chunkheader.ckSize = length;
  50.  
  51.     if (write(fd,&chunkheader,sizeof(chunkheader)) == -1)
  52.     {
  53.         perror("WriteChunkHeader");
  54.         return(0);
  55.     }
  56.     return(1);
  57. }
  58.  
  59. /* write a chunk that has a four character subtype, like FORM, CAT or LIST
  60. */
  61. WriteSuperChunkHeader(fd,chunktype,subtype,length)
  62. int fd;
  63. ULONG chunktype, subtype;
  64. long length;
  65. {
  66.     if (!WriteChunkHeader(fd,chunktype,length+sizeof(subtype)))
  67.         return(0);
  68.     return(write(fd,&subtype,sizeof(subtype)) != -1);
  69. }
  70.  
  71. /* write a chunk header and body, that's the 8-byte header and the data
  72.  * to match the specified length
  73.  */
  74. WriteChunk(fd,chunktype,data,length)
  75. int fd;
  76. ULONG chunktype;
  77. char *data;
  78. long length;
  79. {
  80.     static char zero = '\0';
  81.  
  82.     if (!WriteChunkHeader(fd,chunktype,length))
  83.         return(0);
  84.  
  85.     /* if there's a body, write it out */
  86.     if (length != 0)
  87.         if (write(fd,data,length) == -1)
  88.         {
  89.             perror("WriteChunk");
  90.             return(0);
  91.         }
  92.  
  93.     /* if the length is odd, write out an additional zero byte */
  94.     if (length & 1)
  95.         if (write(fd,&zero,1) == -1)
  96.         {
  97.             perror("WriteChunk");
  98.             return(0);
  99.         }
  100.     return(1);
  101. }
  102.  
  103. WriteTextChunk(fd,chunktype,text)
  104. int fd;
  105. ULONG chunktype;
  106. char *text;
  107. {
  108.     int len = strlen(text);
  109.     WriteChunk(fd,chunktype,text,len);
  110. }
  111.  
  112. /* rewrite_IFF_header - write (filesize - sizeof(ChunkHeader)) into
  113.  * IFF file's header, assumes file is open for write
  114.  */
  115. Rewrite_IFF_header(fd)
  116. int fd;
  117. {
  118.     long filesize;
  119.  
  120.     /* get filesize by seeking to EOF */
  121.     if ((filesize = lseek(fd,0,2)) == -1)
  122.     {
  123.         perror("rewrite_IFF_header");
  124.         return(0);
  125.     }
  126.  
  127.     /* go back to the beginning of the file */
  128.     if (lseek(fd,0,0) == -1)
  129.     {
  130.         perror("rewrite_IFF_header");
  131.         return(0);
  132.     }
  133.  
  134.     if (!WriteChunkHeader(fd,ID_FORM,(filesize - sizeof(ChunkHeader))))
  135.     {
  136.         perror("rewrite_IFF_header: WriteChunkHeader");
  137.         return(0);
  138.     }
  139.  
  140.     return(1);
  141. }
  142.  
  143. /* create an IFF file by opening it for write and writing the type and subtype
  144.  */
  145. int CreateIFF(filename,type,subtype)
  146. char *filename;
  147. ULONG type, subtype;
  148. {
  149.     int fd;
  150.  
  151.     if ((fd = creat(filename, 0664)) == -1)
  152.     {
  153.         perror(filename);
  154.         return(-1);
  155.     }
  156.  
  157.     if (!WriteSuperChunkHeader(fd,type,subtype,0L))
  158.     {
  159.         fprintf("create_archive: couldn't write CAT chunkheader of new archive\n");
  160.         return(-1);
  161.     }
  162.  
  163.     /* success, return the archive fd */
  164.     return(fd);
  165. }
  166.  
  167. /* end of iff.c */
  168.