Don't define COBJMACROS in objbase.h.
[wine/wine-gecko.git] / dlls / shlwapi / istream.c
blob0b38ad2911f62e9f6d4c8fa51c5c99959f454173
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 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 /* Layout of ISHFileStream object */
39 typedef struct
41 IStreamVtbl *lpVtbl;
42 ULONG ref;
43 HANDLE hFile;
44 DWORD dwMode;
45 LPOLESTR lpszPath;
46 DWORD type;
47 DWORD grfStateBits;
48 } ISHFileStream;
50 static HRESULT WINAPI IStream_fnCommit(IStream*,DWORD);
53 /**************************************************************************
54 * IStream_fnQueryInterface
56 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
58 ISHFileStream *This = (ISHFileStream *)iface;
60 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppvObj);
62 *ppvObj = NULL;
64 if(IsEqualIID(riid, &IID_IUnknown) ||
65 IsEqualIID(riid, &IID_IStream))
67 *ppvObj = This;
69 IStream_AddRef((IStream*)*ppvObj);
70 return S_OK;
72 return E_NOINTERFACE;
75 /**************************************************************************
76 * IStream_fnAddRef
78 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
80 ISHFileStream *This = (ISHFileStream *)iface;
82 TRACE("(%p)\n", This);
83 return InterlockedIncrement(&This->ref);
86 /**************************************************************************
87 * IStream_fnRelease
89 static ULONG WINAPI IStream_fnRelease(IStream *iface)
91 ISHFileStream *This = (ISHFileStream *)iface;
92 ULONG ulRet;
94 TRACE("(%p)\n", This);
96 if (!(ulRet = InterlockedDecrement(&This->ref)))
98 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
99 LocalFree((HLOCAL)This->lpszPath);
100 CloseHandle(This->hFile);
101 HeapFree(GetProcessHeap(), 0, This);
103 return ulRet;
106 /**************************************************************************
107 * IStream_fnRead
109 static HRESULT WINAPI IStream_fnRead(IStream *iface, void* pv, ULONG cb, ULONG* pcbRead)
111 ISHFileStream *This = (ISHFileStream *)iface;
112 HRESULT hRet = S_OK;
113 DWORD dwRead = 0;
115 TRACE("(%p,%p,0x%08lx,%p)\n", This, pv, cb, pcbRead);
117 if (!pv)
118 hRet = STG_E_INVALIDPOINTER;
119 else if (!ReadFile(This->hFile, pv, cb, &dwRead, NULL))
121 hRet = (HRESULT)GetLastError();
122 if(hRet > 0)
123 hRet = HRESULT_FROM_WIN32(hRet);
125 if (pcbRead)
126 *pcbRead = dwRead;
127 return hRet;
130 /**************************************************************************
131 * IStream_fnWrite
133 static HRESULT WINAPI IStream_fnWrite(IStream *iface, const void* pv, ULONG cb, ULONG* pcbWritten)
135 ISHFileStream *This = (ISHFileStream *)iface;
136 HRESULT hRet = S_OK;
137 DWORD dwWritten = 0;
139 TRACE("(%p,%p,0x%08lx,%p)\n", This, pv, cb, pcbWritten);
141 if (!pv)
142 hRet = STG_E_INVALIDPOINTER;
143 else if (!(This->dwMode & STGM_WRITE))
144 hRet = E_FAIL;
145 else if (!WriteFile(This->hFile, pv, cb, &dwWritten, NULL))
147 hRet = (HRESULT)GetLastError();
148 if(hRet > 0)
149 hRet = HRESULT_FROM_WIN32(hRet);
151 if (pcbWritten)
152 *pcbWritten = dwWritten;
153 return hRet;
156 /**************************************************************************
157 * IStream_fnSeek
159 static HRESULT WINAPI IStream_fnSeek(IStream *iface, LARGE_INTEGER dlibMove,
160 DWORD dwOrigin, ULARGE_INTEGER* pNewPos)
162 ISHFileStream *This = (ISHFileStream *)iface;
163 DWORD dwPos;
165 TRACE("(%p,%ld,%ld,%p)\n", This, dlibMove.u.LowPart, dwOrigin, pNewPos);
167 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
168 dwPos = SetFilePointer(This->hFile, dlibMove.u.LowPart, NULL, dwOrigin);
170 if (pNewPos)
172 pNewPos->u.HighPart = 0;
173 pNewPos->u.LowPart = dwPos;
175 return S_OK;
178 /**************************************************************************
179 * IStream_fnSetSize
181 static HRESULT WINAPI IStream_fnSetSize(IStream *iface, ULARGE_INTEGER libNewSize)
183 ISHFileStream *This = (ISHFileStream *)iface;
185 TRACE("(%p,%ld)\n", This, libNewSize.u.LowPart);
186 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
187 return E_NOTIMPL;
190 /**************************************************************************
191 * IStream_fnCopyTo
193 static HRESULT WINAPI IStream_fnCopyTo(IStream *iface, IStream* pstm, ULARGE_INTEGER cb,
194 ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
196 ISHFileStream *This = (ISHFileStream *)iface;
197 char copyBuff[1024];
198 ULONGLONG ulSize;
199 HRESULT hRet = S_OK;
201 TRACE("(%p,%p,%ld,%p,%p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
203 if (pcbRead)
204 pcbRead->QuadPart = 0;
205 if (pcbWritten)
206 pcbWritten->QuadPart = 0;
208 if (!pstm)
209 return STG_E_INVALIDPOINTER;
211 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
213 /* Copy data */
214 ulSize = cb.QuadPart;
215 while (ulSize)
217 ULONG ulLeft, ulAmt;
219 ulLeft = ulSize > sizeof(copyBuff) ? sizeof(copyBuff) : ulSize;
221 /* Read */
222 hRet = IStream_fnRead(iface, copyBuff, ulLeft, &ulAmt);
223 if (pcbRead)
224 pcbRead->QuadPart += ulAmt;
225 if (FAILED(hRet) || ulAmt != ulLeft)
226 break;
228 /* Write */
229 hRet = IStream_fnWrite(pstm, copyBuff, ulLeft, &ulAmt);
230 if (pcbWritten)
231 pcbWritten->QuadPart += ulAmt;
232 if (FAILED(hRet) || ulAmt != ulLeft)
233 break;
235 ulSize -= ulLeft;
237 return hRet;
240 /**************************************************************************
241 * IStream_fnCommit
243 static HRESULT WINAPI IStream_fnCommit(IStream *iface, DWORD grfCommitFlags)
245 ISHFileStream *This = (ISHFileStream *)iface;
247 TRACE("(%p,%ld)\n", This, grfCommitFlags);
248 /* Currently unbuffered: This function is not needed */
249 return S_OK;
252 /**************************************************************************
253 * IStream_fnRevert
255 static HRESULT WINAPI IStream_fnRevert(IStream *iface)
257 ISHFileStream *This = (ISHFileStream *)iface;
259 TRACE("(%p)\n", This);
260 return E_NOTIMPL;
263 /**************************************************************************
264 * IStream_fnLockUnlockRegion
266 static HRESULT WINAPI IStream_fnLockUnlockRegion(IStream *iface, ULARGE_INTEGER libOffset,
267 ULARGE_INTEGER cb, DWORD dwLockType)
269 ISHFileStream *This = (ISHFileStream *)iface;
270 TRACE("(%p,%ld,%ld,%ld)\n", This, libOffset.u.LowPart, cb.u.LowPart, dwLockType);
271 return E_NOTIMPL;
274 /*************************************************************************
275 * IStream_fnStat
277 static HRESULT WINAPI IStream_fnStat(IStream *iface, STATSTG* lpStat,
278 DWORD grfStatFlag)
280 ISHFileStream *This = (ISHFileStream *)iface;
281 BY_HANDLE_FILE_INFORMATION fi;
282 HRESULT hRet = S_OK;
284 TRACE("(%p,%p,%ld)\n", This, lpStat, grfStatFlag);
286 if (!grfStatFlag)
287 hRet = STG_E_INVALIDPOINTER;
288 else
290 memset(&fi, 0, sizeof(fi));
291 GetFileInformationByHandle(This->hFile, &fi);
293 if (grfStatFlag & STATFLAG_NONAME)
294 lpStat->pwcsName = NULL;
295 else
296 lpStat->pwcsName = StrDupW(This->lpszPath);
297 lpStat->type = This->type;
298 lpStat->cbSize.u.LowPart = fi.nFileSizeLow;
299 lpStat->cbSize.u.HighPart = fi.nFileSizeHigh;
300 lpStat->mtime = fi.ftLastWriteTime;
301 lpStat->ctime = fi.ftCreationTime;
302 lpStat->atime = fi.ftLastAccessTime;
303 lpStat->grfMode = This->dwMode;
304 lpStat->grfLocksSupported = 0;
305 memcpy(&lpStat->clsid, &IID_IStream, sizeof(CLSID));
306 lpStat->grfStateBits = This->grfStateBits;
307 lpStat->reserved = 0;
309 return hRet;
312 /*************************************************************************
313 * IStream_fnClone
315 static HRESULT WINAPI IStream_fnClone(IStream *iface, IStream** ppstm)
317 ISHFileStream *This = (ISHFileStream *)iface;
319 TRACE("(%p)\n",This);
320 if (ppstm)
321 *ppstm = NULL;
322 return E_NOTIMPL;
325 static struct IStreamVtbl SHLWAPI_fsVTable =
327 IStream_fnQueryInterface,
328 IStream_fnAddRef,
329 IStream_fnRelease,
330 IStream_fnRead,
331 IStream_fnWrite,
332 IStream_fnSeek,
333 IStream_fnSetSize,
334 IStream_fnCopyTo,
335 IStream_fnCommit,
336 IStream_fnRevert,
337 IStream_fnLockUnlockRegion,
338 IStream_fnLockUnlockRegion,
339 IStream_fnStat,
340 IStream_fnClone
343 /**************************************************************************
344 * IStream_Create
346 * Internal helper: Create and initialise a new file stream object.
348 static IStream *IStream_Create(LPCWSTR lpszPath, HANDLE hFile, DWORD dwMode)
350 ISHFileStream* fileStream;
352 fileStream = (ISHFileStream*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream));
354 if (fileStream)
356 fileStream->lpVtbl = &SHLWAPI_fsVTable;
357 fileStream->ref = 1;
358 fileStream->hFile = hFile;
359 fileStream->dwMode = dwMode;
360 fileStream->lpszPath = StrDupW(lpszPath);
361 fileStream->type = 0; /* FIXME */
362 fileStream->grfStateBits = 0; /* FIXME */
364 TRACE ("Returning %p\n", fileStream);
365 return (IStream *)fileStream;
368 /*************************************************************************
369 * SHCreateStreamOnFileEx [SHLWAPI.@]
371 * Create a stream on a file.
373 * PARAMS
374 * lpszPath [I] Path of file to create stream on
375 * dwMode [I] Mode to create stream in
376 * dwAttributes [I] Attributes of the file
377 * bCreate [I] Whether to create the file if it doesn't exist
378 * lpTemplate [I] Reserved, must be NULL
379 * lppStream [O] Destination for created stream
381 * RETURNS
382 * Success: S_OK. lppStream contains the new stream object
383 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
385 * NOTES
386 * This function is available in Unicode only.
388 HRESULT WINAPI SHCreateStreamOnFileEx(LPCWSTR lpszPath, DWORD dwMode,
389 DWORD dwAttributes, BOOL bCreate,
390 IStream *lpTemplate, IStream **lppStream)
392 DWORD dwAccess, dwShare, dwCreate;
393 HANDLE hFile;
395 TRACE("(%s,%ld,0x%08lX,%d,%p,%p)\n", debugstr_w(lpszPath), dwMode,
396 dwAttributes, bCreate, lpTemplate, lppStream);
398 if (!lpszPath || !lppStream || lpTemplate)
399 return E_INVALIDARG;
401 *lppStream = NULL;
403 if (dwMode & ~(STGM_WRITE|STGM_READWRITE|STGM_SHARE_DENY_NONE|STGM_SHARE_DENY_READ|STGM_CREATE))
405 WARN("Invalid mode 0x%08lX\n", dwMode);
406 return E_INVALIDARG;
409 /* Access */
410 switch (dwMode & (STGM_WRITE|STGM_READWRITE))
412 case STGM_READWRITE|STGM_WRITE:
413 case STGM_READWRITE:
414 dwAccess = GENERIC_READ|GENERIC_WRITE;
415 break;
416 case STGM_WRITE:
417 dwAccess = GENERIC_WRITE;
418 break;
419 default:
420 dwAccess = GENERIC_READ;
421 break;
424 /* Sharing */
425 switch (dwMode & STGM_SHARE_DENY_READ)
427 case STGM_SHARE_DENY_READ:
428 dwShare = FILE_SHARE_WRITE;
429 break;
430 case STGM_SHARE_DENY_WRITE:
431 dwShare = FILE_SHARE_READ;
432 break;
433 case STGM_SHARE_EXCLUSIVE:
434 dwShare = 0;
435 break;
436 default:
437 dwShare = FILE_SHARE_READ|FILE_SHARE_WRITE;
440 /* FIXME: Creation Flags, MSDN is fuzzy on the mapping... */
441 if (dwMode == STGM_FAILIFTHERE)
442 dwCreate = bCreate ? CREATE_NEW : OPEN_EXISTING;
443 else if (dwMode & STGM_CREATE)
444 dwCreate = CREATE_ALWAYS;
445 else
446 dwCreate = OPEN_ALWAYS;
448 /* Open HANDLE to file */
449 hFile = CreateFileW(lpszPath, dwAccess, dwShare, NULL, dwCreate,
450 dwAttributes, 0);
452 if(hFile == INVALID_HANDLE_VALUE)
454 HRESULT hRet = (HRESULT)GetLastError();
455 if(hRet > 0)
456 hRet = HRESULT_FROM_WIN32(hRet);
457 return hRet;
460 *lppStream = IStream_Create(lpszPath, hFile, dwMode);
462 if(!*lppStream)
464 CloseHandle(hFile);
465 return E_OUTOFMEMORY;
467 return S_OK;
470 /*************************************************************************
471 * SHCreateStreamOnFileW [SHLWAPI.@]
473 * See SHCreateStreamOnFileA.
475 HRESULT WINAPI SHCreateStreamOnFileW(LPCWSTR lpszPath, DWORD dwMode,
476 IStream **lppStream)
478 DWORD dwAttr;
480 TRACE("(%s,%ld,%p)\n", debugstr_w(lpszPath), dwMode, lppStream);
482 if (!lpszPath || !lppStream)
483 return E_INVALIDARG;
485 dwAttr = GetFileAttributesW(lpszPath);
486 if (dwAttr == INVALID_FILE_ATTRIBUTES)
487 dwAttr = 0;
489 return SHCreateStreamOnFileEx(lpszPath, dwMode|STGM_WRITE, dwAttr,
490 TRUE, NULL, lppStream);
493 /*************************************************************************
494 * SHCreateStreamOnFileA [SHLWAPI.@]
496 * Create a stream on a file.
498 * PARAMS
499 * lpszPath [I] Path of file to create stream on
500 * dwMode [I] Mode to create stream in
501 * lppStream [O] Destination for created IStream object
503 * RETURNS
504 * Success: S_OK. lppStream contains the new IStream object
505 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
507 HRESULT WINAPI SHCreateStreamOnFileA(LPCSTR lpszPath, DWORD dwMode,
508 IStream **lppStream)
510 WCHAR szPath[MAX_PATH];
512 TRACE("(%s,%ld,%p)\n", debugstr_a(lpszPath), dwMode, lppStream);
514 if (!lpszPath)
515 return E_INVALIDARG;
516 MultiByteToWideChar(0, 0, lpszPath, -1, szPath, MAX_PATH);
517 return SHCreateStreamOnFileW(szPath, dwMode, lppStream);
520 /*************************************************************************
521 * @ [SHLWAPI.184]
523 * Call IStream_Read() on a stream.
525 * PARAMS
526 * lpStream [I] IStream object
527 * lpvDest [O] Destination for data read
528 * ulSize [I] Size of data to read
530 * RETURNS
531 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
532 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
533 * number of bytes read does not match.
535 HRESULT WINAPI SHLWAPI_184(IStream *lpStream, LPVOID lpvDest, ULONG ulSize)
537 ULONG ulRead;
538 HRESULT hRet;
540 TRACE("(%p,%p,%ld)\n", lpStream, lpvDest, ulSize);
542 hRet = IStream_Read(lpStream, lpvDest, ulSize, &ulRead);
544 if (SUCCEEDED(hRet) && ulRead != ulSize)
545 hRet = E_FAIL;
546 return hRet;
549 /*************************************************************************
550 * @ [SHLWAPI.166]
552 * Determine if a stream has 0 length.
554 * PARAMS
555 * lpStream [I] IStream object
557 * RETURNS
558 * TRUE: If the stream has 0 length
559 * FALSE: Otherwise.
561 BOOL WINAPI SHIsEmptyStream(IStream *lpStream)
563 STATSTG statstg;
564 BOOL bRet = TRUE;
566 TRACE("(%p)\n", lpStream);
568 memset(&statstg, 0, sizeof(statstg));
570 if(SUCCEEDED(IStream_Stat(lpStream, &statstg, 1)))
572 if(statstg.cbSize.QuadPart)
573 bRet = FALSE; /* Non-Zero */
575 else
577 DWORD dwDummy;
579 /* Try to read from the stream */
580 if(SUCCEEDED(SHLWAPI_184(lpStream, &dwDummy, sizeof(dwDummy))))
582 LARGE_INTEGER zero;
583 zero.QuadPart = 0;
585 IStream_Seek(lpStream, zero, 0, NULL);
586 bRet = FALSE; /* Non-Zero */
589 return bRet;
592 /*************************************************************************
593 * @ [SHLWAPI.212]
595 * Call IStream_Write() on a stream.
597 * PARAMS
598 * lpStream [I] IStream object
599 * lpvSrc [I] Source for data to write
600 * ulSize [I] Size of data
602 * RETURNS
603 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
604 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
605 * number of bytes written does not match.
607 HRESULT WINAPI SHLWAPI_212(IStream *lpStream, LPCVOID lpvSrc, ULONG ulSize)
609 ULONG ulWritten;
610 HRESULT hRet;
612 TRACE("(%p,%p,%ld)\n", lpStream, lpvSrc, ulSize);
614 hRet = IStream_Write(lpStream, lpvSrc, ulSize, &ulWritten);
616 if (SUCCEEDED(hRet) && ulWritten != ulSize)
617 hRet = E_FAIL;
619 return hRet;
622 /*************************************************************************
623 * @ [SHLWAPI.213]
625 * Seek to the start of a stream.
627 * PARAMS
628 * lpStream [I] IStream object
630 * RETURNS
631 * Success: S_OK. The current position within the stream is updated
632 * Failure: An HRESULT error code.
634 HRESULT WINAPI IStream_Reset(IStream *lpStream)
636 LARGE_INTEGER zero;
637 TRACE("(%p)\n", lpStream);
638 zero.QuadPart = 0;
639 return IStream_Seek(lpStream, zero, 0, NULL);
642 /*************************************************************************
643 * @ [SHLWAPI.214]
645 * Get the size of a stream.
647 * PARAMS
648 * lpStream [I] IStream object
649 * lpulSize [O] Destination for size
651 * RETURNS
652 * Success: S_OK. lpulSize contains the size of the stream.
653 * Failure: An HRESULT error code.
655 HRESULT WINAPI IStream_Size(IStream *lpStream, ULARGE_INTEGER* lpulSize)
657 STATSTG statstg;
658 HRESULT hRet;
660 TRACE("(%p,%p)\n", lpStream, lpulSize);
662 memset(&statstg, 0, sizeof(statstg));
664 hRet = IStream_Stat(lpStream, &statstg, 1);
666 if (SUCCEEDED(hRet) && lpulSize)
667 *lpulSize = statstg.cbSize;
668 return hRet;