From de7840af70504a4b0ba53fd40ad8b416ca11b5d8 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Sun, 21 Sep 2014 14:07:49 +0200 Subject: [PATCH] Removed trailing whitespaces. --- MemoryModule.c | 48 ++++++++++++++++----------------- doc/readme.txt | 34 +++++++++++------------ example/DllLoader/DllLoader.cpp | 18 ++++++------- example/DllLoader/Makefile | 2 +- example/Makefile | 2 +- 5 files changed, 52 insertions(+), 52 deletions(-) diff --git a/MemoryModule.c b/MemoryModule.c index 0bd2ee1..0e89a7f 100644 --- a/MemoryModule.c +++ b/MemoryModule.c @@ -140,7 +140,7 @@ FinalizeSections(PMEMORYMODULE module) #else #define imageOffset 0 #endif - + // loop through all sections and change access flags for (i=0; iheaders->FileHeader.NumberOfSections; i++, section++) { DWORD protect, oldProtect, size; @@ -185,10 +185,10 @@ FinalizeSections(PMEMORYMODULE module) } static void -ExecuteTLS(PMEMORYMODULE module) +ExecuteTLS(PMEMORYMODULE module) { unsigned char *codeBase = module->codeBase; - + PIMAGE_DATA_DIRECTORY directory = GET_HEADER_DICTIONARY(module, IMAGE_DIRECTORY_ENTRY_TLS); if (directory->VirtualAddress > 0) { PIMAGE_TLS_DIRECTORY tls = (PIMAGE_TLS_DIRECTORY) (codeBase + directory->VirtualAddress); @@ -225,7 +225,7 @@ PerformBaseRelocation(PMEMORYMODULE module, SIZE_T delta) type = *relInfo >> 12; // the lower 12 bits define the offset offset = *relInfo & 0xfff; - + switch (type) { case IMAGE_REL_BASED_ABSOLUTE: @@ -237,7 +237,7 @@ PerformBaseRelocation(PMEMORYMODULE module, SIZE_T delta) patchAddrHL = (DWORD *) (dest + offset); *patchAddrHL += (DWORD) delta; break; - + #ifdef _WIN64 case IMAGE_REL_BASED_DIR64: patchAddr64 = (ULONGLONG *) (dest + offset); @@ -325,7 +325,7 @@ static HCUSTOMMODULE _LoadLibrary(LPCSTR filename, void *userdata) if (result == NULL) { return NULL; } - + return (HCUSTOMMODULE) result; } @@ -389,7 +389,7 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, return NULL; } } - + result = (PMEMORYMODULE)HeapAlloc(GetProcessHeap(), 0, sizeof(MEMORYMODULE)); if (result == NULL) { SetLastError(ERROR_OUTOFMEMORY); @@ -411,7 +411,7 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, old_header->OptionalHeader.SizeOfHeaders, MEM_COMMIT, PAGE_READWRITE); - + // copy PE header to code memcpy(headers, dos_header, old_header->OptionalHeader.SizeOfHeaders); result->headers = (PIMAGE_NT_HEADERS)&((const unsigned char *)(headers))[dos_header->e_lfanew]; @@ -557,7 +557,7 @@ static PIMAGE_RESOURCE_DIRECTORY_ENTRY _MemorySearchResourceEntry( DWORD start; DWORD end; DWORD middle; - + if (!IS_INTRESOURCE(key) && key[0] == TEXT('#')) { // special case: resource id given as string TCHAR *endpos = NULL; @@ -570,7 +570,7 @@ static PIMAGE_RESOURCE_DIRECTORY_ENTRY _MemorySearchResourceEntry( key = MAKEINTRESOURCE(tmpkey); } } - + // entries are stored as ordered list of named entries, // followed by an ordered list of id entries - we can do // a binary search to find faster... @@ -578,7 +578,7 @@ static PIMAGE_RESOURCE_DIRECTORY_ENTRY _MemorySearchResourceEntry( WORD check = (WORD) (POINTER_TYPE) key; start = resources->NumberOfNamedEntries; end = start + resources->NumberOfIdEntries; - + while (end > start) { WORD entryName; middle = (start + end) >> 1; @@ -611,7 +611,7 @@ static PIMAGE_RESOURCE_DIRECTORY_ENTRY _MemorySearchResourceEntry( if (tmp == NULL) { break; } - + searchKey = (char *) tmp; } wcstombs(searchKey, resourceString->NameString, resourceString->Length); @@ -632,8 +632,8 @@ static PIMAGE_RESOURCE_DIRECTORY_ENTRY _MemorySearchResourceEntry( free(searchKey); #endif } - - + + return result; } @@ -652,7 +652,7 @@ HMEMORYRSRC MemoryFindResourceEx(HMEMORYMODULE module, LPCTSTR name, LPCTSTR typ SetLastError(ERROR_RESOURCE_DATA_NOT_FOUND); return NULL; } - + if (language == DEFAULT_LANGUAGE) { // use language from current thread language = LANGIDFROMLCID(GetThreadLocale()); @@ -668,14 +668,14 @@ HMEMORYRSRC MemoryFindResourceEx(HMEMORYMODULE module, LPCTSTR name, LPCTSTR typ SetLastError(ERROR_RESOURCE_TYPE_NOT_FOUND); return NULL; } - + typeResources = (PIMAGE_RESOURCE_DIRECTORY) (codeBase + directory->VirtualAddress + (foundType->OffsetToData & 0x7fffffff)); foundName = _MemorySearchResourceEntry(rootResources, typeResources, name); if (foundName == NULL) { SetLastError(ERROR_RESOURCE_NAME_NOT_FOUND); return NULL; } - + nameResources = (PIMAGE_RESOURCE_DIRECTORY) (codeBase + directory->VirtualAddress + (foundName->OffsetToData & 0x7fffffff)); foundLanguage = _MemorySearchResourceEntry(rootResources, nameResources, (LPCTSTR) (POINTER_TYPE) language); if (foundLanguage == NULL) { @@ -684,17 +684,17 @@ HMEMORYRSRC MemoryFindResourceEx(HMEMORYMODULE module, LPCTSTR name, LPCTSTR typ SetLastError(ERROR_RESOURCE_LANG_NOT_FOUND); return NULL; } - + foundLanguage = (PIMAGE_RESOURCE_DIRECTORY_ENTRY) (nameResources + 1); } - + return (codeBase + directory->VirtualAddress + (foundLanguage->OffsetToData & 0x7fffffff)); } DWORD MemorySizeofResource(HMEMORYMODULE module, HMEMORYRSRC resource) { PIMAGE_RESOURCE_DATA_ENTRY entry = (PIMAGE_RESOURCE_DATA_ENTRY) resource; - + return entry->Size; } @@ -702,7 +702,7 @@ LPVOID MemoryLoadResource(HMEMORYMODULE module, HMEMORYRSRC resource) { unsigned char *codeBase = ((PMEMORYMODULE) module)->codeBase; PIMAGE_RESOURCE_DATA_ENTRY entry = (PIMAGE_RESOURCE_DATA_ENTRY) resource; - + return codeBase + entry->OffsetToData; } @@ -721,13 +721,13 @@ MemoryLoadStringEx(HMEMORYMODULE module, UINT id, LPTSTR buffer, int maxsize, WO if (maxsize == 0) { return 0; } - + resource = MemoryFindResourceEx(module, MAKEINTRESOURCE((id >> 4) + 1), RT_STRING, language); if (resource == NULL) { buffer[0] = 0; return 0; } - + data = MemoryLoadResource(module, resource); id = id & 0x0f; while (id--) { @@ -738,7 +738,7 @@ MemoryLoadStringEx(HMEMORYMODULE module, UINT id, LPTSTR buffer, int maxsize, WO buffer[0] = 0; return 0; } - + size = data->Length; if (size >= (DWORD) maxsize) { size = maxsize; diff --git a/doc/readme.txt b/doc/readme.txt index e528b7a..3d90304 100644 --- a/doc/readme.txt +++ b/doc/readme.txt @@ -60,7 +60,7 @@ stub that normally just displays an error message about the program not being able to be run from DOS mode. Microsoft defines the DOS header as follows:: - + typedef struct _IMAGE_DOS_HEADER { // DOS .EXE header WORD e_magic; // Magic number WORD e_cblp; // Bytes on last page of file @@ -114,14 +114,14 @@ about symbols, etc:: .. _OptionalHeader: -The `OptionalHeader` contains informations about the *logical* format of the library, +The `OptionalHeader` contains informations about the *logical* format of the library, including required OS version, memory requirements and entry points:: typedef struct _IMAGE_OPTIONAL_HEADER { // // Standard fields. // - + WORD Magic; BYTE MajorLinkerVersion; BYTE MinorLinkerVersion; @@ -131,11 +131,11 @@ including required OS version, memory requirements and entry points:: DWORD AddressOfEntryPoint; DWORD BaseOfCode; DWORD BaseOfData; - + // // NT additional fields. // - + DWORD ImageBase; DWORD SectionAlignment; DWORD FileAlignment; @@ -247,25 +247,25 @@ When issuing the API call `LoadLibrary`, Windows basically performs these tasks: 2. Try to allocate a memory block of `PEHeader.OptionalHeader.SizeOfImage` bytes at position `PEHeader.OptionalHeader.ImageBase`. - + 3. Parse section headers and copy sections to their addresses. The destination address for each section, relative to the base of the allocated memory block, is stored in the `VirtualAddress` attribute of the `IMAGE_SECTION_HEADER` structure. - + 4. If the allocated memory block differs from `ImageBase`, various references in the code and/or data sections must be adjusted. This is called *Base relocation*. - + 5. The required imports for the library must be resolved by loading the corresponding libraries. - + 6. The memory regions of the different sections must be protected depending on the section's characteristics. Some sections are marked as *discardable* and therefore can be safely freed at this point. These sections normally contain temporary data that is only needed during the import, like the informations for the base relocation. - + 7. Now the library is loaded completely. It must be notified about this by calling the entry point using the flag `DLL_PROCESS_ATTACH`. @@ -363,7 +363,7 @@ as follows:: // -1 if bound, and real date\time stamp // in IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT (new BIND) // O.W. date/time stamp of DLL bound to (Old BIND) - + DWORD ForwarderChain; // -1 if no forwarders DWORD Name; DWORD FirstThunk; // RVA to IAT (if bound this IAT has actual addresses) @@ -401,10 +401,10 @@ These flags can be one or a combination of IMAGE_SCN_MEM_EXECUTE The section contains data that can be executed. - + IMAGE_SCN_MEM_READ The section contains data that is readable. - + IMAGE_SCN_MEM_WRITE The section contains data that is writeable. @@ -428,7 +428,7 @@ In addition the section flags above, the following can be added: IMAGE_SCN_MEM_DISCARDABLE The data in this section can be freed after the import. Usually this is specified for relocation data. - + IMAGE_SCN_MEM_NOT_CACHED The data in this section must not get cached by Windows. Add the bit flag `PAGE_NOCACHE` to the protection flags above. @@ -444,7 +444,7 @@ to a process. The function at the entry point is defined as :: - + typedef BOOL (WINAPI *DllEntryProc)(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved); So the last code we need to execute is @@ -497,7 +497,7 @@ To free the custom loaded library, perform the steps DllEntryProc entry = (DllEntryProc)(baseAddress + PEHeader->OptionalHeader.AddressOfEntryPoint); (*entry)((HINSTANCE)baseAddress, DLL_PROCESS_ATTACH, 0); - + - Free external libraries used to resolve imports. - Free allocated memory. @@ -510,7 +510,7 @@ MemoryModule is a C-library that can be used to load a DLL from memory. The interface is very similar to the standard methods for loading of libraries:: typedef void *HMEMORYMODULE; - + HMEMORYMODULE MemoryLoadLibrary(const void *); FARPROC MemoryGetProcAddress(HMEMORYMODULE, const char *); void MemoryFreeLibrary(HMEMORYMODULE); diff --git a/example/DllLoader/DllLoader.cpp b/example/DllLoader/DllLoader.cpp index 1afe8a0..3fda631 100644 --- a/example/DllLoader/DllLoader.cpp +++ b/example/DllLoader/DllLoader.cpp @@ -18,24 +18,24 @@ void LoadFromFile(void) DWORD resourceSize; LPVOID resourceData; TCHAR buffer[100]; - + HINSTANCE handle = LoadLibrary(DLL_FILE); if (handle == NULL) return; addNumber = (addNumberProc)GetProcAddress(handle, "addNumbers"); _tprintf(_T("From file: %d\n"), addNumber(1, 2)); - + resourceInfo = FindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION); _tprintf(_T("FindResource returned 0x%p\n"), resourceInfo); - + resourceSize = SizeofResource(handle, resourceInfo); resourceData = LoadResource(handle, resourceInfo); _tprintf(_T("Resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData); - + LoadString(handle, 1, buffer, sizeof(buffer)); _tprintf(_T("String1: %s\n"), buffer); - + LoadString(handle, 20, buffer, sizeof(buffer)); _tprintf(_T("String2: %s\n"), buffer); @@ -53,7 +53,7 @@ void LoadFromMemory(void) DWORD resourceSize; LPVOID resourceData; TCHAR buffer[100]; - + fp = _tfopen(DLL_FILE, _T("rb")); if (fp == NULL) { @@ -80,14 +80,14 @@ void LoadFromMemory(void) resourceInfo = MemoryFindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION); _tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo); - + resourceSize = MemorySizeofResource(handle, resourceInfo); resourceData = MemoryLoadResource(handle, resourceInfo); _tprintf(_T("Memory resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData); - + MemoryLoadString(handle, 1, buffer, sizeof(buffer)); _tprintf(_T("String1: %s\n"), buffer); - + MemoryLoadString(handle, 20, buffer, sizeof(buffer)); _tprintf(_T("String2: %s\n"), buffer); diff --git a/example/DllLoader/Makefile b/example/DllLoader/Makefile index 92fbd0a..dcfc263 100644 --- a/example/DllLoader/Makefile +++ b/example/DllLoader/Makefile @@ -15,7 +15,7 @@ endif RM = rm CFLAGS = -Wall -g -LDFLAGS = +LDFLAGS = ifdef UNICODE CFLAGS += -DUNICODE -D_UNICODE diff --git a/example/Makefile b/example/Makefile index 2852993..48b8e0b 100644 --- a/example/Makefile +++ b/example/Makefile @@ -10,7 +10,7 @@ $(SUBDIRS): CLEANDIRS = $(SUBDIRS:%=clean-%) clean: $(CLEANDIRS) -$(CLEANDIRS): +$(CLEANDIRS): $(MAKE) -C $(@:clean-%=%) clean .PHONY: subdirs $(INSTALLDIRS)