ReactOS  0.4.14-dev-358-gbef841c
mspatcha_main.c File Reference
#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "ndk/rtlfuncs.h"
#include "patchapi.h"
#include "lzx.h"
#include "wine/debug.h"
Include dependency graph for mspatcha_main.c:

Go to the source code of this file.

Classes

struct  _SAFE_READ
 
struct  _PATCH_HEADER
 

Macros

#define WIN32_NO_STATUS
 
#define SIGNATURE_MIN_SIZE   9
 
#define UNKNOWN_FLAGS_COMBINATION   0x00c40001
 

Typedefs

typedef struct _SAFE_READ SAFE_READ
 
typedef struct _SAFE_READPSAFE_READ
 

Functions

 WINE_DEFAULT_DEBUG_CHANNEL (mspatcha)
 
ReadByte

Read the next byte available from

Parameters
pRead
pReadThe input buffer
Returns
The byte, or 0
BYTE ReadByte (PSAFE_READ pRead)
 
ReadUShort

Read the next unsigned short available from

Parameters
pRead
pReadThe input buffer
Returns
The unsigned short, or 0
USHORT ReadUShort (PSAFE_READ pRead)
 
ReadDWord

Read the next dword available from

Parameters
pRead
pReadThe input buffer
Returns
The dword, or 0
DWORD ReadDWord (PSAFE_READ pRead)
 
DecodeDWord

Read the next variable length-encoded dword from

Parameters
pRead
pReadThe input buffer
Returns
The dword, or 0
DWORD DecodeDWord (PSAFE_READ pRead)
 
MapFile

Map a view of a file into readonly memory

Parameters
hFileThe input file handle, readable
dwSizeMapped file size (out)
Returns
A Pointer to the start of the memory
static PBYTE MapFile (HANDLE hFile, DWORD *dwSize)
 
BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
 
BOOL WINAPI ApplyPatchToFileA (LPCSTR patch_file, LPCSTR old_file, LPCSTR new_file, ULONG apply_flags)
 
ParseHeader

Parse a Patch file header

Note
The current implementation is far from complete!
Parameters
PatchBuffer pointing to the raw patch data
HeaderThe result of the parsed header
Returns
STATUS_SUCCESS on success, an error code otherwise
DWORD ParseHeader (SAFE_READ *Patch, PATCH_HEADER *Header)
 
CreateNewFileFromPatch

Using the input

Parameters
Headerand
Patch,createa new file on
new_file
HeaderParsed / preprocessed patch header
PatchMemory buffer pointing to the patch payload
new_fileA handle to the output file. This file will be resized
Returns
STATUS_SUCCESS on success, an error code otherwise
DWORD CreateNewFileFromPatch (PATCH_HEADER *Header, SAFE_READ *Patch, HANDLE new_file)
 
BOOL WINAPI ApplyPatchToFileByHandles (HANDLE patch_file, HANDLE old_file, HANDLE new_file, ULONG apply_flags)
 
BOOL WINAPI ApplyPatchToFileW (LPCWSTR patch_file, LPCWSTR old_file, LPCWSTR new_file, ULONG apply_flags)
 
BOOL WINAPI GetFilePatchSignatureA (LPCSTR filename, ULONG flags, PVOID data, ULONG ignore_range_count, PPATCH_IGNORE_RANGE ignore_range, ULONG retain_range_count, PPATCH_RETAIN_RANGE retain_range, ULONG bufsize, PVOID buffer)
 
BOOL WINAPI GetFilePatchSignatureByHandle (HANDLE hFile, ULONG flags, PVOID data, ULONG ignore_range_count, PPATCH_IGNORE_RANGE ignore_range, ULONG retain_range_count, PPATCH_RETAIN_RANGE retain_range, ULONG bufsize, PVOID buffer)
 
BOOL WINAPI GetFilePatchSignatureW (LPCWSTR filename, ULONG flags, PVOID data, ULONG ignore_range_count, PPATCH_IGNORE_RANGE ignore_range, ULONG retain_range_count, PPATCH_RETAIN_RANGE retain_range, ULONG bufsize, PVOID buffer)
 
BOOL WINAPI TestApplyPatchToFileA (LPCSTR patch_file, LPCSTR old_file, ULONG apply_flags)
 
