blob: 770b5259f1e0dc2f522def84a7098a47fd52ead0 [file] [log] [blame]
Fred Drake97793ab1999-06-22 18:49:20 +00001\section{\module{chunk} ---
Fred Drake60b66e11999-06-25 17:52:17 +00002 Read IFF chunked data}
Fred Drake97793ab1999-06-22 18:49:20 +00003
4\declaremodule{standard}{chunk}
Fred Drake60b66e11999-06-25 17:52:17 +00005\modulesynopsis{Module to read IFF chunks.}
6\moduleauthor{Sjoerd Mullender}{sjoerd@acm.org}
7\sectionauthor{Sjoerd Mullender}{sjoerd@acm.org}
Fred Drake97793ab1999-06-22 18:49:20 +00008
Fred Drake97793ab1999-06-22 18:49:20 +00009
Fred Drake97793ab1999-06-22 18:49:20 +000010
Fred Drake60b66e11999-06-25 17:52:17 +000011This module provides an interface for reading files that use EA IFF 85
12chunks.\footnote{``EA IFF 85'' Standard for Interchange Format Files,
13Jerry Morrison, Electronic Arts, January 1985.} This format is used
14in at least the Audio\index{Audio Interchange File
15Format}\index{AIFF}\index{AIFF-C} Interchange File Format
16(AIFF/AIFF-C), the Real\index{Real Media File Format} Media File
17Format\index{RMFF} (RMFF), and the
18Tagged\index{Tagged Image File Format} Image File Format\index{TIFF}
19(TIFF).
Fred Drake97793ab1999-06-22 18:49:20 +000020
Fred Drake60b66e11999-06-25 17:52:17 +000021A chunk has the following structure:
Fred Drake97793ab1999-06-22 18:49:20 +000022
23\begin{tableiii}{c|c|l}{textrm}{Offset}{Length}{Contents}
24 \lineiii{0}{4}{Chunk ID}
25 \lineiii{4}{4}{Size of chunk in big-endian byte order, including the
26 header}
Fred Drake60b66e11999-06-25 17:52:17 +000027 \lineiii{8}{\var{n}}{Data bytes, where \var{n} is the size given in
28 the preceeding field}
29 \lineiii{8 + \var{n}}{0 or 1}{Pad byte needed if \var{n} is odd and
30 chunk alignment is used}
Fred Drake97793ab1999-06-22 18:49:20 +000031\end{tableiii}
32
Fred Drake60b66e11999-06-25 17:52:17 +000033The ID is a 4-byte string which identifies the type of chunk.
Fred Drake97793ab1999-06-22 18:49:20 +000034
Fred Drake60b66e11999-06-25 17:52:17 +000035The size field (a 32-bit value, encoded using big-endian byte order)
36gives the size of the whole chunk, including the 8-byte header.
Fred Drake97793ab1999-06-22 18:49:20 +000037
Fred Drake60b66e11999-06-25 17:52:17 +000038Usually an IFF-type file consists of one or more chunks. The proposed
39usage of the \class{Chunk} class defined here is to instantiate an
40instance at the start of each chunk and read from the instance until
41it reaches the end, after which a new instance can be instantiated.
42At the end of the file, creating a new instance will fail with a
43\exception{EOFError} exception.
44
45\begin{classdesc}{Chunk}{file\optional{, align}}
46Class which represents a chunk. The \var{file} argument is expected
47to be a file-like object. An instance of this class is specifically
48allowed. The only method that is needed is \method{read()}. If the
49methods \method{seek()} and \method{tell()} are present and don't
50raise an exception, they are also used. If these methods are present
51and raise an exception, they are expected to not have altered the
52object. If the optional argument \var{align} is true, chunks are
53assumed to be aligned on 2-byte boundaries. If \var{align} is
54false, no alignment is assumed. The default value is true.
55\end{classdesc}
56
57A \class{Chunk} object supports the following methods:
Fred Drake97793ab1999-06-22 18:49:20 +000058
59\begin{methoddesc}{getname}{}
Fred Drake60b66e11999-06-25 17:52:17 +000060Returns the name (ID) of the chunk. This is the first 4 bytes of the
61chunk.
Fred Drake97793ab1999-06-22 18:49:20 +000062\end{methoddesc}
63
64\begin{methoddesc}{close}{}
Fred Drake60b66e11999-06-25 17:52:17 +000065Close and skip to the end of the chunk. This does not close the
66underlying file.
Fred Drake97793ab1999-06-22 18:49:20 +000067\end{methoddesc}
68
Fred Drake60b66e11999-06-25 17:52:17 +000069The remaining methods will raise \exception{IOError} if called after
70the \method{close()} method has been called.
71
Fred Drake97793ab1999-06-22 18:49:20 +000072\begin{methoddesc}{isatty}{}
Fred Drake60b66e11999-06-25 17:52:17 +000073Returns \code{0}.
Fred Drake97793ab1999-06-22 18:49:20 +000074\end{methoddesc}
75
Fred Drake60b66e11999-06-25 17:52:17 +000076\begin{methoddesc}{seek}{pos\optional{, whence}}
77Set the chunk's current position. The \var{whence} argument is
78optional and defaults to \code{0} (absolute file positioning); other
79values are \code{1} (seek relative to the current position) and
80\code{2} (seek relative to the file's end). There is no return value.
81If the underlying file does not allow seek, only forward seeks are
82allowed.
Fred Drake97793ab1999-06-22 18:49:20 +000083\end{methoddesc}
84
85\begin{methoddesc}{tell}{}
Fred Drake60b66e11999-06-25 17:52:17 +000086Return the current position into the chunk.
Fred Drake97793ab1999-06-22 18:49:20 +000087\end{methoddesc}
88
Fred Drake60b66e11999-06-25 17:52:17 +000089\begin{methoddesc}{read}{\optional{size}}
90Read at most \var{size} bytes from the chunk (less if the read hits
91the end of the chunk before obtaining \var{size} bytes). If the
92\var{size} argument is negative or omitted, read all data until the
93end of the chunk. The bytes are returned as a string object. An
94empty string is returned when the end of the chunk is encountered
95immediately.
Fred Drake97793ab1999-06-22 18:49:20 +000096\end{methoddesc}
97
98\begin{methoddesc}{skip}{}
99Skip to the end of the chunk. All further calls to \method{read()}
100for the chunk will return \code{''}. If you are not interested in the
101contents of the chunk, this method should be called so that the file
102points to the start of the next chunk.
103\end{methoddesc}