CryptCreateHash: Set returned hash handle to zero in case of failure.
[wine.git] / dlls / shlwapi / regstream.c
blob614c70bd1b0878c0faaa97e28a9cfaccce59c906
1 /*
2 * SHLWAPI Registry Stream functions
4 * Copyright 1999 Juergen Schmied
5 * Copyright 2002 Jon Griffiths
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.
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.
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
22 #include <stdarg.h>
23 #include <string.h>
25 #define COBJMACROS
27 #include "winerror.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "objbase.h"
31 #include "winreg.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(shell);
37 typedef struct
38 { IStreamVtbl *lpVtbl;
39 DWORD ref;
40 HKEY hKey;
41 LPBYTE pbBuffer;
42 DWORD dwLength;
43 DWORD dwPos;
44 } ISHRegStream;
46 /**************************************************************************
47 * IStream_fnQueryInterface
49 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
51 ISHRegStream *This = (ISHRegStream *)iface;
53 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
55 *ppvObj = NULL;
57 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
58 *ppvObj = This;
59 else if(IsEqualIID(riid, &IID_IStream)) /*IStream*/
60 *ppvObj = This;
62 if(*ppvObj)
64 IStream_AddRef((IStream*)*ppvObj);
65 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
66 return S_OK;
68 TRACE("-- Interface: E_NOINTERFACE\n");
69 return E_NOINTERFACE;
72 /**************************************************************************
73 * IStream_fnAddRef
75 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
77 ISHRegStream *This = (ISHRegStream *)iface;
79 TRACE("(%p)->(count=%lu)\n",This, This->ref);
81 return InterlockedIncrement(&This->ref);
84 /**************************************************************************
85 * IStream_fnRelease
87 static ULONG WINAPI IStream_fnRelease(IStream *iface)
89 ISHRegStream *This = (ISHRegStream *)iface;
91 TRACE("(%p)->()\n",This);
93 if (!InterlockedDecrement(&This->ref))
95 TRACE(" destroying SHReg IStream (%p)\n",This);
97 if (This->pbBuffer)
98 HeapFree(GetProcessHeap(),0,This->pbBuffer);
100 if (This->hKey)
101 RegCloseKey(This->hKey);
103 HeapFree(GetProcessHeap(),0,This);
104 return 0;
106 return This->ref;
109 /**************************************************************************
110 * IStream_fnRead
112 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
114 ISHRegStream *This = (ISHRegStream *)iface;
116 DWORD dwBytesToRead, dwBytesLeft;
118 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
120 if (!pv)
121 return STG_E_INVALIDPOINTER;
123 dwBytesLeft = This->dwLength - This->dwPos;
125 if ( 0 >= dwBytesLeft ) /* end of buffer */
126 return S_FALSE;
128 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
130 memmove ( pv, (This->pbBuffer) + (This->dwPos), dwBytesToRead);
132 This->dwPos += dwBytesToRead; /* adjust pointer */
134 if (pcbRead)
135 *pcbRead = dwBytesToRead;
137 return S_OK;
140 /**************************************************************************
141 * IStream_fnWrite
143 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
145 ISHRegStream *This = (ISHRegStream *)iface;
147 TRACE("(%p)\n",This);
149 if (pcbWritten)
150 *pcbWritten = 0;
152 return E_NOTIMPL;
155 /**************************************************************************
156 * IStream_fnSeek
158 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
160 ISHRegStream *This = (ISHRegStream *)iface;
162 TRACE("(%p)\n",This);
164 if (plibNewPosition)
165 plibNewPosition->QuadPart = 0;
166 return E_NOTIMPL;
169 /**************************************************************************
170 * IStream_fnSetSize
172 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
174 ISHRegStream *This = (ISHRegStream *)iface;
176 TRACE("(%p)\n",This);
177 return E_NOTIMPL;
180 /**************************************************************************
181 * IStream_fnCopyTo
183 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
185 ISHRegStream *This = (ISHRegStream *)iface;
187 TRACE("(%p)\n",This);
188 if (pcbRead)
189 pcbRead->QuadPart = 0;
190 if (pcbWritten)
191 pcbWritten->QuadPart = 0;
192 return E_NOTIMPL;
195 /**************************************************************************
196 * IStream_fnCommit
198 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
200 ISHRegStream *This = (ISHRegStream *)iface;
202 TRACE("(%p)\n",This);
204 return E_NOTIMPL;
207 /**************************************************************************
208 * IStream_fnRevert
210 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
212 ISHRegStream *This = (ISHRegStream *)iface;
214 TRACE("(%p)\n",This);
216 return E_NOTIMPL;
219 /**************************************************************************
220 * IStream_fnLockUnlockRegion
222 static HRESULT WINAPI IStream_fnLockUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
224 ISHRegStream *This = (ISHRegStream *)iface;
226 TRACE("(%p)\n",This);
228 return E_NOTIMPL;
231 /*************************************************************************
232 * IStream_fnStat
234 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
236 ISHRegStream *This = (ISHRegStream *)iface;
238 TRACE("(%p)\n",This);
240 return E_NOTIMPL;
243 /*************************************************************************
244 * IStream_fnClone
246 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
248 ISHRegStream *This = (ISHRegStream *)iface;
250 TRACE("(%p)\n",This);
251 if (ppstm)
252 *ppstm = NULL;
253 return E_NOTIMPL;
256 static struct IStreamVtbl rstvt =
258 IStream_fnQueryInterface,
259 IStream_fnAddRef,
260 IStream_fnRelease,
261 IStream_fnRead,
262 IStream_fnWrite,
263 IStream_fnSeek,
264 IStream_fnSetSize,
265 IStream_fnCopyTo,
266 IStream_fnCommit,
267 IStream_fnRevert,
268 IStream_fnLockUnlockRegion,
269 IStream_fnLockUnlockRegion,
270 IStream_fnStat,
271 IStream_fnClone
274 /* Methods overridden by the dummy stream */
276 /**************************************************************************
277 * IStream_fnAddRefDummy
279 static ULONG WINAPI IStream_fnAddRefDummy(IStream *iface)
281 ISHRegStream *This = (ISHRegStream *)iface;
282 TRACE("(%p)\n", This);
283 return 2;
286 /**************************************************************************
287 * IStream_fnReleaseDummy
289 static ULONG WINAPI IStream_fnReleaseDummy(IStream *iface)
291 ISHRegStream *This = (ISHRegStream *)iface;
292 TRACE("(%p)\n", This);
293 return 1;
296 /**************************************************************************
297 * IStream_fnReadDummy
299 static HRESULT WINAPI IStream_fnReadDummy(IStream *iface, LPVOID pv, ULONG cb, ULONG* pcbRead)
301 if (pcbRead)
302 *pcbRead = 0;
303 return E_NOTIMPL;
306 static struct IStreamVtbl DummyRegStreamVTable =
308 IStream_fnQueryInterface,
309 IStream_fnAddRefDummy, /* Overridden */
310 IStream_fnReleaseDummy, /* Overridden */
311 IStream_fnReadDummy, /* Overridden */
312 IStream_fnWrite,
313 IStream_fnSeek,
314 IStream_fnSetSize,
315 IStream_fnCopyTo,
316 IStream_fnCommit,
317 IStream_fnRevert,
318 IStream_fnLockUnlockRegion,
319 IStream_fnLockUnlockRegion,
320 IStream_fnStat,
321 IStream_fnClone
324 /* Dummy registry stream object */
325 static ISHRegStream rsDummyRegStream =
327 &DummyRegStreamVTable,
329 NULL,
330 NULL,
335 /**************************************************************************
336 * IStream_Create
338 * Internal helper: Create and initialise a new registry stream object.
340 static IStream *IStream_Create(HKEY hKey, LPBYTE pbBuffer, DWORD dwLength)
342 ISHRegStream* regStream;
344 regStream = (ISHRegStream*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHRegStream));
346 if (regStream)
348 regStream->lpVtbl = &rstvt;
349 regStream->ref = 1;
350 regStream->hKey = hKey;
351 regStream->pbBuffer = pbBuffer;
352 regStream->dwLength = dwLength;
353 regStream->dwPos = 0;
355 TRACE ("Returning %p\n", regStream);
356 return (IStream *)regStream;
359 /*************************************************************************
360 * SHOpenRegStream2A [SHLWAPI.@]
362 * Create a stream to read binary registry data.
364 * PARAMS
365 * hKey [I] Registry handle
366 * pszSubkey [I] The sub key name
367 * pszValue [I] The value name under the sub key
368 * dwMode [I] Unused
370 * RETURNS
371 * Success: An IStream interface referring to the registry data
372 * Failure: NULL, if the registry key could not be opened or is not binary.
374 IStream * WINAPI SHOpenRegStream2A(HKEY hKey, LPCSTR pszSubkey,
375 LPCSTR pszValue,DWORD dwMode)
377 HKEY hStrKey = NULL;
378 LPBYTE lpBuff = NULL;
379 DWORD dwLength, dwType;
381 TRACE("(%p,%s,%s,0x%08lx)\n", hKey, pszSubkey, pszValue, dwMode);
383 /* Open the key, read in binary data and create stream */
384 if (!RegOpenKeyExA (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
385 !RegQueryValueExA (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
386 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
387 !RegQueryValueExA (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
388 dwType == REG_BINARY)
389 return IStream_Create(hStrKey, lpBuff, dwLength);
391 if (lpBuff)
392 HeapFree (GetProcessHeap(), 0, lpBuff);
393 if (hStrKey)
394 RegCloseKey(hStrKey);
395 return NULL;
398 /*************************************************************************
399 * SHOpenRegStream2W [SHLWAPI.@]
401 * See SHOpenRegStream2A.
403 IStream * WINAPI SHOpenRegStream2W(HKEY hKey, LPCWSTR pszSubkey,
404 LPCWSTR pszValue, DWORD dwMode)
406 HKEY hStrKey = NULL;
407 LPBYTE lpBuff = NULL;
408 DWORD dwLength, dwType;
410 TRACE("(%p,%s,%s,0x%08lx)\n", hKey, debugstr_w(pszSubkey),
411 debugstr_w(pszValue), dwMode);
413 /* Open the key, read in binary data and create stream */
414 if (!RegOpenKeyExW (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
415 !RegQueryValueExW (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
416 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
417 !RegQueryValueExW (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
418 dwType == REG_BINARY)
419 return IStream_Create(hStrKey, lpBuff, dwLength);
421 if (lpBuff)
422 HeapFree (GetProcessHeap(), 0, lpBuff);
423 if (hStrKey)
424 RegCloseKey(hStrKey);
425 return NULL;
428 /*************************************************************************
429 * SHOpenRegStreamA [SHLWAPI.@]
431 * Create a stream to read binary registry data.
433 * PARAMS
434 * hKey [I] Registry handle
435 * pszSubkey [I] The sub key name
436 * pszValue [I] The value name under the sub key
437 * dwMode [I] STGM mode for opening the file
439 * RETURNS
440 * Success: An IStream interface referring to the registry data
441 * Failure: If the registry key could not be opened or is not binary,
442 * A dummy (empty) IStream object is returned.
444 IStream * WINAPI SHOpenRegStreamA(HKEY hkey, LPCSTR pszSubkey,
445 LPCSTR pszValue, DWORD dwMode)
447 IStream *iStream;
449 TRACE("(%p,%s,%s,0x%08lx)\n", hkey, pszSubkey, pszValue, dwMode);
451 iStream = SHOpenRegStream2A(hkey, pszSubkey, pszValue, dwMode);
452 return iStream ? iStream : (IStream *)&rsDummyRegStream;
455 /*************************************************************************
456 * SHOpenRegStreamW [SHLWAPI.@]
458 * See SHOpenRegStreamA.
460 IStream * WINAPI SHOpenRegStreamW(HKEY hkey, LPCWSTR pszSubkey,
461 LPCWSTR pszValue, DWORD dwMode)
463 IStream *iStream;
465 TRACE("(%p,%s,%s,0x%08lx)\n", hkey, debugstr_w(pszSubkey),
466 debugstr_w(pszValue), dwMode);
467 iStream = SHOpenRegStream2W(hkey, pszSubkey, pszValue, dwMode);
468 return iStream ? iStream : (IStream *)&rsDummyRegStream;
471 /*************************************************************************
472 * @ [SHLWAPI.12]
474 * Create an IStream object on a block of memory.
476 * PARAMS
477 * lpbData [I] Memory block to create the IStream object on
478 * dwDataLen [I] Length of data block
480 * RETURNS
481 * Success: A pointer to the IStream object.
482 * Failure: NULL, if any parameters are invalid or an error occurs.
484 * NOTES
485 * A copy of the memory pointed to by lpbData is made, and is freed
486 * when the stream is released.
488 IStream * WINAPI SHCreateMemStream(LPBYTE lpbData, DWORD dwDataLen)
490 IStream *iStrmRet = NULL;
492 TRACE("(%p,%ld)\n", lpbData, dwDataLen);
494 if (lpbData)
496 LPBYTE lpbDup = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, dwDataLen);
498 if (lpbDup)
500 memcpy(lpbDup, lpbData, dwDataLen);
501 iStrmRet = IStream_Create(NULL, lpbDup, dwDataLen);
503 if (!iStrmRet)
504 HeapFree(GetProcessHeap(), 0, lpbDup);
507 return iStrmRet;
510 /*************************************************************************
511 * SHCreateStreamWrapper [SHLWAPI.@]
513 * Create an IStream object on a block of memory.
515 * PARAMS
516 * lpbData [I] Memory block to create the IStream object on
517 * dwDataLen [I] Length of data block
518 * dwReserved [I] Reserved, Must be 0.
519 * lppStream [O] Destination for IStream object
521 * RETURNS
522 * Success: S_OK. lppStream contains the new IStream object.
523 * Failure: E_INVALIDARG, if any parameters are invalid,
524 * E_OUTOFMEMORY if memory allocation fails.
526 * NOTES
527 * The stream assumes ownership of the memory passed to it.
529 HRESULT WINAPI SHCreateStreamWrapper(LPBYTE lpbData, DWORD dwDataLen,
530 DWORD dwReserved, IStream **lppStream)
532 IStream* lpStream;
534 if (lppStream)
535 *lppStream = NULL;
537 if(dwReserved || !lppStream)
538 return E_INVALIDARG;
540 lpStream = IStream_Create(NULL, lpbData, dwDataLen);
542 if(!lpStream)
543 return E_OUTOFMEMORY;
545 IStream_QueryInterface(lpStream, &IID_IStream, (void**)lppStream);
546 IStream_Release(lpStream);
547 return S_OK;