ReactOS  0.4.15-dev-1054-gd029a62
cabinet.cpp File Reference
#include "rapps.h"
#include <fdi.h>
#include <fcntl.h>
Include dependency graph for cabinet.cpp:

Go to the source code of this file.

Typedefs

typedef HFDI(* fnFDICreate) (PFNALLOC, PFNFREE, PFNOPEN, PFNREAD, PFNWRITE, PFNCLOSE, PFNSEEK, int, PERF)
 
typedef BOOL(* fnFDICopy) (HFDI, LPSTR, LPSTR, INT, PFNFDINOTIFY, PFNFDIDECRYPT, void FAR *pvUser)
 
typedef BOOL(* fnFDIDestroy) (HFDI)
 

Functions

BOOL WideToMultiByte (const CStringW &szSource, CStringA &szDest, UINT Codepage)
 
BOOL MultiByteToWide (const CStringA &szSource, CStringW &szDest, UINT Codepage)
 
 FNALLOC (fnMemAlloc)
 
 FNFREE (fnMemFree)
 
 FNOPEN (fnFileOpen)
 
 FNREAD (fnFileRead)
 
 FNWRITE (fnFileWrite)
 
 FNCLOSE (fnFileClose)
 
 FNSEEK (fnFileSeek)
 
 FNFDINOTIFY (fnNotify)
 
BOOL ExtractFilesFromCab (const ATL::CStringW &szCabName, const ATL::CStringW &szCabDir, const ATL::CStringW &szOutputDir)
 

Typedef Documentation

◆ fnFDICopy

typedef BOOL(* fnFDICopy) (HFDI, LPSTR, LPSTR, INT, PFNFDINOTIFY, PFNFDIDECRYPT, void FAR *pvUser)

Definition at line 242 of file cabinet.cpp.

◆ fnFDICreate

typedef HFDI(* fnFDICreate) (PFNALLOC, PFNFREE, PFNOPEN, PFNREAD, PFNWRITE, PFNCLOSE, PFNSEEK, int, PERF)

Definition at line 232 of file cabinet.cpp.

◆ fnFDIDestroy

typedef BOOL(* fnFDIDestroy) (HFDI)

Definition at line 250 of file cabinet.cpp.

Function Documentation

◆ ExtractFilesFromCab()

BOOL ExtractFilesFromCab ( const ATL::CStringW szCabName,
const ATL::CStringW szCabDir,
const ATL::CStringW szOutputDir 
)

Definition at line 256 of file cabinet.cpp.

