ReactOS  0.4.15-dev-1638-gc0caa5c
cabinet.cpp File Reference
#include "rapps.h"
#include <debug.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 267 of file cabinet.cpp.

◆ fnFDICreate

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

Definition at line 257 of file cabinet.cpp.

◆ fnFDIDestroy

typedef BOOL(* fnFDIDestroy) (HFDI)

Definition at line 275 of file cabinet.cpp.

Function Documentation

◆ ExtractFilesFromCab()

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

Definition at line 281 of file cabinet.cpp.

284 {
285  HINSTANCE hCabinetDll;
286  HFDI ExtractHandler;
287  ERF ExtractErrors;
288  ATL::CStringA szCabNameUTF8, szCabDirUTF8, szOutputDirUTF8;
289  fnFDICreate pfnFDICreate;
290  fnFDICopy pfnFDICopy;
291  fnFDIDestroy pfnFDIDestroy;
292  BOOL bResult;
293 
294  // Load cabinet.dll and extract needed functions
295  hCabinetDll = LoadLibraryW(L"cabinet.dll");
296 
297  if (!hCabinetDll)
298  {
299  return FALSE;
300  }
301 
302  pfnFDICreate = (fnFDICreate) GetProcAddress(hCabinetDll, "FDICreate");
303  pfnFDICopy = (fnFDICopy) GetProcAddress(hCabinetDll, "FDICopy");
304  pfnFDIDestroy = (fnFDIDestroy) GetProcAddress(hCabinetDll, "FDIDestroy");
305 
306  if (!pfnFDICreate || !pfnFDICopy || !pfnFDIDestroy)
307  {
308  FreeLibrary(hCabinetDll);
309  return FALSE;
310  }
311 
312  // Create FDI context
313  ExtractHandler = pfnFDICreate(fnMemAlloc,
314  fnMemFree,
315  fnFileOpen,
316  fnFileRead,
317  fnFileWrite,
318  fnFileClose,
319  fnFileSeek,
320  cpuUNKNOWN,
321  &ExtractErrors);
322 
323  if (!ExtractHandler)
324  {
325  FreeLibrary(hCabinetDll);
326  return FALSE;
327  }
328 
329  // Create output dir
330  bResult = CreateDirectoryW(szOutputDir, NULL);
331 
332  if (bResult || GetLastError() == ERROR_ALREADY_EXISTS)
333  {
334  // Convert wide strings to UTF-8
335  bResult = WideToMultiByte(szCabName, szCabNameUTF8, CP_UTF8);
336  bResult &= WideToMultiByte(szCabDir, szCabDirUTF8, CP_UTF8);
337  bResult &= WideToMultiByte(szOutputDir, szOutputDirUTF8, CP_UTF8);
338  }
339 
340  // Perform extraction
341  if (bResult)
342  {
343  // Add a slash to cab name as required by the api
344  szCabNameUTF8 = "\\" + szCabNameUTF8;
345 
346  bResult = pfnFDICopy(ExtractHandler,
347  (LPSTR) szCabNameUTF8.GetString(),
348  (LPSTR) szCabDirUTF8.GetString(),
349  0,
350  fnNotify,
351  NULL,
352  (void FAR *) szOutputDirUTF8.GetString());
353  }
354 
355  pfnFDIDestroy(ExtractHandler);
356  FreeLibrary(hCabinetDll);
357  return bResult;
358 }
BOOL WINAPI CreateDirectoryW(IN LPCWSTR lpPathName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: dir.c:90
BOOL(* fnFDIDestroy)(HFDI)
Definition: cabinet.cpp:275
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:257
Definition: fci.h:44
#define LoadLibraryW(x)
Definition: compat.h:606
BOOL WideToMultiByte(const CStringW &szSource, CStringA &szDest, UINT Codepage)
Definition: cabinet.cpp:25
#define FreeLibrary(x)
Definition: compat.h:607
PCXSTR GetString() const
Definition: atlsimpstr.h:361
static const WCHAR L[]
Definition: oid.c:1250
#define NULL
Definition: types.h:112
BOOL(* fnFDICopy)(HFDI, LPSTR, LPSTR, INT, PFNFDINOTIFY, PFNFDIDECRYPT, void FAR *pvUser)
Definition: cabinet.cpp:267
#define GetProcAddress(x, y)
Definition: compat.h:612
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
#define FAR
Definition: zlib.h:34

Referenced by CAvailableApps::UpdateAppsDB().

◆ FNALLOC()

FNALLOC ( fnMemAlloc  )

Definition at line 84 of file cabinet.cpp.

85 {
86  return HeapAlloc(GetProcessHeap(), NULL, cb);
87 }
#define GetProcessHeap()
Definition: compat.h:595
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define NULL
Definition: types.h:112
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32

◆ FNCLOSE()

FNCLOSE ( fnFileClose  )

Definition at line 162 of file cabinet.cpp.

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

◆ FNFDINOTIFY()

FNFDINOTIFY ( fnNotify  )

Definition at line 174 of file cabinet.cpp.

175 {
176  INT_PTR iResult = 0;
177 
178  switch (fdint)
179  {
180  case fdintCOPY_FILE:
181  {
182  CStringW szExtractDir, szCabFileName;
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  if (szCabFileName.Find('\\') >= 0)
189  {
190  CStringW szNewDirName = szExtractDir;
191  int nTokenPos = 0;
192  // We do not want to interpret the filename as directory,
193  // so bail out before the last token!
194  while (szCabFileName.Find('\\', nTokenPos) >= 0)
195  {
196  CStringW token = szCabFileName.Tokenize(L"\\", nTokenPos);
197  if (token.IsEmpty())
198  break;
199 
200  szNewDirName += L"\\" + token;
201  if (!CreateDirectoryW(szNewDirName, NULL))
202  {
205  {
206  DPRINT1("ERROR: Unable to create directory %S (err %lu)\n", szNewDirName.GetString(), dwErr);
207  }
208  }
209  }
210  }
211 
212  CStringW szNewFileName = szExtractDir + L"\\" + szCabFileName;
213 
214  CStringA szFilePathUTF8;
215  WideToMultiByte(szNewFileName, szFilePathUTF8, CP_UTF8);
216 
217  // Open the file
218  iResult = fnFileOpen((LPSTR) szFilePathUTF8.GetString(),
220  0);
221  }
222  break;
223 
225  iResult = !fnFileClose(pfdin->hf);
226  break;
227 
228  case fdintNEXT_CABINET:
229  if (pfdin->fdie != FDIERROR_NONE)
230  {
231  iResult = -1;
232  }
233  break;
234 
235  case fdintPARTIAL_FILE:
236  iResult = 0;
237  break;
238 
239  case fdintCABINET_INFO:
240  iResult = 0;
241  break;
242 
243  case fdintENUMERATE:
244  iResult = 0;
245  break;
246 
247  default:
248  iResult = -1;
249  break;
250  }
251 
252  return iResult;
253 }
BOOL WINAPI CreateDirectoryW(IN LPCWSTR lpPathName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: dir.c:90
#define CP_ACP
Definition: compat.h:109
#define _O_CREAT
Definition: cabinet.h:46
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1044
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:56
#define CP_UTF8
Definition: nls.h:20
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat token
Definition: glfuncs.h:210
const char * LPCSTR
Definition: xmlstorage.h:183
BOOL WideToMultiByte(const CStringW &szSource, CStringA &szDest, UINT Codepage)
Definition: cabinet.cpp:25
DWORD dwErr
Definition: service.c:36
CStringT Tokenize(_In_z_ PCXSTR pszTokens, _Inout_ int &iStart) const
Definition: cstringt.h:835
PCXSTR GetString() const
Definition: atlsimpstr.h:361
unsigned long DWORD
Definition: ntddk_ex.h:95
static const WCHAR L[]
Definition: oid.c:1250
#define _O_WRONLY
Definition: cabinet.h:38
#define NULL
Definition: types.h:112
#define DPRINT1
Definition: precomp.h:8
int Find(_In_ PCXSTR pszSub, _In_opt_ int iStart=0) const
Definition: cstringt.h:611
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80

◆ FNFREE()

FNFREE ( fnMemFree  )

Definition at line 89 of file cabinet.cpp.

90 {
92 }
#define GetProcessHeap()
Definition: compat.h:595
#define NULL
Definition: types.h:112
#define HeapFree(x, y, z)
Definition: compat.h:594

◆ FNOPEN()

FNOPEN ( fnFileOpen  )

Definition at line 94 of file cabinet.cpp.

95 {
96  HANDLE hFile = NULL;
97  DWORD dwDesiredAccess = 0;
98  DWORD dwCreationDisposition = 0;
99  ATL::CStringW szFileName;
100 
101  UNREFERENCED_PARAMETER(pmode);
102 
103  if (oflag & _O_RDWR)
104  {
105  dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
106  }
107  else if (oflag & _O_WRONLY)
108  {
109  dwDesiredAccess = GENERIC_WRITE;
110  }
111  else
112  {
113  dwDesiredAccess = GENERIC_READ;
114  }
115 
116  if (oflag & _O_CREAT)
117  {
118  dwCreationDisposition = CREATE_ALWAYS;
119  }
120  else
121  {
122  dwCreationDisposition = OPEN_EXISTING;
123  }
124 
125  MultiByteToWide(pszFile, szFileName, CP_UTF8);
126 
127  hFile = CreateFileW(szFileName,
128  dwDesiredAccess,
130  NULL,
131  dwCreationDisposition,
133  NULL);
134 
135  return (INT_PTR) hFile;
136 }
#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:56
#define _O_RDWR
Definition: cabinet.h:39
#define CP_UTF8
Definition: nls.h:20
#define GENERIC_WRITE
Definition: nt_native.h:90
#define OPEN_EXISTING
Definition: compat.h:634
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 NULL
Definition: types.h:112
#define CreateFileW
Definition: compat.h:600

◆ FNREAD()

FNREAD ( fnFileRead  )

Definition at line 138 of file cabinet.cpp.

139 {
140  DWORD dwBytesRead = 0;
141 
142  if (ReadFile((HANDLE) hf, pv, cb, &dwBytesRead, NULL) == FALSE)
143  {
144  dwBytesRead = (DWORD) -1L;
145  }
146 
147  return dwBytesRead;
148 }
#define DWORD
Definition: nt_native.h:44
#define FALSE
Definition: types.h:117
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:601
#define NULL
Definition: types.h:112
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32

◆ FNSEEK()

FNSEEK ( fnFileSeek  )

Definition at line 167 of file cabinet.cpp.

168 {
169  return SetFilePointer((HANDLE) hf, dist, NULL, seektype);
170 }
#define NULL
Definition: types.h:112
#define SetFilePointer
Definition: compat.h:602

◆ FNWRITE()

FNWRITE ( fnFileWrite  )

Definition at line 150 of file cabinet.cpp.

151 {
152  DWORD dwBytesWritten = 0;
153 
154  if (WriteFile((HANDLE) hf, pv, cb, &dwBytesWritten, NULL) == FALSE)
155  {
156  dwBytesWritten = (DWORD) -1;
157  }
158 
159  return dwBytesWritten;
160 }
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
unsigned long DWORD
Definition: ntddk_ex.h:95
#define NULL
Definition: types.h:112
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32

◆ MultiByteToWide()

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

Definition at line 56 of file cabinet.cpp.

59 {
60  // determine the needed size
61  INT sz = MultiByteToWideChar(Codepage,
62  0,
63  szSource,
64  -1,
65  NULL,
66  NULL);
67  if (!sz)
68  return FALSE;
69 
70  // do the actual conversion
72  0,
73  szSource,
74  -1,
75  szDest.GetBuffer(sz),
76  sz);
77 
78  szDest.ReleaseBuffer();
79  return sz != 0;
80 }
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
static const WCHAR szSource[]
Definition: automation.c:501
#define NULL
Definition: types.h:112
#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 25 of file cabinet.cpp.

28 {
29  // determine the needed size
30  INT sz = WideCharToMultiByte(Codepage,
31  0,
32  szSource,
33  -1,
34  NULL,
35  NULL,
36  NULL,
37  NULL);
38  if (!sz)
39  return FALSE;
40 
41  // do the actual conversion
42  sz = WideCharToMultiByte(Codepage,
43  0,
44  szSource,
45  -1,
46  szDest.GetBuffer(sz),
47  sz,
48  NULL,
49  NULL);
50 
51  szDest.ReleaseBuffer();
52  return sz != 0;
53 }
#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
static const WCHAR szSource[]
Definition: automation.c:501
#define NULL
Definition: types.h:112

Referenced by ExtractFilesFromCab(), and FNFDINOTIFY().