push bc3e5bf5ba806943a97979264a1f2e4a4dde5c94
[wine/hacks.git] / dlls / shlwapi / istream.c
blobff93b77621f9fb9520a381f74cdc0bc2e2707f92
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
25 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "winnls.h"
31 #define NO_SHLWAPI_REG
32 #define NO_SHLWAPI_PATH
33 #include "shlwapi.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
39 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
40 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
42 /* Layout of ISHFileStream object */
43 typedef struct
45 const IStreamVtbl *lpVtbl;
46 LONG ref;
47 HANDLE hFile;
48 DWORD dwMode;
49 LPOLESTR lpszPath;
50 DWORD type;
51 DWORD grfStateBits;
52 } ISHFileStream;
54 static HRESULT WINAPI IStream_fnCommit(IStream*,DWORD);
57 /**************************************************************************
58 * IStream_fnQueryInterface
60 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
62 ISHFileStream *This = (ISHFileStream *)iface;
64 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppvObj);
66 *ppvObj = NULL;
68 if(IsEqualIID(riid, &IID_IUnknown) ||
69 IsEqualIID(riid, &IID_IStream))
71 *ppvObj = This;
72 IStream_AddRef(iface);
73 return S_OK;
75 return E_NOINTERFACE;
78 /**************************************************************************
79 * IStream_fnAddRef
81 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
83 ISHFileStream *This = (ISHFileStream *)iface;
84 ULONG refCount = InterlockedIncrement(&This->ref);
86 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
88 return refCount;
91 /**************************************************************************
92 * IStream_fnRelease
94 static ULONG WINAPI IStream_fnRelease(IStream *iface)
96 ISHFileStream *This = (ISHFileStream *)iface;
97 ULONG refCount = InterlockedDecrement(&This->ref);
99 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
101 if (!refCount)
103 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
104 LocalFree((HLOCAL)This->lpszPath);
105 CloseHandle(This->hFile);
106 HeapFree(GetProcessHeap(), 0, This);
109 return refCount;
112 /**************************************************************************
113 * IStream_fnRead
115 static HRESULT WINAPI IStream_fnRead(IStream *iface, void* pv, ULONG cb, ULONG* pcbRead)
117 ISHFileStream *This = (ISHFileStream *)iface;
118 DWORD dwRead = 0;
120 TRACE("(%p,%p,0x%08x,%p)\n", This, pv, cb, pcbRead);
122 if (!pv)
123 return STG_E_INVALIDPOINTER;
125 if (!ReadFile(This->hFile, pv, cb, &dwRead, NULL))
127 ERR("error %d reading file\n", GetLastError());
128 return HRESULT_FROM_WIN32(GetLastError());
130 if (pcbRead)
131 *pcbRead = dwRead;
132 return S_OK;
135 /**************************************************************************
136 * IStream_fnWrite
138 static HRESULT WINAPI IStream_fnWrite(IStream *iface, const void* pv, ULONG cb, ULONG* pcbWritten)
140 ISHFileStream *This = (ISHFileStream *)iface;
141 DWORD dwWritten = 0;
143 TRACE("(%p,%p,0x%08x,%p)\n", This, pv, cb, pcbWritten);
145 if (!pv)
146 return STG_E_INVALIDPOINTER;
148 switch (STGM_ACCESS_MODE(This->dwMode))
150 case STGM_WRITE:
151 case STGM_READWRITE:
152 break;
153 default:
154 return E_FAIL;
157 if (!WriteFile(This->hFile, pv, cb, &dwWritten, NULL))
158 return HRESULT_FROM_WIN32(GetLastError());
160 if (pcbWritten)
161 *pcbWritten = dwWritten;
162 return S_OK;
165 /**************************************************************************
166 * IStream_fnSeek
168 static HRESULT WINAPI IStream_fnSeek(IStream *iface, LARGE_INTEGER dlibMove,
169 DWORD dwOrigin, ULARGE_INTEGER* pNewPos)
171 ISHFileStream *This = (ISHFileStream *)iface;
172 DWORD dwPos;
174 TRACE("(%p,%d,%d,%p)\n", This, dlibMove.u.LowPart, dwOrigin, pNewPos);
176 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
177 dwPos = SetFilePointer(This->hFile, dlibMove.u.LowPart, NULL, dwOrigin);
178 if( dwPos == INVALID_SET_FILE_POINTER )
179 return E_FAIL;
181 if (pNewPos)
183 pNewPos->u.HighPart = 0;
184 pNewPos->u.LowPart = dwPos;
186 return S_OK;
189 /**************************************************************************
190 * IStream_fnSetSize
192 static HRESULT WINAPI IStream_fnSetSize(IStream *iface, ULARGE_INTEGER libNewSize)
194 ISHFileStream *This = (ISHFileStream *)iface;
196 TRACE("(%p,%d)\n", This, libNewSize.u.LowPart);
198 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
199 if( ! SetFilePointer( This->hFile, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
200 return E_FAIL;
202 if( ! SetEndOfFile( This->hFile ) )
203 return E_FAIL;
205 return S_OK;
208 /**************************************************************************
209 * IStream_fnCopyTo
211 static HRESULT WINAPI IStream_fnCopyTo(IStream *iface, IStream* pstm, ULARGE_INTEGER cb,
212 ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
214 ISHFileStream *This = (ISHFileStream *)iface;
215 char copyBuff[1024];
216 ULONGLONG ulSize;
217 HRESULT hRet = S_OK;
219 TRACE("(%p,%p,%d,%p,%p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
221 if (pcbRead)
222 pcbRead->QuadPart = 0;
223 if (pcbWritten)
224 pcbWritten->QuadPart = 0;
226 if (!pstm)
227 return STG_E_INVALIDPOINTER;
229 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
231 /* Copy data */
232 ulSize = cb.QuadPart;
233 while (ulSize)
235 ULONG ulLeft, ulAmt;
237 ulLeft = ulSize > sizeof(copyBuff) ? sizeof(copyBuff) : ulSize;
239 /* Read */
240 hRet = IStream_fnRead(iface, copyBuff, ulLeft, &ulAmt);
241 if (pcbRead)
242 pcbRead->QuadPart += ulAmt;
243 if (FAILED(hRet) || ulAmt != ulLeft)
244 break;
246 /* Write */
247 hRet = IStream_fnWrite(pstm, copyBuff, ulLeft, &ulAmt);
248 if (pcbWritten)
249 pcbWritten->QuadPart += ulAmt;
250 if (FAILED(hRet) || ulAmt != ulLeft)
251 break;
253 ulSize -= ulLeft;
255 return hRet;
258 /**************************************************************************
259 * IStream_fnCommit
261 static HRESULT WINAPI IStream_fnCommit(IStream *iface, DWORD grfCommitFlags)
263 ISHFileStream *This = (ISHFileStream *)iface;
265 TRACE("(%p,%d)\n", This, grfCommitFlags);
266 /* Currently unbuffered: This function is not needed */
267 return S_OK;
270 /**************************************************************************
271 * IStream_fnRevert
273 static HRESULT WINAPI IStream_fnRevert(IStream *iface)
275 ISHFileStream *This = (ISHFileStream *)iface;
277 TRACE("(%p)\n", This);
278 return E_NOTIMPL;
281 /**************************************************************************
282 * IStream_fnLockUnlockRegion
284 static HRESULT WINAPI IStream_fnLockUnlockRegion(IStream *iface, ULARGE_INTEGER libOffset,
285 ULARGE_INTEGER cb, DWORD dwLockType)
287 ISHFileStream *This = (ISHFileStream *)iface;
288 TRACE("(%p,%d,%d,%d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
289 return E_NOTIMPL;
292 /*************************************************************************
293 * IStream_fnStat
295 static HRESULT WINAPI IStream_fnStat(IStream *iface, STATSTG* lpStat,
296 DWORD grfStatFlag)
298 ISHFileStream *This = (ISHFileStream *)iface;
299 BY_HANDLE_FILE_INFORMATION fi;
300 HRESULT hRet = S_OK;
302 TRACE("(%p,%p,%d)\n", This, lpStat, grfStatFlag);
304 if (!grfStatFlag)
305 hRet = STG_E_INVALIDPOINTER;
306 else
308 memset(&fi, 0, sizeof(fi));
309 GetFileInformationByHandle(This->hFile, &fi);
311 if (grfStatFlag & STATFLAG_NONAME)
312 lpStat->pwcsName = NULL;
313 else
314 lpStat->pwcsName = StrDupW(This->lpszPath);
315 lpStat->type = This->type;
316 lpStat->cbSize.u.LowPart = fi.nFileSizeLow;
317 lpStat->cbSize.u.HighPart = fi.nFileSizeHigh;
318 lpStat->mtime = fi.ftLastWriteTime;
319 lpStat->ctime = fi.ftCreationTime;
320 lpStat->atime = fi.ftLastAccessTime;
321 lpStat->grfMode = This->dwMode;
322 lpStat->grfLocksSupported = 0;
323 memcpy(&lpStat->clsid, &IID_IStream, sizeof(CLSID));
324 lpStat->grfStateBits = This->grfStateBits;
325 lpStat->reserved = 0;
327 return hRet;
330 /*************************************************************************
331 * IStream_fnClone
333 static HRESULT WINAPI IStream_fnClone(IStream *iface, IStream** ppstm)
335 ISHFileStream *This = (ISHFileStream *)iface;
337 TRACE("(%p)\n",This);
338 if (ppstm)
339 *ppstm = NULL;
340 return E_NOTIMPL;
343 static const IStreamVtbl SHLWAPI_fsVTable =
345 IStream_fnQueryInterface,
346 IStream_fnAddRef,
347 IStream_fnRelease,
348 IStream_fnRead,
349 IStream_fnWrite,
350 IStream_fnSeek,
351 IStream_fnSetSize,
352 IStream_fnCopyTo,
353 IStream_fnCommit,
354 IStream_fnRevert,
355 IStream_fnLockUnlockRegion,
356 IStream_fnLockUnlockRegion,
357 IStream_fnStat,
358 IStream_fnClone
361 /**************************************************************************
362 * IStream_Create
364 * Internal helper: Create and initialise a new file stream object.
366 static IStream *IStream_Create(LPCWSTR lpszPath, HANDLE hFile, DWORD dwMode)
368 ISHFileStream* fileStream;
370 fileStream = HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream));
372 if (fileStream)
374 fileStream->lpVtbl = &SHLWAPI_fsVTable;
375 fileStream->ref = 1;
376 fileStream->hFile = hFile;
377 fileStream->dwMode = dwMode;
378 fileStream->lpszPath = StrDupW(lpszPath);
379 fileStream->type = 0; /* FIXME */
380 fileStream->grfStateBits = 0; /* FIXME */
382 TRACE ("Returning %p\n", fileStream);
383 return (IStream *)fileStream;
386 /*************************************************************************
387 * SHCreateStreamOnFileEx [SHLWAPI.@]
389 * Create a stream on a file.
391 * PARAMS
392 * lpszPath [I] Path of file to create stream on
393 * dwMode [I] Mode to create stream in
394 * dwAttributes [I] Attributes of the file
395 * bCreate [I] Whether to create the file if it doesn't exist
396 * lpTemplate [I] Reserved, must be NULL
397 * lppStream [O] Destination for created stream
399 * RETURNS
400 * Success: S_OK. lppStream contains the new stream object
401 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
403 * NOTES
404 * This function is available in Unicode only.
406 HRESULT WINAPI SHCreateStreamOnFileEx(LPCWSTR lpszPath, DWORD dwMode,
407 DWORD dwAttributes, BOOL bCreate,
408 IStream *lpTemplate, IStream **lppStream)
410 DWORD dwAccess, dwShare, dwCreate;
411 HANDLE hFile;
413 TRACE("(%s,%d,0x%08X,%d,%p,%p)\n", debugstr_w(lpszPath), dwMode,
414 dwAttributes, bCreate, lpTemplate, lppStream);
416 if (!lpszPath || !lppStream || lpTemplate)
417 return E_INVALIDARG;
419 *lppStream = NULL;
421 if (dwMode & STGM_TRANSACTED)
422 return E_INVALIDARG;
424 /* Access */
425 switch (STGM_ACCESS_MODE(dwMode))
427 case STGM_READWRITE:
428 dwAccess = GENERIC_READ|GENERIC_WRITE;
429 break;
430 case STGM_WRITE:
431 dwAccess = GENERIC_WRITE;
432 break;
433 case STGM_READ:
434 dwAccess = GENERIC_READ;
435 break;
436 default:
437 return E_INVALIDARG;
440 /* Sharing */
441 switch (STGM_SHARE_MODE(dwMode))
443 case STGM_SHARE_DENY_READ:
444 dwShare = FILE_SHARE_WRITE;
445 break;
446 case STGM_SHARE_DENY_WRITE:
447 dwShare = FILE_SHARE_READ;
448 break;
449 case STGM_SHARE_EXCLUSIVE:
450 dwShare = 0;
451 break;
452 case STGM_SHARE_DENY_NONE:
453 dwShare = FILE_SHARE_READ|FILE_SHARE_WRITE;
454 break;
455 default:
456 return E_INVALIDARG;
459 switch(STGM_CREATE_MODE(dwMode))
461 case STGM_FAILIFTHERE:
462 dwCreate = OPEN_EXISTING;
463 break;
464 case STGM_CREATE:
465 dwCreate = CREATE_ALWAYS;
466 break;
467 default:
468 return E_INVALIDARG;
471 /* Open HANDLE to file */
472 hFile = CreateFileW(lpszPath, dwAccess, dwShare, NULL, dwCreate,
473 dwAttributes, 0);
475 if(hFile == INVALID_HANDLE_VALUE)
477 HRESULT hRet = (HRESULT)GetLastError();
478 if(hRet > 0)
479 hRet = HRESULT_FROM_WIN32(hRet);
480 return hRet;
483 *lppStream = IStream_Create(lpszPath, hFile, dwMode);
485 if(!*lppStream)
487 CloseHandle(hFile);
488 return E_OUTOFMEMORY;
490 return S_OK;
493 /*************************************************************************
494 * SHCreateStreamOnFileW [SHLWAPI.@]
496 * See SHCreateStreamOnFileA.
498 HRESULT WINAPI SHCreateStreamOnFileW(LPCWSTR lpszPath, DWORD dwMode,
499 IStream **lppStream)
501 TRACE("(%s,%d,%p)\n", debugstr_w(lpszPath), dwMode, lppStream);
503 if (!lpszPath || !lppStream)
504 return E_INVALIDARG;
506 return SHCreateStreamOnFileEx(lpszPath, dwMode, 0, FALSE, NULL, lppStream);
509 /*************************************************************************
510 * SHCreateStreamOnFileA [SHLWAPI.@]
512 * Create a stream on a file.
514 * PARAMS
515 * lpszPath [I] Path of file to create stream on
516 * dwMode [I] Mode to create stream in
517 * lppStream [O] Destination for created IStream object
519 * RETURNS
520 * Success: S_OK. lppStream contains the new IStream object
521 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
523 HRESULT WINAPI SHCreateStreamOnFileA(LPCSTR lpszPath, DWORD dwMode,
524 IStream **lppStream)
526 WCHAR szPath[MAX_PATH];
528 TRACE("(%s,%d,%p)\n", debugstr_a(lpszPath), dwMode, lppStream);
530 if (!lpszPath)
531 return E_INVALIDARG;
532 MultiByteToWideChar(0, 0, lpszPath, -1, szPath, MAX_PATH);
533 return SHCreateStreamOnFileW(szPath, dwMode, lppStream);
536 /*************************************************************************
537 * @ [SHLWAPI.184]
539 * Call IStream_Read() on a stream.
541 * PARAMS
542 * lpStream [I] IStream object
543 * lpvDest [O] Destination for data read
544 * ulSize [I] Size of data to read
546 * RETURNS
547 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
548 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
549 * number of bytes read does not match.
551 HRESULT WINAPI SHLWAPI_184(IStream *lpStream, LPVOID lpvDest, ULONG ulSize)
553 ULONG ulRead;
554 HRESULT hRet;
556 TRACE("(%p,%p,%d)\n", lpStream, lpvDest, ulSize);
558 hRet = IStream_Read(lpStream, lpvDest, ulSize, &ulRead);
560 if (SUCCEEDED(hRet) && ulRead != ulSize)
561 hRet = E_FAIL;
562 return hRet;
565 /*************************************************************************
566 * @ [SHLWAPI.166]
568 * Determine if a stream has 0 length.
570 * PARAMS
571 * lpStream [I] IStream object
573 * RETURNS
574 * TRUE: If the stream has 0 length
575 * FALSE: Otherwise.
577 BOOL WINAPI SHIsEmptyStream(IStream *lpStream)
579 STATSTG statstg;
580 BOOL bRet = TRUE;
582 TRACE("(%p)\n", lpStream);
584 memset(&statstg, 0, sizeof(statstg));
586 if(SUCCEEDED(IStream_Stat(lpStream, &statstg, 1)))
588 if(statstg.cbSize.QuadPart)
589 bRet = FALSE; /* Non-Zero */
591 else
593 DWORD dwDummy;
595 /* Try to read from the stream */
596 if(SUCCEEDED(SHLWAPI_184(lpStream, &dwDummy, sizeof(dwDummy))))
598 LARGE_INTEGER zero;
599 zero.QuadPart = 0;
601 IStream_Seek(lpStream, zero, 0, NULL);
602 bRet = FALSE; /* Non-Zero */
605 return bRet;
608 /*************************************************************************
609 * @ [SHLWAPI.212]
611 * Call IStream_Write() on a stream.
613 * PARAMS
614 * lpStream [I] IStream object
615 * lpvSrc [I] Source for data to write
616 * ulSize [I] Size of data
618 * RETURNS
619 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
620 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
621 * number of bytes written does not match.
623 HRESULT WINAPI SHLWAPI_212(IStream *lpStream, LPCVOID lpvSrc, ULONG ulSize)
625 ULONG ulWritten;
626 HRESULT hRet;
628 TRACE("(%p,%p,%d)\n", lpStream, lpvSrc, ulSize);
630 hRet = IStream_Write(lpStream, lpvSrc, ulSize, &ulWritten);
632 if (SUCCEEDED(hRet) && ulWritten != ulSize)
633 hRet = E_FAIL;
635 return hRet;
638 /*************************************************************************
639 * @ [SHLWAPI.213]
641 * Seek to the start of a stream.
643 * PARAMS
644 * lpStream [I] IStream object
646 * RETURNS
647 * Success: S_OK. The current position within the stream is updated
648 * Failure: An HRESULT error code.
650 HRESULT WINAPI IStream_Reset(IStream *lpStream)
652 LARGE_INTEGER zero;
653 TRACE("(%p)\n", lpStream);
654 zero.QuadPart = 0;
655 return IStream_Seek(lpStream, zero, 0, NULL);
658 /*************************************************************************
659 * @ [SHLWAPI.214]
661 * Get the size of a stream.
663 * PARAMS
664 * lpStream [I] IStream object
665 * lpulSize [O] Destination for size
667 * RETURNS
668 * Success: S_OK. lpulSize contains the size of the stream.
669 * Failure: An HRESULT error code.
671 HRESULT WINAPI IStream_Size(IStream *lpStream, ULARGE_INTEGER* lpulSize)
673 STATSTG statstg;
674 HRESULT hRet;
676 TRACE("(%p,%p)\n", lpStream, lpulSize);
678 memset(&statstg, 0, sizeof(statstg));
680 hRet = IStream_Stat(lpStream, &statstg, 1);
682 if (SUCCEEDED(hRet) && lpulSize)
683 *lpulSize = statstg.cbSize;
684 return hRet;