BOOL WINAPI TestApplyPatchToFileByHandles (HANDLE patch_file, HANDLE old_file, ULONG apply_flags)
 
BOOL WINAPI TestApplyPatchToFileW (LPCWSTR patch_file, LPCWSTR old_file, ULONG apply_flags)
 

Variables

static const char szHexString [] = "0123456789abcdef"
 

DecodeInt

Read the next variable length-encoded int from

Parameters
pRead
pReadThe input buffer
Returns
The int, or 0
typedef struct _PATCH_HEADER PATCH_HEADER
 
INT DecodeInt (PSAFE_READ pRead)
 

Macro Definition Documentation

◆ SIGNATURE_MIN_SIZE

#define SIGNATURE_MIN_SIZE   9

Definition at line 32 of file mspatcha_main.c.

◆ UNKNOWN_FLAGS_COMBINATION

#define UNKNOWN_FLAGS_COMBINATION   0x00c40001

Definition at line 34 of file mspatcha_main.c.

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 22 of file mspatcha_main.c.

Typedef Documentation

◆ PATCH_HEADER

◆ PSAFE_READ

◆ SAFE_READ

Function Documentation

◆ ApplyPatchToFileA()

BOOL WINAPI ApplyPatchToFileA ( LPCSTR  patch_file,
LPCSTR  old_file,
LPCSTR  new_file,
ULONG  apply_flags 
)

Definition at line 230 of file mspatcha_main.c.

231 {
232  BOOL ret = FALSE;
233  HANDLE hPatch, hOld, hNew;
234 
237  if (hPatch != INVALID_HANDLE_VALUE)
238  {
241  if (hOld != INVALID_HANDLE_VALUE)
242  {
245  if (hNew != INVALID_HANDLE_VALUE)
246  {
247  ret = ApplyPatchToFileByHandles(hPatch, hOld, hNew, apply_flags);
248  CloseHandle(hNew);
249  }
250  CloseHandle(hOld);
251  }
252  CloseHandle(hPatch);
253  }
254 
255  return ret;
256 }
#define CloseHandle
Definition: compat.h:406
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
BOOL WINAPI ApplyPatchToFileByHandles(HANDLE patch_file, HANDLE old_file, HANDLE new_file, ULONG apply_flags)
#define FILE_SHARE_READ
Definition: compat.h:125
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GENERIC_WRITE
Definition: nt_native.h:90
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:434
int ret
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
#define GENERIC_READ
Definition: compat.h:124
#define CREATE_ALWAYS
Definition: disk.h:72
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition: disk.h:43
#define CreateFileA(a, b, c, d, e, f, g)
Definition: compat.h:407
static UINT patch_file(MSIPACKAGE *package, MSIFILEPATCH *patch)
Definition: files.c:505

Referenced by apply_patch().

◆ ApplyPatchToFileByHandles()

BOOL WINAPI ApplyPatchToFileByHandles ( HANDLE  patch_file,
HANDLE  old_file,
HANDLE  new_file,
ULONG  apply_flags 
)

Definition at line 388 of file mspatcha_main.c.

