Introduce a decimal itoa, fixed findDumpFile not working properly with more than 100 dumps

This commit is contained in:
Aurora 2016-10-22 16:25:27 +02:00
parent 858efa604e
commit e00ef893d0
5 changed files with 43 additions and 18 deletions

View File

@ -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.");
}

View File

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

View File

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

View File

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

View File

@ -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);
void hexItoa(u32 number, char *out, u32 digits, bool fillString);
void decItoa(u32 number, char *out, u32 digits);
u32 decAtoi(const char *in);