diff --git a/source/fileops.c b/source/fileops.c index acca2cb..ea43a50 100644 --- a/source/fileops.c +++ b/source/fileops.c @@ -4,52 +4,29 @@ #include "fileops.h" #include "malloc.h" +static struct stat st; + bool FSOPFileExists(const char* file) { - FILE* f; - f = fopen(file, "rb"); - if (f) - { - fclose(f); - return true; - } - return false; + return !stat(file, &st) && !S_ISDIR(st.st_mode); } bool FSOPFolderExists(const char* path) { - DIR* dir; - dir = opendir(path); - if (dir) - { - closedir(dir); - return true; - } - return false; + return !stat(path, &st) && S_ISDIR(st.st_mode); } size_t FSOPGetFileSizeBytes(const char* path) { - FILE* f; - size_t size = 0; + if (stat(path, &st) < 0) return 0; - f = fopen(path, "rb"); - if (!f) - return 0; - - fseek(f, 0, SEEK_END); - size = ftell(f); - fclose(f); - - return size; + return st.st_size; } void FSOPDeleteFile(const char* file) { - if (!FSOPFileExists(file)) - return; - - remove(file); + if (FSOPFileExists(file)) + remove(file); } void FSOPMakeFolder(const char* path) @@ -89,8 +66,11 @@ s32 FSOPReadOpenFileA(FILE* fp, void** buffer, u32 offset, u32 length) return -1; s32 ret = FSOPReadOpenFile(fp, *buffer, offset, length); - if (ret < 0) + if (ret <= 0) + { free(*buffer); + *buffer = NULL; + } return ret; } diff --git a/source/fileops.h b/source/fileops.h index b912721..b39a930 100644 --- a/source/fileops.h +++ b/source/fileops.h @@ -1,6 +1,7 @@ #ifndef __FILEOPS_H__ #define __FILEOPS_H__ +#include #include #include @@ -14,4 +15,4 @@ void FSOPMakeFolder(const char* path); s32 FSOPReadOpenFile(FILE* fp, void* buffer, u32 offset, u32 length); s32 FSOPReadOpenFileA(FILE* fp, void** buffer, u32 offset, u32 length); -#endif \ No newline at end of file +#endif diff --git a/source/sys.c b/source/sys.c index a25dc15..8b028d4 100644 --- a/source/sys.c +++ b/source/sys.c @@ -158,6 +158,11 @@ void Sys_Shutdown(void) void Sys_LoadMenu(void) { + /* SYSCALL(exit) does all of this already */ + exit(0); + + /* Also the code gcc generated for this looks really painful */ +#if 0 int HBC = 0; char *sig = (char *)0x80001804; if (sig[0] == 'S' && @@ -179,6 +184,7 @@ void Sys_LoadMenu(void) } /* Return to the Wii system menu */ SYS_ResetSystem(SYS_RETURNTOMENU, 0, 0); +#endif } s32 Sys_GetCerts(signed_blob **certs, u32 *len) diff --git a/source/wad.c b/source/wad.c index cff49fb..da53c74 100644 --- a/source/wad.c +++ b/source/wad.c @@ -1165,6 +1165,13 @@ s32 Wad_Uninstall(FILE *fp) goto out; } + if (!__Wad_VerifyHeader(header)) + { + puts("Invalid WAD file?"); + ret = ES_EINVAL; + goto out; + } + /* Get title ID */ ret = __Wad_GetTitleID(fp, header, &tid); if (ret < 0) { @@ -1215,7 +1222,8 @@ s32 Wad_Uninstall(FILE *fp) GetSysMenuRegion(NULL, ®ion); if((tid == TITLE_ID(0x10008, 0x48414B00 | 'E') || tid == TITLE_ID(0x10008, 0x48414B00 | 'P') || tid == TITLE_ID(0x10008, 0x48414B00 | 'J') || tid == TITLE_ID(0x10008, 0x48414B00 | 'K') - || (tid == TITLE_ID(0x10008, 0x48414C00 | 'E') || tid == TITLE_ID(0x10008, 0x48414C00 | 'P') || tid == TITLE_ID(0x10008, 0x48414C00 | 'J') || tid == TITLE_ID(0x10008, 0x48414C00 | 'K'))) && region == 0) + || (tid == TITLE_ID(0x10008, 0x48414C00 | 'E') || tid == TITLE_ID(0x10008, 0x48414C00 | 'P') || tid == TITLE_ID(0x10008, 0x48414C00 | 'J') || tid == TITLE_ID(0x10008, 0x48414C00 | 'K'))) + && region == 0) { printf("\n Unknown SM region\n Please check the site for updates\n"); ret = -999; @@ -1257,9 +1265,9 @@ s32 Wad_Uninstall(FILE *fp) printf(" ERROR! (ret = %d)\n", ret); /* Delete tickets */ - if (ret >= 0) { + else { u32 cnt; - tikview view ATTRIBUTE_ALIGN(0x20) = {}; + static tikview view ATTRIBUTE_ALIGN(0x20); /* Delete all tickets */ for (cnt = 0; cnt < viewCnt; cnt++) { @@ -1282,6 +1290,7 @@ s32 Wad_Uninstall(FILE *fp) else printf(" OK!\n"); } + free(viewData); printf("\t\t>> Deleting title contents..."); fflush(stdout); diff --git a/source/wkb.c b/source/wkb.c index 36fcfbb..3284a7c 100644 --- a/source/wkb.c +++ b/source/wkb.c @@ -3,6 +3,8 @@ * https://github.com/DacoTaco/priiloader/blob/master/tools/DacosLove/source/Input.cpp */ +#include +#include #include #include @@ -107,7 +109,7 @@ static void* WKBThread(__attribute__((unused)) void* arg) usleep(WKB_THREAD_UDELAY); } - for (int led = 3; led; led--) { USBKeyboard_SetLed(led, false); usleep(WKB_ANIMATION_UDELAY); } +// for (int led = 3; led; led--) { USBKeyboard_SetLed(led, false); usleep(WKB_ANIMATION_UDELAY); } return NULL; } @@ -118,6 +120,7 @@ void WKB_Initialize(void) WKBThreadActive = true; LWP_CreateThread(&WKBThreadHandle, WKBThread, NULL, NULL, WKB_THREAD_STACK, WKB_THREAD_PRIORITY); + atexit(WKB_Deinitialize); } void WKB_Deinitialize(void)