From 5ded6aa6192a2d5349c509a82b94585c1fcb0ef3 Mon Sep 17 00:00:00 2001 From: Aurora Date: Sun, 23 Oct 2016 18:55:41 +0200 Subject: [PATCH] Minor stuff --- source/fs.c | 16 ++++++---------- source/strings.c | 17 ++++++++++++++--- source/strings.h | 3 ++- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/source/fs.c b/source/fs.c index 3debe38..3c06020 100644 --- a/source/fs.c +++ b/source/fs.c @@ -40,9 +40,10 @@ void unmountCtrNand(void) u32 fileRead(void *dest, const char *path, u32 maxSize) { FIL file; - u32 ret = 0; + u32 ret; - if(f_open(&file, path, FA_READ) == FR_OK) + if(f_open(&file, path, FA_READ) != FR_OK) ret = 0; + else { u32 size = f_size(&file); if(size <= maxSize) @@ -98,13 +99,14 @@ u32 firmRead(void *dest) concatenateStrings(path, "/content"); DIR dir; - FILINFO info; u32 firmVersion = 0xFFFFFFFF, ret = 0; if(f_opendir(&dir, path) == FR_OK) { + FILINFO info; + //Parse the target directory while(f_readdir(&dir, &info) == FR_OK && info.fname[0] != 0) { @@ -114,13 +116,7 @@ u32 firmRead(void *dest) //Multiple cxis were found if(firmVersion != 0xFFFFFFFF) ret = 1; - //Convert the .app name to an integer - u32 tempVersion = 0; - for(char *tmp = info.altname; *tmp != '.'; tmp++) - { - tempVersion <<= 4; - tempVersion += *tmp > '9' ? *tmp - 'A' + 10 : *tmp - '0'; - } + u32 tempVersion = hexAtoi(info.altname, 8); //FIRM is equal or newer than 11.0 if(tempVersion >= (ISN3DS ? 0x21 : 0x52)) ret = tempVersion <= (ISN3DS ? 0x26 : 0x56) ? 5 : 2; diff --git a/source/strings.c b/source/strings.c index b20b332..4018d45 100644 --- a/source/strings.c +++ b/source/strings.c @@ -43,11 +43,22 @@ void concatenateStrings(char *destination, const char *source) void hexItoa(u32 number, char *out, u32 digits) { const char hexDigits[] = "0123456789ABCDEF"; - u32 i = 0; + u32 i; - while(number > 0) + for(i = 0; number > 0; i++) { - out[digits - 1 - i++] = hexDigits[number & 0xF]; + out[digits - 1 - i] = hexDigits[number & 0xF]; number >>= 4; } +} + +u32 hexAtoi(const char *in, u32 digits) +{ + u32 res = 0; + char *tmp = (char *)in; + + for(u32 i = 0; i < digits && *tmp != 0; tmp++, i++) + res = (*tmp > '9' ? *tmp - 'A' + 10 : *tmp - '0') + (res << 4); + + return res; } \ No newline at end of file diff --git a/source/strings.h b/source/strings.h index fc40e19..31a8bde 100644 --- a/source/strings.h +++ b/source/strings.h @@ -26,4 +26,5 @@ u32 strlen(const char *string); void concatenateStrings(char *destination, const char *source); -void hexItoa(u32 number, char *out, u32 digits); \ No newline at end of file +void hexItoa(u32 number, char *out, u32 digits); +u32 hexAtoi(const char *in, u32 digits); \ No newline at end of file