push 599a9d3db769aad8dabfd10a59120f719b00f4ee
[wine/hacks.git] / dlls / cabinet / cabinet_main.c
blob8b3c621e1609c015900fde3db8674daffab25514
1 /*
2 * cabinet.dll main
4 * Copyright 2002 Patrik Stridvall
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.
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.
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
21 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #define NO_SHLWAPI_REG
31 #include "shlwapi.h"
32 #undef NO_SHLWAPI_REG
34 #include "cabinet.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(cabinet);
41 /***********************************************************************
42 * DllGetVersion (CABINET.2)
44 * Retrieves version information of the 'CABINET.DLL'
46 * PARAMS
47 * pdvi [O] pointer to version information structure.
49 * RETURNS
50 * Success: S_OK
51 * Failure: E_INVALIDARG
53 * NOTES
54 * Supposedly returns version from IE6SP1RP1
56 HRESULT WINAPI DllGetVersion (DLLVERSIONINFO *pdvi)
58 WARN("hmmm... not right version number \"5.1.1106.1\"?\n");
60 if (pdvi->cbSize != sizeof(DLLVERSIONINFO)) return E_INVALIDARG;
62 pdvi->dwMajorVersion = 5;
63 pdvi->dwMinorVersion = 1;
64 pdvi->dwBuildNumber = 1106;
65 pdvi->dwPlatformID = 1;
67 return S_OK;
70 /* FDI callback functions */
72 static void *mem_alloc(ULONG cb)
74 return HeapAlloc(GetProcessHeap(), 0, cb);
77 static void mem_free(void *memory)
79 HeapFree(GetProcessHeap(), 0, memory);
82 static INT_PTR fdi_open(char *pszFile, int oflag, int pmode)
84 HANDLE handle;
85 DWORD dwAccess = 0;
86 DWORD dwShareMode = 0;
87 DWORD dwCreateDisposition;
89 switch (oflag & _O_ACCMODE)
91 case _O_RDONLY:
92 dwAccess = GENERIC_READ;
93 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
94 break;
95 case _O_WRONLY:
96 dwAccess = GENERIC_WRITE;
97 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
98 break;
99 case _O_RDWR:
100 dwAccess = GENERIC_READ | GENERIC_WRITE;
101 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
102 break;
105 if (oflag & _O_CREAT)
107 dwCreateDisposition = OPEN_ALWAYS;
108 if (oflag & _O_EXCL) dwCreateDisposition = CREATE_NEW;
109 else if (oflag & _O_TRUNC) dwCreateDisposition = CREATE_ALWAYS;
111 else
113 dwCreateDisposition = OPEN_EXISTING;
114 if (oflag & _O_TRUNC) dwCreateDisposition = TRUNCATE_EXISTING;
117 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
118 dwCreateDisposition, 0, NULL);
120 return (INT_PTR) handle;
123 static UINT fdi_read(INT_PTR hf, void *pv, UINT cb)
125 HANDLE handle = (HANDLE) hf;
126 DWORD dwRead;
128 if (ReadFile(handle, pv, cb, &dwRead, NULL))
129 return dwRead;
131 return 0;
134 static UINT fdi_write(INT_PTR hf, void *pv, UINT cb)
136 HANDLE handle = (HANDLE) hf;
137 DWORD dwWritten;
139 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
140 return dwWritten;
142 return 0;
145 static int fdi_close(INT_PTR hf)
147 HANDLE handle = (HANDLE) hf;
148 return CloseHandle(handle) ? 0 : -1;
151 static long fdi_seek(INT_PTR hf, long dist, int seektype)
153 HANDLE handle = (HANDLE) hf;
154 return SetFilePointer(handle, dist, NULL, seektype);
157 static void fill_file_node(struct FILELIST *pNode, LPCSTR szFilename)
159 pNode->next = NULL;
160 pNode->Extracted = FALSE;
162 pNode->FileName = HeapAlloc(GetProcessHeap(), 0, strlen(szFilename) + 1);
163 lstrcpyA(pNode->FileName, szFilename);
166 static BOOL file_in_list(const struct FILELIST *pNode, LPCSTR szFilename)
168 while (pNode)
170 if (!lstrcmpiA(pNode->FileName, szFilename))
171 return TRUE;
173 pNode = pNode->next;
176 return FALSE;
179 static INT_PTR fdi_notify_extract(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
181 switch (fdint)
183 case fdintCOPY_FILE:
185 struct FILELIST **fileList;
186 SESSION *pDestination = pfdin->pv;
187 LPSTR szFullPath, szDirectory;
188 HANDLE hFile = 0;
189 DWORD dwSize;
191 dwSize = lstrlenA(pDestination->Destination) +
192 lstrlenA("\\") + lstrlenA(pfdin->psz1) + 1;
193 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwSize);
195 lstrcpyA(szFullPath, pDestination->Destination);
196 lstrcatA(szFullPath, "\\");
197 lstrcatA(szFullPath, pfdin->psz1);
199 /* pull out the destination directory string from the full path */
200 dwSize = strrchr(szFullPath, '\\') - szFullPath + 1;
201 szDirectory = HeapAlloc(GetProcessHeap(), 0, dwSize);
202 lstrcpynA(szDirectory, szFullPath, dwSize);
204 if (pDestination->Operation & EXTRACT_FILLFILELIST)
206 fileList = &pDestination->FileList;
208 while (*fileList)
209 fileList = &((*fileList)->next);
211 *fileList = HeapAlloc(GetProcessHeap(), 0,
212 sizeof(struct FILELIST));
214 fill_file_node(*fileList, pfdin->psz1);
215 lstrcpyA(pDestination->CurrentFile, szFullPath);
216 pDestination->FileCount++;
219 if ((pDestination->Operation & EXTRACT_EXTRACTFILES) ||
220 file_in_list(pDestination->FilterList, pfdin->psz1))
222 /* skip this file if it is not in the file list */
223 if (!file_in_list(pDestination->FileList, pfdin->psz1))
224 return 0;
226 /* create the destination directory if it doesn't exist */
227 if (GetFileAttributesA(szDirectory) == INVALID_FILE_ATTRIBUTES)
228 CreateDirectoryA(szDirectory, NULL);
230 hFile = CreateFileA(szFullPath, GENERIC_READ | GENERIC_WRITE, 0, NULL,
231 CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
233 if (hFile == INVALID_HANDLE_VALUE)
234 hFile = 0;
237 HeapFree(GetProcessHeap(), 0, szFullPath);
238 HeapFree(GetProcessHeap(), 0, szDirectory);
240 return (INT_PTR) hFile;
243 case fdintCLOSE_FILE_INFO:
245 FILETIME ft;
246 FILETIME ftLocal;
247 HANDLE handle = (HANDLE) pfdin->hf;
249 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
250 return FALSE;
252 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
253 return FALSE;
255 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
256 return FALSE;
258 CloseHandle(handle);
259 return TRUE;
262 default:
263 return 0;
267 /***********************************************************************
268 * Extract (CABINET.3)
270 * Extracts the contents of the cabinet file to the specified
271 * destination.
273 * PARAMS
274 * dest [I/O] Controls the operation of Extract. See NOTES.
275 * szCabName [I] Filename of the cabinet to extract.
277 * RETURNS
278 * Success: S_OK.
279 * Failure: E_FAIL.
281 * NOTES
282 * The following members of the dest struct control the operation
283 * of Extract:
284 * FileSize [O] The size of all files extracted up to CurrentFile.
285 * Error [O] The error in case the extract operation fails.
286 * FileList [I] A linked list of filenames. Extract only extracts
287 * files from the cabinet that are in this list.
288 * FileCount [O] Contains the number of files in FileList on
289 * completion.
290 * Operation [I] See Operation.
291 * Destination [I] The destination directory.
292 * CurrentFile [O] The last file extracted.
293 * FilterList [I] A linked list of files that should not be extracted.
295 * Operation
296 * If Operation contains EXTRACT_FILLFILELIST, then FileList will be
297 * filled with all the files in the cabinet. If Operation contains
298 * EXTRACT_EXTRACTFILES, then only the files in the FileList will
299 * be extracted from the cabinet. EXTRACT_FILLFILELIST can be called
300 * by itself, but EXTRACT_EXTRACTFILES must have a valid FileList
301 * in order to succeed. If Operation contains both EXTRACT_FILLFILELIST
302 * and EXTRACT_EXTRACTFILES, then all the files in the cabinet
303 * will be extracted.
305 HRESULT WINAPI Extract(SESSION *dest, LPCSTR szCabName)
307 HRESULT res = S_OK;
308 HFDI hfdi;
309 ERF erf;
310 char *str, *path, *name;
312 TRACE("(%p, %s)\n", dest, szCabName);
314 hfdi = FDICreate(mem_alloc,
315 mem_free,
316 fdi_open,
317 fdi_read,
318 fdi_write,
319 fdi_close,
320 fdi_seek,
321 cpuUNKNOWN,
322 &erf);
324 if (!hfdi)
325 return E_FAIL;
327 if (GetFileAttributesA(dest->Destination) == INVALID_FILE_ATTRIBUTES)
328 return S_OK;
330 /* split the cabinet name into path + name */
331 str = HeapAlloc(GetProcessHeap(), 0, lstrlenA(szCabName)+1);
332 if (!str)
334 res = E_OUTOFMEMORY;
335 goto end;
337 lstrcpyA(str, szCabName);
339 path = str;
340 name = strrchr(path, '\\');
341 if (name)
342 *name++ = 0;
343 else
345 name = path;
346 path = NULL;
349 if (!FDICopy(hfdi, name, path, 0,
350 fdi_notify_extract, NULL, dest))
351 res = E_FAIL;
353 HeapFree(GetProcessHeap(), 0, str);
354 end:
356 FDIDestroy(hfdi);
358 return res;