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