259 {
260  HINSTANCE hCabinetDll;
261  HFDI ExtractHandler;
262  ERF ExtractErrors;
263  ATL::CStringA szCabNameUTF8, szCabDirUTF8, szOutputDirUTF8;
264  fnFDICreate pfnFDICreate;
265  fnFDICopy pfnFDICopy;
266  fnFDIDestroy pfnFDIDestroy;
267  BOOL bResult;
268 
269  // Load cabinet.dll and extract needed functions
270  hCabinetDll = LoadLibraryW(L"cabinet.dll");
271 
272  if (!hCabinetDll)
273  {
274  return FALSE;
275  }
276 
277  pfnFDICreate = (fnFDICreate) GetProcAddress(hCabinetDll, "FDICreate");
278  pfnFDICopy = (fnFDICopy) GetProcAddress(hCabinetDll, "FDICopy");
279  pfnFDIDestroy = (fnFDIDestroy) GetProcAddress(hCabinetDll, "FDIDestroy");
280 
281  if (!pfnFDICreate || !pfnFDICopy || !pfnFDIDestroy)
282  {
283  FreeLibrary(hCabinetDll);
284  return FALSE;
285  }
286 
287  // Create FDI context
288  ExtractHandler = pfnFDICreate(fnMemAlloc,
289  fnMemFree,
290  fnFileOpen,
291  fnFileRead,
292  fnFileWrite,
293  fnFileClose,
294  fnFileSeek,
295  cpuUNKNOWN,
296  &ExtractErrors);
297 
298  if (!ExtractHandler)
299  {
300  FreeLibrary(hCabinetDll);
301  return FALSE;
302  }
303 
304  // Create output dir
305  bResult = CreateDirectoryW(szOutputDir, NULL);
306 
307  if (bResult || GetLastError() == ERROR_ALREADY_EXISTS)
308  {
309  // Convert wide strings to UTF-8
310  bResult = WideToMultiByte(szCabName, szCabNameUTF8, CP_UTF8);
311  bResult &= WideToMultiByte(szCabDir, szCabDirUTF8, CP_UTF8);
312  bResult &= WideToMultiByte(szOutputDir, szOutputDirUTF8, CP_UTF8);
313  }
314 
315  // Perform extraction
316  if (bResult)
317  {
318  // Add a slash to cab name as required by the api
319  szCabNameUTF8 = "\\" + szCabNameUTF8;
320 
321  bResult = pfnFDICopy(ExtractHandler,
322  (LPSTR) szCabNameUTF8.GetString(),
323  (LPSTR) szCabDirUTF8.GetString(),
324  0,
325  fnNotify,
326  NULL,
327  (void FAR *) szOutputDirUTF8.GetString());
328  }
329 
330  pfnFDIDestroy(ExtractHandler);
331  FreeLibrary(hCabinetDll);
332  return bResult;
333 }
BOOL WINAPI CreateDirectoryW(IN LPCWSTR lpPathName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: dir.c:90
BOOL(* fnFDIDestroy)(HFDI)
Definition: cabinet.cpp:250
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1044
char * LPSTR
Definition: xmlstorage.h:182
#define CP_UTF8
Definition: nls.h:20
#define cpuUNKNOWN
Definition: fdi.h:269
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
HFDI(* fnFDICreate)(PFNALLOC, PFNFREE, PFNOPEN, PFNREAD, PFNWRITE, PFNCLOSE, PFNSEEK, int, PERF)
Definition: cabinet.cpp:232
Definition: fci.h:44
#define LoadLibraryW(x)
Definition: compat.h:495
smooth NULL
Definition: ftsmooth.c:416
BOOL WideToMultiByte(const CStringW &szSource, CStringA &szDest, UINT Codepage)
Definition: cabinet.cpp:24
#define FreeLibrary(x)
Definition: compat.h:496
PCXSTR GetString() const
Definition: atlsimpstr.h:361
static const WCHAR L[]
Definition: oid.c:1250
BOOL(* fnFDICopy)(HFDI, LPSTR, LPSTR, INT, PFNFDINOTIFY, PFNFDIDECRYPT, void FAR *pvUser)
Definition: cabinet.cpp:242
#define GetProcAddress(x, y)
Definition: compat.h:501
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
#define FAR
Definition: zlib.h:34

Referenced by CAvailableApps::UpdateAppsDB().

◆ FNALLOC()

FNALLOC ( fnMemAlloc  )

Definition at line 83 of file cabinet.cpp.

84 {
85  return HeapAlloc(GetProcessHeap(), NULL, cb);
86 }
smooth NULL
Definition: ftsmooth.c:416
#define GetProcessHeap()
Definition: compat.h:484
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32

◆ FNCLOSE()

FNCLOSE ( fnFileClose  )

Definition at line 161 of file cabinet.cpp.

162 {
163  return (CloseHandle((HANDLE) hf) != FALSE) ? 0 : -1;
164 }
#define CloseHandle
Definition: compat.h:487
#define FALSE
Definition: types.h:117

◆ FNFDINOTIFY()

FNFDINOTIFY ( fnNotify  )

Definition at line 173 of file cabinet.cpp.

174 {
175  INT_PTR iResult = 0;
176 
177  switch (fdint)
178  {
179  case fdintCOPY_FILE:
180  {
181  ATL::CStringW szNewFileName, szExtractDir, szCabFileName;
182  ATL::CStringA szFilePathUTF8;
183 
184  // Append the destination directory to the file name.
185  MultiByteToWide((LPCSTR) pfdin->pv, szExtractDir, CP_UTF8);
186  MultiByteToWide(pfdin->psz1, szCabFileName, CP_ACP);
187 
188  szNewFileName = szExtractDir + L"\\" + szCabFileName;
189 
190  WideToMultiByte(szNewFileName, szFilePathUTF8, CP_UTF8);
191 
192  // Copy file
193  iResult = fnFileOpen((LPSTR) szFilePathUTF8.GetString(),
195  0);
196  }
197  break;
198 
200  iResult = !fnFileClose(pfdin->hf);
201  break;
202 
203  case fdintNEXT_CABINET:
204  if (pfdin->fdie != FDIERROR_NONE)
205  {
206  iResult = -1;
207  }
208  break;
209 
210  case fdintPARTIAL_FILE:
211  iResult = 0;
212  break;
213 
214  case fdintCABINET_INFO:
215  iResult = 0;
216  break;
217 
218  case fdintENUMERATE:
219  iResult = 0;
220  break;
221 
222  default:
223  iResult = -1;
224  break;
225  }
226 
227  return iResult;
228 }
#define CP_ACP
Definition: compat.h:109
#define _O_CREAT
Definition: cabinet.h:46
int32_t INT_PTR
Definition: typedefs.h:64
char * LPSTR
Definition: xmlstorage.h:182
BOOL MultiByteToWide(const CStringA &szSource, CStringW &szDest, UINT Codepage)
Definition: cabinet.cpp:55
#define CP_UTF8
Definition: nls.h:20
const char * LPCSTR
Definition: xmlstorage.h:183
BOOL WideToMultiByte(const CStringW &szSource, CStringA &szDest, UINT Codepage)
Definition: cabinet.cpp:24
PCXSTR GetString() const
Definition: atlsimpstr.h:361
static const WCHAR L[]
Definition: oid.c:1250
#define _O_WRONLY
Definition: cabinet.h:38

◆ FNFREE()

FNFREE ( fnMemFree  )

Definition at line 88 of file cabinet.cpp.

89 {
91 }
smooth NULL
Definition: ftsmooth.c:416
#define GetProcessHeap()
Definition: compat.h:484
#define HeapFree(x, y, z)
Definition: compat.h:483

◆ FNOPEN()

FNOPEN ( fnFileOpen  )

Definition at line 93 of file cabinet.cpp.

94 {
95  HANDLE hFile = NULL;
96  DWORD dwDesiredAccess = 0;
97  DWORD dwCreationDisposition = 0;
98  ATL::CStringW szFileName;
99 
100  UNREFERENCED_PARAMETER(pmode);
101 
102  if (oflag & _O_RDWR)
103  {
104  dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
105  }
106  else if (oflag & _O_WRONLY)
107  {
108  dwDesiredAccess = GENERIC_WRITE;
109  }
110  else
111  {
112  dwDesiredAccess = GENERIC_READ;
113  }
114 
115  if (oflag & _O_CREAT)
116  {
117  dwCreationDisposition = CREATE_ALWAYS;
118  }
119  else
120  {
121  dwCreationDisposition = OPEN_EXISTING;
122  }
123 
124  MultiByteToWide(pszFile, szFileName, CP_UTF8);
125 
126  hFile = CreateFileW(szFileName,
127  dwDesiredAccess,
129  NULL,
130  dwCreationDisposition,
132  NULL);
133 
134  return (INT_PTR) hFile;
135 }
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
#define _O_CREAT
Definition: cabinet.h:46
int32_t INT_PTR
Definition: typedefs.h:64
#define FILE_SHARE_READ
Definition: compat.h:136
BOOL MultiByteToWide(const CStringA &szSource, CStringW &szDest, UINT Codepage)
Definition: cabinet.cpp:55
#define _O_RDWR
Definition: cabinet.h:39
#define CP_UTF8
Definition: nls.h:20
#define GENERIC_WRITE
Definition: nt_native.h:90
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:523
unsigned long DWORD
Definition: ntddk_ex.h:95
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define GENERIC_READ
Definition: compat.h:135
_In_ HANDLE hFile
Definition: mswsock.h:90
#define _O_WRONLY
Definition: cabinet.h:38
#define CREATE_ALWAYS
Definition: disk.h:72
#define CreateFileW
Definition: compat.h:489

◆ FNREAD()

FNREAD ( fnFileRead  )

Definition at line 137 of file cabinet.cpp.

138 {
139  DWORD dwBytesRead = 0;
140 
141  if (ReadFile((HANDLE) hf, pv, cb, &dwBytesRead, NULL) == FALSE)
142  {
143  dwBytesRead = (DWORD) -1L;
144  }
145 
146  return dwBytesRead;
147 }
#define DWORD
Definition: nt_native.h:44
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:416
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
#define ReadFile(a, b, c, d, e)
Definition: compat.h:490
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32

◆ FNSEEK()

FNSEEK ( fnFileSeek  )

Definition at line 166 of file cabinet.cpp.

167 {
168  return SetFilePointer((HANDLE) hf, dist, NULL, seektype);
169 }
smooth NULL
Definition: ftsmooth.c:416
#define SetFilePointer
Definition: compat.h:491

◆ FNWRITE()

FNWRITE ( fnFileWrite  )

Definition at line 149 of file cabinet.cpp.

150 {
151  DWORD dwBytesWritten = 0;
152 
153  if (WriteFile((HANDLE) hf, pv, cb, &dwBytesWritten, NULL) == FALSE)
154  {
155  dwBytesWritten = (DWORD) -1;
156  }
157 
158  return dwBytesWritten;
159 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
#define DWORD
Definition: nt_native.h:44
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:416
unsigned long DWORD
Definition: ntddk_ex.h:95
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32

◆ MultiByteToWide()

BOOL MultiByteToWide ( const CStringA szSource,
CStringW szDest,
UINT  Codepage 
)
inline

Definition at line 55 of file cabinet.cpp.

58 {
59  // determine the needed size
60  INT sz = MultiByteToWideChar(Codepage,
61  0,
62  szSource,
63  -1,
64  NULL,
65  NULL);
66  if (!sz)
67  return FALSE;
68 
69  // do the actual conversion
71  0,
72  szSource,
73  -1,
74  szDest.GetBuffer(sz),
75  sz);
76 
77  szDest.ReleaseBuffer();
78  return sz != 0;
79 }
int32_t INT
Definition: typedefs.h:58
void ReleaseBuffer(_In_ int nNewLength=-1)
Definition: atlsimpstr.h:372
#define CP_UTF8
Definition: nls.h:20
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:416
static const WCHAR szSource[]
Definition: automation.c:501
#define MultiByteToWideChar
Definition: compat.h:110

Referenced by FNFDINOTIFY(), and FNOPEN().

◆ WideToMultiByte()

BOOL WideToMultiByte ( const CStringW szSource,
CStringA szDest,
UINT  Codepage 
)
inline

Definition at line 24 of file cabinet.cpp.

27 {
28  // determine the needed size
29  INT sz = WideCharToMultiByte(Codepage,
30  0,
31  szSource,
32  -1,
33  NULL,
34  NULL,
35  NULL,
36  NULL);
37  if (!sz)
38  return FALSE;
39 
40  // do the actual conversion
41  sz = WideCharToMultiByte(Codepage,
42  0,
43  szSource,
44  -1,
45  szDest.GetBuffer(sz),
46  sz,
47  NULL,
48  NULL);
49 
50  szDest.ReleaseBuffer();
51  return sz != 0;
52 }
#define WideCharToMultiByte
Definition: compat.h:111
int32_t INT
Definition: typedefs.h:58
void ReleaseBuffer(_In_ int nNewLength=-1)
Definition: atlsimpstr.h:372
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:416
static const WCHAR szSource[]
Definition: automation.c:501

Referenced by ExtractFilesFromCab(), and FNFDINOTIFY().