Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / shlwapi / istream.c
blobc92d8d1c7475c148ce9240781fb69969efc0aef6
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdarg.h>
21 #include <string.h>
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "winnls.h"
29 #define NO_SHLWAPI_REG
30 #define NO_SHLWAPI_PATH
31 #include "shlwapi.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
36 /* Layout of ISHFileStream object */
37 typedef struct
39 ICOM_VFIELD(IStream);
40 ULONG ref;
41 HANDLE hFile;
42 DWORD dwMode;
43 LPOLESTR lpszPath;
44 DWORD type;
45 DWORD grfStateBits;
46 } ISHFileStream;
48 static HRESULT WINAPI IStream_fnCommit(IStream*,DWORD);
51 /**************************************************************************
52 * IStream_fnQueryInterface
54 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
56 ICOM_THIS(ISHFileStream, iface);
58 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppvObj);
60 *ppvObj = NULL;
62 if(IsEqualIID(riid, &IID_IUnknown) ||
63 IsEqualIID(riid, &IID_IStream))
65 *ppvObj = This;
67 IStream_AddRef((IStream*)*ppvObj);
68 return S_OK;
70 return E_NOINTERFACE;
73 /**************************************************************************
74 * IStream_fnAddRef
76 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
78 ICOM_THIS(ISHFileStream, iface);
80 TRACE("(%p)\n", This);
81 return InterlockedIncrement(&This->ref);
84 /**************************************************************************
85 * IStream_fnRelease
87 static ULONG WINAPI IStream_fnRelease(IStream *iface)
89 ICOM_THIS(ISHFileStream, iface);
90 ULONG ulRet;
92 TRACE("(%p)\n", This);
94 if (!(ulRet = InterlockedDecrement(&This->ref)))
96 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
97 LocalFree((HLOCAL)This->lpszPath);
98 CloseHandle(This->hFile);
99 HeapFree(GetProcessHeap(), 0, This);
101 return ulRet;
104 /**************************************************************************
105 * IStream_fnRead
107 static HRESULT WINAPI IStream_fnRead(IStream *iface, void* pv, ULONG cb, ULONG* pcbRead)
109 ICOM_THIS(ISHFileStream, iface);
110 HRESULT hRet = S_OK;
111 DWORD dwRead = 0;
113 TRACE("(%p,%p,0x%08lx,%p)\n", This, pv, cb, pcbRead);
115 if (!pv)
116 hRet = STG_E_INVALIDPOINTER;
117 else if (!ReadFile(This->hFile, pv, cb, &dwRead, NULL))
119 hRet = (HRESULT)GetLastError();
120 if(hRet > 0)
121 hRet = HRESULT_FROM_WIN32(hRet);
123 if (pcbRead)
124 *pcbRead = dwRead;
125 return hRet;
128 /**************************************************************************
129 * IStream_fnWrite
131 static HRESULT WINAPI IStream_fnWrite(IStream *iface, const void* pv, ULONG cb, ULONG* pcbWritten)
133 ICOM_THIS(ISHFileStream, iface);
134 HRESULT hRet = S_OK;
135 DWORD dwWritten = 0;
137 TRACE("(%p,%p,0x%08lx,%p)\n", This, pv, cb, pcbWritten);
139 if (!pv)
140 hRet = STG_E_INVALIDPOINTER;
141 else if (!(This->dwMode & STGM_WRITE))
142 hRet = E_FAIL;
143 else if (!WriteFile(This->hFile, pv, cb, &dwWritten, NULL))
145 hRet = (HRESULT)GetLastError();
146 if(hRet > 0)
147 hRet = HRESULT_FROM_WIN32(hRet);
149 if (pcbWritten)
150 *pcbWritten = dwWritten;
151 return hRet;
154 /**************************************************************************
155 * IStream_fnSeek
157 static HRESULT WINAPI IStream_fnSeek(IStream *iface, LARGE_INTEGER dlibMove,
158 DWORD dwOrigin, ULARGE_INTEGER* pNewPos)
160 ICOM_THIS(ISHFileStream, iface);
161 DWORD dwPos;
163 TRACE("(%p,%ld,%ld,%p)\n", This, dlibMove.s.LowPart, dwOrigin, pNewPos);
165 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
166 dwPos = SetFilePointer(This->hFile, dlibMove.s.LowPart, NULL, dwOrigin);
168 if (pNewPos)
170 pNewPos->s.HighPart = 0;
171 pNewPos->s.LowPart = dwPos;
173 return S_OK;
176 /**************************************************************************
177 * IStream_fnSetSize
179 static HRESULT WINAPI IStream_fnSetSize(IStream *iface, ULARGE_INTEGER libNewSize)
181 ICOM_THIS(ISHFileStream, iface);
183 TRACE("(%p,%ld)\n", This, libNewSize.s.LowPart);
184 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
185 return E_NOTIMPL;
188 /**************************************************************************
189 * IStream_fnCopyTo
191 static HRESULT WINAPI IStream_fnCopyTo(IStream *iface, IStream* pstm, ULARGE_INTEGER cb,
192 ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
194 ICOM_THIS(ISHFileStream, iface);
195 char copyBuff[1024];
196 ULONGLONG ulSize;
197 HRESULT hRet = S_OK;
199 TRACE("(%p,%p,%ld,%p,%p)\n", This, pstm, cb.s.LowPart, pcbRead, pcbWritten);
201 if (pcbRead)
202 pcbRead->QuadPart = 0;
203 if (pcbWritten)
204 pcbWritten->QuadPart = 0;
206 if (!pstm)
207 return STG_E_INVALIDPOINTER;
209 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
211 /* Copy data */
212 ulSize = cb.QuadPart;
213 while (ulSize)
215 ULONG ulLeft, ulAmt;
217 ulLeft = ulSize > sizeof(copyBuff) ? sizeof(copyBuff) : ulSize;
219 /* Read */
220 hRet = IStream_fnRead(iface, copyBuff, ulLeft, &ulAmt);
221 if (pcbRead)
222 pcbRead->QuadPart += ulAmt;
223 if (FAILED(hRet) || ulAmt != ulLeft)
224 break;
226 /* Write */
227 hRet = IStream_fnWrite(pstm, copyBuff, ulLeft, &ulAmt);
228 if (pcbWritten)
229 pcbWritten->QuadPart += ulAmt;
230 if (FAILED(hRet) || ulAmt != ulLeft)
231 break;
233 ulSize -= ulLeft;
235 return hRet;
238 /**************************************************************************
239 * IStream_fnCommit
241 static HRESULT WINAPI IStream_fnCommit(IStream *iface, DWORD grfCommitFlags)
243 ICOM_THIS(ISHFileStream, iface);
245 TRACE("(%p,%ld)\n", This, grfCommitFlags);
246 /* Currently unbuffered: This function is not needed */
247 return S_OK;
250 /**************************************************************************
251 * IStream_fnRevert
253 static HRESULT WINAPI IStream_fnRevert(IStream *iface)
255 ICOM_THIS(ISHFileStream, iface);
257 TRACE("(%p)\n", This);
258 return E_NOTIMPL;
261 /**************************************************************************
262 * IStream_fnLockUnlockRegion
264 static HRESULT WINAPI IStream_fnLockUnlockRegion(IStream *iface, ULARGE_INTEGER libOffset,
265 ULARGE_INTEGER cb, DWORD dwLockType)
267 ICOM_THIS(ISHFileStream, iface);
268 TRACE("(%p,%ld,%ld,%ld)\n", This, libOffset.s.LowPart, cb.s.LowPart, dwLockType);
269 return E_NOTIMPL;
272 /*************************************************************************
273 * IStream_fnStat
275 static HRESULT WINAPI IStream_fnStat(IStream *iface, STATSTG* lpStat,
276 DWORD grfStatFlag)
278 ICOM_THIS(ISHFileStream, iface);
279 BY_HANDLE_FILE_INFORMATION fi;
280 HRESULT hRet = S_OK;
282 TRACE("(%p,%p,%ld)\n", This, lpStat, grfStatFlag);
284 if (!grfStatFlag)
285 hRet = STG_E_INVALIDPOINTER;
286 else
288 memset(&fi, 0, sizeof(fi));
289 GetFileInformationByHandle(This->hFile, &fi);
291 if (grfStatFlag & STATFLAG_NONAME)
292 lpStat->pwcsName = NULL;
293 else
294 lpStat->pwcsName = StrDupW(This->lpszPath);
295 lpStat->type = This->type;
296 lpStat->cbSize.s.LowPart = fi.nFileSizeLow;
297 lpStat->cbSize.s.HighPart = fi.nFileSizeHigh;
298 lpStat->mtime = fi.ftLastWriteTime;
299 lpStat->ctime = fi.ftCreationTime;
300 lpStat->atime = fi.ftLastAccessTime;
301 lpStat->grfMode = This->dwMode;
302 lpStat->grfLocksSupported = 0;
303 memcpy(&lpStat->clsid, &IID_IStream, sizeof(CLSID));
304 lpStat->grfStateBits = This->grfStateBits;
305 lpStat->reserved = 0;
307 return hRet;
310 /*************************************************************************
311 * IStream_fnClone
313 static HRESULT WINAPI IStream_fnClone(IStream *iface, IStream** ppstm)
315 ICOM_THIS(ISHFileStream, iface);
317 TRACE("(%p)\n",This);
318 if (ppstm)
319 *ppstm = NULL;
320 return E_NOTIMPL;
323 static struct ICOM_VTABLE(IStream) SHLWAPI_fsVTable =
325 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
326 IStream_fnQueryInterface,
327 IStream_fnAddRef,
328 IStream_fnRelease,
329 IStream_fnRead,
330 IStream_fnWrite,
331 IStream_fnSeek,
332 IStream_fnSetSize,
333 IStream_fnCopyTo,
334 IStream_fnCommit,
335 IStream_fnRevert,
336 IStream_fnLockUnlockRegion,
337 IStream_fnLockUnlockRegion,
338 IStream_fnStat,
339 IStream_fnClone
342 /**************************************************************************
343 * IStream_Create
345 * Internal helper: Create and initialise a new file stream object.
347 static IStream *IStream_Create(LPCWSTR lpszPath, HANDLE hFile, DWORD dwMode)
349 ISHFileStream* fileStream;
351 fileStream = (ISHFileStream*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream));
353 if (fileStream)
355 fileStream->lpVtbl = &SHLWAPI_fsVTable;
356 fileStream->ref = 1;
357 fileStream->hFile = hFile;
358 fileStream->dwMode = dwMode;
359 fileStream->lpszPath = StrDupW(lpszPath);
360 fileStream->type = 0; /* FIXME */
361 fileStream->grfStateBits = 0; /* FIXME */
363 TRACE ("Returning %p\n", fileStream);
364 return (IStream *)fileStream;
367 /*************************************************************************
368 * SHCreateStreamOnFileEx [SHLWAPI.@]
370 * Create a stream on a file.
372 * PARAMS
373 * lpszPath [I] Path of file to create stream on
374 * dwMode [I] Mode to create stream in
375 * dwAttributes [I] Attributes of the file
376 * bCreate [I] Whether to create the file if it doesn't exist
377 * lpTemplate [I] Reserved, must be NULL
378 * lppStream [O] Destination for created stream
380 * RETURNS
381 * Success: S_OK. lppStream contains the new stream object
382 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
384 * NOTES
385 * This function is available in Unicode only.
387 HRESULT WINAPI SHCreateStreamOnFileEx(LPCWSTR lpszPath, DWORD dwMode,
388 DWORD dwAttributes, BOOL bCreate,
389 IStream *lpTemplate, IStream **lppStream)
391 DWORD dwAccess, dwShare, dwCreate;
392 HANDLE hFile;
394 TRACE("(%s,%ld,0x%08lX,%d,%p,%p)\n", debugstr_w(lpszPath), dwMode,
395 dwAttributes, bCreate, lpTemplate, lppStream);
397 if (!lpszPath || !lppStream || lpTemplate)
398 return E_INVALIDARG;
400 *lppStream = NULL;
402 if (dwMode & ~(STGM_WRITE|STGM_READWRITE|STGM_SHARE_DENY_NONE|STGM_SHARE_DENY_READ|STGM_CREATE))
404 WARN("Invalid mode 0x%08lX\n", dwMode);
405 return E_INVALIDARG;
408 /* Access */
409 switch (dwMode & (STGM_WRITE|STGM_READWRITE))
411 case STGM_READWRITE|STGM_WRITE:
412 case STGM_READWRITE:
413 dwAccess = GENERIC_READ|GENERIC_WRITE;
414 break;
415 case STGM_WRITE:
416 dwAccess = GENERIC_WRITE;
417 break;
418 default:
419 dwAccess = GENERIC_READ;
420 break;
423 /* Sharing */
424 switch (dwMode & STGM_SHARE_DENY_READ)
426 case STGM_SHARE_DENY_READ:
427 dwShare = FILE_SHARE_WRITE;
428 break;
429 case STGM_SHARE_DENY_WRITE:
430 dwShare = FILE_SHARE_READ;
431 break;
432 case STGM_SHARE_EXCLUSIVE:
433 dwShare = 0;
434 break;
435 default:
436 dwShare = FILE_SHARE_READ|FILE_SHARE_WRITE;
439 /* FIXME: Creation Flags, MSDN is fuzzy on the mapping... */
440 if (dwMode == STGM_FAILIFTHERE)
441 dwCreate = bCreate ? CREATE_NEW : OPEN_EXISTING;
442 else if (dwMode & STGM_CREATE)
443 dwCreate = CREATE_ALWAYS;
444 else
445 dwCreate = OPEN_ALWAYS;
447 /* Open HANDLE to file */
448 hFile = CreateFileW(lpszPath, dwAccess, dwShare, NULL, dwCreate,
449 dwAttributes, 0);
451 if(hFile == INVALID_HANDLE_VALUE)
453 HRESULT hRet = (HRESULT)GetLastError();
454 if(hRet > 0)
455 hRet = HRESULT_FROM_WIN32(hRet);
456 return hRet;
459 *lppStream = IStream_Create(lpszPath, hFile, dwMode);
461 if(!*lppStream)
463 CloseHandle(hFile);
464 return E_OUTOFMEMORY;
466 return S_OK;
469 /*************************************************************************
470 * SHCreateStreamOnFileW [SHLWAPI.@]
472 * See SHCreateStreamOnFileA.
474 HRESULT WINAPI SHCreateStreamOnFileW(LPCWSTR lpszPath, DWORD dwMode,
475 IStream **lppStream)
477 DWORD dwAttr;
479 TRACE("(%s,%ld,%p)\n", debugstr_w(lpszPath), dwMode, lppStream);
481 if (!lpszPath || !lppStream)
482 return E_INVALIDARG;
484 dwAttr = GetFileAttributesW(lpszPath);
485 if (dwAttr == -1u)
486 dwAttr = 0;
488 return SHCreateStreamOnFileEx(lpszPath, dwMode|STGM_WRITE, dwAttr,
489 TRUE, NULL, lppStream);
492 /*************************************************************************
493 * SHCreateStreamOnFileA [SHLWAPI.@]
495 * Create a stream on a file.
497 * PARAMS
498 * lpszPath [I] Path of file to create stream on
499 * dwMode [I] Mode to create stream in
500 * lppStream [O] Destination for created IStream object
502 * RETURNS
503 * Success: S_OK. lppStream contains the new IStream object
504 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
506 HRESULT WINAPI SHCreateStreamOnFileA(LPCSTR lpszPath, DWORD dwMode,
507 IStream **lppStream)
509 WCHAR szPath[MAX_PATH];
511 TRACE("(%s,%ld,%p)\n", debugstr_a(lpszPath), dwMode, lppStream);
513 if (!lpszPath)
514 return E_INVALIDARG;
515 MultiByteToWideChar(0, 0, lpszPath, -1, szPath, MAX_PATH);
516 return SHCreateStreamOnFileW(szPath, dwMode, lppStream);
519 /*************************************************************************
520 * @ [SHLWAPI.184]
522 * Call IStream_Read() on a stream.
524 * PARAMS
525 * lpStream [I] IStream object
526 * lpvDest [O] Destination for data read
527 * ulSize [I] Size of data to read
529 * RETURNS
530 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
531 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
532 * number of bytes read does not match.
534 HRESULT WINAPI SHLWAPI_184(IStream *lpStream, LPVOID lpvDest, ULONG ulSize)
536 ULONG ulRead;
537 HRESULT hRet;
539 TRACE("(%p,%p,%ld)\n", lpStream, lpvDest, ulSize);
541 hRet = IStream_Read(lpStream, lpvDest, ulSize, &ulRead);
543 if (SUCCEEDED(hRet) && ulRead != ulSize)
544 hRet = E_FAIL;
545 return hRet;
548 /*************************************************************************
549 * @ [SHLWAPI.166]
551 * Determine if a stream has 0 length.
553 * PARAMS
554 * lpStream [I] IStream object
556 * RETURNS
557 * TRUE: If the stream has 0 length
558 * FALSE: Otherwise.
560 BOOL WINAPI SHLWAPI_166(IStream *lpStream)
562 STATSTG statstg;
563 BOOL bRet = TRUE;
565 TRACE("(%p)\n", lpStream);
567 memset(&statstg, 0, sizeof(statstg));
569 if(SUCCEEDED(IStream_Stat(lpStream, &statstg, 1)))
571 if(statstg.cbSize.QuadPart)
572 bRet = FALSE; /* Non-Zero */
574 else
576 DWORD dwDummy;
578 /* Try to read from the stream */
579 if(SUCCEEDED(SHLWAPI_184(lpStream, &dwDummy, sizeof(dwDummy))))
581 LARGE_INTEGER zero;
582 zero.QuadPart = 0;
584 IStream_Seek(lpStream, zero, 0, NULL);
585 bRet = FALSE; /* Non-Zero */
588 return bRet;
591 /*************************************************************************
592 * @ [SHLWAPI.212]
594 * Call IStream_Write() on a stream.
596 * PARAMS
597 * lpStream [I] IStream object
598 * lpvSrc [I] Source for data to write
599 * ulSize [I] Size of data
601 * RETURNS
602 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
603 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
604 * number of bytes written does not match.
606 HRESULT WINAPI SHLWAPI_212(IStream *lpStream, LPCVOID lpvSrc, ULONG ulSize)
608 ULONG ulWritten;
609 HRESULT hRet;
611 TRACE("(%p,%p,%ld)\n", lpStream, lpvSrc, ulSize);
613 hRet = IStream_Write(lpStream, lpvSrc, ulSize, &ulWritten);
615 if (SUCCEEDED(hRet) && ulWritten != ulSize)
616 hRet = E_FAIL;
618 return hRet;
621 /*************************************************************************
622 * @ [SHLWAPI.213]
624 * Seek to the start of a stream.
626 * PARAMS
627 * lpStream [I] IStream object
629 * RETURNS
630 * Success: S_OK. The current position within the stream is updated
631 * Failure: An HRESULT error code.
633 HRESULT WINAPI SHLWAPI_213(IStream *lpStream)
635 LARGE_INTEGER zero;
636 TRACE("(%p)\n", lpStream);
637 zero.QuadPart = 0;
638 return IStream_Seek(lpStream, zero, 0, NULL);
641 /*************************************************************************
642 * @ [SHLWAPI.214]
644 * Get the size of a stream.
646 * PARAMS
647 * lpStream [I] IStream object
648 * lpulSize [O] Destination for size
650 * RETURNS
651 * Success: S_OK. lpulSize contains the size of the stream.
652 * Failure: An HRESULT error code.
654 HRESULT WINAPI SHLWAPI_214(IStream *lpStream, ULARGE_INTEGER* lpulSize)
656 STATSTG statstg;
657 HRESULT hRet;
659 TRACE("(%p,%p)\n", lpStream, lpulSize);
661 memset(&statstg, 0, sizeof(statstg));
663 hRet = IStream_Stat(lpStream, &statstg, 1);
665 if (SUCCEEDED(hRet) && lpulSize)
666 *lpulSize = statstg.cbSize;
667 return hRet;