diff --git a/include/util.h b/include/util.h index a2c4147..a5061c2 100644 --- a/include/util.h +++ b/include/util.h @@ -1,45 +1,23 @@ #pragma once +#include #include "types.h" #define min(a,b) (u32) a < (u32) b ? (u32) a : (u32) b -static void wait(u32 cycles) -{ - cycles >>= 2; - while(cycles) - { - __asm("nop"); - cycles--; - } -} +#define arrayEntries(array) sizeof(array)/sizeof(*array) + + +void wait(u32 cycles); + + +// case insensitive string compare function +int stricmp(const char *str1, const char *str2); // custom safe strncpy, string is always 0-terminated for buflen > 0 -static void strncpy_s(char *dest, const char *src, u32 nchars, const u32 buflen) -{ - char c; +void strncpy_s(char *dest, const char *src, u32 nchars, const u32 buflen); - if(!buflen) - return; - - if(buflen > 1) - { - if(nchars >= buflen) - nchars = buflen - 1; - - while(nchars--) - { - c = *src++; - - if(c == '\0') - break; - - *dest++ = c; - } - } - - *dest = '\0'; -} u32 getleu32(const void* ptr); + u32 swap32(u32 val); diff --git a/source/arm9/firm.c b/source/arm9/firm.c index df44a87..6e8bde5 100644 --- a/source/arm9/firm.c +++ b/source/arm9/firm.c @@ -118,6 +118,12 @@ return false; } + if((section->size >> 31) || (section->size + section->offset > fwSize)) + { + printf("\x1B[31mBad section size!\e[0m\n"); + return false; + } + // check for bad sections const u32 numEntries = sizeof(firmProtectedAreas)/sizeof(firmProtectedArea); for(i=0; i>= 2; + while(cycles) + { + __asm("nop"); + cycles--; + } +} + +// case insensitive string compare function +int strnicmp(const char *str1, const char *str2, u32 len) +{ + int diff; + + if(len == 0) + return 0; + + for(;; str1++, str2++) + { + if(len != 0) len --; + else return 0; + diff = tolower(*str1) - tolower(*str2); + if(diff != 0 || *str1 == '\0') + return diff; + } + + return 0; +} + +// custom safe strncpy, string is always 0-terminated for buflen > 0 +void strncpy_s(char *dest, const char *src, u32 nchars, const u32 buflen) +{ + char c; + + if(!buflen) + return; + + if(buflen > 1) + { + if(nchars >= buflen) + nchars = buflen - 1; + + while(nchars--) + { + c = *src++; + + if(c == '\0') + break; + + *dest++ = c; + } + } + + *dest = '\0'; +} u32 getleu32(const void* ptr) {