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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "extrachunk.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(avifile
);
31 /* reads a chunk out of the extrachunk-structure */
32 HRESULT
ReadExtraChunk(const EXTRACHUNKS
*extra
,FOURCC ckid
,LPVOID lpData
,LPLONG size
)
38 assert(extra
!= NULL
);
46 if (((FOURCC
*)lp
)[0] == ckid
) {
47 /* found correct chunk */
48 if (lpData
!= NULL
&& *size
> 0)
49 memcpy(lpData
, lp
+ 2 * sizeof(DWORD
),
50 min(((LPDWORD
)lp
)[1], *(LPDWORD
)size
));
52 *(LPDWORD
)size
= ((LPDWORD
)lp
)[1];
56 /* skip to next chunk */
57 cb
-= ((LPDWORD
)lp
)[1] + 2 * sizeof(DWORD
);
58 lp
+= ((LPDWORD
)lp
)[1] + 2 * sizeof(DWORD
);
63 /* wanted chunk doesn't exist */
69 /* writes a chunk into the extrachunk-structure */
70 HRESULT
WriteExtraChunk(LPEXTRACHUNKS extra
,FOURCC ckid
,LPCVOID lpData
, LONG size
)
75 assert(extra
!= NULL
);
76 assert(lpData
!= NULL
);
79 lp
= _recalloc(extra
->lp
, 1, extra
->cb
+ size
+ 2 * sizeof(DWORD
));
84 lp
= (LPDWORD
) ((LPBYTE
)lp
+ extra
->cb
);
85 extra
->cb
+= size
+ 2 * sizeof(DWORD
);
87 /* insert chunk-header in block */
91 if (lpData
!= NULL
&& size
> 0)
92 memcpy(lp
+ 2, lpData
, size
);
97 /* reads a chunk from the HMMIO into the extrachunk-structure */
98 HRESULT
ReadChunkIntoExtra(LPEXTRACHUNKS extra
,HMMIO hmmio
,const MMCKINFO
*lpck
)
104 assert(extra
!= NULL
);
105 assert(hmmio
!= NULL
);
106 assert(lpck
!= NULL
);
108 cb
= lpck
->cksize
+ 2 * sizeof(DWORD
);
111 lp
= _recalloc(extra
->lp
, 1, extra
->cb
+ cb
);
113 return AVIERR_MEMORY
;
116 lp
= (LPDWORD
) ((LPBYTE
)lp
+ extra
->cb
);
119 /* insert chunk-header in block */
121 lp
[1] = lpck
->cksize
;
123 if (lpck
->cksize
> 0) {
124 if (mmioSeek(hmmio
, lpck
->dwDataOffset
, SEEK_SET
) == -1)
125 return AVIERR_FILEREAD
;
126 if (mmioRead(hmmio
, (HPSTR
)&lp
[2], lpck
->cksize
) != (LONG
)lpck
->cksize
)
127 return AVIERR_FILEREAD
;
133 /* reads all non-junk chunks into the extrachunk-structure until it finds
134 * the given chunk or the optional parent-chunk is at the end */
135 HRESULT
FindChunkAndKeepExtras(LPEXTRACHUNKS extra
,HMMIO hmmio
,MMCKINFO
*lpck
,
136 MMCKINFO
*lpckParent
,UINT flags
)
143 assert(extra
!= NULL
);
144 assert(hmmio
!= NULL
);
145 assert(lpck
!= NULL
);
147 TRACE("({%p,%lu},%p,%p,%p,0x%X)\n", extra
->lp
, extra
->cb
, hmmio
, lpck
,
150 /* what chunk id and form/list type should we search? */
151 if (flags
& MMIO_FINDCHUNK
) {
154 } else if (flags
& MMIO_FINDLIST
) {
156 fccType
= lpck
->fccType
;
157 } else if (flags
& MMIO_FINDRIFF
) {
159 fccType
= lpck
->fccType
;
161 ckid
= fccType
= (FOURCC
)-1; /* collect everything into extra! */
163 TRACE(": find ckid=0x%08lX fccType=0x%08lX\n", ckid
, fccType
);
166 mmr
= mmioDescend(hmmio
, lpck
, lpckParent
, 0);
167 if (mmr
!= MMSYSERR_NOERROR
) {
168 /* No extra chunks in front of desired chunk? */
169 if (flags
== 0 && mmr
== MMIOERR_CHUNKNOTFOUND
)
172 return AVIERR_FILEREAD
;
175 /* Have we found what we search for? */
176 if ((lpck
->ckid
== ckid
) &&
177 (fccType
== 0 || lpck
->fccType
== fccType
))
180 /* Skip padding chunks, the others put into the extrachunk-structure */
181 if (lpck
->ckid
== ckidAVIPADDING
||
182 lpck
->ckid
== mmioFOURCC('p','a','d','d'))
184 mmr
= mmioAscend(hmmio
, lpck
, 0);
185 if (mmr
!= MMSYSERR_NOERROR
) return AVIERR_FILEREAD
;
189 HRESULT hr
= ReadChunkIntoExtra(extra
, hmmio
, lpck
);