2 * Copyright 2002 Michael Günnewig
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define COM_NO_WINDOWS_H
22 #include "extrachunk.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(avifile);
32 /* reads a chunk outof the extrachunk-structure */
33 HRESULT ReadExtraChunk(LPEXTRACHUNKS extra,FOURCC ckid,LPVOID lpData,
40 assert(extra != NULL);
48 if (((FOURCC*)lp)[0] == ckid) {
49 /* found correct chunk */
50 if (lpData != NULL && *size > 0)
51 memcpy(lpData, lp + 2 * sizeof(DWORD),
52 min(((LPDWORD)lp)[1], *(LPDWORD)size));
54 *(LPDWORD)size = ((LPDWORD)lp)[1];
58 /* skip to next chunk */
59 cb -= ((LPDWORD)lp)[1] + 2 * sizeof(DWORD);
60 lp += ((LPDWORD)lp)[1] + 2 * sizeof(DWORD);
65 /* wanted chunk doesn't exist */
71 /* writes a chunk into the extrachunk-structure */
72 HRESULT WriteExtraChunk(LPEXTRACHUNKS extra,FOURCC ckid,LPVOID lpData,
78 assert(extra != NULL);
79 assert(lpData != NULL);
83 lp = HeapReAlloc(GetProcessHeap(), 0, extra->lp, extra->cb + size + 2 * sizeof(DWORD));
85 lp = HeapAlloc(GetProcessHeap(), 0, size + 2 * sizeof(DWORD));
91 lp = (LPDWORD) ((LPBYTE)lp + extra->cb);
92 extra->cb += size + 2 * sizeof(DWORD);
94 /* insert chunk-header in block */
98 if (lpData != NULL && size > 0)
99 memcpy(lp + 2, lpData, size);
104 /* reads a chunk fomr the HMMIO into the extrachunk-structure */
105 HRESULT ReadChunkIntoExtra(LPEXTRACHUNKS extra,HMMIO hmmio,MMCKINFO *lpck)
111 assert(extra != NULL);
112 assert(hmmio != NULL);
113 assert(lpck != NULL);
115 cb = lpck->cksize + 2 * sizeof(DWORD);
118 if (extra->lp != NULL)
119 lp = HeapReAlloc(GetProcessHeap(), 0, extra->lp, extra->cb + cb);
121 lp = HeapAlloc(GetProcessHeap(), 0, cb);
124 return AVIERR_MEMORY;
127 lp = (LPDWORD) ((LPBYTE)lp + extra->cb);
130 /* insert chunk-header in block */
132 lp[1] = lpck->cksize;
134 if (lpck->cksize > 0) {
135 if (mmioSeek(hmmio, lpck->dwDataOffset, SEEK_SET) == -1)
136 return AVIERR_FILEREAD;
137 if (mmioRead(hmmio, (HPSTR)&lp[2], lpck->cksize) != (LONG)lpck->cksize)
138 return AVIERR_FILEREAD;
144 /* reads all non-junk chunks into the extrachunk-structure until it finds
145 * the given chunk or the optional parent-chunk is at the end */
146 HRESULT FindChunkAndKeepExtras(LPEXTRACHUNKS extra,HMMIO hmmio,MMCKINFO *lpck,
147 MMCKINFO *lpckParent,UINT flags)
154 assert(extra != NULL);
155 assert(hmmio != NULL);
156 assert(lpck != NULL);
158 TRACE("({%p,%lu},%p,%p,%p,0x%X)\n", extra->lp, extra->cb, hmmio, lpck,
161 /* what chunk id and form/list type should we search? */
162 if (flags & MMIO_FINDCHUNK) {
165 } else if (flags & MMIO_FINDLIST) {
167 fccType = lpck->fccType;
168 } else if (flags & MMIO_FINDRIFF) {
170 fccType = lpck->fccType;
172 ckid = fccType = (FOURCC)-1; /* collect everything into extra! */
174 TRACE(": find ckid=0x%08lX fccType=0x%08lX\n", ckid, fccType);
177 hr = mmioDescend(hmmio, lpck, lpckParent, 0);
179 /* No extra chunks in front of desired chunk? */
180 if (flags == 0 && hr == MMIOERR_CHUNKNOTFOUND)
185 /* Have we found what we search for? */
186 if ((lpck->ckid == ckid) &&
187 (fccType == (FOURCC)0 || lpck->fccType == fccType))
190 /* Skip padding chunks, the others put into the extrachunk-structure */
191 if (lpck->ckid == ckidAVIPADDING ||
192 lpck->ckid == mmioFOURCC('p','a','d','d'))
193 hr = mmioAscend(hmmio, lpck, 0);
195 hr = ReadChunkIntoExtra(extra, hmmio, lpck);