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