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