ole32: Use the real proxy IID in ClientRpcChannelBuffer_GetBuffer().
[wine.git] / dlls / shlwapi / istream.c
bloba77ed8ab1b08178d52950b469b5ec3fe8bc6c762
1 /*
2 * SHLWAPI IStream functions
4 * Copyright 2002 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
21 #include <string.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "winnls.h"
30 #define NO_SHLWAPI_REG
31 #define NO_SHLWAPI_PATH
32 #include "shlwapi.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(shell);
37 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
38 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
39 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
41 /* Layout of ISHFileStream object */
42 typedef struct
44 IStream IStream_iface;
45 LONG ref;
46 HANDLE hFile;
47 DWORD dwMode;
48 LPOLESTR lpszPath;
49 DWORD type;
50 DWORD grfStateBits;
51 } ISHFileStream;
53 static inline ISHFileStream *impl_from_IStream(IStream *iface)
55 return CONTAINING_RECORD(iface, ISHFileStream, IStream_iface);
58 /**************************************************************************
59 * IStream_fnQueryInterface
61 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
63 ISHFileStream *This = impl_from_IStream(iface);
65 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppvObj);
67 *ppvObj = NULL;
69 if(IsEqualIID(riid, &IID_IUnknown) ||
70 IsEqualIID(riid, &IID_IStream))
72 IStream_AddRef(iface);
73 *ppvObj = iface;
74 return S_OK;
76 return E_NOINTERFACE;
79 /**************************************************************************
80 * IStream_fnAddRef
82 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
84 ISHFileStream *This = impl_from_IStream(iface);
85 ULONG refCount = InterlockedIncrement(&This->ref);
87 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
89 return refCount;
92 /**************************************************************************
93 * IStream_fnRelease
95 static ULONG WINAPI IStream_fnRelease(IStream *iface)
97 ISHFileStream *This = impl_from_IStream(iface);
98 ULONG refCount = InterlockedDecrement(&This->ref);
100 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
102 if (!refCount)
104 IStream_Commit(iface, 0); /* If ever buffered, this will be needed */
105 LocalFree(This->lpszPath);
106 CloseHandle(This->hFile);
107 HeapFree(GetProcessHeap(), 0, This);
110 return refCount;
113 /**************************************************************************
114 * IStream_fnRead
116 static HRESULT WINAPI IStream_fnRead(IStream *iface, void* pv, ULONG cb, ULONG* pcbRead)
118 ISHFileStream *This = impl_from_IStream(iface);
119 DWORD dwRead = 0;
121 TRACE("(%p,%p,%u,%p)\n", This, pv, cb, pcbRead);
123 if (!ReadFile(This->hFile, pv, cb, &dwRead, NULL))
125 WARN("error %d reading file\n", GetLastError());
126 return S_FALSE;
128 if (pcbRead)
129 *pcbRead = dwRead;
130 return dwRead == cb ? S_OK : S_FALSE;
133 /**************************************************************************
134 * IStream_fnWrite
136 static HRESULT WINAPI IStream_fnWrite(IStream *iface, const void* pv, ULONG cb, ULONG* pcbWritten)
138 ISHFileStream *This = impl_from_IStream(iface);
139 DWORD dwWritten = 0;
141 TRACE("(%p,%p,%u,%p)\n", This, pv, cb, pcbWritten);
143 switch (STGM_ACCESS_MODE(This->dwMode))
145 case STGM_WRITE:
146 case STGM_READWRITE:
147 break;
148 default:
149 return STG_E_ACCESSDENIED;
152 if (!WriteFile(This->hFile, pv, cb, &dwWritten, NULL))
153 return HRESULT_FROM_WIN32(GetLastError());
155 if (pcbWritten)
156 *pcbWritten = dwWritten;
157 return S_OK;
160 /**************************************************************************
161 * IStream_fnSeek
163 static HRESULT WINAPI IStream_fnSeek(IStream *iface, LARGE_INTEGER dlibMove,
164 DWORD dwOrigin, ULARGE_INTEGER* pNewPos)
166 ISHFileStream *This = impl_from_IStream(iface);
167 DWORD dwPos;
169 TRACE("(%p,%s,%d,%p)\n", This, wine_dbgstr_longlong(dlibMove.QuadPart), dwOrigin, pNewPos);
171 IStream_Commit(iface, 0); /* If ever buffered, this will be needed */
172 dwPos = SetFilePointer(This->hFile, dlibMove.u.LowPart, NULL, dwOrigin);
173 if( dwPos == INVALID_SET_FILE_POINTER )
174 return HRESULT_FROM_WIN32(GetLastError());
176 if (pNewPos)
178 pNewPos->u.HighPart = 0;
179 pNewPos->u.LowPart = dwPos;
181 return S_OK;
184 /**************************************************************************
185 * IStream_fnSetSize
187 static HRESULT WINAPI IStream_fnSetSize(IStream *iface, ULARGE_INTEGER libNewSize)
189 ISHFileStream *This = impl_from_IStream(iface);
191 TRACE("(%p,%s)\n", This, wine_dbgstr_longlong(libNewSize.QuadPart));
193 IStream_Commit(iface, 0); /* If ever buffered, this will be needed */
194 if( ! SetFilePointer( This->hFile, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
195 return E_FAIL;
197 if( ! SetEndOfFile( This->hFile ) )
198 return E_FAIL;
200 return S_OK;
203 /**************************************************************************
204 * IStream_fnCopyTo
206 static HRESULT WINAPI IStream_fnCopyTo(IStream *iface, IStream* pstm, ULARGE_INTEGER cb,
207 ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
209 ISHFileStream *This = impl_from_IStream(iface);
210 char copyBuff[1024];
211 ULONGLONG ulSize;
212 HRESULT hRet = S_OK;
214 TRACE("(%p,%p,%s,%p,%p)\n", This, pstm, wine_dbgstr_longlong(cb.QuadPart), pcbRead, pcbWritten);
216 if (pcbRead)
217 pcbRead->QuadPart = 0;
218 if (pcbWritten)
219 pcbWritten->QuadPart = 0;
221 if (!pstm)
222 return S_OK;
224 IStream_Commit(iface, 0); /* If ever buffered, this will be needed */
226 /* Copy data */
227 ulSize = cb.QuadPart;
228 while (ulSize)
230 ULONG ulLeft, ulRead, ulWritten;
232 ulLeft = ulSize > sizeof(copyBuff) ? sizeof(copyBuff) : ulSize;
234 /* Read */
235 hRet = IStream_Read(iface, copyBuff, ulLeft, &ulRead);
236 if (FAILED(hRet) || ulRead == 0)
237 break;
238 if (pcbRead)
239 pcbRead->QuadPart += ulRead;
241 /* Write */
242 hRet = IStream_Write(pstm, copyBuff, ulRead, &ulWritten);
243 if (pcbWritten)
244 pcbWritten->QuadPart += ulWritten;
245 if (FAILED(hRet) || ulWritten != ulLeft)
246 break;
248 ulSize -= ulLeft;
250 return hRet;
253 /**************************************************************************
254 * IStream_fnCommit
256 static HRESULT WINAPI IStream_fnCommit(IStream *iface, DWORD grfCommitFlags)
258 ISHFileStream *This = impl_from_IStream(iface);
260 TRACE("(%p,%d)\n", This, grfCommitFlags);
261 /* Currently unbuffered: This function is not needed */
262 return S_OK;
265 /**************************************************************************
266 * IStream_fnRevert
268 static HRESULT WINAPI IStream_fnRevert(IStream *iface)
270 ISHFileStream *This = impl_from_IStream(iface);
272 TRACE("(%p)\n", This);
273 return E_NOTIMPL;
276 /**************************************************************************
277 * IStream_fnLockRegion
279 static HRESULT WINAPI IStream_fnLockRegion(IStream *iface, ULARGE_INTEGER libOffset,
280 ULARGE_INTEGER cb, DWORD dwLockType)
282 ISHFileStream *This = impl_from_IStream(iface);
283 TRACE("(%p,%s,%s,%d)\n", This, wine_dbgstr_longlong(libOffset.QuadPart), wine_dbgstr_longlong(cb.QuadPart), dwLockType);
284 return E_NOTIMPL;
287 /**************************************************************************
288 * IStream_fnUnlockRegion
290 static HRESULT WINAPI IStream_fnUnlockRegion(IStream *iface, ULARGE_INTEGER libOffset,
291 ULARGE_INTEGER cb, DWORD dwLockType)
293 ISHFileStream *This = impl_from_IStream(iface);
294 TRACE("(%p,%s,%s,%d)\n", This, wine_dbgstr_longlong(libOffset.QuadPart), wine_dbgstr_longlong(cb.QuadPart), dwLockType);
295 return E_NOTIMPL;
298 /*************************************************************************
299 * IStream_fnStat
301 static HRESULT WINAPI IStream_fnStat(IStream *iface, STATSTG* lpStat,
302 DWORD grfStatFlag)
304 ISHFileStream *This = impl_from_IStream(iface);
305 BY_HANDLE_FILE_INFORMATION fi;
307 TRACE("(%p,%p,%d)\n", This, lpStat, grfStatFlag);
309 if (!lpStat)
310 return STG_E_INVALIDPOINTER;
312 memset(&fi, 0, sizeof(fi));
313 GetFileInformationByHandle(This->hFile, &fi);
315 if (grfStatFlag & STATFLAG_NONAME)
316 lpStat->pwcsName = NULL;
317 else
318 lpStat->pwcsName = StrDupW(This->lpszPath);
319 lpStat->type = This->type;
320 lpStat->cbSize.u.LowPart = fi.nFileSizeLow;
321 lpStat->cbSize.u.HighPart = fi.nFileSizeHigh;
322 lpStat->mtime = fi.ftLastWriteTime;
323 lpStat->ctime = fi.ftCreationTime;
324 lpStat->atime = fi.ftLastAccessTime;
325 lpStat->grfMode = This->dwMode;
326 lpStat->grfLocksSupported = 0;
327 memcpy(&lpStat->clsid, &IID_IStream, sizeof(CLSID));
328 lpStat->grfStateBits = This->grfStateBits;
329 lpStat->reserved = 0;
331 return S_OK;
334 /*************************************************************************
335 * IStream_fnClone
337 static HRESULT WINAPI IStream_fnClone(IStream *iface, IStream** ppstm)
339 ISHFileStream *This = impl_from_IStream(iface);
341 TRACE("(%p,%p)\n", This, ppstm);
342 if (ppstm)
343 *ppstm = NULL;
344 return E_NOTIMPL;
347 static const IStreamVtbl SHLWAPI_fsVTable =
349 IStream_fnQueryInterface,
350 IStream_fnAddRef,
351 IStream_fnRelease,
352 IStream_fnRead,
353 IStream_fnWrite,
354 IStream_fnSeek,
355 IStream_fnSetSize,
356 IStream_fnCopyTo,
357 IStream_fnCommit,
358 IStream_fnRevert,
359 IStream_fnLockRegion,
360 IStream_fnUnlockRegion,
361 IStream_fnStat,
362 IStream_fnClone
365 /**************************************************************************
366 * IStream_Create
368 * Internal helper: Create and initialise a new file stream object.
370 static IStream *IStream_Create(LPCWSTR lpszPath, HANDLE hFile, DWORD dwMode)
372 ISHFileStream *fileStream;
374 fileStream = HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream));
375 if (!fileStream) return NULL;
377 fileStream->IStream_iface.lpVtbl = &SHLWAPI_fsVTable;
378 fileStream->ref = 1;
379 fileStream->hFile = hFile;
380 fileStream->dwMode = dwMode;
381 fileStream->lpszPath = StrDupW(lpszPath);
382 fileStream->type = 0; /* FIXME */
383 fileStream->grfStateBits = 0; /* FIXME */
385 TRACE ("Returning %p\n", fileStream);
386 return &fileStream->IStream_iface;
389 /*************************************************************************
390 * SHCreateStreamOnFileEx [SHLWAPI.@]
392 * Create a stream on a file.
394 * PARAMS
395 * lpszPath [I] Path of file to create stream on
396 * dwMode [I] Mode to create stream in
397 * dwAttributes [I] Attributes of the file
398 * bCreate [I] Whether to create the file if it doesn't exist
399 * lpTemplate [I] Reserved, must be NULL
400 * lppStream [O] Destination for created stream
402 * RETURNS
403 * Success: S_OK. lppStream contains the new stream object
404 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
406 * NOTES
407 * This function is available in Unicode only.
409 HRESULT WINAPI SHCreateStreamOnFileEx(LPCWSTR lpszPath, DWORD dwMode,
410 DWORD dwAttributes, BOOL bCreate,
411 IStream *lpTemplate, IStream **lppStream)
413 DWORD dwAccess, dwShare, dwCreate;
414 HANDLE hFile;
416 TRACE("(%s,%d,0x%08X,%d,%p,%p)\n", debugstr_w(lpszPath), dwMode,
417 dwAttributes, bCreate, lpTemplate, lppStream);
419 if (!lpszPath || !lppStream || lpTemplate)
420 return E_INVALIDARG;
422 *lppStream = NULL;
424 /* Access */
425 switch (STGM_ACCESS_MODE(dwMode))
427 case STGM_WRITE:
428 case STGM_READWRITE:
429 dwAccess = GENERIC_READ|GENERIC_WRITE;
430 break;
431 case STGM_READ:
432 dwAccess = GENERIC_READ;
433 break;
434 default:
435 return E_INVALIDARG;
438 /* Sharing */
439 switch (STGM_SHARE_MODE(dwMode))
441 case 0:
442 case STGM_SHARE_DENY_NONE:
443 dwShare = FILE_SHARE_READ|FILE_SHARE_WRITE;
444 break;
445 case STGM_SHARE_DENY_READ:
446 dwShare = FILE_SHARE_WRITE;
447 break;
448 case STGM_SHARE_DENY_WRITE:
449 dwShare = FILE_SHARE_READ;
450 break;
451 case STGM_SHARE_EXCLUSIVE:
452 dwShare = 0;
453 break;
454 default:
455 return E_INVALIDARG;
458 switch(STGM_CREATE_MODE(dwMode))
460 case STGM_FAILIFTHERE:
461 dwCreate = bCreate ? CREATE_NEW : OPEN_EXISTING;
462 break;
463 case STGM_CREATE:
464 dwCreate = CREATE_ALWAYS;
465 break;
466 default:
467 return E_INVALIDARG;
470 /* Open HANDLE to file */
471 hFile = CreateFileW(lpszPath, dwAccess, dwShare, NULL, dwCreate,
472 dwAttributes, 0);
474 if(hFile == INVALID_HANDLE_VALUE)
475 return HRESULT_FROM_WIN32(GetLastError());
477 *lppStream = IStream_Create(lpszPath, hFile, dwMode);
479 if(!*lppStream)
481 CloseHandle(hFile);
482 return E_OUTOFMEMORY;
484 return S_OK;
487 /*************************************************************************
488 * SHCreateStreamOnFileW [SHLWAPI.@]
490 * See SHCreateStreamOnFileA.
492 HRESULT WINAPI SHCreateStreamOnFileW(LPCWSTR lpszPath, DWORD dwMode,
493 IStream **lppStream)
495 TRACE("(%s,%d,%p)\n", debugstr_w(lpszPath), dwMode, lppStream);
497 if (!lpszPath || !lppStream)
498 return E_INVALIDARG;
500 if ((dwMode & (STGM_CONVERT|STGM_DELETEONRELEASE|STGM_TRANSACTED)) != 0)
501 return E_INVALIDARG;
503 return SHCreateStreamOnFileEx(lpszPath, dwMode, 0, FALSE, NULL, lppStream);
506 /*************************************************************************
507 * SHCreateStreamOnFileA [SHLWAPI.@]
509 * Create a stream on a file.
511 * PARAMS
512 * lpszPath [I] Path of file to create stream on
513 * dwMode [I] Mode to create stream in
514 * lppStream [O] Destination for created IStream object
516 * RETURNS
517 * Success: S_OK. lppStream contains the new IStream object
518 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
520 HRESULT WINAPI SHCreateStreamOnFileA(LPCSTR lpszPath, DWORD dwMode,
521 IStream **lppStream)
523 WCHAR szPath[MAX_PATH];
525 TRACE("(%s,%d,%p)\n", debugstr_a(lpszPath), dwMode, lppStream);
527 if (!lpszPath)
528 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
530 MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, szPath, MAX_PATH);
531 return SHCreateStreamOnFileW(szPath, dwMode, lppStream);
534 /*************************************************************************
535 * @ [SHLWAPI.184]
537 * Call IStream_Read() on a stream.
539 * PARAMS
540 * lpStream [I] IStream object
541 * lpvDest [O] Destination for data read
542 * ulSize [I] Size of data to read
544 * RETURNS
545 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
546 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
547 * number of bytes read does not match.
549 HRESULT WINAPI SHIStream_Read(IStream *lpStream, LPVOID lpvDest, ULONG ulSize)
551 ULONG ulRead;
552 HRESULT hRet;
554 TRACE("(%p,%p,%u)\n", lpStream, lpvDest, ulSize);
556 hRet = IStream_Read(lpStream, lpvDest, ulSize, &ulRead);
558 if (SUCCEEDED(hRet) && ulRead != ulSize)
559 hRet = E_FAIL;
560 return hRet;
563 /*************************************************************************
564 * @ [SHLWAPI.166]
566 * Determine if a stream has 0 length.
568 * PARAMS
569 * lpStream [I] IStream object
571 * RETURNS
572 * TRUE: If the stream has 0 length
573 * FALSE: Otherwise.
575 BOOL WINAPI SHIsEmptyStream(IStream *lpStream)
577 STATSTG statstg;
578 BOOL bRet = TRUE;
580 TRACE("(%p)\n", lpStream);
582 memset(&statstg, 0, sizeof(statstg));
584 if(SUCCEEDED(IStream_Stat(lpStream, &statstg, 1)))
586 if(statstg.cbSize.QuadPart)
587 bRet = FALSE; /* Non-Zero */
589 else
591 DWORD dwDummy;
593 /* Try to read from the stream */
594 if(SUCCEEDED(SHIStream_Read(lpStream, &dwDummy, sizeof(dwDummy))))
596 LARGE_INTEGER zero;
597 zero.QuadPart = 0;
599 IStream_Seek(lpStream, zero, 0, NULL);
600 bRet = FALSE; /* Non-Zero */
603 return bRet;
606 /*************************************************************************
607 * @ [SHLWAPI.212]
609 * Call IStream_Write() on a stream.
611 * PARAMS
612 * lpStream [I] IStream object
613 * lpvSrc [I] Source for data to write
614 * ulSize [I] Size of data
616 * RETURNS
617 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
618 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
619 * number of bytes written does not match.
621 HRESULT WINAPI SHIStream_Write(IStream *lpStream, LPCVOID lpvSrc, ULONG ulSize)
623 ULONG ulWritten;
624 HRESULT hRet;
626 TRACE("(%p,%p,%d)\n", lpStream, lpvSrc, ulSize);
628 hRet = IStream_Write(lpStream, lpvSrc, ulSize, &ulWritten);
630 if (SUCCEEDED(hRet) && ulWritten != ulSize)
631 hRet = E_FAIL;
633 return hRet;
636 /*************************************************************************
637 * @ [SHLWAPI.213]
639 * Seek to the start of a stream.
641 * PARAMS
642 * lpStream [I] IStream object
644 * RETURNS
645 * Success: S_OK. The current position within the stream is updated
646 * Failure: An HRESULT error code.
648 HRESULT WINAPI IStream_Reset(IStream *lpStream)
650 LARGE_INTEGER zero;
651 TRACE("(%p)\n", lpStream);
652 zero.QuadPart = 0;
653 return IStream_Seek(lpStream, zero, 0, NULL);
656 /*************************************************************************
657 * @ [SHLWAPI.214]
659 * Get the size of a stream.
661 * PARAMS
662 * lpStream [I] IStream object
663 * lpulSize [O] Destination for size
665 * RETURNS
666 * Success: S_OK. lpulSize contains the size of the stream.
667 * Failure: An HRESULT error code.
669 HRESULT WINAPI IStream_Size(IStream *lpStream, ULARGE_INTEGER* lpulSize)
671 STATSTG statstg;
672 HRESULT hRet;
674 TRACE("(%p,%p)\n", lpStream, lpulSize);
676 memset(&statstg, 0, sizeof(statstg));
678 hRet = IStream_Stat(lpStream, &statstg, 1);
680 if (SUCCEEDED(hRet) && lpulSize)
681 *lpulSize = statstg.cbSize;
682 return hRet;