Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/dlls/cabinet/cabinet_main.c
4393 views
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
#include "shlwapi.h"
30
31
#include "cabinet.h"
32
33
#include "wine/debug.h"
34
35
WINE_DEFAULT_DEBUG_CHANNEL(cabinet);
36
37
typedef struct
38
{
39
DWORD cbStruct;
40
DWORD dwReserved1;
41
DWORD dwReserved2;
42
DWORD dwFileVersionMS;
43
DWORD dwFileVersionLS;
44
} CABINETDLLVERSIONINFO, *PCABINETDLLVERSIONINFO;
45
46
/***********************************************************************
47
* DllGetVersion (CABINET.2)
48
*/
49
void WINAPI cabinet_DllGetVersion( CABINETDLLVERSIONINFO *info )
50
{
51
DLLVERSIONINFO2 ver = { .info1.cbSize = sizeof(ver) };
52
53
if (info->cbStruct < sizeof(*info)) return;
54
DllGetVersion( &ver.info1 );
55
info->dwFileVersionMS = ver.ullVersion >> 32;
56
info->dwFileVersionLS = ver.ullVersion;
57
}
58
59
/* FDI callback functions */
60
61
static void * CDECL mem_alloc(ULONG cb)
62
{
63
return HeapAlloc(GetProcessHeap(), 0, cb);
64
}
65
66
static void CDECL mem_free(void *memory)
67
{
68
HeapFree(GetProcessHeap(), 0, memory);
69
}
70
71
static INT_PTR CDECL fdi_open(char *pszFile, int oflag, int pmode)
72
{
73
HANDLE handle;
74
DWORD dwAccess = 0;
75
DWORD dwShareMode = 0;
76
DWORD dwCreateDisposition;
77
78
switch (oflag & _O_ACCMODE)
79
{
80
case _O_RDONLY:
81
dwAccess = GENERIC_READ;
82
dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
83
break;
84
case _O_WRONLY:
85
dwAccess = GENERIC_WRITE;
86
dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
87
break;
88
case _O_RDWR:
89
dwAccess = GENERIC_READ | GENERIC_WRITE;
90
dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
91
break;
92
}
93
94
if (oflag & _O_CREAT)
95
{
96
dwCreateDisposition = OPEN_ALWAYS;
97
if (oflag & _O_EXCL) dwCreateDisposition = CREATE_NEW;
98
else if (oflag & _O_TRUNC) dwCreateDisposition = CREATE_ALWAYS;
99
}
100
else
101
{
102
dwCreateDisposition = OPEN_EXISTING;
103
if (oflag & _O_TRUNC) dwCreateDisposition = TRUNCATE_EXISTING;
104
}
105
106
handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
107
dwCreateDisposition, 0, NULL);
108
109
return (INT_PTR) handle;
110
}
111
112
static UINT CDECL fdi_read(INT_PTR hf, void *pv, UINT cb)
113
{
114
HANDLE handle = (HANDLE) hf;
115
DWORD dwRead;
116
117
if (ReadFile(handle, pv, cb, &dwRead, NULL))
118
return dwRead;
119
120
return 0;
121
}
122
123
static UINT CDECL fdi_write(INT_PTR hf, void *pv, UINT cb)
124
{
125
HANDLE handle = (HANDLE) hf;
126
DWORD dwWritten;
127
128
if (WriteFile(handle, pv, cb, &dwWritten, NULL))
129
return dwWritten;
130
131
return 0;
132
}
133
134
static int CDECL fdi_close(INT_PTR hf)
135
{
136
HANDLE handle = (HANDLE) hf;
137
return CloseHandle(handle) ? 0 : -1;
138
}
139
140
static LONG CDECL fdi_seek(INT_PTR hf, LONG dist, int seektype)
141
{
142
HANDLE handle = (HANDLE) hf;
143
return SetFilePointer(handle, dist, NULL, seektype);
144
}
145
146
static void fill_file_node(struct FILELIST *pNode, LPCSTR szFilename)
147
{
148
pNode->next = NULL;
149
pNode->DoExtract = FALSE;
150
151
pNode->FileName = HeapAlloc(GetProcessHeap(), 0, strlen(szFilename) + 1);
152
lstrcpyA(pNode->FileName, szFilename);
153
}
154
155
static BOOL file_in_list(struct FILELIST *pNode, LPCSTR szFilename,
156
struct FILELIST **pOut)
157
{
158
while (pNode)
159
{
160
if (!lstrcmpiA(pNode->FileName, szFilename))
161
{
162
if (pOut)
163
*pOut = pNode;
164
165
return TRUE;
166
}
167
168
pNode = pNode->next;
169
}
170
171
return FALSE;
172
}
173
174
static INT_PTR CDECL fdi_notify_extract(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
175
{
176
switch (fdint)
177
{
178
case fdintCOPY_FILE:
179
{
180
struct FILELIST *fileList, *node = NULL;
181
SESSION *pDestination = pfdin->pv;
182
LPSTR szFullPath, szDirectory;
183
HANDLE hFile = 0;
184
DWORD dwSize;
185
186
dwSize = lstrlenA(pDestination->Destination) +
187
lstrlenA("\\") + lstrlenA(pfdin->psz1) + 1;
188
szFullPath = HeapAlloc(GetProcessHeap(), 0, dwSize);
189
190
lstrcpyA(szFullPath, pDestination->Destination);
191
lstrcatA(szFullPath, "\\");
192
lstrcatA(szFullPath, pfdin->psz1);
193
194
/* pull out the destination directory string from the full path */
195
dwSize = strrchr(szFullPath, '\\') - szFullPath + 1;
196
szDirectory = HeapAlloc(GetProcessHeap(), 0, dwSize);
197
lstrcpynA(szDirectory, szFullPath, dwSize);
198
199
pDestination->FileSize += pfdin->cb;
200
201
if (pDestination->Operation & EXTRACT_FILLFILELIST)
202
{
203
fileList = HeapAlloc(GetProcessHeap(), 0,
204
sizeof(struct FILELIST));
205
206
fill_file_node(fileList, pfdin->psz1);
207
fileList->DoExtract = TRUE;
208
fileList->next = pDestination->FileList;
209
pDestination->FileList = fileList;
210
lstrcpyA(pDestination->CurrentFile, szFullPath);
211
pDestination->FileCount++;
212
}
213
214
if ((pDestination->Operation & EXTRACT_EXTRACTFILES) ||
215
file_in_list(pDestination->FilterList, pfdin->psz1, NULL))
216
{
217
/* find the file node */
218
file_in_list(pDestination->FileList, pfdin->psz1, &node);
219
220
if (node && !node->DoExtract)
221
{
222
HeapFree(GetProcessHeap(), 0, szFullPath);
223
HeapFree(GetProcessHeap(), 0, szDirectory);
224
return 0;
225
}
226
227
/* create the destination directory if it doesn't exist */
228
if (GetFileAttributesA(szDirectory) == INVALID_FILE_ATTRIBUTES)
229
{
230
char *ptr;
231
232
for(ptr = szDirectory + strlen(pDestination->Destination)+1; *ptr; ptr++) {
233
if(*ptr == '\\') {
234
*ptr = 0;
235
CreateDirectoryA(szDirectory, NULL);
236
*ptr = '\\';
237
}
238
}
239
CreateDirectoryA(szDirectory, NULL);
240
}
241
242
hFile = CreateFileA(szFullPath, GENERIC_READ | GENERIC_WRITE, 0, NULL,
243
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
244
245
if (hFile != INVALID_HANDLE_VALUE && node)
246
node->DoExtract = FALSE;
247
}
248
249
HeapFree(GetProcessHeap(), 0, szFullPath);
250
HeapFree(GetProcessHeap(), 0, szDirectory);
251
252
return (INT_PTR) hFile;
253
}
254
255
case fdintCLOSE_FILE_INFO:
256
{
257
FILETIME ft;
258
FILETIME ftLocal;
259
HANDLE handle = (HANDLE) pfdin->hf;
260
261
if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
262
return FALSE;
263
264
if (!LocalFileTimeToFileTime(&ft, &ftLocal))
265
return FALSE;
266
267
if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
268
return FALSE;
269
270
CloseHandle(handle);
271
return TRUE;
272
}
273
274
default:
275
return 0;
276
}
277
}
278
279
/***********************************************************************
280
* Extract (CABINET.3)
281
*
282
* Extracts the contents of the cabinet file to the specified
283
* destination.
284
*
285
* PARAMS
286
* dest [I/O] Controls the operation of Extract. See NOTES.
287
* szCabName [I] Filename of the cabinet to extract.
288
*
289
* RETURNS
290
* Success: S_OK.
291
* Failure: E_FAIL.
292
*
293
* NOTES
294
* The following members of the dest struct control the operation
295
* of Extract:
296
* FileSize [O] The size of all files extracted up to CurrentFile.
297
* Error [O] The error in case the extract operation fails.
298
* FileList [I] A linked list of filenames. Extract only extracts
299
* files from the cabinet that are in this list.
300
* FileCount [O] Contains the number of files in FileList on
301
* completion.
302
* Operation [I] See Operation.
303
* Destination [I] The destination directory.
304
* CurrentFile [O] The last file extracted.
305
* FilterList [I] A linked list of files that should not be extracted.
306
*
307
* Operation
308
* If Operation contains EXTRACT_FILLFILELIST, then FileList will be
309
* filled with all the files in the cabinet. If Operation contains
310
* EXTRACT_EXTRACTFILES, then only the files in the FileList will
311
* be extracted from the cabinet. EXTRACT_FILLFILELIST can be called
312
* by itself, but EXTRACT_EXTRACTFILES must have a valid FileList
313
* in order to succeed. If Operation contains both EXTRACT_FILLFILELIST
314
* and EXTRACT_EXTRACTFILES, then all the files in the cabinet
315
* will be extracted.
316
*/
317
HRESULT WINAPI Extract(SESSION *dest, LPCSTR szCabName)
318
{
319
HRESULT res = S_OK;
320
HFDI hfdi;
321
char *str, *end, *path = NULL, *name = NULL;
322
323
TRACE("(%p, %s)\n", dest, debugstr_a(szCabName));
324
325
hfdi = FDICreate(mem_alloc,
326
mem_free,
327
fdi_open,
328
fdi_read,
329
fdi_write,
330
fdi_close,
331
fdi_seek,
332
cpuUNKNOWN,
333
&dest->Error);
334
335
if (!hfdi)
336
return E_FAIL;
337
338
if (GetFileAttributesA(dest->Destination) == INVALID_FILE_ATTRIBUTES)
339
{
340
res = S_OK;
341
goto end;
342
}
343
344
/* split the cabinet name into path + name */
345
str = HeapAlloc(GetProcessHeap(), 0, lstrlenA(szCabName)+1);
346
if (!str)
347
{
348
res = E_OUTOFMEMORY;
349
goto end;
350
}
351
lstrcpyA(str, szCabName);
352
353
if ((end = strrchr(str, '\\')))
354
{
355
path = str;
356
end++;
357
name = HeapAlloc( GetProcessHeap(), 0, strlen(end) + 1 );
358
if (!name)
359
{
360
res = E_OUTOFMEMORY;
361
goto end;
362
}
363
strcpy( name, end );
364
*end = 0;
365
}
366
else
367
{
368
name = str;
369
path = NULL;
370
}
371
372
dest->FileSize = 0;
373
374
if (!FDICopy(hfdi, name, path, 0,
375
fdi_notify_extract, NULL, dest))
376
res = HRESULT_FROM_WIN32(GetLastError());
377
378
end:
379
HeapFree(GetProcessHeap(), 0, path);
380
HeapFree(GetProcessHeap(), 0, name);
381
FDIDestroy(hfdi);
382
return res;
383
}
384
385