home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 403.lha / VediSrc / iffr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-06  |  11.3 KB  |  323 lines

  1. /*----------------------------------------------------------------------*
  2.  * IFFR.C  Support routines for reading IFF-85 files.          1/23/86
  3.  * (IFF is Interchange Format File.)
  4.  *
  5.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  6.  * This software is in the public domain.
  7.  *
  8.  * This version for the Commodore-Amiga computer.
  9.  *
  10.  * Uses "gio".  Either link with gio.c, or set the GIO_ACTIVE flag to 0
  11.  * in gio.h.
  12.  *----------------------------------------------------------------------*/
  13. #include "iff/intuall.h"
  14. #include "iff/gio.h"
  15. #include "iff/iff.h"
  16. #include "functions.h"
  17.  
  18. /* ----- Private subroutine FileLength() --------------------------------*/
  19. /* Returns the length of the file or else a negative IFFP error code
  20.  * (NO_FILE or DOS_ERROR). AmigaDOS-specific implementation.
  21.  * SIDE EFFECT: Thanks to AmigaDOS, we have to change the file's position
  22.  * to find its length.
  23.  * Now if Amiga DOS maintained fh_End, we'd just do this:
  24.  *    fileLength = (FileHandle *)BADDR(file)->fh_End; */
  25. LONG FileLength(file)  BPTR file;  {
  26.     LONG fileLength = NO_FILE;
  27.  
  28.     if (file > 0L)  {
  29.         GSeek(file, 0L, OFFSET_END);   /* Seek to end of file.*/
  30.         fileLength = GSeek(file, 0L, OFFSET_CURRENT);
  31.             /* Returns position BEFORE the seek, which is #bytes in file. */
  32.         if (fileLength < 0L)
  33.             fileLength = DOS_ERROR;   /* DOS being absurd.*/
  34.         }
  35.  
  36.     return(fileLength);
  37.     }
  38.  
  39. /* ---------- Read -----------------------------------------------------*/
  40.  
  41. /* ---------- OpenRIFF --------------------------------------------------*/
  42. IFFP OpenRIFF(file0, new0, clientFrame)
  43.     register BPTR file0;   GroupContext *new0;  ClientFrame *clientFrame; {
  44.     BPTR file = file0;
  45.     register GroupContext *new = new0;
  46.     IFFP iffp = IFF_OKAY;
  47.  
  48.     new->parent       = NULL;      /* "whole file" has no parent.*/
  49.     new->clientFrame  = clientFrame;
  50.     new->file         = file;
  51.     new->position     = 0L;
  52.     new->ckHdr.ckID   = new->subtype    = NULL_CHUNK;
  53.     new->ckHdr.ckSize = new->bytesSoFar = 0L;
  54.  
  55.     /* Set new->bound and go to the file's beginning. */
  56.     new->bound = FileLength(file);
  57.     if (new->bound < 0L)
  58.         iffp = new->bound;         /* File system error! */
  59.     else if ( new->bound < (long)sizeof(ChunkHeader) )
  60.         iffp = NOT_IFF;            /* Too small for an IFF file. */
  61.     else
  62.         GSeek(file, 0L, OFFSET_BEGINNING);  /* Go to file start. */
  63.  
  64.     return(iffp);
  65.     }
  66.  
  67. /* ---------- OpenRGroup -----------------------------------------------*/
  68. IFFP OpenRGroup(parent0, new0)   GroupContext *parent0, *new0; {
  69.     register GroupContext *parent = parent0;
  70.     register GroupContext *new    = new0;
  71.     IFFP iffp = IFF_OKAY;
  72.  
  73.     new->parent       = parent;
  74.     new->clientFrame  = parent->clientFrame;
  75.     new->file         = parent->file;
  76.     new->position     = parent->position;
  77.     new->bound        = parent->position + ChunkMoreBytes(parent);
  78.     new->ckHdr.ckID   = new->subtype    = NULL_CHUNK;
  79.     new->ckHdr.ckSize = new->bytesSoFar = 0L;
  80.  
  81.     if ( new->bound > parent->bound  ||  IS_ODD(new->bound) )
  82.         iffp = BAD_IFF;
  83.     return(iffp);
  84.     }
  85.     
  86. /* ---------- CloseRGroup -----------------------------------------------*/
  87. IFFP CloseRGroup(context)   GroupContext *context; {
  88.     register LONG position;
  89.  
  90.     if (context->parent == NULL) {
  91.         }  /* Context for whole file.*/
  92.     else {
  93.         position = context->position;
  94.         context->parent->bytesSoFar += position - context->parent->position;
  95.         context->parent->position = position;
  96.         }
  97.     return(IFF_OKAY);
  98.     }
  99.  
  100. /* ---------- SkipFwd --------------------------------------------------*/
  101. /* Skip over bytes in a context. Won't go backwards.*/
  102. /* Updates context->position but not context->bytesSoFar.*/
  103. /* This implementation is AmigaDOS specific.*/
  104. IFFP SkipFwd(context, bytes)   GroupContext *context;  LONG bytes; {
  105.     IFFP iffp = IFF_OKAY;
  106.  
  107.     if (bytes > 0L) {
  108.         if (-1L == GSeek(context->file, bytes, 0L))
  109.             iffp = BAD_IFF;   /* Ran out of bytes before chunk complete.*/
  110.         else
  111.             context->position += bytes;
  112.         }
  113.     return(iffp);
  114.     }
  115.  
  116. /* ---------- GetChunkHdr ----------------------------------------------*/
  117. ID GetChunkHdr(context0)   GroupContext *context0;  {
  118.     register GroupContext *context = context0;
  119.     register IFFP iffp;
  120.     LONG remaining;
  121.  
  122.     /* Skip remainder of previous chunk & padding. */
  123.     iffp = SkipFwd(context,
  124.         ChunkMoreBytes(context) + IS_ODD(context->ckHdr.ckSize));
  125.     CheckIFFP();
  126.  
  127.     /* Set up to read the new header. */
  128.     context->ckHdr.ckID = BAD_IFF;      /* Until we know it's okay, mark it BAD.*/
  129.     context->subtype    = NULL_CHUNK;
  130.     context->bytesSoFar = 0L;
  131.  
  132.     /* Generate a psuedo-chunk if at end-of-context. */
  133.     remaining = context->bound - context->position;
  134.     if (remaining == 0L) {
  135.         context->ckHdr.ckSize = 0L;
  136.         context->ckHdr.ckID   = END_MARK;
  137.         }
  138.  
  139.     /* BAD_IFF if not enough bytes in the context for a ChunkHeader.*/
  140.     else if ((long)sizeof(ChunkHeader) > remaining) {
  141.         context->ckHdr.ckSize = remaining;
  142.         }
  143.  
  144.     /* Read the chunk header (finally). */
  145.     else {
  146.         switch (
  147.             GRead(context->file, (BYTE *)&context->ckHdr,(long)sizeof(ChunkHeader))
  148.             ) {
  149.             case -1: return(context->ckHdr.ckID = DOS_ERROR);
  150.             case 0:  return(context->ckHdr.ckID = BAD_IFF);
  151.             }
  152.  
  153.         /* Check: Top level chunk must be LIST or FORM or CAT. */
  154.         if (context->parent == NULL)
  155.             switch(context->ckHdr.ckID) {
  156.                 case FORM:  case LIST:  case CAT:  break;
  157.                 default:    return(context->ckHdr.ckID = NOT_IFF);
  158.                 }
  159.         /* Update the context. */
  160.         context->position += (long)sizeof(ChunkHeader);
  161.         remaining         -= (long)sizeof(ChunkHeader);
  162.  
  163.         /* Non-positive ID values are illegal and used for error codes.*/
  164.         /* We could check for other illegal IDs...*/
  165.         if (context->ckHdr.ckID <= 0L)
  166.             context->ckHdr.ckID = BAD_IFF;
  167.  
  168.         /* Check: ckSize negative or larger than # bytes left in context? */
  169.         else if (context->ckHdr.ckSize < 0L  ||
  170.                  context->ckHdr.ckSize > remaining) {
  171.             context->ckHdr.ckSize = remaining;
  172.             context->ckHdr.ckID   = BAD_IFF;
  173.             }
  174.  
  175.         /* Automatically read the LIST, FORM, PROP, or CAT subtype ID */
  176.         else switch (context->ckHdr.ckID) {
  177.             case LIST:  case FORM:  case PROP:  case CAT:  {
  178.                 iffp = IFFReadBytes(context,
  179.                                     (BYTE *)&context->subtype,
  180.                                     (long)sizeof(ID));
  181.                 if (iffp != IFF_OKAY)
  182.                     context->ckHdr.ckID = iffp;
  183.                 break; }
  184.             }
  185.  
  186.         }
  187.     return(context->ckHdr.ckID);
  188.     }
  189.  
  190. /* ---------- IFFReadBytes ---------------------------------------------*/
  191. IFFP IFFReadBytes(context, buffer, nBytes)
  192.     GroupContext *context;   BYTE *buffer;   LONG nBytes; {
  193.     register IFFP iffp = IFF_OKAY;
  194.  
  195.     if (nBytes < 0L)
  196.         iffp = CLIENT_ERROR;
  197.     else if (nBytes > ChunkMoreBytes(context))
  198.         iffp = SHORT_CHUNK;
  199.     else if (nBytes > 0L) {
  200.         switch ( GRead(context->file, buffer, nBytes) ) {
  201.             case -1: {iffp = DOS_ERROR; break; }
  202.             case 0:  {iffp = BAD_IFF;   break; }
  203.             default: {
  204.                 context->position   += nBytes;
  205.                 context->bytesSoFar += nBytes;
  206.                 }
  207.             }
  208.         }
  209.     return(iffp);
  210.     }
  211.  
  212. /* ---------- SkipGroup ------------------------------------------------*/
  213. IFFP SkipGroup(context)  GroupContext *context;  {
  214.     }   /* Nothing to do, thanks to GetChunkHdr */
  215.  
  216. /* ---------- ReadIFF --------------------------------------------------*/
  217. IFFP ReadIFF(file, clientFrame)  BPTR file;  ClientFrame *clientFrame;  {
  218.     /*CompilerBug register*/ IFFP iffp;
  219.     GroupContext context;
  220.  
  221.     iffp = OpenRIFF(file, &context);
  222.     context.clientFrame = clientFrame;
  223.  
  224.     if (iffp == IFF_OKAY)
  225.         switch (iffp = GetChunkHdr(&context)) {
  226.             case FORM: { iffp = (*clientFrame->getForm)(&context); break; }
  227.             case LIST: { iffp = (*clientFrame->getList)(&context); break; }
  228.             case CAT : { iffp = (*clientFrame->getCat )(&context); break; }
  229.             /* default: Includes IFF_DONE, BAD_IFF, NOT_IFF... */
  230.             }
  231.  
  232.     CloseRGroup(&context);
  233.  
  234.     if (iffp > 0L)              /* Make sure we don't return an ID.*/
  235.         iffp = NOT_IFF;         /* GetChunkHdr should've caught this.*/
  236.     return(iffp);
  237.     }
  238.  
  239. /* ---------- ReadIList ------------------------------------------------*/
  240. IFFP ReadIList(parent, clientFrame)
  241.     GroupContext *parent;  ClientFrame *clientFrame; {
  242.     GroupContext listContext;
  243.     IFFP iffp;
  244.     BOOL propOk = TRUE;
  245.  
  246.     iffp = OpenRGroup(parent, &listContext);
  247.     CheckIFFP();
  248.  
  249.     /* One special case test lets us handle CATs as well as LISTs.*/
  250.     if (parent->ckHdr.ckID == CAT)
  251.         propOk = FALSE;
  252.     else
  253.         listContext.clientFrame = clientFrame;
  254.  
  255.     do {
  256.         switch (iffp = GetChunkHdr(&listContext)) {
  257.             case PROP: {
  258.                 if (propOk)
  259.                     iffp = (*clientFrame->getProp)(&listContext);
  260.                 else
  261.                     iffp = BAD_IFF;
  262.                 break;
  263.                 }
  264.             case FORM: { iffp = (*clientFrame->getForm)(&listContext); break; }
  265.             case LIST: { iffp = (*clientFrame->getList)(&listContext); break; }
  266.             case CAT : { iffp = (*clientFrame->getCat )(&listContext); break; }
  267.             /* default: Includes END_MARK, IFF_DONE, BAD_IFF, NOT_IFF... */
  268.             }
  269.         if (listContext.ckHdr.ckID != PROP)
  270.             propOk = FALSE;   /* No PROPs allowed after this point.*/
  271.         } while (iffp == IFF_OKAY);
  272.  
  273.     CloseRGroup(&listContext);
  274.  
  275.     if (iffp > 0L)  /* Only chunk types above are allowed in a LIST/CAT.*/
  276.         iffp = BAD_IFF;
  277.     return(iffp == END_MARK ? IFF_OKAY : iffp);
  278.     }
  279.  
  280. /* ---------- ReadICat -------------------------------------------------*/
  281. /* By special arrangement with the ReadIList implement'n, this is trivial.*/
  282. IFFP ReadICat(parent)  GroupContext *parent;  {
  283.     return( ReadIList(parent, NULL) );
  284.     }
  285.  
  286. /* ---------- GetFChunkHdr ---------------------------------------------*/
  287. ID GetFChunkHdr(context)   GroupContext *context; {
  288.     register ID id;
  289.  
  290.     id = GetChunkHdr(context);
  291.     if (id == PROP)
  292.         context->ckHdr.ckID = id = BAD_IFF;
  293.     return(id);
  294.     }
  295.  
  296. /* ---------- GetF1ChunkHdr ---------------------------------------------*/
  297. ID GetF1ChunkHdr(context)   GroupContext *context; {
  298.     register ID id;
  299.     register ClientFrame *clientFrame = context->clientFrame;
  300.  
  301.     switch (id = GetChunkHdr(context))  {
  302.         case PROP: { id = BAD_IFF; break; }
  303.         case FORM: { id = (*clientFrame->getForm)(context); break; }
  304.         case LIST: { id = (*clientFrame->getList)(context); break; }
  305.         case CAT : { id = (*clientFrame->getCat )(context); break; }
  306.         /* Default: let the caller handle other chunks */
  307.         }
  308.     return(context->ckHdr.ckID = id);
  309.     }
  310.  
  311. /* ---------- GetPChunkHdr ---------------------------------------------*/
  312. ID GetPChunkHdr(context)   GroupContext *context; {
  313.     register ID id;
  314.  
  315.     id = GetChunkHdr(context);
  316.     switch (id) {
  317.         case LIST:  case FORM:  case PROP:  case CAT:  {
  318.             id = context->ckHdr.ckID = BAD_IFF;
  319.             break; }
  320.         }
  321.     return(id);
  322.     }
  323.