ReactOS 0.4.16-dev-92-g0c2cdca
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.

Classes

struct  NotifyData
 

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 CStringW &szCabName, const CStringW &szCabDir, const CStringW &szOutputDir, EXTRACTCALLBACK Callback, void *Cookie)
 
BOOL ExtractFilesFromCab (LPCWSTR FullCabPath, const CStringW &szOutputDir, EXTRACTCALLBACK Callback, void *Cookie)
 

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 240 of file cabinet.cpp.

◆ fnFDIDestroy

typedef BOOL(* fnFDIDestroy) (HFDI)

Definition at line 244 of file cabinet.cpp.

Function Documentation

◆ ExtractFilesFromCab() [1/2]

BOOL ExtractFilesFromCab ( const CStringW szCabName,
const CStringW szCabDir,
const CStringW szOutputDir,
EXTRACTCALLBACK  Callback,
void Cookie 
)

Definition at line 250 of file cabinet.cpp.

252{
253 HINSTANCE hCabinetDll;
254 HFDI ExtractHandler;
255 ERF ExtractErrors;
256 ATL::CStringA szCabNameUTF8, szCabDirUTF8, szOutputDirUTF8;
257 fnFDICreate pfnFDICreate;
258 fnFDICopy pfnFDICopy;
259 fnFDIDestroy pfnFDIDestroy;
260 BOOL bResult;
261 NotifyData nd = { Callback, Cookie };
262
263 // Load cabinet.dll and extract needed functions
264 hCabinetDll = LoadLibraryW(L"cabinet.dll");
265
266 if (!hCabinetDll)
267 {
268 return FALSE;
269 }
270
271 pfnFDICreate = (fnFDICreate)GetProcAddress(hCabinetDll, "FDICreate");
272 pfnFDICopy = (fnFDICopy)GetProcAddress(hCabinetDll, "FDICopy");
273 pfnFDIDestroy = (fnFDIDestroy)GetProcAddress(hCabinetDll, "FDIDestroy");
274
275 if (!pfnFDICreate || !pfnFDICopy || !pfnFDIDestroy)
276 {
277 FreeLibrary(hCabinetDll);
278 return FALSE;
279 }
280
281 // Create FDI context
282 ExtractHandler = pfnFDICreate(
283 fnMemAlloc, fnMemFree, fnFileOpen, fnFileRead, fnFileWrite, fnFileClose, fnFileSeek, cpuUNKNOWN,
284 &ExtractErrors);
285
286 if (!ExtractHandler)
287 {
288 FreeLibrary(hCabinetDll);
289 return FALSE;
290 }
291
292 // Create output dir
293 bResult = CreateDirectoryW(szOutputDir, NULL);
294
295 if (bResult || GetLastError() == ERROR_ALREADY_EXISTS)
296 {
297 // Convert wide strings to UTF-8
298 bResult = WideToMultiByte(szCabName, szCabNameUTF8, CP_UTF8);
299 bResult &= WideToMultiByte(szCabDir, szCabDirUTF8, CP_UTF8);
300 bResult &= WideToMultiByte(szOutputDir, szOutputDirUTF8, CP_UTF8);
301 }
302
303 // Perform extraction
304 if (bResult)
305 {
306 // Add a slash to cab name as required by the api
307 szCabNameUTF8 = "\\" + szCabNameUTF8;
308
309 nd.OutputDir = szOutputDirUTF8.GetString();
310 bResult = pfnFDICopy(
311 ExtractHandler, (LPSTR)szCabNameUTF8.GetString(), (LPSTR)szCabDirUTF8.GetString(), 0, fnNotify, NULL,
312 (void FAR *)&nd);
313 }
314
315 pfnFDIDestroy(ExtractHandler);
316 FreeLibrary(hCabinetDll);
317 return bResult;
318}
BOOL WideToMultiByte(const CStringW &szSource, CStringA &szDest, UINT Codepage)
Definition: cabinet.cpp:26
HFDI(* fnFDICreate)(PFNALLOC, PFNFREE, PFNOPEN, PFNREAD, PFNWRITE, PFNCLOSE, PFNSEEK, int, PERF)
Definition: cabinet.cpp:240
BOOL(* fnFDICopy)(HFDI, LPSTR, LPSTR, INT, PFNFDINOTIFY, PFNFDIDECRYPT, void FAR *pvUser)
Definition: cabinet.cpp:242
BOOL(* fnFDIDestroy)(HFDI)
Definition: cabinet.cpp:244
PXSTR GetString() noexcept
Definition: atlsimpstr.h:367
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define GetProcAddress(x, y)
Definition: compat.h:753
#define FreeLibrary(x)
Definition: compat.h:748
#define LoadLibraryW(x)
Definition: compat.h:747
#define FAR
Definition: zlib.h:34
BOOL WINAPI CreateDirectoryW(IN LPCWSTR lpPathName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: dir.c:90
#define cpuUNKNOWN
Definition: fdi.h:269
unsigned int BOOL
Definition: ntddk_ex.h:94
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
#define L(x)
Definition: ntvdm.h:50
#define CP_UTF8
Definition: nls.h:20
Definition: fci.h:44
LPCSTR OutputDir
Definition: cabinet.cpp:60
_In_ WDFINTERRUPT _In_ PFN_WDF_INTERRUPT_SYNCHRONIZE Callback
Definition: wdfinterrupt.h:458
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
_In_opt_ PVOID _Out_ PLARGE_INTEGER Cookie
Definition: cmfuncs.h:14
char * LPSTR
Definition: xmlstorage.h:182

Referenced by ExtractCab(), ExtractFilesFromCab(), and CAppDB::UpdateAvailable().

◆ ExtractFilesFromCab() [2/2]

BOOL ExtractFilesFromCab ( LPCWSTR  FullCabPath,
const CStringW szOutputDir,
EXTRACTCALLBACK  Callback,
void Cookie 
)

Definition at line 321 of file cabinet.cpp.

323{
324 CStringW dir, file = SplitFileAndDirectory(FullCabPath, &dir);
325 return ExtractFilesFromCab(file, dir, szOutputDir, Callback, Cookie);
326}
unsigned int dir
Definition: maze.c:112
CStringW SplitFileAndDirectory(LPCWSTR FullPath, CStringW *pDir=NULL)
Definition: misc.cpp:466
BOOL ExtractFilesFromCab(const CStringW &szCabName, const CStringW &szCabDir, const CStringW &szOutputDir, EXTRACTCALLBACK Callback, void *Cookie)
Definition: cabinet.cpp:250
Definition: fci.c:127

◆ FNALLOC()

FNALLOC ( fnMemAlloc  )

Definition at line 65 of file cabinet.cpp.

66{
67 return HeapAlloc(GetProcessHeap(), NULL, cb);
68}
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
static HMODULE MODULEINFO DWORD cb
Definition: module.c:33

◆ FNCLOSE()

FNCLOSE ( fnFileClose  )

Definition at line 138 of file cabinet.cpp.

139{
140 return (CloseHandle((HANDLE)hf) != FALSE) ? 0 : -1;
141}
#define CloseHandle
Definition: compat.h:739

◆ FNFDINOTIFY()

FNFDINOTIFY ( fnNotify  )

Definition at line 150 of file cabinet.cpp.

151{
152 INT_PTR iResult = 0;
153 NotifyData *pND = (NotifyData *)pfdin->pv;
154
155 switch (fdint)
156 {
157 case fdintCOPY_FILE:
158 {
159 CStringW szExtractDir, szCabFileName;
160
161 // Append the destination directory to the file name.
162 MultiByteToWide(pND->OutputDir, szExtractDir, CP_UTF8);
163 UINT codepage = (pfdin->attribs & _A_NAME_IS_UTF) ? CP_UTF8 : CP_ACP;
164 MultiByteToWide(pfdin->psz1, szCabFileName, codepage);
165
166 if (!NotifyFileExtractCallback(szCabFileName, pfdin->cb, pfdin->attribs,
167 pND->Callback, pND->CallerCookie))
168 {
169 break; // Skip file
170 }
171
172 if (szCabFileName.Find('\\') >= 0)
173 {
174 CStringW szNewDirName = szExtractDir;
175 int nTokenPos = 0;
176 // We do not want to interpret the filename as directory,
177 // so bail out before the last token!
178 while (szCabFileName.Find('\\', nTokenPos) >= 0)
179 {
180 CStringW token = szCabFileName.Tokenize(L"\\", nTokenPos);
181 if (token.IsEmpty())
182 break;
183
184 szNewDirName += L"\\" + token;
185 if (!CreateDirectoryW(szNewDirName, NULL))
186 {
189 {
190 DPRINT1(
191 "ERROR: Unable to create directory %S (err %lu)\n", szNewDirName.GetString(), dwErr);
192 }
193 }
194 }
195 }
196
197 CStringW szNewFileName = szExtractDir + L"\\" + szCabFileName;
198
199 CStringA szFilePathUTF8;
200 WideToMultiByte(szNewFileName, szFilePathUTF8, CP_UTF8);
201
202 // Open the file
203 iResult = fnFileOpen((LPSTR)szFilePathUTF8.GetString(), _O_WRONLY | _O_CREAT, 0);
204 }
205 break;
206
208 iResult = !fnFileClose(pfdin->hf);
209 break;
210
212 if (pfdin->fdie != FDIERROR_NONE)
213 {
214 iResult = -1;
215 }
216 break;
217
219 iResult = 0;
220 break;
221
223 iResult = 0;
224 break;
225
226 case fdintENUMERATE:
227 iResult = 0;
228 break;
229
230 default:
231 iResult = -1;
232 break;
233 }
234
235 return iResult;
236}
static BOOL NotifyFileExtractCallback(const CStringW &ItemPath, UINT64 UncompressedSize, UINT FileAttributes, EXTRACTCALLBACK Callback, void *Cookie)
Definition: misc.h:58
#define DPRINT1
Definition: precomp.h:8
DWORD dwErr
Definition: service.c:36
BOOL MultiByteToWide(const CStringA &szSource, CStringW &szDest, UINT Codepage)
Definition: cabinet.cpp:42
CStringT Tokenize(_In_z_ PCXSTR pszTokens, _Inout_ int &iStart) const
Definition: cstringt.h:947
int Find(_In_ PCXSTR pszSub, _In_opt_ int iStart=0) const noexcept
Definition: cstringt.h:696
#define _O_CREAT
Definition: cabinet.h:46
#define _O_WRONLY
Definition: cabinet.h:38
#define CP_ACP
Definition: compat.h:109
switch(r->id)
Definition: btrfs.c:3046
#define _A_NAME_IS_UTF
Definition: fci.h:131
@ FDIERROR_NONE
Definition: fdi.h:115
@ fdintCABINET_INFO
Definition: fdi.h:247
@ fdintCOPY_FILE
Definition: fdi.h:249
@ fdintPARTIAL_FILE
Definition: fdi.h:248
@ fdintCLOSE_FILE_INFO
Definition: fdi.h:250
@ fdintNEXT_CABINET
Definition: fdi.h:251
@ fdintENUMERATE
Definition: fdi.h:252
unsigned long DWORD
Definition: ntddk_ex.h:95
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
unsigned int UINT
Definition: ndis.h:50
void * CallerCookie
Definition: cabinet.cpp:59
EXTRACTCALLBACK Callback
Definition: cabinet.cpp:58
int32_t INT_PTR
Definition: typedefs.h:64
int codepage
Definition: win_iconv.c:156

◆ FNFREE()

FNFREE ( fnMemFree  )

Definition at line 70 of file cabinet.cpp.

71{
73}
#define HeapFree(x, y, z)
Definition: compat.h:735

◆ FNOPEN()

FNOPEN ( fnFileOpen  )

Definition at line 75 of file cabinet.cpp.

76{
78 DWORD dwDesiredAccess = 0;
79 DWORD dwCreationDisposition = 0;
80 CStringW szFileName;
81
83
84 if (oflag & _O_RDWR)
85 {
86 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
87 }
88 else if (oflag & _O_WRONLY)
89 {
90 dwDesiredAccess = GENERIC_WRITE;
91 }
92 else
93 {
94 dwDesiredAccess = GENERIC_READ;
95 }
96
97 if (oflag & _O_CREAT)
98 {
99 dwCreationDisposition = CREATE_ALWAYS;
100 }
101 else
102 {
103 dwCreationDisposition = OPEN_EXISTING;
104 }
105
106 MultiByteToWide(pszFile, szFileName, CP_UTF8);
107
109 szFileName, dwDesiredAccess, FILE_SHARE_READ, NULL, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
110
111 return (INT_PTR)hFile;
112}
#define _O_RDWR
Definition: cabinet.h:39
#define OPEN_EXISTING
Definition: compat.h:775
#define GENERIC_READ
Definition: compat.h:135
#define CreateFileW
Definition: compat.h:741
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
#define CREATE_ALWAYS
Definition: disk.h:72
_In_ HANDLE hFile
Definition: mswsock.h:90
#define GENERIC_WRITE
Definition: nt_native.h:90
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317

◆ FNREAD()

FNREAD ( fnFileRead  )

Definition at line 114 of file cabinet.cpp.

115{
116 DWORD dwBytesRead = 0;
117
118 if (ReadFile((HANDLE)hf, pv, cb, &dwBytesRead, NULL) == FALSE)
119 {
120 dwBytesRead = (DWORD)-1L;
121 }
122
123 return dwBytesRead;
124}
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define DWORD
Definition: nt_native.h:44

◆ FNSEEK()

FNSEEK ( fnFileSeek  )

Definition at line 143 of file cabinet.cpp.

144{
145 return SetFilePointer((HANDLE)hf, dist, NULL, seektype);
146}
#define SetFilePointer
Definition: compat.h:743

◆ FNWRITE()

FNWRITE ( fnFileWrite  )

Definition at line 126 of file cabinet.cpp.

127{
128 DWORD dwBytesWritten = 0;
129
130 if (WriteFile((HANDLE)hf, pv, cb, &dwBytesWritten, NULL) == FALSE)
131 {
132 dwBytesWritten = (DWORD)-1;
133 }
134
135 return dwBytesWritten;
136}
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24

◆ MultiByteToWide()

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

Definition at line 42 of file cabinet.cpp.

43{
44 // determine the needed size
45 INT sz = MultiByteToWideChar(Codepage, 0, szSource, -1, NULL, NULL);
46 if (!sz)
47 return FALSE;
48
49 // do the actual conversion
50 sz = MultiByteToWideChar(Codepage, 0, szSource, -1, szDest.GetBuffer(sz), sz);
51
52 szDest.ReleaseBuffer();
53 return sz != 0;
54}
void ReleaseBuffer(_In_ int nNewLength=-1)
Definition: atlsimpstr.h:387
#define MultiByteToWideChar
Definition: compat.h:110
int32_t INT
Definition: typedefs.h:58

Referenced by FNFDINOTIFY(), and FNOPEN().

◆ WideToMultiByte()

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

Definition at line 26 of file cabinet.cpp.

27{
28 // determine the needed size
29 INT sz = WideCharToMultiByte(Codepage, 0, szSource, -1, NULL, NULL, NULL, NULL);
30 if (!sz)
31 return FALSE;
32
33 // do the actual conversion
34 sz = WideCharToMultiByte(Codepage, 0, szSource, -1, szDest.GetBuffer(sz), sz, NULL, NULL);
35
36 szDest.ReleaseBuffer();
37 return sz != 0;
38}
#define WideCharToMultiByte
Definition: compat.h:111

Referenced by ExtractFilesFromCab(), and FNFDINOTIFY().