diff options
-rw-r--r-- | CHANGELOG.txt | 12 | ||||
-rw-r--r-- | CMakeLists.txt | 21 | ||||
-rw-r--r-- | agbsum/CMakeLists.txt | 45 | ||||
-rw-r--r-- | agbsum/include/agbsum.h | 44 | ||||
-rw-r--r-- | agbsum/src/calc.c | 15 | ||||
-rw-r--r-- | agbsum/src/chkparams.c | 89 | ||||
-rw-r--r-- | agbsum/src/exit.c | 7 | ||||
-rw-r--r-- | agbsum/src/help.c | 24 | ||||
-rw-r--r-- | agbsum/src/initdat.c | 10 | ||||
-rw-r--r-- | agbsum/src/main.c | 38 | ||||
-rw-r--r-- | agbsum/src/open.c | 13 | ||||
-rw-r--r-- | agbsum/src/pat.c | 11 | ||||
-rw-r--r-- | agbsum/src/rd.c | 13 |
13 files changed, 173 insertions, 169 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 16fa013..96de3d6 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,15 @@ +| 7 + +- Don't use susinfo; +- Update CMake style; +- Update code style; +- Don't use global variables; +- Update compiler options; +- Fortify sources; +- Don't use Zap; +- Remove some errors; +- Remove platform-specific behaviour; + | 6 - Fix errors during parameter checks; diff --git a/CMakeLists.txt b/CMakeLists.txt index d0b2faa..e49d1ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,20 +1,11 @@ -cmake_minimum_required( - VERSION - 3.21 -) +cmake_minimum_required(VERSION 3.21) project( agbsum - VERSION - 6 - DESCRIPTION - "GBA ROM header checksum patcher." - HOMEPAGE_URL - "https://mandelbrot.dk/agbsum" - LANGUAGES - C + VERSION 7 + DESCRIPTION "GBA ROM header checksum patcher." + HOMEPAGE_URL "https://mandelbrot.dk/agbsum" + LANGUAGES C ) -add_subdirectory( - agbsum -) +add_subdirectory(agbsum) diff --git a/agbsum/CMakeLists.txt b/agbsum/CMakeLists.txt index bdfdda9..df89bf3 100644 --- a/agbsum/CMakeLists.txt +++ b/agbsum/CMakeLists.txt @@ -1,19 +1,10 @@ -cmake_minimum_required( - VERSION - 3.21 -) - -set( - CMAKE_C_STANDARD - 23 -) +cmake_minimum_required(VERSION 3.21) -set( - CMAKE_C_STANDARD_REQUIRED -) +set(CMAKE_C_STANDARD 23) add_executable( agbsum + "src/calc.c" "src/chkparams.c" "src/exit.c" @@ -26,32 +17,26 @@ add_executable( ) target_include_directories( - agbsum - PRIVATE + agbsum PRIVATE + "include" ) -target_link_libraries( - agbsum - PRIVATE - zap -) +target_compile_definitions( + agbsum PRIVATE -if( - "${CMAKE_C_COMPILER_ID}" - MATCHES - "Clang|GNU" + _FORTIFY_SOURCE=$<IF:$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">,0x0,0x2> ) + +if("${CMAKE_C_COMPILER_ID}" MATCHES "Clang|GNU") target_compile_options( - agbsum - PRIVATE - -Ofast + agbsum PRIVATE + + $<IF:$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">,-Og,-Ofast> -Wall -Wextra - -fPIC + -Wpedantic -fdiagnostics-color=always - -gdwarf-2 # Clang 16 uses a different format. - -march=native - -pedantic-errors + -g ) endif() diff --git a/agbsum/include/agbsum.h b/agbsum/include/agbsum.h index 7cf7940..359cc1b 100644 --- a/agbsum/include/agbsum.h +++ b/agbsum/include/agbsum.h @@ -10,42 +10,52 @@ You should have received a copy of the GNU Affero General Public License along with agbsum. If not, see <https://www.gnu.org/licenses/>. */ -#if !defined(agbsum_hdr) +#ifndef agbsum_hdr #define agbsum_hdr #include <stdint.h> #include <stdio.h> -/* Temporary C2x support: */ -#define constexpr static const /* This can make arrays variadic-length. */ -#define nullptr NULL +// Temporary C23 support: +#define constexpr static const // This can make arrays variadic-length. +#ifndef __clang__ +#define bool _Bool +#define false ((bool)+0x0u) +#define nullptr (NULL) +#define static_assert _Static_assert +#define true ((bool)+0x1u) #define typeof __typeof__ +#endif -constexpr uint_least64_t agbsum_ver = 0x6u; - -constexpr size_t agbsum_romstart = 0xA0u; +constexpr uint_least64_t agbsum_ver = 0x7u; -constexpr size_t agbsum_chksumoff = 0xBDu - agbsum_romstart; +constexpr size_t agbsum_romstart = 0xA0u; +constexpr size_t agbsum_chksumoff = 0xBDu-agbsum_romstart; typedef enum { agbsum_stat_err, agbsum_stat_ok, } agbsum_stat; -extern struct { +typedef struct { bool dopat; char const * pth; bool sil; FILE * rom; } agbsum_dat; - unsigned char agbsum_calc( void const * rom); - void agbsum_chkparams(int argc, char const * const * argv); -[[noreturn]] void agbsum_exit( agbsum_stat stat); - void agbsum_help( void); - void agbsum_initdat( void); - void agbsum_open( void); - void agbsum_pat( unsigned char chksum); - void agbsum_rd( void * buf); +uint8_t agbsum_calc(void const * rom); + +void agbsum_help(void); + +void agbsum_chkparams(agbsum_dat * dat,int argc,char const * const * argv); + +void agbsum_initdat(agbsum_dat * dat); + +FILE * agbsum_open(char const * pth); +void agbsum_pat( FILE * rom, unsigned char chksum); +void agbsum_rd( void * buf, FILE * rom); + +[[noreturn]] void agbsum_exit(agbsum_stat stat,FILE * rom); #endif diff --git a/agbsum/src/calc.c b/agbsum/src/calc.c index 62cc7a6..a1aef02 100644 --- a/agbsum/src/calc.c +++ b/agbsum/src/calc.c @@ -12,10 +12,15 @@ #include <agbsum.h> -unsigned char agbsum_calc(void const * const _rom) { - unsigned char const * rom = _rom; - unsigned char chksum = 0x0u; - for (unsigned char const * pos = rom;pos != rom + agbsum_chksumoff;++pos) {chksum += *pos;} - chksum = -(0x19u + chksum); +#include <stdint.h> + +uint8_t agbsum_calc(void const * const romptr) { + uint8_t const * rom = romptr; + uint8_t chksum = 0x0u; + + for (unsigned char const * pos = rom;pos != rom+agbsum_chksumoff;++pos) {chksum += *pos;} + + chksum = -(0x19u+chksum); + return chksum; } diff --git a/agbsum/src/chkparams.c b/agbsum/src/chkparams.c index b99816f..e8dbc18 100644 --- a/agbsum/src/chkparams.c +++ b/agbsum/src/chkparams.c @@ -13,99 +13,84 @@ #include <agbsum.h> #include <stdio.h> -#include <sus/extra.h> -#include <sus/os.h> -#include <zap/mem.h> - -#if defined(sus_os_win) -#else -#include <sys/stat.h> -#endif - -[[noreturn]] static void agbsum_expectparamval(char const _chrparam) { - fprintf(stderr,"Expected value for character parameter '%c'\n",_chrparam); - agbsum_exit(agbsum_stat_err); +#include <string.h> + +[[noreturn]] static void agbsum_expectparamval(char const chrparam) { + fprintf(stderr,"Expected value for character parameter '%c'\n",chrparam); + agbsum_exit(agbsum_stat_err,nullptr); } -static bool agbsum_chkchrparam(char const * const _param) { - char const chrparam = _param[0x0u]; +static bool agbsum_chkchrparam(agbsum_dat * const dat,char const * const param) { + char const chrparam = param[0x0u]; if (chrparam == '\x0') {return true;} - char const * const paramval = &_param[0x1]; + + char const * const paramval = ¶m[0x1]; + switch (chrparam) { default: fprintf(stderr,"Invalid character parameter '%c'\n",chrparam); - agbsum_exit(agbsum_stat_err); + agbsum_exit(agbsum_stat_err,nullptr); case 'h': agbsum_help(); - agbsum_exit(agbsum_stat_ok); + agbsum_exit(agbsum_stat_ok,nullptr); case 'i': { - sus_unlikely (paramval[0x0u] == '\x0') {agbsum_expectparamval(chrparam);} - sus_unlikely (agbsum_dat.pth != nullptr) { - fprintf(stderr,"ROM already set (to \"%s\", now \"%s\")\n",agbsum_dat.pth,paramval); - agbsum_exit(agbsum_stat_err); - } -#if defined(sus_os_win) -#else - struct stat statstruct; - int const statcd = stat(paramval,&statstruct); - bool const exist = statcd == 0x0; - bool const isdir = S_ISDIR(statstruct.st_mode); -#endif - if (!exist) { - fprintf(stderr,"No such file \"%s\"\n",paramval); - agbsum_exit(agbsum_stat_err); - } - if (isdir) { - fprintf(stderr,"File \"%s\" is a directory\n",paramval); - agbsum_exit(agbsum_stat_err); - } - agbsum_dat.pth = paramval; + if (paramval[0x0u] == '\x0') {agbsum_expectparamval(chrparam);} + dat->pth = paramval; } return true; case 'p': - agbsum_dat.dopat = true; + dat->dopat = true; return false; case 's': - agbsum_dat.sil = true; + dat->sil = true; return false; } } -void agbsum_chkparams(int const _argc,char const * const * const _argv) { +void agbsum_chkparams(agbsum_dat * const dat,int const _argc,char const * const * const _argv) { if (_argc < 0x2) { agbsum_help(); - agbsum_exit(agbsum_stat_ok); + agbsum_exit(agbsum_stat_ok,nullptr); } else { size_t const numparam = _argc; - for (size_t pos = 0x1;pos < numparam;++pos) { + + for (size_t pos = 0x1u;pos < numparam;++pos) { + char const * const param = _argv[pos]; if (param[0x0u] == '-') { if (param[0x1u] == '-') { char const * const lngparam = ¶m[0x2u]; + if (lngparam[0x0u] == '\x0') { fputs("Missing long parameter after '--' sequence\n",stderr); - agbsum_exit(agbsum_stat_err); + agbsum_exit(agbsum_stat_err,nullptr); } - if (zap_streq(lngparam,"help")) { + + if (!strcmp(lngparam,"help")) { agbsum_help(); - agbsum_exit(agbsum_stat_ok); + agbsum_exit(agbsum_stat_ok,nullptr); } + fprintf(stderr,"Invalid long parameter \"%s\"\n",lngparam); - agbsum_exit(agbsum_stat_err); + agbsum_exit(agbsum_stat_err,nullptr); } - sus_unlikely (param[0x1u] == '\x0') { + + if (param[0x1u] == '\x0') { fputs("Missing character parameter after '-'\n",stderr); - agbsum_exit(agbsum_stat_err); + agbsum_exit(agbsum_stat_err,nullptr); } - for (char const * chrpos = ¶m[0x1u];;++chrpos) {if (agbsum_chkchrparam(chrpos)) {break;}} + + for (char const * chrpos = ¶m[0x1u];;++chrpos) {if (agbsum_chkchrparam(dat,chrpos)) {break;}} + continue; } } - if (agbsum_dat.pth == nullptr) { + + if (dat->pth == nullptr) { fputs("ROM not set (missing character parameter 'i')\n",stderr); - agbsum_exit(agbsum_stat_err); + agbsum_exit(agbsum_stat_err,nullptr); } } } diff --git a/agbsum/src/exit.c b/agbsum/src/exit.c index c2d7c79..329c8ef 100644 --- a/agbsum/src/exit.c +++ b/agbsum/src/exit.c @@ -15,7 +15,8 @@ #include <stdio.h> #include <stdlib.h> -void agbsum_exit(agbsum_stat const _stat) { - if (agbsum_dat.rom != nullptr) {fclose(agbsum_dat.rom);} - exit(_stat == agbsum_stat_ok ? EXIT_SUCCESS : EXIT_FAILURE); +void agbsum_exit(agbsum_stat const stat,FILE * rom) { + if (rom != nullptr) {fclose(rom);} + + exit(stat == agbsum_stat_ok ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/agbsum/src/help.c b/agbsum/src/help.c index 1bfb4ad..91a5cdc 100644 --- a/agbsum/src/help.c +++ b/agbsum/src/help.c @@ -16,15 +16,17 @@ #include <stdio.h> void agbsum_help(void) { - fputs("agbsum - Calculate GBA ROM header checksums.\n",stderr); - fprintf(stderr,"Release #%" PRIXLEAST64 ". Copyright 2022 Gabriel Jensen.\n",agbsum_ver); - fputc('\n',stderr); - fputs("Usage: agbsum [options] <ROM>\n",stderr); - fputc('\n',stderr); - fputs("Options:\n",stderr); - fputs(" --help -h Print the help screen\n",stderr); - fputs(" -p Patch the ROM\n",stderr); - fputs(" -s Don't print the results\n",stderr); - fputc('\n',stderr); - fputs("Built at " __TIME__ ", " __DATE__ ".\n",stderr); + fprintf(stderr, + "agbsum - Calculate GBA ROM header checksums.\n" + "Release #%" PRIXLEAST64 ". Copyright 2022 Gabriel Jensen.\n" + "\n" + "Usage: agbsum [options] <ROM>\n" + "Options:\n" + " --help -h Print the help screen\n" + " -p Patch the ROM\n" + " -s Don't print the results\n" + "\n" + "Built at " __TIME__ ", " __DATE__ ".\n", + agbsum_ver + ); } diff --git a/agbsum/src/initdat.c b/agbsum/src/initdat.c index 63fe001..01eafed 100644 --- a/agbsum/src/initdat.c +++ b/agbsum/src/initdat.c @@ -12,9 +12,9 @@ #include <agbsum.h> -void agbsum_initdat(void) { - agbsum_dat.dopat = false; - agbsum_dat.pth = nullptr; - agbsum_dat.sil = false; - agbsum_dat.rom = nullptr; +void agbsum_initdat(agbsum_dat * const dat) { + dat->dopat = false; + dat->pth = nullptr; + dat->sil = false; + dat->rom = nullptr; } diff --git a/agbsum/src/main.c b/agbsum/src/main.c index 61c10b5..58fc4ac 100644 --- a/agbsum/src/main.c +++ b/agbsum/src/main.c @@ -13,27 +13,35 @@ #include <agbsum.h> #include <limits.h> -#include <signal.h> #include <stdint.h> #include <stdio.h> -_Static_assert(CHAR_WIDTH == 0x8u,"agbsum only support 8-bit byte systems"); - -typeof (agbsum_dat) agbsum_dat; +static_assert(CHAR_WIDTH == 0x8u,"agbsum only support 8-bit byte systems"); int main(int const argc,char const * const * const argv) { - agbsum_initdat(); - agbsum_chkparams(argc,argv); - agbsum_open(); - unsigned char buf[agbsum_chksumoff + 0x1u]; - agbsum_rd(buf); + agbsum_dat dat; + + agbsum_initdat(&dat); + + agbsum_chkparams(&dat,argc,argv); + + dat.rom = agbsum_open(dat.pth); + + unsigned char buf[agbsum_chksumoff+0x1u]; + + agbsum_rd(buf,dat.rom); + unsigned char const chksum = agbsum_calc(buf); unsigned char const romchksum = buf[agbsum_chksumoff]; - if (romchksum == chksum || !agbsum_dat.dopat) { - if (!agbsum_dat.sil) {printf("\"%s\": %hhX (%hhX in file)\n",agbsum_dat.pth,chksum,romchksum);} - agbsum_exit(agbsum_stat_ok); + + if (romchksum == chksum || !dat.dopat) { + if (!dat.sil) {printf("\"%s\": %hhX (%hhX in file)\n",dat.pth,chksum,romchksum);} + agbsum_exit(agbsum_stat_ok,dat.rom); } - agbsum_pat(chksum); - if (!agbsum_dat.sil) {printf("\"%s\" @ %zX: %hhX => %hhX\n",agbsum_dat.pth,agbsum_romstart + agbsum_chksumoff,romchksum,chksum);} - agbsum_exit(agbsum_stat_ok); + + agbsum_pat(dat.rom,chksum); + + if (!dat.sil) {printf("\"%s\" @ %zX: %hhX => %hhX\n",dat.pth,agbsum_romstart+agbsum_chksumoff,romchksum,chksum);} + + agbsum_exit(agbsum_stat_ok,dat.rom); } diff --git a/agbsum/src/open.c b/agbsum/src/open.c index f746230..2b54cb7 100644 --- a/agbsum/src/open.c +++ b/agbsum/src/open.c @@ -14,10 +14,13 @@ #include <stdio.h> -void agbsum_open(void) { - agbsum_dat.rom = fopen(agbsum_dat.pth,"r+"); - if (agbsum_dat.rom == nullptr) { - fprintf(stderr,"Unable to open ROM \"%s\"\n",agbsum_dat.pth); - agbsum_exit(agbsum_stat_err); +FILE * agbsum_open(char const * const pth) { + FILE * rom = fopen(pth,"r+"); + + if (rom == nullptr) { + fputs("Unable to open ROM\n",stderr); + agbsum_exit(agbsum_stat_err,nullptr); } + + return rom; } diff --git a/agbsum/src/pat.c b/agbsum/src/pat.c index d9ce136..07cf2e9 100644 --- a/agbsum/src/pat.c +++ b/agbsum/src/pat.c @@ -14,10 +14,11 @@ #include <stdio.h> -void agbsum_pat(unsigned char _chksum) { - fseek(agbsum_dat.rom,(long)(agbsum_romstart + agbsum_chksumoff),SEEK_SET); - if (fwrite(&_chksum,0x1u,0x1u,agbsum_dat.rom) != 0x1u) { - fprintf(stderr,"Unable to patch ROM \"%s\"\n",agbsum_dat.pth); - agbsum_exit(agbsum_stat_err); +void agbsum_pat(FILE * const rom,unsigned char chksum) { + fseek(rom,(long)(agbsum_romstart+agbsum_chksumoff),SEEK_SET); + + if (fwrite(&chksum,0x1u,0x1u,rom) != 0x1u) { + fputs("Unable to patch ROM\n",stderr); + agbsum_exit(agbsum_stat_err,rom); } } diff --git a/agbsum/src/rd.c b/agbsum/src/rd.c index c408d0f..7094604 100644 --- a/agbsum/src/rd.c +++ b/agbsum/src/rd.c @@ -14,11 +14,12 @@ #include <stdio.h> -void agbsum_rd(void * const _buf) { - fseek(agbsum_dat.rom,agbsum_romstart,SEEK_SET); /* We only need to read the part of the ROM that is used for the checksum. */ - size_t const num = agbsum_chksumoff + 0x1u; - if (fread(_buf,0x1u,num,agbsum_dat.rom) != num) { - fprintf(stderr,"Unable to read ROM \"%s\"\n",agbsum_dat.pth); - agbsum_exit(agbsum_stat_err); +void agbsum_rd(void * const buf,FILE * rom) { + fseek(rom,agbsum_romstart,SEEK_SET); // We only need to read the part of the ROM that is used for the checksum. + size_t const num = agbsum_chksumoff+0x1u; + + if (fread(buf,0x1u,num,rom) != num) { + fputs("Unable to read ROM\n",stderr); + agbsum_exit(agbsum_stat_err,rom); } } |