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