Merge 13511:13830 from trunk
[reactos.git] / reactos / lib / 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 <stdarg.h>
23 #include <string.h>
24
25 #define COBJMACROS
26
27 #include "winerror.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "objbase.h"
31 #include "winreg.h"
32
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(shell);
36
37 typedef struct
38 { IStreamVtbl *lpVtbl;
39 DWORD ref;
40 HKEY hKey;
41 LPBYTE pbBuffer;
42 DWORD dwLength;
43 DWORD dwPos;
44 } ISHRegStream;
45
46 /**************************************************************************
47 * IStream_fnQueryInterface
48 */
49 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
50 {
51 ISHRegStream *This = (ISHRegStream *)iface;
52
53 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
54
55 *ppvObj = NULL;
56
57 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
58 *ppvObj = This;
59 else if(IsEqualIID(riid, &IID_IStream)) /*IStream*/
60 *ppvObj = This;
61
62 if(*ppvObj)
63 {
64 IStream_AddRef((IStream*)*ppvObj);
65 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
66 return S_OK;
67 }
68 TRACE("-- Interface: E_NOINTERFACE\n");
69 return E_NOINTERFACE;
70 }
71
72 /**************************************************************************
73 * IStream_fnAddRef
74 */
75 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
76 {
77 ISHRegStream *This = (ISHRegStream *)iface;
78 ULONG refCount = InterlockedIncrement(&This->ref);
79
80 TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
81
82 return refCount;
83 }
84
85 /**************************************************************************
86 * IStream_fnRelease
87 */
88 static ULONG WINAPI IStream_fnRelease(IStream *iface)
89 {
90 ISHRegStream *This = (ISHRegStream *)iface;
91 ULONG refCount = InterlockedDecrement(&This->ref);
92
93 TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
94
95 if (!refCount)
96 {
97 TRACE(" destroying SHReg IStream (%p)\n",This);
98
99 HeapFree(GetProcessHeap(),0,This->pbBuffer);
100
101 if (This->hKey)
102 RegCloseKey(This->hKey);
103
104 HeapFree(GetProcessHeap(),0,This);
105 return 0;
106 }
107
108 return refCount;
109 }
110
111 /**************************************************************************
112 * IStream_fnRead
113 */
114 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
115 {
116 ISHRegStream *This = (ISHRegStream *)iface;
117
118 DWORD dwBytesToRead, dwBytesLeft;
119
120 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
121
122 if (!pv)
123 return STG_E_INVALIDPOINTER;
124
125 dwBytesLeft = This->dwLength - This->dwPos;
126
127 if ( 0 >= dwBytesLeft ) /* end of buffer */
128 return S_FALSE;
129
130 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
131
132 memmove ( pv, (This->pbBuffer) + (This->dwPos), dwBytesToRead);
133
134 This->dwPos += dwBytesToRead; /* adjust pointer */
135
136 if (pcbRead)
137 *pcbRead = dwBytesToRead;
138
139 return S_OK;
140 }
141
142 /**************************************************************************
143 * IStream_fnWrite
144 */
145 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
146 {
147 ISHRegStream *This = (ISHRegStream *)iface;
148
149 TRACE("(%p)\n",This);
150
151 if (pcbWritten)
152 *pcbWritten = 0;
153
154 return E_NOTIMPL;
155 }
156
157 /**************************************************************************
158 * IStream_fnSeek
159 */
160 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
161 {
162 ISHRegStream *This = (ISHRegStream *)iface;
163
164 TRACE("(%p)\n",This);
165
166 if (plibNewPosition)
167 plibNewPosition->QuadPart = 0;
168 return E_NOTIMPL;
169 }
170
171 /**************************************************************************
172 * IStream_fnSetSize
173 */
174 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
175 {
176 ISHRegStream *This = (ISHRegStream *)iface;
177
178 TRACE("(%p)\n",This);
179 return E_NOTIMPL;
180 }
181
182 /**************************************************************************
183 * IStream_fnCopyTo
184 */
185 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
186 {
187 ISHRegStream *This = (ISHRegStream *)iface;
188
189 TRACE("(%p)\n",This);
190 if (pcbRead)
191 pcbRead->QuadPart = 0;
192 if (pcbWritten)
193 pcbWritten->QuadPart = 0;
194 return E_NOTIMPL;
195 }
196
197 /**************************************************************************
198 * IStream_fnCommit
199 */
200 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
201 {
202 ISHRegStream *This = (ISHRegStream *)iface;
203
204 TRACE("(%p)\n",This);
205
206 return E_NOTIMPL;
207 }
208
209 /**************************************************************************
210 * IStream_fnRevert
211 */
212 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
213 {
214 ISHRegStream *This = (ISHRegStream *)iface;
215
216 TRACE("(%p)\n",This);
217
218 return E_NOTIMPL;
219 }
220
221 /**************************************************************************
222 * IStream_fnLockUnlockRegion
223 */
224 static HRESULT WINAPI IStream_fnLockUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
225 {
226 ISHRegStream *This = (ISHRegStream *)iface;
227
228 TRACE("(%p)\n",This);
229
230 return E_NOTIMPL;
231 }
232
233 /*************************************************************************
234 * IStream_fnStat
235 */
236 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
237 {
238 ISHRegStream *This = (ISHRegStream *)iface;
239
240 TRACE("(%p)\n",This);
241
242 return E_NOTIMPL;
243 }
244
245 /*************************************************************************
246 * IStream_fnClone
247 */
248 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
249 {
250 ISHRegStream *This = (ISHRegStream *)iface;
251
252 TRACE("(%p)\n",This);
253 if (ppstm)
254 *ppstm = NULL;
255 return E_NOTIMPL;
256 }
257
258 static struct IStreamVtbl rstvt =
259 {
260 IStream_fnQueryInterface,
261 IStream_fnAddRef,
262 IStream_fnRelease,
263 IStream_fnRead,
264 IStream_fnWrite,
265 IStream_fnSeek,
266 IStream_fnSetSize,
267 IStream_fnCopyTo,
268 IStream_fnCommit,
269 IStream_fnRevert,
270 IStream_fnLockUnlockRegion,
271 IStream_fnLockUnlockRegion,
272 IStream_fnStat,
273 IStream_fnClone
274 };
275
276 /* Methods overridden by the dummy stream */
277
278 /**************************************************************************
279 * IStream_fnAddRefDummy
280 */
281 static ULONG WINAPI IStream_fnAddRefDummy(IStream *iface)
282 {
283 ISHRegStream *This = (ISHRegStream *)iface;
284 TRACE("(%p)\n", This);
285 return 2;
286 }
287
288 /**************************************************************************
289 * IStream_fnReleaseDummy
290 */
291 static ULONG WINAPI IStream_fnReleaseDummy(IStream *iface)
292 {
293 ISHRegStream *This = (ISHRegStream *)iface;
294 TRACE("(%p)\n", This);
295 return 1;
296 }
297
298 /**************************************************************************
299 * IStream_fnReadDummy
300 */
301 static HRESULT WINAPI IStream_fnReadDummy(IStream *iface, LPVOID pv, ULONG cb, ULONG* pcbRead)
302 {
303 if (pcbRead)
304 *pcbRead = 0;
305 return E_NOTIMPL;
306 }
307
308 static struct IStreamVtbl DummyRegStreamVTable =
309 {
310 IStream_fnQueryInterface,
311 IStream_fnAddRefDummy, /* Overridden */
312 IStream_fnReleaseDummy, /* Overridden */
313 IStream_fnReadDummy, /* Overridden */
314 IStream_fnWrite,
315 IStream_fnSeek,
316 IStream_fnSetSize,
317 IStream_fnCopyTo,
318 IStream_fnCommit,
319 IStream_fnRevert,
320 IStream_fnLockUnlockRegion,
321 IStream_fnLockUnlockRegion,
322 IStream_fnStat,
323 IStream_fnClone
324 };
325
326 /* Dummy registry stream object */
327 static ISHRegStream rsDummyRegStream =
328 {
329 &DummyRegStreamVTable,
330 1,
331 NULL,
332 NULL,
333 0,
334 0
335 };
336
337 /**************************************************************************
338 * IStream_Create
339 *
340 * Internal helper: Create and initialise a new registry stream object.
341 */
342 static IStream *IStream_Create(HKEY hKey, LPBYTE pbBuffer, DWORD dwLength)
343 {
344 ISHRegStream* regStream;
345
346 regStream = (ISHRegStream*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHRegStream));
347
348 if (regStream)
349 {
350 regStream->lpVtbl = &rstvt;
351 regStream->ref = 1;
352 regStream->hKey = hKey;
353 regStream->pbBuffer = pbBuffer;
354 regStream->dwLength = dwLength;
355 regStream->dwPos = 0;
356 }
357 TRACE ("Returning %p\n", regStream);
358 return (IStream *)regStream;
359 }
360
361 /*************************************************************************
362 * SHOpenRegStream2A [SHLWAPI.@]
363 *
364 * Create a stream to read binary registry data.
365 *
366 * PARAMS
367 * hKey [I] Registry handle
368 * pszSubkey [I] The sub key name
369 * pszValue [I] The value name under the sub key
370 * dwMode [I] Unused
371 *
372 * RETURNS
373 * Success: An IStream interface referring to the registry data
374 * Failure: NULL, if the registry key could not be opened or is not binary.
375 */
376 IStream * WINAPI SHOpenRegStream2A(HKEY hKey, LPCSTR pszSubkey,
377 LPCSTR pszValue,DWORD dwMode)
378 {
379 HKEY hStrKey = NULL;
380 LPBYTE lpBuff = NULL;
381 DWORD dwLength, dwType;
382
383 TRACE("(%p,%s,%s,0x%08lx)\n", hKey, pszSubkey, pszValue, dwMode);
384
385 /* Open the key, read in binary data and create stream */
386 if (!RegOpenKeyExA (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
387 !RegQueryValueExA (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
388 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
389 !RegQueryValueExA (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
390 dwType == REG_BINARY)
391 return IStream_Create(hStrKey, lpBuff, dwLength);
392
393 HeapFree (GetProcessHeap(), 0, lpBuff);
394 if (hStrKey)
395 RegCloseKey(hStrKey);
396 return NULL;
397 }
398
399 /*************************************************************************
400 * SHOpenRegStream2W [SHLWAPI.@]
401 *
402 * See SHOpenRegStream2A.
403 */
404 IStream * WINAPI SHOpenRegStream2W(HKEY hKey, LPCWSTR pszSubkey,
405 LPCWSTR pszValue, DWORD dwMode)
406 {
407 HKEY hStrKey = NULL;
408 LPBYTE lpBuff = NULL;
409 DWORD dwLength, dwType;
410
411 TRACE("(%p,%s,%s,0x%08lx)\n", hKey, debugstr_w(pszSubkey),
412 debugstr_w(pszValue), dwMode);
413
414 /* Open the key, read in binary data and create stream */
415 if (!RegOpenKeyExW (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
416 !RegQueryValueExW (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
417 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
418 !RegQueryValueExW (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
419 dwType == REG_BINARY)
420 return IStream_Create(hStrKey, lpBuff, dwLength);
421
422 HeapFree (GetProcessHeap(), 0, lpBuff);
423 if (hStrKey)
424 RegCloseKey(hStrKey);
425 return NULL;
426 }
427
428 /*************************************************************************
429 * SHOpenRegStreamA [SHLWAPI.@]
430 *
431 * Create a stream to read binary registry data.
432 *
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
438 *
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.
443 */
444 IStream * WINAPI SHOpenRegStreamA(HKEY hkey, LPCSTR pszSubkey,
445 LPCSTR pszValue, DWORD dwMode)
446 {
447 IStream *iStream;
448
449 TRACE("(%p,%s,%s,0x%08lx)\n", hkey, pszSubkey, pszValue, dwMode);
450
451 iStream = SHOpenRegStream2A(hkey, pszSubkey, pszValue, dwMode);
452 return iStream ? iStream : (IStream *)&rsDummyRegStream;
453 }
454
455 /*************************************************************************
456 * SHOpenRegStreamW [SHLWAPI.@]
457 *
458 * See SHOpenRegStreamA.
459 */
460 IStream * WINAPI SHOpenRegStreamW(HKEY hkey, LPCWSTR pszSubkey,
461 LPCWSTR pszValue, DWORD dwMode)
462 {
463 IStream *iStream;
464
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;
469 }
470
471 /*************************************************************************
472 * @ [SHLWAPI.12]
473 *
474 * Create an IStream object on a block of memory.
475 *
476 * PARAMS
477 * lpbData [I] Memory block to create the IStream object on
478 * dwDataLen [I] Length of data block
479 *
480 * RETURNS
481 * Success: A pointer to the IStream object.
482 * Failure: NULL, if any parameters are invalid or an error occurs.
483 *
484 * NOTES
485 * A copy of the memory pointed to by lpbData is made, and is freed
486 * when the stream is released.
487 */
488 IStream * WINAPI SHCreateMemStream(LPBYTE lpbData, DWORD dwDataLen)
489 {
490 IStream *iStrmRet = NULL;
491
492 TRACE("(%p,%ld)\n", lpbData, dwDataLen);
493
494 if (lpbData)
495 {
496 LPBYTE lpbDup = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, dwDataLen);
497
498 if (lpbDup)
499 {
500 memcpy(lpbDup, lpbData, dwDataLen);
501 iStrmRet = IStream_Create(NULL, lpbDup, dwDataLen);
502
503 if (!iStrmRet)
504 HeapFree(GetProcessHeap(), 0, lpbDup);
505 }
506 }
507 return iStrmRet;
508 }
509
510 /*************************************************************************
511 * SHCreateStreamWrapper [SHLWAPI.@]
512 *
513 * Create an IStream object on a block of memory.
514 *
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
520 *
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.
525 *
526 * NOTES
527 * The stream assumes ownership of the memory passed to it.
528 */
529 HRESULT WINAPI SHCreateStreamWrapper(LPBYTE lpbData, DWORD dwDataLen,
530 DWORD dwReserved, IStream **lppStream)
531 {
532 IStream* lpStream;
533
534 if (lppStream)
535 *lppStream = NULL;
536
537 if(dwReserved || !lppStream)
538 return E_INVALIDARG;
539
540 lpStream = IStream_Create(NULL, lpbData, dwDataLen);
541
542 if(!lpStream)
543 return E_OUTOFMEMORY;
544
545 IStream_QueryInterface(lpStream, &IID_IStream, (void**)lppStream);
546 IStream_Release(lpStream);
547 return S_OK;
548 }