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
29 #define NO_SHLWAPI_REG
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(cabinet
);
40 /***********************************************************************
41 * DllGetVersion (CABINET.2)
43 * Retrieves version information of the 'CABINET.DLL'
46 * pdvi [O] pointer to version information structure.
50 * Failure: E_INVALIDARG
53 * Supposedly returns version from IE6SP1RP1
55 HRESULT WINAPI
DllGetVersion (DLLVERSIONINFO
*pdvi
)
57 WARN("hmmm... not right version number \"5.1.1106.1\"?\n");
59 if (pdvi
->cbSize
!= sizeof(DLLVERSIONINFO
)) return E_INVALIDARG
;
61 pdvi
->dwMajorVersion
= 5;
62 pdvi
->dwMinorVersion
= 1;
63 pdvi
->dwBuildNumber
= 1106;
64 pdvi
->dwPlatformID
= 1;
69 /* FDI callback functions */
71 static void * CDECL
mem_alloc(ULONG cb
)
73 return HeapAlloc(GetProcessHeap(), 0, cb
);
76 static void CDECL
mem_free(void *memory
)
78 HeapFree(GetProcessHeap(), 0, memory
);
81 static INT_PTR CDECL
fdi_open(char *pszFile
, int oflag
, int pmode
)
85 DWORD dwShareMode
= 0;
86 DWORD dwCreateDisposition
;
88 switch (oflag
& _O_ACCMODE
)
91 dwAccess
= GENERIC_READ
;
92 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_DELETE
;
95 dwAccess
= GENERIC_WRITE
;
96 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
99 dwAccess
= GENERIC_READ
| GENERIC_WRITE
;
100 dwShareMode
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
104 if (oflag
& _O_CREAT
)
106 dwCreateDisposition
= OPEN_ALWAYS
;
107 if (oflag
& _O_EXCL
) dwCreateDisposition
= CREATE_NEW
;
108 else if (oflag
& _O_TRUNC
) dwCreateDisposition
= CREATE_ALWAYS
;
112 dwCreateDisposition
= OPEN_EXISTING
;
113 if (oflag
& _O_TRUNC
) dwCreateDisposition
= TRUNCATE_EXISTING
;
116 handle
= CreateFileA(pszFile
, dwAccess
, dwShareMode
, NULL
,
117 dwCreateDisposition
, 0, NULL
);
119 return (INT_PTR
) handle
;
122 static UINT CDECL
fdi_read(INT_PTR hf
, void *pv
, UINT cb
)
124 HANDLE handle
= (HANDLE
) hf
;
127 if (ReadFile(handle
, pv
, cb
, &dwRead
, NULL
))
133 static UINT CDECL
fdi_write(INT_PTR hf
, void *pv
, UINT cb
)
135 HANDLE handle
= (HANDLE
) hf
;
138 if (WriteFile(handle
, pv
, cb
, &dwWritten
, NULL
))
144 static int CDECL
fdi_close(INT_PTR hf
)
146 HANDLE handle
= (HANDLE
) hf
;
147 return CloseHandle(handle
) ? 0 : -1;
150 static LONG CDECL
fdi_seek(INT_PTR hf
, LONG dist
, int seektype
)
152 HANDLE handle
= (HANDLE
) hf
;
153 return SetFilePointer(handle
, dist
, NULL
, seektype
);
156 static void fill_file_node(struct FILELIST
*pNode
, LPCSTR szFilename
)
159 pNode
->DoExtract
= FALSE
;
161 pNode
->FileName
= HeapAlloc(GetProcessHeap(), 0, strlen(szFilename
) + 1);
162 lstrcpyA(pNode
->FileName
, szFilename
);
165 static BOOL
file_in_list(struct FILELIST
*pNode
, LPCSTR szFilename
,
166 struct FILELIST
**pOut
)
170 if (!lstrcmpiA(pNode
->FileName
, szFilename
))
184 static INT_PTR CDECL
fdi_notify_extract(FDINOTIFICATIONTYPE fdint
, PFDINOTIFICATION pfdin
)
190 struct FILELIST
*fileList
, *node
= NULL
;
191 SESSION
*pDestination
= pfdin
->pv
;
192 LPSTR szFullPath
, szDirectory
;
196 dwSize
= lstrlenA(pDestination
->Destination
) +
197 lstrlenA("\\") + lstrlenA(pfdin
->psz1
) + 1;
198 szFullPath
= HeapAlloc(GetProcessHeap(), 0, dwSize
);
200 lstrcpyA(szFullPath
, pDestination
->Destination
);
201 lstrcatA(szFullPath
, "\\");
202 lstrcatA(szFullPath
, pfdin
->psz1
);
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
);
209 pDestination
->FileSize
+= pfdin
->cb
;
211 if (pDestination
->Operation
& EXTRACT_FILLFILELIST
)
213 fileList
= HeapAlloc(GetProcessHeap(), 0,
214 sizeof(struct FILELIST
));
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
++;
224 if ((pDestination
->Operation
& EXTRACT_EXTRACTFILES
) ||
225 file_in_list(pDestination
->FilterList
, pfdin
->psz1
, NULL
))
227 /* find the file node */
228 file_in_list(pDestination
->FileList
, pfdin
->psz1
, &node
);
230 if (node
&& !node
->DoExtract
)
232 HeapFree(GetProcessHeap(), 0, szFullPath
);
233 HeapFree(GetProcessHeap(), 0, szDirectory
);
237 /* create the destination directory if it doesn't exist */
238 if (GetFileAttributesA(szDirectory
) == INVALID_FILE_ATTRIBUTES
)
242 for(ptr
= szDirectory
+ strlen(pDestination
->Destination
)+1; *ptr
; ptr
++) {
245 CreateDirectoryA(szDirectory
, NULL
);
249 CreateDirectoryA(szDirectory
, NULL
);
252 hFile
= CreateFileA(szFullPath
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
253 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
255 if (hFile
!= INVALID_HANDLE_VALUE
&& node
)
256 node
->DoExtract
= FALSE
;
259 HeapFree(GetProcessHeap(), 0, szFullPath
);
260 HeapFree(GetProcessHeap(), 0, szDirectory
);
262 return (INT_PTR
) hFile
;
265 case fdintCLOSE_FILE_INFO
:
269 HANDLE handle
= (HANDLE
) pfdin
->hf
;
271 if (!DosDateTimeToFileTime(pfdin
->date
, pfdin
->time
, &ft
))
274 if (!LocalFileTimeToFileTime(&ft
, &ftLocal
))
277 if (!SetFileTime(handle
, &ftLocal
, 0, &ftLocal
))
289 /***********************************************************************
290 * Extract (CABINET.3)
292 * Extracts the contents of the cabinet file to the specified
296 * dest [I/O] Controls the operation of Extract. See NOTES.
297 * szCabName [I] Filename of the cabinet to extract.
304 * The following members of the dest struct control the operation
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
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.
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
327 HRESULT WINAPI
Extract(SESSION
*dest
, LPCSTR szCabName
)
331 char *str
, *end
, *path
= NULL
, *name
= NULL
;
333 TRACE("(%p, %s)\n", dest
, debugstr_a(szCabName
));
335 hfdi
= FDICreate(mem_alloc
,
348 if (GetFileAttributesA(dest
->Destination
) == INVALID_FILE_ATTRIBUTES
)
354 /* split the cabinet name into path + name */
355 str
= HeapAlloc(GetProcessHeap(), 0, lstrlenA(szCabName
)+1);
361 lstrcpyA(str
, szCabName
);
363 if ((end
= strrchr(str
, '\\')))
367 name
= HeapAlloc( GetProcessHeap(), 0, strlen(end
) + 1 );
384 if (!FDICopy(hfdi
, name
, path
, 0,
385 fdi_notify_extract
, NULL
, dest
))
386 res
= HRESULT_FROM_WIN32(GetLastError());
389 HeapFree(GetProcessHeap(), 0, path
);
390 HeapFree(GetProcessHeap(), 0, name
);