Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
13 KiB

  1. /* mmioriff.c
  2. *
  3. * MMIO RIFF functions.
  4. */
  5. #include <windows.h>
  6. #include "mmsystem.h"
  7. #include "mmsysi.h"
  8. #include "mmioi.h"
  9. #define BCODE BYTE _based(_segname("_CODE"))
  10. static BCODE bPad;
  11. /* @doc EXTERNAL
  12. @api UINT | mmioDescend | This function descends into a chunk of a
  13. RIFF file opened with <f mmioOpen>. It can also search for a given
  14. chunk.
  15. @parm HMMIO | hmmio | Specifies the file handle of an open RIFF file.
  16. @parm LPMMCKINFO | lpck | Specifies a far pointer to a
  17. caller-supplied <t MMCKINFO> structure that <f mmioDescend> fills
  18. with the following information:
  19. -- The <e MMCKINFO.ckid> field is the chunk ID of the chunk.
  20. -- The <e MMCKINFO.cksize> field is the size of the data portion
  21. of the chunk. The data size includes the form type or list type (if
  22. any), but does not include the 8-byte chunk header or the pad byte at
  23. the end of the data (if any).
  24. -- The <e MMCKINFO.fccType> field is the form type if
  25. <e MMCKINFO.ckid> is "RIFF", or the list type if
  26. <e MMCKINFO.ckid> is "LIST". Otherwise, it is NULL.
  27. -- The <e MMCKINFO.dwDataOffset> field is the file offset of the
  28. beginning of the data portion of the chunk. If the chunk is a
  29. "RIFF" chunk or a "LIST" chunk, then <e MMCKINFO.dwDataOffset>
  30. is the offset of the form type or list type.
  31. -- The <e MMCKINFO.dwFlags> contains other information about the chunk.
  32. Currently, this information is not used and is set to zero.
  33. If the MMIO_FINDCHUNK, MMIO_FINDRIFF, or MMIO_FINDLIST flag is
  34. specified for <p wFlags>, then the <t MMCKINFO> structure is also
  35. used to pass parameters to <f mmioDescend>:
  36. -- The <e MMCKINFO.ckid> field specifies the four-character code
  37. of the chunk ID, form type, or list type to search for.
  38. @parm LPMMCKINFO | lpckParent | Specifies a far pointer to an
  39. optional caller-supplied <t MMCKINFO> structure identifying
  40. the parent of the chunk being searched for.
  41. A parent of a chunk is the enclosing chunk--only "RIFF" and "LIST"
  42. chunks can be parents. If <p lpckParent> is not NULL, then
  43. <f mmioDescend> assumes the <t MMCKINFO> structure it refers to
  44. was filled when <f mmioDescend> was called to descend into the parent
  45. chunk, and <f mmioDescend> will only search for a chunk within the
  46. parent chunk. Set <p lpckParent> to NULL if no parent chunk is
  47. being specified.
  48. @parm UINT | wFlags | Specifies search options. Contains up to one
  49. of the following flags. If no flags are specified,
  50. <f mmioDescend> descends into the chunk beginning at the current file
  51. position.
  52. @flag MMIO_FINDCHUNK | Searches for a chunk with the specified chunk ID.
  53. @flag MMIO_FINDRIFF | Searches for a chunk with chunk ID "RIFF"
  54. and with the specified form type.
  55. @flag MMIO_FINDLIST | Searches for a chunk with chunk ID "LIST"
  56. and with the specified form type.
  57. @rdesc The return value is zero if the function is successful.
  58. Otherwise, the return value specifies an error code. If the end of
  59. the file (or the end of the parent chunk, if given) is reached before
  60. the desired chunk is found, the return value is
  61. MMIOERR_CHUNKNOTFOUND.
  62. @comm A RIFF chunk consists of a four-byte chunk ID (type FOURCC),
  63. followed by a four-byte chunk size (type DWORD), followed
  64. by the data portion of the chunk, followed by a null pad byte if
  65. the size of the data portion is odd. If the chunk ID is "RIFF" or
  66. "LIST", the first four bytes of the data portion of the chunk are
  67. a form type or list type (type FOURCC).
  68. If <f mmioDescend> is used to search for a chunk, the file
  69. position should be at the beginning of a
  70. chunk before calling <f mmioDescend>. The search begins at the
  71. current file position and continues to the end of the file. If a
  72. parent chunk is specified, the file position should be somewhere
  73. within the parent chunk before calling <f mmioDescend>. In this case,
  74. the search begins at the current file position and continues to the
  75. end of the parent chunk.
  76. If <f mmioDescend> is unsuccessful in searching for a chunk, the
  77. current file position is undefined. If <f mmioDescend> is
  78. successful, the current file position is changed. If the chunk
  79. is a "RIFF" or "LIST" chunk, the new file position
  80. will be just after the form type or list type (12 bytes from the
  81. beginning of the chunk). For other chunks, the new file position will be
  82. the start of the data portion of the chunk (8 bytes from the
  83. beginning of the chunk).
  84. For efficient RIFF file I/O, use buffered I/O.
  85. @xref mmioAscend MMCKINFO
  86. */
  87. UINT WINAPI
  88. mmioDescend(HMMIO hmmio, LPMMCKINFO lpck, const MMCKINFO FAR* lpckParent, UINT wFlags)
  89. {
  90. FOURCC ckidFind; // chunk ID to find (or NULL)
  91. FOURCC fccTypeFind; // form/list type to find (or NULL)
  92. V_FLAGS(wFlags, MMIO_DESCEND_VALID, mmioDescend, MMSYSERR_INVALFLAG);
  93. V_WPOINTER(lpck, sizeof(MMCKINFO), MMSYSERR_INVALPARAM);
  94. V_RPOINTER0(lpckParent, sizeof(MMCKINFO), MMSYSERR_INVALPARAM);
  95. /* figure out what chunk id and form/list type to search for */
  96. if (wFlags & MMIO_FINDCHUNK)
  97. ckidFind = lpck->ckid, fccTypeFind = NULL;
  98. else
  99. if (wFlags & MMIO_FINDRIFF)
  100. ckidFind = FOURCC_RIFF, fccTypeFind = lpck->fccType;
  101. else
  102. if (wFlags & MMIO_FINDLIST)
  103. ckidFind = FOURCC_LIST, fccTypeFind = lpck->fccType;
  104. else
  105. ckidFind = fccTypeFind = NULL;
  106. lpck->dwFlags = 0L;
  107. while (TRUE)
  108. {
  109. UINT w;
  110. /* read the chunk header */
  111. if (mmioRead(hmmio, (HPSTR) lpck, 2 * sizeof(DWORD)) !=
  112. 2 * sizeof(DWORD))
  113. return MMIOERR_CHUNKNOTFOUND;
  114. /* store the offset of the data part of the chunk */
  115. if ((lpck->dwDataOffset = mmioSeek(hmmio, 0L, SEEK_CUR)) == -1)
  116. return MMIOERR_CANNOTSEEK;
  117. /* see if the chunk is within the parent chunk (if given) */
  118. if ((lpckParent != NULL) &&
  119. (lpck->dwDataOffset - 8L >=
  120. lpckParent->dwDataOffset + lpckParent->cksize))
  121. return MMIOERR_CHUNKNOTFOUND;
  122. /* if the chunk if a 'RIFF' or 'LIST' chunk, read the
  123. * form type or list type
  124. */
  125. if ((lpck->ckid == FOURCC_RIFF) || (lpck->ckid == FOURCC_LIST))
  126. {
  127. if (mmioRead(hmmio, (HPSTR) &lpck->fccType,
  128. sizeof(DWORD)) != sizeof(DWORD))
  129. return MMIOERR_CHUNKNOTFOUND;
  130. }
  131. else
  132. lpck->fccType = NULL;
  133. /* if this is the chunk we're looking for, stop looking */
  134. if ( ((ckidFind == NULL) || (ckidFind == lpck->ckid)) &&
  135. ((fccTypeFind == NULL) || (fccTypeFind == lpck->fccType)) )
  136. break;
  137. /* ascend out of the chunk and try again */
  138. if ((w = mmioAscend(hmmio, lpck, 0)) != 0)
  139. return w;
  140. }
  141. return 0;
  142. }
  143. /* @doc EXTERNAL
  144. @api UINT | mmioAscend | This function ascends out of a chunk in a
  145. RIFF file descended into with <f mmioDescend> or created with
  146. <f mmioCreateChunk>.
  147. @parm HMMIO | hmmio | Specifies the file handle of an open RIFF file.
  148. @parm LPMMCKINFO | lpck | Specifies a far pointer to a
  149. caller-supplied <t MMCKINFO> structure previously filled by
  150. <f mmioDescend> or <f mmioCreateChunk>.
  151. @parm UINT | wFlags | Is not used and should be set to zero.
  152. @rdesc The return value is zero if the function is successful.
  153. Otherwise, the return value specifies an error code. The error
  154. code can be one of the following codes:
  155. @flag MMIOERR_CANNOTWRITE | The contents of the buffer could
  156. not be written to disk.
  157. @flag MMIOERR_CANNOTSEEK | There was an error while seeking to
  158. the end of the chunk.
  159. @comm If the chunk was descended into using <f mmioDescend>, then
  160. <f mmioAscend> seeks to the location following the end of the
  161. chunk (past the extra pad byte, if any).
  162. If the chunk was created and descended into using
  163. <f mmioCreateChunk>, or if the MMIO_DIRTY flag is set in the
  164. <e MMCKINFO.dwFlags> field of the <t MMCKINFO> structure
  165. referenced by <p lpck>, then the current file position
  166. is assumed to be the end of the data portion of the chunk.
  167. If the chunk size is not the same as the value stored
  168. in the <e MMCKINFO.cksize> field when <f mmioCreateChunk>
  169. was called, then <f mmioAscend> corrects the chunk
  170. size in the file before ascending from the chunk. If the chunk
  171. size is odd, <f mmioAscend> writes a null pad byte at the end of the
  172. chunk. After ascending from the chunk, the current file position is
  173. the location following the end of the chunk (past the extra pad byte,
  174. if any).
  175. @xref mmioDescend mmioCreateChunk MMCKINFO
  176. */
  177. UINT WINAPI
  178. mmioAscend(HMMIO hmmio, LPMMCKINFO lpck, UINT wFlags)
  179. {
  180. V_FLAGS(wFlags, 0, mmioAscend, MMSYSERR_INVALFLAG);
  181. V_WPOINTER(lpck, sizeof(MMCKINFO), MMSYSERR_INVALPARAM);
  182. if (lpck->dwFlags & MMIO_DIRTY)
  183. {
  184. /* <lpck> refers to a chunk created by mmioCreateChunk();
  185. * check that the chunk size that was written when
  186. * mmioCreateChunk() was called is the real chunk size;
  187. * if not, fix it
  188. */
  189. LONG lOffset; // current offset in file
  190. LONG lActualSize; // actual size of chunk data
  191. if ((lOffset = mmioSeek(hmmio, 0L, SEEK_CUR)) == -1)
  192. return MMIOERR_CANNOTSEEK;
  193. if ((lActualSize = lOffset - lpck->dwDataOffset) < 0)
  194. return MMIOERR_CANNOTWRITE;
  195. if (LOWORD(lActualSize) & 1)
  196. {
  197. /* chunk size is odd -- write a null pad byte */
  198. if (mmioWrite(hmmio, (HPSTR) &bPad, sizeof(bPad))
  199. != sizeof(bPad))
  200. return MMIOERR_CANNOTWRITE;
  201. }
  202. if (lpck->cksize == (DWORD)lActualSize)
  203. return 0;
  204. /* fix the chunk header */
  205. lpck->cksize = lActualSize;
  206. if (mmioSeek(hmmio, lpck->dwDataOffset
  207. - sizeof(DWORD), SEEK_SET) == -1)
  208. return MMIOERR_CANNOTSEEK;
  209. if (mmioWrite(hmmio, (HPSTR) &lpck->cksize,
  210. sizeof(DWORD)) != sizeof(DWORD))
  211. return MMIOERR_CANNOTWRITE;
  212. }
  213. /* seek to the end of the chunk, past the null pad byte
  214. * (which is only there if chunk size is odd)
  215. */
  216. if (mmioSeek(hmmio, lpck->dwDataOffset + lpck->cksize
  217. + (lpck->cksize & 1L), SEEK_SET) == -1)
  218. return MMIOERR_CANNOTSEEK;
  219. return 0;
  220. }
  221. /* @doc EXTERNAL
  222. @api UINT | mmioCreateChunk | This function creates a chunk in a
  223. RIFF file opened with <f mmioOpen>. The new chunk is created at the
  224. current file position. After the new chunk is created, the current
  225. file position is the beginning of the data portion of the new chunk.
  226. @parm HMMIO | hmmio | Specifies the file handle of an open RIFF
  227. file.
  228. @parm LPMMCKINFO | lpck | Specifies a pointer to a caller-supplied
  229. <t MMCKINFO> structure containing information about the chunk to be
  230. created. The <t MMCKINFO> structure should be set up as follows:
  231. -- The <e MMCKINFO.ckid> field specifies the chunk ID of the
  232. chunk. If <p wFlags> includes MMIO_CREATERIFF or MMIO_CREATELIST,
  233. this field will be filled by <f mmioCreateChunk>.
  234. -- The <e MMCKINFO.cksize> field specifies the size of the data
  235. portion of the chunk, including the form type or list type (if any).
  236. If this value is not correct when <f mmioAscend> is called to mark
  237. the end of the chunk, them <f mmioAscend> will correct the chunk
  238. size.
  239. -- The <e MMCKINFO.fccType> field specifies the form type or list
  240. type if the chunk is a "RIFF" or "LIST" chunk. If the chunk is not a
  241. "RIFF" or "LIST" chunk, this field need not be filled in.
  242. -- The <e MMCKINFO.dwDataOffset> field need not be filled in. The
  243. <f mmioCreateChunk> function will fill this field with the file
  244. offset of the data portion of the chunk.
  245. -- The <e MMCKINFO.dwFlags> field need not be filled in. The
  246. <f mmioCreateChunk> function will set the MMIO_DIRTY flag in
  247. <e MMCKINFO.dwFlags>.
  248. @parm UINT | wFlags | Specifies flags to optionally create either a
  249. "RIFF" chunk or a "LIST" chunk. Can contain one of the following
  250. flags:
  251. @flag MMIO_CREATERIFF | Creates a "RIFF" chunk.
  252. @flag MMIO_CREATELIST | Creates a "LIST" chunk.
  253. @rdesc The return value is zero if the function is successful.
  254. Otherwise, the return value specifies an error code. The error
  255. code can be one of the following codes:
  256. @flag MMIOERR_CANNOTWRITE | Unable to write the chunk header.
  257. @flag MMIOERR_CANNOTSEEK | Uanble to determine offset of data
  258. portion of the chunk.
  259. @comm This function cannot insert a chunk into the middle of a
  260. file. If a chunk is created anywhere but the end of a file,
  261. <f mmioCreateChunk> will overwrite existing information in the file.
  262. */
  263. UINT WINAPI
  264. mmioCreateChunk(HMMIO hmmio, LPMMCKINFO lpck, UINT wFlags)
  265. {
  266. int iBytes; // bytes to write
  267. LONG lOffset; // current offset in file
  268. V_FLAGS(wFlags, MMIO_CREATE_VALID, mmioCreateChunk, MMSYSERR_INVALFLAG);
  269. V_WPOINTER(lpck, sizeof(MMCKINFO), MMSYSERR_INVALPARAM);
  270. /* store the offset of the data part of the chunk */
  271. if ((lOffset = mmioSeek(hmmio, 0L, SEEK_CUR)) == -1)
  272. return MMIOERR_CANNOTSEEK;
  273. lpck->dwDataOffset = lOffset + 2 * sizeof(DWORD);
  274. /* figure out if a form/list type needs to be written */
  275. if (wFlags & MMIO_CREATERIFF)
  276. lpck->ckid = FOURCC_RIFF, iBytes = 3 * sizeof(DWORD);
  277. else
  278. if (wFlags & MMIO_CREATELIST)
  279. lpck->ckid = FOURCC_LIST, iBytes = 3 * sizeof(DWORD);
  280. else
  281. iBytes = 2 * sizeof(DWORD);
  282. /* write the chunk header */
  283. if (mmioWrite(hmmio, (HPSTR) lpck, (LONG) iBytes) != (LONG) iBytes)
  284. return MMIOERR_CANNOTWRITE;
  285. lpck->dwFlags = MMIO_DIRTY;
  286. return 0;
  287. }