From 024335e5cd7436e810a7d4567829caa6ad7b0f07 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 6 Sep 2016 13:47:04 +0200 Subject: [PATCH 1/2] We do not use this --- injector/source/patcher.c | 8 ++------ loader/source/main.c | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/injector/source/patcher.c b/injector/source/patcher.c index 40686c5..636e6ad 100644 --- a/injector/source/patcher.c +++ b/injector/source/patcher.c @@ -46,11 +46,9 @@ static u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSiz return NULL; } -static u32 patchMemory(u8 *start, u32 size, const void *pattern, u32 patSize, int offset, const void *replace, u32 repSize, u32 count) +static void patchMemory(u8 *start, u32 size, const void *pattern, u32 patSize, int offset, const void *replace, u32 repSize, u32 count) { - u32 i; - - for(i = 0; i < count; i++) + for(u32 i = 0; i < count; i++) { u8 *found = memsearch(start, pattern, size, patSize); @@ -65,8 +63,6 @@ static u32 patchMemory(u8 *start, u32 size, const void *pattern, u32 patSize, in size -= at + patSize; start = found + patSize; } - - return i; } static inline size_t strnlen(const char *string, size_t maxlen) diff --git a/loader/source/main.c b/loader/source/main.c index f6d9cae..b902b9d 100644 --- a/loader/source/main.c +++ b/loader/source/main.c @@ -29,9 +29,9 @@ void main(void) { void *payloadAddress = (void *)0x23F00000; - memcpy(payloadAddress, (void*)0x24F00000, payloadSize); + memcpy(payloadAddress, (void *)0x24F00000, payloadSize); flushCaches(); - + ((void (*)())payloadAddress)(); } \ No newline at end of file From 16bc08f0463b023198edfb6507acbfdf7e5c98a1 Mon Sep 17 00:00:00 2001 From: Aurora Date: Tue, 6 Sep 2016 14:09:29 +0200 Subject: [PATCH 2/2] Minor stuff --- injector/Makefile | 2 +- injector/source/memory.c | 40 +++++++++++++++++++++++++ injector/source/memory.h | 4 ++- injector/source/patcher.c | 62 ++------------------------------------- injector/source/strings.c | 20 +++++++++++++ injector/source/strings.h | 6 ++++ source/memory.c | 6 ++-- 7 files changed, 75 insertions(+), 65 deletions(-) create mode 100644 injector/source/strings.c create mode 100644 injector/source/strings.h diff --git a/injector/Makefile b/injector/Makefile index 6fa5844..e733480 100755 --- a/injector/Makefile +++ b/injector/Makefile @@ -43,7 +43,7 @@ clean: $(dir_build)/$(name).elf: $(objects) $(LINK.o) $(OUTPUT_OPTION) $^ $(LIBPATHS) $(LIBS) -$(dir_build)/memory.o : CFLAGS += -O3 +$(dir_build)/memory.o $(dir_build)/strings.o: CFLAGS += -O3 $(dir_build)/%.o: $(dir_source)/%.c @mkdir -p "$(@D)" diff --git a/injector/source/memory.c b/injector/source/memory.c index 4f4c96a..8c36f5f 100644 --- a/injector/source/memory.c +++ b/injector/source/memory.c @@ -7,4 +7,44 @@ void memcpy(void *dest, const void *src, u32 size) for(u32 i = 0; i < size; i++) destc[i] = srcc[i]; +} + +int memcmp(const void *buf1, const void *buf2, u32 size) +{ + const u8 *buf1c = (const u8 *)buf1; + const u8 *buf2c = (const u8 *)buf2; + + for(u32 i = 0; i < size; i++) + { + int cmp = buf1c[i] - buf2c[i]; + if(cmp) return cmp; + } + + return 0; +} + +//Quick Search algorithm, adapted from http://igm.univ-mlv.fr/~lecroq/string/node19.html#SECTION00190 +u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize) +{ + const u8 *patternc = (const u8 *)pattern; + + //Preprocessing + u32 table[256]; + + for(u32 i = 0; i < 256; i++) + table[i] = patternSize + 1; + for(u32 i = 0; i < patternSize; i++) + table[patternc[i]] = patternSize - i; + + //Searching + u32 j = 0; + + while(j <= size - patternSize) + { + if(memcmp(pattern, startPos + j, patternSize) == 0) + return startPos + j; + j += table[startPos[j + patternSize]]; + } + + return NULL; } \ No newline at end of file diff --git a/injector/source/memory.h b/injector/source/memory.h index c1e1609..b7cf593 100644 --- a/injector/source/memory.h +++ b/injector/source/memory.h @@ -2,4 +2,6 @@ #include <3ds/types.h> -void memcpy(void *dest, const void *src, u32 size); \ No newline at end of file +void memcpy(void *dest, const void *src, u32 size); +int memcmp(const void *buf1, const void *buf2, u32 size); +u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize); \ No newline at end of file diff --git a/injector/source/patcher.c b/injector/source/patcher.c index 636e6ad..9c69eb4 100644 --- a/injector/source/patcher.c +++ b/injector/source/patcher.c @@ -1,51 +1,12 @@ #include <3ds.h> -#include "memory.h" #include "patcher.h" +#include "memory.h" +#include "strings.h" #include "ifile.h" #include "CFWInfo.h" static CFWInfo info; -static int memcmp(const void *buf1, const void *buf2, u32 size) -{ - const u8 *buf1c = (const u8 *)buf1; - const u8 *buf2c = (const u8 *)buf2; - - for(u32 i = 0; i < size; i++) - { - int cmp = buf1c[i] - buf2c[i]; - if(cmp) return cmp; - } - - return 0; -} - -//Quick Search algorithm, adapted from http://igm.univ-mlv.fr/~lecroq/string/node19.html#SECTION00190 -static u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize) -{ - const u8 *patternc = (const u8 *)pattern; - - //Preprocessing - u32 table[256]; - - for(u32 i = 0; i < 256; ++i) - table[i] = patternSize + 1; - for(u32 i = 0; i < patternSize; ++i) - table[patternc[i]] = patternSize - i; - - //Searching - u32 j = 0; - - while(j <= size - patternSize) - { - if(memcmp(patternc, startPos + j, patternSize) == 0) - return startPos + j; - j += table[startPos[j + patternSize]]; - } - - return NULL; -} - static void patchMemory(u8 *start, u32 size, const void *pattern, u32 patSize, int offset, const void *replace, u32 repSize, u32 count) { for(u32 i = 0; i < count; i++) @@ -65,15 +26,6 @@ static void patchMemory(u8 *start, u32 size, const void *pattern, u32 patSize, i } } -static inline size_t strnlen(const char *string, size_t maxlen) -{ - size_t size; - - for(size = 0; *string && size < maxlen; string++, size++); - - return size; -} - static int fileOpen(IFile *file, FS_ArchiveID archiveId, const char *path, int flags) { FS_Path filePath = {PATH_ASCII, strnlen(path, PATH_MAX) + 1, path}, @@ -115,16 +67,6 @@ static bool secureInfoExists(void) return exists; } -static void progIdToStr(char *strEnd, u64 progId) -{ - while(progId) - { - static const char hexDigits[] = "0123456789ABCDEF"; - *strEnd-- = hexDigits[(u32)(progId & 0xF)]; - progId >>= 4; - } -} - static void loadTitleCodeSection(u64 progId, u8 *code, u32 size) { /* Here we look for "/luma/code_sections/[u64 titleID in hex, uppercase].bin" diff --git a/injector/source/strings.c b/injector/source/strings.c new file mode 100644 index 0000000..4f34404 --- /dev/null +++ b/injector/source/strings.c @@ -0,0 +1,20 @@ +#include "strings.h" + +size_t strnlen(const char *string, size_t maxlen) +{ + size_t size; + + for(size = 0; *string && size < maxlen; string++, size++); + + return size; +} + +void progIdToStr(char *strEnd, u64 progId) +{ + while(progId) + { + static const char hexDigits[] = "0123456789ABCDEF"; + *strEnd-- = hexDigits[(u32)(progId & 0xF)]; + progId >>= 4; + } +} \ No newline at end of file diff --git a/injector/source/strings.h b/injector/source/strings.h new file mode 100644 index 0000000..6599118 --- /dev/null +++ b/injector/source/strings.h @@ -0,0 +1,6 @@ +#pragma once + +#include <3ds/types.h> + +size_t strnlen(const char *string, size_t maxlen); +void progIdToStr(char *strEnd, u64 progId); \ No newline at end of file diff --git a/source/memory.c b/source/memory.c index 03a36ae..48d4060 100644 --- a/source/memory.c +++ b/source/memory.c @@ -66,9 +66,9 @@ u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize) //Preprocessing u32 table[256]; - for(u32 i = 0; i < 256; ++i) + for(u32 i = 0; i < 256; i++) table[i] = patternSize + 1; - for(u32 i = 0; i < patternSize; ++i) + for(u32 i = 0; i < patternSize; i++) table[patternc[i]] = patternSize - i; //Searching @@ -76,7 +76,7 @@ u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize) while(j <= size - patternSize) { - if(memcmp(patternc, startPos + j, patternSize) == 0) + if(memcmp(pattern, startPos + j, patternSize) == 0) return startPos + j; j += table[startPos[j + patternSize]]; }