home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 004.lha / Dpaintx_source / IFF.h < prev    next >
Encoding:
C/C++ Source or Header  |  1986-03-02  |  19.7 KB  |  433 lines

  1. #ifndef IFF_H
  2. #define IFF_H
  3. /*----------------------------------------------------------------------*/
  4. /* IFF.H  defs for IFF-85 Interchange Format Files.           10/10/85 */
  5. /*                                           */
  6. /* By Jerry Morrison and Steve Shaw, Electronic Arts.            */
  7. /* This software is in the public domain.                   */
  8. /*----------------------------------------------------------------------*/
  9.  
  10. #ifndef LIBRARIES_DOS_H
  11. #include "libraries/dos.h"
  12. #endif
  13.  
  14. typedef LONG IFFP;  /* Status code result from an IFF procedure */
  15.      /* LONG, because must be type compatable with ID for GetChunkHdr.*/
  16.      /* Note that the error codes below are not legal IDs.*/
  17. #define IFF_OKAY  0 /* Keep going...*/
  18. #define END_MARK  -1     /* As if there was a chunk at end of group.*/
  19. #define IFF_DONE  -2     /* clientProc returns this when it has READ enough.
  20.                 * It means return thru all levels. File is Okay.*/
  21. #define DOS_ERROR -3
  22. #define NOT_IFF   -4     /* not an IFF file.*/
  23. #define NO_FILE   -5     /* Tried to open file, DOS didn't find it.*/
  24. #define CLIENT_ERROR -6  /* Client made invalid request, for instance, asking
  25.                 * for more bytes than existed in chunk.*/
  26. #define BAD_FORM  -7     /* A client read proc complains about FORM semantics;
  27.                 * e.g. valid IFF, but missing a required chunk.*/
  28. #define SHORT_CHUNK -8   /* Client asked to IFFReadBytes more bytes than left
  29.                 * in the chunk. Could be client bug or bad form.*/
  30. #define BAD_IFF   -9     /* mal-formed IFF file. [TBD] Expand this into a
  31.                 * range of error codes.*/
  32. #define LAST_ERROR BAD_IFF
  33.  
  34. /* This MACRO is used to RETURN immediately when a termination condition is
  35.  * found. This is a pretty weird macro. It requires the caller to declare a
  36.  * local "IFFP iffp" and assign it. This wouldn't work as a subroutine since
  37.  * it returns for it's caller. */
  38. #define CheckIFFP()   { if (iffp != IFF_OKAY) return(iffp); }
  39.  
  40.  
  41. /* ---------- ID -------------------------------------------------------*/
  42.  
  43. typedef LONG ID;    /* An ID is four printable ASCII chars but
  44.                 * stored as a LONG for efficient copy & compare.*/
  45.  
  46. /* Four-character IDentifier builder.*/
  47. #define MakeID(a,b,c,d)  ( (a)<<24 | (b)<<16 | (c)<<8 | (d) )
  48.  
  49. /* Standard group IDs.  A chunk with one of these IDs contains a
  50.    SubTypeID followed by zero or more chunks.*/
  51. #define FORM MakeID('F','O','R','M')
  52. #define PROP MakeID('P','R','O','P')
  53. #define LIST MakeID('L','I','S','T')
  54. #define CAT  MakeID('C','A','T',' ')
  55. #define FILLER MakeID(' ',' ',' ',' ')
  56. /* The IDs "FOR1".."FOR9", "LIS1".."LIS9", & "CAT1".."CAT9" are reserved
  57.  * for future standardization.*/
  58.  
  59. /* Pseudo-ID used internally by chunk reader and writer.*/
  60. #define NULL_CHUNK 0L           /* No current chunk.*/
  61.  
  62.  
  63. /* ---------- Chunk ----------------------------------------------------*/
  64.  
  65. /* All chunks start with a type ID and a count of the data bytes that 
  66.    follow--the chunk's "logical size" or "data size". If that number is odd,
  67.    a 0 pad byte is written, too. */
  68. typedef struct {
  69.     ID      ckID;
  70.     LONG  ckSize;
  71.     } ChunkHeader;
  72.  
  73. typedef struct {
  74.     ID      ckID;
  75.     LONG  ckSize;
  76.     UBYTE ckData[ 1 /*REALLY: ckSize*/ ];
  77.     } Chunk;
  78.  
  79. /* Pass ckSize = szNotYetKnown to the writer to mean "compute the size".*/
  80. #define szNotYetKnown 0x80000001L
  81.  
  82. /* Need to know whether a value is odd so can word-align.*/
  83. #define IS_ODD(a)   ((a) & 1)
  84.  
  85. /* This macro rounds up to an even number. */
  86. #define WordAlign(size)   ((size+1)&~1)
  87.  
  88. /* ALL CHUNKS MUST BE PADDED TO EVEN NUMBER OF BYTES.
  89.  * ChunkPSize computes the total "physical size" of a padded chunk from
  90.  * its "data size" or "logical size". */
  91. #define ChunkPSize(dataSize)  (WordAlign(dataSize) + sizeof(ChunkHeader))
  92.  
  93. /* The Grouping chunks (LIST, FORM, PROP, & CAT) contain concatenations of
  94.  * chunks after a subtype ID that identifies the content chunks.
  95.  * "FORM type XXXX", "LIST of FORM type XXXX", "PROPerties associated
  96.  * with FORM type XXXX", or "conCATenation of XXXX".*/
  97. typedef struct {
  98.     ID      ckID;
  99.     LONG  ckSize;   /* this ckSize includes "grpSubID".*/
  100.     ID    grpSubID;
  101.     } GroupHeader;
  102.  
  103. typedef struct {
  104.     ID      ckID;
  105.     LONG  ckSize;
  106.     ID    grpSubID;
  107.     UBYTE grpData[ 1 /*REALLY: ckSize-sizeof(grpSubID)*/ ];
  108.     } GroupChunk;
  109.  
  110.  
  111. /* ---------- IFF Reader -----------------------------------------------*/
  112.  
  113. /******** Routines to support a stream-oriented IFF file reader *******
  114.  *
  115.  * These routines handle lots of details like error checking and skipping
  116.  * over padding. They're also careful not to read past any containing context.
  117.  *
  118.  * These routines ASSUME they're the only ones reading from the file.
  119.  * Client should check IFFP error codes. Don't press on after an error!
  120.  * These routines try to have no side effects in the error case, except
  121.  * partial I/O is sometimes unavoidable.
  122.  *
  123.  * All of these routines may return DOS_ERROR. In that case, ask DOS for the
  124.  * specific error code.
  125.  *
  126.  * The overall scheme for the low level chunk reader is to open a "group read
  127.  * context" with OpenRIFF or OpenRGroup, read the chunks with GetChunkHdr
  128.  * (and its kin) and IFFReadBytes, and close the context with CloseRGroup.
  129.  *
  130.  * The overall scheme for reading an IFF file is to use ReadIFF, ReadIList,
  131.  * and ReadICat to scan the file. See those procedures, ClientProc (below),
  132.  * and the skeleton IFF reader. */
  133.  
  134. /* Client passes ptrs to procedures of this type to ReadIFF which call them
  135.  * back to handle LISTs, FORMs, CATs, and PROPs.
  136.  *
  137.  * Use the GroupContext ptr when calling reader routines like GetChunkHdr.
  138.  * Look inside the GroupContext ptr for your ClientFrame ptr. You'll
  139.  * want to type cast it into a ptr to your containing struct to get your
  140.  * private contextual data (stacked property settings). See below. */
  141. typedef IFFP ClientProc(/* struct _GroupContext * */);
  142.  
  143. /* Client's context for reading an IFF file or a group.
  144.  * Client should actually make this the first component of a larger struct
  145.  * (it's personal stack "frame") that has a field to store each "interesting"
  146.  * property encountered.
  147.  * Either initialize each such field to a global default or keep a boolean
  148.  * indicating if you've read a property chunk into that field.
  149.  * Your getList and getForm procs should allocate a new "frame" and copy the
  150.  * parent frame's contents. The getProp procedure should store into the frame
  151.  * allocated by getList for the containing LIST. */
  152. typedef struct _ClientFrame {
  153.     ClientProc *getList, *getProp, *getForm, *getCat;
  154.     /* client's own data follows; place to stack property settings */
  155.     } ClientFrame;
  156.  
  157. /* Our context for reading a group chunk. */
  158. typedef struct _GroupContext {
  159.     struct _GroupContext *parent; /* Containing group; NULL => whole file. */
  160.     ClientFrame *clientFrame;     /* Reader data & client's context state. */
  161.     BPTR file;      /* Byte-stream file handle. */
  162.     LONG position;  /* The context's logical file position. */
  163.     LONG bound;          /* File-absolute context bound
  164.                 * or szNotYetKnown (writer only). */
  165.     ChunkHeader ckHdr;   /* Current chunk header. ckHdr.ckSize = szNotYetKnown
  166.                 * means we need to go back and set the size (writer only).
  167.                 * See also Pseudo-IDs, above. */
  168.     ID subtype;          /* Group's subtype ID when reading. */
  169.     LONG bytesSoFar;     /* # bytes read/written of current chunk's data. */
  170.     } GroupContext;
  171.  
  172. /* Computes the number of bytes not yet read from the current chunk, given
  173.  * a group read context gc. */
  174. #define ChunkMoreBytes(gc)  ((gc)->ckHdr.ckSize - (gc)->bytesSoFar)
  175.  
  176.  
  177. /***** Low Level IFF Chunk Reader *****/
  178.  
  179. /* Given an open file, open a read context spanning the whole file.
  180.  * This is normally only called by ReadIFF.
  181.  * This sets new->clientFrame = clientFrame.
  182.  * ASSUME context allocated by caller but not initialized.
  183.  * ASSUME caller doesn't deallocate the context before calling CloseRGroup.
  184.  * NOT_IFF ERROR if the file is too short for even a chunk header.*/
  185. extern IFFP OpenRIFF(/* BPTR, GroupContext *, ClientFrame * */);
  186.                   /* file, new,            clientFrame  */
  187.  
  188. /* Open the remainder of the current chunk as a group read context.
  189.  * This will be called just after the group's subtype ID has been read
  190.  * (automatically by GetChunkHdr for LIST, FORM, PROP, and CAT) so the
  191.  * remainder is a sequence of chunks.
  192.  * This sets new->clientFrame = parent->clientFrame. The caller should repoint
  193.  * it at a new clientFrame if opening a LIST context so it'll have a "stack
  194.  * frame" to store PROPs for the LIST. (It's usually convenient to also
  195.  * allocate a new Frame when you encounter FORM of the right type.)
  196.  *
  197.  * ASSUME new context allocated by caller but not initialized.
  198.  * ASSUME caller doesn't deallocate the context or access the parent context
  199.  * before calling CloseRGroup.
  200.  * BAD_IFF ERROR if context end is odd or extends past parent. */
  201. extern IFFP OpenRGroup(/* GroupContext *, GroupContext * */);
  202.                  /* parent,         new  */
  203.  
  204. /* Close a group read context, updating its parent context.
  205.  * After calling this, the old context may be deallocated and the parent
  206.  * context can be accessed again. It's okay to call this particular procedure
  207.  * after an error has occurred reading the group.
  208.  * This always returns IFF_OKAY. */
  209. extern IFFP CloseRGroup(/* GroupContext * */);
  210.                   /* old  */
  211.  
  212. /* Skip any remaining bytes of the previous chunk and any padding, then
  213.  * read the next chunk header into context.ckHdr.
  214.  * If the ckID is LIST, FORM, CAT, or PROP, this automatically reads the
  215.  * subtype ID into context->subtype.
  216.  * Caller should dispatch on ckID (and subtype) to an appropriate handler.
  217.  *
  218.  * RETURNS context.ckHdr.ckID (the ID of the new chunk header); END_MARK
  219.  * if there are no more chunks in this context; or NOT_IFF if the top level
  220.  * file chunk isn't a FORM, LIST, or CAT; or BAD_IFF if malformed chunk, e.g.
  221.  * ckSize is negative or too big for containing context, ckID isn't positive,
  222.  * or we hit end-of-file.
  223.  *
  224.  * See also GetFChunkHdr, GetF1ChunkHdr, and GetPChunkHdr, below.*/
  225. extern ID       GetChunkHdr(/* GroupContext * */);
  226.   /*  context.ckHdr.ckID       context  */
  227.  
  228. /* Read nBytes number of data bytes of current chunk. (Use OpenGroup, etc.
  229.  * instead to read the contents of a group chunk.) You can call this several
  230.  * times to read the data piecemeal.
  231.  * CLIENT_ERROR if nBytes < 0. SHORT_CHUNK if nBytes > ChunkMoreBytes(context)
  232.  * which could be due to a client bug or a chunk that's shorter than it
  233.  * ought to be (bad form). (on either CLIENT_ERROR or SHORT_CHUNK,
  234.  * IFFReadBytes won't read any bytes.) */
  235. extern IFFP IFFReadBytes(/* GroupContext *, BYTE *, LONG */);
  236.                    /* context,        buffer, nBytes  */
  237.  
  238.  
  239. /***** IFF File Reader *****/
  240.  
  241. /* This is a noop ClientProc that you can use for a getList, getForm, getProp,
  242.  * or getCat procedure that just skips the group. A simple reader might just
  243.  * implement getForm, store &ReadICat in the getCat field of clientFrame, and
  244.  * use &SkipGroup for the getList and getProp procs.*/
  245. extern IFFP SkipGroup(/* GroupContext * */);
  246.  
  247. /* IFF file reader.
  248.  * Given an open file, allocate a group context and use it to read the FORM,
  249.  * LIST, or CAT and it's contents. The idea is to parse the file's contents,
  250.  * and for each FORM, LIST, CAT, or PROP encountered, call the getForm,
  251.  * getList, getCat, or getProp procedure in clientFrame, passing the
  252.  * GroupContext ptr.
  253.  * This is achieved with the aid of ReadIList (which your getList should
  254.  * call) and ReadICat (which your getCat should call, if you don't just use
  255.  * &ReadICat for your getCat). If you want to handle FORMs, LISTs, and CATs
  256.  * nested within FORMs, the getForm procedure must dispatch to getForm,
  257.  * getList, and getCat (it can use GetF1ChunkHdr to make this easy).
  258.  *
  259.  * Normal return is IFF_OKAY (if whole file scanned) or IFF_DONE (if a client
  260.  * proc said "done" first).
  261.  * See the skeletal getList, getForm, getCat, and getProp procedures. */
  262. extern IFFP ReadIFF(/* BPTR, ClientFrame * */);
  263.                     /* file, clientFrame  */
  264.  
  265. /* IFF LIST reader.
  266.  * Your "getList" procedure should allocate a ClientFrame, copy the parent's
  267.  * ClientFrame, and then call this procedure to do all the work.
  268.  *
  269.  * Normal return is IFF_OKAY (if whole LIST scanned) or IFF_DONE (if a client
  270.  * proc said "done" first).
  271.  * BAD_IFF ERROR if a PROP appears after a non-PROP. */
  272. extern IFFP ReadIList(/* GroupContext *, ClientFrame * */);
  273.                 /* parent,         clientFrame  */
  274.  
  275. /* IFF CAT reader.
  276.  * Most clients can simply use this to read their CATs. If you must do extra
  277.  * setup work, put a ptr to your getCat procedure in the clientFrame, and
  278.  * have that procedure call ReadICat to do the detail work.
  279.  *
  280.  * Normal return is IFF_OKAY (if whole CAT scanned) or IFF_DONE (if a client
  281.  * proc said "done" first).
  282.  * BAD_IFF ERROR if a PROP appears in the CAT. */
  283. extern IFFP ReadICat(/* GroupContext * */);
  284.                /* parent  */
  285.  
  286. /* Call GetFChunkHdr instead of GetChunkHdr to read each chunk inside a FORM.
  287.  * It just calls GetChunkHdr and returns BAD_IFF if it gets a PROP chunk. */
  288. extern ID GetFChunkHdr(/* GroupContext * */);
  289.   /*  context.ckHdr.ckID        context  */
  290.  
  291. /* GetF1ChunkHdr is like GetFChunkHdr, but it automatically dispatches to the
  292.  * getForm, getList, and getCat procedure (and returns the result) if it
  293.  * encounters a FORM, LIST, or CAT. */
  294. extern ID GetF1ChunkHdr(/* GroupContext * */);
  295.   /*  context.ckHdr.ckID         context  */
  296.  
  297. /* Call GetPChunkHdr instead of GetChunkHdr to read each chunk inside a PROP.
  298.  * It just calls GetChunkHdr and returns BAD_IFF if it gets a group chunk. */
  299. extern ID GetPChunkHdr(/* GroupContext * */);
  300.   /*  context.ckHdr.ckID        context  */
  301.  
  302.  
  303. /* ---------- IFF Writer -----------------------------------------------*/
  304.  
  305. /******* Routines to support a stream-oriented IFF file writer *******
  306.  *
  307.  * These routines will random access back to set a chunk size value when the
  308.  * caller doesn't know it ahead of time. They'll also do things automatically
  309.  * like padding and error checking.
  310.  *
  311.  * These routines ASSUME they're the only ones writing to the file.
  312.  * Client should check IFFP error codes. Don't press on after an error!
  313.  * These routines try to have no side effects in the error case, except that
  314.  * partial I/O is sometimes unavoidable.
  315.  *
  316.  * All of these routines may return DOS_ERROR. In that case, ask DOS for the
  317.  * specific error code.
  318.  *
  319.  * The overall scheme is to open an output GroupContext via OpenWIFF or
  320.  * OpenWGroup, call either PutCk or {PutCkHdr {IFFWriteBytes}* PutCkEnd} for
  321.  * each chunk, then use CloseWGroup to close the GroupContext.
  322.  *
  323.  * To write a group (LIST, FORM, PROP, or CAT), call StartWGroup, write out
  324.  * its chunks, then call EndWGroup. StartWGroup automatically writes the
  325.  * group header and opens a nested context for writing the contents.
  326.  * EndWGroup closes the nested context and completes the group chunk. */
  327.  
  328.  
  329. /* Given a file open for output, open a write context.
  330.  * The "limit" arg imposes a fence or upper limit on the logical file
  331.  * position for writing data in this context. Pass in szNotYetKnown to be
  332.  * bounded only by disk capacity.
  333.  * ASSUME new context structure allocated by caller but not initialized.
  334.  * ASSUME caller doesn't deallocate the context before calling CloseWGroup.
  335.  * The caller is only allowed to write out one FORM, LIST, or CAT in this top
  336.  * level context (see StartWGroup and PutCkHdr).
  337.  * CLIENT_ERROR if limit is odd.*/
  338. extern IFFP OpenWIFF(/* BPTR, GroupContext *, LONG */);
  339.                /* file, new,            limit {file position}  */
  340.  
  341. /* Start writing a group (presumably LIST, FORM, PROP, or CAT), opening a
  342.  * nested context. The groupSize includes all nested chunks + the subtype ID.
  343.  *
  344.  * The subtype of a LIST or CAT is a hint at the contents' FORM type(s). Pass
  345.  * in FILLER if it's a mixture of different kinds.
  346.  *
  347.  * This writes the chunk header via PutCkHdr, writes the subtype ID via
  348.  * IFFWriteBytes, and calls OpenWGroup. The caller may then write the nested
  349.  * chunks and finish by calling EndWGroup.
  350.  * The OpenWGroup call sets new->clientFrame = parent->clientFrame.
  351.  *
  352.  * ASSUME new context structure allocated by caller but not initialized.
  353.  * ASSUME caller doesn't deallocate the context or access the parent context
  354.  * before calling CloseWGroup.
  355.  * ERROR conditions: See PutCkHdr, IFFWriteBytes, OpenWGroup. */
  356. extern IFFP StartWGroup(/* GroupContext *, ID, LONG, ID, GroupContext * */);
  357.                   /* parent, groupType, groupSize, subtype, new  */
  358.  
  359. /* End a group started by StartWGroup.
  360.  * This just calls CloseWGroup and PutCkEnd.
  361.  * ERROR conditions: See CloseWGroup and PutCkEnd. */
  362. extern IFFP EndWGroup(/* GroupContext * */);
  363.                 /* old  */
  364.  
  365. /* Open the remainder of the current chunk as a group write context.
  366.  * This is normally only called by StartWGroup.
  367.  *
  368.  * Any fixed limit to this group chunk or a containing context will impose
  369.  * a limit on the new context.
  370.  * This will be called just after the group's subtype ID has been written
  371.  * so the remaining contents will be a sequence of chunks.
  372.  * This sets new->clientFrame = parent->clientFrame.
  373.  * ASSUME new context structure allocated by caller but not initialized.
  374.  * ASSUME caller doesn't deallocate the context or access the parent context
  375.  * before calling CloseWGroup.
  376.  * CLIENT_ERROR if context end is odd or PutCkHdr wasn't called first. */
  377. extern IFFP OpenWGroup(/* GroupContext *, GroupContext * */);
  378.                  /* parent,         new  */
  379.  
  380. /* Close a write context and update its parent context.
  381.  * This is normally only called by EndWGroup.
  382.  *
  383.  * If this is a top level context (created by OpenWIFF) we'll set the file's
  384.  * EOF (end of file) but won't close the file.
  385.  * After calling this, the old context may be deallocated and the parent
  386.  * context can be accessed again.
  387.  *
  388.  * Amiga DOS Note: There's no call to set the EOF. We just position to the
  389.  * desired end and return. Caller must Close file at that position.
  390.  * CLIENT_ERROR if PutCkEnd wasn't called first. */
  391. extern IFFP CloseWGroup(/* GroupContext * */);
  392.                   /* old  */
  393.  
  394. /* Write a whole chunk to a GroupContext. This writes a chunk header, ckSize
  395.  * data bytes, and (if needed) a pad byte. It also updates the GroupContext.
  396.  * CLIENT_ERROR if ckSize == szNotYetKnown. See also PutCkHdr errors. */
  397. extern IFFP PutCk(/* GroupContext *, ID,   LONG,   BYTE * */);
  398.                /* context,        ckID, ckSize, *data  */
  399.  
  400. /* Write just a chunk header. Follow this will any number of calls to
  401.  * IFFWriteBytes and finish with PutCkEnd.
  402.  * If you don't yet know how big the chunk is, pass in ckSize = szNotYetKnown,
  403.  * then PutCkEnd will set the ckSize for you later.
  404.  * Otherwise, IFFWriteBytes and PutCkEnd will ensure that the specified
  405.  * number of bytes get written.
  406.  * CLIENT_ERROR if the chunk would overflow the GroupContext's bound, if
  407.  * PutCkHdr was previously called without a matching PutCkEnd, if ckSize < 0
  408.  * (except szNotYetKnown), if you're trying to write something other
  409.  * than one FORM, LIST, or CAT in a top level (file level) context, or
  410.  * if ckID <= 0 (these illegal ID values are used for error codes). */
  411. extern IFFP PutCkHdr(/* GroupContext *, ID,   LONG */);
  412.                /* context,        ckID, ckSize  */
  413.  
  414. /* Write nBytes number of data bytes for the current chunk and update
  415.  * GroupContext.
  416.  * CLIENT_ERROR if this would overflow the GroupContext's limit or the
  417.  * current chunk's ckSize, or if PutCkHdr wasn't called first, or if
  418.  * nBytes < 0. */
  419. extern IFFP IFFWriteBytes(/* GroupContext *, BYTE *, LONG */);
  420.                     /* context,        *data,  nBytes  */
  421.  
  422. /* Complete the current chunk, write a pad byte if needed, and update
  423.  * GroupContext.
  424.  * If current chunk's ckSize = szNotYetKnown, this goes back and sets the
  425.  * ckSize in the file.
  426.  * CLIENT_ERROR if PutCkHdr wasn't called first, or if client hasn't
  427.  * written 'ckSize' number of bytes with IFFWriteBytes. */
  428. extern IFFP PutCkEnd(/* GroupContext * */);
  429.                /* context  */
  430.  
  431. #endif
  432.  
  433.