From e00ef893d06726078250194cd56aecc65ea667d0 Mon Sep 17 00:00:00 2001 From: Aurora Date: Sat, 22 Oct 2016 16:25:27 +0200 Subject: [PATCH] Introduce a decimal itoa, fixed findDumpFile not working properly with more than 100 dumps --- source/crypto.c | 9 ++------- source/fs.c | 11 ++--------- source/main.c | 2 +- source/strings.c | 35 +++++++++++++++++++++++++++++++++++ source/strings.h | 4 +++- 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/source/crypto.c b/source/crypto.c index 406976b..e228a0e 100755 --- a/source/crypto.c +++ b/source/crypto.c @@ -29,6 +29,7 @@ #include "crypto.h" #include "memory.h" +#include "strings.h" #include "utils.h" #include "fatfs/sdmmc/sdmmc.h" @@ -502,15 +503,9 @@ void kernel9Loader(Arm9Bin *arm9Section) u8 __attribute__((aligned(4))) arm9BinCtr[AES_BLOCK_SIZE]; memcpy(arm9BinCtr, arm9Section->ctr, sizeof(arm9BinCtr)); - //Calculate the size of the ARM9 binary - u32 arm9BinSize = 0; - //http://stackoverflow.com/questions/12791077/atoi-implementation-in-c - for(char *tmp = arm9Section->size; *tmp != 0; tmp++) - arm9BinSize = (arm9BinSize << 3) + (arm9BinSize << 1) + *tmp - '0'; - //Decrypt ARM9 binary aes_use_keyslot(arm9BinSlot); - aes(startOfArm9Bin, startOfArm9Bin, arm9BinSize / AES_BLOCK_SIZE, arm9BinCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL); + aes(startOfArm9Bin, startOfArm9Bin, decAtoi(arm9Section->size) / AES_BLOCK_SIZE, arm9BinCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL); if(*startOfArm9Bin != 0x47704770 && *startOfArm9Bin != 0xB0862000) error("Failed to decrypt the ARM9 binary."); } diff --git a/source/fs.c b/source/fs.c index fa3ee9c..5bc2ad5 100644 --- a/source/fs.c +++ b/source/fs.c @@ -242,16 +242,9 @@ void findDumpFile(const char *path, char *fileName) { result = f_findfirst(&dir, &info, path, fileName); - if(result != FR_OK || !info.fname[0]) break; + if(result != FR_OK || !info.fname[0] || n == 99999999) break; - u32 i = 18, - tmp = ++n; - - while(tmp > 0) - { - fileName[i--] = '0' + (tmp % 10); - tmp /= 10; - } + decItoa(++n, fileName + 11, 8); } if(result == FR_OK) f_closedir(&dir); diff --git a/source/main.c b/source/main.c index e97730c..41a2ad7 100644 --- a/source/main.c +++ b/source/main.c @@ -253,7 +253,7 @@ void main(void) if(res != 0) { char patchesError[] = "Failed to apply FIRM patch(es)."; - hexItoa(res, patchesError + 16, 2, false); + decItoa(res, patchesError + 16, 2); error(patchesError); } diff --git a/source/strings.c b/source/strings.c index 23a2447..c581877 100644 --- a/source/strings.c +++ b/source/strings.c @@ -52,4 +52,39 @@ void hexItoa(u32 number, char *out, u32 digits, bool fillString) } if(fillString) while(i < digits) out[digits - 1 - i++] = '0'; +} + +void decItoa(u32 number, char *out, u32 digits) +{ + while(number >= 10) + { + u32 i, + tmp; + + for(i = 0, tmp = number; tmp >= 10; tmp /= 10, i++); + + out[digits - 1 - i] = '0' + tmp; + + u32 tmp2 = 10; + + while(i > 1) + { + tmp2 *= 10; + i--; + } + + number -= tmp * tmp2; + } + + out[digits - 1] = '0' + number; +} + +u32 decAtoi(const char *in) +{ + u32 res = 0; + + for(char *tmp = (char *)in; *tmp != 0; tmp++) + res = *tmp - '0' + res * 10; + + return res; } \ No newline at end of file diff --git a/source/strings.h b/source/strings.h index 7bc0859..3dbd67a 100644 --- a/source/strings.h +++ b/source/strings.h @@ -26,4 +26,6 @@ u32 strlen(const char *string); void concatenateStrings(char *destination, const char *source); -void hexItoa(u32 number, char *out, u32 digits, bool fillString); \ No newline at end of file +void hexItoa(u32 number, char *out, u32 digits, bool fillString); +void decItoa(u32 number, char *out, u32 digits); +u32 decAtoi(const char *in); \ No newline at end of file