shell32: Added some test to document native ITEMIDLIST format.
[wine.git] / dlls / urlmon / umstream.c
blob36909b6f7129e68687eec009062ce658fd010fdb
1 /*
2 * Based on ../shell32/memorystream.c
4 * Copyright 1999 Juergen Schmied
5 * Copyright 2003 Mike McCormack for CodeWeavers
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>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "winuser.h"
31 #include "objbase.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34 #include "ole2.h"
35 #include "urlmon.h"
36 #include "wininet.h"
37 #include "shlwapi.h"
38 #include "urlmon_main.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
42 static const IStreamVtbl stvt;
44 HRESULT UMCreateStreamOnCacheFile(LPCWSTR pszURL,
45 DWORD dwSize,
46 LPWSTR pszFileName,
47 HANDLE *phfile,
48 IUMCacheStream **ppstr)
50 IUMCacheStream* ucstr;
51 HANDLE handle;
52 LPWSTR ext;
53 LPCWSTR c;
54 LPCWSTR eloc = 0;
55 HRESULT hr;
57 for (c = pszURL; *c && *c != '#' && *c != '?'; ++c)
59 if (*c == '.')
60 eloc = c + 1;
61 else if (*c == '/' || *c == '\\')
62 eloc = 0;
65 if (!eloc)
66 eloc = c;
68 ext = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (c - eloc + 1));
69 memcpy(ext, eloc, sizeof(WCHAR) * (c - eloc));
70 ext[c - eloc] = 0;
72 if(!CreateUrlCacheEntryW(pszURL, dwSize, ext, pszFileName, 0))
73 hr = HRESULT_FROM_WIN32(GetLastError());
74 else
75 hr = 0;
77 HeapFree(GetProcessHeap(), 0, ext);
79 if (hr)
80 return hr;
82 TRACE("Opening %s\n", debugstr_w(pszFileName) );
84 handle = CreateFileW( pszFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL );
85 if( handle == INVALID_HANDLE_VALUE )
86 return HRESULT_FROM_WIN32(GetLastError());
88 if (phfile)
90 /* Call CreateFileW again because we need a handle with its own file pointer, and DuplicateHandle will return
91 * a handle that shares its file pointer with the original.
93 *phfile = CreateFileW( pszFileName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
95 if (*phfile == (HANDLE) HFILE_ERROR)
97 DWORD dwError = GetLastError();
99 CloseHandle(handle);
100 return HRESULT_FROM_WIN32(dwError);
104 ucstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,sizeof(IUMCacheStream));
105 if(ucstr )
107 ucstr->pszURL = HeapAlloc(GetProcessHeap(),
108 HEAP_ZERO_MEMORY,
109 sizeof(WCHAR) * (lstrlenW(pszURL) + 1));
110 if (ucstr->pszURL)
112 ucstr->pszFileName = HeapAlloc(GetProcessHeap(),
113 HEAP_ZERO_MEMORY,
114 sizeof(WCHAR) * (lstrlenW(pszFileName) + 1));
115 if (ucstr->pszFileName)
117 ucstr->lpVtbl=&stvt;
118 ucstr->ref = 1;
119 ucstr->handle = handle;
120 ucstr->closed = 0;
121 lstrcpyW(ucstr->pszURL, pszURL);
122 lstrcpyW(ucstr->pszFileName, pszFileName);
124 *ppstr = ucstr;
126 return S_OK;
128 HeapFree(GetProcessHeap(), 0, ucstr->pszURL);
130 HeapFree(GetProcessHeap(), 0, ucstr);
132 CloseHandle(handle);
133 if (phfile)
134 CloseHandle(*phfile);
135 return E_OUTOFMEMORY;
138 void UMCloseCacheFileStream(IUMCacheStream *This)
140 if (!This->closed)
142 FILETIME ftZero;
144 ftZero.dwLowDateTime = ftZero.dwHighDateTime = 0;
146 This->closed = 1;
147 CommitUrlCacheEntryW(This->pszURL,
148 This->pszFileName,
149 ftZero,
150 ftZero,
151 NORMAL_CACHE_ENTRY,
159 /**************************************************************************
160 * IStream_fnQueryInterface
162 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface,
163 REFIID riid,
164 LPVOID *ppvObj)
166 IUMCacheStream *This = (IUMCacheStream *)iface;
168 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
170 *ppvObj = NULL;
172 if(IsEqualIID(riid, &IID_IUnknown) ||
173 IsEqualIID(riid, &IID_IStream))
175 *ppvObj = This;
178 if(*ppvObj)
180 IStream_AddRef((IStream*)*ppvObj);
181 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
182 return S_OK;
184 TRACE("-- Interface: E_NOINTERFACE\n");
185 return E_NOINTERFACE;
188 /**************************************************************************
189 * IStream_fnAddRef
191 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
193 IUMCacheStream *This = (IUMCacheStream *)iface;
194 ULONG refCount = InterlockedIncrement(&This->ref);
196 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
198 return refCount;
201 /**************************************************************************
202 * IStream_fnRelease
204 static ULONG WINAPI IStream_fnRelease(IStream *iface)
206 IUMCacheStream *This = (IUMCacheStream *)iface;
207 ULONG refCount = InterlockedDecrement(&This->ref);
209 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
211 if (!refCount)
213 TRACE(" destroying UMCacheStream (%p)\n",This);
214 UMCloseCacheFileStream(This);
215 CloseHandle(This->handle);
216 HeapFree(GetProcessHeap(), 0, This->pszFileName);
217 HeapFree(GetProcessHeap(), 0, This->pszURL);
218 HeapFree(GetProcessHeap(),0,This);
220 return refCount;
223 static HRESULT WINAPI IStream_fnRead (IStream * iface,
224 void* pv,
225 ULONG cb,
226 ULONG* pcbRead)
228 ULONG dwBytesRead;
229 IUMCacheStream *This = (IUMCacheStream *)iface;
231 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
233 if ( !pv )
234 return STG_E_INVALIDPOINTER;
236 if ( !pcbRead)
237 pcbRead = &dwBytesRead;
239 if ( ! ReadFile( This->handle, pv, cb, (LPDWORD)pcbRead, NULL ) )
240 return S_FALSE;
242 if (!*pcbRead)
243 return This->closed ? S_FALSE : E_PENDING;
244 return S_OK;
247 static HRESULT WINAPI IStream_fnWrite (IStream * iface,
248 const void* pv,
249 ULONG cb,
250 ULONG* pcbWritten)
252 return E_NOTIMPL;
255 static HRESULT WINAPI IStream_fnSeek ( IStream * iface,
256 LARGE_INTEGER dlibMove,
257 DWORD dwOrigin,
258 ULARGE_INTEGER* plibNewPosition)
260 DWORD pos, newposlo;
261 LONG newposhi;
263 IUMCacheStream *This = (IUMCacheStream *)iface;
265 TRACE("(%p)\n",This);
267 pos = dlibMove.QuadPart; /* FIXME: truncates */
268 newposhi = 0;
269 newposlo = SetFilePointer( This->handle, pos, &newposhi, dwOrigin );
270 if( newposlo == INVALID_SET_FILE_POINTER && GetLastError())
271 return E_FAIL;
273 if (plibNewPosition)
274 plibNewPosition->QuadPart = newposlo | ( (LONGLONG)newposhi<<32);
276 return S_OK;
279 static HRESULT WINAPI IStream_fnSetSize (IStream * iface,
280 ULARGE_INTEGER libNewSize)
282 IUMCacheStream *This = (IUMCacheStream *)iface;
284 TRACE("(%p)\n",This);
286 if( ! SetFilePointer( This->handle, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
287 return E_FAIL;
289 if( ! SetEndOfFile( This->handle ) )
290 return E_FAIL;
292 return S_OK;
295 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface,
296 IStream* pstm,
297 ULARGE_INTEGER cb,
298 ULARGE_INTEGER* pcbRead,
299 ULARGE_INTEGER* pcbWritten)
301 IUMCacheStream *This = (IUMCacheStream *)iface;
303 TRACE("(%p)\n",This);
305 return E_NOTIMPL;
308 static HRESULT WINAPI IStream_fnCommit (IStream * iface,
309 DWORD grfCommitFlags)
311 IUMCacheStream *This = (IUMCacheStream *)iface;
313 TRACE("(%p)\n",This);
315 return E_NOTIMPL;
318 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
320 IUMCacheStream *This = (IUMCacheStream *)iface;
322 TRACE("(%p)\n",This);
324 return E_NOTIMPL;
326 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface,
327 ULARGE_INTEGER libOffset,
328 ULARGE_INTEGER cb,
329 DWORD dwLockType)
331 IUMCacheStream *This = (IUMCacheStream *)iface;
333 TRACE("(%p)\n",This);
335 return E_NOTIMPL;
337 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface,
338 ULARGE_INTEGER libOffset,
339 ULARGE_INTEGER cb,
340 DWORD dwLockType)
342 IUMCacheStream *This = (IUMCacheStream *)iface;
344 TRACE("(%p)\n",This);
346 return E_NOTIMPL;
348 static HRESULT WINAPI IStream_fnStat (IStream * iface,
349 STATSTG* pstatstg,
350 DWORD grfStatFlag)
352 IUMCacheStream *This = (IUMCacheStream *)iface;
354 TRACE("(%p)\n",This);
356 return E_NOTIMPL;
358 static HRESULT WINAPI IStream_fnClone (IStream * iface,
359 IStream** ppstm)
361 IUMCacheStream *This = (IUMCacheStream *)iface;
363 TRACE("(%p)\n",This);
365 return E_NOTIMPL;
368 static const IStreamVtbl stvt =
370 IStream_fnQueryInterface,
371 IStream_fnAddRef,
372 IStream_fnRelease,
373 IStream_fnRead,
374 IStream_fnWrite,
375 IStream_fnSeek,
376 IStream_fnSetSize,
377 IStream_fnCopyTo,
378 IStream_fnCommit,
379 IStream_fnRevert,
380 IStream_fnLockRegion,
381 IStream_fnUnlockRegion,
382 IStream_fnStat,
383 IStream_fnClone