ReactOS 0.4.17-dev-806-gffa4164
cabinet_main.c
Go to the documentation of this file.
1/*
2 * cabinet.dll main
3 *
4 * Copyright 2002 Patrik Stridvall
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
21#include <assert.h>
22#include <stdarg.h>
23#include <string.h>
24#include <fcntl.h>
25
26#include "windef.h"
27#include "winbase.h"
28#include "winerror.h"
29#define NO_SHLWAPI_REG
30#include "shlwapi.h"
31#undef NO_SHLWAPI_REG
32
33#include "cabinet.h"
34
35#include "wine/debug.h"
36
38
39
40/***********************************************************************
41 * DllGetVersion (CABINET.2)
42 *
43 * Retrieves version information of the 'CABINET.DLL'
44 *
45 * PARAMS
46 * pdvi [O] pointer to version information structure.
47 *
48 * RETURNS
49 * Success: S_OK
50 * Failure: E_INVALIDARG
51 *
52 * NOTES
53 * Supposedly returns version from IE6SP1RP1
54 */
56{
57 WARN("hmmm... not right version number \"5.1.1106.1\"?\n");
58
59 if (pdvi->cbSize != sizeof(DLLVERSIONINFO)) return E_INVALIDARG;
60
61 pdvi->dwMajorVersion = 5;
62 pdvi->dwMinorVersion = 1;
63 pdvi->dwBuildNumber = 1106;
64 pdvi->dwPlatformID = 1;
65
66 return S_OK;
67}
68
69/* FDI callback functions */
70
71static void * CDECL mem_alloc(ULONG cb)
72{
73 return HeapAlloc(GetProcessHeap(), 0, cb);
74}
75
76static void CDECL mem_free(void *memory)
77{
79}
80
81static INT_PTR CDECL fdi_open(char *pszFile, int oflag, int pmode)
82{
84 DWORD dwAccess = 0;
85 DWORD dwShareMode = 0;
86 DWORD dwCreateDisposition;
87
88 switch (oflag & _O_ACCMODE)
89 {
90 case _O_RDONLY:
91 dwAccess = GENERIC_READ;
92 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
93 break;
94 case _O_WRONLY:
95 dwAccess = GENERIC_WRITE;
97 break;
98 case _O_RDWR:
99 dwAccess = GENERIC_READ | GENERIC_WRITE;
101 break;
102 }
103
104 if (oflag & _O_CREAT)
105 {
106 dwCreateDisposition = OPEN_ALWAYS;
107 if (oflag & _O_EXCL) dwCreateDisposition = CREATE_NEW;
108 else if (oflag & _O_TRUNC) dwCreateDisposition = CREATE_ALWAYS;
109 }
110 else
111 {
112 dwCreateDisposition = OPEN_EXISTING;
113 if (oflag & _O_TRUNC) dwCreateDisposition = TRUNCATE_EXISTING;
114 }
115
116 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
117 dwCreateDisposition, 0, NULL);
118
119 return (INT_PTR) handle;
120}
121
122static UINT CDECL fdi_read(INT_PTR hf, void *pv, UINT cb)
123{
124 HANDLE handle = (HANDLE) hf;
125 DWORD dwRead;
126
127 if (ReadFile(handle, pv, cb, &dwRead, NULL))
128 return dwRead;
129
130 return 0;
131}
132
133static UINT CDECL fdi_write(INT_PTR hf, void *pv, UINT cb)
134{
135 HANDLE handle = (HANDLE) hf;
136 DWORD dwWritten;
137
138 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
139 return dwWritten;
140
141 return 0;
142}
143
144static int CDECL fdi_close(INT_PTR hf)
145{
146 HANDLE handle = (HANDLE) hf;
147 return CloseHandle(handle) ? 0 : -1;
148}
149
150static LONG CDECL fdi_seek(INT_PTR hf, LONG dist, int seektype)
151{
152 HANDLE handle = (HANDLE) hf;
153 return SetFilePointer(handle, dist, NULL, seektype);
154}
155
156static void fill_file_node(struct FILELIST *pNode, LPCSTR szFilename)
157{
158 pNode->next = NULL;
159 pNode->DoExtract = FALSE;
160
161 pNode->FileName = HeapAlloc(GetProcessHeap(), 0, strlen(szFilename) + 1);
162 lstrcpyA(pNode->FileName, szFilename);
163}
164
165static BOOL file_in_list(struct FILELIST *pNode, LPCSTR szFilename,
166 struct FILELIST **pOut)
167{
168 while (pNode)
169 {
170 if (!lstrcmpiA(pNode->FileName, szFilename))
171 {
172 if (pOut)
173 *pOut = pNode;
174
175 return TRUE;
176 }
177
178 pNode = pNode->next;
179 }
180
181 return FALSE;
182}
183
185{
186 switch (fdint)
187 {
188 case fdintCOPY_FILE:
189 {
190 struct FILELIST *fileList, *node = NULL;
191 SESSION *pDestination = pfdin->pv;
192 LPSTR szFullPath, szDirectory;
193 HANDLE hFile = 0;
195
196 dwSize = lstrlenA(pDestination->Destination) +
197 lstrlenA("\\") + lstrlenA(pfdin->psz1) + 1;
198 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwSize);
199
200 lstrcpyA(szFullPath, pDestination->Destination);
201 lstrcatA(szFullPath, "\\");
202 lstrcatA(szFullPath, pfdin->psz1);
203
204 /* pull out the destination directory string from the full path */
205 dwSize = strrchr(szFullPath, '\\') - szFullPath + 1;
206 szDirectory = HeapAlloc(GetProcessHeap(), 0, dwSize);
207 lstrcpynA(szDirectory, szFullPath, dwSize);
208
209 pDestination->FileSize += pfdin->cb;
210
211 if (pDestination->Operation & EXTRACT_FILLFILELIST)
212 {
213 fileList = HeapAlloc(GetProcessHeap(), 0,
214 sizeof(struct FILELIST));
215
216 fill_file_node(fileList, pfdin->psz1);
217 fileList->DoExtract = TRUE;
218 fileList->next = pDestination->FileList;
219 pDestination->FileList = fileList;
220 lstrcpyA(pDestination->CurrentFile, szFullPath);
221 pDestination->FileCount++;
222 }
223
224 if ((pDestination->Operation & EXTRACT_EXTRACTFILES) ||
225 file_in_list(pDestination->FilterList, pfdin->psz1, NULL))
226 {
227 /* find the file node */
228 file_in_list(pDestination->FileList, pfdin->psz1, &node);
229
230 if (node && !node->DoExtract)
231 {
232 HeapFree(GetProcessHeap(), 0, szFullPath);
233 HeapFree(GetProcessHeap(), 0, szDirectory);
234 return 0;
235 }
236
237 /* create the destination directory if it doesn't exist */
239 {
240 char *ptr;
241
242 for(ptr = szDirectory + strlen(pDestination->Destination)+1; *ptr; ptr++) {
243 if(*ptr == '\\') {
244 *ptr = 0;
245 CreateDirectoryA(szDirectory, NULL);
246 *ptr = '\\';
247 }
248 }
249 CreateDirectoryA(szDirectory, NULL);
250 }
251
252 hFile = CreateFileA(szFullPath, GENERIC_READ | GENERIC_WRITE, 0, NULL,
254
256 node->DoExtract = FALSE;
257 }
258
259 HeapFree(GetProcessHeap(), 0, szFullPath);
260 HeapFree(GetProcessHeap(), 0, szDirectory);
261
262 return (INT_PTR) hFile;
263 }
264
266 {
267 FILETIME ft;
268 FILETIME ftLocal;
269 HANDLE handle = (HANDLE) pfdin->hf;
270
271 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
272 return FALSE;
273
274 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
275 return FALSE;
276
277 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
278 return FALSE;
279
281 return TRUE;
282 }
283
284 default:
285 return 0;
286 }
287}
288
289/***********************************************************************
290 * Extract (CABINET.3)
291 *
292 * Extracts the contents of the cabinet file to the specified
293 * destination.
294 *
295 * PARAMS
296 * dest [I/O] Controls the operation of Extract. See NOTES.
297 * szCabName [I] Filename of the cabinet to extract.
298 *
299 * RETURNS
300 * Success: S_OK.
301 * Failure: E_FAIL.
302 *
303 * NOTES
304 * The following members of the dest struct control the operation
305 * of Extract:
306 * FileSize [O] The size of all files extracted up to CurrentFile.
307 * Error [O] The error in case the extract operation fails.
308 * FileList [I] A linked list of filenames. Extract only extracts
309 * files from the cabinet that are in this list.
310 * FileCount [O] Contains the number of files in FileList on
311 * completion.
312 * Operation [I] See Operation.
313 * Destination [I] The destination directory.
314 * CurrentFile [O] The last file extracted.
315 * FilterList [I] A linked list of files that should not be extracted.
316 *
317 * Operation
318 * If Operation contains EXTRACT_FILLFILELIST, then FileList will be
319 * filled with all the files in the cabinet. If Operation contains
320 * EXTRACT_EXTRACTFILES, then only the files in the FileList will
321 * be extracted from the cabinet. EXTRACT_FILLFILELIST can be called
322 * by itself, but EXTRACT_EXTRACTFILES must have a valid FileList
323 * in order to succeed. If Operation contains both EXTRACT_FILLFILELIST
324 * and EXTRACT_EXTRACTFILES, then all the files in the cabinet
325 * will be extracted.
326 */
328{
329 HRESULT res = S_OK;
330 HFDI hfdi;
331 char *str, *end, *path = NULL, *name = NULL;
332
333 TRACE("(%p, %s)\n", dest, debugstr_a(szCabName));
334
335 hfdi = FDICreate(mem_alloc,
336 mem_free,
337 fdi_open,
338 fdi_read,
339 fdi_write,
340 fdi_close,
341 fdi_seek,
343 &dest->Error);
344
345 if (!hfdi)
346 return E_FAIL;
347
349 {
350 res = S_OK;
351 goto end;
352 }
353
354 /* split the cabinet name into path + name */
355 str = HeapAlloc(GetProcessHeap(), 0, lstrlenA(szCabName)+1);
356 if (!str)
357 {
359 goto end;
360 }
361 lstrcpyA(str, szCabName);
362
363 if ((end = strrchr(str, '\\')))
364 {
365 path = str;
366 end++;
367 name = HeapAlloc( GetProcessHeap(), 0, strlen(end) + 1 );
368 if (!name)
369 {
371 goto end;
372 }
373 strcpy( name, end );
374 *end = 0;
375 }
376 else
377 {
378 name = str;
379 path = NULL;
380 }
381
382 dest->FileSize = 0;
383
384 if (!FDICopy(hfdi, name, path, 0,
387
388end:
391 FDIDestroy(hfdi);
392 return res;
393}
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define WARN(fmt,...)
Definition: precomp.h:61
static INT_PTR CDECL fdi_notify_extract(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
Definition: cabinet_main.c:184
static BOOL file_in_list(struct FILELIST *pNode, LPCSTR szFilename, struct FILELIST **pOut)
Definition: cabinet_main.c:165
static UINT CDECL fdi_read(INT_PTR hf, void *pv, UINT cb)
Definition: cabinet_main.c:122
HRESULT WINAPI Extract(SESSION *dest, LPCSTR szCabName)
Definition: cabinet_main.c:327
static LONG CDECL fdi_seek(INT_PTR hf, LONG dist, int seektype)
Definition: cabinet_main.c:150
static int CDECL fdi_close(INT_PTR hf)
Definition: cabinet_main.c:144
static void fill_file_node(struct FILELIST *pNode, LPCSTR szFilename)
Definition: cabinet_main.c:156
static UINT CDECL fdi_write(INT_PTR hf, void *pv, UINT cb)
Definition: cabinet_main.c:133
static INT_PTR CDECL fdi_open(char *pszFile, int oflag, int pmode)
Definition: cabinet_main.c:81
HRESULT WINAPI DllGetVersion(DLLVERSIONINFO *pdvi)
Definition: cabinet_main.c:55
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define E_INVALIDARG
Definition: ddrawi.h:101
#define E_FAIL
Definition: ddrawi.h:102
#define mem_free(ptr, bsize)
Definition: types.h:124
#define NULL
Definition: types.h:112
#define mem_alloc(bsize)
Definition: types.h:123
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define EXTRACT_EXTRACTFILES
Definition: files.c:518
#define EXTRACT_FILLFILELIST
Definition: files.c:517
HFDI __cdecl FDICreate(PFNALLOC pfnalloc, PFNFREE pfnfree, PFNOPEN pfnopen, PFNREAD pfnread, PFNWRITE pfnwrite, PFNCLOSE pfnclose, PFNSEEK pfnseek, int cpuType, PERF perf)
Definition: fdi.c:412
BOOL __cdecl FDICopy(HFDI hfdi, char *pszCabinet, char *pszCabPath, int flags, PFNFDINOTIFY pfnfdin, PFNFDIDECRYPT pfnfdid, void *pvUser)
Definition: fdi.c:2420
BOOL __cdecl FDIDestroy(HFDI hfdi)
Definition: fdi.c:2820
#define CDECL
Definition: compat.h:29
#define CloseHandle
Definition: compat.h:739
#define GetProcessHeap()
Definition: compat.h:736
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define SetFilePointer
Definition: compat.h:743
#define lstrcpynA
Definition: compat.h:751
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define HeapAlloc
Definition: compat.h:733
#define CreateFileA(a, b, c, d, e, f, g)
Definition: compat.h:740
#define GENERIC_READ
Definition: compat.h:135
#define HeapFree(x, y, z)
Definition: compat.h:735
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
BOOL WINAPI CreateDirectoryA(IN LPCSTR lpPathName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: dir.c:37
BOOL WINAPI SetFileTime(IN HANDLE hFile, CONST FILETIME *lpCreationTime OPTIONAL, CONST FILETIME *lpLastAccessTime OPTIONAL, CONST FILETIME *lpLastWriteTime OPTIONAL)
Definition: fileinfo.c:933
DWORD WINAPI GetFileAttributesA(LPCSTR lpFileName)
Definition: fileinfo.c:620
BOOL WINAPI WriteFile(_In_ HANDLE hFile, _In_reads_bytes_opt_(nNumberOfBytesToWrite) LPCVOID lpBuffer, _In_ DWORD nNumberOfBytesToWrite, _Out_opt_ LPDWORD lpNumberOfBytesWritten, _Inout_opt_ LPOVERLAPPED lpOverlapped)
Definition: rw.c:25
BOOL WINAPI DosDateTimeToFileTime(IN WORD wFatDate, IN WORD wFatTime, OUT LPFILETIME lpFileTime)
Definition: time.c:75
BOOL WINAPI LocalFileTimeToFileTime(IN CONST FILETIME *lpLocalFileTime, OUT LPFILETIME lpFileTime)
Definition: time.c:244
int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
Definition: locale.c:4133
static MonoProfilerRuntimeShutdownBeginCallback cb
Definition: metahost.c:118
#define _O_RDWR
Definition: fcntl.h:15
#define _O_ACCMODE
Definition: fcntl.h:16
#define _O_RDONLY
Definition: fcntl.h:13
#define _O_TRUNC
Definition: fcntl.h:23
#define _O_CREAT
Definition: fcntl.h:22
#define _O_EXCL
Definition: fcntl.h:24
#define _O_WRONLY
Definition: fcntl.h:14
_ACRTIMP size_t __cdecl strlen(const char *)
Definition: string.c:1597
_ACRTIMP char *__cdecl strrchr(const char *, int)
Definition: string.c:3303
FDINOTIFICATIONTYPE
Definition: fdi.h:246
@ fdintCOPY_FILE
Definition: fdi.h:249
@ fdintCLOSE_FILE_INFO
Definition: fdi.h:250
#define cpuUNKNOWN
Definition: fdi.h:269
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint GLuint end
Definition: gl.h:1545
GLuint res
Definition: glext.h:9613
unsigned int UINT
Definition: sysinfo.c:13
#define S_OK
Definition: intsafe.h:52
#define debugstr_a
Definition: kernel32.h:31
if(dx< 0)
Definition: linetemp.h:194
LPSTR WINAPI lstrcpyA(LPSTR lpString1, LPCSTR lpString2)
Definition: lstring.c:100
LPSTR WINAPI lstrcatA(LPSTR lpString1, LPCSTR lpString2)
Definition: lstring.c:123
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
#define CREATE_ALWAYS
Definition: disk.h:72
#define TRUNCATE_EXISTING
Definition: disk.h:71
#define CREATE_NEW
Definition: disk.h:69
#define OPEN_ALWAYS
Definition: disk.h:70
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
static PVOID ptr
Definition: dispmode.c:27
static char memory[1024 *256]
Definition: process.c:122
static char * dest
Definition: rtl.c:149
_In_ HANDLE hFile
Definition: mswsock.h:90
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
#define GENERIC_WRITE
Definition: nt_native.h:90
long LONG
Definition: pedump.c:60
const WCHAR * str
strcpy
Definition: string.h:131
#define TRACE(s)
Definition: solgame.cpp:4
USHORT time
Definition: fdi.h:236
void * pv
Definition: fdi.h:231
INT_PTR hf
Definition: fdi.h:233
char * psz1
Definition: fdi.h:228
USHORT date
Definition: fdi.h:235
LONG cb
Definition: fdi.h:227
struct FILELIST * next
Definition: files.c:522
LPSTR FileName
Definition: files.c:521
BOOL DoExtract
Definition: files.c:523
Definition: files.c:526
struct FILELIST * FilterList
Definition: files.c:535
INT Operation
Definition: files.c:531
INT FileSize
Definition: files.c:527
CHAR CurrentFile[MAX_PATH]
Definition: files.c:533
struct FILELIST * FileList
Definition: files.c:529
INT FileCount
Definition: files.c:530
CHAR Destination[MAX_PATH]
Definition: files.c:532
DWORD dwMajorVersion
Definition: shlwapi.h:121
DWORD dwBuildNumber
Definition: shlwapi.h:123
DWORD dwMinorVersion
Definition: shlwapi.h:122
DWORD cbSize
Definition: shlwapi.h:120
DWORD dwPlatformID
Definition: shlwapi.h:124
Definition: name.c:39
const char * LPCSTR
Definition: typedefs.h:52
int32_t INT_PTR
Definition: typedefs.h:64
PVOID HANDLE
Definition: typedefs.h:73
char * LPSTR
Definition: typedefs.h:51
uint32_t ULONG
Definition: typedefs.h:59
Definition: dlist.c:348
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define WINAPI
Definition: msvc.h:6
static HRESULT HRESULT_FROM_WIN32(unsigned int x)
Definition: winerror.h:210