389 {
390  SAFE_READ Patch, OldFile;
391  DWORD dwStatus;
393 
394  Patch.Root = Patch.Ptr = MapFile(patch_file, &Patch.Size);
395  if (!Patch.Root)
396  {
398  return FALSE;
399  }
400 
401  /* Let's decode the header */
403  if (dwStatus != STATUS_SUCCESS)
404  {
405  UnmapViewOfFile(Patch.Root);
407  return FALSE;
408  }
409 
410  OldFile.Root = OldFile.Ptr = MapFile(old_file, &OldFile.Size);
411  if (OldFile.Root)
412  {
413  DWORD dwCrc;
414 
415  /* Verify the input file */
416  dwCrc = RtlComputeCrc32(0, OldFile.Root, OldFile.Size);
417  if (OldFile.Size == Header.OldSize && dwCrc == Header.OldCrc)
418  {
419  if (apply_flags & APPLY_OPTION_TEST_ONLY)
421  else
423  }
424  else
425  {
427  }
428  UnmapViewOfFile(OldFile.Root);
429  }
430  else
431  {
433  if (dwStatus == STATUS_SUCCESS)
435  }
436 
437  UnmapViewOfFile(Patch.Root);
439  return dwStatus == STATUS_SUCCESS;
440 }
#define APPLY_OPTION_TEST_ONLY
Definition: patchapi.h:28
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
struct _Patch Patch
DWORD CreateNewFileFromPatch(PATCH_HEADER *Header, SAFE_READ *Patch, HANDLE new_file)
Definition: patch.h:62
Definition: Header.h:8
#define ERROR_PATCH_NOT_AVAILABLE
Definition: patchapi.h:40
static PBYTE MapFile(HANDLE hFile, DWORD *dwSize)
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:417
DWORD ParseHeader(SAFE_READ *Patch, PATCH_HEADER *Header)
#define ERROR_PATCH_CORRUPT
Definition: patchapi.h:36
DWORD dwStatus
Definition: mediaobj.idl:95
NTSYSAPI ULONG NTAPI RtlComputeCrc32(_In_ ULONG InitialCrc, _In_ PUCHAR Buffer, _In_ ULONG Length)
#define ERROR_PATCH_WRONG_FILE
Definition: patchapi.h:38
return STATUS_SUCCESS
Definition: btrfs.c:2938
#define UnmapViewOfFile
Definition: compat.h:411
static UINT patch_file(MSIPACKAGE *package, MSIFILEPATCH *patch)
Definition: files.c:505

Referenced by ApplyPatchToFileA(), ApplyPatchToFileW(), and TestApplyPatchToFileByHandles().

◆ ApplyPatchToFileW()

BOOL WINAPI ApplyPatchToFileW ( LPCWSTR  patch_file,
LPCWSTR  old_file,
LPCWSTR  new_file,
ULONG  apply_flags 
)

Definition at line 445 of file mspatcha_main.c.

446 {
447  BOOL ret = FALSE;
448  HANDLE hPatch, hOld, hNew;
449 
452  if (hPatch != INVALID_HANDLE_VALUE)
453  {
456  if (hOld != INVALID_HANDLE_VALUE)
457  {
460  if (hNew != INVALID_HANDLE_VALUE)
461  {
462  ret = ApplyPatchToFileByHandles(hPatch, hOld, hNew, apply_flags);
463  CloseHandle(hNew);
464  }
465  CloseHandle(hOld);
466  }
467  CloseHandle(hPatch);
468  }
469 
470  return ret;
471 }
#define CloseHandle
Definition: compat.h:406
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
BOOL WINAPI ApplyPatchToFileByHandles(HANDLE patch_file, HANDLE old_file, HANDLE new_file, ULONG apply_flags)
#define FILE_SHARE_READ
Definition: compat.h:125
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GENERIC_WRITE
Definition: nt_native.h:90
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:434
int ret
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
#define GENERIC_READ
Definition: compat.h:124
#define CREATE_ALWAYS
Definition: disk.h:72
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition: disk.h:43
#define CreateFileW
Definition: compat.h:408
static UINT patch_file(MSIPACKAGE *package, MSIFILEPATCH *patch)
Definition: files.c:505

Referenced by patch_file().

◆ CreateNewFileFromPatch()

DWORD CreateNewFileFromPatch ( PATCH_HEADER Header,
SAFE_READ Patch,
HANDLE  new_file 
)

Definition at line 340 of file mspatcha_main.c.

