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