Minor stuff

This commit is contained in:
Aurora 2016-09-06 14:09:29 +02:00
parent 024335e5cd
commit 16bc08f046
7 changed files with 75 additions and 65 deletions

View File

@ -43,7 +43,7 @@ clean:
$(dir_build)/$(name).elf: $(objects) $(dir_build)/$(name).elf: $(objects)
$(LINK.o) $(OUTPUT_OPTION) $^ $(LIBPATHS) $(LIBS) $(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 $(dir_build)/%.o: $(dir_source)/%.c
@mkdir -p "$(@D)" @mkdir -p "$(@D)"

View File

@ -7,4 +7,44 @@ void memcpy(void *dest, const void *src, u32 size)
for(u32 i = 0; i < size; i++) for(u32 i = 0; i < size; i++)
destc[i] = srcc[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;
} }

View File

@ -2,4 +2,6 @@
#include <3ds/types.h> #include <3ds/types.h>
void memcpy(void *dest, const void *src, u32 size); 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);

View File

@ -1,51 +1,12 @@
#include <3ds.h> #include <3ds.h>
#include "memory.h"
#include "patcher.h" #include "patcher.h"
#include "memory.h"
#include "strings.h"
#include "ifile.h" #include "ifile.h"
#include "CFWInfo.h" #include "CFWInfo.h"
static CFWInfo info; 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) 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++) 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) static int fileOpen(IFile *file, FS_ArchiveID archiveId, const char *path, int flags)
{ {
FS_Path filePath = {PATH_ASCII, strnlen(path, PATH_MAX) + 1, path}, FS_Path filePath = {PATH_ASCII, strnlen(path, PATH_MAX) + 1, path},
@ -115,16 +67,6 @@ static bool secureInfoExists(void)
return exists; 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) static void loadTitleCodeSection(u64 progId, u8 *code, u32 size)
{ {
/* Here we look for "/luma/code_sections/[u64 titleID in hex, uppercase].bin" /* Here we look for "/luma/code_sections/[u64 titleID in hex, uppercase].bin"

20
injector/source/strings.c Normal file
View File

@ -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;
}
}

View File

@ -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);

View File

@ -66,9 +66,9 @@ u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize)
//Preprocessing //Preprocessing
u32 table[256]; u32 table[256];
for(u32 i = 0; i < 256; ++i) for(u32 i = 0; i < 256; i++)
table[i] = patternSize + 1; table[i] = patternSize + 1;
for(u32 i = 0; i < patternSize; ++i) for(u32 i = 0; i < patternSize; i++)
table[patternc[i]] = patternSize - i; table[patternc[i]] = patternSize - i;
//Searching //Searching
@ -76,7 +76,7 @@ u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize)
while(j <= size - patternSize) while(j <= size - patternSize)
{ {
if(memcmp(patternc, startPos + j, patternSize) == 0) if(memcmp(pattern, startPos + j, patternSize) == 0)
return startPos + j; return startPos + j;
j += table[startPos[j + patternSize]]; j += table[startPos[j + patternSize]];
} }