341 {
342  SAFE_READ NewFile;
343  HANDLE hMap;
344  USHORT BlockSize;
345  DWORD dwStatus;
346  struct LZXstate* state;
347  int lzxResult;
348 
349  hMap = CreateFileMappingW(new_file, NULL, PAGE_READWRITE, 0, Header->OutputSize, NULL);
350  if (hMap == INVALID_HANDLE_VALUE)
352 
353  NewFile.Root = NewFile.Ptr = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0);
354  CloseHandle(hMap);
355  NewFile.Size = Header->OutputSize;
356 
357  if (!NewFile.Root)
359 
360  /* At this point Patch->Ptr should point to the payload */
361  BlockSize = ReadUShort(Patch);
362 
363  /* This window size does not work on all files (for example, MS SQL Express 2008 setup) */
364  state = LZXinit(17);
365  if (state)
366  {
367  lzxResult = LZXdecompress(state, Patch->Ptr, NewFile.Ptr, BlockSize, NewFile.Size);
369 
370  if (lzxResult == DECR_OK)
372  else
374  }
375  else
376  {
378  }
379 
380  UnmapViewOfFile(NewFile.Root);
381  return dwStatus;
382 }
#define CloseHandle
Definition: compat.h:406
struct LZXstate * LZXinit(int wndsize)
Definition: lzx.c:172
#define MapViewOfFile
Definition: compat.h:410
void LZXteardown(struct LZXstate *pState)
Definition: lzx.c:209
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
#define DECR_OK
Definition: mszip.h:79
Definition: patch.h:62
Definition: Header.h:8
smooth NULL
Definition: ftsmooth.c:416
#define ERROR_PATCH_NOT_AVAILABLE
Definition: patchapi.h:40
#define CreateFileMappingW(a, b, c, d, e, f)
Definition: compat.h:409
#define FILE_MAP_WRITE
Definition: winbase.h:154
USHORT ReadUShort(PSAFE_READ pRead)
Definition: mspatcha_main.c:78
unsigned long DWORD
Definition: ntddk_ex.h:95
static int state
Definition: maze.c:121
#define ERROR_PATCH_DECODE_FAILURE
Definition: patchapi.h:35
DWORD dwStatus
Definition: mediaobj.idl:95
unsigned short USHORT
Definition: pedump.c:61
int LZXdecompress(struct LZXstate *pState, unsigned char *inpos, unsigned char *outpos, int inlen, int outlen)
Definition: lzx.c:461
return STATUS_SUCCESS
Definition: btrfs.c:2938
#define UnmapViewOfFile
Definition: compat.h:411
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

Referenced by ApplyPatchToFileByHandles().

◆ DecodeDWord()

DWORD DecodeDWord ( PSAFE_READ  pRead)

Definition at line 120 of file mspatcha_main.c.

121 {
122  UINT Result = 0, offset;
123 
124  for (offset = 0; offset < 32; offset += 7)
125  {
126  DWORD val = ReadByte(pRead);
127  Result |= ((val & 0x7f) << offset);
128  if (val & 0x80)
129  break;
130  }
131 
132  return Result;
133 }
BYTE ReadByte(PSAFE_READ pRead)
Definition: mspatcha_main.c:57
GLintptr offset
Definition: glext.h:5920
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
GLuint GLfloat * val
Definition: glext.h:7180
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned int UINT
Definition: ndis.h:50

Referenced by ParseHeader().

◆ DecodeInt()

INT DecodeInt ( PSAFE_READ  pRead)

Definition at line 145 of file mspatcha_main.c.

146 {
147  INT Result = 0, offset;
148 
149  for (offset = 0; offset < 32; offset += 6)
150  {
151  INT val = (INT)(DWORD)ReadByte(pRead);
152  Result |= ((val & 0x3f) << offset);
153  if (val & 0x80)
154  {
155  if (val & 0x40)
156  Result *= -1;
157  break;
158  }
159  }
160 
161  return Result;
162 }
BYTE ReadByte(PSAFE_READ pRead)
Definition: mspatcha_main.c:57
#define INT
Definition: polytest.cpp:20
GLintptr offset
Definition: glext.h:5920
int32_t INT
Definition: typedefs.h:56
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
GLuint GLfloat * val
Definition: glext.h:7180
unsigned long DWORD
Definition: ntddk_ex.h:95

Referenced by ParseHeader().

◆ DllMain()

BOOL WINAPI DllMain ( HINSTANCE  hinstDLL,
DWORD  fdwReason,
LPVOID  lpvReserved 
)

Definition at line 215 of file mspatcha_main.c.

216 {
217  switch (fdwReason)
218  {
219  case DLL_PROCESS_ATTACH:
220  DisableThreadLibraryCalls(hinstDLL);
221  break;
222  }
223 
224  return TRUE;
225 }
#define TRUE
Definition: types.h:120
BOOL WINAPI DisableThreadLibraryCalls(IN HMODULE hLibModule)
Definition: loader.c:85
#define DLL_PROCESS_ATTACH
Definition: compat.h:120

◆ GetFilePatchSignatureA()

