Added regedit unit test, a couple minor changes to regedit.
[wine] / dlls / shlwapi / regstream.c
1 /*
2  *      SHRegOpenStream
3  *
4  * Copyright 1999 Juergen Schmied
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 <string.h>
22
23 #include "winerror.h"
24 #include "winbase.h"
25 #include "winreg.h"
26 #include "shlobj.h"
27
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(shell);
31
32 typedef struct
33 {       ICOM_VFIELD(IStream);
34         DWORD           ref;
35         HKEY            hKey;
36         LPBYTE          pbBuffer;
37         DWORD           dwLength;
38         DWORD           dwPos;
39 } ISHRegStream;
40
41 static struct ICOM_VTABLE(IStream) rstvt;
42
43 /**************************************************************************
44 *   IStream_ConstructorA        [internal]
45 */
46 static IStream *IStream_ConstructorA(HKEY hKey, LPCSTR pszSubKey, LPCSTR pszValue, DWORD grfMode)
47 {
48         ISHRegStream*   rstr;
49         DWORD           dwType;
50
51         rstr = (ISHRegStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHRegStream));
52
53         ICOM_VTBL(rstr)=&rstvt;
54         rstr->ref = 1;
55
56         if (!(RegOpenKeyExA (hKey, pszSubKey, 0, KEY_READ, &(rstr->hKey))))
57         {
58           if (!(RegQueryValueExA(rstr->hKey, pszValue,0,0,0,&(rstr->dwLength))))
59           {
60             /* read the binary data into the buffer */
61             if((rstr->pbBuffer = HeapAlloc(GetProcessHeap(),0,rstr->dwLength)))
62             {
63               if (!(RegQueryValueExA(rstr->hKey, pszValue,0,&dwType,rstr->pbBuffer,&(rstr->dwLength))))
64               {
65                 if (dwType == REG_BINARY )
66                 {
67                   TRACE ("%p\n", rstr);
68                   return (IStream*)rstr;
69                 }
70               }
71               HeapFree (GetProcessHeap(),0,rstr->pbBuffer);
72             }
73           }
74           RegCloseKey(rstr->hKey);
75         }
76         HeapFree (GetProcessHeap(),0,rstr);
77         return NULL;
78 }
79
80 /**************************************************************************
81 *   IStream_ConstructorW        [internal]
82 */
83 static IStream *IStream_ConstructorW(HKEY hKey, LPCWSTR pszSubKey, LPCWSTR pszValue, DWORD grfMode)
84 {
85         ISHRegStream*   rstr;
86         DWORD           dwType;
87
88         rstr = (ISHRegStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHRegStream));
89
90         ICOM_VTBL(rstr)=&rstvt;
91         rstr->ref = 1;
92
93         if (!(RegOpenKeyExW (hKey, pszSubKey, 0, KEY_READ, &(rstr->hKey))))
94         {
95           if (!(RegQueryValueExW(rstr->hKey, pszValue,0,0,0,&(rstr->dwLength))))
96           {
97             /* read the binary data into the buffer */
98             if((rstr->pbBuffer = HeapAlloc(GetProcessHeap(),0,rstr->dwLength)))
99             {
100               if (!(RegQueryValueExW(rstr->hKey, pszValue,0,&dwType,rstr->pbBuffer,&(rstr->dwLength))))
101               {
102                 if (dwType == REG_BINARY )
103                 {
104                   TRACE ("%p\n", rstr);
105                   return (IStream*)rstr;
106                 }
107               }
108               HeapFree (GetProcessHeap(),0,rstr->pbBuffer);
109             }
110           }
111           RegCloseKey(rstr->hKey);
112         }
113         HeapFree (GetProcessHeap(),0,rstr);
114         return NULL;
115 }
116
117 /**************************************************************************
118 *  IStream_fnQueryInterface
119 */
120 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
121 {
122         ICOM_THIS(ISHRegStream, iface);
123
124         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
125
126         *ppvObj = NULL;
127
128         if(IsEqualIID(riid, &IID_IUnknown))     /*IUnknown*/
129         { *ppvObj = This;
130         }
131         else if(IsEqualIID(riid, &IID_IStream)) /*IStream*/
132         { *ppvObj = This;
133         }
134
135         if(*ppvObj)
136         {
137           IStream_AddRef((IStream*)*ppvObj);
138           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
139           return S_OK;
140         }
141         TRACE("-- Interface: E_NOINTERFACE\n");
142         return E_NOINTERFACE;
143 }
144
145 /**************************************************************************
146 *  IStream_fnAddRef
147 */
148 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
149 {
150         ICOM_THIS(ISHRegStream, iface);
151
152         TRACE("(%p)->(count=%lu)\n",This, This->ref);
153
154         return ++(This->ref);
155 }
156
157 /**************************************************************************
158 *  IStream_fnRelease
159 */
160 static ULONG WINAPI IStream_fnRelease(IStream *iface)
161 {
162         ICOM_THIS(ISHRegStream, iface);
163
164         TRACE("(%p)->()\n",This);
165
166         if (!--(This->ref))
167         { TRACE(" destroying SHReg IStream (%p)\n",This);
168
169           if (This->pbBuffer)
170             HeapFree(GetProcessHeap(),0,This->pbBuffer);
171
172           if (This->hKey)
173             RegCloseKey(This->hKey);
174
175           HeapFree(GetProcessHeap(),0,This);
176           return 0;
177         }
178         return This->ref;
179 }
180
181 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
182 {
183         ICOM_THIS(ISHRegStream, iface);
184
185         DWORD dwBytesToRead, dwBytesLeft;
186
187         TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
188
189         if ( !pv )
190           return STG_E_INVALIDPOINTER;
191
192         dwBytesLeft = This->dwLength - This->dwPos;
193
194         if ( 0 >= dwBytesLeft )                                         /* end of buffer */
195           return S_FALSE;
196
197         dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
198
199         memmove ( pv, (This->pbBuffer) + (This->dwPos), dwBytesToRead);
200
201         This->dwPos += dwBytesToRead;                                   /* adjust pointer */
202
203         if (pcbRead)
204           *pcbRead = dwBytesToRead;
205
206         return S_OK;
207 }
208 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
209 {
210         ICOM_THIS(ISHRegStream, iface);
211
212         TRACE("(%p)\n",This);
213
214         return E_NOTIMPL;
215 }
216 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
217 {
218         ICOM_THIS(ISHRegStream, iface);
219
220         TRACE("(%p)\n",This);
221
222         return E_NOTIMPL;
223 }
224 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
225 {
226         ICOM_THIS(ISHRegStream, iface);
227
228         TRACE("(%p)\n",This);
229
230         return E_NOTIMPL;
231 }
232 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
233 {
234         ICOM_THIS(ISHRegStream, iface);
235
236         TRACE("(%p)\n",This);
237
238         return E_NOTIMPL;
239 }
240 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
241 {
242         ICOM_THIS(ISHRegStream, iface);
243
244         TRACE("(%p)\n",This);
245
246         return E_NOTIMPL;
247 }
248 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
249 {
250         ICOM_THIS(ISHRegStream, iface);
251
252         TRACE("(%p)\n",This);
253
254         return E_NOTIMPL;
255 }
256 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
257 {
258         ICOM_THIS(ISHRegStream, iface);
259
260         TRACE("(%p)\n",This);
261
262         return E_NOTIMPL;
263 }
264 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
265 {
266         ICOM_THIS(ISHRegStream, iface);
267
268         TRACE("(%p)\n",This);
269
270         return E_NOTIMPL;
271 }
272 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag)
273 {
274         ICOM_THIS(ISHRegStream, iface);
275
276         TRACE("(%p)\n",This);
277
278         return E_NOTIMPL;
279 }
280 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
281 {
282         ICOM_THIS(ISHRegStream, iface);
283
284         TRACE("(%p)\n",This);
285
286         return E_NOTIMPL;
287 }
288
289 static struct ICOM_VTABLE(IStream) rstvt =
290 {
291         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
292         IStream_fnQueryInterface,
293         IStream_fnAddRef,
294         IStream_fnRelease,
295         IStream_fnRead,
296         IStream_fnWrite,
297         IStream_fnSeek,
298         IStream_fnSetSize,
299         IStream_fnCopyTo,
300         IStream_fnCommit,
301         IStream_fnRevert,
302         IStream_fnLockRegion,
303         IStream_fnUnlockRegion,
304         IStream_fnStat,
305         IStream_fnClone
306
307 };
308
309 /*************************************************************************
310  * SHOpenRegStreamA                             [SHLWAPI.@]
311  * SHOpenRegStream2A                            [SHLWAPI.@]
312  */
313 IStream * WINAPI SHOpenRegStreamA(
314         HKEY hkey,
315         LPCSTR pszSubkey,
316         LPCSTR pszValue,
317         DWORD grfMode)
318 {
319         TRACE("(0x%08x,%s,%s,0x%08lx)\n",
320         hkey, pszSubkey, pszValue, grfMode);
321
322         return IStream_ConstructorA(hkey, pszSubkey, pszValue, grfMode);
323 }
324
325 /*************************************************************************
326  * SHOpenRegStreamW                             [SHLWAPI.@]
327  * SHOpenRegStream2W                            [SHLWAPI.@]
328  */
329 IStream * WINAPI SHOpenRegStreamW(
330         HKEY hkey,
331         LPCWSTR pszSubkey,
332         LPCWSTR pszValue,
333         DWORD grfMode)
334 {
335         TRACE("(0x%08x,%s,%s,0x%08lx)\n",
336         hkey, debugstr_w(pszSubkey), debugstr_w(pszValue), grfMode);
337
338         return IStream_ConstructorW(hkey, pszSubkey, pszValue, grfMode);
339 }