Removed some uses of the non-standard ICOM_THIS macro.
[wine/multimedia.git] / dlls / shlwapi / istream.c
blobbc1cb551d038438f9c027fd5b74dc8e369199d69
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 IStreamVtbl *lpVtbl;
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 ISHFileStream *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 ISHFileStream *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 ISHFileStream *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 ISHFileStream *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 ISHFileStream *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 ISHFileStream *This = (ISHFileStream *)iface;
161 DWORD dwPos;
163 TRACE("(%p,%ld,%ld,%p)\n", This, dlibMove.u.LowPart, dwOrigin, pNewPos);
165 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
166 dwPos = SetFilePointer(This->hFile, dlibMove.u.LowPart, NULL, dwOrigin);
168 if (pNewPos)
170 pNewPos->u.HighPart = 0;
171 pNewPos->u.LowPart = dwPos;
173 return S_OK;
176 /**************************************************************************
177 * IStream_fnSetSize
179 static HRESULT WINAPI IStream_fnSetSize(IStream *iface, ULARGE_INTEGER libNewSize)
181 ISHFileStream *This = (ISHFileStream *)iface;
183 TRACE("(%p,%ld)\n", This, libNewSize.u.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 ISHFileStream *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.u.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 ISHFileStream *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 ISHFileStream *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 ISHFileStream *This = (ISHFileStream *)iface;
268 TRACE("(%p,%ld,%ld,%ld)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
269 return E_NOTIMPL;
272 /*************************************************************************
273 * IStream_fnStat
275 static HRESULT WINAPI IStream_fnStat(IStream *iface, STATSTG* lpStat,
276 DWORD grfStatFlag)
278 ISHFileStream *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.u.LowPart = fi.nFileSizeLow;
297 lpStat->cbSize.u.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 ISHFileStream *This = (ISHFileStream *)iface;
317 TRACE("(%p)\n",This);
318 if (ppstm)
319 *ppstm = NULL;
320 return E_NOTIMPL;
323 static struct IStreamVtbl SHLWAPI_fsVTable =
325 IStream_fnQueryInterface,
326 IStream_fnAddRef,
327 IStream_fnRelease,
328 IStream_fnRead,
329 IStream_fnWrite,
330 IStream_fnSeek,
331 IStream_fnSetSize,
332 IStream_fnCopyTo,
333 IStream_fnCommit,
334 IStream_fnRevert,
335 IStream_fnLockUnlockRegion,
336 IStream_fnLockUnlockRegion,
337 IStream_fnStat,
338 IStream_fnClone
341 /**************************************************************************
342 * IStream_Create
344 * Internal helper: Create and initialise a new file stream object.
346 static IStream *IStream_Create(LPCWSTR lpszPath, HANDLE hFile, DWORD dwMode)
348 ISHFileStream* fileStream;
350 fileStream = (ISHFileStream*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream));
352 if (fileStream)
354 fileStream->lpVtbl = &SHLWAPI_fsVTable;
355 fileStream->ref = 1;
356 fileStream->hFile = hFile;
357 fileStream->dwMode = dwMode;
358 fileStream->lpszPath = StrDupW(lpszPath);
359 fileStream->type = 0; /* FIXME */
360 fileStream->grfStateBits = 0; /* FIXME */
362 TRACE ("Returning %p\n", fileStream);
363 return (IStream *)fileStream;
366 /*************************************************************************
367 * SHCreateStreamOnFileEx [SHLWAPI.@]
369 * Create a stream on a file.
371 * PARAMS
372 * lpszPath [I] Path of file to create stream on
373 * dwMode [I] Mode to create stream in
374 * dwAttributes [I] Attributes of the file
375 * bCreate [I] Whether to create the file if it doesn't exist
376 * lpTemplate [I] Reserved, must be NULL
377 * lppStream [O] Destination for created stream
379 * RETURNS
380 * Success: S_OK. lppStream contains the new stream object
381 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
383 * NOTES
384 * This function is available in Unicode only.
386 HRESULT WINAPI SHCreateStreamOnFileEx(LPCWSTR lpszPath, DWORD dwMode,
387 DWORD dwAttributes, BOOL bCreate,
388 IStream *lpTemplate, IStream **lppStream)
390 DWORD dwAccess, dwShare, dwCreate;
391 HANDLE hFile;
393 TRACE("(%s,%ld,0x%08lX,%d,%p,%p)\n", debugstr_w(lpszPath), dwMode,
394 dwAttributes, bCreate, lpTemplate, lppStream);
396 if (!lpszPath || !lppStream || lpTemplate)
397 return E_INVALIDARG;
399 *lppStream = NULL;
401 if (dwMode & ~(STGM_WRITE|STGM_READWRITE|STGM_SHARE_DENY_NONE|STGM_SHARE_DENY_READ|STGM_CREATE))
403 WARN("Invalid mode 0x%08lX\n", dwMode);
404 return E_INVALIDARG;
407 /* Access */
408 switch (dwMode & (STGM_WRITE|STGM_READWRITE))
410 case STGM_READWRITE|STGM_WRITE:
411 case STGM_READWRITE:
412 dwAccess = GENERIC_READ|GENERIC_WRITE;
413 break;
414 case STGM_WRITE:
415 dwAccess = GENERIC_WRITE;
416 break;
417 default:
418 dwAccess = GENERIC_READ;
419 break;
422 /* Sharing */
423 switch (dwMode & STGM_SHARE_DENY_READ)
425 case STGM_SHARE_DENY_READ:
426 dwShare = FILE_SHARE_WRITE;
427 break;
428 case STGM_SHARE_DENY_WRITE:
429 dwShare = FILE_SHARE_READ;
430 break;
431 case STGM_SHARE_EXCLUSIVE:
432 dwShare = 0;
433 break;
434 default:
435 dwShare = FILE_SHARE_READ|FILE_SHARE_WRITE;
438 /* FIXME: Creation Flags, MSDN is fuzzy on the mapping... */
439 if (dwMode == STGM_FAILIFTHERE)
440 dwCreate = bCreate ? CREATE_NEW : OPEN_EXISTING;
441 else if (dwMode & STGM_CREATE)
442 dwCreate = CREATE_ALWAYS;
443 else
444 dwCreate = OPEN_ALWAYS;
446 /* Open HANDLE to file */
447 hFile = CreateFileW(lpszPath, dwAccess, dwShare, NULL, dwCreate,
448 dwAttributes, 0);
450 if(hFile == INVALID_HANDLE_VALUE)
452 HRESULT hRet = (HRESULT)GetLastError();
453 if(hRet > 0)
454 hRet = HRESULT_FROM_WIN32(hRet);
455 return hRet;
458 *lppStream = IStream_Create(lpszPath, hFile, dwMode);
460 if(!*lppStream)
462 CloseHandle(hFile);
463 return E_OUTOFMEMORY;
465 return S_OK;
468 /*************************************************************************
469 * SHCreateStreamOnFileW [SHLWAPI.@]
471 * See SHCreateStreamOnFileA.
473 HRESULT WINAPI SHCreateStreamOnFileW(LPCWSTR lpszPath, DWORD dwMode,
474 IStream **lppStream)
476 DWORD dwAttr;
478 TRACE("(%s,%ld,%p)\n", debugstr_w(lpszPath), dwMode, lppStream);
480 if (!lpszPath || !lppStream)
481 return E_INVALIDARG;
483 dwAttr = GetFileAttributesW(lpszPath);
484 if (dwAttr == INVALID_FILE_ATTRIBUTES)
485 dwAttr = 0;
487 return SHCreateStreamOnFileEx(lpszPath, dwMode|STGM_WRITE, dwAttr,
488 TRUE, NULL, lppStream);
491 /*************************************************************************
492 * SHCreateStreamOnFileA [SHLWAPI.@]
494 * Create a stream on a file.
496 * PARAMS
497 * lpszPath [I] Path of file to create stream on
498 * dwMode [I] Mode to create stream in
499 * lppStream [O] Destination for created IStream object
501 * RETURNS
502 * Success: S_OK. lppStream contains the new IStream object
503 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
505 HRESULT WINAPI SHCreateStreamOnFileA(LPCSTR lpszPath, DWORD dwMode,
506 IStream **lppStream)
508 WCHAR szPath[MAX_PATH];
510 TRACE("(%s,%ld,%p)\n", debugstr_a(lpszPath), dwMode, lppStream);
512 if (!lpszPath)
513 return E_INVALIDARG;
514 MultiByteToWideChar(0, 0, lpszPath, -1, szPath, MAX_PATH);
515 return SHCreateStreamOnFileW(szPath, dwMode, lppStream);
518 /*************************************************************************
519 * @ [SHLWAPI.184]
521 * Call IStream_Read() on a stream.
523 * PARAMS
524 * lpStream [I] IStream object
525 * lpvDest [O] Destination for data read
526 * ulSize [I] Size of data to read
528 * RETURNS
529 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
530 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
531 * number of bytes read does not match.
533 HRESULT WINAPI SHLWAPI_184(IStream *lpStream, LPVOID lpvDest, ULONG ulSize)
535 ULONG ulRead;
536 HRESULT hRet;
538 TRACE("(%p,%p,%ld)\n", lpStream, lpvDest, ulSize);
540 hRet = IStream_Read(lpStream, lpvDest, ulSize, &ulRead);
542 if (SUCCEEDED(hRet) && ulRead != ulSize)
543 hRet = E_FAIL;
544 return hRet;
547 /*************************************************************************
548 * @ [SHLWAPI.166]
550 * Determine if a stream has 0 length.
552 * PARAMS
553 * lpStream [I] IStream object
555 * RETURNS
556 * TRUE: If the stream has 0 length
557 * FALSE: Otherwise.
559 BOOL WINAPI SHIsEmptyStream(IStream *lpStream)
561 STATSTG statstg;
562 BOOL bRet = TRUE;
564 TRACE("(%p)\n", lpStream);
566 memset(&statstg, 0, sizeof(statstg));
568 if(SUCCEEDED(IStream_Stat(lpStream, &statstg, 1)))
570 if(statstg.cbSize.QuadPart)
571 bRet = FALSE; /* Non-Zero */
573 else
575 DWORD dwDummy;
577 /* Try to read from the stream */
578 if(SUCCEEDED(SHLWAPI_184(lpStream, &dwDummy, sizeof(dwDummy))))
580 LARGE_INTEGER zero;
581 zero.QuadPart = 0;
583 IStream_Seek(lpStream, zero, 0, NULL);
584 bRet = FALSE; /* Non-Zero */
587 return bRet;
590 /*************************************************************************
591 * @ [SHLWAPI.212]
593 * Call IStream_Write() on a stream.
595 * PARAMS
596 * lpStream [I] IStream object
597 * lpvSrc [I] Source for data to write
598 * ulSize [I] Size of data
600 * RETURNS
601 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
602 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
603 * number of bytes written does not match.
605 HRESULT WINAPI SHLWAPI_212(IStream *lpStream, LPCVOID lpvSrc, ULONG ulSize)
607 ULONG ulWritten;
608 HRESULT hRet;
610 TRACE("(%p,%p,%ld)\n", lpStream, lpvSrc, ulSize);
612 hRet = IStream_Write(lpStream, lpvSrc, ulSize, &ulWritten);
614 if (SUCCEEDED(hRet) && ulWritten != ulSize)
615 hRet = E_FAIL;
617 return hRet;
620 /*************************************************************************
621 * @ [SHLWAPI.213]
623 * Seek to the start of a stream.
625 * PARAMS
626 * lpStream [I] IStream object
628 * RETURNS
629 * Success: S_OK. The current position within the stream is updated
630 * Failure: An HRESULT error code.
632 HRESULT WINAPI IStream_Reset(IStream *lpStream)
634 LARGE_INTEGER zero;
635 TRACE("(%p)\n", lpStream);
636 zero.QuadPart = 0;
637 return IStream_Seek(lpStream, zero, 0, NULL);
640 /*************************************************************************
641 * @ [SHLWAPI.214]
643 * Get the size of a stream.
645 * PARAMS
646 * lpStream [I] IStream object
647 * lpulSize [O] Destination for size
649 * RETURNS
650 * Success: S_OK. lpulSize contains the size of the stream.
651 * Failure: An HRESULT error code.
653 HRESULT WINAPI IStream_Size(IStream *lpStream, ULARGE_INTEGER* lpulSize)
655 STATSTG statstg;
656 HRESULT hRet;
658 TRACE("(%p,%p)\n", lpStream, lpulSize);
660 memset(&statstg, 0, sizeof(statstg));
662 hRet = IStream_Stat(lpStream, &statstg, 1);
664 if (SUCCEEDED(hRet) && lpulSize)
665 *lpulSize = statstg.cbSize;
666 return hRet;