BOOL WINAPI GetFilePatchSignatureA ( LPCSTR  filename,
ULONG  flags,
PVOID  data,
ULONG  ignore_range_count,
PPATCH_IGNORE_RANGE  ignore_range,
ULONG  retain_range_count,
PPATCH_RETAIN_RANGE  retain_range,
ULONG  bufsize,
PVOID  buffer 
)

Definition at line 476 of file mspatcha_main.c.

479 {
480  BOOL ret = FALSE;
481  HANDLE hFile;
482 
486  {
487  ret = GetFilePatchSignatureByHandle(hFile, flags, data, ignore_range_count, ignore_range,
488  retain_range_count, retain_range, bufsize, buffer);
490  }
491 
492  return ret;
493 }
#define CloseHandle
Definition: compat.h:406
BOOL WINAPI GetFilePatchSignatureByHandle(HANDLE hFile, ULONG flags, PVOID data, ULONG ignore_range_count, PPATCH_IGNORE_RANGE ignore_range, ULONG retain_range_count, PPATCH_RETAIN_RANGE retain_range, ULONG bufsize, PVOID buffer)
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
GLuint buffer
Definition: glext.h:5915
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
GLenum GLuint GLsizei bufsize
Definition: glext.h:7473
const char * filename
Definition: ioapi.h:135
#define FILE_SHARE_READ
Definition: compat.h:125
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:434
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLbitfield flags
Definition: glext.h:7161
int ret
#define GENERIC_READ
Definition: compat.h:124
_In_ HANDLE hFile
Definition: mswsock.h:90
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition: disk.h:43
#define CreateFileA(a, b, c, d, e, f, g)
Definition: compat.h:407

Referenced by validate_signature().

◆ GetFilePatchSignatureByHandle()

BOOL WINAPI GetFilePatchSignatureByHandle ( HANDLE  hFile,
ULONG  flags,
PVOID  data,
ULONG  ignore_range_count,
PPATCH_IGNORE_RANGE  ignore_range,
ULONG  retain_range_count,
PPATCH_RETAIN_RANGE  retain_range,
ULONG  bufsize,
PVOID  buffer 
)

Definition at line 498 of file mspatcha_main.c.

501 {
502  BOOL ret = FALSE;
503  DWORD dwSize, ulCrc;
504  PBYTE pData;
505 
506  if (flags)
507  FIXME("Unhandled flags 0x%x\n", flags);
508  if (ignore_range_count)
509  FIXME("Unhandled ignore_range_count %u\n", ignore_range_count);
510  if (retain_range_count)
511  FIXME("Unhandled ignore_range_count %u\n", retain_range_count);
512 
513  if ((pData = MapFile(hFile, &dwSize)))
514  {
515  if (dwSize >= 2 && *(PWORD)pData == IMAGE_DOS_SIGNATURE)
516  {
517  FIXME("Potentially unimplemented case, normalized signature\n");
518  }
519 
520  ulCrc = RtlComputeCrc32(0, pData, dwSize);
522  {
523  char *pBuffer = buffer;
524  pBuffer[8] = '\0';
525  for (dwSize = 0; dwSize < 8; ++dwSize)
526  {
527  pBuffer[7 - dwSize] = szHexString[ulCrc & 0xf];
528  ulCrc >>= 4;
529  }
530  ret = TRUE;
531  }
533 
536  }
537 
538  return ret;
539 }
static const char szHexString[]
Definition: mspatcha_main.c:31
#define TRUE
Definition: types.h:120
GLuint buffer
Definition: glext.h:5915
GLenum GLuint GLsizei bufsize
Definition: glext.h:7473
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89
unsigned int BOOL
Definition: ntddk_ex.h:94
#define FIXME(fmt,...)
Definition: debug.h:110
PVOID pBuffer
#define SIGNATURE_MIN_SIZE
Definition: mspatcha_main.c:32
WORD * PWORD
Definition: pedump.c:67
static PBYTE MapFile(HANDLE hFile, DWORD *dwSize)
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SetLastError(x)
Definition: compat.h:417
GLbitfield flags
Definition: glext.h:7161
int ret
_In_ HANDLE hFile
Definition: mswsock.h:90
NTSYSAPI ULONG NTAPI RtlComputeCrc32(_In_ ULONG InitialCrc, _In_ PUCHAR Buffer, _In_ ULONG Length)
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1827
#define UnmapViewOfFile
Definition: compat.h:411
BYTE * PBYTE
Definition: pedump.c:66
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

