Implemented CPSignHash and CPVerifySignature.
[wine] / dlls / quartz / enumfilters.c
1 /*
2  * Implementation of IEnumFilters Interface
3  *
4  * Copyright 2004 Christian Costa
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "quartz_private.h"
22
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
26
27 typedef struct IEnumFiltersImpl
28 {
29     const IEnumFiltersVtbl * lpVtbl;
30     ULONG refCount;
31     IBaseFilter ** ppFilters;
32     int nFilters;
33     ULONG uIndex;
34 } IEnumFiltersImpl;
35
36 static const struct IEnumFiltersVtbl IEnumFiltersImpl_Vtbl;
37
38 HRESULT IEnumFiltersImpl_Construct(IBaseFilter ** ppFilters, ULONG nFilters, IEnumFilters ** ppEnum)
39 {
40     /* Note: The incoming IBaseFilter interfaces are not AddRef'd here as in Windows,
41      * they should have been previously AddRef'd. */
42     IEnumFiltersImpl * pEnumFilters = CoTaskMemAlloc(sizeof(IEnumFiltersImpl));
43
44     TRACE("(%p, %ld, %p)\n", ppFilters, nFilters, ppEnum);
45
46     *ppEnum = NULL;
47
48     if (!pEnumFilters)
49     {
50         return E_OUTOFMEMORY;
51     }
52
53     pEnumFilters->lpVtbl = &IEnumFiltersImpl_Vtbl;
54     pEnumFilters->refCount = 1;
55     pEnumFilters->uIndex = 0;
56     pEnumFilters->nFilters = nFilters;
57     pEnumFilters->ppFilters = CoTaskMemAlloc(sizeof(IBaseFilter*) * nFilters);
58     if (!pEnumFilters->ppFilters)
59     {
60         CoTaskMemFree(pEnumFilters);
61         return E_OUTOFMEMORY;
62     }
63
64     memcpy(pEnumFilters->ppFilters, ppFilters, nFilters * sizeof(IBaseFilter*));
65
66     *ppEnum = (IEnumFilters *)(&pEnumFilters->lpVtbl);
67     return S_OK;
68 }
69
70 static HRESULT WINAPI IEnumFiltersImpl_QueryInterface(IEnumFilters * iface, REFIID riid, LPVOID * ppv)
71 {
72     TRACE("(%p)->(%s, %p)\n", iface, qzdebugstr_guid(riid), ppv);
73
74     *ppv = NULL;
75
76     if (IsEqualIID(riid, &IID_IUnknown))
77         *ppv = (LPVOID)iface;
78     else if (IsEqualIID(riid, &IID_IEnumFilters))
79         *ppv = (LPVOID)iface;
80
81     if (*ppv)
82     {
83         IUnknown_AddRef((IUnknown *)(*ppv));
84         return S_OK;
85     }
86
87     FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
88
89     return E_NOINTERFACE;
90 }
91
92 static ULONG WINAPI IEnumFiltersImpl_AddRef(IEnumFilters * iface)
93 {
94     IEnumFiltersImpl *This = (IEnumFiltersImpl *)iface;
95     TRACE("(%p)->()\n", iface);
96     return ++This->refCount;
97 }
98
99 static ULONG WINAPI IEnumFiltersImpl_Release(IEnumFilters * iface)
100 {
101     IEnumFiltersImpl *This = (IEnumFiltersImpl *)iface;
102     TRACE("(%p)->()\n", iface);
103     if (!--This->refCount)
104     {
105         CoTaskMemFree(This->ppFilters);
106         CoTaskMemFree(This);
107         return 0;
108     }
109     else
110         return This->refCount;
111 }
112
113 static HRESULT WINAPI IEnumFiltersImpl_Next(IEnumFilters * iface, ULONG cFilters, IBaseFilter ** ppFilters, ULONG * pcFetched)
114 {
115     ULONG cFetched; 
116     ULONG i;
117     IEnumFiltersImpl *This = (IEnumFiltersImpl *)iface;
118
119     cFetched = min(This->nFilters, This->uIndex + cFilters) - This->uIndex;
120
121     TRACE("(%p)->(%lu, %p, %p)\n", iface, cFilters, ppFilters, pcFetched);
122
123     for (i = 0; i < cFetched; i++)
124     {
125         ppFilters[i] = This->ppFilters[This->uIndex + i];
126         IBaseFilter_AddRef(ppFilters[i]);
127     }
128
129     This->uIndex += cFetched;
130
131     if (pcFetched)
132         *pcFetched = cFetched;
133
134     if (cFetched != cFilters)
135         return S_FALSE;
136     return S_OK;
137 }
138
139 static HRESULT WINAPI IEnumFiltersImpl_Skip(IEnumFilters * iface, ULONG cFilters)
140 {
141     IEnumFiltersImpl *This = (IEnumFiltersImpl *)iface;
142
143     TRACE("(%p)->(%lu)\n", iface, cFilters);
144
145     if (This->uIndex + cFilters < This->nFilters)
146     {
147         This->uIndex += cFilters;
148         return S_OK;
149     }
150     return S_FALSE;
151 }
152
153 static HRESULT WINAPI IEnumFiltersImpl_Reset(IEnumFilters * iface)
154 {
155     IEnumFiltersImpl *This = (IEnumFiltersImpl *)iface;
156
157     TRACE("(%p)->()\n", iface);
158
159     This->uIndex = 0;
160     return S_OK;
161 }
162
163 static HRESULT WINAPI IEnumFiltersImpl_Clone(IEnumFilters * iface, IEnumFilters ** ppEnum)
164 {
165     HRESULT hr;
166     IEnumFiltersImpl *This = (IEnumFiltersImpl *)iface;
167
168     TRACE("(%p)->(%p)\n", iface, ppEnum);
169
170     hr = IEnumFiltersImpl_Construct(This->ppFilters, This->nFilters, ppEnum);
171     if (FAILED(hr))
172         return hr;
173     return IEnumFilters_Skip(*ppEnum, This->uIndex);
174 }
175
176 static const IEnumFiltersVtbl IEnumFiltersImpl_Vtbl =
177 {
178     IEnumFiltersImpl_QueryInterface,
179     IEnumFiltersImpl_AddRef,
180     IEnumFiltersImpl_Release,
181     IEnumFiltersImpl_Next,
182     IEnumFiltersImpl_Skip,
183     IEnumFiltersImpl_Reset,
184     IEnumFiltersImpl_Clone
185 };