Move hexAtoi to strings.c, add bound check to decAtoi

This commit is contained in:
Aurora 2016-10-22 16:57:18 +02:00
parent 04f42f0be4
commit 211cd964d7
4 changed files with 21 additions and 11 deletions

View File

@ -505,7 +505,7 @@ void kernel9Loader(Arm9Bin *arm9Section)
//Decrypt ARM9 binary
aes_use_keyslot(arm9BinSlot);
aes(startOfArm9Bin, startOfArm9Bin, decAtoi(arm9Section->size) / AES_BLOCK_SIZE, arm9BinCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
aes(startOfArm9Bin, startOfArm9Bin, decAtoi(arm9Section->size, sizeof(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

@ -202,13 +202,7 @@ u32 firmRead(void *dest, u32 firmType)
//Not a cxi
if(info.fname[9] != 'a' || strlen(info.fname) != 12) continue;
//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);
//Found an older cxi
if(tempVersion < firmVersion) firmVersion = tempVersion;

View File

@ -79,11 +79,26 @@ void decItoa(u32 number, char *out, u32 digits)
out[digits - 1] = '0' + number;
}
u32 decAtoi(const char *in)
u32 hexAtoi(const char *in, u32 digits)
{
u32 res = 0;
char *tmp = (char *)in;
for(char *tmp = (char *)in; *tmp != 0; tmp++)
for(u32 i = 0; i < digits && *tmp != 0; tmp++, i++)
{
res <<= 4;
res += *tmp > '9' ? *tmp - 'A' + 10 : *tmp - '0';
}
return res;
}
u32 decAtoi(const char *in, u32 digits)
{
u32 res = 0;
char *tmp = (char *)in;
for(u32 i = 0; i < digits && *tmp != 0; tmp++, i++)
res = *tmp - '0' + res * 10;
return res;

View File

@ -28,4 +28,5 @@ u32 strlen(const char *string);
void concatenateStrings(char *destination, const char *source);
void hexItoa(u32 number, char *out, u32 digits, bool fillString);
void decItoa(u32 number, char *out, u32 digits);
u32 decAtoi(const char *in);
u32 hexAtoi(const char *in, u32 digits);
u32 decAtoi(const char *in, u32 digits);