Referenced by GetFilePatchSignatureA(), GetFilePatchSignatureW(), and validate_signature().

◆ GetFilePatchSignatureW()

BOOL WINAPI GetFilePatchSignatureW ( LPCWSTR  filename,
ULONG  flags,
PVOID  data,
ULONG  ignore_range_count,
PPATCH_IGNORE_RANGE  ignore_range,
ULONG  retain_range_count,
PPATCH_RETAIN_RANGE  retain_range,
ULONG  bufsize,
PVOID  buffer 
)

Definition at line 544 of file mspatcha_main.c.

547 {
548  CHAR LocalBuf[SIGNATURE_MIN_SIZE];
549  BOOL ret = FALSE;
550  HANDLE hFile;
551 
555  {
556  ret = GetFilePatchSignatureByHandle(hFile, flags, data, ignore_range_count, ignore_range,
557  retain_range_count, retain_range, sizeof(LocalBuf), LocalBuf);
559 
560  if (bufsize < (SIGNATURE_MIN_SIZE * sizeof(WCHAR)))
561  {
563  return FALSE;
564  }
565  if (ret)
566  {
567  MultiByteToWideChar(CP_ACP, 0, LocalBuf, -1, buffer, bufsize / sizeof(WCHAR));
568  }
569  }
570 
571  return ret;
572 }
#define CloseHandle
Definition: compat.h:406
#define CP_ACP
Definition: compat.h:99
BOOL WINAPI GetFilePatchSignatureByHandle(HANDLE hFile, ULONG flags, PVOID data, ULONG ignore_range_count, PPATCH_IGNORE_RANGE ignore_range, ULONG retain_range_count, PPATCH_RETAIN_RANGE retain_range, ULONG bufsize, PVOID buffer)
char CHAR
Definition: xmlstorage.h:175
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
GLuint buffer
Definition: glext.h:5915
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
GLenum GLuint GLsizei bufsize
Definition: glext.h:7473
const char * filename
Definition: ioapi.h:135
#define FILE_SHARE_READ
Definition: compat.h:125
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:434
#define SIGNATURE_MIN_SIZE
Definition: mspatcha_main.c:32
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define SetLastError(x)
Definition: compat.h:417
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLbitfield flags
Definition: glext.h:7161
int ret
#define GENERIC_READ
Definition: compat.h:124
_In_ HANDLE hFile
Definition: mswsock.h:90
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition: disk.h:43
#define MultiByteToWideChar
Definition: compat.h:100
#define CreateFileW
Definition: compat.h:408
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

Referenced by validate_signature().

◆ MapFile()

static PBYTE MapFile ( HANDLE  hFile,
DWORD dwSize 
)
static

Definition at line 194 of file mspatcha_main.c.

195 {
196  HANDLE hMap;
197  PVOID pView;
198 
201  if (hMap != INVALID_HANDLE_VALUE)
202  {
203  pView = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
204  CloseHandle(hMap);
205  return pView;
206  }
207 
208  return NULL;
209 }
#define CloseHandle
Definition: compat.h:406
#define MapViewOfFile
Definition: compat.h:410
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
smooth NULL
Definition: ftsmooth.c:416
#define FILE_MAP_READ
Definition: compat.h:435
#define CreateFileMappingW(a, b, c, d, e, f)
Definition: compat.h:409
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:481
_In_ HANDLE hFile
Definition: mswsock.h:90
#define PAGE_READONLY
Definition: compat.h:127
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:54

Referenced by ApplyPatchToFileByHandles(), and GetFilePatchSignatureByHandle().

◆ ParseHeader()

DWORD ParseHeader ( SAFE_READ Patch,
PATCH_HEADER Header 
)

Definition at line 272 of file mspatcha_main.c.

