From a6131ef9ce985dcc448e73fadf5d220c563b9560 Mon Sep 17 00:00:00 2001 From: Xpl0itR Date: Tue, 17 Jan 2023 08:20:25 +0000 Subject: [PATCH] consolidate --- VersionShim/VersionShim.vcxproj | 4 +-- VersionShim/dllmain.c | 41 ++++++++++--------------- VersionShim/pch.h | 8 ++--- VersionShim/util.c | 53 +++++++++++++-------------------- VersionShim/util.h | 10 +++---- 5 files changed, 45 insertions(+), 71 deletions(-) diff --git a/VersionShim/VersionShim.vcxproj b/VersionShim/VersionShim.vcxproj index 57d34b2..3648205 100644 --- a/VersionShim/VersionShim.vcxproj +++ b/VersionShim/VersionShim.vcxproj @@ -43,7 +43,7 @@ Level3 true - _DEBUG;VERSIONSHIM_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + _DEBUG;VERSIONSHIM_EXPORTS;_WINDOWS;_USRDLL;PROJECT_NAME="$(ProjectName)";%(PreprocessorDefinitions) true Create pch.h @@ -59,7 +59,7 @@ true true true - NDEBUG;VERSIONSHIM_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + NDEBUG;VERSIONSHIM_EXPORTS;_WINDOWS;_USRDLL;PROJECT_NAME="$(ProjectName)";%(PreprocessorDefinitions) true Create pch.h diff --git a/VersionShim/dllmain.c b/VersionShim/dllmain.c index aa79719..178a405 100644 --- a/VersionShim/dllmain.c +++ b/VersionShim/dllmain.c @@ -32,35 +32,26 @@ BOOL APIENTRY DllMain(const HMODULE hModule, const DWORD fdwReason, LPCVOID lpRe if (fdwReason != DLL_PROCESS_ATTACH) return TRUE; - HANDLE hFile = NULL; - if (!OpenRead("Libraries.txt", &hFile)) - return FALSE; - - LPCH fileBuf = NULL; + LPCH fileStr = NULL; DWORD fileLen = 0; - BOOL success = ReadUtf8File(hFile, &fileBuf, &fileLen); - CloseHandle(hFile); - if (!success) - return Error(L"Failed to read Libraries.txt"); - if (fileLen > INT_MAX) - return Error(L"Libraries.txt was too large to read"); - LPWSTR fileStr = NULL; - success = Utf8ToUtf16(fileBuf, &fileStr, (int)fileLen); - free(fileBuf); - if (!success) - return Error(L"Failed to read Libraries.txt as UTF-16"); - - LPWSTR end = fileStr + fileLen; - for (LPWSTR dllPath = fileStr; dllPath < end;) + if (!OpenReadFileUtf8("Libraries.txt", &fileStr, &fileLen)) { - int numCharInVal = NextLine(dllPath); - - LoadLibrary(dllPath); - - dllPath += numCharInVal + 2; + MessageBoxA(NULL, "Failed to read Libraries.txt", PROJECT_NAME, ErrBoxType); + return TRUE; } - free(fileStr); + int charsInLine; + for (LPCH line = fileStr, fileEnd = fileStr + fileLen; line < fileEnd; line += charsInLine + 2) + { + charsInLine = TerminateLineCrlf(line); + + if (!LoadLibraryA(line)) + { + MessageBoxA(NULL, line, PROJECT_NAME" - Failed to load library", ErrBoxType); + } + } + + HeapFree(GetProcessHeap(), 0, fileStr); return TRUE; } \ No newline at end of file diff --git a/VersionShim/pch.h b/VersionShim/pch.h index 8f68251..5aa60e2 100644 --- a/VersionShim/pch.h +++ b/VersionShim/pch.h @@ -4,11 +4,7 @@ // However, files listed here are ALL re-compiled if any one of them is updated between builds. // Do not add files here that you will be updating frequently as this negates the performance advantage. -#ifndef PCH_H -#define PCH_H +#pragma once #define WIN32_LEAN_AND_MEAN -#include -#include - -#endif //PCH_H \ No newline at end of file +#include \ No newline at end of file diff --git a/VersionShim/util.c b/VersionShim/util.c index 4fcbc75..c0bbe93 100644 --- a/VersionShim/util.c +++ b/VersionShim/util.c @@ -1,4 +1,4 @@ -// Copyright © 2023 Xpl0itR +// Copyright © 2023 Xpl0itR // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -6,71 +6,60 @@ #include "pch.h" -BOOL Error(const LPWSTR message) -{ - MessageBox(NULL, message, L"VersionShim", MB_ICONHAND | MB_OK); - return FALSE; -} - -BOOL OpenRead(LPCSTR lpFileName, HANDLE* fileHandle) +BOOL OpenFileRead(const LPCSTR lpFileName, HANDLE* fileHandle) { OFSTRUCT ofStruct = { 0 }; HFILE hFile = OpenFile(lpFileName, &ofStruct, OF_READ | OF_PROMPT); - *fileHandle = (HANDLE)hFile; + *fileHandle = (HANDLE)(INT_PTR)hFile; return hFile != HFILE_ERROR; } -BOOL ReadUtf8File(const HANDLE fileHandle, LPCH* fileBuffer, DWORD* fileLength) +BOOL ReadFileUtf8(const HANDLE fileHandle, LPCH* fileString, DWORD* fileLength) { - const DWORD fileLen = GetFileSize(fileHandle, NULL); + DWORD fileLen = GetFileSize(fileHandle, NULL); *fileLength = fileLen; - LPCH buffer = malloc(fileLen + 1); + HANDLE heap = GetProcessHeap(); + LPCH buffer = HeapAlloc(heap, 0, fileLen + 1); if (!buffer) return FALSE; if (!ReadFile(fileHandle, buffer, fileLen, NULL, NULL)) { - free(buffer); + HeapFree(heap, 0, buffer); return FALSE; } buffer[fileLen] = '\0'; - *fileBuffer = buffer; + *fileString = buffer; return TRUE; } -BOOL Utf8ToUtf16(LPCCH utf8String, const PZPWSTR utf16String, const int length) +BOOL OpenReadFileUtf8(const LPCSTR lpFileName, LPCH* fileString, DWORD* fileLength) { - LPWSTR string = malloc(length * 2 + 2); - if (!string) + HANDLE hFile = NULL; + if (!OpenFileRead(lpFileName, &hFile)) return FALSE; - if (!MultiByteToWideChar(CP_UTF8, 0, utf8String, length, string, length)) - { - free(string); - return FALSE; - } + BOOL success = ReadFileUtf8(hFile, fileString, fileLength); + CloseHandle(hFile); - string[length] = L'\0'; - *utf16String = string; - - return TRUE; + return success; } -int NextLine(const LPWSTR text) +INT TerminateLineCrlf(const LPCH fileString) { - for (int i = 0; ; i++) + for (INT i = 0; ; i++) { - if (text[i] == L'\r' && - text[i + 1] == L'\n') + if (fileString[i] == '\r' && + fileString[i + 1] == '\n') { - text[i] = text[i + 1] = L'\0'; + fileString[i] = fileString[i + 1] = '\0'; } - if (text[i] == L'\0') + if (fileString[i] == '\0') { return i; } diff --git a/VersionShim/util.h b/VersionShim/util.h index 61839b5..d4ec6dd 100644 --- a/VersionShim/util.h +++ b/VersionShim/util.h @@ -1,8 +1,6 @@ #pragma once -#include "pch.h" -BOOL Error(LPWSTR message); -BOOL OpenRead(LPCSTR lpFileName, HANDLE* fileHandle); -BOOL ReadUtf8File(HANDLE fileHandle, LPCH* fileBuffer, DWORD* fileLength); -BOOL Utf8ToUtf16(LPCCH utf8String, PZPWSTR utf16String, int length); -int NextLine(LPWSTR text); \ No newline at end of file +UINT ErrBoxType = MB_ICONERROR | MB_TOPMOST; + +BOOL OpenReadFileUtf8(LPCSTR lpFileName, LPCH* fileBuffer, DWORD* fileLength); +INT TerminateLineCrlf(LPCH fileString); \ No newline at end of file