winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / shlwapi / istream.c
blobbbd331ad754ec7257271a6bcd805b2eccfabe971
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 IStream IStream_iface;
46 LONG ref;
47 HANDLE hFile;
48 DWORD dwMode;
49 LPOLESTR lpszPath;
50 DWORD type;
51 DWORD grfStateBits;
52 } ISHFileStream;
54 static inline ISHFileStream *impl_from_IStream(IStream *iface)
56 return CONTAINING_RECORD(iface, ISHFileStream, IStream_iface);
59 static HRESULT WINAPI IStream_fnCommit(IStream*,DWORD);
62 /**************************************************************************
63 * IStream_fnQueryInterface
65 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
67 ISHFileStream *This = impl_from_IStream(iface);
69 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppvObj);
71 *ppvObj = NULL;
73 if(IsEqualIID(riid, &IID_IUnknown) ||
74 IsEqualIID(riid, &IID_IStream))
76 IStream_AddRef(iface);
77 *ppvObj = iface;
78 return S_OK;
80 return E_NOINTERFACE;
83 /**************************************************************************
84 * IStream_fnAddRef
86 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
88 ISHFileStream *This = impl_from_IStream(iface);
89 ULONG refCount = InterlockedIncrement(&This->ref);
91 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
93 return refCount;
96 /**************************************************************************
97 * IStream_fnRelease
99 static ULONG WINAPI IStream_fnRelease(IStream *iface)
101 ISHFileStream *This = impl_from_IStream(iface);
102 ULONG refCount = InterlockedDecrement(&This->ref);
104 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
106 if (!refCount)
108 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
109 LocalFree(This->lpszPath);
110 CloseHandle(This->hFile);
111 HeapFree(GetProcessHeap(), 0, This);
114 return refCount;
117 /**************************************************************************
118 * IStream_fnRead
120 static HRESULT WINAPI IStream_fnRead(IStream *iface, void* pv, ULONG cb, ULONG* pcbRead)
122 ISHFileStream *This = impl_from_IStream(iface);
123 DWORD dwRead = 0;
125 TRACE("(%p,%p,0x%08x,%p)\n", This, pv, cb, pcbRead);
127 if (!ReadFile(This->hFile, pv, cb, &dwRead, NULL))
129 WARN("error %d reading file\n", GetLastError());
130 return S_FALSE;
132 if (pcbRead)
133 *pcbRead = dwRead;
134 return S_OK;
137 /**************************************************************************
138 * IStream_fnWrite
140 static HRESULT WINAPI IStream_fnWrite(IStream *iface, const void* pv, ULONG cb, ULONG* pcbWritten)
142 ISHFileStream *This = impl_from_IStream(iface);
143 DWORD dwWritten = 0;
145 TRACE("(%p,%p,0x%08x,%p)\n", This, pv, cb, pcbWritten);
147 switch (STGM_ACCESS_MODE(This->dwMode))
149 case STGM_WRITE:
150 case STGM_READWRITE:
151 break;
152 default:
153 return STG_E_ACCESSDENIED;
156 if (!WriteFile(This->hFile, pv, cb, &dwWritten, NULL))
157 return HRESULT_FROM_WIN32(GetLastError());
159 if (pcbWritten)
160 *pcbWritten = dwWritten;
161 return S_OK;
164 /**************************************************************************
165 * IStream_fnSeek
167 static HRESULT WINAPI IStream_fnSeek(IStream *iface, LARGE_INTEGER dlibMove,
168 DWORD dwOrigin, ULARGE_INTEGER* pNewPos)
170 ISHFileStream *This = impl_from_IStream(iface);
171 DWORD dwPos;
173 TRACE("(%p,%d,%d,%p)\n", This, dlibMove.u.LowPart, dwOrigin, pNewPos);
175 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
176 dwPos = SetFilePointer(This->hFile, dlibMove.u.LowPart, NULL, dwOrigin);
177 if( dwPos == INVALID_SET_FILE_POINTER )
178 return HRESULT_FROM_WIN32(GetLastError());
180 if (pNewPos)
182 pNewPos->u.HighPart = 0;
183 pNewPos->u.LowPart = dwPos;
185 return S_OK;
188 /**************************************************************************
189 * IStream_fnSetSize
191 static HRESULT WINAPI IStream_fnSetSize(IStream *iface, ULARGE_INTEGER libNewSize)
193 ISHFileStream *This = impl_from_IStream(iface);
195 TRACE("(%p,%d)\n", This, libNewSize.u.LowPart);
197 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
198 if( ! SetFilePointer( This->hFile, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
199 return E_FAIL;
201 if( ! SetEndOfFile( This->hFile ) )
202 return E_FAIL;
204 return S_OK;
207 /**************************************************************************
208 * IStream_fnCopyTo
210 static HRESULT WINAPI IStream_fnCopyTo(IStream *iface, IStream* pstm, ULARGE_INTEGER cb,
211 ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
213 ISHFileStream *This = impl_from_IStream(iface);
214 char copyBuff[1024];
215 ULONGLONG ulSize;
216 HRESULT hRet = S_OK;
218 TRACE("(%p,%p,%d,%p,%p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
220 if (pcbRead)
221 pcbRead->QuadPart = 0;
222 if (pcbWritten)
223 pcbWritten->QuadPart = 0;
225 if (!pstm)
226 return S_OK;
228 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
230 /* Copy data */
231 ulSize = cb.QuadPart;
232 while (ulSize)
234 ULONG ulLeft, ulRead, ulWritten;
236 ulLeft = ulSize > sizeof(copyBuff) ? sizeof(copyBuff) : ulSize;
238 /* Read */
239 hRet = IStream_fnRead(iface, copyBuff, ulLeft, &ulRead);
240 if (FAILED(hRet) || ulRead == 0)
241 break;
242 if (pcbRead)
243 pcbRead->QuadPart += ulRead;
245 /* Write */
246 hRet = IStream_fnWrite(pstm, copyBuff, ulRead, &ulWritten);
247 if (pcbWritten)
248 pcbWritten->QuadPart += ulWritten;
249 if (FAILED(hRet) || ulWritten != ulLeft)
250 break;
252 ulSize -= ulLeft;
254 return hRet;
257 /**************************************************************************
258 * IStream_fnCommit
260 static HRESULT WINAPI IStream_fnCommit(IStream *iface, DWORD grfCommitFlags)
262 ISHFileStream *This = impl_from_IStream(iface);
264 TRACE("(%p,%d)\n", This, grfCommitFlags);
265 /* Currently unbuffered: This function is not needed */
266 return S_OK;
269 /**************************************************************************
270 * IStream_fnRevert
272 static HRESULT WINAPI IStream_fnRevert(IStream *iface)
274 ISHFileStream *This = impl_from_IStream(iface);
276 TRACE("(%p)\n", This);
277 return E_NOTIMPL;
280 /**************************************************************************
281 * IStream_fnLockUnlockRegion
283 static HRESULT WINAPI IStream_fnLockUnlockRegion(IStream *iface, ULARGE_INTEGER libOffset,
284 ULARGE_INTEGER cb, DWORD dwLockType)
286 ISHFileStream *This = impl_from_IStream(iface);
287 TRACE("(%p,%d,%d,%d)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
288 return E_NOTIMPL;
291 /*************************************************************************
292 * IStream_fnStat
294 static HRESULT WINAPI IStream_fnStat(IStream *iface, STATSTG* lpStat,
295 DWORD grfStatFlag)
297 ISHFileStream *This = impl_from_IStream(iface);
298 BY_HANDLE_FILE_INFORMATION fi;
300 TRACE("(%p,%p,%d)\n", This, lpStat, grfStatFlag);
302 if (!grfStatFlag)
303 return STG_E_INVALIDPOINTER;
305 memset(&fi, 0, sizeof(fi));
306 GetFileInformationByHandle(This->hFile, &fi);
308 if (grfStatFlag & STATFLAG_NONAME)
309 lpStat->pwcsName = NULL;
310 else
311 lpStat->pwcsName = StrDupW(This->lpszPath);
312 lpStat->type = This->type;
313 lpStat->cbSize.u.LowPart = fi.nFileSizeLow;
314 lpStat->cbSize.u.HighPart = fi.nFileSizeHigh;
315 lpStat->mtime = fi.ftLastWriteTime;
316 lpStat->ctime = fi.ftCreationTime;
317 lpStat->atime = fi.ftLastAccessTime;
318 lpStat->grfMode = This->dwMode;
319 lpStat->grfLocksSupported = 0;
320 memcpy(&lpStat->clsid, &IID_IStream, sizeof(CLSID));
321 lpStat->grfStateBits = This->grfStateBits;
322 lpStat->reserved = 0;
324 return S_OK;
327 /*************************************************************************
328 * IStream_fnClone
330 static HRESULT WINAPI IStream_fnClone(IStream *iface, IStream** ppstm)
332 ISHFileStream *This = impl_from_IStream(iface);
334 TRACE("(%p)\n",This);
335 if (ppstm)
336 *ppstm = NULL;
337 return E_NOTIMPL;
340 static const IStreamVtbl SHLWAPI_fsVTable =
342 IStream_fnQueryInterface,
343 IStream_fnAddRef,
344 IStream_fnRelease,
345 IStream_fnRead,
346 IStream_fnWrite,
347 IStream_fnSeek,
348 IStream_fnSetSize,
349 IStream_fnCopyTo,
350 IStream_fnCommit,
351 IStream_fnRevert,
352 IStream_fnLockUnlockRegion,
353 IStream_fnLockUnlockRegion,
354 IStream_fnStat,
355 IStream_fnClone
358 /**************************************************************************
359 * IStream_Create
361 * Internal helper: Create and initialise a new file stream object.
363 static IStream *IStream_Create(LPCWSTR lpszPath, HANDLE hFile, DWORD dwMode)
365 ISHFileStream *fileStream;
367 fileStream = HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream));
368 if (!fileStream) return NULL;
370 fileStream->IStream_iface.lpVtbl = &SHLWAPI_fsVTable;
371 fileStream->ref = 1;
372 fileStream->hFile = hFile;
373 fileStream->dwMode = dwMode;
374 fileStream->lpszPath = StrDupW(lpszPath);
375 fileStream->type = 0; /* FIXME */
376 fileStream->grfStateBits = 0; /* FIXME */
378 TRACE ("Returning %p\n", fileStream);
379 return &fileStream->IStream_iface;
382 /*************************************************************************
383 * SHCreateStreamOnFileEx [SHLWAPI.@]
385 * Create a stream on a file.
387 * PARAMS
388 * lpszPath [I] Path of file to create stream on
389 * dwMode [I] Mode to create stream in
390 * dwAttributes [I] Attributes of the file
391 * bCreate [I] Whether to create the file if it doesn't exist
392 * lpTemplate [I] Reserved, must be NULL
393 * lppStream [O] Destination for created stream
395 * RETURNS
396 * Success: S_OK. lppStream contains the new stream object
397 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
399 * NOTES
400 * This function is available in Unicode only.
402 HRESULT WINAPI SHCreateStreamOnFileEx(LPCWSTR lpszPath, DWORD dwMode,
403 DWORD dwAttributes, BOOL bCreate,
404 IStream *lpTemplate, IStream **lppStream)
406 DWORD dwAccess, dwShare, dwCreate;
407 HANDLE hFile;
409 TRACE("(%s,%d,0x%08X,%d,%p,%p)\n", debugstr_w(lpszPath), dwMode,
410 dwAttributes, bCreate, lpTemplate, lppStream);
412 if (!lpszPath || !lppStream || lpTemplate)
413 return E_INVALIDARG;
415 *lppStream = NULL;
417 /* Access */
418 switch (STGM_ACCESS_MODE(dwMode))
420 case STGM_WRITE:
421 case STGM_READWRITE:
422 dwAccess = GENERIC_READ|GENERIC_WRITE;
423 break;
424 case STGM_READ:
425 dwAccess = GENERIC_READ;
426 break;
427 default:
428 return E_INVALIDARG;
431 /* Sharing */
432 switch (STGM_SHARE_MODE(dwMode))
434 case 0:
435 case STGM_SHARE_DENY_NONE:
436 dwShare = FILE_SHARE_READ|FILE_SHARE_WRITE;
437 break;
438 case STGM_SHARE_DENY_READ:
439 dwShare = FILE_SHARE_WRITE;
440 break;
441 case STGM_SHARE_DENY_WRITE:
442 dwShare = FILE_SHARE_READ;
443 break;
444 case STGM_SHARE_EXCLUSIVE:
445 dwShare = 0;
446 break;
447 default:
448 return E_INVALIDARG;
451 switch(STGM_CREATE_MODE(dwMode))
453 case STGM_FAILIFTHERE:
454 dwCreate = bCreate ? CREATE_NEW : OPEN_EXISTING;
455 break;
456 case STGM_CREATE:
457 dwCreate = CREATE_ALWAYS;
458 break;
459 default:
460 return E_INVALIDARG;
463 /* Open HANDLE to file */
464 hFile = CreateFileW(lpszPath, dwAccess, dwShare, NULL, dwCreate,
465 dwAttributes, 0);
467 if(hFile == INVALID_HANDLE_VALUE)
468 return HRESULT_FROM_WIN32(GetLastError());
470 *lppStream = IStream_Create(lpszPath, hFile, dwMode);
472 if(!*lppStream)
474 CloseHandle(hFile);
475 return E_OUTOFMEMORY;
477 return S_OK;
480 /*************************************************************************
481 * SHCreateStreamOnFileW [SHLWAPI.@]
483 * See SHCreateStreamOnFileA.
485 HRESULT WINAPI SHCreateStreamOnFileW(LPCWSTR lpszPath, DWORD dwMode,
486 IStream **lppStream)
488 TRACE("(%s,%d,%p)\n", debugstr_w(lpszPath), dwMode, lppStream);
490 if (!lpszPath || !lppStream)
491 return E_INVALIDARG;
493 if ((dwMode & (STGM_CONVERT|STGM_DELETEONRELEASE|STGM_TRANSACTED)) != 0)
494 return E_INVALIDARG;
496 return SHCreateStreamOnFileEx(lpszPath, dwMode, 0, FALSE, NULL, lppStream);
499 /*************************************************************************
500 * SHCreateStreamOnFileA [SHLWAPI.@]
502 * Create a stream on a file.
504 * PARAMS
505 * lpszPath [I] Path of file to create stream on
506 * dwMode [I] Mode to create stream in
507 * lppStream [O] Destination for created IStream object
509 * RETURNS
510 * Success: S_OK. lppStream contains the new IStream object
511 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
513 HRESULT WINAPI SHCreateStreamOnFileA(LPCSTR lpszPath, DWORD dwMode,
514 IStream **lppStream)
516 WCHAR szPath[MAX_PATH];
518 TRACE("(%s,%d,%p)\n", debugstr_a(lpszPath), dwMode, lppStream);
520 if (!lpszPath)
521 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
523 MultiByteToWideChar(CP_ACP, 0, lpszPath, -1, szPath, MAX_PATH);
524 return SHCreateStreamOnFileW(szPath, dwMode, lppStream);
527 /*************************************************************************
528 * @ [SHLWAPI.184]
530 * Call IStream_Read() on a stream.
532 * PARAMS
533 * lpStream [I] IStream object
534 * lpvDest [O] Destination for data read
535 * ulSize [I] Size of data to read
537 * RETURNS
538 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
539 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
540 * number of bytes read does not match.
542 HRESULT WINAPI SHIStream_Read(IStream *lpStream, LPVOID lpvDest, ULONG ulSize)
544 ULONG ulRead;
545 HRESULT hRet;
547 TRACE("(%p,%p,%d)\n", lpStream, lpvDest, ulSize);
549 hRet = IStream_Read(lpStream, lpvDest, ulSize, &ulRead);
551 if (SUCCEEDED(hRet) && ulRead != ulSize)
552 hRet = E_FAIL;
553 return hRet;
556 /*************************************************************************
557 * @ [SHLWAPI.166]
559 * Determine if a stream has 0 length.
561 * PARAMS
562 * lpStream [I] IStream object
564 * RETURNS
565 * TRUE: If the stream has 0 length
566 * FALSE: Otherwise.
568 BOOL WINAPI SHIsEmptyStream(IStream *lpStream)
570 STATSTG statstg;
571 BOOL bRet = TRUE;
573 TRACE("(%p)\n", lpStream);
575 memset(&statstg, 0, sizeof(statstg));
577 if(SUCCEEDED(IStream_Stat(lpStream, &statstg, 1)))
579 if(statstg.cbSize.QuadPart)
580 bRet = FALSE; /* Non-Zero */
582 else
584 DWORD dwDummy;
586 /* Try to read from the stream */
587 if(SUCCEEDED(SHIStream_Read(lpStream, &dwDummy, sizeof(dwDummy))))
589 LARGE_INTEGER zero;
590 zero.QuadPart = 0;
592 IStream_Seek(lpStream, zero, 0, NULL);
593 bRet = FALSE; /* Non-Zero */
596 return bRet;
599 /*************************************************************************
600 * @ [SHLWAPI.212]
602 * Call IStream_Write() on a stream.
604 * PARAMS
605 * lpStream [I] IStream object
606 * lpvSrc [I] Source for data to write
607 * ulSize [I] Size of data
609 * RETURNS
610 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
611 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
612 * number of bytes written does not match.
614 HRESULT WINAPI SHIStream_Write(IStream *lpStream, LPCVOID lpvSrc, ULONG ulSize)
616 ULONG ulWritten;
617 HRESULT hRet;
619 TRACE("(%p,%p,%d)\n", lpStream, lpvSrc, ulSize);
621 hRet = IStream_Write(lpStream, lpvSrc, ulSize, &ulWritten);
623 if (SUCCEEDED(hRet) && ulWritten != ulSize)
624 hRet = E_FAIL;
626 return hRet;
629 /*************************************************************************
630 * @ [SHLWAPI.213]
632 * Seek to the start of a stream.
634 * PARAMS
635 * lpStream [I] IStream object
637 * RETURNS
638 * Success: S_OK. The current position within the stream is updated
639 * Failure: An HRESULT error code.
641 HRESULT WINAPI IStream_Reset(IStream *lpStream)
643 LARGE_INTEGER zero;
644 TRACE("(%p)\n", lpStream);
645 zero.QuadPart = 0;
646 return IStream_Seek(lpStream, zero, 0, NULL);
649 /*************************************************************************
650 * @ [SHLWAPI.214]
652 * Get the size of a stream.
654 * PARAMS
655 * lpStream [I] IStream object
656 * lpulSize [O] Destination for size
658 * RETURNS
659 * Success: S_OK. lpulSize contains the size of the stream.
660 * Failure: An HRESULT error code.
662 HRESULT WINAPI IStream_Size(IStream *lpStream, ULARGE_INTEGER* lpulSize)
664 STATSTG statstg;
665 HRESULT hRet;
667 TRACE("(%p,%p)\n", lpStream, lpulSize);
669 memset(&statstg, 0, sizeof(statstg));
671 hRet = IStream_Stat(lpStream, &statstg, 1);
673 if (SUCCEEDED(hRet) && lpulSize)
674 *lpulSize = statstg.cbSize;
675 return hRet;