From b93b48779d2305046be86c5119b36e78b0f405fe Mon Sep 17 00:00:00 2001 From: John Chadwick Date: Mon, 22 Jul 2024 22:40:04 -0400 Subject: [PATCH] Enforce clang-format style --- .github/workflows/format.yml | 16 + .github/workflows/main.yml | 7 +- .github/workflows/release.yml | 6 +- flake.nix | 4 +- src/common-fnptr.h | 162 +++++---- src/common.h | 4 +- src/config.h | 10 +- src/hex.c | 86 ++--- src/regex.c | 615 +++++++++++++++++----------------- 9 files changed, 467 insertions(+), 443 deletions(-) create mode 100644 .github/workflows/format.yml diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 0000000..35eb3ad --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,16 @@ +name: Format +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - uses: DeterminateSystems/nix-installer-action@v11 + - uses: DeterminateSystems/magic-nix-cache-action@v6 + - name: Check formatting + run: nix develop --command clang-format --dry-run -Werror src/**.c src/**.h diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ee46a00..89b9413 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,10 +10,9 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 - - name: Install Nix - uses: cachix/install-nix-action@v19 - with: - nix_path: nixpkgs=channel:nixos-unstable + - uses: DeterminateSystems/nix-installer-action@v11 + - uses: DeterminateSystems/magic-nix-cache-action@v6 + - uses: DeterminateSystems/flake-checker-action@v7 - name: Configure run: nix develop --command make -j8 all - name: Upload diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e5c6d66..15d7eaa 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,10 +10,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 - - name: Install Nix - uses: cachix/install-nix-action@v19 - with: - nix_path: nixpkgs=channel:nixos-unstable + - uses: DeterminateSystems/nix-installer-action@v11 + - uses: DeterminateSystems/magic-nix-cache-action@v6 - name: Compile run: nix develop --command make -j8 all - name: Create Release diff --git a/flake.nix b/flake.nix index 950d988..31123f0 100644 --- a/flake.nix +++ b/flake.nix @@ -63,7 +63,9 @@ }; # Dev shell - devShell = pkgs.mkShell { nativeBuildInputs = nativeBuildInputs ++ nativeCheckInputs; }; + devShell = pkgs.mkShell { + nativeBuildInputs = nativeBuildInputs ++ nativeCheckInputs ++ [ pkgs.clang-tools ]; + }; # Clangd setup setupClangd = pkgs.writeShellScriptBin "setup-clangd.sh" '' diff --git a/src/common-fnptr.h b/src/common-fnptr.h index 998c877..012710c 100644 --- a/src/common-fnptr.h +++ b/src/common-fnptr.h @@ -14,18 +14,18 @@ #ifdef __MINGW32__ typedef struct _DNS_QUERY_REQUEST { - ULONG Version; - PCWSTR QueryName; - WORD QueryType; - ULONG64 QueryOptions; - PVOID pDnsServerList; - ULONG InterfaceIndex; - PVOID pQueryCompletionCallback; - PVOID pQueryContext; + ULONG Version; + PCWSTR QueryName; + WORD QueryType; + ULONG64 QueryOptions; + PVOID pDnsServerList; + ULONG InterfaceIndex; + PVOID pQueryCompletionCallback; + PVOID pQueryContext; } DNS_QUERY_REQUEST, *PDNS_QUERY_REQUEST; typedef struct _DNS_QUERY_CANCEL { - CHAR Reserved[32]; + CHAR Reserved[32]; } DNS_QUERY_CANCEL, *PDNS_QUERY_CANCEL; #endif @@ -39,84 +39,100 @@ typedef struct tagINITCOMMONCONTROLSEX { typedef BOOL(STDCALL *PFNINITCOMMONCONTROLSEXPROC)(const INITCOMMONCONTROLSEX *picce); // general -typedef BOOL (WINAPI *PFNDLLMAINPROC)(HANDLE, DWORD, LPVOID); +typedef BOOL(WINAPI *PFNDLLMAINPROC)(HANDLE, DWORD, LPVOID); // ws2_32.dll -typedef int (STDCALL *PFNCONNECTPROC)(SOCKET, const struct sockaddr *, int); -typedef BOOL (STDCALL *PFNCONNECTEXPROC)(SOCKET, const struct sockaddr *, int, PVOID, DWORD, LPDWORD, LPOVERLAPPED); -typedef int (STDCALL *PFNWSACONNECTPROC)(SOCKET, const struct sockaddr *, int, LPWSABUF, LPWSABUF, LPQOS, LPQOS); -typedef WORD (WSAAPI *PFNHTONSPROC)(WORD); -typedef INT (WSAAPI *PFNGETADDRINFO)(PCSTR, PCSTR, const PADDRINFOA, PADDRINFOA*); -typedef VOID (WSAAPI *PFNFREEADDRINFO)(PADDRINFOA); +typedef int(STDCALL *PFNCONNECTPROC)(SOCKET, const struct sockaddr *, int); +typedef BOOL(STDCALL *PFNCONNECTEXPROC)(SOCKET, const struct sockaddr *, int, PVOID, DWORD, LPDWORD, + LPOVERLAPPED); +typedef int(STDCALL *PFNWSACONNECTPROC)(SOCKET, const struct sockaddr *, int, LPWSABUF, LPWSABUF, + LPQOS, LPQOS); +typedef WORD(WSAAPI *PFNHTONSPROC)(WORD); +typedef INT(WSAAPI *PFNGETADDRINFO)(PCSTR, PCSTR, const PADDRINFOA, PADDRINFOA *); +typedef VOID(WSAAPI *PFNFREEADDRINFO)(PADDRINFOA); // dnsapi.dll -typedef DNS_STATUS (STDCALL *PFNDNSQUERYEXPROC)(PDNS_QUERY_REQUEST pQueryRequest, PDNS_QUERY_RESULT pQueryResults, PDNS_QUERY_CANCEL pCancelHandle); +typedef DNS_STATUS(STDCALL *PFNDNSQUERYEXPROC)(PDNS_QUERY_REQUEST pQueryRequest, + PDNS_QUERY_RESULT pQueryResults, + PDNS_QUERY_CANCEL pCancelHandle); // advapi32.dll -typedef BOOL (STDCALL *PFNCRYPTDECRYPTPROC)(HCRYPTKEY, HCRYPTHASH, BOOL, DWORD, BYTE *, DWORD *); -typedef BOOL (STDCALL *PFNCRYPTGETHASHPARAMPROC)(HCRYPTHASH, DWORD, BYTE*, DWORD*, DWORD); -typedef BOOL (STDCALL *PFNCRYPTVERIFYSIGNATUREAPROC)(HCRYPTHASH, CONST BYTE *, DWORD, HCRYPTKEY, LPCSTR, DWORD); -typedef BOOL (STDCALL *PFNCRYPTIMPORTKEYPROC)(HCRYPTPROV, CONST BYTE *, DWORD, HCRYPTKEY, DWORD, HCRYPTKEY *); +typedef BOOL(STDCALL *PFNCRYPTDECRYPTPROC)(HCRYPTKEY, HCRYPTHASH, BOOL, DWORD, BYTE *, DWORD *); +typedef BOOL(STDCALL *PFNCRYPTGETHASHPARAMPROC)(HCRYPTHASH, DWORD, BYTE *, DWORD *, DWORD); +typedef BOOL(STDCALL *PFNCRYPTVERIFYSIGNATUREAPROC)(HCRYPTHASH, CONST BYTE *, DWORD, HCRYPTKEY, + LPCSTR, DWORD); +typedef BOOL(STDCALL *PFNCRYPTIMPORTKEYPROC)(HCRYPTPROV, CONST BYTE *, DWORD, HCRYPTKEY, DWORD, + HCRYPTKEY *); // user32.dll -typedef int (STDCALL *PFNMESSAGEBOXAPROC)(HWND, LPCSTR, LPCSTR, UINT); -typedef HWND (STDCALL *PFNCREATEWINDOWEXAPROC)(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID); -typedef LRESULT (STDCALL *PFNSENDMESSAGEPROC)(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); -typedef BOOL (STDCALL *PFNPEEKMESSAGEAPROC)(LPMSG, HWND, UINT, UINT, UINT); -typedef BOOL (STDCALL *PFNGETMESSAGEAPROC)(LPMSG, HWND, UINT, UINT); -typedef UINT_PTR (STDCALL *PFNSETTIMERPROC)(HWND, UINT_PTR, UINT, TIMERPROC); -typedef int (STDCALL *PFNWVSPRINTFAPROC)(LPSTR, LPCSTR, void *); +typedef int(STDCALL *PFNMESSAGEBOXAPROC)(HWND, LPCSTR, LPCSTR, UINT); +typedef HWND(STDCALL *PFNCREATEWINDOWEXAPROC)(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, + HWND, HMENU, HINSTANCE, LPVOID); +typedef LRESULT(STDCALL *PFNSENDMESSAGEPROC)(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); +typedef BOOL(STDCALL *PFNPEEKMESSAGEAPROC)(LPMSG, HWND, UINT, UINT, UINT); +typedef BOOL(STDCALL *PFNGETMESSAGEAPROC)(LPMSG, HWND, UINT, UINT); +typedef UINT_PTR(STDCALL *PFNSETTIMERPROC)(HWND, UINT_PTR, UINT, TIMERPROC); +typedef int(STDCALL *PFNWVSPRINTFAPROC)(LPSTR, LPCSTR, void *); // kernel32.dll -typedef HMODULE (STDCALL *PFNLOADLIBRARYAPROC)(LPCSTR); -typedef VOID (STDCALL *PFNFREELIBRARYPROC)(HMODULE); -typedef FARPROC (STDCALL *PFNGETPROCADDRESSPROC)(HMODULE, LPCSTR); -typedef DWORD (STDCALL *PFNGETENVIRONMENTVARIABLEAPROC)(LPCSTR, LPSTR, DWORD); -typedef BOOL (STDCALL *PFNCREATEPROCESSAPROC)(LPCSTR, LPSTR, LPSECURITY_ATTRIBUTES, LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, LPCSTR, LPSTARTUPINFOA, LPPROCESS_INFORMATION); -typedef BOOL (STDCALL *PFNISWOW64PROCESSPROC)(HANDLE, PBOOL); -typedef HANDLE (STDCALL *PFNCREATEFILEAPROC)(LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE); -typedef HANDLE (STDCALL *PFNCREATEFILEMAPPINGAPROC)(HANDLE, LPSECURITY_ATTRIBUTES, DWORD, DWORD, DWORD, LPCSTR); -typedef HANDLE (STDCALL *PFNOPENMUTEXAPROC)(DWORD, BOOL, LPCSTR); -typedef HANDLE (STDCALL *PFNCREATEMUTEXAPROC)(LPSECURITY_ATTRIBUTES, BOOL, LPCSTR); -typedef DWORD (STDCALL *PFNGETCURRENTTHREADIDPROC)(VOID); -typedef DWORD (STDCALL *PFNGETCURRENTPROCESSIDPROC)(VOID); -typedef VOID (STDCALL *PFNSLEEPPROC)(DWORD); -typedef DWORD (STDCALL *PFNSLEEPEXPROC)(DWORD, BOOL); -typedef DWORD (STDCALL *PFNWAITFORSINGLEOBJECTPROC)(HANDLE, DWORD); -typedef HANDLE (STDCALL *PFNOPENEVENTAPROC)(DWORD, BOOL, LPCSTR); -typedef HANDLE (STDCALL *PFNCREATEEVENTAPROC)(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCSTR lpName); -typedef BOOL (STDCALL *PFNGETEXITCODEPROCESSPROC)(HANDLE hProcess, LPDWORD lpExitCode); -typedef HLOCAL (STDCALL *PFNLOCALALLOCPROC)(UINT, UINT); -typedef HLOCAL (STDCALL *PFNLOCALFREEPROC)(HLOCAL); -typedef HMODULE (STDCALL *PFNGETMODULEHANDLEAPROC)(LPCSTR); -typedef DWORD (STDCALL *PFNGETMODULEFILENAMEAPROC)(HMODULE, LPCH, DWORD); -typedef DWORD (STDCALL *PFNGETFILEATTRIBUTESAPROC)(LPCSTR); -typedef VOID (STDCALL *PFNEXITPROCESSPROC)(UINT); -typedef DWORD (STDCALL *PFNGETFILESIZEPROC)(HANDLE, LPDWORD); -typedef BOOL (STDCALL *PFNREADFILEPROC)(HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED); -typedef BOOL (STDCALL *PFNWRITEFILEPROC)(HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED); -typedef BOOL (STDCALL *PFNCLOSEHANDLEPROC)(HANDLE); -typedef LPSTR (STDCALL *PFNLSTRCPYAPROC)(LPSTR, LPCSTR); -typedef DWORD (STDCALL *PFNGETLASTERRORPROC)(VOID); -typedef BOOL (STDCALL *PFNSETENVIRONMENTVARIABLEAPROC)(LPCSTR, LPCSTR); -typedef BOOL (STDCALL *PFNVIRTUALPROTECTEXPROC)(HANDLE, LPVOID, SIZE_T, DWORD, PDWORD); -typedef BOOL (STDCALL *PFNREADPROCESSMEMORYPROC)(HANDLE, LPCVOID, LPVOID, SIZE_T, SIZE_T *); -typedef BOOL (STDCALL *PFNWRITEPROCESSMEMORYPROC)(HANDLE, LPVOID, LPCVOID, SIZE_T, SIZE_T *); -typedef BOOL (STDCALL *PFNFLUSHINSTRUCTIONCACHEPROC)(HANDLE, LPCVOID, SIZE_T); -typedef BOOL (STDCALL *PFNVIRTUALPROTECTPROC)(LPVOID, SIZE_T, DWORD, PDWORD); -typedef HANDLE (STDCALL *PFNGETCURRENTPROCESSPROC)(VOID); -typedef HANDLE (STDCALL *PFNCREATETHREADPROC)(LPSECURITY_ATTRIBUTES, SIZE_T, LPTHREAD_START_ROUTINE, LPVOID, DWORD, LPDWORD); -typedef HANDLE (STDCALL *PFNGETSTDHANDLEPROC)(DWORD); -typedef SIZE_T (STDCALL *PFNVIRTUALQUERYPROC)(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength); -typedef VOID (STDCALL *PFNGETSTARTUPINFOAPROC)(LPSTARTUPINFOA lpStartupInfo); -typedef VOID (STDCALL *PFNGETSTARTUPINFOWPROC)(LPSTARTUPINFOW lpStartupInfo); +typedef HMODULE(STDCALL *PFNLOADLIBRARYAPROC)(LPCSTR); +typedef VOID(STDCALL *PFNFREELIBRARYPROC)(HMODULE); +typedef FARPROC(STDCALL *PFNGETPROCADDRESSPROC)(HMODULE, LPCSTR); +typedef DWORD(STDCALL *PFNGETENVIRONMENTVARIABLEAPROC)(LPCSTR, LPSTR, DWORD); +typedef BOOL(STDCALL *PFNCREATEPROCESSAPROC)(LPCSTR, LPSTR, LPSECURITY_ATTRIBUTES, + LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, LPCSTR, + LPSTARTUPINFOA, LPPROCESS_INFORMATION); +typedef BOOL(STDCALL *PFNISWOW64PROCESSPROC)(HANDLE, PBOOL); +typedef HANDLE(STDCALL *PFNCREATEFILEAPROC)(LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, + DWORD, HANDLE); +typedef HANDLE(STDCALL *PFNCREATEFILEMAPPINGAPROC)(HANDLE, LPSECURITY_ATTRIBUTES, DWORD, DWORD, + DWORD, LPCSTR); +typedef HANDLE(STDCALL *PFNOPENMUTEXAPROC)(DWORD, BOOL, LPCSTR); +typedef HANDLE(STDCALL *PFNCREATEMUTEXAPROC)(LPSECURITY_ATTRIBUTES, BOOL, LPCSTR); +typedef DWORD(STDCALL *PFNGETCURRENTTHREADIDPROC)(VOID); +typedef DWORD(STDCALL *PFNGETCURRENTPROCESSIDPROC)(VOID); +typedef VOID(STDCALL *PFNSLEEPPROC)(DWORD); +typedef DWORD(STDCALL *PFNSLEEPEXPROC)(DWORD, BOOL); +typedef DWORD(STDCALL *PFNWAITFORSINGLEOBJECTPROC)(HANDLE, DWORD); +typedef HANDLE(STDCALL *PFNOPENEVENTAPROC)(DWORD, BOOL, LPCSTR); +typedef HANDLE(STDCALL *PFNCREATEEVENTAPROC)(LPSECURITY_ATTRIBUTES lpEventAttributes, + BOOL bManualReset, BOOL bInitialState, LPCSTR lpName); +typedef BOOL(STDCALL *PFNGETEXITCODEPROCESSPROC)(HANDLE hProcess, LPDWORD lpExitCode); +typedef HLOCAL(STDCALL *PFNLOCALALLOCPROC)(UINT, UINT); +typedef HLOCAL(STDCALL *PFNLOCALFREEPROC)(HLOCAL); +typedef HMODULE(STDCALL *PFNGETMODULEHANDLEAPROC)(LPCSTR); +typedef DWORD(STDCALL *PFNGETMODULEFILENAMEAPROC)(HMODULE, LPCH, DWORD); +typedef DWORD(STDCALL *PFNGETFILEATTRIBUTESAPROC)(LPCSTR); +typedef VOID(STDCALL *PFNEXITPROCESSPROC)(UINT); +typedef DWORD(STDCALL *PFNGETFILESIZEPROC)(HANDLE, LPDWORD); +typedef BOOL(STDCALL *PFNREADFILEPROC)(HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED); +typedef BOOL(STDCALL *PFNWRITEFILEPROC)(HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED); +typedef BOOL(STDCALL *PFNCLOSEHANDLEPROC)(HANDLE); +typedef LPSTR(STDCALL *PFNLSTRCPYAPROC)(LPSTR, LPCSTR); +typedef DWORD(STDCALL *PFNGETLASTERRORPROC)(VOID); +typedef BOOL(STDCALL *PFNSETENVIRONMENTVARIABLEAPROC)(LPCSTR, LPCSTR); +typedef BOOL(STDCALL *PFNVIRTUALPROTECTEXPROC)(HANDLE, LPVOID, SIZE_T, DWORD, PDWORD); +typedef BOOL(STDCALL *PFNREADPROCESSMEMORYPROC)(HANDLE, LPCVOID, LPVOID, SIZE_T, SIZE_T *); +typedef BOOL(STDCALL *PFNWRITEPROCESSMEMORYPROC)(HANDLE, LPVOID, LPCVOID, SIZE_T, SIZE_T *); +typedef BOOL(STDCALL *PFNFLUSHINSTRUCTIONCACHEPROC)(HANDLE, LPCVOID, SIZE_T); +typedef BOOL(STDCALL *PFNVIRTUALPROTECTPROC)(LPVOID, SIZE_T, DWORD, PDWORD); +typedef HANDLE(STDCALL *PFNGETCURRENTPROCESSPROC)(VOID); +typedef HANDLE(STDCALL *PFNCREATETHREADPROC)(LPSECURITY_ATTRIBUTES, SIZE_T, LPTHREAD_START_ROUTINE, + LPVOID, DWORD, LPDWORD); +typedef HANDLE(STDCALL *PFNGETSTDHANDLEPROC)(DWORD); +typedef SIZE_T(STDCALL *PFNVIRTUALQUERYPROC)(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, + SIZE_T dwLength); +typedef VOID(STDCALL *PFNGETSTARTUPINFOAPROC)(LPSTARTUPINFOA lpStartupInfo); +typedef VOID(STDCALL *PFNGETSTARTUPINFOWPROC)(LPSTARTUPINFOW lpStartupInfo); // wininet.dll -typedef void (STDCALL *PFNINTERNETCONNECTAPROC)(HINTERNET, LPCSTR, INTERNET_PORT, LPCSTR, LPCSTR, DWORD, DWORD, DWORD_PTR); -typedef HINTERNET (STDCALL *PFNINTERNETOPENURLAPROC)(HINTERNET, LPCSTR, LPCSTR, DWORD, DWORD, DWORD_PTR); +typedef void(STDCALL *PFNINTERNETCONNECTAPROC)(HINTERNET, LPCSTR, INTERNET_PORT, LPCSTR, LPCSTR, + DWORD, DWORD, DWORD_PTR); +typedef HINTERNET(STDCALL *PFNINTERNETOPENURLAPROC)(HINTERNET, LPCSTR, LPCSTR, DWORD, DWORD, + DWORD_PTR); // winmm.dll -typedef DWORD (STDCALL *PFNTIMEGETTIMEPROC)(); +typedef DWORD(STDCALL *PFNTIMEGETTIMEPROC)(); // ijl15.dll typedef int(STDCALL *PFNIJLGETLIBVERSIONPROC)(); diff --git a/src/common.h b/src/common.h index 3c9596d..91ee95b 100644 --- a/src/common.h +++ b/src/common.h @@ -24,8 +24,8 @@ typedef enum _PANGYAVER { PANGYA_BR, PANGYA_TW, PANGYA_US, - PANGYA_EU, - PANGYA_SEA + PANGYA_EU, + PANGYA_SEA } PANGYAVER; // C standard library functions diff --git a/src/config.h b/src/config.h index 61ac0ee..c2149ab 100644 --- a/src/config.h +++ b/src/config.h @@ -20,9 +20,9 @@ typedef struct _PORTREWRITERULE { } PORTREWRITERULE, *LPPORTREWRITERULE; typedef struct _PATCHADDRESS { - DWORD addr; - LPSTR patch; - DWORD patchLen; + DWORD addr; + LPSTR patch; + DWORD patchLen; } PATCHADDRESS, *LPPATCHADDRESS; typedef struct _RUGBURNCONFIG { @@ -32,8 +32,8 @@ typedef struct _RUGBURNCONFIG { PORTREWRITERULE PortRewriteRules[MAXPORTREWRITES]; int NumPortRewriteRules; - PATCHADDRESS PatchAddress[MAXPATCHADDRESS]; - int NumPatchAddress; + PATCHADDRESS PatchAddress[MAXPATCHADDRESS]; + int NumPatchAddress; } RUGBURNCONFIG, *LPRUGBURNCONFIG; extern RUGBURNCONFIG Config; diff --git a/src/hex.c b/src/hex.c index dc03b76..41a1db8 100644 --- a/src/hex.c +++ b/src/hex.c @@ -99,29 +99,29 @@ void ParsePatch(LPCSTR lpszText, LPSTR *pDataOut, DWORD *pSizeOut) { inPos = lpszText; while (*inPos) { switch (*inPos++) { - case '\\': - switch (*inPos++) { - case 'x': - if (ParseHex(*inPos++, &hexDigitVal) == FALSE) { - FatalError("ParsePatch: Bad hex escape near %s", inPos); - } - if (ParseHex(*inPos++, &hexDigitVal) == FALSE) { - FatalError("ParsePatch: Bad hex escape near %s", inPos); - } - (*pSizeOut)++; - break; - case '\\': - (*pSizeOut)++; - break; - case '\0': - FatalError("ParsePatch: Unexpected end of string in escape code"); - break; - default: - FatalError("ParsePatch: Unknown escape code: %s", inPos); + case '\\': + switch (*inPos++) { + case 'x': + if (ParseHex(*inPos++, &hexDigitVal) == FALSE) { + FatalError("ParsePatch: Bad hex escape near %s", inPos); + } + if (ParseHex(*inPos++, &hexDigitVal) == FALSE) { + FatalError("ParsePatch: Bad hex escape near %s", inPos); } + (*pSizeOut)++; break; - default: + case '\\': (*pSizeOut)++; + break; + case '\0': + FatalError("ParsePatch: Unexpected end of string in escape code"); + break; + default: + FatalError("ParsePatch: Unknown escape code: %s", inPos); + } + break; + default: + (*pSizeOut)++; } } @@ -133,32 +133,32 @@ void ParsePatch(LPCSTR lpszText, LPSTR *pDataOut, DWORD *pSizeOut) { outPos = *pDataOut; while (*inPos) { switch (*inPos) { - case '\\': - inPos++; - switch (*inPos++) { - case 'x': - if (!ParseHex(*inPos++, &hexDigitVal)) { - FatalError("ParsePatch: Bad hex escape near %s", inPos); - } - hexOctetVal = hexDigitVal << 4; - if (!ParseHex(*inPos++, &hexDigitVal)) { - FatalError("ParsePatch: Bad hex escape near %s", inPos); - } - hexOctetVal |= hexDigitVal; - *outPos++ = (CHAR)(BYTE)hexOctetVal; - break; - case '\\': - *outPos++ = '\\'; - break; - case '\0': - FatalError("ParsePatch: Unexpected end of string in escape code"); - break; - default: - FatalError("ParsePatch: Unknown escape code: %s", inPos); + case '\\': + inPos++; + switch (*inPos++) { + case 'x': + if (!ParseHex(*inPos++, &hexDigitVal)) { + FatalError("ParsePatch: Bad hex escape near %s", inPos); } + hexOctetVal = hexDigitVal << 4; + if (!ParseHex(*inPos++, &hexDigitVal)) { + FatalError("ParsePatch: Bad hex escape near %s", inPos); + } + hexOctetVal |= hexDigitVal; + *outPos++ = (CHAR)(BYTE)hexOctetVal; + break; + case '\\': + *outPos++ = '\\'; + break; + case '\0': + FatalError("ParsePatch: Unexpected end of string in escape code"); break; default: - *(outPos++) = *(inPos++); + FatalError("ParsePatch: Unknown escape code: %s", inPos); + } + break; + default: + *(outPos++) = *(inPos++); } } } diff --git a/src/regex.c b/src/regex.c index 979f5ea..c10010e 100644 --- a/src/regex.c +++ b/src/regex.c @@ -5,374 +5,367 @@ #define MAXRECAPTURE 10 typedef enum _RETOKENTYPE { - RE_TOK_NONE, - RE_TOK_LPAREN, - RE_TOK_RPAREN, - RE_TOK_PERIOD, - RE_TOK_ASTERISK, - RE_TOK_PLUS, - RE_TOK_QUESTIONMARK, - RE_TOK_CHAR, - RE_TOK_CHARCLASS, - RE_TOK_INVCHARCLASS, - RE_TOK_DIGIT, - RE_TOK_NONDIGIT, - RE_TOK_ALPHA, - RE_TOK_NONALPHA, - RE_TOK_WHITESPACE, - RE_TOK_NONWHITESPACE, + RE_TOK_NONE, + RE_TOK_LPAREN, + RE_TOK_RPAREN, + RE_TOK_PERIOD, + RE_TOK_ASTERISK, + RE_TOK_PLUS, + RE_TOK_QUESTIONMARK, + RE_TOK_CHAR, + RE_TOK_CHARCLASS, + RE_TOK_INVCHARCLASS, + RE_TOK_DIGIT, + RE_TOK_NONDIGIT, + RE_TOK_ALPHA, + RE_TOK_NONALPHA, + RE_TOK_WHITESPACE, + RE_TOK_NONWHITESPACE, } RETOKENTYPE; typedef struct _RETOKEN { - RETOKENTYPE type; - union { - BYTE ch; - BYTE *charClass; - }; + RETOKENTYPE type; + union { + BYTE ch; + BYTE *charClass; + }; } RETOKEN; typedef struct _RECAPGROUP { - LPCSTR str; - int len; + LPCSTR str; + int len; } RECAPGROUP; struct _REGEX { - RETOKEN tok[MAXRETOKENS]; - BYTE charClass[MAXRECHARCLASS]; - RECAPGROUP cap[MAXRECAPTURE]; - int numCap; + RETOKEN tok[MAXRETOKENS]; + BYTE charClass[MAXRECHARCLASS]; + RECAPGROUP cap[MAXRECAPTURE]; + int numCap; }; static BOOL ReMatchPattern(REGEX *pattern, RETOKEN *tokenptr, LPCSTR text); -int ReGetNumCaptures(REGEX *pattern) { - return pattern->numCap; -} +int ReGetNumCaptures(REGEX *pattern) { return pattern->numCap; } -int ReGetCaptureLen(REGEX *pattern, int i) { - return pattern->cap[i].len; -} +int ReGetCaptureLen(REGEX *pattern, int i) { return pattern->cap[i].len; } void ReGetCaptureData(REGEX *pattern, int i, LPSTR buffer) { - memcpy(buffer, pattern->cap[i].str, pattern->cap[i].len); + memcpy(buffer, pattern->cap[i].str, pattern->cap[i].len); } BOOL ReMatch(REGEX *pattern, LPCSTR text) { - pattern->numCap = 0; - memset(pattern->cap, 0, sizeof(RECAPGROUP) * MAXRECAPTURE); + pattern->numCap = 0; + memset(pattern->cap, 0, sizeof(RECAPGROUP) * MAXRECAPTURE); - return ReMatchPattern(pattern, &pattern->tok[0], text); + return ReMatchPattern(pattern, &pattern->tok[0], text); } LPSTR ReReplace(REGEX *pattern, LPCSTR replace, LPCSTR text) { - LPCSTR ptr; - LPSTR result, ptrout; - int outLen = 0; - - if (!ReMatch(pattern, text)) { - return NULL; - } - - // Preflight - ptr = replace; - outLen = 0; - while (*ptr != 0) { - switch (*ptr++) { - case '$': - if (*ptr >= '0' && *ptr <= '9') { - outLen += pattern->cap[*ptr-'0'].len; - } else if (*ptr == '$') { - outLen++; - } - ptr++; - break; - default: - outLen++; - } - } - outLen++; - - // Substitution - result = AllocMem(outLen); - ptr = replace; - ptrout = result; - while (*ptr != 0) { - switch (*ptr) { - case '$': - ptr++; - if (*ptr >= '0' && *ptr <= '9') { - memcpy(ptrout, pattern->cap[*ptr-'0'].str, pattern->cap[*ptr-'0'].len); - ptrout += pattern->cap[*ptr-'0'].len; - } else if (*ptr == '$') { - *ptrout++ = '$'; - } - ptr++; - break; - default: - *ptrout++ = *ptr++; - } - } - *ptrout = 0; - return result; + LPCSTR ptr; + LPSTR result, ptrout; + int outLen = 0; + + if (!ReMatch(pattern, text)) { + return NULL; + } + + // Preflight + ptr = replace; + outLen = 0; + while (*ptr != 0) { + switch (*ptr++) { + case '$': + if (*ptr >= '0' && *ptr <= '9') { + outLen += pattern->cap[*ptr - '0'].len; + } else if (*ptr == '$') { + outLen++; + } + ptr++; + break; + default: + outLen++; + } + } + outLen++; + + // Substitution + result = AllocMem(outLen); + ptr = replace; + ptrout = result; + while (*ptr != 0) { + switch (*ptr) { + case '$': + ptr++; + if (*ptr >= '0' && *ptr <= '9') { + memcpy(ptrout, pattern->cap[*ptr - '0'].str, pattern->cap[*ptr - '0'].len); + ptrout += pattern->cap[*ptr - '0'].len; + } else if (*ptr == '$') { + *ptrout++ = '$'; + } + ptr++; + break; + default: + *ptrout++ = *ptr++; + } + } + *ptrout = 0; + return result; } REGEX *ReParse(LPCSTR pattern) { - REGEX *compiled = (REGEX *)AllocMem(sizeof(REGEX)); - int charClassIndex = 1, charClassBegin = 0, i = 0, j = 0; - CHAR ch; - - ZeroMemory(compiled, sizeof(REGEX)); - - while (pattern[i] != 0) { - if (j+1 >= MAXRETOKENS) { - FatalError("Parsing regular expression: reached max number of tokens."); - } - ch = pattern[i]; - switch (ch) { - case '(': - compiled->tok[j].type = RE_TOK_LPAREN; - break; - case ')': - compiled->tok[j].type = RE_TOK_RPAREN; - break; - case '.': - compiled->tok[j].type = RE_TOK_PERIOD; - break; - case '*': - compiled->tok[j].type = RE_TOK_ASTERISK; - break; - case '+': - compiled->tok[j].type = RE_TOK_PLUS; - break; - case '?': - compiled->tok[j].type = RE_TOK_QUESTIONMARK; - break; - case '\\': - if (pattern[i+1] != 0) { - i++; - switch (pattern[i]) { - case 'd': - compiled->tok[j].type = RE_TOK_DIGIT; - break; - case 'D': - compiled->tok[j].type = RE_TOK_NONDIGIT; - break; - case 'w': - compiled->tok[j].type = RE_TOK_ALPHA; - break; - case 'W': - compiled->tok[j].type = RE_TOK_NONALPHA; - break; - case 's': - compiled->tok[j].type = RE_TOK_WHITESPACE; - break; - case 'S': - compiled->tok[j].type = RE_TOK_NONWHITESPACE; - break; - default: - compiled->tok[j].type = RE_TOK_CHAR; - compiled->tok[j].ch = pattern[i]; - break; - } - } - break; - case '[': - charClassBegin = charClassIndex; - compiled->tok[j].type = RE_TOK_CHARCLASS; - - if (pattern[++i] == '^') { - compiled->tok[j].type = RE_TOK_INVCHARCLASS; - i++; - } - - while (pattern[i] != ']' && pattern[i] != 0) { - if (pattern[i] == '\\') { - if (charClassIndex >= MAXRECHARCLASS - 1) { - FatalError("Parsing regular expression: reached max character class storage."); - } - compiled->charClass[charClassIndex++] = pattern[i++]; - } else if (charClassIndex >= MAXRECHARCLASS) { - FatalError("Parsing regular expression: reached max character class storage."); - } - compiled->charClass[charClassIndex++] = pattern[i]; - i++; - } - if (charClassIndex >= MAXRECHARCLASS) { - FatalError("Parsing regular expression: reached max character class storage."); - } - compiled->charClass[charClassIndex++] = 0; - compiled->tok[j].charClass = &compiled->charClass[charClassBegin]; - break; - - default: - compiled->tok[j].type = RE_TOK_CHAR; - compiled->tok[j].ch = ch; - break; - } - i++; - j++; - } - compiled->tok[j].type = RE_TOK_NONE; - return compiled; + REGEX *compiled = (REGEX *)AllocMem(sizeof(REGEX)); + int charClassIndex = 1, charClassBegin = 0, i = 0, j = 0; + CHAR ch; + + ZeroMemory(compiled, sizeof(REGEX)); + + while (pattern[i] != 0) { + if (j + 1 >= MAXRETOKENS) { + FatalError("Parsing regular expression: reached max number of tokens."); + } + ch = pattern[i]; + switch (ch) { + case '(': + compiled->tok[j].type = RE_TOK_LPAREN; + break; + case ')': + compiled->tok[j].type = RE_TOK_RPAREN; + break; + case '.': + compiled->tok[j].type = RE_TOK_PERIOD; + break; + case '*': + compiled->tok[j].type = RE_TOK_ASTERISK; + break; + case '+': + compiled->tok[j].type = RE_TOK_PLUS; + break; + case '?': + compiled->tok[j].type = RE_TOK_QUESTIONMARK; + break; + case '\\': + if (pattern[i + 1] != 0) { + i++; + switch (pattern[i]) { + case 'd': + compiled->tok[j].type = RE_TOK_DIGIT; + break; + case 'D': + compiled->tok[j].type = RE_TOK_NONDIGIT; + break; + case 'w': + compiled->tok[j].type = RE_TOK_ALPHA; + break; + case 'W': + compiled->tok[j].type = RE_TOK_NONALPHA; + break; + case 's': + compiled->tok[j].type = RE_TOK_WHITESPACE; + break; + case 'S': + compiled->tok[j].type = RE_TOK_NONWHITESPACE; + break; + default: + compiled->tok[j].type = RE_TOK_CHAR; + compiled->tok[j].ch = pattern[i]; + break; + } + } + break; + case '[': + charClassBegin = charClassIndex; + compiled->tok[j].type = RE_TOK_CHARCLASS; + + if (pattern[++i] == '^') { + compiled->tok[j].type = RE_TOK_INVCHARCLASS; + i++; + } + + while (pattern[i] != ']' && pattern[i] != 0) { + if (pattern[i] == '\\') { + if (charClassIndex >= MAXRECHARCLASS - 1) { + FatalError( + "Parsing regular expression: reached max character class storage."); + } + compiled->charClass[charClassIndex++] = pattern[i++]; + } else if (charClassIndex >= MAXRECHARCLASS) { + FatalError("Parsing regular expression: reached max character class storage."); + } + compiled->charClass[charClassIndex++] = pattern[i]; + i++; + } + if (charClassIndex >= MAXRECHARCLASS) { + FatalError("Parsing regular expression: reached max character class storage."); + } + compiled->charClass[charClassIndex++] = 0; + compiled->tok[j].charClass = &compiled->charClass[charClassBegin]; + break; + + default: + compiled->tok[j].type = RE_TOK_CHAR; + compiled->tok[j].ch = ch; + break; + } + i++; + j++; + } + compiled->tok[j].type = RE_TOK_NONE; + return compiled; } -static BOOL ReMatchDigit(CHAR ch) { - return ch >= '0' && ch <= '9'; -} +static BOOL ReMatchDigit(CHAR ch) { return ch >= '0' && ch <= '9'; } -static BOOL ReMatchAlpha(CHAR ch) { - return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); -} +static BOOL ReMatchAlpha(CHAR ch) { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); } -static BOOL ReMatchAlphaNum(CHAR ch) { - return ch == '_' || ReMatchAlpha(ch) || ReMatchDigit(ch); -} +static BOOL ReMatchAlphaNum(CHAR ch) { return ch == '_' || ReMatchAlpha(ch) || ReMatchDigit(ch); } static BOOL ReMatchWhitespace(CHAR ch) { - return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\f' || ch == '\v'; + return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\f' || ch == '\v'; } static BOOL ReMatchRange(CHAR ch, LPCSTR str) { - return ch != '-' && str[0] != 0 && str[0] != '-' && str[1] == '-' && str[1] != 0 && str[2] != 0 && (ch >= str[0] && ch <= str[2]); + return ch != '-' && str[0] != 0 && str[0] != '-' && str[1] == '-' && str[1] != 0 && + str[2] != 0 && (ch >= str[0] && ch <= str[2]); } static BOOL ReIsMetaCh(CHAR ch) { - return ch == 's' || ch == 'S' || ch == 'd' || ch == 'D' || ch == 'w' || ch == 'W'; + return ch == 's' || ch == 'S' || ch == 'd' || ch == 'D' || ch == 'w' || ch == 'W'; } static BOOL ReMatchMetaChar(CHAR ch, LPCSTR str) { - switch (str[0]) { - case 'd': - return ReMatchDigit(ch); - case 'D': - return !ReMatchDigit(ch); - case 'w': - return ReMatchAlphaNum(ch); - case 'W': - return !ReMatchAlphaNum(ch); - case 's': - return ReMatchWhitespace(ch); - case 'S': - return !ReMatchWhitespace(ch); - default: - return ch == str[0]; - } + switch (str[0]) { + case 'd': + return ReMatchDigit(ch); + case 'D': + return !ReMatchDigit(ch); + case 'w': + return ReMatchAlphaNum(ch); + case 'W': + return !ReMatchAlphaNum(ch); + case 's': + return ReMatchWhitespace(ch); + case 'S': + return !ReMatchWhitespace(ch); + default: + return ch == str[0]; + } } static BOOL ReMatchCharClass(CHAR ch, LPCSTR str) { - do { - if (ReMatchRange(ch, str)) { - return TRUE; - } else if (str[0] == '\\') { - str++; - if (ReMatchMetaChar(ch, str)) { - return TRUE; - } else if (ch == str[0] && !ReIsMetaCh(ch)) { - return TRUE; - } - } else if (ch == str[0]) { - if (ch == '-') { - return str[-1] == 0 || str[1] == 0; - } else { - return TRUE; - } - } - } while (*str++ != 0); - return FALSE; + do { + if (ReMatchRange(ch, str)) { + return TRUE; + } else if (str[0] == '\\') { + str++; + if (ReMatchMetaChar(ch, str)) { + return TRUE; + } else if (ch == str[0] && !ReIsMetaCh(ch)) { + return TRUE; + } + } else if (ch == str[0]) { + if (ch == '-') { + return str[-1] == 0 || str[1] == 0; + } else { + return TRUE; + } + } + } while (*str++ != 0); + return FALSE; } static BOOL ReMatchOne(RETOKEN p, CHAR ch) { - switch (p.type) { - case RE_TOK_PERIOD: - return TRUE; - case RE_TOK_CHARCLASS: - return ReMatchCharClass(ch, (LPCSTR)p.charClass); - case RE_TOK_INVCHARCLASS: - return !ReMatchCharClass(ch, (LPCSTR)p.charClass); - case RE_TOK_DIGIT: - return ReMatchDigit(ch); - case RE_TOK_NONDIGIT: - return !ReMatchDigit(ch); - case RE_TOK_ALPHA: - return ReMatchAlphaNum(ch); - case RE_TOK_NONALPHA: - return !ReMatchAlphaNum(ch); - case RE_TOK_WHITESPACE: - return ReMatchWhitespace(ch); - case RE_TOK_NONWHITESPACE: - return !ReMatchWhitespace(ch); - default: - return p.ch == ch; - } + switch (p.type) { + case RE_TOK_PERIOD: + return TRUE; + case RE_TOK_CHARCLASS: + return ReMatchCharClass(ch, (LPCSTR)p.charClass); + case RE_TOK_INVCHARCLASS: + return !ReMatchCharClass(ch, (LPCSTR)p.charClass); + case RE_TOK_DIGIT: + return ReMatchDigit(ch); + case RE_TOK_NONDIGIT: + return !ReMatchDigit(ch); + case RE_TOK_ALPHA: + return ReMatchAlphaNum(ch); + case RE_TOK_NONALPHA: + return !ReMatchAlphaNum(ch); + case RE_TOK_WHITESPACE: + return ReMatchWhitespace(ch); + case RE_TOK_NONWHITESPACE: + return !ReMatchWhitespace(ch); + default: + return p.ch == ch; + } } static BOOL ReMatchZeroOrMore(REGEX *pattern, RETOKEN p, RETOKEN *tokenptr, LPCSTR text) { - LPCSTR start = text; - while (text[0] != 0 && ReMatchOne(p, *text)) { - text++; - } - while (text >= start) { - if (ReMatchPattern(pattern, tokenptr, text--)) { - return TRUE; - } - } - return FALSE; + LPCSTR start = text; + while (text[0] != 0 && ReMatchOne(p, *text)) { + text++; + } + while (text >= start) { + if (ReMatchPattern(pattern, tokenptr, text--)) { + return TRUE; + } + } + return FALSE; } static BOOL ReMatchOneOrMore(REGEX *pattern, RETOKEN p, RETOKEN *tokenptr, LPCSTR text) { - LPCSTR start = text; - while (text[0] != 0 && ReMatchOne(p, *text)) { - text++; - } - while (text > start) { - if (ReMatchPattern(pattern, tokenptr, text--)) { - return TRUE; - } - } - return FALSE; + LPCSTR start = text; + while (text[0] != 0 && ReMatchOne(p, *text)) { + text++; + } + while (text > start) { + if (ReMatchPattern(pattern, tokenptr, text--)) { + return TRUE; + } + } + return FALSE; } static BOOL ReMatchOptional(REGEX *pattern, RETOKEN p, RETOKEN *tokenptr, LPCSTR text) { - if (p.type == RE_TOK_NONE) { - return TRUE; - } - if (ReMatchPattern(pattern, tokenptr, text)) { - return TRUE; - } - if (*text && ReMatchOne(p, *text++)) { - if (ReMatchPattern(pattern, tokenptr, text)) { - return TRUE; - } - } - return FALSE; + if (p.type == RE_TOK_NONE) { + return TRUE; + } + if (ReMatchPattern(pattern, tokenptr, text)) { + return TRUE; + } + if (*text && ReMatchOne(p, *text++)) { + if (ReMatchPattern(pattern, tokenptr, text)) { + return TRUE; + } + } + return FALSE; } static BOOL ReMatchPattern(REGEX *pattern, RETOKEN *tokenptr, LPCSTR text) { - do { - // End capture group. - if (pattern->numCap > 0 && pattern->cap[pattern->numCap - 1].str && tokenptr[0].type == RE_TOK_RPAREN) { - pattern->cap[pattern->numCap - 1].len = text - pattern->cap[pattern->numCap - 1].str; - tokenptr++; - } - - // Start capture group. - if (tokenptr[0].type == RE_TOK_LPAREN) { - if (pattern->numCap == MAXRECAPTURE) { - FatalError("Matching regular expression: reached max capture groups."); - } - pattern->cap[pattern->numCap++].str = text; - tokenptr++; - } - - if (tokenptr[1].type == RE_TOK_QUESTIONMARK) { - return ReMatchOptional(pattern, tokenptr[0], &tokenptr[2], text); - } else if (tokenptr[1].type == RE_TOK_ASTERISK) { - return ReMatchZeroOrMore(pattern, tokenptr[0], &tokenptr[2], text); - } else if (tokenptr[1].type == RE_TOK_PLUS) { - return ReMatchOneOrMore(pattern, tokenptr[0], &tokenptr[2], text); - } else if (tokenptr[0].type == RE_TOK_NONE) { - return text[0] == 0; - } - } while (text[0] != 0 && ReMatchOne(*tokenptr++, *text++)); - return FALSE; + do { + // End capture group. + if (pattern->numCap > 0 && pattern->cap[pattern->numCap - 1].str && + tokenptr[0].type == RE_TOK_RPAREN) { + pattern->cap[pattern->numCap - 1].len = text - pattern->cap[pattern->numCap - 1].str; + tokenptr++; + } + + // Start capture group. + if (tokenptr[0].type == RE_TOK_LPAREN) { + if (pattern->numCap == MAXRECAPTURE) { + FatalError("Matching regular expression: reached max capture groups."); + } + pattern->cap[pattern->numCap++].str = text; + tokenptr++; + } + + if (tokenptr[1].type == RE_TOK_QUESTIONMARK) { + return ReMatchOptional(pattern, tokenptr[0], &tokenptr[2], text); + } else if (tokenptr[1].type == RE_TOK_ASTERISK) { + return ReMatchZeroOrMore(pattern, tokenptr[0], &tokenptr[2], text); + } else if (tokenptr[1].type == RE_TOK_PLUS) { + return ReMatchOneOrMore(pattern, tokenptr[0], &tokenptr[2], text); + } else if (tokenptr[0].type == RE_TOK_NONE) { + return text[0] == 0; + } + } while (text[0] != 0 && ReMatchOne(*tokenptr++, *text++)); + return FALSE; }