273 {
274  DWORD Crc, Unknown;
275  int Delta;
276 
277  ZeroMemory(Header, sizeof(*Header));
278 
279  /* Validate the patch */
280  Crc = RtlComputeCrc32(0, Patch->Root, Patch->Size);
281  if (Crc != ~0)
282  return ERROR_PATCH_CORRUPT;
283 
284  if (ReadDWord(Patch) != '91AP')
286 
287  /* Read the flags, warn about an unknown combination */
288  Header->Flags = ReadDWord(Patch);
289  if (Header->Flags ^ UNKNOWN_FLAGS_COMBINATION)
290  ERR("Unknown flags: 0x%x, patch will most likely fail\n", Header->Flags ^ UNKNOWN_FLAGS_COMBINATION);
291 
292  /* 0x5bb3284e, 0x5bb33562, 0x5bb357b1 */
294  TRACE("Unknown: 0x%x\n", Unknown);
295 
296  Header->OutputSize = DecodeDWord(Patch);
297  Header->OutputCrc = ReadDWord(Patch);
298 
300  if (Unknown != 1)
301  ERR("Field after CRC is not 1 but %u\n", Unknown);
302 
303  Delta = DecodeInt(Patch);
304  Header->OldSize = Header->OutputSize + Delta;
305  Header->OldCrc = ReadDWord(Patch);
306 
308  if (Unknown != 0)
309  ERR("Field1 after OldCrc is not 0 but %u\n", Unknown);
310 
312  if (Unknown != 0)
313  ERR("Field2 after OldCrc is not 0 but %u\n", Unknown);
314 
315  Header->DataSize = DecodeDWord(Patch);
316  /* Remaining data, minus the CRC appended */
317  if (Header->DataSize != (Patch->Size - (Patch->Ptr - Patch->Root) - sizeof(DWORD)))
318  {
319  ERR("Unable to read header, check previous logging!\n");
321  }
322  return STATUS_SUCCESS;
323 }
BYTE ReadByte(PSAFE_READ pRead)
Definition: mspatcha_main.c:57
DWORD ReadDWord(PSAFE_READ pRead)
Definition: mspatcha_main.c:99
#define ZeroMemory
Definition: winbase.h:1642
Definition: patch.h:62
Definition: Header.h:8
#define TRACE(s)
Definition: solgame.cpp:4
USHORT ReadUShort(PSAFE_READ pRead)
Definition: mspatcha_main.c:78
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ERROR_PATCH_DECODE_FAILURE
Definition: patchapi.h:35
#define ERROR_PATCH_CORRUPT
Definition: patchapi.h:36
#define ERR(fmt,...)
Definition: debug.h:109
NTSYSAPI ULONG NTAPI RtlComputeCrc32(_In_ ULONG InitialCrc, _In_ PUCHAR Buffer, _In_ ULONG Length)
static ULONG Delta
Definition: xboxvideo.c:32
DWORD DecodeDWord(PSAFE_READ pRead)
INT DecodeInt(PSAFE_READ pRead)
return STATUS_SUCCESS
Definition: btrfs.c:2938
#define UNKNOWN_FLAGS_COMBINATION
Definition: mspatcha_main.c:34

Referenced by ApplyPatchToFileByHandles().

◆ ReadByte()

BYTE ReadByte ( PSAFE_READ  pRead)

Definition at line 57 of file mspatcha_main.c.

58 {
59  if (pRead->Ptr + sizeof(BYTE) <= (pRead->Root + pRead->Size))
60  {
61  BYTE Value = *(PBYTE)pRead->Ptr;
62  pRead->Ptr += sizeof(BYTE);
63  return Value;
64  }
65  pRead->Ptr = pRead->Root + pRead->Size;
66  return 0;
67 }
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2343
unsigned char BYTE
Definition: mem.h:68
BYTE * PBYTE
Definition: pedump.c:66

Referenced by DecodeDWord(), DecodeInt(), and ParseHeader().

◆ ReadDWord()

DWORD ReadDWord ( PSAFE_READ  pRead)

Definition at line 99 of file mspatcha_main.c.

100 {
101  if (pRead->Ptr + sizeof(DWORD) <= (pRead->Root + pRead->Size))
102  {
103  DWORD Value = *(PDWORD)pRead->Ptr;
104  pRead->Ptr += sizeof(DWORD);
105  return Value;
106  }
107  pRead->Ptr = pRead->Root + pRead->Size;
108  return 0;
109 }
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2343
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD * PDWORD
Definition: pedump.c:68

Referenced by ParseHeader().

◆ ReadUShort()

USHORT ReadUShort ( PSAFE_READ  pRead)

Definition at line 78 of file mspatcha_main.c.

