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