Sync with trunk r58033.
[reactos.git] / dll / win32 / shlwapi / istream.c
1 /*
2 * SHLWAPI IStream functions
3 *
4 * Copyright 2002 Jon Griffiths
5 *
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.
10 *
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.
15 *
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
19 */
20 #include <stdarg.h>
21 #include <string.h>
22
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26
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"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
37
38 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
39 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
40 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
41
42 /* Layout of ISHFileStream object */
43 typedef struct
44 {
45 IStream IStream_iface;
46 LONG ref;
47 HANDLE hFile;
48 DWORD dwMode;
49 LPOLESTR lpszPath;
50 DWORD type;
51 DWORD grfStateBits;
52 } ISHFileStream;
53
54 static inline ISHFileStream *impl_from_IStream(IStream *iface)
55 {
56 return CONTAINING_RECORD(iface, ISHFileStream, IStream_iface);
57 }
58
59 static HRESULT WINAPI IStream_fnCommit(IStream*,DWORD);
60
61
62 /**************************************************************************
63 * IStream_fnQueryInterface
64 */
65 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
66 {
67 ISHFileStream *This = impl_from_IStream(iface);
68
69 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppvObj);
70
71 *ppvObj = NULL;
72
73 if(IsEqualIID(riid, &IID_IUnknown) ||
74 IsEqualIID(riid, &IID_IStream))
75 {
76 *ppvObj = This;
77 IStream_AddRef(iface);
78 return S_OK;
79 }
80 return E_NOINTERFACE;
81 }
82
83 /**************************************************************************
84 * IStream_fnAddRef
85 */
86 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
87 {
88 ISHFileStream *This = impl_from_IStream(iface);
89 ULONG refCount = InterlockedIncrement(&This->ref);
90
91 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
92
93 return refCount;
94 }
95
96 /**************************************************************************
97 * IStream_fnRelease
98 */
99 static ULONG WINAPI IStream_fnRelease(IStream *iface)
100 {
101 ISHFileStream *This = impl_from_IStream(iface);
102 ULONG refCount = InterlockedDecrement(&This->ref);
103
104 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
105
106 if (!refCount)
107 {
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);
112 }
113
114 return refCount;
115 }
116
117 /**************************************************************************
118 * IStream_fnRead
119 */
120 static HRESULT WINAPI IStream_fnRead(IStream *iface, void* pv, ULONG cb, ULONG* pcbRead)
121 {
122 ISHFileStream *This = impl_from_IStream(iface);
123 DWORD dwRead = 0;
124
125 TRACE("(%p,%p,0x%08x,%p)\n", This, pv, cb, pcbRead);
126
127 if (!ReadFile(This->hFile, pv, cb, &dwRead, NULL))
128 {
129 WARN("error %d reading file\n", GetLastError());
130 return S_FALSE;
131 }
132 if (pcbRead)
133 *pcbRead = dwRead;
134 return S_OK;
135 }
136
137 /**************************************************************************
138 * IStream_fnWrite
139 */
140 static HRESULT WINAPI IStream_fnWrite(IStream *iface, const void* pv, ULONG cb, ULONG* pcbWritten)
141 {
142 ISHFileStream *This = impl_from_IStream(iface);
143 DWORD dwWritten = 0;
144
145 TRACE("(%p,%p,0x%08x,%p)\n", This, pv, cb, pcbWritten);
146
147 switch (STGM_ACCESS_MODE(This->dwMode))
148 {
149 case STGM_WRITE:
150 case STGM_READWRITE:
151 break;
152 default:
153 return STG_E_ACCESSDENIED;
154 }
155
156 if (!WriteFile(This->hFile, pv, cb, &dwWritten, NULL))
157 return HRESULT_FROM_WIN32(GetLastError());
158
159 if (pcbWritten)
160 *pcbWritten = dwWritten;
161 return S_OK;
162 }
163
164 /**************************************************************************
165 * IStream_fnSeek
166 */
167 static HRESULT WINAPI IStream_fnSeek(IStream *iface, LARGE_INTEGER dlibMove,
168 DWORD dwOrigin, ULARGE_INTEGER* pNewPos)
169 {
170 ISHFileStream *This = impl_from_IStream(iface);
171 DWORD dwPos;
172
173 TRACE("(%p,%d,%d,%p)\n", This, dlibMove.u.LowPart, dwOrigin, pNewPos);
174
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());
179
180 if (pNewPos)
181 {
182 pNewPos->u.HighPart = 0;
183 pNewPos->u.LowPart = dwPos;
184 }
185 return S_OK;
186 }
187
188 /**************************************************************************
189 * IStream_fnSetSize
190 */
191 static HRESULT WINAPI IStream_fnSetSize(IStream *iface, ULARGE_INTEGER libNewSize)
192 {
193 ISHFileStream *This = impl_from_IStream(iface);
194
195 TRACE("(%p,%d)\n", This, libNewSize.u.LowPart);
196
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;
200
201 if( ! SetEndOfFile( This->hFile ) )
202 return E_FAIL;
203
204 return S_OK;
205 }
206
207 /**************************************************************************
208 * IStream_fnCopyTo
209 */
210 static HRESULT WINAPI IStream_fnCopyTo(IStream *iface, IStream* pstm, ULARGE_INTEGER cb,
211 ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
212 {
213 ISHFileStream *This = impl_from_IStream(iface);
214 char copyBuff[1024];
215 ULONGLONG ulSize;
216 HRESULT hRet = S_OK;
217
218 TRACE("(%p,%p,%d,%p,%p)\n", This, pstm, cb.u.LowPart, pcbRead, pcbWritten);
219
220 if (pcbRead)
221 pcbRead->QuadPart = 0;
222 if (pcbWritten)
223 pcbWritten->QuadPart = 0;
224
225 if (!pstm)
226 return S_OK;
227
228 IStream_fnCommit(iface, 0); /* If ever buffered, this will be needed */
229
230 /* Copy data */
231 ulSize = cb.QuadPart;
232 while (ulSize)
233 {
234 ULONG ulLeft, ulAmt;
235
236 ulLeft = ulSize > sizeof(copyBuff) ? sizeof(copyBuff) : ulSize;
237
238 /* Read */
239 hRet = IStream_fnRead(iface, copyBuff, ulLeft, &ulAmt);
240 if (pcbRead)
241 pcbRead->QuadPart += ulAmt;
242 if (FAILED(hRet) || ulAmt != ulLeft)
243 break;
244
245 /* Write */
246 hRet = IStream_fnWrite(pstm, copyBuff, ulLeft, &ulAmt);
247 if (pcbWritten)
248 pcbWritten->QuadPart += ulAmt;
249 if (FAILED(hRet) || ulAmt != ulLeft)
250 break;
251
252 ulSize -= ulLeft;
253 }
254 return hRet;
255 }
256
257 /**************************************************************************
258 * IStream_fnCommit
259 */
260 static HRESULT WINAPI IStream_fnCommit(IStream *iface, DWORD grfCommitFlags)
261 {
262 ISHFileStream *This = impl_from_IStream(iface);
263
264 TRACE("(%p,%d)\n", This, grfCommitFlags);
265 /* Currently unbuffered: This function is not needed */
266 return S_OK;
267 }
268
269 /**************************************************************************
270 * IStream_fnRevert
271 */
272 static HRESULT WINAPI IStream_fnRevert(IStream *iface)
273 {
274 ISHFileStream *This = impl_from_IStream(iface);
275
276 TRACE("(%p)\n", This);
277 return E_NOTIMPL;
278 }
279
280 /**************************************************************************
281 * IStream_fnLockUnlockRegion
282 */
283 static HRESULT WINAPI IStream_fnLockUnlockRegion(IStream *iface, ULARGE_INTEGER libOffset,
284 ULARGE_INTEGER cb, DWORD dwLockType)
285 {
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;
289 }
290
291 /*************************************************************************
292 * IStream_fnStat
293 */
294 static HRESULT WINAPI IStream_fnStat(IStream *iface, STATSTG* lpStat,
295 DWORD grfStatFlag)
296 {
297 ISHFileStream *This = impl_from_IStream(iface);
298 BY_HANDLE_FILE_INFORMATION fi;
299 HRESULT hRet = S_OK;
300
301 TRACE("(%p,%p,%d)\n", This, lpStat, grfStatFlag);
302
303 if (!grfStatFlag)
304 hRet = STG_E_INVALIDPOINTER;
305 else
306 {
307 memset(&fi, 0, sizeof(fi));
308 GetFileInformationByHandle(This->hFile, &fi);
309
310 if (grfStatFlag & STATFLAG_NONAME)
311 lpStat->pwcsName = NULL;
312 else
313 lpStat->pwcsName = StrDupW(This->lpszPath);
314 lpStat->type = This->type;
315 lpStat->cbSize.u.LowPart = fi.nFileSizeLow;
316 lpStat->cbSize.u.HighPart = fi.nFileSizeHigh;
317 lpStat->mtime = fi.ftLastWriteTime;
318 lpStat->ctime = fi.ftCreationTime;
319 lpStat->atime = fi.ftLastAccessTime;
320 lpStat->grfMode = This->dwMode;
321 lpStat->grfLocksSupported = 0;
322 memcpy(&lpStat->clsid, &IID_IStream, sizeof(CLSID));
323 lpStat->grfStateBits = This->grfStateBits;
324 lpStat->reserved = 0;
325 }
326 return hRet;
327 }
328
329 /*************************************************************************
330 * IStream_fnClone
331 */
332 static HRESULT WINAPI IStream_fnClone(IStream *iface, IStream** ppstm)
333 {
334 ISHFileStream *This = impl_from_IStream(iface);
335
336 TRACE("(%p)\n",This);
337 if (ppstm)
338 *ppstm = NULL;
339 return E_NOTIMPL;
340 }
341
342 static const IStreamVtbl SHLWAPI_fsVTable =
343 {
344 IStream_fnQueryInterface,
345 IStream_fnAddRef,
346 IStream_fnRelease,
347 IStream_fnRead,
348 IStream_fnWrite,
349 IStream_fnSeek,
350 IStream_fnSetSize,
351 IStream_fnCopyTo,
352 IStream_fnCommit,
353 IStream_fnRevert,
354 IStream_fnLockUnlockRegion,
355 IStream_fnLockUnlockRegion,
356 IStream_fnStat,
357 IStream_fnClone
358 };
359
360 /**************************************************************************
361 * IStream_Create
362 *
363 * Internal helper: Create and initialise a new file stream object.
364 */
365 static IStream *IStream_Create(LPCWSTR lpszPath, HANDLE hFile, DWORD dwMode)
366 {
367 ISHFileStream* fileStream;
368
369 fileStream = HeapAlloc(GetProcessHeap(), 0, sizeof(ISHFileStream));
370
371 if (fileStream)
372 {
373 fileStream->IStream_iface.lpVtbl = &SHLWAPI_fsVTable;
374 fileStream->ref = 1;
375 fileStream->hFile = hFile;
376 fileStream->dwMode = dwMode;
377 fileStream->lpszPath = StrDupW(lpszPath);
378 fileStream->type = 0; /* FIXME */
379 fileStream->grfStateBits = 0; /* FIXME */
380 }
381 TRACE ("Returning %p\n", fileStream);
382 return &fileStream->IStream_iface;
383 }
384
385 /*************************************************************************
386 * SHCreateStreamOnFileEx [SHLWAPI.@]
387 *
388 * Create a stream on a file.
389 *
390 * PARAMS
391 * lpszPath [I] Path of file to create stream on
392 * dwMode [I] Mode to create stream in
393 * dwAttributes [I] Attributes of the file
394 * bCreate [I] Whether to create the file if it doesn't exist
395 * lpTemplate [I] Reserved, must be NULL
396 * lppStream [O] Destination for created stream
397 *
398 * RETURNS
399 * Success: S_OK. lppStream contains the new stream object
400 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
401 *
402 * NOTES
403 * This function is available in Unicode only.
404 */
405 HRESULT WINAPI SHCreateStreamOnFileEx(LPCWSTR lpszPath, DWORD dwMode,
406 DWORD dwAttributes, BOOL bCreate,
407 IStream *lpTemplate, IStream **lppStream)
408 {
409 DWORD dwAccess, dwShare, dwCreate;
410 HANDLE hFile;
411
412 TRACE("(%s,%d,0x%08X,%d,%p,%p)\n", debugstr_w(lpszPath), dwMode,
413 dwAttributes, bCreate, lpTemplate, lppStream);
414
415 if (!lpszPath || !lppStream || lpTemplate)
416 return E_INVALIDARG;
417
418 *lppStream = NULL;
419
420 /* Access */
421 switch (STGM_ACCESS_MODE(dwMode))
422 {
423 case STGM_READWRITE:
424 dwAccess = GENERIC_READ|GENERIC_WRITE;
425 break;
426 case STGM_WRITE:
427 dwAccess = GENERIC_WRITE;
428 break;
429 case STGM_READ:
430 dwAccess = GENERIC_READ;
431 break;
432 default:
433 return E_INVALIDARG;
434 }
435
436 /* Sharing */
437 switch (STGM_SHARE_MODE(dwMode))
438 {
439 case 0:
440 dwShare = FILE_SHARE_READ|FILE_SHARE_WRITE;
441 break;
442 case STGM_SHARE_DENY_READ:
443 dwShare = FILE_SHARE_WRITE;
444 break;
445 case STGM_SHARE_DENY_WRITE:
446 dwShare = FILE_SHARE_READ;
447 break;
448 case STGM_SHARE_EXCLUSIVE:
449 dwShare = 0;
450 break;
451 case STGM_SHARE_DENY_NONE:
452 dwShare = FILE_SHARE_READ|FILE_SHARE_WRITE;
453 break;
454 default:
455 return E_INVALIDARG;
456 }
457
458 switch(STGM_CREATE_MODE(dwMode))
459 {
460 case STGM_FAILIFTHERE:
461 dwCreate = bCreate ? CREATE_NEW : OPEN_EXISTING;
462 break;
463 case STGM_CREATE:
464 dwCreate = CREATE_ALWAYS;
465 break;
466 default:
467 return E_INVALIDARG;
468 }
469
470 /* Open HANDLE to file */
471 hFile = CreateFileW(lpszPath, dwAccess, dwShare, NULL, dwCreate,
472 dwAttributes, 0);
473
474 if(hFile == INVALID_HANDLE_VALUE)
475 return HRESULT_FROM_WIN32(GetLastError());
476
477 *lppStream = IStream_Create(lpszPath, hFile, dwMode);
478
479 if(!*lppStream)
480 {
481 CloseHandle(hFile);
482 return E_OUTOFMEMORY;
483 }
484 return S_OK;
485 }
486
487 /*************************************************************************
488 * SHCreateStreamOnFileW [SHLWAPI.@]
489 *
490 * See SHCreateStreamOnFileA.
491 */
492 HRESULT WINAPI SHCreateStreamOnFileW(LPCWSTR lpszPath, DWORD dwMode,
493 IStream **lppStream)
494 {
495 TRACE("(%s,%d,%p)\n", debugstr_w(lpszPath), dwMode, lppStream);
496
497 if (!lpszPath || !lppStream)
498 return E_INVALIDARG;
499
500 if ((dwMode & (STGM_CONVERT|STGM_DELETEONRELEASE|STGM_TRANSACTED)) != 0)
501 return E_INVALIDARG;
502
503 return SHCreateStreamOnFileEx(lpszPath, dwMode, 0, FALSE, NULL, lppStream);
504 }
505
506 /*************************************************************************
507 * SHCreateStreamOnFileA [SHLWAPI.@]
508 *
509 * Create a stream on a file.
510 *
511 * PARAMS
512 * lpszPath [I] Path of file to create stream on
513 * dwMode [I] Mode to create stream in
514 * lppStream [O] Destination for created IStream object
515 *
516 * RETURNS
517 * Success: S_OK. lppStream contains the new IStream object
518 * Failure: E_INVALIDARG if any parameter is invalid, or an HRESULT error code
519 */
520 HRESULT WINAPI SHCreateStreamOnFileA(LPCSTR lpszPath, DWORD dwMode,
521 IStream **lppStream)
522 {
523 WCHAR szPath[MAX_PATH];
524
525 TRACE("(%s,%d,%p)\n", debugstr_a(lpszPath), dwMode, lppStream);
526
527 if (!lpszPath)
528 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
529
530 MultiByteToWideChar(0, 0, lpszPath, -1, szPath, MAX_PATH);
531 return SHCreateStreamOnFileW(szPath, dwMode, lppStream);
532 }
533
534 /*************************************************************************
535 * @ [SHLWAPI.184]
536 *
537 * Call IStream_Read() on a stream.
538 *
539 * PARAMS
540 * lpStream [I] IStream object
541 * lpvDest [O] Destination for data read
542 * ulSize [I] Size of data to read
543 *
544 * RETURNS
545 * Success: S_OK. ulSize bytes have been read from the stream into lpvDest.
546 * Failure: An HRESULT error code, or E_FAIL if the read succeeded but the
547 * number of bytes read does not match.
548 */
549 HRESULT WINAPI SHIStream_Read(IStream *lpStream, LPVOID lpvDest, ULONG ulSize)
550 {
551 ULONG ulRead;
552 HRESULT hRet;
553
554 TRACE("(%p,%p,%d)\n", lpStream, lpvDest, ulSize);
555
556 hRet = IStream_Read(lpStream, lpvDest, ulSize, &ulRead);
557
558 if (SUCCEEDED(hRet) && ulRead != ulSize)
559 hRet = E_FAIL;
560 return hRet;
561 }
562
563 /*************************************************************************
564 * @ [SHLWAPI.166]
565 *
566 * Determine if a stream has 0 length.
567 *
568 * PARAMS
569 * lpStream [I] IStream object
570 *
571 * RETURNS
572 * TRUE: If the stream has 0 length
573 * FALSE: Otherwise.
574 */
575 BOOL WINAPI SHIsEmptyStream(IStream *lpStream)
576 {
577 STATSTG statstg;
578 BOOL bRet = TRUE;
579
580 TRACE("(%p)\n", lpStream);
581
582 memset(&statstg, 0, sizeof(statstg));
583
584 if(SUCCEEDED(IStream_Stat(lpStream, &statstg, 1)))
585 {
586 if(statstg.cbSize.QuadPart)
587 bRet = FALSE; /* Non-Zero */
588 }
589 else
590 {
591 DWORD dwDummy;
592
593 /* Try to read from the stream */
594 if(SUCCEEDED(SHIStream_Read(lpStream, &dwDummy, sizeof(dwDummy))))
595 {
596 LARGE_INTEGER zero;
597 zero.QuadPart = 0;
598
599 IStream_Seek(lpStream, zero, 0, NULL);
600 bRet = FALSE; /* Non-Zero */
601 }
602 }
603 return bRet;
604 }
605
606 /*************************************************************************
607 * @ [SHLWAPI.212]
608 *
609 * Call IStream_Write() on a stream.
610 *
611 * PARAMS
612 * lpStream [I] IStream object
613 * lpvSrc [I] Source for data to write
614 * ulSize [I] Size of data
615 *
616 * RETURNS
617 * Success: S_OK. ulSize bytes have been written to the stream from lpvSrc.
618 * Failure: An HRESULT error code, or E_FAIL if the write succeeded but the
619 * number of bytes written does not match.
620 */
621 HRESULT WINAPI SHIStream_Write(IStream *lpStream, LPCVOID lpvSrc, ULONG ulSize)
622 {
623 ULONG ulWritten;
624 HRESULT hRet;
625
626 TRACE("(%p,%p,%d)\n", lpStream, lpvSrc, ulSize);
627
628 hRet = IStream_Write(lpStream, lpvSrc, ulSize, &ulWritten);
629
630 if (SUCCEEDED(hRet) && ulWritten != ulSize)
631 hRet = E_FAIL;
632
633 return hRet;
634 }
635
636 /*************************************************************************
637 * @ [SHLWAPI.213]
638 *
639 * Seek to the start of a stream.
640 *
641 * PARAMS
642 * lpStream [I] IStream object
643 *
644 * RETURNS
645 * Success: S_OK. The current position within the stream is updated
646 * Failure: An HRESULT error code.
647 */
648 HRESULT WINAPI IStream_Reset(IStream *lpStream)
649 {
650 LARGE_INTEGER zero;
651 TRACE("(%p)\n", lpStream);
652 zero.QuadPart = 0;
653 return IStream_Seek(lpStream, zero, 0, NULL);
654 }
655
656 /*************************************************************************
657 * @ [SHLWAPI.214]
658 *
659 * Get the size of a stream.
660 *
661 * PARAMS
662 * lpStream [I] IStream object
663 * lpulSize [O] Destination for size
664 *
665 * RETURNS
666 * Success: S_OK. lpulSize contains the size of the stream.
667 * Failure: An HRESULT error code.
668 */
669 HRESULT WINAPI IStream_Size(IStream *lpStream, ULARGE_INTEGER* lpulSize)
670 {
671 STATSTG statstg;
672 HRESULT hRet;
673
674 TRACE("(%p,%p)\n", lpStream, lpulSize);
675
676 memset(&statstg, 0, sizeof(statstg));
677
678 hRet = IStream_Stat(lpStream, &statstg, 1);
679
680 if (SUCCEEDED(hRet) && lpulSize)
681 *lpulSize = statstg.cbSize;
682 return hRet;
683 }