79 {
80  if (pRead->Ptr + sizeof(USHORT) <= (pRead->Root + pRead->Size))
81  {
82  USHORT Value = *(PUSHORT)pRead->Ptr;
83  pRead->Ptr += sizeof(USHORT);
84  return Value;
85  }
86  pRead->Ptr = pRead->Root + pRead->Size;
87  return 0;
88 }
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2343
unsigned short USHORT
Definition: pedump.c:61
unsigned short * PUSHORT
Definition: retypes.h:2

Referenced by CreateNewFileFromPatch(), and ParseHeader().

◆ TestApplyPatchToFileA()

BOOL WINAPI TestApplyPatchToFileA ( LPCSTR  patch_file,
LPCSTR  old_file,
ULONG  apply_flags 
)

Definition at line 577 of file mspatcha_main.c.

578 {
579  BOOL ret = FALSE;
580  HANDLE hPatch, hOld;
581 
584  if (hPatch != INVALID_HANDLE_VALUE)
585  {
588  if (hOld != INVALID_HANDLE_VALUE)
589  {
590  ret = TestApplyPatchToFileByHandles(hPatch, hOld, apply_flags);
591  CloseHandle(hOld);
592  }
593  CloseHandle(hPatch);
594  }
595 
596  return ret;
597 }
#define CloseHandle
Definition: compat.h:406
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_SHARE_READ
Definition: compat.h:125
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI TestApplyPatchToFileByHandles(HANDLE patch_file, HANDLE old_file, ULONG apply_flags)
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:434
int ret
#define GENERIC_READ
Definition: compat.h:124
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition: disk.h:43
#define CreateFileA(a, b, c, d, e, f, g)
Definition: compat.h:407
static UINT patch_file(MSIPACKAGE *package, MSIFILEPATCH *patch)
Definition: files.c:505

Referenced by apply_patch().

◆ TestApplyPatchToFileByHandles()

BOOL WINAPI TestApplyPatchToFileByHandles ( HANDLE  patch_file,
HANDLE  old_file,
ULONG  apply_flags 
)

Definition at line 602 of file mspatcha_main.c.

603 {
605 }
#define APPLY_OPTION_TEST_ONLY
Definition: patchapi.h:28
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
BOOL WINAPI ApplyPatchToFileByHandles(HANDLE patch_file, HANDLE old_file, HANDLE new_file, ULONG apply_flags)
static UINT patch_file(MSIPACKAGE *package, MSIFILEPATCH *patch)
Definition: files.c:505

Referenced by apply_patch(), TestApplyPatchToFileA(), and TestApplyPatchToFileW().

◆ TestApplyPatchToFileW()

BOOL WINAPI TestApplyPatchToFileW ( LPCWSTR  patch_file,
LPCWSTR  old_file,
ULONG  apply_flags 
)

Definition at line 610 of file mspatcha_main.c.

611 {
612  BOOL ret = FALSE;
613  HANDLE hPatch, hOld;
614 
617  if (hPatch != INVALID_HANDLE_VALUE)
618  {
621  if (hOld != INVALID_HANDLE_VALUE)
622  {
623  ret = TestApplyPatchToFileByHandles(hPatch, hOld, apply_flags);
624  CloseHandle(hOld);
625  }
626  CloseHandle(hPatch);
627  }
628 
629  return ret;
630 }
#define CloseHandle
Definition: compat.h:406
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_SHARE_READ
Definition: compat.h:125
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI TestApplyPatchToFileByHandles(HANDLE patch_file, HANDLE old_file, ULONG apply_flags)
smooth NULL
Definition: ftsmooth.c:416
#define OPEN_EXISTING
Definition: compat.h:434
int ret
#define GENERIC_READ
Definition: compat.h:124
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition: disk.h:43
#define CreateFileW
Definition: compat.h:408
static UINT patch_file(MSIPACKAGE *package, MSIFILEPATCH *patch)
Definition: files.c:505

◆ WINE_DEFAULT_DEBUG_CHANNEL()

WINE_DEFAULT_DEBUG_CHANNEL ( mspatcha  )

Variable Documentation

◆ szHexString

const char szHexString[] = "0123456789abcdef"
static

Definition at line 31 of file mspatcha_main.c.

Referenced by GetFilePatchSignatureByHandle().