Fixed some issues found by winapi_check.
[wine] / dlls / shlwapi / regstream.c
1 /*
2  * SHLWAPI Registry Stream functions
3  *
4  * Copyright 1999 Juergen Schmied
5  * Copyright 2002 Jon Griffiths
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <string.h>
23
24 #include "winerror.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "wine/obj_base.h"
28 #include "wine/obj_storage.h"
29
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(shell);
33
34 typedef struct
35 {       ICOM_VFIELD(IStream);
36         DWORD  ref;
37         HKEY   hKey;
38         LPBYTE pbBuffer;
39         DWORD  dwLength;
40         DWORD  dwPos;
41 } ISHRegStream;
42
43 /**************************************************************************
44 *  IStream_fnQueryInterface
45 */
46 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
47 {
48         ICOM_THIS(ISHRegStream, iface);
49
50         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
51
52         *ppvObj = NULL;
53
54         if(IsEqualIID(riid, &IID_IUnknown))     /*IUnknown*/
55           *ppvObj = This;
56         else if(IsEqualIID(riid, &IID_IStream)) /*IStream*/
57           *ppvObj = This;
58
59         if(*ppvObj)
60         {
61           IStream_AddRef((IStream*)*ppvObj);
62           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
63           return S_OK;
64         }
65         TRACE("-- Interface: E_NOINTERFACE\n");
66         return E_NOINTERFACE;
67 }
68
69 /**************************************************************************
70 *  IStream_fnAddRef
71 */
72 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
73 {
74         ICOM_THIS(ISHRegStream, iface);
75
76         TRACE("(%p)->(count=%lu)\n",This, This->ref);
77
78         return InterlockedIncrement(&This->ref);
79 }
80
81 /**************************************************************************
82 *  IStream_fnRelease
83 */
84 static ULONG WINAPI IStream_fnRelease(IStream *iface)
85 {
86         ICOM_THIS(ISHRegStream, iface);
87
88         TRACE("(%p)->()\n",This);
89
90         if (!InterlockedDecrement(&This->ref))
91         {
92           TRACE(" destroying SHReg IStream (%p)\n",This);
93
94           if (This->pbBuffer)
95             HeapFree(GetProcessHeap(),0,This->pbBuffer);
96
97           if (This->hKey)
98             RegCloseKey(This->hKey);
99
100           HeapFree(GetProcessHeap(),0,This);
101           return 0;
102         }
103         return This->ref;
104 }
105
106 /**************************************************************************
107  * IStream_fnRead
108  */
109 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
110 {
111         ICOM_THIS(ISHRegStream, iface);
112
113         DWORD dwBytesToRead, dwBytesLeft;
114
115         TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
116
117         if (!pv)
118           return STG_E_INVALIDPOINTER;
119
120         dwBytesLeft = This->dwLength - This->dwPos;
121
122         if ( 0 >= dwBytesLeft ) /* end of buffer */
123           return S_FALSE;
124
125         dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
126
127         memmove ( pv, (This->pbBuffer) + (This->dwPos), dwBytesToRead);
128
129         This->dwPos += dwBytesToRead; /* adjust pointer */
130
131         if (pcbRead)
132           *pcbRead = dwBytesToRead;
133
134         return S_OK;
135 }
136
137 /**************************************************************************
138  * IStream_fnWrite
139  */
140 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
141 {
142         ICOM_THIS(ISHRegStream, iface);
143
144         TRACE("(%p)\n",This);
145
146         if (pcbWritten)
147           *pcbWritten = 0;
148
149         return E_NOTIMPL;
150 }
151
152 /**************************************************************************
153  *  IStream_fnSeek
154  */
155 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
156 {
157         ICOM_THIS(ISHRegStream, iface);
158
159         TRACE("(%p)\n",This);
160
161         if (plibNewPosition)
162           plibNewPosition->QuadPart = 0;
163         return E_NOTIMPL;
164 }
165
166 /**************************************************************************
167  * IStream_fnSetSize
168  */
169 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
170 {
171         ICOM_THIS(ISHRegStream, iface);
172
173         TRACE("(%p)\n",This);
174         return E_NOTIMPL;
175 }
176
177 /**************************************************************************
178  * IStream_fnCopyTo
179  */
180 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
181 {
182         ICOM_THIS(ISHRegStream, iface);
183
184         TRACE("(%p)\n",This);
185         if (pcbRead)
186           pcbRead->QuadPart = 0;
187         if (pcbWritten)
188           pcbWritten->QuadPart = 0;
189         return E_NOTIMPL;
190 }
191
192 /**************************************************************************
193  * IStream_fnCommit
194  */
195 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
196 {
197         ICOM_THIS(ISHRegStream, iface);
198
199         TRACE("(%p)\n",This);
200
201         return E_NOTIMPL;
202 }
203
204 /**************************************************************************
205  * IStream_fnRevert
206  */
207 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
208 {
209         ICOM_THIS(ISHRegStream, iface);
210
211         TRACE("(%p)\n",This);
212
213         return E_NOTIMPL;
214 }
215
216 /**************************************************************************
217  * IStream_fnLockUnlockRegion
218  */
219 static HRESULT WINAPI IStream_fnLockUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
220 {
221         ICOM_THIS(ISHRegStream, iface);
222
223         TRACE("(%p)\n",This);
224
225         return E_NOTIMPL;
226 }
227
228 /*************************************************************************
229  * IStream_fnStat
230  */
231 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag)
232 {
233         ICOM_THIS(ISHRegStream, iface);
234
235         TRACE("(%p)\n",This);
236
237         return E_NOTIMPL;
238 }
239
240 /*************************************************************************
241  * IStream_fnClone
242  */
243 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
244 {
245         ICOM_THIS(ISHRegStream, iface);
246
247         TRACE("(%p)\n",This);
248         if (ppstm)
249           *ppstm = NULL;
250         return E_NOTIMPL;
251 }
252
253 static struct ICOM_VTABLE(IStream) rstvt =
254 {
255         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
256         IStream_fnQueryInterface,
257         IStream_fnAddRef,
258         IStream_fnRelease,
259         IStream_fnRead,
260         IStream_fnWrite,
261         IStream_fnSeek,
262         IStream_fnSetSize,
263         IStream_fnCopyTo,
264         IStream_fnCommit,
265         IStream_fnRevert,
266         IStream_fnLockUnlockRegion,
267         IStream_fnLockUnlockRegion,
268         IStream_fnStat,
269         IStream_fnClone
270 };
271
272 /* Methods overridden by the dummy stream */
273
274 /**************************************************************************
275  *  IStream_fnAddRefDummy
276  */
277 static ULONG WINAPI IStream_fnAddRefDummy(IStream *iface)
278 {
279         ICOM_THIS(ISHRegStream, iface);
280         TRACE("(%p)\n", This);
281         return 2;
282 }
283
284 /**************************************************************************
285  *  IStream_fnReleaseDummy
286  */
287 static ULONG WINAPI IStream_fnReleaseDummy(IStream *iface)
288 {
289         ICOM_THIS(ISHRegStream, iface);
290         TRACE("(%p)\n", This);
291         return 1;
292 }
293
294 /**************************************************************************
295  * IStream_fnReadDummy
296  */
297 static HRESULT WINAPI IStream_fnReadDummy(IStream *iface, LPVOID pv, ULONG cb, ULONG* pcbRead)
298 {
299   if (pcbRead)
300     *pcbRead = 0;
301   return E_NOTIMPL;
302 }
303
304 static struct ICOM_VTABLE(IStream) DummyRegStreamVTable =
305 {
306   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
307   IStream_fnQueryInterface,
308   IStream_fnAddRefDummy,  /* Overridden */
309   IStream_fnReleaseDummy, /* Overridden */
310   IStream_fnReadDummy,    /* Overridden */
311   IStream_fnWrite,
312   IStream_fnSeek,
313   IStream_fnSetSize,
314   IStream_fnCopyTo,
315   IStream_fnCommit,
316   IStream_fnRevert,
317   IStream_fnLockUnlockRegion,
318   IStream_fnLockUnlockRegion,
319   IStream_fnStat,
320   IStream_fnClone
321 };
322
323 /* Dummy registry stream object */
324 static ISHRegStream rsDummyRegStream =
325 {
326  &DummyRegStreamVTable,
327  1,
328  (HKEY)0,
329  NULL,
330  0,
331  0
332 };
333
334 /**************************************************************************
335  * IStream_Create
336  *
337  * Internal helper: Create and initialise a new registry stream object.
338  */
339 static IStream *IStream_Create(HKEY hKey, LPBYTE pbBuffer, DWORD dwLength)
340 {
341  ISHRegStream* regStream;
342
343  regStream = (ISHRegStream*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHRegStream));
344
345  if (regStream)
346  {
347    ICOM_VTBL(regStream) = &rstvt;
348    regStream->ref = 1;
349    regStream->hKey = hKey;
350    regStream->pbBuffer = pbBuffer;
351    regStream->dwLength = dwLength;
352    regStream->dwPos = 0;
353  }
354  TRACE ("Returning %p\n", regStream);
355  return (IStream *)regStream;
356 }
357
358 /*************************************************************************
359  * SHOpenRegStream2A    [SHLWAPI.@]
360  *
361  * Create a stream to read binary registry data.
362  *
363  * PARAMS
364  * hKey      [I] Registry handle
365  * pszSubkey [I] The sub key name
366  * pszValue  [I] The value name under the sub key
367  * dwMode    [I] Unused
368  *
369  * RETURNS
370  * Success: An IStream interface referring to the registry data
371  * Failure: NULL, if the registry key could not be opened or is not binary.
372  */
373 IStream * WINAPI SHOpenRegStream2A(HKEY hKey, LPCSTR pszSubkey,
374                                    LPCSTR pszValue,DWORD dwMode)
375 {
376   HKEY hStrKey = (HKEY)0;
377   LPBYTE lpBuff = NULL;
378   DWORD dwLength, dwType;
379
380   TRACE("(0x%08x,%s,%s,0x%08lx)\n", hKey, pszSubkey, pszValue, dwMode);
381
382   /* Open the key, read in binary data and create stream */
383   if (!RegOpenKeyExA (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
384       !RegQueryValueExA (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
385       (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
386       !RegQueryValueExA (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
387       dwType == REG_BINARY)
388     return IStream_Create(hStrKey, lpBuff, dwLength);
389
390   if (lpBuff)
391     HeapFree (GetProcessHeap(), 0, lpBuff);
392   if (hStrKey)
393     RegCloseKey(hStrKey);
394   return NULL;
395 }
396
397 /*************************************************************************
398  * SHOpenRegStream2W    [SHLWAPI.@]
399  *
400  * See SHOpenRegStream2A.
401  */
402 IStream * WINAPI SHOpenRegStream2W(HKEY hKey, LPCWSTR pszSubkey,
403                                    LPCWSTR pszValue, DWORD dwMode)
404 {
405   HKEY hStrKey = (HKEY)0;
406   LPBYTE lpBuff = NULL;
407   DWORD dwLength, dwType;
408
409   TRACE("(0x%08x,%s,%s,0x%08lx)\n", hKey, debugstr_w(pszSubkey),
410         debugstr_w(pszValue), dwMode);
411
412   /* Open the key, read in binary data and create stream */
413   if (!RegOpenKeyExW (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
414       !RegQueryValueExW (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
415       (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
416       !RegQueryValueExW (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
417       dwType == REG_BINARY)
418     return IStream_Create(hStrKey, lpBuff, dwLength);
419
420   if (lpBuff)
421     HeapFree (GetProcessHeap(), 0, lpBuff);
422   if (hStrKey)
423     RegCloseKey(hStrKey);
424   return NULL;
425 }
426
427 /*************************************************************************
428  * SHOpenRegStreamA     [SHLWAPI.@]
429  *
430  * Create a stream to read binary registry data.
431  *
432  * PARAMS
433  * hKey      [I] Registry handle
434  * pszSubkey [I] The sub key name
435  * pszValue  [I] The value name under the sub key
436  * dwMode    [I] STGM mode for opening the file
437  *
438  * RETURNS
439  * Success: An IStream interface referring to the registry data
440  * Failure: If the registry key could not be opened or is not binary,
441  *          A dummy (empty) IStream object is returned.
442  */
443 IStream * WINAPI SHOpenRegStreamA(HKEY hkey, LPCSTR pszSubkey,
444                                   LPCSTR pszValue, DWORD dwMode)
445 {
446   IStream *iStream;
447
448   TRACE("(0x%08x,%s,%s,0x%08lx)\n", hkey, pszSubkey, pszValue, dwMode);
449
450   iStream = SHOpenRegStream2A(hkey, pszSubkey, pszValue, dwMode);
451   return iStream ? iStream : (IStream *)&rsDummyRegStream;
452 }
453
454 /*************************************************************************
455  * SHOpenRegStreamW     [SHLWAPI.@]
456  *
457  * See SHOpenRegStreamA.
458  */
459 IStream * WINAPI SHOpenRegStreamW(HKEY hkey, LPCWSTR pszSubkey,
460                                   LPCWSTR pszValue, DWORD dwMode)
461 {
462   IStream *iStream;
463
464   TRACE("(0x%08x,%s,%s,0x%08lx)\n", hkey, debugstr_w(pszSubkey),
465         debugstr_w(pszValue), dwMode);
466   iStream = SHOpenRegStream2W(hkey, pszSubkey, pszValue, dwMode);
467   return iStream ? iStream : (IStream *)&rsDummyRegStream;
468 }
469
470 /*************************************************************************
471  * @   [SHLWAPI.12]
472  *
473  * Create a stream on a block of memory.
474  *
475  * PARAMS
476  * lpbData   [I] Memory block to create the stream on
477  * dwDataLen [I] Length of data block
478  *
479  * RETURNS
480  * Success: A pointer to the stream object.
481  * Failure: NULL, if any parameters are invalid or an error occurs.
482  *
483  * NOTES
484  *  A copy of the memory pointed to by lpbData is made, and is freed
485  *  when the stream is released.
486  */
487 IStream * WINAPI SHLWAPI_12(LPBYTE lpbData, DWORD dwDataLen)
488 {
489   IStream *iStrmRet = NULL;
490
491   TRACE("(%p,%ld)\n", lpbData, dwDataLen);
492
493   if (lpbData)
494   {
495     LPBYTE lpbDup = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, dwDataLen);
496
497     if (lpbDup)
498     {
499       memcpy(lpbDup, lpbData, dwDataLen);
500       iStrmRet = IStream_Create((HKEY)0, lpbDup, dwDataLen);
501
502       if (!iStrmRet)
503         HeapFree(GetProcessHeap(), 0, lpbDup);
504     }
505   }
506   return iStrmRet;
507 }